gcode: Catch common gcode parameter errors

Don't force a firmware shutdown on a simple gcode parse error.
Instead, report the error back to the user and otherwise ignore the
command.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2017-03-15 23:40:46 -04:00
parent d2027cb4a9
commit cbdc54843d
1 changed files with 58 additions and 28 deletions

View File

@ -115,6 +115,8 @@ class GCodeParser:
handler = self.gcode_handlers.get(cmd, self.cmd_default) handler = self.gcode_handlers.get(cmd, self.cmd_default)
try: try:
handler(params) handler(params)
except error, e:
self.respond_error(str(e))
except: except:
logging.exception("Exception in command handler") logging.exception("Exception in command handler")
self.toolhead.force_shutdown() self.toolhead.force_shutdown()
@ -168,6 +170,27 @@ class GCodeParser:
if len(lines) > 1: if len(lines) > 1:
self.respond_info("\n".join(lines[:-1])) self.respond_info("\n".join(lines[:-1]))
self.respond('!! %s' % (lines[-1].strip(),)) self.respond('!! %s' % (lines[-1].strip(),))
# Parameter parsing helpers
def get_int(self, name, params, default=None):
if name in params:
try:
return int(params[name])
except ValueError:
raise error("Error on '%s': unable to parse %s" % (
params['#original'], params[name]))
if default is not None:
return default
raise error("Error on '%s': missing %s" % (params['#original'], name))
def get_float(self, name, params, default=None):
if name in params:
try:
return float(params[name])
except ValueError:
raise error("Error on '%s': unable to parse %s" % (
params['#original'], params[name]))
if default is not None:
return default
raise error("Error on '%s': missing %s" % (params['#original'], name))
# Temperature wrappers # Temperature wrappers
def get_temp(self): def get_temp(self):
if not self.is_printer_ready: if not self.is_printer_ready:
@ -191,7 +214,7 @@ class GCodeParser:
eventtime = self.reactor.pause(eventtime + 1.) eventtime = self.reactor.pause(eventtime + 1.)
def set_temp(self, heater, params, wait=False): def set_temp(self, heater, params, wait=False):
print_time = self.toolhead.get_last_move_time() print_time = self.toolhead.get_last_move_time()
temp = float(params.get('S', '0')) temp = self.get_float('S', params, 0.)
try: try:
heater.set_temp(print_time, temp) heater.set_temp(print_time, temp)
except heater.error, e: except heater.error, e:
@ -212,17 +235,22 @@ class GCodeParser:
cmd_G1_aliases = ['G0'] cmd_G1_aliases = ['G0']
def cmd_G1(self, params): def cmd_G1(self, params):
# Move # Move
for a, p in self.axis2pos.items(): try:
if a in params: for a, p in self.axis2pos.items():
v = float(params[a]) if a in params:
if not self.absolutecoord or (p>2 and not self.absoluteextrude): v = float(params[a])
# value relative to position of last move if (not self.absolutecoord
self.last_position[p] += v or (p>2 and not self.absoluteextrude)):
else: # value relative to position of last move
# value relative to base coordinate position self.last_position[p] += v
self.last_position[p] = v + self.base_position[p] else:
if 'F' in params: # value relative to base coordinate position
self.speed = float(params['F']) / 60. self.last_position[p] = v + self.base_position[p]
if 'F' in params:
self.speed = float(params['F']) / 60.
except ValueError, e:
self.last_position = self.toolhead.get_position()
raise error("Unable to parse move '%s'" % (params['#original'],))
try: try:
self.toolhead.move(self.last_position, self.speed) self.toolhead.move(self.last_position, self.speed)
except homing.EndstopError, e: except homing.EndstopError, e:
@ -231,9 +259,9 @@ class GCodeParser:
def cmd_G4(self, params): def cmd_G4(self, params):
# Dwell # Dwell
if 'S' in params: if 'S' in params:
delay = float(params['S']) delay = self.get_float('S', params)
else: else:
delay = float(params.get('P', '0')) / 1000. delay = self.get_float('P', params, 0.) / 1000.
self.toolhead.dwell(delay) self.toolhead.dwell(delay)
def cmd_G20(self, params): def cmd_G20(self, params):
# Set units to inches # Set units to inches
@ -270,12 +298,11 @@ class GCodeParser:
self.absolutecoord = False self.absolutecoord = False
def cmd_G92(self, params): def cmd_G92(self, params):
# Set position # Set position
mcount = 0 offsets = { p: self.get_float(a, params)
for a, p in self.axis2pos.items(): for a, p in self.axis2pos.items() if a in params }
if a in params: for p, offset in offsets.items():
self.base_position[p] = self.last_position[p] - float(params[a]) self.base_position[p] = self.last_position[p] - offset
mcount += 1 if not offsets:
if not mcount:
self.base_position = list(self.last_position) self.base_position = list(self.last_position)
def cmd_M82(self, params): def cmd_M82(self, params):
# Use absolute distances for extrusion # Use absolute distances for extrusion
@ -330,18 +357,18 @@ class GCodeParser:
def cmd_M106(self, params): def cmd_M106(self, params):
# Set fan speed # Set fan speed
print_time = self.toolhead.get_last_move_time() print_time = self.toolhead.get_last_move_time()
self.fan.set_speed(print_time, float(params.get('S', '255')) / 255.) self.fan.set_speed(print_time, self.get_float('S', params, 255.) / 255.)
def cmd_M107(self, params): def cmd_M107(self, params):
# Turn fan off # Turn fan off
print_time = self.toolhead.get_last_move_time() print_time = self.toolhead.get_last_move_time()
self.fan.set_speed(print_time, 0.) self.fan.set_speed(print_time, 0.)
def cmd_M206(self, params): def cmd_M206(self, params):
# Set home offset # Set home offset
for a, p in self.axis2pos.items(): offsets = { p: self.get_float(a, params)
if a in params: for a, p in self.axis2pos.items() if a in params }
v = float(params[a]) for p, offset in offsets.items():
self.base_position[p] += self.homing_add[p] - v self.base_position[p] += self.homing_add[p] - offset
self.homing_add[p] = v self.homing_add[p] = offset
def cmd_M400(self, params): def cmd_M400(self, params):
# Wait for current moves to finish # Wait for current moves to finish
self.toolhead.wait_moves() self.toolhead.wait_moves()
@ -362,9 +389,9 @@ class GCodeParser:
cmd_PID_TUNE_aliases = ["M303"] cmd_PID_TUNE_aliases = ["M303"]
def cmd_PID_TUNE(self, params): def cmd_PID_TUNE(self, params):
# Run PID tuning # Run PID tuning
heater = int(params.get('E', '0')) heater = self.get_int('E', params, 0)
heater = {0: self.heater_nozzle, -1: self.heater_bed}[heater] heater = {0: self.heater_nozzle, -1: self.heater_bed}[heater]
temp = float(params.get('S', '60')) temp = self.get_float('S', params)
heater.start_auto_tune(temp) heater.start_auto_tune(temp)
self.bg_temp(heater) self.bg_temp(heater)
cmd_CLEAR_SHUTDOWN_when_not_ready = True cmd_CLEAR_SHUTDOWN_when_not_ready = True
@ -410,3 +437,6 @@ class GCodeParser:
if desc is not None: if desc is not None:
cmdhelp.append("%-10s: %s" % (cmd, desc)) cmdhelp.append("%-10s: %s" % (cmd, desc))
self.respond_info("\n".join(cmdhelp)) self.respond_info("\n".join(cmdhelp))
class error(Exception):
pass