From 89835940f70ff4b9f19a77d14fdaeefa86d777db Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Mon, 16 Jul 2018 10:06:30 -0400 Subject: [PATCH] query_endstops: Move QUERY_ENDSTOP command to it own extras/ module Signed-off-by: Kevin O'Connor --- klippy/extras/query_endstops.py | 34 +++++++++++++++++++++++++++++++++ klippy/gcode.py | 9 +-------- klippy/homing.py | 12 ------------ klippy/stepper.py | 13 ++++++++++--- test/klippy/dual_carriage.test | 2 ++ 5 files changed, 47 insertions(+), 23 deletions(-) create mode 100644 klippy/extras/query_endstops.py diff --git a/klippy/extras/query_endstops.py b/klippy/extras/query_endstops.py new file mode 100644 index 00000000..0b139ec2 --- /dev/null +++ b/klippy/extras/query_endstops.py @@ -0,0 +1,34 @@ +# Utility for querying the current state of all endstops +# +# Copyright (C) 2018 Kevin O'Connor +# +# This file may be distributed under the terms of the GNU GPLv3 license. + +class QueryEndstops: + def __init__(self, config): + self.printer = config.get_printer() + self.endstops = [] + gcode = self.printer.lookup_object('gcode') + gcode.register_command("QUERY_ENDSTOPS", self.cmd_QUERY_ENDSTOPS, + desc=self.cmd_QUERY_ENDSTOPS_help) + gcode.register_command("M119", self.cmd_QUERY_ENDSTOPS) + def register_endstop(self, mcu_endstop, name): + self.endstops.append((mcu_endstop, name)) + cmd_QUERY_ENDSTOPS_help = "Report on the status of each endstop" + def cmd_QUERY_ENDSTOPS(self, params): + toolhead = self.printer.lookup_object('toolhead') + print_time = toolhead.get_last_move_time() + # Query the endstops + for mcu_endstop, name in self.endstops: + mcu_endstop.query_endstop(print_time) + out = [] + for mcu_endstop, name in self.endstops: + out.append((name, mcu_endstop.query_endstop_wait())) + # Report results + msg = " ".join(["%s:%s" % (name, ["open", "TRIGGERED"][not not t]) + for name, t in out]) + gcode = self.printer.lookup_object('gcode') + gcode.respond(msg) + +def load_config(config): + return QueryEndstops(config) diff --git a/klippy/gcode.py b/klippy/gcode.py index e9c20055..47a8b2d0 100644 --- a/klippy/gcode.py +++ b/klippy/gcode.py @@ -425,7 +425,7 @@ class GCodeParser: 'G20', 'M82', 'M83', 'G90', 'G91', 'G92', 'M114', 'M220', 'M221', 'SET_GCODE_OFFSET', 'M206', 'M105', 'M104', 'M109', 'M140', 'M190', 'M106', 'M107', - 'M112', 'M115', 'IGNORE', 'QUERY_ENDSTOPS', 'GET_POSITION', + 'M112', 'M115', 'IGNORE', 'GET_POSITION', 'RESTART', 'FIRMWARE_RESTART', 'ECHO', 'STATUS', 'HELP'] # G-Code movement commands cmd_G1_aliases = ['G0'] @@ -596,13 +596,6 @@ class GCodeParser: def cmd_IGNORE(self, params): # Commands that are just silently accepted pass - 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 - res = homing.query_endstops(self.toolhead) - self.respond(" ".join(["%s:%s" % (name, ["open", "TRIGGERED"][not not t]) - for name, t in res])) cmd_GET_POSITION_when_not_ready = True def cmd_GET_POSITION(self, params): if self.toolhead is None: diff --git a/klippy/homing.py b/klippy/homing.py index 0fc09cec..48144001 100644 --- a/klippy/homing.py +++ b/klippy/homing.py @@ -116,18 +116,6 @@ class Homing: self.toolhead.motor_off() raise -def query_endstops(toolhead): - print_time = toolhead.get_last_move_time() - rails = toolhead.get_kinematics().get_rails() - out = [] - for rail in rails: - for mcu_endstop, name in rail.get_endstops(): - mcu_endstop.query_endstop(print_time) - for rail in rails: - for mcu_endstop, name in rail.get_endstops(): - out.append((name, mcu_endstop.query_endstop_wait())) - return out - class EndstopError(Exception): pass diff --git a/klippy/stepper.py b/klippy/stepper.py index b1d74ce0..f937029a 100644 --- a/klippy/stepper.py +++ b/klippy/stepper.py @@ -112,7 +112,8 @@ class PrinterRail: self.get_commanded_position = stepper.get_commanded_position self.is_motor_enabled = stepper.is_motor_enabled # Primary endstop and its position - ppins = config.get_printer().lookup_object('pins') + printer = config.get_printer() + ppins = printer.lookup_object('pins') mcu_endstop = ppins.setup_pin('endstop', config.get('endstop_pin')) self.endstops = [(mcu_endstop, self.name)] stepper.add_to_endstop(mcu_endstop) @@ -121,6 +122,8 @@ class PrinterRail: else: self.position_endstop = config.getfloat( 'position_endstop', default_position_endstop) + query_endstops = printer.try_load_module(config, 'query_endstops') + query_endstops.register_endstop(mcu_endstop, self.name) # Axis range if need_position_minmax: self.position_min = config.getfloat('position_min', 0.) @@ -225,9 +228,13 @@ class PrinterRail: mcu_endstop = self.endstops[0][0] endstop_pin = config.get('endstop_pin', None) if endstop_pin is not None: - ppins = config.get_printer().lookup_object('pins') + printer = config.get_printer() + ppins = printer.lookup_object('pins') mcu_endstop = ppins.setup_pin('endstop', endstop_pin) - self.endstops.append((mcu_endstop, stepper.get_name(short=True))) + name = stepper.get_name(short=True) + self.endstops.append((mcu_endstop, name)) + query_endstops = printer.try_load_module(config, 'query_endstops') + query_endstops.register_endstop(mcu_endstop, name) stepper.add_to_endstop(mcu_endstop) def add_to_endstop(self, mcu_endstop): for stepper in self.steppers: diff --git a/test/klippy/dual_carriage.test b/test/klippy/dual_carriage.test index d3781270..d9d8009f 100644 --- a/test/klippy/dual_carriage.test +++ b/test/klippy/dual_carriage.test @@ -27,6 +27,8 @@ G91 G1 X20 E.2 G90 +QUERY_ENDSTOPS + # Servo tests SET_SERVO servo=my_servo angle=160 SET_SERVO servo=my_servo angle=130