2021-01-08 20:37:57 +03:00
|
|
|
# Helper code for implementing homing operations
|
2016-05-25 18:37:40 +03:00
|
|
|
#
|
2021-01-08 20:07:45 +03:00
|
|
|
# Copyright (C) 2016-2021 Kevin O'Connor <kevin@koconnor.net>
|
2016-05-25 18:37:40 +03:00
|
|
|
#
|
|
|
|
# This file may be distributed under the terms of the GNU GPLv3 license.
|
2021-04-05 17:59:47 +03:00
|
|
|
import logging, math
|
2016-05-25 18:37:40 +03:00
|
|
|
|
2019-01-10 20:17:57 +03:00
|
|
|
HOMING_START_DELAY = 0.001
|
2017-10-12 17:39:46 +03:00
|
|
|
ENDSTOP_SAMPLE_TIME = .000015
|
|
|
|
ENDSTOP_SAMPLE_COUNT = 4
|
2017-08-29 18:39:57 +03:00
|
|
|
|
2021-03-29 18:22:30 +03:00
|
|
|
# Return a completion that completes when all completions in a list complete
|
|
|
|
def multi_complete(printer, completions):
|
|
|
|
if len(completions) == 1:
|
|
|
|
return completions[0]
|
2021-04-02 20:42:25 +03:00
|
|
|
# Build completion that waits for all completions
|
|
|
|
reactor = printer.get_reactor()
|
|
|
|
cp = reactor.register_callback(lambda e: [c.wait() for c in completions])
|
|
|
|
# If any completion indicates an error, then exit main completion early
|
|
|
|
for c in completions:
|
2022-05-13 20:53:17 +03:00
|
|
|
reactor.register_callback(
|
|
|
|
lambda e, c=c: cp.complete(1) if c.wait() else 0)
|
2021-04-02 20:42:25 +03:00
|
|
|
return cp
|
2021-03-29 18:22:30 +03:00
|
|
|
|
2021-04-05 17:59:47 +03:00
|
|
|
# Tracking of stepper positions during a homing/probing move
|
|
|
|
class StepperPosition:
|
|
|
|
def __init__(self, stepper, endstop_name):
|
|
|
|
self.stepper = stepper
|
|
|
|
self.endstop_name = endstop_name
|
|
|
|
self.stepper_name = stepper.get_name()
|
|
|
|
self.start_pos = stepper.get_mcu_position()
|
|
|
|
self.halt_pos = self.trig_pos = None
|
|
|
|
def note_home_end(self, trigger_time):
|
|
|
|
self.halt_pos = self.stepper.get_mcu_position()
|
|
|
|
self.trig_pos = self.stepper.get_past_mcu_position(trigger_time)
|
|
|
|
|
2021-03-29 18:22:30 +03:00
|
|
|
# Implementation of homing/probing moves
|
|
|
|
class HomingMove:
|
2021-03-29 20:34:25 +03:00
|
|
|
def __init__(self, printer, endstops, toolhead=None):
|
2018-10-08 21:23:20 +03:00
|
|
|
self.printer = printer
|
2021-03-29 20:34:25 +03:00
|
|
|
self.endstops = endstops
|
|
|
|
if toolhead is None:
|
|
|
|
toolhead = printer.lookup_object('toolhead')
|
|
|
|
self.toolhead = toolhead
|
2021-04-05 17:59:47 +03:00
|
|
|
self.stepper_positions = []
|
2021-03-29 21:23:46 +03:00
|
|
|
def get_mcu_endstops(self):
|
|
|
|
return [es for es, name in self.endstops]
|
2019-12-02 05:57:38 +03:00
|
|
|
def _calc_endstop_rate(self, mcu_endstop, movepos, speed):
|
|
|
|
startpos = self.toolhead.get_position()
|
|
|
|
axes_d = [mp - sp for mp, sp in zip(movepos, startpos)]
|
|
|
|
move_d = math.sqrt(sum([d*d for d in axes_d[:3]]))
|
|
|
|
move_t = move_d / speed
|
|
|
|
max_steps = max([(abs(s.calc_position_from_coord(startpos)
|
|
|
|
- s.calc_position_from_coord(movepos))
|
|
|
|
/ s.get_step_dist())
|
|
|
|
for s in mcu_endstop.get_steppers()])
|
2020-04-19 19:46:50 +03:00
|
|
|
if max_steps <= 0.:
|
|
|
|
return .001
|
2019-12-02 05:57:38 +03:00
|
|
|
return move_t / max_steps
|
2021-04-05 17:59:47 +03:00
|
|
|
def calc_toolhead_pos(self, kin_spos, offsets):
|
|
|
|
kin_spos = dict(kin_spos)
|
|
|
|
kin = self.toolhead.get_kinematics()
|
|
|
|
for stepper in kin.get_steppers():
|
|
|
|
sname = stepper.get_name()
|
|
|
|
kin_spos[sname] += offsets.get(sname, 0) * stepper.get_step_dist()
|
|
|
|
thpos = self.toolhead.get_position()
|
|
|
|
return list(kin.calc_position(kin_spos))[:3] + thpos[3:]
|
2021-03-29 20:34:25 +03:00
|
|
|
def homing_move(self, movepos, speed, probe_pos=False,
|
|
|
|
triggered=True, check_triggered=True):
|
2020-02-12 21:03:42 +03:00
|
|
|
# Notify start of homing/probing move
|
2021-03-29 21:23:46 +03:00
|
|
|
self.printer.send_event("homing:homing_move_begin", self)
|
2019-11-14 02:34:46 +03:00
|
|
|
# Note start location
|
2019-11-14 07:34:21 +03:00
|
|
|
self.toolhead.flush_step_generation()
|
2019-11-14 02:34:46 +03:00
|
|
|
kin = self.toolhead.get_kinematics()
|
2021-05-01 07:27:43 +03:00
|
|
|
kin_spos = {s.get_name(): s.get_commanded_position()
|
|
|
|
for s in kin.get_steppers()}
|
2021-04-05 17:59:47 +03:00
|
|
|
self.stepper_positions = [ StepperPosition(s, name)
|
|
|
|
for es, name in self.endstops
|
|
|
|
for s in es.get_steppers() ]
|
2019-11-14 02:34:46 +03:00
|
|
|
# Start endstop checking
|
2019-11-14 07:34:21 +03:00
|
|
|
print_time = self.toolhead.get_last_move_time()
|
2020-02-19 19:38:11 +03:00
|
|
|
endstop_triggers = []
|
2021-03-29 20:34:25 +03:00
|
|
|
for mcu_endstop, name in self.endstops:
|
2019-12-02 05:57:38 +03:00
|
|
|
rest_time = self._calc_endstop_rate(mcu_endstop, movepos, speed)
|
2020-02-19 19:38:11 +03:00
|
|
|
wait = mcu_endstop.home_start(print_time, ENDSTOP_SAMPLE_TIME,
|
2021-03-29 20:34:25 +03:00
|
|
|
ENDSTOP_SAMPLE_COUNT, rest_time,
|
|
|
|
triggered=triggered)
|
2020-02-19 19:38:11 +03:00
|
|
|
endstop_triggers.append(wait)
|
|
|
|
all_endstop_trigger = multi_complete(self.printer, endstop_triggers)
|
2019-06-26 04:25:01 +03:00
|
|
|
self.toolhead.dwell(HOMING_START_DELAY)
|
2017-12-06 07:33:23 +03:00
|
|
|
# Issue move
|
|
|
|
error = None
|
|
|
|
try:
|
2020-02-19 19:38:11 +03:00
|
|
|
self.toolhead.drip_move(movepos, speed, all_endstop_trigger)
|
2021-01-11 18:01:14 +03:00
|
|
|
except self.printer.command_error as e:
|
2017-12-06 07:33:23 +03:00
|
|
|
error = "Error during homing move: %s" % (str(e),)
|
|
|
|
# Wait for endstops to trigger
|
2021-04-05 17:59:47 +03:00
|
|
|
trigger_times = {}
|
2016-11-18 21:03:40 +03:00
|
|
|
move_end_print_time = self.toolhead.get_last_move_time()
|
2021-03-29 20:34:25 +03:00
|
|
|
for mcu_endstop, name in self.endstops:
|
2021-04-03 20:20:36 +03:00
|
|
|
trigger_time = mcu_endstop.home_wait(move_end_print_time)
|
2021-04-05 17:59:47 +03:00
|
|
|
if trigger_time > 0.:
|
|
|
|
trigger_times[name] = trigger_time
|
|
|
|
elif trigger_time < 0. and error is None:
|
2021-04-03 20:20:36 +03:00
|
|
|
error = "Communication timeout during homing %s" % (name,)
|
2021-04-05 17:59:47 +03:00
|
|
|
elif check_triggered and error is None:
|
2021-04-03 20:20:36 +03:00
|
|
|
error = "No trigger on %s after full movement" % (name,)
|
2019-11-14 02:34:46 +03:00
|
|
|
# Determine stepper halt positions
|
2019-11-14 07:34:21 +03:00
|
|
|
self.toolhead.flush_step_generation()
|
2021-04-05 17:59:47 +03:00
|
|
|
for sp in self.stepper_positions:
|
|
|
|
tt = trigger_times.get(sp.endstop_name, move_end_print_time)
|
|
|
|
sp.note_home_end(tt)
|
2017-12-06 09:00:33 +03:00
|
|
|
if probe_pos:
|
2021-04-05 17:59:47 +03:00
|
|
|
halt_steps = {sp.stepper_name: sp.halt_pos - sp.start_pos
|
|
|
|
for sp in self.stepper_positions}
|
|
|
|
trig_steps = {sp.stepper_name: sp.trig_pos - sp.start_pos
|
|
|
|
for sp in self.stepper_positions}
|
|
|
|
haltpos = trigpos = self.calc_toolhead_pos(kin_spos, trig_steps)
|
|
|
|
if trig_steps != halt_steps:
|
|
|
|
haltpos = self.calc_toolhead_pos(kin_spos, halt_steps)
|
|
|
|
else:
|
|
|
|
haltpos = trigpos = movepos
|
|
|
|
over_steps = {sp.stepper_name: sp.halt_pos - sp.trig_pos
|
|
|
|
for sp in self.stepper_positions}
|
|
|
|
if any(over_steps.values()):
|
|
|
|
self.toolhead.set_position(movepos)
|
|
|
|
halt_kin_spos = {s.get_name(): s.get_commanded_position()
|
|
|
|
for s in kin.get_steppers()}
|
|
|
|
haltpos = self.calc_toolhead_pos(halt_kin_spos, over_steps)
|
|
|
|
self.toolhead.set_position(haltpos)
|
2020-02-12 21:03:42 +03:00
|
|
|
# Signal homing/probing move complete
|
|
|
|
try:
|
2021-03-29 21:23:46 +03:00
|
|
|
self.printer.send_event("homing:homing_move_end", self)
|
2021-01-11 18:01:14 +03:00
|
|
|
except self.printer.command_error as e:
|
2020-02-12 21:03:42 +03:00
|
|
|
if error is None:
|
|
|
|
error = str(e)
|
2017-12-06 07:33:23 +03:00
|
|
|
if error is not None:
|
2021-01-11 18:01:14 +03:00
|
|
|
raise self.printer.command_error(error)
|
2021-04-05 17:59:47 +03:00
|
|
|
return trigpos
|
2021-03-29 19:32:30 +03:00
|
|
|
def check_no_movement(self):
|
|
|
|
if self.printer.get_start_args().get('debuginput') is not None:
|
|
|
|
return None
|
2021-04-05 17:59:47 +03:00
|
|
|
for sp in self.stepper_positions:
|
|
|
|
if sp.start_pos == sp.trig_pos:
|
|
|
|
return sp.endstop_name
|
2021-03-29 19:32:30 +03:00
|
|
|
return None
|
2021-03-29 18:22:30 +03:00
|
|
|
|
|
|
|
# State tracking of homing requests
|
|
|
|
class Homing:
|
|
|
|
def __init__(self, printer):
|
|
|
|
self.printer = printer
|
|
|
|
self.toolhead = printer.lookup_object('toolhead')
|
|
|
|
self.changed_axes = []
|
2021-06-10 06:34:47 +03:00
|
|
|
self.trigger_mcu_pos = {}
|
|
|
|
self.adjust_pos = {}
|
2021-03-29 18:22:30 +03:00
|
|
|
def set_axes(self, axes):
|
|
|
|
self.changed_axes = axes
|
|
|
|
def get_axes(self):
|
|
|
|
return self.changed_axes
|
2021-06-10 06:34:47 +03:00
|
|
|
def get_trigger_position(self, stepper_name):
|
|
|
|
return self.trigger_mcu_pos[stepper_name]
|
|
|
|
def set_stepper_adjustment(self, stepper_name, adjustment):
|
|
|
|
self.adjust_pos[stepper_name] = adjustment
|
2021-03-29 18:22:30 +03:00
|
|
|
def _fill_coord(self, coord):
|
|
|
|
# Fill in any None entries in 'coord' with current toolhead position
|
|
|
|
thcoord = list(self.toolhead.get_position())
|
|
|
|
for i in range(len(coord)):
|
|
|
|
if coord[i] is not None:
|
|
|
|
thcoord[i] = coord[i]
|
|
|
|
return thcoord
|
|
|
|
def set_homed_position(self, pos):
|
|
|
|
self.toolhead.set_position(self._fill_coord(pos))
|
2019-06-27 20:11:58 +03:00
|
|
|
def home_rails(self, rails, forcepos, movepos):
|
2020-02-12 21:17:45 +03:00
|
|
|
# Notify of upcoming homing operation
|
2021-01-08 20:37:57 +03:00
|
|
|
self.printer.send_event("homing:home_rails_begin", self, rails)
|
2017-12-06 07:33:23 +03:00
|
|
|
# Alter kinematics class to think printer is at forcepos
|
2018-01-17 02:58:41 +03:00
|
|
|
homing_axes = [axis for axis in range(3) if forcepos[axis] is not None]
|
2021-08-28 19:59:01 +03:00
|
|
|
startpos = self._fill_coord(forcepos)
|
|
|
|
homepos = self._fill_coord(movepos)
|
|
|
|
self.toolhead.set_position(startpos, homing_axes=homing_axes)
|
2019-06-27 20:11:58 +03:00
|
|
|
# Perform first home
|
2018-10-09 04:49:56 +03:00
|
|
|
endstops = [es for rail in rails for es in rail.get_endstops()]
|
|
|
|
hi = rails[0].get_homing_info()
|
2021-03-29 20:34:25 +03:00
|
|
|
hmove = HomingMove(self.printer, endstops)
|
2021-08-28 19:59:01 +03:00
|
|
|
hmove.homing_move(homepos, hi.speed)
|
2018-10-09 04:49:56 +03:00
|
|
|
# Perform second home
|
|
|
|
if hi.retract_dist:
|
|
|
|
# Retract
|
2021-08-28 19:59:01 +03:00
|
|
|
startpos = self._fill_coord(forcepos)
|
|
|
|
homepos = self._fill_coord(movepos)
|
|
|
|
axes_d = [hp - sp for hp, sp in zip(homepos, startpos)]
|
2018-10-09 04:49:56 +03:00
|
|
|
move_d = math.sqrt(sum([d*d for d in axes_d[:3]]))
|
|
|
|
retract_r = min(1., hi.retract_dist / move_d)
|
2021-08-28 19:59:01 +03:00
|
|
|
retractpos = [hp - ad * retract_r
|
|
|
|
for hp, ad in zip(homepos, axes_d)]
|
2020-02-12 18:09:39 +03:00
|
|
|
self.toolhead.move(retractpos, hi.retract_speed)
|
2018-10-09 04:49:56 +03:00
|
|
|
# Home again
|
2021-08-28 19:59:01 +03:00
|
|
|
startpos = [rp - ad * retract_r
|
2018-10-09 04:49:56 +03:00
|
|
|
for rp, ad in zip(retractpos, axes_d)]
|
2021-08-28 19:59:01 +03:00
|
|
|
self.toolhead.set_position(startpos)
|
2021-03-29 20:34:25 +03:00
|
|
|
hmove = HomingMove(self.printer, endstops)
|
2021-08-28 19:59:01 +03:00
|
|
|
hmove.homing_move(homepos, hi.second_homing_speed)
|
2021-03-29 19:32:30 +03:00
|
|
|
if hmove.check_no_movement() is not None:
|
|
|
|
raise self.printer.command_error(
|
|
|
|
"Endstop %s still triggered after retract"
|
|
|
|
% (hmove.check_no_movement(),))
|
2018-10-10 04:36:02 +03:00
|
|
|
# Signal home operation complete
|
2019-11-14 07:34:21 +03:00
|
|
|
self.toolhead.flush_step_generation()
|
2021-06-10 06:34:47 +03:00
|
|
|
self.trigger_mcu_pos = {sp.stepper_name: sp.trig_pos
|
|
|
|
for sp in hmove.stepper_positions}
|
|
|
|
self.adjust_pos = {}
|
2021-05-01 07:27:43 +03:00
|
|
|
self.printer.send_event("homing:home_rails_end", self, rails)
|
2021-06-10 06:34:47 +03:00
|
|
|
if any(self.adjust_pos.values()):
|
2018-10-10 04:36:02 +03:00
|
|
|
# Apply any homing offsets
|
2021-06-10 06:34:47 +03:00
|
|
|
kin = self.toolhead.get_kinematics()
|
2021-08-28 19:59:01 +03:00
|
|
|
homepos = self.toolhead.get_position()
|
2021-06-10 06:34:47 +03:00
|
|
|
kin_spos = {s.get_name(): (s.get_commanded_position()
|
|
|
|
+ self.adjust_pos.get(s.get_name(), 0.))
|
|
|
|
for s in kin.get_steppers()}
|
|
|
|
newpos = kin.calc_position(kin_spos)
|
2018-10-10 04:36:02 +03:00
|
|
|
for axis in homing_axes:
|
2021-08-28 19:59:01 +03:00
|
|
|
homepos[axis] = newpos[axis]
|
|
|
|
self.toolhead.set_position(homepos)
|
2016-09-22 18:09:20 +03:00
|
|
|
|
2021-01-08 20:37:57 +03:00
|
|
|
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)
|
2021-03-29 20:34:25 +03:00
|
|
|
def manual_home(self, toolhead, endstops, pos, speed,
|
|
|
|
triggered, check_triggered):
|
|
|
|
hmove = HomingMove(self.printer, endstops, toolhead)
|
2021-08-06 20:05:09 +03:00
|
|
|
try:
|
|
|
|
hmove.homing_move(pos, speed, triggered=triggered,
|
|
|
|
check_triggered=check_triggered)
|
|
|
|
except self.printer.command_error:
|
|
|
|
if self.printer.is_shutdown():
|
|
|
|
raise self.printer.command_error(
|
|
|
|
"Homing failed due to printer shutdown")
|
|
|
|
raise
|
2021-03-29 17:05:39 +03:00
|
|
|
def probing_move(self, mcu_probe, pos, speed):
|
|
|
|
endstops = [(mcu_probe, "probe")]
|
2021-03-29 20:34:25 +03:00
|
|
|
hmove = HomingMove(self.printer, endstops)
|
2021-08-06 20:05:09 +03:00
|
|
|
try:
|
|
|
|
epos = hmove.homing_move(pos, speed, probe_pos=True)
|
|
|
|
except self.printer.command_error:
|
|
|
|
if self.printer.is_shutdown():
|
|
|
|
raise self.printer.command_error(
|
|
|
|
"Probing failed due to printer shutdown")
|
|
|
|
raise
|
2021-03-29 19:32:30 +03:00
|
|
|
if hmove.check_no_movement() is not None:
|
|
|
|
raise self.printer.command_error(
|
|
|
|
"Probe triggered prior to movement")
|
|
|
|
return epos
|
2021-01-08 20:37:57 +03:00
|
|
|
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)
|
2021-03-29 17:45:59 +03:00
|
|
|
homing_state.set_axes(axes)
|
|
|
|
kin = self.printer.lookup_object('toolhead').get_kinematics()
|
|
|
|
try:
|
|
|
|
kin.home(homing_state)
|
|
|
|
except self.printer.command_error:
|
2021-08-06 20:05:09 +03:00
|
|
|
if self.printer.is_shutdown():
|
|
|
|
raise self.printer.command_error(
|
|
|
|
"Homing failed due to printer shutdown")
|
2021-03-29 17:45:59 +03:00
|
|
|
self.printer.lookup_object('stepper_enable').motor_off()
|
|
|
|
raise
|
2021-01-08 20:37:57 +03:00
|
|
|
|
|
|
|
def load_config(config):
|
|
|
|
return PrinterHoming(config)
|