homing: Merge home_prepare()/home_finalize() into move_begin/move_end events

Collapse the "homing:move_begin", "homing:move_end", home_prepare(),
and home_finalize() into two new events: "homing:homing_move_begin"
and "homing:homing_move_end".  This simplifies the homing code.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2020-02-12 13:03:42 -05:00 committed by KevinOConnor
parent b23346a521
commit feb15eaf2e
7 changed files with 63 additions and 74 deletions

View File

@ -137,26 +137,24 @@ class BLTouchEndstopWrapper:
# Test was successful
self.next_test_time = check_end_time + TEST_TIME
self.sync_print_time()
def home_prepare(self):
def probe_prepare(self):
self.test_sensor()
self.sync_print_time()
duration = max(MIN_CMD_TIME, self.pin_move_time - MIN_CMD_TIME)
self.send_cmd('pin_down', duration=duration)
self.send_cmd(None)
self.sync_print_time()
self.mcu_endstop.home_prepare()
toolhead = self.printer.lookup_object('toolhead')
toolhead.flush_step_generation()
self.start_mcu_pos = [(s, s.get_mcu_position())
for s in self.mcu_endstop.get_steppers()]
def home_finalize(self):
def probe_finalize(self):
self.raise_probe()
self.sync_print_time()
# Verify the probe actually deployed during the attempt
for s, mcu_pos in self.start_mcu_pos:
if s.get_mcu_position() == mcu_pos:
raise homing.EndstopError("BLTouch failed to deploy")
self.mcu_endstop.home_finalize()
def home_start(self, print_time, sample_time, sample_count, rest_time,
notify=None):
rest_time = min(rest_time, ENDSTOP_REST_TIME)

View File

@ -11,9 +11,9 @@ class HomingHeaters:
self.printer = config.get_printer()
self.printer.register_event_handler("klippy:connect",
self.handle_connect)
self.printer.register_event_handler("homing:move_begin",
self.printer.register_event_handler("homing:homing_move_begin",
self.handle_homing_move_begin)
self.printer.register_event_handler("homing:move_end",
self.printer.register_event_handler("homing:homing_move_end",
self.handle_homing_move_end)
self.heaters_to_disable = config.get("heaters", "")
self.disable_heaters = []
@ -49,7 +49,7 @@ class HomingHeaters:
if self.flaky_steppers == [""]:
return True
steppers_being_homed = [s.get_name()
for es, name in endstops
for es in endstops
for s in es.get_steppers()]
return any(x in self.flaky_steppers for x in steppers_being_homed)
def handle_homing_move_begin(self, endstops):

View File

@ -77,12 +77,9 @@ class ManualStepper:
def do_homing_move(self, movepos, speed, accel, triggered):
if not self.can_home:
raise self.gcode.error("No endstop for this manual stepper")
# Notify endstops of upcoming home
endstops = self.rail.get_endstops()
for mcu_endstop, name in endstops:
mcu_endstop.home_prepare()
# Start endstop checking
self.sync_print_time()
endstops = self.rail.get_endstops()
for mcu_endstop, name in endstops:
min_step_dist = min([s.get_step_dist()
for s in mcu_endstop.get_steppers()])
@ -99,12 +96,6 @@ class ManualStepper:
except mcu_endstop.TimeoutError as e:
if error is None:
error = "Failed to home %s: %s" % (name, str(e))
for mcu_endstop, name in endstops:
try:
mcu_endstop.home_finalize()
except homing.CommandError as e:
if error is None:
error = str(e)
self.sync_print_time()
if error is not None:
raise homing.CommandError(error)

View File

@ -43,6 +43,11 @@ class PrinterProbe:
minval=0)
# Register z_virtual_endstop pin
self.printer.lookup_object('pins').register_chip('probe', self)
# Register homing event handlers
self.printer.register_event_handler("homing:homing_move_begin",
self._handle_homing_move_begin)
self.printer.register_event_handler("homing:homing_move_end",
self._handle_homing_move_end)
# Register PROBE/QUERY_PROBE commands
self.gcode = self.printer.lookup_object('gcode')
self.gcode.register_command('PROBE', self.cmd_PROBE,
@ -53,6 +58,12 @@ class PrinterProbe:
desc=self.cmd_PROBE_CALIBRATE_help)
self.gcode.register_command('PROBE_ACCURACY', self.cmd_PROBE_ACCURACY,
desc=self.cmd_PROBE_ACCURACY_help)
def _handle_homing_move_begin(self, endstops):
if self.mcu_probe in endstops:
self.mcu_probe.probe_prepare()
def _handle_homing_move_end(self, endstops):
if self.mcu_probe in endstops:
self.mcu_probe.probe_finalize()
def setup_pin(self, pin_type, pin_params):
if pin_type != 'endstop' or pin_params['pin'] != 'z_virtual_endstop':
raise pins.error("Probe virtual endstop only useful as endstop pin")
@ -255,22 +266,20 @@ class ProbeEndstopWrapper:
for stepper in kin.get_steppers():
if stepper.is_active_axis('z'):
self.add_stepper(stepper)
def home_prepare(self):
def probe_prepare(self):
toolhead = self.printer.lookup_object('toolhead')
start_pos = toolhead.get_position()
self.activate_gcode.run_gcode_from_command()
if toolhead.get_position()[:3] != start_pos[:3]:
raise homing.CommandError(
"Toolhead moved during probe activate_gcode script")
self.mcu_endstop.home_prepare()
def home_finalize(self):
def probe_finalize(self):
toolhead = self.printer.lookup_object('toolhead')
start_pos = toolhead.get_position()
self.deactivate_gcode.run_gcode_from_command()
if toolhead.get_position()[:3] != start_pos[:3]:
raise homing.CommandError(
"Toolhead moved during probe deactivate_gcode script")
self.mcu_endstop.home_finalize()
def get_position_endstop(self):
return self.position_endstop

View File

@ -182,12 +182,30 @@ class TMCCommandHelper:
# TMC virtual pins
######################################################################
# Endstop wrapper that enables "sensorless homing"
class TMCVirtualEndstop:
def __init__(self, mcu_tmc, mcu_endstop):
# Helper class for "sensorless homing"
class TMCVirtualPinHelper:
def __init__(self, config, mcu_tmc, diag_pin):
self.printer = config.get_printer()
self.mcu_tmc = mcu_tmc
self.fields = mcu_tmc.get_fields()
self.mcu_endstop = mcu_endstop
self.diag_pin = diag_pin
self.mcu_endstop = None
self.en_pwm = False
self.pwmthrs = 0
# Register virtual_endstop pin
name_parts = config.get_name().split()
ppins = self.printer.lookup_object("pins")
ppins.register_chip("%s_%s" % (name_parts[0], name_parts[-1]), self)
def setup_pin(self, pin_type, pin_params):
# Validate pin
ppins = self.printer.lookup_object('pins')
if pin_type != 'endstop' or pin_params['pin'] != 'virtual_endstop':
raise ppins.error("tmc virtual endstop only useful as endstop")
if pin_params['invert'] or pin_params['pullup']:
raise ppins.error("Can not pullup/invert tmc virtual pin")
if self.diag_pin is None:
raise ppins.error("tmc virtual endstop requires diag pin config")
# Setup for sensorless homing
reg = self.fields.lookup_register("en_pwm_mode", None)
if reg is None:
self.en_pwm = not self.fields.get_field("en_spreadCycle")
@ -195,15 +213,15 @@ class TMCVirtualEndstop:
else:
self.en_pwm = self.fields.get_field("en_pwm_mode")
self.pwmthrs = 0
# Wrappers
self.get_mcu = self.mcu_endstop.get_mcu
self.add_stepper = self.mcu_endstop.add_stepper
self.get_steppers = self.mcu_endstop.get_steppers
self.home_start = self.mcu_endstop.home_start
self.home_wait = self.mcu_endstop.home_wait
self.query_endstop = self.mcu_endstop.query_endstop
self.TimeoutError = self.mcu_endstop.TimeoutError
def home_prepare(self):
self.printer.register_event_handler("homing:homing_move_begin",
self.handle_homing_move_begin)
self.printer.register_event_handler("homing:homing_move_end",
self.handle_homing_move_end)
self.mcu_endstop = ppins.setup_pin('endstop', self.diag_pin)
return self.mcu_endstop
def handle_homing_move_begin(self, endstops):
if self.mcu_endstop not in endstops:
return
reg = self.fields.lookup_register("en_pwm_mode", None)
if reg is None:
# On "stallguard4" drivers, "stealthchop" must be enabled
@ -215,8 +233,9 @@ class TMCVirtualEndstop:
val = self.fields.set_field("diag1_stall", 1)
self.mcu_tmc.set_register("GCONF", val)
self.mcu_tmc.set_register("TCOOLTHRS", 0xfffff)
self.mcu_endstop.home_prepare()
def home_finalize(self):
def handle_homing_move_end(self, endstops):
if self.mcu_endstop not in endstops:
return
reg = self.fields.lookup_register("en_pwm_mode", None)
if reg is None:
self.mcu_tmc.set_register("TPWMTHRS", self.pwmthrs)
@ -226,26 +245,6 @@ class TMCVirtualEndstop:
val = self.fields.set_field("diag1_stall", 0)
self.mcu_tmc.set_register("GCONF", val)
self.mcu_tmc.set_register("TCOOLTHRS", 0)
self.mcu_endstop.home_finalize()
class TMCVirtualPinHelper:
def __init__(self, config, mcu_tmc, diag_pin):
self.printer = config.get_printer()
self.mcu_tmc = mcu_tmc
self.diag_pin = diag_pin
name_parts = config.get_name().split()
ppins = self.printer.lookup_object("pins")
ppins.register_chip("%s_%s" % (name_parts[0], name_parts[-1]), self)
def setup_pin(self, pin_type, pin_params):
ppins = self.printer.lookup_object('pins')
if pin_type != 'endstop' or pin_params['pin'] != 'virtual_endstop':
raise ppins.error("tmc virtual endstop only useful as endstop")
if pin_params['invert'] or pin_params['pullup']:
raise ppins.error("Can not pullup/invert tmc virtual pin")
if self.diag_pin is None:
raise ppins.error("tmc virtual endstop requires diag pin config")
mcu_endstop = ppins.setup_pin('endstop', self.diag_pin)
return TMCVirtualEndstop(self.mcu_tmc, mcu_endstop)
######################################################################

View File

@ -47,9 +47,9 @@ class Homing:
self.toolhead.signal_drip_mode_end()
def homing_move(self, movepos, endstops, speed,
probe_pos=False, verify_movement=False):
# Notify endstops of upcoming home
for mcu_endstop, name in endstops:
mcu_endstop.home_prepare()
# Notify start of homing/probing move
self.printer.send_event("homing:homing_move_begin",
[es for es, name in endstops])
# Note start location
self.toolhead.flush_step_generation()
kin = self.toolhead.get_kinematics()
@ -66,8 +66,6 @@ class Homing:
print_time, ENDSTOP_SAMPLE_TIME, ENDSTOP_SAMPLE_COUNT,
rest_time, notify=self._endstop_notify)
self.toolhead.dwell(HOMING_START_DELAY)
# notify anyone out there of move start
self.printer.send_event("homing:move_begin", endstops)
# Issue move
error = None
try:
@ -82,8 +80,6 @@ class Homing:
except mcu_endstop.TimeoutError as e:
if error is None:
error = "Failed to home %s: %s" % (name, str(e))
# notify anyone out there of move end
self.printer.send_event("homing:move_end", endstops)
# Determine stepper halt positions
self.toolhead.flush_step_generation()
end_mcu_pos = [(s, name, spos, s.get_mcu_position())
@ -95,13 +91,13 @@ class Homing:
self.set_homed_position(kin.calc_tag_position())
else:
self.toolhead.set_position(movepos)
# Signal homing complete
for mcu_endstop, name in endstops:
try:
mcu_endstop.home_finalize()
except CommandError as e:
if error is None:
error = str(e)
# Signal homing/probing move complete
try:
self.printer.send_event("homing:homing_move_end",
[es for es, name in endstops])
except CommandError as e:
if error is None:
error = str(e)
if error is not None:
raise CommandError(error)
# Check if some movement occurred

View File

@ -54,8 +54,6 @@ class MCU_endstop:
" rest_ticks=%u pin_value=%c", cq=cmd_queue)
self._query_cmd = self._mcu.lookup_command(
"endstop_query_state oid=%c", cq=cmd_queue)
def home_prepare(self):
pass
def home_start(self, print_time, sample_time, sample_count, rest_time,
triggered=True, notify=None):
clock = self._mcu.print_time_to_clock(print_time)
@ -112,8 +110,6 @@ class MCU_endstop:
s.note_homing_end(did_trigger=did_trigger)
if not did_trigger:
raise self.TimeoutError("Timeout during endstop homing")
def home_finalize(self):
pass
def query_endstop(self, print_time):
clock = self._mcu.print_time_to_clock(print_time)
if self._mcu.is_fileoutput():