✨ Support position autoreporting

Includes proper handling auf cancel/pause situation, where we use the
position report as a signal that all movement has ended and the position
report should be used for final cancel/pause position, adds a new
terminal filter and also takes care of capability report evaluation and
proper interval setting via M154.

cc @thinkyhead - the cancel/pause situation worried me, so I took a look
how to solve it and that meant the implementation was already done as
well. Hope you don't hold it against me to tackle this myself after all.
This commit is contained in:
Gina Häußge committed 2021-05-25 19:50:06 +02:00
1 parent e11cd882e6
commit fcf45f7d59
7 files changed
+190 -68

No files matched your search

@@ -54,6 +54,7 @@ class VirtualPrinterPlugin(
"capabilities": {
"AUTOREPORT_TEMP": True,
"AUTOREPORT_SD_STATUS": True,
"AUTOREPORT_POS": False,
"EMERGENCY_PARSER": True,
"EXTENDED_M20": False,
},
@@ -188,6 +188,7 @@ class VirtualPrinter(object):
self._temperature_reporter = None
self._sdstatus_reporter = None
self._pos_reporter = None
self.current_line = 0
self.lastN = 0
@@ -704,26 +705,7 @@ class VirtualPrinter(object):
# noinspection PyUnusedLocal
def _gcode_M114(self, data):
# type: (str) -> bool
m114FormatString = self._settings.get(["m114FormatString"])
e = {index: value for index, value in enumerate(self._lastE)}
e["current"] = self._lastE[self.currentExtruder]
e["all"] = " ".join(
[
"E{}:{}".format(num, self._lastE[self.currentExtruder])
for num in range(self.extruderCount)
]
)
output = m114FormatString.format(
x=self._lastX,
y=self._lastY,
z=self._lastZ,
e=e,
f=self._lastF,
a=int(self._lastX * 100),
b=int(self._lastY * 100),
c=int(self._lastZ * 100),
)
output = self._generatePositionOutput()
if not self._okBeforeCommandOutput:
ok = self._ok()
if ok:
@@ -750,6 +732,22 @@ class VirtualPrinter(object):
except AttributeError:
self._send("echo:")
def _gcode_M154(self, data):
# type: (str) -> None
matchS = re.search(r"S([0-9]+)", data)
if matchS is not None:
interval = int(matchS.group(1))
if self._pos_reporter is not None:
self._pos_reporter.cancel()
if interval > 0:
self._pos_reporter = RepeatedTimer(
interval, lambda: self._send(self._generatePositionOutput())
)
self._pos_reporter.start()
else:
self._pos_reporter = None
def _gcode_M155(self, data):
# type: (str) -> None
matchS = re.search(r"S([0-9]+)", data)
@@ -1501,6 +1499,29 @@ class VirtualPrinter(object):
else:
self._send("Not SD printing")
def _generatePositionOutput(self):
# type: () -> str
m114FormatString = self._settings.get(["m114FormatString"])
e = {index: value for index, value in enumerate(self._lastE)}
e["current"] = self._lastE[self.currentExtruder]
e["all"] = " ".join(
[
"E{}:{}".format(num, self._lastE[self.currentExtruder])
for num in range(self.extruderCount)
]
)
output = m114FormatString.format(
x=self._lastX,
y=self._lastY,
z=self._lastZ,
e=e,
f=self._lastF,
a=int(self._lastX * 100),
b=int(self._lastY * 100),
c=int(self._lastZ * 100),
)
return output
def _generateTemperatureOutput(self):
# type: () -> str
if self._settings.get_boolean(["repetierStyleTargetTemperature"]):
+15
View File
@@ -189,6 +189,7 @@ def getSettings():
"timeoutSdStatusAutoreport": s.getFloat(
["serial", "timeout", "sdStatusAutoreport"]
),
"timeoutPosAutoreport": s.getFloat(["serial", "timeout", "posAutoreport"]),
"timeoutBaudrateDetectionPause": s.getFloat(
["serial", "timeout", "baudrateDetectionPause"]
),
@@ -249,6 +250,9 @@ def getSettings():
"capAutoreportSdStatus": s.getBoolean(
["serial", "capabilities", "autoreport_sdstatus"]
),
"capAutoreportPos": s.getBoolean(
["serial", "capabilities", "autoreport_pos"]
),
"capBusyProtocol": s.getBoolean(["serial", "capabilities", "busy_protocol"]),
"capEmergencyParser": s.getBoolean(
["serial", "capabilities", "emergency_parser"]
@@ -722,6 +726,12 @@ def _saveSettings(data):
data["serial"]["timeoutSdStatusAutoreport"],
min=0.0,
)
if "timeoutPosAutoreport" in data["serial"]:
s.setFloat(
["serial", "timeout", "posAutoreport"],
data["serial"]["timeoutPosAutoreport"],
min=0.0,
)
if "timeoutBaudrateDetectionPause" in data["serial"]:
s.setFloat(
["serial", "timeout", "baudrateDetectionPause"],
@@ -904,6 +914,11 @@ def _saveSettings(data):
["serial", "capabilities", "autoreport_sdstatus"],
data["serial"]["capAutoreportSdStatus"],
)
if "capAutoreportPos" in data["serial"]:
s.setBoolean(
["serial", "capabilities", "autoreport_pos"],
data["serial"]["capAutoreportPos"],
)
if "capBusyProtocol" in data["serial"]:
s.setBoolean(
["serial", "capabilities", "busy_protocol"],
+6
View File
@@ -124,6 +124,7 @@ default_settings = {
"temperatureAutoreport": 2,
"sdStatus": 1,
"sdStatusAutoreport": 1,
"posAutoreport": 5,
"resendOk": 0.5,
"baudrateDetectionPause": 1.0,
"positionLogWait": 10.0,
@@ -178,6 +179,7 @@ default_settings = {
"capabilities": {
"autoreport_temp": True,
"autoreport_sdstatus": True,
"autoreport_pos": True,
"busy_protocol": True,
"emergency_parser": True,
"extended_m20": True,
@@ -437,6 +439,10 @@ default_settings = {
"name": "Suppress SD status messages",
"regex": r"(Send: (N\d+\s+)?M27)|(Recv: SD printing byte)|(Recv: Not SD printing)",
},
{
"name": "Suppress position messages",
"regex": r"(Send:\s+(N\d+\s+)?M114)|(Recv:\s+(ok\s+)?X:[+-]?([0-9]*[.])?[0-9]+\s+Y:[+-]?([0-9]*[.])?[0-9]+\s+Z:[+-]?([0-9]*[.])?[0-9]+\s+E\d*:[+-]?([0-9]*[.])?[0-9]+).*",
},
{"name": "Suppress wait responses", "regex": r"Recv: wait"},
{
"name": "Suppress processing responses",
@@ -197,6 +197,7 @@ $(function () {
self.serial_timeoutTemperatureAutoreport = ko.observable(undefined);
self.serial_timeoutSdStatus = ko.observable(undefined);
self.serial_timeoutSdStatusAutoreport = ko.observable(undefined);
self.serial_timeoutPosAutoreport = ko.observable(undefined);
self.serial_timeoutBaudrateDetectionPause = ko.observable(undefined);
self.serial_timeoutPositionLogWait = ko.observable(undefined);
self.serial_log = ko.observable(undefined);
@@ -235,6 +236,7 @@ $(function () {
self.serial_maxTimeoutsLong = ko.observable(undefined);
self.serial_capAutoreportTemp = ko.observable(undefined);
self.serial_capAutoreportSdStatus = ko.observable(undefined);
self.serial_capAutoreportPos = ko.observable(undefined);
self.serial_capBusyProtocol = ko.observable(undefined);
self.serial_capEmergencyParser = ko.observable(undefined);
self.serial_capExtendedM20 = ko.observable(undefined);
@@ -155,6 +155,16 @@
<span class="help-inline">{{ _('Autoreport interval to request from firmware')}}</span>
</div>
</div>
<div class="control-group" title="{{ _('Position interval to request from autoreport capable firmwares. A value of 0 disables autoreporting by the firmware.')|edq }}">
<label class="control-label" for="settings-serialTimeoutPosAutoreport">{{ _('Position autoreport interval') }}</label>
<div class="controls">
<div class="input-append">
<input type="number" step="any" min="0" class="input-mini text-right" data-bind="value: serial_timeoutPosAutoreport" id="settings-serialTimeoutPosAutoreport">
<span class="add-on">s</span>
</div>
<span class="help-inline">{{ _('Autoreport interval to request from firmware')}}</span>
</div>
</div>
</fieldset>
<fieldset id="settings_serial_timeouts_timeouts">
<legend>{{ _('Timeouts') }}</legend>
@@ -351,6 +361,13 @@
</label>
</div>
</div>
<div class="control-group">
<div class="controls">
<label class="checkbox">
<input type="checkbox" data-bind="checked: serial_capAutoreportPos" id="settings-serialCapAutoreportPos"> {{ _('Support position autoreporting by firmware, if detected') }}
</label>
</div>
</div>
<div class="control-group">
<div class="controls">
<label class="checkbox">
+108 -48
View File
@@ -467,6 +467,7 @@ class MachineCom(object):
CAPABILITY_AUTOREPORT_TEMP = "AUTOREPORT_TEMP"
CAPABILITY_AUTOREPORT_SD_STATUS = "AUTOREPORT_SD_STATUS"
CAPABILITY_AUTOREPORT_POS = "AUTOREPORT_POS"
CAPABILITY_BUSY_PROTOCOL = "BUSY_PROTOCOL"
CAPABILITY_EMERGENCY_PARSER = "EMERGENCY_PARSER"
CAPABILITY_CHAMBER_TEMP = "CHAMBER_TEMPERATURE"
@@ -581,6 +582,9 @@ class MachineCom(object):
self.CAPABILITY_AUTOREPORT_SD_STATUS: settings().getBoolean(
["serial", "capabilities", "autoreport_sdstatus"]
),
self.CAPABILITY_AUTOREPORT_POS: settings().getBoolean(
["serial", "capabilities", "autoreport_pos"]
),
self.CAPABILITY_BUSY_PROTOCOL: settings().getBoolean(
["serial", "capabilities", "busy_protocol"]
),
@@ -616,6 +620,7 @@ class MachineCom(object):
self._temperature_autoreporting = False
self._sdstatus_autoreporting = False
self._pos_autoreporting = False
self._busy_protocol_detected = False
self._busy_protocol_support = False
@@ -1655,6 +1660,14 @@ class MachineCom(object):
tags=None,
external_sd=False,
):
cancel_tags = {"trigger:comm.cancel", "trigger:cancel"}
abort_heatup_tags = cancel_tags | {
"trigger:abort_heatup",
}
record_position_tags = cancel_tags | {
"trigger:record_position",
}
if tags is None:
tags = set()
@@ -1679,42 +1692,47 @@ class MachineCom(object):
self.cancelFileTransfer()
return
def _on_M400_sent():
# we don't call on_print_job_cancelled on our callback here
# because we do this only after our M114 has been answered
# by the firmware
self._record_cancel_data = True
with self._cancel_mutex:
if self._cancel_position_timer is not None:
self._cancel_position_timer.cancel()
self._cancel_position_timer = ResettableTimer(
self._timeout_intervals.get("positionLogWait", 10.0),
self._cancel_preparation_failed,
)
self._cancel_position_timer.daemon = True
self._cancel_position_timer.start()
self.sendCommand(
"M114",
part_of_job=True,
tags=tags
| {"trigger:comm.cancel", "trigger:cancel", "trigger:record_position"},
)
self._callback.on_comm_print_job_cancelling(
firmware_error=firmware_error, user=user
)
with self._jobLock:
pos_autoreporting = self._pos_autoreporting
self._changeState(self.STATE_CANCELLING)
def _reenable_pos_autoreport():
if pos_autoreporting:
self._set_autoreport_pos_interval(tags=record_position_tags)
def _on_M400_sent():
# we don't call on_print_job_cancelled on our callback here
# because we do this only after our M114 has been answered
# by the firmware
self._record_cancel_data = True
with self._cancel_mutex:
if self._cancel_position_timer is not None:
self._cancel_position_timer.cancel()
self._cancel_position_timer = ResettableTimer(
self._timeout_intervals.get("positionLogWait", 10.0),
self._cancel_preparation_failed,
)
self._cancel_position_timer.daemon = True
self._cancel_position_timer.start()
self.sendCommand(
"M114",
on_sent=_reenable_pos_autoreport,
part_of_job=True,
tags=tags | record_position_tags,
)
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:cancel", "trigger:abort_heatup"},
tags=tags | abort_heatup_tags,
force=True,
)
@@ -1723,33 +1741,35 @@ class MachineCom(object):
self.sendCommand(
"M25",
part_of_job=True,
tags=tags | {"trigger:comm.cancel", "trigger:cancel"},
tags=tags | cancel_tags,
) # pause print
self.sendCommand(
"M27",
part_of_job=True,
tags=tags | {"trigger:comm.cancel", "trigger:cancel"},
tags=tags | cancel_tags,
) # get current byte position in file
self.sendCommand(
"M26 S0",
part_of_job=True,
tags=tags | {"trigger:comm.cancel", "trigger:cancel"},
tags=tags | cancel_tags,
) # reset position in file to byte 0
if self._log_position_on_cancel and not disable_log_position:
with self._action_users_mutex:
self._action_users["cancel"] = user
# disable position autoreporting if enabled
if pos_autoreporting:
self._set_autoreport_pos_interval(
interval=0,
part_of_job=True,
tags=tags | record_position_tags,
)
self.sendCommand(
"M400",
on_sent=_on_M400_sent,
part_of_job=True,
tags=tags
| {
"trigger:comm.cancel",
"trigger:cancel",
"trigger:record_position",
},
tags=tags | record_position_tags,
)
self._continue_sending()
else:
@@ -1799,6 +1819,12 @@ class MachineCom(object):
if not self._currentFile:
return
pause_tags = {"trigger:comm.set_pause", "trigger:pause"}
resume_tags = {"trigger:comm.set_pause", "trigger:resume"}
record_position_tags = pause_tags | {
"trigger:record_position",
}
if tags is None:
tags = set()
@@ -1822,6 +1848,7 @@ class MachineCom(object):
return
with self._jobLock:
pos_autoreporting = self._pos_autoreporting
if not pause and self._state in valid_paused_states:
if self._pauseWaitStartTime:
self._pauseWaitTimeLost = self._pauseWaitTimeLost + (
@@ -1839,12 +1866,12 @@ class MachineCom(object):
self.sendCommand(
"M24",
part_of_job=True,
tags=tags | {"trigger:comm.set_pause", "trigger:resume"},
tags=tags | resume_tags,
)
self.sendCommand(
"M27",
part_of_job=True,
tags=tags | {"trigger:comm.set_pause", "trigger:resume"},
tags=tags | resume_tags,
)
def finalize():
@@ -1865,9 +1892,13 @@ class MachineCom(object):
self.sendCommand(
"M25",
part_of_job=True,
tags=tags | {"trigger:comm.set_pause", "trigger:pause"},
tags=tags | pause_tags,
) # pause print
def _reenable_pos_autoreport():
if pos_autoreporting:
self._set_autoreport_pos_interval(tags=record_position_tags)
def _on_M400_sent():
# we don't call on_print_job_paused on our callback here
# because we do this only after our M114 has been answered
@@ -1886,29 +1917,27 @@ class MachineCom(object):
self._pause_position_timer.start()
self.sendCommand(
"M114",
on_sent=_reenable_pos_autoreport,
part_of_job=True,
tags=tags
| {
"trigger:comm.set_pause",
"trigger:pause",
"trigger:record_position",
},
tags=tags | record_position_tags,
)
if self._log_position_on_pause and local_handling:
with self._action_users_mutex:
self._action_users["pause"] = user
# disable position autoreporting if enabled
if pos_autoreporting:
self._set_autoreport_pos_interval(
interval=0,
part_of_job=True,
tags=tags | record_position_tags,
)
self.sendCommand(
"M400",
on_sent=_on_M400_sent,
part_of_job=True,
tags=tags
| {
"trigger:comm.set_pause",
"trigger:pause",
"trigger:record_position",
},
tags=tags | record_position_tags,
)
self._continue_sending()
else:
@@ -2745,6 +2774,11 @@ class MachineCom(object):
"Firmware states that it supports sd status autoreporting"
)
self._set_autoreport_sdstatus_interval()
elif capability == self.CAPABILITY_AUTOREPORT_POS and enabled:
self._logger.info(
"Firmware states that it supports position autoreporting"
)
self._set_autoreport_pos_interval()
elif (
capability == self.CAPABILITY_EMERGENCY_PARSER and enabled
):
@@ -3477,6 +3511,20 @@ class MachineCom(object):
tags={"trigger:comm.set_autoreport_sdstatus_interval"},
)
def _set_autoreport_pos_interval(self, interval=None, part_of_job=False, tags=None):
if tags is None:
tags = set()
if interval is None:
try:
interval = int(self._timeout_intervals.get("posAutoreport", 5))
except Exception:
interval = 5
self.sendCommand(
"M154 S{}".format(interval),
part_of_job=part_of_job,
tags=tags | {"trigger:comm.set_autoreport_pos_interval"},
)
def _set_busy_protocol_interval(self, interval=None, callback=None):
if interval is None:
try:
@@ -3549,6 +3597,8 @@ class MachineCom(object):
self._set_autoreport_temperature_interval()
if self._sdstatus_autoreporting:
self._set_autoreport_sdstatus_interval()
if self._pos_autoreporting:
self._set_autoreport_pos_interval()
if self._busy_protocol_support:
self._set_busy_protocol_interval()
@@ -5139,6 +5189,16 @@ class MachineCom(object):
self.CAPABILITY_AUTOREPORT_SD_STATUS, False
) and (interval > 0)
def _gcode_M154_sending(
self, cmd, cmd_type=None, gcode=None, subcode=None, *args, **kwargs
):
match = regexes_parameters["intS"].search(cmd)
if match:
interval = int(match.group("value"))
self._pos_autoreporting = self._firmware_capabilities.get(
self.CAPABILITY_AUTOREPORT_SD_STATUS, False
) and (interval > 0)
def _gcode_M33_sending(
self, cmd, cmd_type=None, gcode=None, subcode=None, *args, **kwargs
):