Switch to lower comm timeouts on busy protocol detection

If the printer supports the busy protocol (which we should automatically
notice by it using it on the first blocking heatup or otherwise blocking
operation), it's safe reduce the communication timeout to a way way
lower value since the busy protocol should guarantee a heartbeat from
the printer at known intervals and so timeouts really should only happen
if something gets lost on the way.

We also set the interval on the printer to our configured busy timeout
minus 1s so we can be sure that we should always see a busy line from
the printer before we run into our own timeout. We do this via M113.
This commit is contained in:
Gina Häußge committed 2018-02-15 11:56:28 +01:00
1 parent 6afb291440
commit 7d3d3408eb
6 files changed
+77 -19

No files matched your search

@@ -114,6 +114,7 @@ class VirtualPrinter(object):
self._sendWait = settings().getBoolean(["devel", "virtualPrinter", "sendWait"])
self._sendBusy = settings().getBoolean(["devel", "virtualPrinter", "sendBusy"])
self._waitInterval = settings().getFloat(["devel", "virtualPrinter", "waitInterval"])
self._busyInterval = settings().getFloat(["devel", "virtualPrinter", "busyInterval"])
self._echoOnM117 = settings().getBoolean(["devel", "virtualPrinter", "echoOnM117"])
@@ -427,6 +428,11 @@ class VirtualPrinter(object):
filename = data.split(None, 1)[1].strip()
self._deleteSdFile(filename)
def _gcode_M113(self, data):
interval = int(re.search("S([0-9]+)", data).group(1))
if 0 <= interval <= 60:
self._busyInterval = interval
def _gcode_M114(self, data):
if settings().getBoolean(["devel", "virtualPrinter", "reprapfwM114"]):
output = "X:{} Y:{} Z:{} {}".format(self._lastX,
@@ -536,10 +542,10 @@ class VirtualPrinter(object):
elif matchS:
_timeout = float(matchS.group(1))
if self._sendBusy:
if self._sendBusy and self._busyInterval > 0:
until = time.time() + _timeout
while time.time() < until:
time.sleep(1.0)
time.sleep(self._busyInterval)
self._send("busy:processing")
else:
time.sleep(_timeout)
@@ -1035,19 +1041,26 @@ class VirtualPrinter(object):
def _waitForHeatup(self, heater, only_wait_if_higher):
delta = 1
delay = 1
last_busy = time.time()
try:
if heater.startswith("tool"):
toolNum = int(heater[len("tool"):])
while not self._killed and (self.temp[toolNum] < self.targetTemp[toolNum] - delta or (not only_wait_if_higher and self.temp[toolNum] > self.targetTemp[toolNum] + delta)):
self._simulateTemps(delta=delta)
self._output("T:%0.2f" % self.temp[toolNum])
time.sleep(delay)
test = lambda: self.temp[toolNum] < self.targetTemp[toolNum] - delta or (not only_wait_if_higher and self.temp[toolNum] > self.targetTemp[toolNum] + delta)
output = lambda: "T:%0.2f" % self.temp[toolNum]
elif heater == "bed":
while not self._killed and (self.bedTemp < self.bedTargetTemp - delta or (not only_wait_if_higher and self.bedTemp > self.bedTargetTemp + delta)):
self._simulateTemps(delta=delta)
self._output("B:%0.2f" % self.bedTemp)
time.sleep(delay)
test = lambda: self.bedTemp < self.bedTargetTemp - delta or (not only_wait_if_higher and self.bedTemp > self.bedTargetTemp + delta)
output = lambda: "B:%0.2f" % self.bedTemp
else:
return
while not self._killed and test():
self._simulateTemps(delta=delta)
self._output(output())
if self._sendBusy and time.time() - last_busy >= self._busyInterval:
self._output("echo:busy: processing")
last_busy = time.time()
time.sleep(delay)
except AttributeError:
if self.outgoing is not None:
raise
+2
View File
@@ -142,6 +142,7 @@ def getSettings():
"timeoutConnection": s.getFloat(["serial", "timeout", "connection"]),
"timeoutDetection": s.getFloat(["serial", "timeout", "detection"]),
"timeoutCommunication": s.getFloat(["serial", "timeout", "communication"]),
"timeoutCommunicationBusy": s.getFloat(["serial", "timeout", "communicationBusy"]),
"timeoutTemperature": s.getFloat(["serial", "timeout", "temperature"]),
"timeoutTemperatureTargetSet": s.getFloat(["serial", "timeout", "temperatureTargetSet"]),
"timeoutTemperatureAutoreport": s.getFloat(["serial", "timeout", "temperatureAutoreport"]),
@@ -368,6 +369,7 @@ def _saveSettings(data):
if "timeoutConnection" in data["serial"]: s.setFloat(["serial", "timeout", "connection"], data["serial"]["timeoutConnection"])
if "timeoutDetection" in data["serial"]: s.setFloat(["serial", "timeout", "detection"], data["serial"]["timeoutDetection"])
if "timeoutCommunication" in data["serial"]: s.setFloat(["serial", "timeout", "communication"], data["serial"]["timeoutCommunication"])
if "timeoutCommunicationBusy" in data["serial"]: s.setFloat(["serial", "timeout", "communicationBusy"], data["serial"]["timeoutCommunicationBusy"])
if "timeoutTemperature" in data["serial"]: s.setFloat(["serial", "timeout", "temperature"], data["serial"]["timeoutTemperature"])
if "timeoutTemperatureTargetSet" in data["serial"]: s.setFloat(["serial", "timeout", "temperatureTargetSet"], data["serial"]["timeoutTemperatureTargetSet"])
if "timeoutTemperatureAutoreport" in data["serial"]: s.setFloat(["serial", "timeout", "temperatureAutoreport"], data["serial"]["timeoutTemperatureAutoreport"])
+2
View File
@@ -94,6 +94,7 @@ default_settings = {
"detection": 0.5,
"connection": 10,
"communication": 30,
"communicationBusy": 3,
"temperature": 5,
"temperatureTargetSet": 2,
"temperatureAutoreport": 2,
@@ -383,6 +384,7 @@ default_settings = {
"firmwareName": "Virtual Marlin 1.0",
"sharedNozzle": False,
"sendBusy": False,
"busyInterval": 2.0,
"simulateReset": True,
"resetLines": ['start', 'Marlin: Virtual Marlin!', '\x80', 'SD card ok'],
"preparedOks": [],
@@ -160,6 +160,7 @@ $(function() {
self.serial_timeoutConnection = ko.observable(undefined);
self.serial_timeoutDetection = ko.observable(undefined);
self.serial_timeoutCommunication = ko.observable(undefined);
self.serial_timeoutCommunicationBusy = ko.observable(undefined);
self.serial_timeoutTemperature = ko.observable(undefined);
self.serial_timeoutTemperatureTargetSet = ko.observable(undefined);
self.serial_timeoutTemperatureAutoreport = ko.observable(undefined);
@@ -61,6 +61,14 @@
<input type="number" step="any" min="0" class="input-mini text-right" data-bind="value: serial_timeoutCommunication" id="settings-serialTimeoutCommunication">
<span class="add-on">s</span>
</div>
<span class="help-inline">{{ _('<code>busy</code> protocol support not detected')}}</span>
</div>
<div class="controls">
<div class="input-append">
<input type="number" step="any" min="0" class="input-mini text-right" data-bind="value: serial_timeoutCommunicationBusy" id="settings-serialTimeoutCommunicationBusy">
<span class="add-on">s</span>
</div>
<span class="help-inline">{{ _('<code>busy</code> protocol support detected')}}</span>
</div>
</div>
<div class="control-group" title="{{ _('Time after which a connection attempt to the printer will be considered as having failed') }}">
+41 -9
View File
@@ -420,6 +420,7 @@ class MachineCom(object):
self._firmware_capabilities = dict()
self._temperature_autoreporting = False
self._busy_protocol_detected = False
self._supportResendsWithoutOk = settings().getBoolean(["serial", "supportResendsWithoutOk"])
@@ -1229,7 +1230,7 @@ class MachineCom(object):
self._changeState(self.STATE_CONNECTING)
#Start monitoring the serial port.
self._timeout = get_new_timeout("communication", self._timeout_intervals)
self._timeout = get_new_timeout("communicationBusy" if self._busy_protocol_detected else "communication", self._timeout_intervals)
startSeen = False
supportRepetierTargetTemp = settings().getBoolean(["feature", "repetierTargetTemp"])
@@ -1250,13 +1251,33 @@ class MachineCom(object):
break
if line.strip() is not "":
self._consecutive_timeouts = 0
self._timeout = get_new_timeout("communication", self._timeout_intervals)
self._timeout = get_new_timeout("communicationBusy" if self._busy_protocol_detected else "communication", self._timeout_intervals)
if self._dwelling_until and now > self._dwelling_until:
self._dwelling_until = False
##~~ busy protocol handling
if line.startswith("echo:busy:") or line.startswith("busy:"):
# make sure the printer sends busy in a small enough interval to match our timeout
if not self._busy_protocol_detected:
self._log("Printer seems to support the busy protocol, adjusting timeouts and setting busy "
"interval accordingly")
self._busy_protocol_detected = True
new_communication_timeout = self._timeout_intervals.get("communicationBusy", 2)
self._serial.timeout = new_communication_timeout
busy_interval = max(int(new_communication_timeout) - 1, 1)
self._logger.info("Printer seems to support the busy protocol, telling it to set the busy "
"interval to our \"communicationBusy\" timeout - 1s = {}s".format(busy_interval))
self._set_busy_protocol_interval(interval=busy_interval)
if self._state not in (self.STATE_CONNECTING, self.STATE_DETECT_BAUDRATE):
continue
##~~ debugging output handling
if line.startswith("//"):
elif line.startswith("//"):
debugging_output = line[2:].strip()
if debugging_output.startswith("action:"):
action_command = debugging_output[len("action:"):].strip()
@@ -1277,7 +1298,8 @@ class MachineCom(object):
except:
self._logger.exception("Error while calling hook {} with action command {}".format(self._printer_action_hooks[hook], action_command))
continue
else:
if self._state not in (self.STATE_CONNECTING, self.STATE_DETECT_BAUDRATE):
continue
def convert_line(line):
@@ -1488,7 +1510,7 @@ class MachineCom(object):
if capability == self.CAPABILITY_AUTOREPORT_TEMP and enabled:
self._logger.info("Firmware states that it supports temperature autoreporting")
self._set_autoreport_temperature()
self._set_autoreport_temperature_interval()
##~~ SD Card handling
elif 'SD init fail' in line or 'volume.init failed' in line or 'openRoot failed' in line:
@@ -1594,7 +1616,7 @@ class MachineCom(object):
self._serial.timeout = connection_timeout
self._log("Trying baudrate: %d" % (baudrate))
self._baudrateDetectRetry = 5
self._timeout = get_new_timeout("communication", self._timeout_intervals)
self._timeout = get_new_timeout("communicationBusy" if self._busy_protocol_detected else "communication", self._timeout_intervals)
self._serial.write('\n')
self.sayHello()
except:
@@ -1830,7 +1852,7 @@ class MachineCom(object):
if self.isOperational() and not self._connection_closing and self.isSdPrinting() and not self._long_running_command and not self._dwelling_until and not self._heating:
self.sendCommand("M27", cmd_type="sd_status_poll")
def _set_autoreport_temperature(self, interval=None):
def _set_autoreport_temperature_interval(self, interval=None):
if interval is None:
try:
interval = int(self._timeout_intervals.get("temperatureAutoreport", 2))
@@ -1838,6 +1860,14 @@ class MachineCom(object):
interval = 2
self.sendCommand("M155 S{}".format(interval))
def _set_busy_protocol_interval(self, interval=None):
if interval is None:
try:
interval = max(int(self._timeout_intervals.get("communicationBusy", 3)) - 1, 1)
except:
interval = 2
self.sendCommand("M113 S{}".format(interval))
def _onConnected(self):
self._serial.timeout = settings().getFloat(["serial", "timeout", "communication"])
self._temperature_timer = RepeatedTimer(self._getTemperatureTimerInterval, self._poll_temperature, run_first=True)
@@ -1862,7 +1892,9 @@ class MachineCom(object):
self.resetLineNumbers()
if self._temperature_autoreporting:
self._set_autoreport_temperature()
self._set_autoreport_temperature_interval()
if self._busy_protocol_detected:
self._set_busy_protocol_interval()
def _getTemperatureTimerInterval(self):
busy_default = 4.0
@@ -2803,7 +2835,7 @@ class MachineCom(object):
elif s_match:
_timeout = float(s_match.group("value"))
self._timeout = get_new_timeout("communication", self._timeout_intervals) + _timeout
self._timeout = get_new_timeout("communicationBusy" if self._busy_protocol_detected else "communication", self._timeout_intervals) + _timeout
self._dwelling_until = time.time() + _timeout
##~~ command phase handlers