From 8260a3a0c38e25168e3dfc414ed62fd23e380b21 Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Fri, 8 Jan 2021 12:37:57 -0500 Subject: [PATCH] homing: Make homing.py an "extras" module Move klippy/homing.py to klippy/extras/homing.py and convert the code to an "extras" modules. Signed-off-by: Kevin O'Connor --- klippy/extras/endstop_phase.py | 6 +++--- klippy/extras/gcode_move.py | 21 +++++++-------------- klippy/{ => extras}/homing.py | 29 ++++++++++++++++++++++++++--- klippy/extras/homing_override.py | 2 +- klippy/extras/probe.py | 10 +++++----- klippy/extras/safe_z_home.py | 2 +- klippy/toolhead.py | 4 ++-- 7 files changed, 45 insertions(+), 29 deletions(-) rename klippy/{ => extras}/homing.py (88%) diff --git a/klippy/extras/endstop_phase.py b/klippy/extras/endstop_phase.py index 66e9fc38..8e70be74 100644 --- a/klippy/extras/endstop_phase.py +++ b/klippy/extras/endstop_phase.py @@ -1,6 +1,6 @@ # Endstop accuracy improvement via stepper phase tracking # -# Copyright (C) 2016-2018 Kevin O'Connor +# Copyright (C) 2016-2021 Kevin O'Connor # # This file may be distributed under the terms of the GNU GPLv3 license. import math, logging @@ -101,7 +101,7 @@ class EndstopPhase: "Endstop %s incorrect phase (got %d vs %d)" % ( self.name, phase, self.endstop_phase)) return delta * self.step_dist - def handle_home_rails_end(self, rails): + def handle_home_rails_end(self, homing_state, rails): for rail in rails: stepper = rail.get_steppers()[0] if stepper.get_name() != self.name: @@ -149,7 +149,7 @@ class EndstopPhases: return phase = convert_phase(driver_phase, driver_phases, len(phase_history)) phase_history[phase] += 1 - def handle_home_rails_end(self, rails): + def handle_home_rails_end(self, homing_state, rails): for rail in rails: stepper = rail.get_steppers()[0] stepper_name = stepper.get_name() diff --git a/klippy/extras/gcode_move.py b/klippy/extras/gcode_move.py index 4bdfdf8b..1b9a22b7 100644 --- a/klippy/extras/gcode_move.py +++ b/klippy/extras/gcode_move.py @@ -4,7 +4,6 @@ # # This file may be distributed under the terms of the GNU GPLv3 license. import logging -import homing class GCodeMove: def __init__(self, config): @@ -19,11 +18,13 @@ class GCodeMove: self.reset_last_position) printer.register_event_handler("extruder:activate_extruder", self._handle_activate_extruder) + printer.register_event_handler("homing:home_rails_end", + self._handle_home_rails_end) self.is_printer_ready = False # Register g-code commands gcode = printer.lookup_object('gcode') handlers = [ - 'G1', 'G28', 'G20', 'G21', + 'G1', 'G20', 'G21', 'M82', 'M83', 'G90', 'G91', 'G92', 'M220', 'M221', 'SET_GCODE_OFFSET', 'SAVE_GCODE_STATE', 'RESTORE_GCODE_STATE', ] @@ -68,6 +69,10 @@ class GCodeMove: self.reset_last_position() self.extrude_factor = 1. self.base_position[3] = self.last_position[3] + def _handle_home_rails_end(self, homing_state, rails): + self.reset_last_position() + for axis in homing_state.get_axes(): + self.base_position[axis] = self.homing_position[axis] def set_move_transform(self, transform, force=False): if self.move_transform is not None and not force: raise self.printer.config_error( @@ -134,18 +139,6 @@ class GCodeMove: raise gcmd.error("Unable to parse move '%s'" % (gcmd.get_commandline(),)) self.move_with_transform(self.last_position, self.speed) - def cmd_G28(self, gcmd): - # Move to origin - axes = [] - for pos, axis in enumerate('XYZ'): - if gcmd.get(axis, None) is not None: - axes.append(pos) - if not axes: - axes = [0, 1, 2] - homing_state = homing.Homing(self.printer) - homing_state.home_axes(axes) - for axis in homing_state.get_axes(): - self.base_position[axis] = self.homing_position[axis] # G-Code coordinate manipulation def cmd_G20(self, gcmd): # Set units to inches diff --git a/klippy/homing.py b/klippy/extras/homing.py similarity index 88% rename from klippy/homing.py rename to klippy/extras/homing.py index 49f5a8dc..283e0df7 100644 --- a/klippy/homing.py +++ b/klippy/extras/homing.py @@ -1,4 +1,4 @@ -# Code for state tracking during homing operations +# Helper code for implementing homing operations # # Copyright (C) 2016-2021 Kevin O'Connor # @@ -9,6 +9,7 @@ HOMING_START_DELAY = 0.001 ENDSTOP_SAMPLE_TIME = .000015 ENDSTOP_SAMPLE_COUNT = 4 +# State tracking during toolhead homing/probing operations class Homing: def __init__(self, printer): self.printer = printer @@ -106,7 +107,7 @@ class Homing: "Endstop %s still triggered after retract" % (name,)) def home_rails(self, rails, forcepos, movepos): # Notify of upcoming homing operation - self.printer.send_event("homing:home_rails_begin", rails) + self.printer.send_event("homing:home_rails_begin", self, rails) # Alter kinematics class to think printer is at forcepos homing_axes = [axis for axis in range(3) if forcepos[axis] is not None] forcepos = self._fill_coord(forcepos) @@ -136,7 +137,7 @@ class Homing: kin = self.toolhead.get_kinematics() for s in kin.get_steppers(): s.set_tag_position(s.get_commanded_position()) - ret = self.printer.send_event("homing:home_rails_end", rails) + ret = self.printer.send_event("homing:home_rails_end", self, rails) if any(ret): # Apply any homing offsets adjustpos = kin.calc_tag_position() @@ -157,3 +158,25 @@ def multi_complete(printer, completions): return completions[0] cb = (lambda e: all([c.wait() for c in completions])) return printer.get_reactor().register_callback(cb) + +class PrinterHoming: + def __init__(self, config): + self.printer = config.get_printer() + # Register g-code commands + gcode = self.printer.lookup_object('gcode') + gcode.register_command('G28', self.cmd_G28) + def new_homing_state(self): + return Homing(self.printer) + def cmd_G28(self, gcmd): + # Move to origin + axes = [] + for pos, axis in enumerate('XYZ'): + if gcmd.get(axis, None) is not None: + axes.append(pos) + if not axes: + axes = [0, 1, 2] + homing_state = Homing(self.printer) + homing_state.home_axes(axes) + +def load_config(config): + return PrinterHoming(config) diff --git a/klippy/extras/homing_override.py b/klippy/extras/homing_override.py index 149bd022..a1cf94b5 100644 --- a/klippy/extras/homing_override.py +++ b/klippy/extras/homing_override.py @@ -13,7 +13,7 @@ class HomingOverride: gcode_macro = self.printer.load_object(config, 'gcode_macro') self.template = gcode_macro.load_template(config, 'gcode') self.in_script = False - self.printer.load_object(config, 'gcode_move') + self.printer.load_object(config, 'homing') self.gcode = self.printer.lookup_object('gcode') self.prev_G28 = self.gcode.register_command("G28", None) self.gcode.register_command("G28", self.cmd_G28) diff --git a/klippy/extras/probe.py b/klippy/extras/probe.py index e1115e0e..fe6cb151 100644 --- a/klippy/extras/probe.py +++ b/klippy/extras/probe.py @@ -1,10 +1,10 @@ # Z-Probe support # -# Copyright (C) 2017-2020 Kevin O'Connor +# Copyright (C) 2017-2021 Kevin O'Connor # # This file may be distributed under the terms of the GNU GPLv3 license. import logging -import pins, homing +import pins from . import manual_probe HINT_TIMEOUT = """ @@ -75,11 +75,11 @@ class PrinterProbe: def _handle_homing_move_end(self, endstops): if self.mcu_probe in endstops: self.mcu_probe.probe_finish() - def _handle_home_rails_begin(self, rails): + def _handle_home_rails_begin(self, homing_state, rails): endstops = [es for rail in rails for es, name in rail.get_endstops()] if self.mcu_probe in endstops: self.multi_probe_begin() - def _handle_home_rails_end(self, rails): + def _handle_home_rails_end(self, homing_state, rails): endstops = [es for rail in rails for es, name in rail.get_endstops()] if self.mcu_probe in endstops: self.multi_probe_end() @@ -112,7 +112,7 @@ class PrinterProbe: curtime = self.printer.get_reactor().monotonic() if 'z' not in toolhead.get_status(curtime)['homed_axes']: raise self.printer.command_error("Must home before probe") - homing_state = homing.Homing(self.printer) + homing_state = self.printer.lookup_object('homing').new_homing_state() pos = toolhead.get_position() pos[2] = self.z_position endstops = [(self.mcu_probe, "probe")] diff --git a/klippy/extras/safe_z_home.py b/klippy/extras/safe_z_home.py index 93536274..6dd2f292 100644 --- a/klippy/extras/safe_z_home.py +++ b/klippy/extras/safe_z_home.py @@ -20,7 +20,7 @@ class SafeZHoming: self.max_z = zconfig.getfloat('position_max', note_valid=False) self.speed = config.getfloat('speed', 50.0, above=0.) self.move_to_previous = config.getboolean('move_to_previous', False) - self.printer.load_object(config, 'gcode_move') + self.printer.load_object(config, 'homing') self.gcode = self.printer.lookup_object('gcode') self.prev_G28 = self.gcode.register_command("G28", None) self.gcode.register_command("G28", self.cmd_G28) diff --git a/klippy/toolhead.py b/klippy/toolhead.py index 5c229dc7..c6ba130f 100644 --- a/klippy/toolhead.py +++ b/klippy/toolhead.py @@ -273,8 +273,8 @@ class ToolHead: desc=self.cmd_SET_VELOCITY_LIMIT_help) gcode.register_command('M204', self.cmd_M204) # Load some default modules - modules = ["gcode_move", "idle_timeout", "statistics", "manual_probe", - "tuning_tower"] + modules = ["gcode_move", "homing", "idle_timeout", "statistics", + "manual_probe", "tuning_tower"] for module_name in modules: self.printer.load_object(config, module_name) # Print time tracking