2016-05-25 18:37:40 +03:00
|
|
|
# Parse gcode commands
|
|
|
|
#
|
2018-01-20 06:22:17 +03:00
|
|
|
# Copyright (C) 2016-2018 Kevin O'Connor <kevin@koconnor.net>
|
2016-05-25 18:37:40 +03:00
|
|
|
#
|
|
|
|
# This file may be distributed under the terms of the GNU GPLv3 license.
|
2017-02-06 21:31:34 +03:00
|
|
|
import os, re, logging, collections
|
2017-12-04 04:22:44 +03:00
|
|
|
import homing, extruder
|
2016-05-25 18:37:40 +03:00
|
|
|
|
2017-12-04 03:30:49 +03:00
|
|
|
class error(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
# Parse and handle G-Code commands
|
2016-05-25 18:37:40 +03:00
|
|
|
class GCodeParser:
|
2017-12-04 04:22:44 +03:00
|
|
|
error = error
|
2016-05-25 18:37:40 +03:00
|
|
|
RETRY_TIME = 0.100
|
2017-08-22 00:19:43 +03:00
|
|
|
def __init__(self, printer, fd):
|
2016-05-25 18:37:40 +03:00
|
|
|
self.printer = printer
|
|
|
|
self.fd = fd
|
|
|
|
# Input handling
|
2018-01-20 06:22:17 +03:00
|
|
|
self.reactor = printer.get_reactor()
|
2016-11-16 18:56:10 +03:00
|
|
|
self.is_processing_data = False
|
2017-08-22 00:19:43 +03:00
|
|
|
self.is_fileinput = not not printer.get_start_args().get("debuginput")
|
2016-05-25 18:37:40 +03:00
|
|
|
self.fd_handle = None
|
2017-08-22 00:19:43 +03:00
|
|
|
if not self.is_fileinput:
|
2016-11-23 03:38:51 +03:00
|
|
|
self.fd_handle = self.reactor.register_fd(self.fd, self.process_data)
|
2017-05-20 04:05:46 +03:00
|
|
|
self.partial_input = ""
|
2018-02-02 17:38:04 +03:00
|
|
|
self.pending_commands = []
|
2016-05-25 18:37:40 +03:00
|
|
|
self.bytes_read = 0
|
2016-09-25 01:10:24 +03:00
|
|
|
self.input_log = collections.deque([], 50)
|
2016-05-25 18:37:40 +03:00
|
|
|
# Command handling
|
2016-11-23 03:38:51 +03:00
|
|
|
self.is_printer_ready = False
|
2017-12-04 02:53:42 +03:00
|
|
|
self.base_gcode_handlers = self.gcode_handlers = {}
|
|
|
|
self.ready_gcode_handlers = {}
|
|
|
|
self.gcode_help = {}
|
|
|
|
for cmd in self.all_handlers:
|
|
|
|
func = getattr(self, 'cmd_' + cmd)
|
|
|
|
wnr = getattr(self, 'cmd_' + cmd + '_when_not_ready', False)
|
|
|
|
desc = getattr(self, 'cmd_' + cmd + '_help', None)
|
|
|
|
self.register_command(cmd, func, wnr, desc)
|
|
|
|
for a in getattr(self, 'cmd_' + cmd + '_aliases', []):
|
|
|
|
self.register_command(a, func, wnr)
|
2017-12-22 04:45:07 +03:00
|
|
|
# G-Code coordinate manipulation
|
|
|
|
self.absolutecoord = self.absoluteextrude = True
|
|
|
|
self.base_position = [0.0, 0.0, 0.0, 0.0]
|
|
|
|
self.last_position = [0.0, 0.0, 0.0, 0.0]
|
|
|
|
self.homing_add = [0.0, 0.0, 0.0, 0.0]
|
2017-12-22 04:53:44 +03:00
|
|
|
self.speed_factor = 1. / 60.
|
2017-12-22 05:33:38 +03:00
|
|
|
self.extrude_factor = 1.
|
2018-01-21 07:47:36 +03:00
|
|
|
self.move_transform = self.move_with_transform = None
|
|
|
|
self.position_with_transform = (lambda: [0., 0., 0., 0.])
|
2017-12-04 02:53:42 +03:00
|
|
|
# G-Code state
|
2016-05-25 18:37:40 +03:00
|
|
|
self.need_ack = False
|
2017-04-29 22:26:53 +03:00
|
|
|
self.toolhead = self.fan = self.extruder = None
|
|
|
|
self.heaters = []
|
2016-12-10 01:28:09 +03:00
|
|
|
self.speed = 25.0
|
2016-05-25 18:37:40 +03:00
|
|
|
self.axis2pos = {'X': 0, 'Y': 1, 'Z': 2, 'E': 3}
|
2017-12-04 02:53:42 +03:00
|
|
|
def register_command(self, cmd, func, when_not_ready=False, desc=None):
|
2018-01-17 19:26:09 +03:00
|
|
|
if func is None:
|
|
|
|
if cmd in self.ready_gcode_handlers:
|
|
|
|
del self.ready_gcode_handlers[cmd]
|
|
|
|
if cmd in self.base_gcode_handlers:
|
|
|
|
del self.base_gcode_handlers[cmd]
|
|
|
|
return
|
2017-12-04 04:22:44 +03:00
|
|
|
if not (len(cmd) >= 2 and not cmd[0].isupper() and cmd[1].isdigit()):
|
|
|
|
origfunc = func
|
|
|
|
func = lambda params: origfunc(self.get_extended_params(params))
|
2017-12-04 02:53:42 +03:00
|
|
|
self.ready_gcode_handlers[cmd] = func
|
|
|
|
if when_not_ready:
|
|
|
|
self.base_gcode_handlers[cmd] = func
|
|
|
|
if desc is not None:
|
|
|
|
self.gcode_help[cmd] = desc
|
2018-01-21 07:47:36 +03:00
|
|
|
def set_move_transform(self, transform):
|
|
|
|
if self.move_transform is not None:
|
|
|
|
raise self.printer.config_error(
|
|
|
|
"G-Code move transform already specified")
|
|
|
|
self.move_transform = transform
|
|
|
|
self.move_with_transform = transform.move
|
|
|
|
self.position_with_transform = transform.get_position
|
2016-05-25 18:37:40 +03:00
|
|
|
def stats(self, eventtime):
|
2018-02-05 21:52:05 +03:00
|
|
|
return False, "gcodein=%d" % (self.bytes_read,)
|
2018-01-20 06:49:27 +03:00
|
|
|
def printer_state(self, state):
|
|
|
|
if state == 'shutdown':
|
|
|
|
if not self.is_printer_ready:
|
|
|
|
return
|
|
|
|
self.is_printer_ready = False
|
|
|
|
self.gcode_handlers = self.base_gcode_handlers
|
|
|
|
self.dump_debug()
|
|
|
|
if self.is_fileinput:
|
|
|
|
self.printer.request_exit()
|
|
|
|
return
|
|
|
|
if state != 'ready':
|
|
|
|
return
|
2017-10-12 22:15:14 +03:00
|
|
|
self.is_printer_ready = True
|
2017-12-04 02:53:42 +03:00
|
|
|
self.gcode_handlers = self.ready_gcode_handlers
|
2017-04-26 01:34:15 +03:00
|
|
|
# Lookup printer components
|
2018-01-20 06:22:17 +03:00
|
|
|
self.toolhead = self.printer.lookup_object('toolhead')
|
2018-01-21 07:47:36 +03:00
|
|
|
if self.move_transform is None:
|
|
|
|
self.move_with_transform = self.toolhead.move
|
|
|
|
self.position_with_transform = self.toolhead.get_position
|
2017-04-29 21:56:39 +03:00
|
|
|
extruders = extruder.get_printer_extruders(self.printer)
|
|
|
|
if extruders:
|
|
|
|
self.extruder = extruders[0]
|
|
|
|
self.toolhead.set_extruder(self.extruder)
|
2017-04-29 22:26:53 +03:00
|
|
|
self.heaters = [ e.get_heater() for e in extruders ]
|
2018-01-20 06:22:17 +03:00
|
|
|
self.heaters.append(self.printer.lookup_object('heater_bed', None))
|
|
|
|
self.fan = self.printer.lookup_object('fan', None)
|
2017-04-26 01:34:15 +03:00
|
|
|
if self.is_fileinput and self.fd_handle is None:
|
2016-11-23 03:38:51 +03:00
|
|
|
self.fd_handle = self.reactor.register_fd(self.fd, self.process_data)
|
2017-12-04 03:30:49 +03:00
|
|
|
def reset_last_position(self):
|
2018-01-21 07:47:36 +03:00
|
|
|
self.last_position = self.position_with_transform()
|
2016-12-01 07:47:40 +03:00
|
|
|
def motor_heater_off(self):
|
2017-02-13 02:42:29 +03:00
|
|
|
if self.toolhead is None:
|
|
|
|
return
|
|
|
|
self.toolhead.motor_off()
|
|
|
|
print_time = self.toolhead.get_last_move_time()
|
2017-04-29 22:26:53 +03:00
|
|
|
for heater in self.heaters:
|
|
|
|
if heater is not None:
|
|
|
|
heater.set_temp(print_time, 0.)
|
2017-02-13 02:42:29 +03:00
|
|
|
if self.fan is not None:
|
|
|
|
self.fan.set_speed(print_time, 0.)
|
2016-12-01 03:05:25 +03:00
|
|
|
def dump_debug(self):
|
2017-09-27 18:54:53 +03:00
|
|
|
out = []
|
|
|
|
out.append("Dumping gcode input %d blocks" % (
|
2016-12-01 03:05:25 +03:00
|
|
|
len(self.input_log),))
|
|
|
|
for eventtime, data in self.input_log:
|
2017-09-27 18:54:53 +03:00
|
|
|
out.append("Read %f: %s" % (eventtime, repr(data)))
|
2017-12-22 06:23:07 +03:00
|
|
|
out.append(
|
|
|
|
"gcode state: absolutecoord=%s absoluteextrude=%s"
|
|
|
|
" base_position=%s last_position=%s homing_add=%s"
|
2017-12-22 05:33:38 +03:00
|
|
|
" speed_factor=%s extrude_factor=%s speed=%s" % (
|
2017-12-22 06:23:07 +03:00
|
|
|
self.absolutecoord, self.absoluteextrude,
|
|
|
|
self.base_position, self.last_position, self.homing_add,
|
2017-12-22 05:33:38 +03:00
|
|
|
self.speed_factor, self.extrude_factor, self.speed))
|
2017-09-27 18:54:53 +03:00
|
|
|
logging.info("\n".join(out))
|
2016-05-25 18:37:40 +03:00
|
|
|
# Parse input into commands
|
2017-06-06 22:04:01 +03:00
|
|
|
args_r = re.compile('([A-Z_]+|[A-Z*])')
|
2017-05-20 04:05:46 +03:00
|
|
|
def process_commands(self, commands, need_ack=True):
|
|
|
|
for line in commands:
|
2016-05-25 18:37:40 +03:00
|
|
|
# Ignore comments and leading/trailing spaces
|
|
|
|
line = origline = line.strip()
|
|
|
|
cpos = line.find(';')
|
|
|
|
if cpos >= 0:
|
|
|
|
line = line[:cpos]
|
|
|
|
# Break command into parts
|
2017-06-06 22:04:01 +03:00
|
|
|
parts = self.args_r.split(line.upper())[1:]
|
|
|
|
params = { parts[i]: parts[i+1].strip()
|
2017-06-06 19:35:13 +03:00
|
|
|
for i in range(0, len(parts), 2) }
|
2016-05-25 18:37:40 +03:00
|
|
|
params['#original'] = origline
|
2017-06-06 22:04:01 +03:00
|
|
|
if parts and parts[0] == 'N':
|
2016-05-25 18:37:40 +03:00
|
|
|
# Skip line number at start of command
|
|
|
|
del parts[:2]
|
|
|
|
if not parts:
|
2018-02-01 18:55:45 +03:00
|
|
|
# Treat empty line as empty command
|
|
|
|
parts = ['', '']
|
2017-06-06 22:04:01 +03:00
|
|
|
params['#command'] = cmd = parts[0] + parts[1].strip()
|
2016-05-25 18:37:40 +03:00
|
|
|
# Invoke handler for command
|
2017-05-20 04:05:46 +03:00
|
|
|
self.need_ack = need_ack
|
2016-05-25 18:37:40 +03:00
|
|
|
handler = self.gcode_handlers.get(cmd, self.cmd_default)
|
|
|
|
try:
|
|
|
|
handler(params)
|
2017-06-10 06:32:49 +03:00
|
|
|
except error as e:
|
2017-03-16 06:40:46 +03:00
|
|
|
self.respond_error(str(e))
|
2017-12-04 03:30:49 +03:00
|
|
|
self.reset_last_position()
|
2018-02-01 20:13:48 +03:00
|
|
|
if not need_ack:
|
|
|
|
raise
|
2016-05-25 18:37:40 +03:00
|
|
|
except:
|
2017-10-12 22:15:14 +03:00
|
|
|
msg = 'Internal error on command:"%s"' % (cmd,)
|
|
|
|
logging.exception(msg)
|
|
|
|
self.printer.invoke_shutdown(msg)
|
|
|
|
self.respond_error(msg)
|
2018-02-01 20:13:48 +03:00
|
|
|
if not need_ack:
|
|
|
|
raise
|
2016-05-25 18:37:40 +03:00
|
|
|
self.ack()
|
2018-02-02 17:38:04 +03:00
|
|
|
m112_r = re.compile('^(?:[nN][0-9]+)?\s*[mM]112(?:\s|$)')
|
2016-05-25 18:37:40 +03:00
|
|
|
def process_data(self, eventtime):
|
2018-02-02 17:38:04 +03:00
|
|
|
# Read input, separate by newline, and add to pending_commands
|
2016-05-25 18:37:40 +03:00
|
|
|
data = os.read(self.fd, 4096)
|
2016-09-25 01:10:24 +03:00
|
|
|
self.input_log.append((eventtime, data))
|
2016-05-25 18:37:40 +03:00
|
|
|
self.bytes_read += len(data)
|
|
|
|
lines = data.split('\n')
|
2017-05-20 04:05:46 +03:00
|
|
|
lines[0] = self.partial_input + lines[0]
|
|
|
|
self.partial_input = lines.pop()
|
2018-02-02 17:38:04 +03:00
|
|
|
pending_commands = self.pending_commands
|
|
|
|
pending_commands.extend(lines)
|
|
|
|
# Check for M112 out-of-order
|
|
|
|
if ((len(pending_commands) > 1 or self.is_processing_data)
|
|
|
|
and len(pending_commands) < 20):
|
|
|
|
for line in lines:
|
|
|
|
if self.m112_r.match(line) is not None:
|
|
|
|
self.cmd_M112({})
|
|
|
|
# Check if already processing data
|
2016-12-09 21:55:18 +03:00
|
|
|
if self.is_processing_data:
|
2018-02-02 17:38:04 +03:00
|
|
|
if len(pending_commands) >= 20 or not data:
|
|
|
|
# Stop reading input
|
|
|
|
self.reactor.unregister_fd(self.fd_handle)
|
|
|
|
self.fd_handle = None
|
|
|
|
return
|
|
|
|
# Process commands
|
2016-11-16 18:56:10 +03:00
|
|
|
self.is_processing_data = True
|
2018-02-02 17:38:04 +03:00
|
|
|
while pending_commands:
|
|
|
|
self.pending_commands = []
|
|
|
|
self.process_commands(pending_commands)
|
|
|
|
pending_commands = self.pending_commands
|
|
|
|
self.is_processing_data = False
|
|
|
|
# Reenable input reading if it was stopped
|
|
|
|
if self.fd_handle is None:
|
|
|
|
self.fd_handle = self.reactor.register_fd(self.fd, self.process_data)
|
2016-11-21 04:40:31 +03:00
|
|
|
if not data and self.is_fileinput:
|
2016-12-01 07:47:40 +03:00
|
|
|
self.motor_heater_off()
|
2017-07-22 04:13:59 +03:00
|
|
|
if self.toolhead is not None:
|
2017-08-22 00:19:43 +03:00
|
|
|
self.toolhead.wait_moves()
|
2017-08-26 20:20:16 +03:00
|
|
|
self.printer.request_exit()
|
2018-01-25 19:39:46 +03:00
|
|
|
def run_script(self, script):
|
2018-02-03 02:28:48 +03:00
|
|
|
prev_need_ack = self.need_ack
|
|
|
|
try:
|
|
|
|
self.process_commands(script.split('\n'), need_ack=False)
|
|
|
|
finally:
|
|
|
|
self.need_ack = prev_need_ack
|
2016-05-25 18:37:40 +03:00
|
|
|
# Response handling
|
|
|
|
def ack(self, msg=None):
|
2016-11-21 04:40:31 +03:00
|
|
|
if not self.need_ack or self.is_fileinput:
|
2016-05-25 18:37:40 +03:00
|
|
|
return
|
|
|
|
if msg:
|
|
|
|
os.write(self.fd, "ok %s\n" % (msg,))
|
|
|
|
else:
|
|
|
|
os.write(self.fd, "ok\n")
|
|
|
|
self.need_ack = False
|
|
|
|
def respond(self, msg):
|
2016-11-21 04:40:31 +03:00
|
|
|
if self.is_fileinput:
|
2016-05-25 18:37:40 +03:00
|
|
|
return
|
|
|
|
os.write(self.fd, msg+"\n")
|
2016-12-21 19:33:03 +03:00
|
|
|
def respond_info(self, msg):
|
2017-07-17 18:24:15 +03:00
|
|
|
logging.debug(msg)
|
2016-12-21 19:33:03 +03:00
|
|
|
lines = [l.strip() for l in msg.strip().split('\n')]
|
|
|
|
self.respond("// " + "\n// ".join(lines))
|
2016-11-30 22:30:45 +03:00
|
|
|
def respond_error(self, msg):
|
2017-07-17 18:24:15 +03:00
|
|
|
logging.warning(msg)
|
2016-11-30 22:30:45 +03:00
|
|
|
lines = msg.strip().split('\n')
|
2016-12-21 19:33:03 +03:00
|
|
|
if len(lines) > 1:
|
|
|
|
self.respond_info("\n".join(lines[:-1]))
|
2016-11-30 22:30:45 +03:00
|
|
|
self.respond('!! %s' % (lines[-1].strip(),))
|
2017-03-16 06:40:46 +03:00
|
|
|
# Parameter parsing helpers
|
2017-12-04 04:16:20 +03:00
|
|
|
class sentinel: pass
|
|
|
|
def get_str(self, name, params, default=sentinel, parser=str):
|
2017-03-16 06:40:46 +03:00
|
|
|
if name in params:
|
|
|
|
try:
|
2017-12-04 04:16:20 +03:00
|
|
|
return parser(params[name])
|
|
|
|
except:
|
2017-03-16 06:40:46 +03:00
|
|
|
raise error("Error on '%s': unable to parse %s" % (
|
|
|
|
params['#original'], params[name]))
|
2017-12-04 04:16:20 +03:00
|
|
|
if default is not self.sentinel:
|
2017-03-16 06:40:46 +03:00
|
|
|
return default
|
|
|
|
raise error("Error on '%s': missing %s" % (params['#original'], name))
|
2017-12-04 04:16:20 +03:00
|
|
|
def get_int(self, name, params, default=sentinel):
|
|
|
|
return self.get_str(name, params, default, parser=int)
|
|
|
|
def get_float(self, name, params, default=sentinel):
|
|
|
|
return self.get_str(name, params, default, parser=float)
|
2017-06-06 22:04:01 +03:00
|
|
|
extended_r = re.compile(
|
|
|
|
r'^\s*(?:N[0-9]+\s*)?'
|
|
|
|
r'(?P<cmd>[a-zA-Z_][a-zA-Z_]+)(?:\s+|$)'
|
|
|
|
r'(?P<args>[^#*;]*?)'
|
|
|
|
r'\s*(?:[#*;].*)?$')
|
|
|
|
def get_extended_params(self, params):
|
|
|
|
m = self.extended_r.match(params['#original'])
|
|
|
|
if m is None:
|
|
|
|
# Not an "extended" command
|
|
|
|
return params
|
|
|
|
eargs = m.group('args')
|
|
|
|
try:
|
|
|
|
eparams = [earg.split('=', 1) for earg in eargs.split()]
|
2017-10-03 04:48:45 +03:00
|
|
|
eparams = { k.upper(): v for k, v in eparams }
|
|
|
|
eparams.update({k: params[k] for k in params if k.startswith('#')})
|
|
|
|
return eparams
|
2017-06-06 22:04:01 +03:00
|
|
|
except ValueError as e:
|
|
|
|
raise error("Malformed command '%s'" % (params['#original'],))
|
2016-05-25 18:37:40 +03:00
|
|
|
# Temperature wrappers
|
2017-10-12 05:16:02 +03:00
|
|
|
def get_temp(self, eventtime):
|
2017-04-29 22:26:53 +03:00
|
|
|
# Tn:XXX /YYY B:XXX /YYY
|
2016-05-25 18:37:40 +03:00
|
|
|
out = []
|
2017-04-29 22:26:53 +03:00
|
|
|
for i, heater in enumerate(self.heaters):
|
|
|
|
if heater is not None:
|
2017-10-12 05:16:02 +03:00
|
|
|
cur, target = heater.get_temp(eventtime)
|
2017-04-29 22:26:53 +03:00
|
|
|
name = "B"
|
|
|
|
if i < len(self.heaters) - 1:
|
|
|
|
name = "T%d" % (i,)
|
|
|
|
out.append("%s:%.1f /%.1f" % (name, cur, target))
|
2017-10-12 05:16:02 +03:00
|
|
|
if not out:
|
|
|
|
return "T:0"
|
2016-05-25 18:37:40 +03:00
|
|
|
return " ".join(out)
|
|
|
|
def bg_temp(self, heater):
|
2016-11-21 04:40:31 +03:00
|
|
|
if self.is_fileinput:
|
2016-05-25 18:37:40 +03:00
|
|
|
return
|
2017-02-06 21:31:34 +03:00
|
|
|
eventtime = self.reactor.monotonic()
|
2016-11-17 20:37:57 +03:00
|
|
|
while self.is_printer_ready and heater.check_busy(eventtime):
|
2017-02-06 22:07:55 +03:00
|
|
|
print_time = self.toolhead.get_last_move_time()
|
2017-10-12 05:16:02 +03:00
|
|
|
self.respond(self.get_temp(eventtime))
|
2016-11-17 20:37:57 +03:00
|
|
|
eventtime = self.reactor.pause(eventtime + 1.)
|
2017-04-29 22:26:53 +03:00
|
|
|
def set_temp(self, params, is_bed=False, wait=False):
|
2017-05-01 21:58:07 +03:00
|
|
|
temp = self.get_float('S', params, 0.)
|
2017-04-29 22:26:53 +03:00
|
|
|
heater = None
|
|
|
|
if is_bed:
|
|
|
|
heater = self.heaters[-1]
|
|
|
|
elif 'T' in params:
|
|
|
|
heater_index = self.get_int('T', params)
|
|
|
|
if heater_index >= 0 and heater_index < len(self.heaters) - 1:
|
|
|
|
heater = self.heaters[heater_index]
|
|
|
|
elif self.extruder is not None:
|
|
|
|
heater = self.extruder.get_heater()
|
2017-04-26 01:34:15 +03:00
|
|
|
if heater is None:
|
2017-05-01 21:58:07 +03:00
|
|
|
if temp > 0.:
|
|
|
|
self.respond_error("Heater not configured")
|
2017-04-26 01:34:15 +03:00
|
|
|
return
|
2016-07-07 21:23:48 +03:00
|
|
|
print_time = self.toolhead.get_last_move_time()
|
2017-02-21 18:48:42 +03:00
|
|
|
try:
|
|
|
|
heater.set_temp(print_time, temp)
|
2017-06-10 06:32:49 +03:00
|
|
|
except heater.error as e:
|
2017-12-04 03:30:49 +03:00
|
|
|
raise error(str(e))
|
2018-01-03 18:33:10 +03:00
|
|
|
if wait and temp:
|
2016-05-25 18:37:40 +03:00
|
|
|
self.bg_temp(heater)
|
2017-04-26 01:34:15 +03:00
|
|
|
def set_fan_speed(self, speed):
|
|
|
|
if self.fan is None:
|
2017-10-11 21:28:19 +03:00
|
|
|
if speed and not self.is_fileinput:
|
2017-05-01 21:58:07 +03:00
|
|
|
self.respond_info("Fan not configured")
|
2017-04-26 01:34:15 +03:00
|
|
|
return
|
|
|
|
print_time = self.toolhead.get_last_move_time()
|
|
|
|
self.fan.set_speed(print_time, speed)
|
2017-12-22 04:45:07 +03:00
|
|
|
# G-Code special command handlers
|
2016-05-25 18:37:40 +03:00
|
|
|
def cmd_default(self, params):
|
2016-11-23 03:38:51 +03:00
|
|
|
if not self.is_printer_ready:
|
2016-11-30 22:30:45 +03:00
|
|
|
self.respond_error(self.printer.get_state_message())
|
2016-05-25 18:37:40 +03:00
|
|
|
return
|
|
|
|
cmd = params.get('#command')
|
|
|
|
if not cmd:
|
|
|
|
logging.debug(params['#original'])
|
|
|
|
return
|
2017-04-29 21:56:39 +03:00
|
|
|
if cmd[0] == 'T' and len(cmd) > 1 and cmd[1].isdigit():
|
|
|
|
# Tn command has to be handled specially
|
|
|
|
self.cmd_Tn(params)
|
|
|
|
return
|
2017-07-17 18:24:15 +03:00
|
|
|
self.respond_info('Unknown command:"%s"' % (cmd,))
|
2017-04-29 21:56:39 +03:00
|
|
|
def cmd_Tn(self, params):
|
|
|
|
# Select Tool
|
|
|
|
index = self.get_int('T', params)
|
|
|
|
extruders = extruder.get_printer_extruders(self.printer)
|
|
|
|
if self.extruder is None or index < 0 or index >= len(extruders):
|
|
|
|
self.respond_error("Extruder %d not configured" % (index,))
|
|
|
|
return
|
|
|
|
e = extruders[index]
|
|
|
|
if self.extruder is e:
|
|
|
|
return
|
2018-01-25 19:39:46 +03:00
|
|
|
self.run_script(self.extruder.get_activate_gcode(False))
|
2017-04-29 21:56:39 +03:00
|
|
|
try:
|
|
|
|
self.toolhead.set_extruder(e)
|
2017-06-10 06:32:49 +03:00
|
|
|
except homing.EndstopError as e:
|
2017-12-04 03:30:49 +03:00
|
|
|
raise error(str(e))
|
2017-04-29 21:56:39 +03:00
|
|
|
self.extruder = e
|
2017-12-04 03:30:49 +03:00
|
|
|
self.reset_last_position()
|
2018-01-25 19:39:46 +03:00
|
|
|
self.run_script(self.extruder.get_activate_gcode(True))
|
2017-04-26 01:34:15 +03:00
|
|
|
all_handlers = [
|
2017-12-22 04:45:07 +03:00
|
|
|
'G1', 'G4', 'G28', 'M18', 'M400',
|
2017-12-22 05:33:38 +03:00
|
|
|
'G20', 'M82', 'M83', 'G90', 'G91', 'G92', 'M206', 'M220', 'M221',
|
2017-12-22 04:45:07 +03:00
|
|
|
'M105', 'M104', 'M109', 'M140', 'M190', 'M106', 'M107',
|
|
|
|
'M112', 'M114', 'M115', 'IGNORE', 'QUERY_ENDSTOPS', 'PID_TUNE',
|
2017-06-06 22:04:01 +03:00
|
|
|
'RESTART', 'FIRMWARE_RESTART', 'ECHO', 'STATUS', 'HELP']
|
2017-12-22 04:45:07 +03:00
|
|
|
# G-Code movement commands
|
2016-12-01 06:34:38 +03:00
|
|
|
cmd_G1_aliases = ['G0']
|
|
|
|
def cmd_G1(self, params):
|
2016-05-25 18:37:40 +03:00
|
|
|
# Move
|
2017-03-16 06:40:46 +03:00
|
|
|
try:
|
2017-12-22 05:33:38 +03:00
|
|
|
for axis in 'XYZ':
|
|
|
|
if axis in params:
|
|
|
|
v = float(params[axis])
|
|
|
|
pos = self.axis2pos[axis]
|
|
|
|
if not self.absolutecoord:
|
2017-03-16 06:40:46 +03:00
|
|
|
# value relative to position of last move
|
2017-12-22 05:33:38 +03:00
|
|
|
self.last_position[pos] += v
|
2017-03-16 06:40:46 +03:00
|
|
|
else:
|
|
|
|
# value relative to base coordinate position
|
2017-12-22 05:33:38 +03:00
|
|
|
self.last_position[pos] = v + self.base_position[pos]
|
|
|
|
if 'E' in params:
|
|
|
|
v = float(params['E']) * self.extrude_factor
|
|
|
|
if not self.absolutecoord or not self.absoluteextrude:
|
|
|
|
# value relative to position of last move
|
|
|
|
self.last_position[3] += v
|
|
|
|
else:
|
|
|
|
# value relative to base coordinate position
|
|
|
|
self.last_position[3] = v + self.base_position[3]
|
2017-03-16 06:40:46 +03:00
|
|
|
if 'F' in params:
|
2017-12-22 04:53:44 +03:00
|
|
|
speed = float(params['F']) * self.speed_factor
|
2017-05-02 01:04:33 +03:00
|
|
|
if speed <= 0.:
|
2017-12-04 03:30:49 +03:00
|
|
|
raise error("Invalid speed in '%s'" % (params['#original'],))
|
2017-05-02 01:04:33 +03:00
|
|
|
self.speed = speed
|
2017-06-10 06:32:49 +03:00
|
|
|
except ValueError as e:
|
2017-03-16 06:40:46 +03:00
|
|
|
raise error("Unable to parse move '%s'" % (params['#original'],))
|
2016-09-30 23:00:32 +03:00
|
|
|
try:
|
2018-01-21 07:47:36 +03:00
|
|
|
self.move_with_transform(self.last_position, self.speed)
|
2017-06-10 06:32:49 +03:00
|
|
|
except homing.EndstopError as e:
|
2017-12-04 03:30:49 +03:00
|
|
|
raise error(str(e))
|
2016-05-25 18:37:40 +03:00
|
|
|
def cmd_G4(self, params):
|
|
|
|
# Dwell
|
|
|
|
if 'S' in params:
|
2017-03-16 06:40:46 +03:00
|
|
|
delay = self.get_float('S', params)
|
2016-05-25 18:37:40 +03:00
|
|
|
else:
|
2017-03-16 06:40:46 +03:00
|
|
|
delay = self.get_float('P', params, 0.) / 1000.
|
2016-07-07 21:23:48 +03:00
|
|
|
self.toolhead.dwell(delay)
|
2016-05-25 18:37:40 +03:00
|
|
|
def cmd_G28(self, params):
|
|
|
|
# Move to origin
|
2016-07-26 06:47:30 +03:00
|
|
|
axes = []
|
|
|
|
for axis in 'XYZ':
|
|
|
|
if axis in params:
|
|
|
|
axes.append(self.axis2pos[axis])
|
|
|
|
if not axes:
|
|
|
|
axes = [0, 1, 2]
|
2017-12-06 17:59:00 +03:00
|
|
|
homing_state = homing.Homing(self.toolhead)
|
2016-11-21 04:40:31 +03:00
|
|
|
if self.is_fileinput:
|
2016-11-28 19:23:26 +03:00
|
|
|
homing_state.set_no_verify_retract()
|
2016-12-09 02:12:20 +03:00
|
|
|
try:
|
2017-12-06 17:59:00 +03:00
|
|
|
homing_state.home_axes(axes)
|
2017-06-10 06:32:49 +03:00
|
|
|
except homing.EndstopError as e:
|
2017-12-04 03:30:49 +03:00
|
|
|
raise error(str(e))
|
2016-12-09 02:12:20 +03:00
|
|
|
for axis in homing_state.get_axes():
|
|
|
|
self.base_position[axis] = -self.homing_add[axis]
|
2018-01-17 05:46:42 +03:00
|
|
|
self.reset_last_position()
|
2017-12-22 04:45:07 +03:00
|
|
|
cmd_M18_aliases = ["M84"]
|
|
|
|
def cmd_M18(self, params):
|
|
|
|
# Turn off motors
|
|
|
|
self.toolhead.motor_off()
|
|
|
|
def cmd_M400(self, params):
|
|
|
|
# Wait for current moves to finish
|
|
|
|
self.toolhead.wait_moves()
|
|
|
|
# G-Code coordinate manipulation
|
|
|
|
def cmd_G20(self, params):
|
|
|
|
# Set units to inches
|
|
|
|
self.respond_error('Machine does not support G20 (inches) command')
|
|
|
|
def cmd_M82(self, params):
|
|
|
|
# Use absolute distances for extrusion
|
|
|
|
self.absoluteextrude = True
|
|
|
|
def cmd_M83(self, params):
|
|
|
|
# Use relative distances for extrusion
|
|
|
|
self.absoluteextrude = False
|
2016-05-25 18:37:40 +03:00
|
|
|
def cmd_G90(self, params):
|
|
|
|
# Use absolute coordinates
|
|
|
|
self.absolutecoord = True
|
|
|
|
def cmd_G91(self, params):
|
|
|
|
# Use relative coordinates
|
|
|
|
self.absolutecoord = False
|
|
|
|
def cmd_G92(self, params):
|
|
|
|
# Set position
|
2017-03-16 06:40:46 +03:00
|
|
|
offsets = { p: self.get_float(a, params)
|
|
|
|
for a, p in self.axis2pos.items() if a in params }
|
|
|
|
for p, offset in offsets.items():
|
2017-12-22 05:33:38 +03:00
|
|
|
if p == 3:
|
|
|
|
offset *= self.extrude_factor
|
2017-03-16 06:40:46 +03:00
|
|
|
self.base_position[p] = self.last_position[p] - offset
|
|
|
|
if not offsets:
|
2016-05-25 18:37:40 +03:00
|
|
|
self.base_position = list(self.last_position)
|
2017-12-22 04:45:07 +03:00
|
|
|
def cmd_M206(self, params):
|
|
|
|
# Set home offset
|
2017-12-22 05:33:38 +03:00
|
|
|
offsets = { self.axis2pos[a]: self.get_float(a, params)
|
|
|
|
for a in 'XYZ' if a in params }
|
2017-12-22 04:45:07 +03:00
|
|
|
for p, offset in offsets.items():
|
|
|
|
self.base_position[p] += self.homing_add[p] - offset
|
|
|
|
self.homing_add[p] = offset
|
2017-12-22 04:53:44 +03:00
|
|
|
def cmd_M220(self, params):
|
|
|
|
# Set speed factor override percentage
|
|
|
|
value = self.get_float('S', params, 100.) / (60. * 100.)
|
|
|
|
if value <= 0.:
|
|
|
|
raise error("Invalid factor in '%s'" % (params['#original'],))
|
|
|
|
self.speed_factor = value
|
2017-12-22 05:33:38 +03:00
|
|
|
def cmd_M221(self, params):
|
|
|
|
# Set extrude factor override percentage
|
|
|
|
new_extrude_factor = self.get_float('S', params, 100.) / 100.
|
|
|
|
if new_extrude_factor <= 0.:
|
|
|
|
raise error("Invalid factor in '%s'" % (params['#original'],))
|
|
|
|
last_e_pos = self.last_position[3]
|
|
|
|
e_value = (last_e_pos - self.base_position[3]) / self.extrude_factor
|
|
|
|
self.base_position[3] = last_e_pos - e_value * new_extrude_factor
|
|
|
|
self.extrude_factor = new_extrude_factor
|
2017-12-22 04:45:07 +03:00
|
|
|
# G-Code temperature and fan commands
|
2016-12-01 06:34:38 +03:00
|
|
|
cmd_M105_when_not_ready = True
|
2016-05-25 18:37:40 +03:00
|
|
|
def cmd_M105(self, params):
|
|
|
|
# Get Extruder Temperature
|
2017-10-12 05:16:02 +03:00
|
|
|
self.ack(self.get_temp(self.reactor.monotonic()))
|
2016-05-25 18:37:40 +03:00
|
|
|
def cmd_M104(self, params):
|
|
|
|
# Set Extruder Temperature
|
2017-04-29 22:26:53 +03:00
|
|
|
self.set_temp(params)
|
2016-05-25 18:37:40 +03:00
|
|
|
def cmd_M109(self, params):
|
|
|
|
# Set Extruder Temperature and Wait
|
2017-04-29 22:26:53 +03:00
|
|
|
self.set_temp(params, wait=True)
|
2017-12-22 04:45:07 +03:00
|
|
|
def cmd_M140(self, params):
|
|
|
|
# Set Bed Temperature
|
|
|
|
self.set_temp(params, is_bed=True)
|
|
|
|
def cmd_M190(self, params):
|
|
|
|
# Set Bed Temperature and Wait
|
|
|
|
self.set_temp(params, is_bed=True, wait=True)
|
|
|
|
def cmd_M106(self, params):
|
|
|
|
# Set fan speed
|
|
|
|
self.set_fan_speed(self.get_float('S', params, 255.) / 255.)
|
|
|
|
def cmd_M107(self, params):
|
|
|
|
# Turn fan off
|
|
|
|
self.set_fan_speed(0.)
|
|
|
|
# G-Code miscellaneous commands
|
2017-12-04 02:16:23 +03:00
|
|
|
cmd_M112_when_not_ready = True
|
2016-12-09 21:55:18 +03:00
|
|
|
def cmd_M112(self, params):
|
|
|
|
# Emergency Stop
|
2017-10-12 22:15:14 +03:00
|
|
|
self.printer.invoke_shutdown("Shutdown due to M112 command")
|
2016-12-01 06:34:38 +03:00
|
|
|
cmd_M114_when_not_ready = True
|
2016-05-25 18:37:40 +03:00
|
|
|
def cmd_M114(self, params):
|
|
|
|
# Get Current Position
|
2016-11-23 03:38:51 +03:00
|
|
|
if self.toolhead is None:
|
|
|
|
self.cmd_default(params)
|
|
|
|
return
|
2017-12-06 18:13:58 +03:00
|
|
|
raw_pos = homing.query_position(self.toolhead)
|
2017-10-04 02:28:20 +03:00
|
|
|
self.respond("X:%.3f Y:%.3f Z:%.3f E:%.3f Count %s" % (
|
2016-05-25 18:37:40 +03:00
|
|
|
self.last_position[0], self.last_position[1],
|
|
|
|
self.last_position[2], self.last_position[3],
|
2017-10-04 02:28:20 +03:00
|
|
|
" ".join(["%s:%d" % (n.upper(), p) for n, p in raw_pos])))
|
2016-12-29 05:45:29 +03:00
|
|
|
cmd_M115_when_not_ready = True
|
|
|
|
def cmd_M115(self, params):
|
|
|
|
# Get Firmware Version and Capabilities
|
2017-08-22 00:19:43 +03:00
|
|
|
software_version = self.printer.get_start_args().get('software_version')
|
|
|
|
kw = {"FIRMWARE_NAME": "Klipper", "FIRMWARE_VERSION": software_version}
|
2016-12-29 05:45:29 +03:00
|
|
|
self.ack(" ".join(["%s:%s" % (k, v) for k, v in kw.items()]))
|
2017-04-20 17:11:10 +03:00
|
|
|
cmd_IGNORE_when_not_ready = True
|
|
|
|
cmd_IGNORE_aliases = ["G21", "M110", "M21"]
|
|
|
|
def cmd_IGNORE(self, params):
|
|
|
|
# Commands that are just silently accepted
|
|
|
|
pass
|
2016-12-01 06:34:38 +03:00
|
|
|
cmd_QUERY_ENDSTOPS_help = "Report on the status of each endstop"
|
|
|
|
cmd_QUERY_ENDSTOPS_aliases = ["M119"]
|
|
|
|
def cmd_QUERY_ENDSTOPS(self, params):
|
|
|
|
# Get Endstop Status
|
2017-12-06 18:13:58 +03:00
|
|
|
res = homing.query_endstops(self.toolhead)
|
2016-12-08 20:42:12 +03:00
|
|
|
self.respond(" ".join(["%s:%s" % (name, ["open", "TRIGGERED"][not not t])
|
|
|
|
for name, t in res]))
|
2016-12-01 06:34:38 +03:00
|
|
|
cmd_PID_TUNE_help = "Run PID Tuning"
|
|
|
|
cmd_PID_TUNE_aliases = ["M303"]
|
|
|
|
def cmd_PID_TUNE(self, params):
|
2016-05-25 18:37:40 +03:00
|
|
|
# Run PID tuning
|
2017-04-29 22:26:53 +03:00
|
|
|
heater_index = self.get_int('E', params, 0)
|
|
|
|
if (heater_index < -1 or heater_index >= len(self.heaters) - 1
|
|
|
|
or self.heaters[heater_index] is None):
|
2017-04-26 01:34:15 +03:00
|
|
|
self.respond_error("Heater not configured")
|
2017-04-29 22:26:53 +03:00
|
|
|
heater = self.heaters[heater_index]
|
2017-03-16 06:40:46 +03:00
|
|
|
temp = self.get_float('S', params)
|
2016-05-25 18:37:40 +03:00
|
|
|
heater.start_auto_tune(temp)
|
|
|
|
self.bg_temp(heater)
|
2017-03-09 06:26:10 +03:00
|
|
|
def prep_restart(self):
|
2017-02-13 02:42:29 +03:00
|
|
|
if self.is_printer_ready:
|
|
|
|
self.respond_info("Preparing to restart...")
|
|
|
|
self.motor_heater_off()
|
|
|
|
self.toolhead.dwell(0.500)
|
|
|
|
self.toolhead.wait_moves()
|
2017-03-09 06:26:10 +03:00
|
|
|
cmd_RESTART_when_not_ready = True
|
|
|
|
cmd_RESTART_help = "Reload config file and restart host software"
|
|
|
|
def cmd_RESTART(self, params):
|
|
|
|
self.prep_restart()
|
|
|
|
self.printer.request_exit('restart')
|
|
|
|
cmd_FIRMWARE_RESTART_when_not_ready = True
|
|
|
|
cmd_FIRMWARE_RESTART_help = "Restart firmware, host, and reload config"
|
|
|
|
def cmd_FIRMWARE_RESTART(self, params):
|
|
|
|
self.prep_restart()
|
|
|
|
self.printer.request_exit('firmware_restart')
|
2017-09-02 18:59:27 +03:00
|
|
|
cmd_ECHO_when_not_ready = True
|
|
|
|
def cmd_ECHO(self, params):
|
|
|
|
self.respond_info(params['#original'])
|
2016-12-21 19:33:03 +03:00
|
|
|
cmd_STATUS_when_not_ready = True
|
|
|
|
cmd_STATUS_help = "Report the printer status"
|
|
|
|
def cmd_STATUS(self, params):
|
|
|
|
msg = self.printer.get_state_message()
|
|
|
|
if self.is_printer_ready:
|
|
|
|
self.respond_info(msg)
|
|
|
|
else:
|
|
|
|
self.respond_error(msg)
|
2016-12-01 07:47:40 +03:00
|
|
|
cmd_HELP_when_not_ready = True
|
2016-12-01 06:34:38 +03:00
|
|
|
def cmd_HELP(self, params):
|
2016-12-21 19:33:03 +03:00
|
|
|
cmdhelp = []
|
|
|
|
if not self.is_printer_ready:
|
|
|
|
cmdhelp.append("Printer is not ready - not all commands available.")
|
|
|
|
cmdhelp.append("Available extended commands:")
|
2017-04-26 17:44:22 +03:00
|
|
|
for cmd in sorted(self.gcode_handlers):
|
2017-12-04 02:53:42 +03:00
|
|
|
if cmd in self.gcode_help:
|
|
|
|
cmdhelp.append("%-10s: %s" % (cmd, self.gcode_help[cmd]))
|
2016-12-21 19:33:03 +03:00
|
|
|
self.respond_info("\n".join(cmdhelp))
|