2016-05-25 17:37:40 +02:00
|
|
|
# Parse gcode commands
|
|
|
|
#
|
2019-05-25 01:37:08 +02:00
|
|
|
# Copyright (C) 2016-2019 Kevin O'Connor <kevin@koconnor.net>
|
2016-05-25 17:37:40 +02:00
|
|
|
#
|
|
|
|
# This file may be distributed under the terms of the GNU GPLv3 license.
|
2019-01-02 23:45:35 +01:00
|
|
|
import os, re, logging, collections, shlex
|
2019-11-13 17:02:26 +01:00
|
|
|
import homing
|
2016-05-25 17:37:40 +02:00
|
|
|
|
2017-12-04 01:30:49 +01:00
|
|
|
# Parse and handle G-Code commands
|
2016-05-25 17:37:40 +02:00
|
|
|
class GCodeParser:
|
2019-06-03 18:48:35 +02:00
|
|
|
error = homing.CommandError
|
2016-05-25 17:37:40 +02:00
|
|
|
RETRY_TIME = 0.100
|
2017-08-21 23:19:43 +02:00
|
|
|
def __init__(self, printer, fd):
|
2016-05-25 17:37:40 +02:00
|
|
|
self.printer = printer
|
|
|
|
self.fd = fd
|
2019-05-25 00:45:18 +02:00
|
|
|
printer.register_event_handler("klippy:ready", self._handle_ready)
|
|
|
|
printer.register_event_handler("klippy:shutdown", self._handle_shutdown)
|
2019-01-08 16:58:35 +01:00
|
|
|
printer.register_event_handler("klippy:disconnect",
|
2019-05-25 00:45:18 +02:00
|
|
|
self._handle_disconnect)
|
2019-11-25 01:43:50 +01:00
|
|
|
printer.register_event_handler("extruder:activate_extruder",
|
|
|
|
self._handle_activate_extruder)
|
2016-05-25 17:37:40 +02:00
|
|
|
# Input handling
|
2018-01-20 04:22:17 +01:00
|
|
|
self.reactor = printer.get_reactor()
|
2016-11-16 16:56:10 +01:00
|
|
|
self.is_processing_data = False
|
2017-08-21 23:19:43 +02:00
|
|
|
self.is_fileinput = not not printer.get_start_args().get("debuginput")
|
2016-05-25 17:37:40 +02:00
|
|
|
self.fd_handle = None
|
2017-08-21 23:19:43 +02:00
|
|
|
if not self.is_fileinput:
|
2019-02-27 19:04:19 +01:00
|
|
|
self.fd_handle = self.reactor.register_fd(self.fd,
|
2019-05-25 00:45:18 +02:00
|
|
|
self._process_data)
|
2017-05-20 03:05:46 +02:00
|
|
|
self.partial_input = ""
|
2018-02-02 15:38:04 +01:00
|
|
|
self.pending_commands = []
|
2016-05-25 17:37:40 +02:00
|
|
|
self.bytes_read = 0
|
2016-09-25 00:10:24 +02:00
|
|
|
self.input_log = collections.deque([], 50)
|
2016-05-25 17:37:40 +02:00
|
|
|
# Command handling
|
2016-11-23 01:38:51 +01:00
|
|
|
self.is_printer_ready = False
|
2019-06-09 18:01:58 +02:00
|
|
|
self.mutex = self.reactor.mutex()
|
2017-12-04 00:53:42 +01:00
|
|
|
self.base_gcode_handlers = self.gcode_handlers = {}
|
|
|
|
self.ready_gcode_handlers = {}
|
2018-05-20 18:33:43 +02:00
|
|
|
self.mux_commands = {}
|
2017-12-04 00:53:42 +01:00
|
|
|
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 02:45:07 +01:00
|
|
|
# G-Code coordinate manipulation
|
2019-05-27 02:59:44 +02:00
|
|
|
self.absolute_coord = self.absolute_extrude = True
|
2017-12-22 02:45:07 +01:00
|
|
|
self.base_position = [0.0, 0.0, 0.0, 0.0]
|
|
|
|
self.last_position = [0.0, 0.0, 0.0, 0.0]
|
2018-04-21 01:42:26 +02:00
|
|
|
self.homing_position = [0.0, 0.0, 0.0, 0.0]
|
2019-05-25 01:37:08 +02:00
|
|
|
self.speed = 25.
|
2017-12-22 02:53:44 +01:00
|
|
|
self.speed_factor = 1. / 60.
|
2017-12-22 03:33:38 +01:00
|
|
|
self.extrude_factor = 1.
|
2019-05-26 17:08:48 +02:00
|
|
|
# G-Code state
|
|
|
|
self.saved_states = {}
|
2018-01-21 05:47:36 +01:00
|
|
|
self.move_transform = self.move_with_transform = None
|
|
|
|
self.position_with_transform = (lambda: [0., 0., 0., 0.])
|
2016-05-25 17:37:40 +02:00
|
|
|
self.need_ack = False
|
2019-11-13 17:29:27 +01:00
|
|
|
self.toolhead = None
|
2019-02-26 02:04:05 +01:00
|
|
|
self.heaters = None
|
2016-05-25 17:37:40 +02:00
|
|
|
self.axis2pos = {'X': 0, 'Y': 1, 'Z': 2, 'E': 3}
|
2017-12-04 00:53:42 +01:00
|
|
|
def register_command(self, cmd, func, when_not_ready=False, desc=None):
|
2018-01-17 17:26:09 +01: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
|
2018-05-20 19:04:52 +02:00
|
|
|
if cmd in self.ready_gcode_handlers:
|
2019-02-19 00:04:42 +01:00
|
|
|
raise self.printer.config_error(
|
|
|
|
"gcode command %s already registered" % (cmd,))
|
2017-12-04 02:22:44 +01:00
|
|
|
if not (len(cmd) >= 2 and not cmd[0].isupper() and cmd[1].isdigit()):
|
|
|
|
origfunc = func
|
2019-05-25 00:45:18 +02:00
|
|
|
func = lambda params: origfunc(self._get_extended_params(params))
|
2017-12-04 00:53:42 +01: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-05-20 18:33:43 +02:00
|
|
|
def register_mux_command(self, cmd, key, value, func, desc=None):
|
|
|
|
prev = self.mux_commands.get(cmd)
|
|
|
|
if prev is None:
|
2019-05-25 00:45:18 +02:00
|
|
|
self.register_command(cmd, self._cmd_mux, desc=desc)
|
2018-05-20 18:33:43 +02:00
|
|
|
self.mux_commands[cmd] = prev = (key, {})
|
|
|
|
prev_key, prev_values = prev
|
|
|
|
if prev_key != key:
|
2019-02-19 00:04:42 +01:00
|
|
|
raise self.printer.config_error(
|
|
|
|
"mux command %s %s %s may have only one key (%s)" % (
|
|
|
|
cmd, key, value, prev_key))
|
2018-05-20 18:33:43 +02:00
|
|
|
if value in prev_values:
|
2019-02-19 00:04:42 +01:00
|
|
|
raise self.printer.config_error(
|
|
|
|
"mux command %s %s %s already registered (%s)" % (
|
|
|
|
cmd, key, value, prev_values))
|
2018-05-20 18:33:43 +02:00
|
|
|
prev_values[value] = func
|
2019-06-12 13:33:28 +02:00
|
|
|
def set_move_transform(self, transform, force=False):
|
|
|
|
if self.move_transform is not None and not force:
|
2018-01-21 05:47:36 +01:00
|
|
|
raise self.printer.config_error(
|
|
|
|
"G-Code move transform already specified")
|
2019-06-12 13:33:28 +02:00
|
|
|
old_transform = self.move_transform
|
|
|
|
if old_transform is None:
|
|
|
|
old_transform = self.toolhead
|
2018-01-21 05:47:36 +01:00
|
|
|
self.move_transform = transform
|
|
|
|
self.move_with_transform = transform.move
|
|
|
|
self.position_with_transform = transform.get_position
|
2019-06-12 13:33:28 +02:00
|
|
|
return old_transform
|
2016-05-25 17:37:40 +02:00
|
|
|
def stats(self, eventtime):
|
2018-02-05 19:52:05 +01:00
|
|
|
return False, "gcodein=%d" % (self.bytes_read,)
|
2019-05-28 17:07:11 +02:00
|
|
|
def _action_emergency_stop(self, msg="action_emergency_stop"):
|
|
|
|
self.printer.invoke_shutdown("Shutdown due to %s" % (msg,))
|
|
|
|
return ""
|
|
|
|
def _action_respond_info(self, msg):
|
|
|
|
self.respond_info(msg)
|
|
|
|
return ""
|
|
|
|
def _action_respond_error(self, msg):
|
|
|
|
self.respond_error(msg)
|
|
|
|
return ""
|
2019-05-25 01:37:08 +02:00
|
|
|
def _get_gcode_position(self):
|
2019-03-21 15:37:28 +01:00
|
|
|
p = [lp - bp for lp, bp in zip(self.last_position, self.base_position)]
|
|
|
|
p[3] /= self.extrude_factor
|
|
|
|
return p
|
2019-05-25 01:37:08 +02:00
|
|
|
def _get_gcode_speed(self):
|
|
|
|
return self.speed / self.speed_factor
|
|
|
|
def _get_gcode_speed_override(self):
|
|
|
|
return self.speed_factor * 60.
|
2018-02-21 02:50:06 +01:00
|
|
|
def get_status(self, eventtime):
|
2019-05-25 01:37:08 +02:00
|
|
|
move_position = self._get_gcode_position()
|
2018-02-21 02:50:06 +01:00
|
|
|
busy = self.is_processing_data
|
2018-09-17 14:25:28 +02:00
|
|
|
return {
|
2019-05-25 01:37:08 +02:00
|
|
|
'speed_factor': self._get_gcode_speed_override(),
|
|
|
|
'speed': self._get_gcode_speed(),
|
2018-09-17 14:25:28 +02:00
|
|
|
'extrude_factor': self.extrude_factor,
|
2019-05-27 02:59:44 +02:00
|
|
|
'abs_extrude': self.absolute_extrude,
|
2018-09-17 14:25:28 +02:00
|
|
|
'busy': busy,
|
2019-03-21 15:37:28 +01:00
|
|
|
'move_xpos': move_position[0],
|
|
|
|
'move_ypos': move_position[1],
|
|
|
|
'move_zpos': move_position[2],
|
|
|
|
'move_epos': move_position[3],
|
2018-09-17 14:25:28 +02:00
|
|
|
'last_xpos': self.last_position[0],
|
|
|
|
'last_ypos': self.last_position[1],
|
|
|
|
'last_zpos': self.last_position[2],
|
|
|
|
'last_epos': self.last_position[3],
|
|
|
|
'base_xpos': self.base_position[0],
|
|
|
|
'base_ypos': self.base_position[1],
|
|
|
|
'base_zpos': self.base_position[2],
|
|
|
|
'base_epos': self.base_position[3],
|
|
|
|
'homing_xpos': self.homing_position[0],
|
|
|
|
'homing_ypos': self.homing_position[1],
|
2019-05-28 17:07:11 +02:00
|
|
|
'homing_zpos': self.homing_position[2],
|
2019-05-28 23:40:19 +02:00
|
|
|
'gcode_position': homing.Coord(*move_position),
|
2019-05-28 17:07:11 +02:00
|
|
|
'action_respond_info': self._action_respond_info,
|
|
|
|
'action_respond_error': self._action_respond_error,
|
|
|
|
'action_emergency_stop': self._action_emergency_stop,
|
2018-09-17 14:25:28 +02:00
|
|
|
}
|
2019-05-25 00:45:18 +02:00
|
|
|
def _handle_shutdown(self):
|
2019-01-08 15:15:40 +01:00
|
|
|
if not self.is_printer_ready:
|
2018-01-20 04:49:27 +01:00
|
|
|
return
|
2019-01-08 15:15:40 +01:00
|
|
|
self.is_printer_ready = False
|
|
|
|
self.gcode_handlers = self.base_gcode_handlers
|
2019-05-25 00:45:18 +02:00
|
|
|
self._dump_debug()
|
2019-01-08 15:15:40 +01:00
|
|
|
if self.is_fileinput:
|
|
|
|
self.printer.request_exit('error_exit')
|
|
|
|
self._respond_state("Shutdown")
|
2019-05-25 00:45:18 +02:00
|
|
|
def _handle_disconnect(self):
|
2019-01-08 16:58:35 +01:00
|
|
|
self._respond_state("Disconnect")
|
2019-05-25 00:45:18 +02:00
|
|
|
def _handle_ready(self):
|
2017-10-12 21:15:14 +02:00
|
|
|
self.is_printer_ready = True
|
2017-12-04 00:53:42 +01:00
|
|
|
self.gcode_handlers = self.ready_gcode_handlers
|
2017-04-26 00:34:15 +02:00
|
|
|
# Lookup printer components
|
2019-02-26 02:04:05 +01:00
|
|
|
self.heaters = self.printer.lookup_object('heater')
|
2018-01-20 04:22:17 +01:00
|
|
|
self.toolhead = self.printer.lookup_object('toolhead')
|
2018-01-21 05:47:36 +01:00
|
|
|
if self.move_transform is None:
|
|
|
|
self.move_with_transform = self.toolhead.move
|
|
|
|
self.position_with_transform = self.toolhead.get_position
|
2017-04-26 00:34:15 +02:00
|
|
|
if self.is_fileinput and self.fd_handle is None:
|
2019-02-27 19:04:19 +01:00
|
|
|
self.fd_handle = self.reactor.register_fd(self.fd,
|
2019-05-25 00:45:18 +02:00
|
|
|
self._process_data)
|
2018-09-13 00:46:25 +02:00
|
|
|
self._respond_state("Ready")
|
2019-11-25 01:43:50 +01:00
|
|
|
def _handle_activate_extruder(self):
|
|
|
|
self.reset_last_position()
|
|
|
|
self.extrude_factor = 1.
|
|
|
|
self.base_position[3] = self.last_position[3]
|
2017-12-04 01:30:49 +01:00
|
|
|
def reset_last_position(self):
|
2018-01-21 05:47:36 +01:00
|
|
|
self.last_position = self.position_with_transform()
|
2019-05-25 00:45:18 +02:00
|
|
|
def _dump_debug(self):
|
2017-09-27 17:54:53 +02:00
|
|
|
out = []
|
|
|
|
out.append("Dumping gcode input %d blocks" % (
|
2016-12-01 01:05:25 +01:00
|
|
|
len(self.input_log),))
|
|
|
|
for eventtime, data in self.input_log:
|
2017-09-27 17:54:53 +02:00
|
|
|
out.append("Read %f: %s" % (eventtime, repr(data)))
|
2017-12-22 04:23:07 +01:00
|
|
|
out.append(
|
2019-05-27 02:59:44 +02:00
|
|
|
"gcode state: absolute_coord=%s absolute_extrude=%s"
|
2018-04-21 01:42:26 +02:00
|
|
|
" base_position=%s last_position=%s homing_position=%s"
|
2017-12-22 03:33:38 +01:00
|
|
|
" speed_factor=%s extrude_factor=%s speed=%s" % (
|
2019-05-27 02:59:44 +02:00
|
|
|
self.absolute_coord, self.absolute_extrude,
|
2018-04-21 01:42:26 +02:00
|
|
|
self.base_position, self.last_position, self.homing_position,
|
2017-12-22 03:33:38 +01:00
|
|
|
self.speed_factor, self.extrude_factor, self.speed))
|
2017-09-27 17:54:53 +02:00
|
|
|
logging.info("\n".join(out))
|
2016-05-25 17:37:40 +02:00
|
|
|
# Parse input into commands
|
2017-10-10 17:12:15 +02:00
|
|
|
args_r = re.compile('([A-Z_]+|[A-Z*/])')
|
2019-05-25 00:45:18 +02:00
|
|
|
def _process_commands(self, commands, need_ack=True):
|
2017-05-20 03:05:46 +02:00
|
|
|
for line in commands:
|
2016-05-25 17:37:40 +02: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 21:04:01 +02:00
|
|
|
parts = self.args_r.split(line.upper())[1:]
|
|
|
|
params = { parts[i]: parts[i+1].strip()
|
2017-06-06 18:35:13 +02:00
|
|
|
for i in range(0, len(parts), 2) }
|
2016-05-25 17:37:40 +02:00
|
|
|
params['#original'] = origline
|
2017-06-06 21:04:01 +02:00
|
|
|
if parts and parts[0] == 'N':
|
2016-05-25 17:37:40 +02:00
|
|
|
# Skip line number at start of command
|
|
|
|
del parts[:2]
|
|
|
|
if not parts:
|
2018-02-01 16:55:45 +01:00
|
|
|
# Treat empty line as empty command
|
|
|
|
parts = ['', '']
|
2017-06-06 21:04:01 +02:00
|
|
|
params['#command'] = cmd = parts[0] + parts[1].strip()
|
2016-05-25 17:37:40 +02:00
|
|
|
# Invoke handler for command
|
2017-05-20 03:05:46 +02:00
|
|
|
self.need_ack = need_ack
|
2016-05-25 17:37:40 +02:00
|
|
|
handler = self.gcode_handlers.get(cmd, self.cmd_default)
|
|
|
|
try:
|
|
|
|
handler(params)
|
2019-06-03 18:48:35 +02:00
|
|
|
except self.error as e:
|
2017-03-16 04:40:46 +01:00
|
|
|
self.respond_error(str(e))
|
2017-12-04 01:30:49 +01:00
|
|
|
self.reset_last_position()
|
2018-02-01 18:13:48 +01:00
|
|
|
if not need_ack:
|
|
|
|
raise
|
2016-05-25 17:37:40 +02:00
|
|
|
except:
|
2017-10-12 21:15:14 +02:00
|
|
|
msg = 'Internal error on command:"%s"' % (cmd,)
|
|
|
|
logging.exception(msg)
|
|
|
|
self.printer.invoke_shutdown(msg)
|
|
|
|
self.respond_error(msg)
|
2018-02-01 18:13:48 +01:00
|
|
|
if not need_ack:
|
|
|
|
raise
|
2016-05-25 17:37:40 +02:00
|
|
|
self.ack()
|
2018-02-02 15:38:04 +01:00
|
|
|
m112_r = re.compile('^(?:[nN][0-9]+)?\s*[mM]112(?:\s|$)')
|
2019-05-25 00:45:18 +02:00
|
|
|
def _process_data(self, eventtime):
|
2018-02-02 15:38:04 +01:00
|
|
|
# Read input, separate by newline, and add to pending_commands
|
2018-09-13 00:27:33 +02:00
|
|
|
try:
|
|
|
|
data = os.read(self.fd, 4096)
|
|
|
|
except os.error:
|
|
|
|
logging.exception("Read g-code")
|
|
|
|
return
|
2016-09-25 00:10:24 +02:00
|
|
|
self.input_log.append((eventtime, data))
|
2016-05-25 17:37:40 +02:00
|
|
|
self.bytes_read += len(data)
|
|
|
|
lines = data.split('\n')
|
2017-05-20 03:05:46 +02:00
|
|
|
lines[0] = self.partial_input + lines[0]
|
|
|
|
self.partial_input = lines.pop()
|
2018-02-02 15:38:04 +01:00
|
|
|
pending_commands = self.pending_commands
|
|
|
|
pending_commands.extend(lines)
|
2018-02-11 18:49:33 +01:00
|
|
|
# Special handling for debug file input EOF
|
|
|
|
if not data and self.is_fileinput:
|
|
|
|
if not self.is_processing_data:
|
2019-03-01 16:06:44 +01:00
|
|
|
self.reactor.unregister_fd(self.fd_handle)
|
|
|
|
self.fd_handle = None
|
2018-02-11 19:52:20 +01:00
|
|
|
self.request_restart('exit')
|
2018-02-11 18:49:33 +01:00
|
|
|
pending_commands.append("")
|
|
|
|
# Handle case where multiple commands pending
|
|
|
|
if self.is_processing_data or len(pending_commands) > 1:
|
|
|
|
if len(pending_commands) < 20:
|
|
|
|
# Check for M112 out-of-order
|
|
|
|
for line in lines:
|
|
|
|
if self.m112_r.match(line) is not None:
|
|
|
|
self.cmd_M112({})
|
|
|
|
if self.is_processing_data:
|
|
|
|
if len(pending_commands) >= 20:
|
|
|
|
# Stop reading input
|
|
|
|
self.reactor.unregister_fd(self.fd_handle)
|
|
|
|
self.fd_handle = None
|
|
|
|
return
|
2018-02-02 15:38:04 +01:00
|
|
|
# Process commands
|
2016-11-16 16:56:10 +01:00
|
|
|
self.is_processing_data = True
|
2018-02-02 15:38:04 +01:00
|
|
|
while pending_commands:
|
|
|
|
self.pending_commands = []
|
2019-06-09 18:01:58 +02:00
|
|
|
with self.mutex:
|
|
|
|
self._process_commands(pending_commands)
|
2018-02-02 15:38:04 +01:00
|
|
|
pending_commands = self.pending_commands
|
2019-06-09 18:01:58 +02:00
|
|
|
self.is_processing_data = False
|
2018-02-02 15:38:04 +01:00
|
|
|
if self.fd_handle is None:
|
2019-02-27 19:04:19 +01:00
|
|
|
self.fd_handle = self.reactor.register_fd(self.fd,
|
2019-05-25 00:45:18 +02:00
|
|
|
self._process_data)
|
2018-06-30 20:08:02 +02:00
|
|
|
def run_script_from_command(self, script):
|
2018-02-03 00:28:48 +01:00
|
|
|
prev_need_ack = self.need_ack
|
|
|
|
try:
|
2019-05-25 00:45:18 +02:00
|
|
|
self._process_commands(script.split('\n'), need_ack=False)
|
2018-02-03 00:28:48 +01:00
|
|
|
finally:
|
|
|
|
self.need_ack = prev_need_ack
|
2018-06-30 20:10:59 +02:00
|
|
|
def run_script(self, script):
|
2019-06-09 18:01:58 +02:00
|
|
|
with self.mutex:
|
|
|
|
self._process_commands(script.split('\n'), need_ack=False)
|
2019-06-09 19:33:21 +02:00
|
|
|
def get_mutex(self):
|
|
|
|
return self.mutex
|
2016-05-25 17:37:40 +02:00
|
|
|
# Response handling
|
|
|
|
def ack(self, msg=None):
|
2016-11-21 02:40:31 +01:00
|
|
|
if not self.need_ack or self.is_fileinput:
|
2016-05-25 17:37:40 +02:00
|
|
|
return
|
2018-09-13 00:27:33 +02:00
|
|
|
try:
|
|
|
|
if msg:
|
|
|
|
os.write(self.fd, "ok %s\n" % (msg,))
|
|
|
|
else:
|
|
|
|
os.write(self.fd, "ok\n")
|
|
|
|
except os.error:
|
|
|
|
logging.exception("Write g-code ack")
|
2016-05-25 17:37:40 +02:00
|
|
|
self.need_ack = False
|
|
|
|
def respond(self, msg):
|
2016-11-21 02:40:31 +01:00
|
|
|
if self.is_fileinput:
|
2016-05-25 17:37:40 +02:00
|
|
|
return
|
2018-09-13 00:27:33 +02:00
|
|
|
try:
|
|
|
|
os.write(self.fd, msg+"\n")
|
|
|
|
except os.error:
|
|
|
|
logging.exception("Write g-code response")
|
2019-03-04 19:04:18 +01:00
|
|
|
def respond_info(self, msg, log=True):
|
|
|
|
if log:
|
|
|
|
logging.info(msg)
|
2016-12-21 17:33:03 +01:00
|
|
|
lines = [l.strip() for l in msg.strip().split('\n')]
|
|
|
|
self.respond("// " + "\n// ".join(lines))
|
2016-11-30 20:30:45 +01:00
|
|
|
def respond_error(self, msg):
|
2017-07-17 17:24:15 +02:00
|
|
|
logging.warning(msg)
|
2016-11-30 20:30:45 +01:00
|
|
|
lines = msg.strip().split('\n')
|
2016-12-21 17:33:03 +01:00
|
|
|
if len(lines) > 1:
|
2019-03-04 19:04:18 +01:00
|
|
|
self.respond_info("\n".join(lines), log=False)
|
2018-03-13 03:37:14 +01:00
|
|
|
self.respond('!! %s' % (lines[0].strip(),))
|
2018-06-16 21:15:17 +02:00
|
|
|
if self.is_fileinput:
|
|
|
|
self.printer.request_exit('error_exit')
|
2018-09-13 00:46:25 +02:00
|
|
|
def _respond_state(self, state):
|
2019-03-04 19:04:18 +01:00
|
|
|
self.respond_info("Klipper state: %s" % (state,), log=False)
|
2017-03-16 04:40:46 +01:00
|
|
|
# Parameter parsing helpers
|
2017-12-04 02:16:20 +01:00
|
|
|
class sentinel: pass
|
2018-04-21 03:41:13 +02:00
|
|
|
def get_str(self, name, params, default=sentinel, parser=str,
|
|
|
|
minval=None, maxval=None, above=None, below=None):
|
|
|
|
if name not in params:
|
|
|
|
if default is self.sentinel:
|
2019-06-03 18:48:35 +02:00
|
|
|
raise self.error("Error on '%s': missing %s" % (
|
2018-04-21 03:41:13 +02:00
|
|
|
params['#original'], name))
|
2017-03-16 04:40:46 +01:00
|
|
|
return default
|
2018-04-21 03:41:13 +02:00
|
|
|
try:
|
|
|
|
value = parser(params[name])
|
|
|
|
except:
|
2019-06-03 18:48:35 +02:00
|
|
|
raise self.error("Error on '%s': unable to parse %s" % (
|
2018-04-21 03:41:13 +02:00
|
|
|
params['#original'], params[name]))
|
|
|
|
if minval is not None and value < minval:
|
2019-06-03 18:48:35 +02:00
|
|
|
raise self.error("Error on '%s': %s must have minimum of %s" % (
|
2018-04-21 03:41:13 +02:00
|
|
|
params['#original'], name, minval))
|
|
|
|
if maxval is not None and value > maxval:
|
2019-06-03 18:48:35 +02:00
|
|
|
raise self.error("Error on '%s': %s must have maximum of %s" % (
|
2018-04-21 03:41:13 +02:00
|
|
|
params['#original'], name, maxval))
|
|
|
|
if above is not None and value <= above:
|
2019-06-03 18:48:35 +02:00
|
|
|
raise self.error("Error on '%s': %s must be above %s" % (
|
2018-04-21 03:41:13 +02:00
|
|
|
params['#original'], name, above))
|
|
|
|
if below is not None and value >= below:
|
2019-06-03 18:48:35 +02:00
|
|
|
raise self.error("Error on '%s': %s must be below %s" % (
|
2018-04-21 03:41:13 +02:00
|
|
|
params['#original'], name, below))
|
|
|
|
return value
|
|
|
|
def get_int(self, name, params, default=sentinel, minval=None, maxval=None):
|
|
|
|
return self.get_str(name, params, default, parser=int,
|
|
|
|
minval=minval, maxval=maxval)
|
|
|
|
def get_float(self, name, params, default=sentinel,
|
|
|
|
minval=None, maxval=None, above=None, below=None):
|
|
|
|
return self.get_str(name, params, default, parser=float, minval=minval,
|
|
|
|
maxval=maxval, above=above, below=below)
|
2017-06-06 21:04:01 +02: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*(?:[#*;].*)?$')
|
2019-05-25 00:45:18 +02:00
|
|
|
def _get_extended_params(self, params):
|
2017-06-06 21:04:01 +02:00
|
|
|
m = self.extended_r.match(params['#original'])
|
|
|
|
if m is None:
|
|
|
|
# Not an "extended" command
|
|
|
|
return params
|
|
|
|
eargs = m.group('args')
|
|
|
|
try:
|
2019-01-02 23:45:35 +01:00
|
|
|
eparams = [earg.split('=', 1) for earg in shlex.split(eargs)]
|
2017-10-03 03:48:45 +02: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 21:04:01 +02:00
|
|
|
except ValueError as e:
|
2019-06-03 18:48:35 +02:00
|
|
|
raise self.error("Malformed command '%s'" % (params['#original'],))
|
2016-05-25 17:37:40 +02:00
|
|
|
# Temperature wrappers
|
2019-05-25 00:45:18 +02:00
|
|
|
def _get_temp(self, eventtime):
|
2017-04-29 21:26:53 +02:00
|
|
|
# Tn:XXX /YYY B:XXX /YYY
|
2016-05-25 17:37:40 +02:00
|
|
|
out = []
|
2019-02-26 02:04:05 +01:00
|
|
|
if self.heaters is not None:
|
2019-02-26 02:30:37 +01:00
|
|
|
for gcode_id, sensor in sorted(self.heaters.get_gcode_sensors()):
|
|
|
|
cur, target = sensor.get_temp(eventtime)
|
|
|
|
out.append("%s:%.1f /%.1f" % (gcode_id, cur, target))
|
2017-10-12 04:16:02 +02:00
|
|
|
if not out:
|
|
|
|
return "T:0"
|
2016-05-25 17:37:40 +02:00
|
|
|
return " ".join(out)
|
|
|
|
def bg_temp(self, heater):
|
2016-11-21 02:40:31 +01:00
|
|
|
if self.is_fileinput:
|
2016-05-25 17:37:40 +02:00
|
|
|
return
|
2017-02-06 19:31:34 +01:00
|
|
|
eventtime = self.reactor.monotonic()
|
2016-11-17 18:37:57 +01:00
|
|
|
while self.is_printer_ready and heater.check_busy(eventtime):
|
2017-02-06 20:07:55 +01:00
|
|
|
print_time = self.toolhead.get_last_move_time()
|
2019-05-25 00:45:18 +02:00
|
|
|
self.respond(self._get_temp(eventtime))
|
2016-11-17 18:37:57 +01:00
|
|
|
eventtime = self.reactor.pause(eventtime + 1.)
|
2019-05-25 00:45:18 +02:00
|
|
|
def _set_temp(self, params, is_bed=False, wait=False):
|
2017-05-01 20:58:07 +02:00
|
|
|
temp = self.get_float('S', params, 0.)
|
2019-02-26 02:50:34 +01:00
|
|
|
heater = None
|
2017-04-29 21:26:53 +02:00
|
|
|
if is_bed:
|
2019-02-26 02:50:34 +01:00
|
|
|
heater = self.printer.lookup_object('heater_bed', None)
|
|
|
|
elif 'T' in params:
|
|
|
|
index = self.get_int('T', params, minval=0)
|
2019-11-07 22:24:52 +01:00
|
|
|
section = 'extruder'
|
|
|
|
if index:
|
|
|
|
section = 'extruder%d' % (index,)
|
|
|
|
extruder = self.printer.lookup_object(section, None)
|
2019-02-26 02:50:34 +01:00
|
|
|
if extruder is not None:
|
|
|
|
heater = extruder.get_heater()
|
2019-11-13 17:02:26 +01:00
|
|
|
else:
|
|
|
|
heater = self.toolhead.get_extruder().get_heater()
|
2019-02-26 02:50:34 +01:00
|
|
|
if heater is None:
|
|
|
|
if temp > 0.:
|
|
|
|
self.respond_error("Heater not configured")
|
|
|
|
return
|
2016-07-07 20:23:48 +02:00
|
|
|
print_time = self.toolhead.get_last_move_time()
|
2019-12-17 01:16:58 +01:00
|
|
|
heater.set_temp(print_time, temp)
|
2018-01-03 16:33:10 +01:00
|
|
|
if wait and temp:
|
2016-05-25 17:37:40 +02:00
|
|
|
self.bg_temp(heater)
|
2017-12-22 02:45:07 +01:00
|
|
|
# G-Code special command handlers
|
2016-05-25 17:37:40 +02:00
|
|
|
def cmd_default(self, params):
|
2016-11-23 01:38:51 +01:00
|
|
|
if not self.is_printer_ready:
|
2016-11-30 20:30:45 +01:00
|
|
|
self.respond_error(self.printer.get_state_message())
|
2016-05-25 17:37:40 +02:00
|
|
|
return
|
|
|
|
cmd = params.get('#command')
|
|
|
|
if not cmd:
|
|
|
|
logging.debug(params['#original'])
|
|
|
|
return
|
2019-11-25 01:59:36 +01:00
|
|
|
if cmd.startswith("M117 "):
|
2018-08-22 02:59:51 +02:00
|
|
|
# Handle M117 gcode with numeric and special characters
|
|
|
|
handler = self.gcode_handlers.get("M117", None)
|
|
|
|
if handler is not None:
|
|
|
|
handler(params)
|
|
|
|
return
|
2019-11-13 17:29:27 +01:00
|
|
|
elif cmd == 'M107' or (cmd == 'M106' and (
|
|
|
|
not self.get_float('S', params, 1.) or self.is_fileinput)):
|
|
|
|
# Don't warn about requests to turn off fan when fan not present
|
|
|
|
return
|
2017-07-17 17:24:15 +02:00
|
|
|
self.respond_info('Unknown command:"%s"' % (cmd,))
|
2019-05-25 00:45:18 +02:00
|
|
|
def _cmd_mux(self, params):
|
2018-05-20 18:33:43 +02:00
|
|
|
key, values = self.mux_commands[params['#command']]
|
|
|
|
if None in values:
|
|
|
|
key_param = self.get_str(key, params, None)
|
|
|
|
else:
|
|
|
|
key_param = self.get_str(key, params)
|
|
|
|
if key_param not in values:
|
2019-06-03 18:48:35 +02:00
|
|
|
raise self.error("The value '%s' is not valid for %s" % (
|
|
|
|
key_param, key))
|
2018-05-20 18:33:43 +02:00
|
|
|
values[key_param](params)
|
2017-04-26 00:34:15 +02:00
|
|
|
all_handlers = [
|
2019-11-10 17:55:53 +01:00
|
|
|
'G1', 'G4', 'G28', 'M400',
|
2018-04-21 01:58:37 +02:00
|
|
|
'G20', 'M82', 'M83', 'G90', 'G91', 'G92', 'M114', 'M220', 'M221',
|
2019-11-25 03:10:51 +01:00
|
|
|
'SET_GCODE_OFFSET', 'SAVE_GCODE_STATE', 'RESTORE_GCODE_STATE',
|
2019-11-13 17:29:27 +01:00
|
|
|
'M105', 'M104', 'M109', 'M140', 'M190',
|
2018-07-16 16:06:30 +02:00
|
|
|
'M112', 'M115', 'IGNORE', 'GET_POSITION',
|
2017-06-06 21:04:01 +02:00
|
|
|
'RESTART', 'FIRMWARE_RESTART', 'ECHO', 'STATUS', 'HELP']
|
2017-12-22 02:45:07 +01:00
|
|
|
# G-Code movement commands
|
2016-12-01 04:34:38 +01:00
|
|
|
cmd_G1_aliases = ['G0']
|
|
|
|
def cmd_G1(self, params):
|
2016-05-25 17:37:40 +02:00
|
|
|
# Move
|
2017-03-16 04:40:46 +01:00
|
|
|
try:
|
2017-12-22 03:33:38 +01:00
|
|
|
for axis in 'XYZ':
|
|
|
|
if axis in params:
|
|
|
|
v = float(params[axis])
|
|
|
|
pos = self.axis2pos[axis]
|
2019-05-27 02:59:44 +02:00
|
|
|
if not self.absolute_coord:
|
2017-03-16 04:40:46 +01:00
|
|
|
# value relative to position of last move
|
2017-12-22 03:33:38 +01:00
|
|
|
self.last_position[pos] += v
|
2017-03-16 04:40:46 +01:00
|
|
|
else:
|
|
|
|
# value relative to base coordinate position
|
2017-12-22 03:33:38 +01:00
|
|
|
self.last_position[pos] = v + self.base_position[pos]
|
|
|
|
if 'E' in params:
|
|
|
|
v = float(params['E']) * self.extrude_factor
|
2019-05-27 02:59:44 +02:00
|
|
|
if not self.absolute_coord or not self.absolute_extrude:
|
2017-12-22 03:33:38 +01:00
|
|
|
# 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 04:40:46 +01:00
|
|
|
if 'F' in params:
|
2019-05-25 01:37:08 +02:00
|
|
|
gcode_speed = float(params['F'])
|
|
|
|
if gcode_speed <= 0.:
|
2019-06-03 18:48:35 +02:00
|
|
|
raise self.error("Invalid speed in '%s'" % (
|
2019-02-27 19:04:19 +01:00
|
|
|
params['#original'],))
|
2019-05-25 01:37:08 +02:00
|
|
|
self.speed = gcode_speed * self.speed_factor
|
2017-06-10 05:32:49 +02:00
|
|
|
except ValueError as e:
|
2019-06-03 18:48:35 +02:00
|
|
|
raise self.error("Unable to parse move '%s'" % (
|
|
|
|
params['#original'],))
|
2019-06-03 18:53:50 +02:00
|
|
|
self.move_with_transform(self.last_position, self.speed)
|
2016-05-25 17:37:40 +02:00
|
|
|
def cmd_G4(self, params):
|
|
|
|
# Dwell
|
2019-11-25 03:16:53 +01:00
|
|
|
delay = self.get_float('P', params, 0., minval=0.) / 1000.
|
2016-07-07 20:23:48 +02:00
|
|
|
self.toolhead.dwell(delay)
|
2016-05-25 17:37:40 +02:00
|
|
|
def cmd_G28(self, params):
|
|
|
|
# Move to origin
|
2016-07-26 05:47:30 +02:00
|
|
|
axes = []
|
|
|
|
for axis in 'XYZ':
|
|
|
|
if axis in params:
|
|
|
|
axes.append(self.axis2pos[axis])
|
|
|
|
if not axes:
|
|
|
|
axes = [0, 1, 2]
|
2018-10-08 20:23:20 +02:00
|
|
|
homing_state = homing.Homing(self.printer)
|
2016-11-21 02:40:31 +01:00
|
|
|
if self.is_fileinput:
|
2016-11-28 17:23:26 +01:00
|
|
|
homing_state.set_no_verify_retract()
|
2019-06-03 18:53:50 +02:00
|
|
|
homing_state.home_axes(axes)
|
2016-12-09 00:12:20 +01:00
|
|
|
for axis in homing_state.get_axes():
|
2018-04-21 01:42:26 +02:00
|
|
|
self.base_position[axis] = self.homing_position[axis]
|
2018-01-17 03:46:42 +01:00
|
|
|
self.reset_last_position()
|
2017-12-22 02:45:07 +01:00
|
|
|
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
|
2019-05-27 02:59:44 +02:00
|
|
|
self.absolute_extrude = True
|
2017-12-22 02:45:07 +01:00
|
|
|
def cmd_M83(self, params):
|
|
|
|
# Use relative distances for extrusion
|
2019-05-27 02:59:44 +02:00
|
|
|
self.absolute_extrude = False
|
2016-05-25 17:37:40 +02:00
|
|
|
def cmd_G90(self, params):
|
|
|
|
# Use absolute coordinates
|
2019-05-27 02:59:44 +02:00
|
|
|
self.absolute_coord = True
|
2016-05-25 17:37:40 +02:00
|
|
|
def cmd_G91(self, params):
|
|
|
|
# Use relative coordinates
|
2019-05-27 02:59:44 +02:00
|
|
|
self.absolute_coord = False
|
2016-05-25 17:37:40 +02:00
|
|
|
def cmd_G92(self, params):
|
|
|
|
# Set position
|
2017-03-16 04:40:46 +01: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 03:33:38 +01:00
|
|
|
if p == 3:
|
|
|
|
offset *= self.extrude_factor
|
2017-03-16 04:40:46 +01:00
|
|
|
self.base_position[p] = self.last_position[p] - offset
|
|
|
|
if not offsets:
|
2016-05-25 17:37:40 +02:00
|
|
|
self.base_position = list(self.last_position)
|
2018-03-10 03:43:24 +01:00
|
|
|
cmd_M114_when_not_ready = True
|
|
|
|
def cmd_M114(self, params):
|
|
|
|
# Get Current Position
|
2019-05-25 01:37:08 +02:00
|
|
|
p = self._get_gcode_position()
|
2018-03-10 03:43:24 +01:00
|
|
|
self.respond("X:%.3f Y:%.3f Z:%.3f E:%.3f" % tuple(p))
|
2017-12-22 02:53:44 +01:00
|
|
|
def cmd_M220(self, params):
|
|
|
|
# Set speed factor override percentage
|
2018-04-21 03:41:13 +02:00
|
|
|
value = self.get_float('S', params, 100., above=0.) / (60. * 100.)
|
2019-05-25 01:37:08 +02:00
|
|
|
self.speed = self._get_gcode_speed() * value
|
2017-12-22 02:53:44 +01:00
|
|
|
self.speed_factor = value
|
2017-12-22 03:33:38 +01:00
|
|
|
def cmd_M221(self, params):
|
|
|
|
# Set extrude factor override percentage
|
2018-04-21 03:41:13 +02:00
|
|
|
new_extrude_factor = self.get_float('S', params, 100., above=0.) / 100.
|
2017-12-22 03:33:38 +01:00
|
|
|
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
|
2018-04-21 01:58:37 +02:00
|
|
|
cmd_SET_GCODE_OFFSET_help = "Set a virtual offset to g-code positions"
|
|
|
|
def cmd_SET_GCODE_OFFSET(self, params):
|
2019-05-21 04:36:57 +02:00
|
|
|
move_delta = [0., 0., 0., 0.]
|
2018-04-21 01:58:37 +02:00
|
|
|
for axis, pos in self.axis2pos.items():
|
|
|
|
if axis in params:
|
|
|
|
offset = self.get_float(axis, params)
|
|
|
|
elif axis + '_ADJUST' in params:
|
|
|
|
offset = self.homing_position[pos]
|
|
|
|
offset += self.get_float(axis + '_ADJUST', params)
|
|
|
|
else:
|
|
|
|
continue
|
2018-05-29 20:09:04 +02:00
|
|
|
delta = offset - self.homing_position[pos]
|
2019-05-21 04:36:57 +02:00
|
|
|
move_delta[pos] = delta
|
2018-05-29 20:09:04 +02:00
|
|
|
self.base_position[pos] += delta
|
2018-04-21 01:58:37 +02:00
|
|
|
self.homing_position[pos] = offset
|
2019-05-21 04:36:57 +02:00
|
|
|
# Move the toolhead the given offset if requested
|
|
|
|
if self.get_int('MOVE', params, 0):
|
2019-05-27 02:04:50 +02:00
|
|
|
speed = self.get_float('MOVE_SPEED', params, self.speed, above=0.)
|
2019-05-21 04:36:57 +02:00
|
|
|
for pos, delta in enumerate(move_delta):
|
|
|
|
self.last_position[pos] += delta
|
2019-06-03 18:53:50 +02:00
|
|
|
self.move_with_transform(self.last_position, speed)
|
2019-05-26 17:08:48 +02:00
|
|
|
cmd_SAVE_GCODE_STATE_help = "Save G-Code coordinate state"
|
|
|
|
def cmd_SAVE_GCODE_STATE(self, params):
|
|
|
|
state_name = self.get_str('NAME', params, 'default')
|
|
|
|
self.saved_states[state_name] = {
|
|
|
|
'absolute_coord': self.absolute_coord,
|
|
|
|
'absolute_extrude': self.absolute_extrude,
|
|
|
|
'base_position': list(self.base_position),
|
|
|
|
'last_position': list(self.last_position),
|
|
|
|
'homing_position': list(self.homing_position),
|
|
|
|
'speed': self.speed, 'speed_factor': self.speed_factor,
|
|
|
|
'extrude_factor': self.extrude_factor,
|
|
|
|
}
|
|
|
|
cmd_RESTORE_GCODE_STATE_help = "Restore a previously saved G-Code state"
|
|
|
|
def cmd_RESTORE_GCODE_STATE(self, params):
|
|
|
|
state_name = self.get_str('NAME', params, 'default')
|
|
|
|
state = self.saved_states.get(state_name)
|
|
|
|
if state is None:
|
2019-06-03 18:48:35 +02:00
|
|
|
raise self.error("Unknown g-code state: %s" % (state_name,))
|
2019-05-26 17:08:48 +02:00
|
|
|
# Restore state
|
|
|
|
self.absolute_coord = state['absolute_coord']
|
|
|
|
self.absolute_extrude = state['absolute_extrude']
|
|
|
|
self.base_position = list(state['base_position'])
|
|
|
|
self.homing_position = list(state['homing_position'])
|
|
|
|
self.speed = state['speed']
|
|
|
|
self.speed_factor = state['speed_factor']
|
|
|
|
self.extrude_factor = state['extrude_factor']
|
|
|
|
# Restore the relative E position
|
|
|
|
e_diff = self.last_position[3] - state['last_position'][3]
|
|
|
|
self.base_position[3] += e_diff
|
|
|
|
# Move the toolhead back if requested
|
|
|
|
if self.get_int('MOVE', params, 0):
|
|
|
|
speed = self.get_float('MOVE_SPEED', params, self.speed, above=0.)
|
|
|
|
self.last_position[:3] = state['last_position'][:3]
|
2019-06-03 18:53:50 +02:00
|
|
|
self.move_with_transform(self.last_position, speed)
|
2019-11-13 17:29:27 +01:00
|
|
|
# G-Code temperature commands
|
2016-12-01 04:34:38 +01:00
|
|
|
cmd_M105_when_not_ready = True
|
2016-05-25 17:37:40 +02:00
|
|
|
def cmd_M105(self, params):
|
|
|
|
# Get Extruder Temperature
|
2019-09-18 15:58:16 +02:00
|
|
|
msg = self._get_temp(self.reactor.monotonic())
|
|
|
|
if self.need_ack:
|
|
|
|
self.ack(msg)
|
|
|
|
else:
|
|
|
|
self.respond(msg)
|
2016-05-25 17:37:40 +02:00
|
|
|
def cmd_M104(self, params):
|
|
|
|
# Set Extruder Temperature
|
2019-05-25 00:45:18 +02:00
|
|
|
self._set_temp(params)
|
2016-05-25 17:37:40 +02:00
|
|
|
def cmd_M109(self, params):
|
|
|
|
# Set Extruder Temperature and Wait
|
2019-05-25 00:45:18 +02:00
|
|
|
self._set_temp(params, wait=True)
|
2017-12-22 02:45:07 +01:00
|
|
|
def cmd_M140(self, params):
|
|
|
|
# Set Bed Temperature
|
2019-05-25 00:45:18 +02:00
|
|
|
self._set_temp(params, is_bed=True)
|
2017-12-22 02:45:07 +01:00
|
|
|
def cmd_M190(self, params):
|
|
|
|
# Set Bed Temperature and Wait
|
2019-05-25 00:45:18 +02:00
|
|
|
self._set_temp(params, is_bed=True, wait=True)
|
2017-12-22 02:45:07 +01:00
|
|
|
# G-Code miscellaneous commands
|
2017-12-04 00:16:23 +01:00
|
|
|
cmd_M112_when_not_ready = True
|
2016-12-09 19:55:18 +01:00
|
|
|
def cmd_M112(self, params):
|
|
|
|
# Emergency Stop
|
2017-10-12 21:15:14 +02:00
|
|
|
self.printer.invoke_shutdown("Shutdown due to M112 command")
|
2016-12-29 03:45:29 +01:00
|
|
|
cmd_M115_when_not_ready = True
|
|
|
|
def cmd_M115(self, params):
|
|
|
|
# Get Firmware Version and Capabilities
|
2017-08-21 23:19:43 +02:00
|
|
|
software_version = self.printer.get_start_args().get('software_version')
|
|
|
|
kw = {"FIRMWARE_NAME": "Klipper", "FIRMWARE_VERSION": software_version}
|
2016-12-29 03:45:29 +01:00
|
|
|
self.ack(" ".join(["%s:%s" % (k, v) for k, v in kw.items()]))
|
2017-04-20 16:11:10 +02: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
|
2018-03-10 03:43:24 +01:00
|
|
|
cmd_GET_POSITION_when_not_ready = True
|
|
|
|
def cmd_GET_POSITION(self, params):
|
|
|
|
if self.toolhead is None:
|
|
|
|
self.cmd_default(params)
|
|
|
|
return
|
|
|
|
kin = self.toolhead.get_kinematics()
|
2018-07-16 16:16:16 +02:00
|
|
|
steppers = kin.get_steppers()
|
2018-06-22 00:15:23 +02:00
|
|
|
mcu_pos = " ".join(["%s:%d" % (s.get_name(), s.get_mcu_position())
|
2018-03-10 03:43:24 +01:00
|
|
|
for s in steppers])
|
2019-11-13 23:59:40 +01:00
|
|
|
for s in steppers:
|
|
|
|
s.set_tag_position(s.get_commanded_position())
|
|
|
|
stepper_pos = " ".join(["%s:%.6f" % (s.get_name(), s.get_tag_position())
|
|
|
|
for s in steppers])
|
|
|
|
kin_pos = " ".join(["%s:%.6f" % (a, v)
|
|
|
|
for a, v in zip("XYZ", kin.calc_tag_position())])
|
2018-03-10 03:43:24 +01:00
|
|
|
toolhead_pos = " ".join(["%s:%.6f" % (a, v) for a, v in zip(
|
|
|
|
"XYZE", self.toolhead.get_position())])
|
|
|
|
gcode_pos = " ".join(["%s:%.6f" % (a, v)
|
|
|
|
for a, v in zip("XYZE", self.last_position)])
|
2018-04-21 01:42:26 +02:00
|
|
|
base_pos = " ".join(["%s:%.6f" % (a, v)
|
|
|
|
for a, v in zip("XYZE", self.base_position)])
|
2018-03-10 03:43:24 +01:00
|
|
|
homing_pos = " ".join(["%s:%.6f" % (a, v)
|
2018-04-21 01:42:26 +02:00
|
|
|
for a, v in zip("XYZ", self.homing_position)])
|
2019-11-13 23:59:40 +01:00
|
|
|
self.respond_info("mcu: %s\n"
|
|
|
|
"stepper: %s\n"
|
|
|
|
"kinematic: %s\n"
|
|
|
|
"toolhead: %s\n"
|
|
|
|
"gcode: %s\n"
|
|
|
|
"gcode base: %s\n"
|
|
|
|
"gcode homing: %s"
|
|
|
|
% (mcu_pos, stepper_pos, kin_pos, toolhead_pos,
|
|
|
|
gcode_pos, base_pos, homing_pos))
|
2018-02-11 19:52:20 +01:00
|
|
|
def request_restart(self, result):
|
2017-02-13 00:42:29 +01:00
|
|
|
if self.is_printer_ready:
|
2018-02-11 19:52:20 +01:00
|
|
|
print_time = self.toolhead.get_last_move_time()
|
2019-02-26 03:26:27 +01:00
|
|
|
self.printer.send_event("gcode:request_restart", print_time)
|
2017-02-13 00:42:29 +01:00
|
|
|
self.toolhead.dwell(0.500)
|
|
|
|
self.toolhead.wait_moves()
|
2018-02-11 19:52:20 +01:00
|
|
|
self.printer.request_exit(result)
|
2017-03-09 04:26:10 +01:00
|
|
|
cmd_RESTART_when_not_ready = True
|
|
|
|
cmd_RESTART_help = "Reload config file and restart host software"
|
|
|
|
def cmd_RESTART(self, params):
|
2018-02-11 19:52:20 +01:00
|
|
|
self.request_restart('restart')
|
2017-03-09 04:26:10 +01:00
|
|
|
cmd_FIRMWARE_RESTART_when_not_ready = True
|
|
|
|
cmd_FIRMWARE_RESTART_help = "Restart firmware, host, and reload config"
|
|
|
|
def cmd_FIRMWARE_RESTART(self, params):
|
2018-02-11 19:52:20 +01:00
|
|
|
self.request_restart('firmware_restart')
|
2017-09-02 17:59:27 +02:00
|
|
|
cmd_ECHO_when_not_ready = True
|
|
|
|
def cmd_ECHO(self, params):
|
2019-03-04 19:04:18 +01:00
|
|
|
self.respond_info(params['#original'], log=False)
|
2016-12-21 17:33:03 +01:00
|
|
|
cmd_STATUS_when_not_ready = True
|
|
|
|
cmd_STATUS_help = "Report the printer status"
|
|
|
|
def cmd_STATUS(self, params):
|
|
|
|
if self.is_printer_ready:
|
2018-09-13 00:46:25 +02:00
|
|
|
self._respond_state("Ready")
|
|
|
|
return
|
|
|
|
msg = self.printer.get_state_message()
|
2018-10-13 23:20:21 +02:00
|
|
|
msg = msg.rstrip() + "\nKlipper state: Not ready"
|
2018-09-13 00:46:25 +02:00
|
|
|
self.respond_error(msg)
|
2016-12-01 05:47:40 +01:00
|
|
|
cmd_HELP_when_not_ready = True
|
2016-12-01 04:34:38 +01:00
|
|
|
def cmd_HELP(self, params):
|
2016-12-21 17:33:03 +01: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 16:44:22 +02:00
|
|
|
for cmd in sorted(self.gcode_handlers):
|
2017-12-04 00:53:42 +01:00
|
|
|
if cmd in self.gcode_help:
|
|
|
|
cmdhelp.append("%-10s: %s" % (cmd, self.gcode_help[cmd]))
|
2019-03-04 19:04:18 +01:00
|
|
|
self.respond_info("\n".join(cmdhelp), log=False)
|