diff --git a/src/octoprint/plugins/virtual_printer/virtual.py b/src/octoprint/plugins/virtual_printer/virtual.py index a3991ccff..a0d490fc3 100644 --- a/src/octoprint/plugins/virtual_printer/virtual.py +++ b/src/octoprint/plugins/virtual_printer/virtual.py @@ -88,7 +88,7 @@ class VirtualPrinter(object): self._lastX = 0.0 self._lastY = 0.0 self._lastZ = 0.0 - self._lastE = 0.0 + self._lastE = [0.0] * self.extruderCount self._lastF = 200 self._unitModifier = 1 @@ -428,7 +428,19 @@ class VirtualPrinter(object): self._deleteSdFile(filename) def _gcode_M114(self, data): - output = "X:{} Y:{} Z:{} E:{} Count: A:{} B:{} C:{}".format(self._lastX, self._lastY, self._lastZ, self._lastE, int(self._lastX*100), int(self._lastY*100), int(self._lastZ*100)) + if settings().getBoolean(["devel", "virtualPrinter", "reprapfwM114"]): + output = "X:{} Y:{} Z:{} {}".format(self._lastX, + self._lastY, + self._lastZ, + " ".join(["E{}:{}".format(num, self._lastE[self.currentExtruder]) for num in range(self.extruderCount)])) + else: + output = "X:{} Y:{} Z:{} E:{} Count: A:{} B:{} C:{}".format(self._lastX, + self._lastY, + self._lastZ, + self._lastE[self.currentExtruder], + int(self._lastX*100), + int(self._lastY*100), + int(self._lastZ*100)) if not self._okBeforeCommandOutput: ok = self._ok() if ok: @@ -482,7 +494,7 @@ class VirtualPrinter(object): if self._lastZ is not None: self._lastZ *= 2.54 if self._lastE is not None: - self._lastE *= 2.54 + self._lastE = [e * 2.54 if e is not None else None for e in self._lastE] def _gcode_G21(self, data): self._unitModifier = 1.0 @@ -493,7 +505,7 @@ class VirtualPrinter(object): if self._lastZ is not None: self._lastZ /= 2.54 if self._lastE is not None: - self._lastE /= 2.54 + self._lastE = [e / 2.54 if e is not None else None for e in self._lastE] def _gcode_G90(self, data): self._relative = False @@ -894,15 +906,16 @@ class VirtualPrinter(object): if matchE is not None: try: e = float(matchE.group(1)) - if self._relative or self._lastE is None: + lastE = self._lastE[self.currentExtruder] + if self._relative or lastE is None: duration = max(duration, e * self._unitModifier / speedE * 60.0) else: - duration = max(duration, (e - self._lastE) * self._unitModifier / speedE * 60.0) + duration = max(duration, (e - lastE) * self._unitModifier / speedE * 60.0) - if self._relative and self._lastE is not None: - self._lastE += e + if self._relative and lastE is not None: + self._lastE[self.currentExtruder] += e else: - self._lastE = e + self._lastE[self.currentExtruder] = e except: pass @@ -922,7 +935,7 @@ class VirtualPrinter(object): matchE = re.search("E([0-9.]+)", line) if matchX is None and matchY is None and matchZ is None and matchE is None: - self._lastX = self._lastY = self._lastZ = self._lastE = 0 + self._lastX = self._lastY = self._lastZ = self._lastE[self.currentExtruder] = 0 else: if matchX is not None: try: @@ -941,7 +954,7 @@ class VirtualPrinter(object): pass if matchE is not None: try: - self._lastE = float(matchE.group(1)) + self._lastE[self.currentExtruder] = float(matchE.group(1)) except: pass diff --git a/src/octoprint/settings.py b/src/octoprint/settings.py index 350c63bf9..d2929b013 100644 --- a/src/octoprint/settings.py +++ b/src/octoprint/settings.py @@ -367,6 +367,7 @@ default_settings = { "repetierStyleResends": False, "okBeforeCommandOutput": False, "smoothieTemperatureReporting": False, + "reprapfwM114": False, "extendedSdFileList": False, "throttle": 0.01, "sendWait": True, diff --git a/src/octoprint/util/comm.py b/src/octoprint/util/comm.py index e3fb219d3..09a3834fa 100644 --- a/src/octoprint/util/comm.py +++ b/src/octoprint/util/comm.py @@ -118,7 +118,7 @@ Groups will be as follows: * ``target``: new target temperature (float) """ -regex_position = re.compile("X:(?P{float})\s*Y:(?P{float})\s*Z:(?P{float})\s*E:(?P{float})".format(float=regex_float_pattern)) +regex_position = re.compile("X:(?P{float})\s*Y:(?P{float})\s*Z:(?P{float})\s*((E:(?P{float}))|(?P(E\d+:{float}\s*)+))".format(float=regex_float_pattern)) """Regex for matching position reporting. Groups will be as follows: @@ -126,7 +126,17 @@ Groups will be as follows: * ``x``: X coordinate * ``y``: Y coordinate * ``z``: Z coordinate - * ``e``: E coordinate + * ``e``: E coordinate if present, or + * ``es``: multiple E coordinates if present, to be parsed further with regex_e_positions +""" + +regex_e_positions = re.compile("E(?P\d+):(?P{float})".format(float=regex_float_pattern)) +"""Regex for matching multiple E coordinates in a position report. + +Groups will be as follows: + + * ``id``: id of the extruder or which the position is reported + * ``value``: reported position value """ regex_firmware_splitter = re.compile("\s*([A-Z0-9_]+):") @@ -218,29 +228,39 @@ gcodeToEvent = { } class PositionRecord(object): + _standard_attrs = {"x", "y", "z", "e", "f", "t"} + + @classmethod + def valid_e(cls, attr): + if not attr.startswith("e"): + return False + + try: + int(attr[1:]) + except: + return False + + return True + def __init__(self, *args, **kwargs): - self.x = kwargs.get("x") - self.y = kwargs.get("y") - self.z = kwargs.get("z") - self.e = kwargs.get("e") - self.f = kwargs.get("f") - self.t = kwargs.get("t") + attrs = self._standard_attrs | set([key for key in kwargs if self.valid_e(key)]) + for attr in attrs: + setattr(self, attr, kwargs.get(attr)) def copy_from(self, other): - self.x = other.x - self.y = other.y - self.z = other.z - self.e = other.e - self.f = other.f - self.t = other.t + # make sure all standard attrs and attrs from other are set + attrs = self._standard_attrs | set([key for key in dir(other) if self.valid_e(key)]) + for attr in attrs: + setattr(self, attr, getattr(other, attr)) + + # delete attrs other doesn't have + attrs = set([key for key in dir(self) if self.valid_e(key)]) - attrs + for attr in attrs: + delattr(self, attr) def as_dict(self): - return dict(x=self.x, - y=self.y, - z=self.z, - e=self.e, - t=self.t, - f=self.f) + attrs = self._standard_attrs | set([key for key in dir(self) if self.valid_e(key)]) + return dict((attr, getattr(self, attr)) for attr in attrs) class TemperatureRecord(object): def __init__(self): @@ -1306,17 +1326,26 @@ class MachineCom(object): # position report processing if 'X:' in line and 'Y:' in line and 'Z:' in line: - match = regex_position.search(line) - if match: + parsed = parse_position_line(line) + if parsed: # we don't know T or F when printing from SD since # there's no way to query it from the firmware and # no way to track it ourselves when not streaming # the file - this all sucks sooo much self.last_position.valid = True - self.last_position.x = float(match.group("x")) - self.last_position.y = float(match.group("y")) - self.last_position.z = float(match.group("z")) - self.last_position.e = float(match.group("e")) + self.last_position.x = parsed.get("x") + self.last_position.y = parsed.get("y") + self.last_position.z = parsed.get("z") + + if "e" in parsed: + self.last_position.e = parsed.get("e") + else: + # multiple extruder coordinates provided, find current one + self.last_position.e = parsed.get("e{}".format(self._currentTool)) if not self.isSdFileSelected() else None + + for key in [key for key in parsed if key.startswith("e") and len(key) > 1]: + setattr(self.last_position, key, parsed.get(key)) + self.last_position.t = self._currentTool if not self.isSdFileSelected() else None self.last_position.f = self._currentF if not self.isSdFileSelected() else None @@ -3474,6 +3503,41 @@ def parse_resend_line(line): return None +def parse_position_line(line): + """ + Parses the provided M114 response line and returns the parsed coordinates. + + Args: + line (str): the line to parse + + Returns: + dict or None: the parsed coordinates, or None if no coordinates could be parsed + """ + + match = regex_position.search(line) + if match is not None: + result = dict(x=float(match.group("x")), + y=float(match.group("y")), + z=float(match.group("z"))) + if match.group("e") is not None: + # report contains only one E + result["e"] = float(match.group("e")) + + elif match.group("es") is not None: + # report contains individual entries for multiple extruders ("E0:... E1:... E2:...") + es = match.group("es") + for m in regex_e_positions.finditer(es): + result["e{}".format(m.group("id"))] = float(m.group("value")) + + else: + # apparently no E at all, should never happen but let's still handle this + return None + + return result + + return None + + def gcode_command_for_cmd(cmd): """ Tries to parse the provided ``cmd`` and extract the GCODE command identifier from it (e.g. "G0" for "G0 X10.0"). diff --git a/tests/util/test_comm_helpers.py b/tests/util/test_comm_helpers.py index 0da2cf609..10ab72122 100644 --- a/tests/util/test_comm_helpers.py +++ b/tests/util/test_comm_helpers.py @@ -297,3 +297,102 @@ class TestCommHelpers(unittest.TestCase): from octoprint.util.comm import parse_resend_line result = parse_resend_line(line) self.assertEqual(expected, result) + + @data( + # Marlin + ("ok X:62.417 Y:64.781 Z:0.2 E:2.72328 Count: A:6241 B:6478 C:20", dict(x=62.417, + y=64.781, + z=0.2, + e=2.72328)), + ("X:62.417 Y:64.781 Z:0.2 E:2.72328 Count: A:6241 B:6478 C:20", dict(x=62.417, + y=64.781, + z=0.2, + e=2.72328)), + + # RepRapFirmware + ("X:96.99 Y:88.31 Z:0.30 E0:0.0 E1:0.0 E2:0.0 E3:0.0 E4:0.0 E5:0.0", dict(x=96.99, + y=88.31, + z=0.3, + e0=0.0, + e1=0.0, + e2=0.0, + e3=0.0, + e4=0.0, + e5=0.0)), + + # invalid + ("", None), + ("X:62.417 Y:64.781 Z:0.2", None) + ) + @unpack + def test_parse_position_line(self, line, expected): + from octoprint.util.comm import parse_position_line + result = parse_position_line(line) + if expected is None: + self.assertIsNone(result) + else: + self.assertDictEqual(expected, result) + + +class TestPositionRecord(unittest.TestCase): + + def test_as_dict_regular(self): + coords = dict(x=1, y=2, z=3, e=4) + + position = self._create_position(**coords) + + expected = dict(coords) + expected.update(dict(f=None, t=None)) + self.assertDictEqual(position.as_dict(), expected) + + def test_as_dict_extra_e(self): + coords = dict(x=1, y=2, z=3, e0=4, e1=5) + + position = self._create_position(**coords) + + expected = dict(coords) + expected.update(dict(e=None, f=None, t=None)) + self.assertDictEqual(position.as_dict(), expected) + + def test_copy_from_regular(self): + coords = dict(x=1, y=2, z=3, e=4) + position1 = self._create_position(**coords) + position2 = self._create_position() + + position2.copy_from(position1) + + expected = dict(coords) + expected.update(dict(f=None, t=None)) + self.assertDictEqual(position2.as_dict(), expected) + + def test_copy_from_extra_e(self): + coords = dict(x=1, y=2, z=3, e0=4, e1=5) + position1 = self._create_position(**coords) + position2 = self._create_position() + + position2.copy_from(position1) + + expected = dict(coords) + expected.update(dict(e=None, f=None, t=None)) + self.assertDictEqual(position2.as_dict(), expected) + + def test_copy_from_extra_e_changed(self): + coords1 = dict(x=1, y=2, z=3, e0=4, e1=5) + position1 = self._create_position(**coords1) + + coords2 = dict(x=2, y=4, z=6, e0=8, e1=10, e2=12) + position2 = self._create_position(**coords2) + + expected_before = dict(coords2) + expected_before.update(dict(e=None, f=None, t=None)) + self.assertDictEqual(position2.as_dict(), expected_before) + + position2.copy_from(position1) + + expected_after = dict(coords1) + expected_after.update(dict(e=None, f=None, t=None)) + self.assertDictEqual(position2.as_dict(), expected_after) + + def _create_position(self, **kwargs): + from octoprint.util.comm import PositionRecord + return PositionRecord(**kwargs)