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 <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2021-01-08 12:37:57 -05:00
parent ea85d419de
commit 8260a3a0c3
7 changed files with 45 additions and 29 deletions

View File

@ -1,6 +1,6 @@
# Endstop accuracy improvement via stepper phase tracking # Endstop accuracy improvement via stepper phase tracking
# #
# Copyright (C) 2016-2018 Kevin O'Connor <kevin@koconnor.net> # Copyright (C) 2016-2021 Kevin O'Connor <kevin@koconnor.net>
# #
# This file may be distributed under the terms of the GNU GPLv3 license. # This file may be distributed under the terms of the GNU GPLv3 license.
import math, logging import math, logging
@ -101,7 +101,7 @@ class EndstopPhase:
"Endstop %s incorrect phase (got %d vs %d)" % ( "Endstop %s incorrect phase (got %d vs %d)" % (
self.name, phase, self.endstop_phase)) self.name, phase, self.endstop_phase))
return delta * self.step_dist 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: for rail in rails:
stepper = rail.get_steppers()[0] stepper = rail.get_steppers()[0]
if stepper.get_name() != self.name: if stepper.get_name() != self.name:
@ -149,7 +149,7 @@ class EndstopPhases:
return return
phase = convert_phase(driver_phase, driver_phases, len(phase_history)) phase = convert_phase(driver_phase, driver_phases, len(phase_history))
phase_history[phase] += 1 phase_history[phase] += 1
def handle_home_rails_end(self, rails): def handle_home_rails_end(self, homing_state, rails):
for rail in rails: for rail in rails:
stepper = rail.get_steppers()[0] stepper = rail.get_steppers()[0]
stepper_name = stepper.get_name() stepper_name = stepper.get_name()

View File

@ -4,7 +4,6 @@
# #
# This file may be distributed under the terms of the GNU GPLv3 license. # This file may be distributed under the terms of the GNU GPLv3 license.
import logging import logging
import homing
class GCodeMove: class GCodeMove:
def __init__(self, config): def __init__(self, config):
@ -19,11 +18,13 @@ class GCodeMove:
self.reset_last_position) self.reset_last_position)
printer.register_event_handler("extruder:activate_extruder", printer.register_event_handler("extruder:activate_extruder",
self._handle_activate_extruder) self._handle_activate_extruder)
printer.register_event_handler("homing:home_rails_end",
self._handle_home_rails_end)
self.is_printer_ready = False self.is_printer_ready = False
# Register g-code commands # Register g-code commands
gcode = printer.lookup_object('gcode') gcode = printer.lookup_object('gcode')
handlers = [ handlers = [
'G1', 'G28', 'G20', 'G21', 'G1', 'G20', 'G21',
'M82', 'M83', 'G90', 'G91', 'G92', 'M220', 'M221', 'M82', 'M83', 'G90', 'G91', 'G92', 'M220', 'M221',
'SET_GCODE_OFFSET', 'SAVE_GCODE_STATE', 'RESTORE_GCODE_STATE', 'SET_GCODE_OFFSET', 'SAVE_GCODE_STATE', 'RESTORE_GCODE_STATE',
] ]
@ -68,6 +69,10 @@ class GCodeMove:
self.reset_last_position() self.reset_last_position()
self.extrude_factor = 1. self.extrude_factor = 1.
self.base_position[3] = self.last_position[3] 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): def set_move_transform(self, transform, force=False):
if self.move_transform is not None and not force: if self.move_transform is not None and not force:
raise self.printer.config_error( raise self.printer.config_error(
@ -134,18 +139,6 @@ class GCodeMove:
raise gcmd.error("Unable to parse move '%s'" raise gcmd.error("Unable to parse move '%s'"
% (gcmd.get_commandline(),)) % (gcmd.get_commandline(),))
self.move_with_transform(self.last_position, self.speed) 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 # G-Code coordinate manipulation
def cmd_G20(self, gcmd): def cmd_G20(self, gcmd):
# Set units to inches # Set units to inches

View File

@ -1,4 +1,4 @@
# Code for state tracking during homing operations # Helper code for implementing homing operations
# #
# Copyright (C) 2016-2021 Kevin O'Connor <kevin@koconnor.net> # Copyright (C) 2016-2021 Kevin O'Connor <kevin@koconnor.net>
# #
@ -9,6 +9,7 @@ HOMING_START_DELAY = 0.001
ENDSTOP_SAMPLE_TIME = .000015 ENDSTOP_SAMPLE_TIME = .000015
ENDSTOP_SAMPLE_COUNT = 4 ENDSTOP_SAMPLE_COUNT = 4
# State tracking during toolhead homing/probing operations
class Homing: class Homing:
def __init__(self, printer): def __init__(self, printer):
self.printer = printer self.printer = printer
@ -106,7 +107,7 @@ class Homing:
"Endstop %s still triggered after retract" % (name,)) "Endstop %s still triggered after retract" % (name,))
def home_rails(self, rails, forcepos, movepos): def home_rails(self, rails, forcepos, movepos):
# Notify of upcoming homing operation # 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 # Alter kinematics class to think printer is at forcepos
homing_axes = [axis for axis in range(3) if forcepos[axis] is not None] homing_axes = [axis for axis in range(3) if forcepos[axis] is not None]
forcepos = self._fill_coord(forcepos) forcepos = self._fill_coord(forcepos)
@ -136,7 +137,7 @@ class Homing:
kin = self.toolhead.get_kinematics() kin = self.toolhead.get_kinematics()
for s in kin.get_steppers(): for s in kin.get_steppers():
s.set_tag_position(s.get_commanded_position()) 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): if any(ret):
# Apply any homing offsets # Apply any homing offsets
adjustpos = kin.calc_tag_position() adjustpos = kin.calc_tag_position()
@ -157,3 +158,25 @@ def multi_complete(printer, completions):
return completions[0] return completions[0]
cb = (lambda e: all([c.wait() for c in completions])) cb = (lambda e: all([c.wait() for c in completions]))
return printer.get_reactor().register_callback(cb) 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)

View File

@ -13,7 +13,7 @@ class HomingOverride:
gcode_macro = self.printer.load_object(config, 'gcode_macro') gcode_macro = self.printer.load_object(config, 'gcode_macro')
self.template = gcode_macro.load_template(config, 'gcode') self.template = gcode_macro.load_template(config, 'gcode')
self.in_script = False 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.gcode = self.printer.lookup_object('gcode')
self.prev_G28 = self.gcode.register_command("G28", None) self.prev_G28 = self.gcode.register_command("G28", None)
self.gcode.register_command("G28", self.cmd_G28) self.gcode.register_command("G28", self.cmd_G28)

View File

@ -1,10 +1,10 @@
# Z-Probe support # Z-Probe support
# #
# Copyright (C) 2017-2020 Kevin O'Connor <kevin@koconnor.net> # Copyright (C) 2017-2021 Kevin O'Connor <kevin@koconnor.net>
# #
# This file may be distributed under the terms of the GNU GPLv3 license. # This file may be distributed under the terms of the GNU GPLv3 license.
import logging import logging
import pins, homing import pins
from . import manual_probe from . import manual_probe
HINT_TIMEOUT = """ HINT_TIMEOUT = """
@ -75,11 +75,11 @@ class PrinterProbe:
def _handle_homing_move_end(self, endstops): def _handle_homing_move_end(self, endstops):
if self.mcu_probe in endstops: if self.mcu_probe in endstops:
self.mcu_probe.probe_finish() 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()] endstops = [es for rail in rails for es, name in rail.get_endstops()]
if self.mcu_probe in endstops: if self.mcu_probe in endstops:
self.multi_probe_begin() 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()] endstops = [es for rail in rails for es, name in rail.get_endstops()]
if self.mcu_probe in endstops: if self.mcu_probe in endstops:
self.multi_probe_end() self.multi_probe_end()
@ -112,7 +112,7 @@ class PrinterProbe:
curtime = self.printer.get_reactor().monotonic() curtime = self.printer.get_reactor().monotonic()
if 'z' not in toolhead.get_status(curtime)['homed_axes']: if 'z' not in toolhead.get_status(curtime)['homed_axes']:
raise self.printer.command_error("Must home before probe") 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 = toolhead.get_position()
pos[2] = self.z_position pos[2] = self.z_position
endstops = [(self.mcu_probe, "probe")] endstops = [(self.mcu_probe, "probe")]

View File

@ -20,7 +20,7 @@ class SafeZHoming:
self.max_z = zconfig.getfloat('position_max', note_valid=False) self.max_z = zconfig.getfloat('position_max', note_valid=False)
self.speed = config.getfloat('speed', 50.0, above=0.) self.speed = config.getfloat('speed', 50.0, above=0.)
self.move_to_previous = config.getboolean('move_to_previous', False) 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.gcode = self.printer.lookup_object('gcode')
self.prev_G28 = self.gcode.register_command("G28", None) self.prev_G28 = self.gcode.register_command("G28", None)
self.gcode.register_command("G28", self.cmd_G28) self.gcode.register_command("G28", self.cmd_G28)

View File

@ -273,8 +273,8 @@ class ToolHead:
desc=self.cmd_SET_VELOCITY_LIMIT_help) desc=self.cmd_SET_VELOCITY_LIMIT_help)
gcode.register_command('M204', self.cmd_M204) gcode.register_command('M204', self.cmd_M204)
# Load some default modules # Load some default modules
modules = ["gcode_move", "idle_timeout", "statistics", "manual_probe", modules = ["gcode_move", "homing", "idle_timeout", "statistics",
"tuning_tower"] "manual_probe", "tuning_tower"]
for module_name in modules: for module_name in modules:
self.printer.load_object(config, module_name) self.printer.load_object(config, module_name)
# Print time tracking # Print time tracking