From f32ea65b2c65513583bc55de1005e87d6cf95925 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Wed, 26 Sep 2018 15:17:31 +0200 Subject: [PATCH] Support EMERGENCY_PARSER & use M108 on cancel (unless disabled) Implements #1504 --- src/octoprint/server/api/settings.py | 6 +- src/octoprint/settings.py | 9 ++- .../static/js/app/viewmodels/settings.js | 2 + .../dialogs/settings/serialconnection.jinja2 | 18 ++++- src/octoprint/util/__init__.py | 3 +- src/octoprint/util/comm.py | 79 +++++++++++++++---- 6 files changed, 96 insertions(+), 21 deletions(-) diff --git a/src/octoprint/server/api/settings.py b/src/octoprint/server/api/settings.py index 9b68e79e4..4da866326 100644 --- a/src/octoprint/server/api/settings.py +++ b/src/octoprint/server/api/settings.py @@ -154,6 +154,7 @@ def getSettings(): "blockM0M1": s.getBoolean(["serial", "blockM0M1"]), "logPositionOnPause": s.getBoolean(["serial", "logPositionOnPause"]), "logPositionOnCancel": s.getBoolean(["serial", "logPositionOnCancel"]), + "abortHeatupOnCancel": s.getBoolean(["serial", "abortHeatupOnCancel"]), "supportResendsWithoutOk": s.get(["serial", "supportResendsWithoutOk"]), "waitForStart": s.getBoolean(["serial", "waitForStartOnConnect"]), "alwaysSendChecksum": s.getBoolean(["serial", "alwaysSendChecksum"]), @@ -171,7 +172,8 @@ def getSettings(): "maxTimeoutsLong": s.getInt(["serial", "maxCommunicationTimeouts", "long"]), "capAutoreportTemp": s.getBoolean(["serial", "capabilities", "autoreport_temp"]), "capAutoreportSdStatus": s.getBoolean(["serial", "capabilities", "autoreport_sdstatus"]), - "capBusyProtocol": s.getBoolean(["serial", "capabilities", "busy_protocol"]) + "capBusyProtocol": s.getBoolean(["serial", "capabilities", "busy_protocol"]), + "capEmergencyParser": s.getBoolean(["serial", "capabilities", "emergency_parser"]) }, "folder": { "uploads": s.getBaseFolder("uploads"), @@ -448,12 +450,14 @@ def _saveSettings(data): if "blockWhileDwelling" in data["serial"]: s.setBoolean(["serial", "blockWhileDwelling"], data["serial"]["blockWhileDwelling"]) if "logPositionOnPause" in data["serial"]: s.setBoolean(["serial", "logPositionOnPause"], data["serial"]["logPositionOnPause"]) if "logPositionOnCancel" in data["serial"]: s.setBoolean(["serial", "logPositionOnCancel"], data["serial"]["logPositionOnCancel"]) + if "abortHeatupOnCancel" in data["serial"]: s.setBoolean(["serial", "abortHeatupOnCancel"], data["serial"]["abortHeatupOnCancel"]) if "maxTimeoutsIdle" in data["serial"]: s.setInt(["serial", "maxCommunicationTimeouts", "idle"], data["serial"]["maxTimeoutsIdle"]) if "maxTimeoutsPrinting" in data["serial"]: s.setInt(["serial", "maxCommunicationTimeouts", "printing"], data["serial"]["maxTimeoutsPrinting"]) if "maxTimeoutsLong" in data["serial"]: s.setInt(["serial", "maxCommunicationTimeouts", "long"], data["serial"]["maxTimeoutsLong"]) if "capAutoreportTemp" in data["serial"]: s.setBoolean(["serial", "capabilities", "autoreport_temp"], data["serial"]["capAutoreportTemp"]) if "capAutoreportSdStatus" in data["serial"]: s.setBoolean(["serial", "capabilities", "autoreport_sdstatus"], data["serial"]["capAutoreportSdStatus"]) if "capBusyProtocol" in data["serial"]: s.setBoolean(["serial", "capabilities", "busy_protocol"], data["serial"]["capBusyProtocol"]) + if "capEmergencyParser" in data["serial"]: s.setBoolean(["serial", "capabilities", "emergency_parser"], data["serial"]["capEmergencyParser"]) oldLog = s.getBoolean(["serial", "log"]) if "log" in data["serial"]: s.setBoolean(["serial", "log"], data["serial"]["log"]) diff --git a/src/octoprint/settings.py b/src/octoprint/settings.py index 5c24547f9..2364f8d92 100644 --- a/src/octoprint/settings.py +++ b/src/octoprint/settings.py @@ -121,6 +121,7 @@ default_settings = { "supportResendsWithoutOk": "detect", "logPositionOnPause": True, "logPositionOnCancel": False, + "abortHeatupOnCancel": True, "waitForStartOnConnect": False, "alwaysSendChecksum": False, "neverSendChecksum": False, @@ -141,7 +142,8 @@ default_settings = { "capabilities": { "autoreport_temp": True, "autoreport_sdstatus": True, - "busy_protocol": True + "busy_protocol": True, + "emergency_parser": True }, # command specific flags @@ -411,10 +413,11 @@ default_settings = { "preparedOks": [], "okFormatString": "ok", "m115FormatString": "FIRMWARE_NAME:{firmware_name} PROTOCOL_VERSION:1.0", - "m115ReportCapabilities": False, + "m115ReportCapabilities": True, "capabilities": { "AUTOREPORT_TEMP": True, - "AUTOREPORT_SD_STATUS": True + "AUTOREPORT_SD_STATUS": True, + "EMERGENCY_PARSER": True }, "m114FormatString": "X:{x} Y:{y} Z:{z} E:{e[current]} Count: A:{a} B:{b} C:{c}", "ambientTemperature": 21.3, diff --git a/src/octoprint/static/js/app/viewmodels/settings.js b/src/octoprint/static/js/app/viewmodels/settings.js index deae964d5..397c00a46 100644 --- a/src/octoprint/static/js/app/viewmodels/settings.js +++ b/src/octoprint/static/js/app/viewmodels/settings.js @@ -183,12 +183,14 @@ $(function() { self.serial_supportResendsWithoutOk = ko.observable(undefined); self.serial_logPositionOnPause = ko.observable(undefined); self.serial_logPositionOnCancel = ko.observable(undefined); + self.serial_abortHeatupOnCancel = ko.observable(undefined); self.serial_maxTimeoutsIdle = ko.observable(undefined); self.serial_maxTimeoutsPrinting = ko.observable(undefined); self.serial_maxTimeoutsLong = ko.observable(undefined); self.serial_capAutoreportTemp = ko.observable(undefined); self.serial_capAutoreportSdStatus = ko.observable(undefined); self.serial_capBusyProtocol = ko.observable(undefined); + self.serial_capEmergencyParser = ko.observable(undefined); self.folder_uploads = ko.observable(undefined); self.folder_timelapse = ko.observable(undefined); diff --git a/src/octoprint/templates/dialogs/settings/serialconnection.jinja2 b/src/octoprint/templates/dialogs/settings/serialconnection.jinja2 index cdf39635d..2d7cca2a0 100644 --- a/src/octoprint/templates/dialogs/settings/serialconnection.jinja2 +++ b/src/octoprint/templates/dialogs/settings/serialconnection.jinja2 @@ -280,6 +280,13 @@ +
+
+ +
+
- {{ _('Event based position logging') }} + {{ _('Pausing') }}
+
+
+ {{ _('Cancelling') }}
+
+ +
diff --git a/src/octoprint/util/__init__.py b/src/octoprint/util/__init__.py index 07a44eb81..581051299 100644 --- a/src/octoprint/util/__init__.py +++ b/src/octoprint/util/__init__.py @@ -1302,7 +1302,7 @@ class CountedEvent(object): self._event.wait(timeout) def blocked(self): - return self.counter == 0 + return self.counter <= 0 def acquire(self, blocking=1): return self._mutex.acquire(blocking=blocking) @@ -1313,7 +1313,6 @@ class CountedEvent(object): def _internal_set(self, value): self._counter = value if self._counter <= 0: - self._counter = 0 self._event.clear() else: if self._max is not None and self._counter > self._max: diff --git a/src/octoprint/util/comm.py b/src/octoprint/util/comm.py index f8fa1c02f..5b6f23f0f 100644 --- a/src/octoprint/util/comm.py +++ b/src/octoprint/util/comm.py @@ -351,6 +351,7 @@ class MachineCom(object): CAPABILITY_AUTOREPORT_TEMP = "AUTOREPORT_TEMP" CAPABILITY_AUTOREPORT_SD_STATUS = "AUTOREPORT_SD_STATUS" CAPABILITY_BUSY_PROTOCOL = "BUSY_PROTOCOL" + CAPABILITY_EMERGENCY_PARSER = "EMERGENCY_PARSER" CAPABILITY_SUPPORT_ENABLED = "enabled" CAPABILITY_SUPPORT_DETECTED = "detected" @@ -438,7 +439,8 @@ class MachineCom(object): self._capability_support = { self.CAPABILITY_AUTOREPORT_TEMP: settings().getBoolean(["serial", "capabilities", "autoreport_temp"]), self.CAPABILITY_AUTOREPORT_SD_STATUS: settings().getBoolean(["serial", "capabilities", "autoreport_sdstatus"]), - self.CAPABILITY_BUSY_PROTOCOL: settings().getBoolean(["serial", "capabilities", "busy_protocol"]) + self.CAPABILITY_BUSY_PROTOCOL: settings().getBoolean(["serial", "capabilities", "busy_protocol"]), + self.CAPABILITY_EMERGENCY_PARSER: settings().getBoolean(["serial", "capabilities", "emergency_parser"]) } self._lastLines = deque([], 50) @@ -477,7 +479,7 @@ class MachineCom(object): self._long_running_commands = settings().get(["serial", "longRunningCommands"]) self._checksum_requiring_commands = settings().get(["serial", "checksumRequiringCommands"]) - self._clear_to_send = CountedEvent(max=10, name="comm.clear_to_send") + self._clear_to_send = CountedEvent(name="comm.clear_to_send") self._send_queue = SendQueue() self._temperature_timer = None self._sd_status_timer = None @@ -541,6 +543,7 @@ class MachineCom(object): self._log_position_on_pause = settings().getBoolean(["serial", "logPositionOnPause"]) self._log_position_on_cancel = settings().getBoolean(["serial", "logPositionOnCancel"]) + self._abort_heatup_on_cancel = settings().getBoolean(["serial", "abortHeatupOnCancel"]) # print job self._currentFile = None @@ -1197,6 +1200,13 @@ class MachineCom(object): with self._jobLock: self._changeState(self.STATE_CANCELLING) + if self._abort_heatup_on_cancel: + # abort any ongoing heatups immediately to get back control over the printer + self.sendCommand("M108", + part_of_job=False, + tags=tags | {"trigger:comm.cancel", "trigger:abort_heatup"}, + force=True) + if self.isSdFileSelected(): if not external_sd: self.sendCommand("M25", part_of_job=True, tags=tags | {"trigger:comm.cancel",}) # pause print @@ -1825,6 +1835,8 @@ class MachineCom(object): elif capability == self.CAPABILITY_AUTOREPORT_SD_STATUS and enabled: self._logger.info("Firmware states that it supports sd status autoreporting") self._set_autoreport_sdstatus_interval() + elif capability == self.CAPABILITY_EMERGENCY_PARSER and enabled: + self._logger.info("Firmware states that it supports emergency GCODEs M108 and M410 to be sent without waiting for an acknowledgement first") # notify plugins for name, hook in self._firmware_info_hooks["capabilities"].items(): @@ -2982,17 +2994,7 @@ class MachineCom(object): continue # now comes the part where we increase line numbers and send stuff - no turning back now - command_requiring_checksum = gcode is not None and gcode in self._checksum_requiring_commands - command_allowing_checksum = gcode is not None or self._sendChecksumWithUnknownCommands - checksum_enabled = not self._neverSendChecksum and ((self.isPrinting() and self._currentFile and self._currentFile.checksum) or - self._alwaysSendChecksum or - not self._firmware_info_received) - - command_to_send = command.encode("ascii", errors="replace") - if command_requiring_checksum or (command_allowing_checksum and checksum_enabled): - self._do_increment_and_send_with_checksum(command_to_send) - else: - self._do_send_without_checksum(command_to_send) + self._do_send(command, gcode=gcode) # trigger "sent" phase and use up one "ok" if on_sent is not None and callable(on_sent): @@ -3152,6 +3154,24 @@ class MachineCom(object): ##~~ actual sending via serial + def _needs_checksum(self, gcode=None): + command_requiring_checksum = gcode is not None and gcode in self._checksum_requiring_commands + command_allowing_checksum = gcode is not None or self._sendChecksumWithUnknownCommands + return command_requiring_checksum or (command_allowing_checksum and self._checksum_enabled) + + @property + def _checksum_enabled(self): + return not self._neverSendChecksum and ((self.isPrinting() and self._currentFile and self._currentFile.checksum) or + self._alwaysSendChecksum or + not self._firmware_info_received) + + def _do_send(self, command, gcode=None): + command_to_send = command.encode("ascii", errors="replace") + if self._needs_checksum(gcode): + self._do_increment_and_send_with_checksum(command_to_send) + else: + self._do_send_without_checksum(command_to_send) + def _do_increment_and_send_with_checksum(self, cmd): with self._line_mutex: linenumber = self._currentLine @@ -3420,7 +3440,12 @@ class MachineCom(object): self._resendDelta = None def _gcode_M112_queuing(self, cmd, cmd_type=None, gcode=None, subcode=None, *args, **kwargs): - # emergency stop, jump the queue with the M112 + self._logger.info("Force-sending M112 to the printer") + + # emergency stop, jump the queue with the M112, regardless of whether the EMERGENCY_PARSER capability is + # available or not + # + # send the M112 once without and with checksum self._do_send_without_checksum("M112") self._do_increment_and_send_with_checksum("M112") @@ -3448,6 +3473,32 @@ class MachineCom(object): # I hope it got it the first time because as far as I can tell, there is no way to know return None, + def _gcode_M108_queuing(self, cmd, gcode=None, *args, **kwargs): + # only jump the queue with M108 if the EMERGENCY_PARSER capability is available + if not self._capability_support.get(self.CAPABILITY_EMERGENCY_PARSER, False) or not self._firmware_capabilities.get(self.CAPABILITY_EMERGENCY_PARSER, False): + return + + self._logger.info("Force-sending M108 to the printer") + self._do_send(cmd, gcode=gcode) + + # use up an ok since we will get one back for this command and don't want to get out of sync + self._clear_to_send.clear() + + return None, + + def _gcode_M410_queuing(self, cmd, gcode=None, *args, **kwargs): + # only jump the queue with M410 if the EMERGENCY_PARSER capability is available + if not self._capability_support.get(self.CAPABILITY_EMERGENCY_PARSER, False) or not self._firmware_capabilities.get(self.CAPABILITY_EMERGENCY_PARSER, False): + return + + self._logger.info("Force-sending M410 to the printer") + self._do_send(cmd, gcode=gcode) + + # use up an ok since we will get one back for this command and don't want to get out of sync + self._clear_to_send.clear() + + return None, + def _gcode_M114_queued(self, *args, **kwargs): self._reset_position_timers() _gcode_M114_sent = _gcode_M114_queued