2016-05-25 17:37:40 +02:00
|
|
|
# Code for state tracking during homing operations
|
|
|
|
#
|
2019-05-28 23:40:19 +02:00
|
|
|
# Copyright (C) 2016-2019 Kevin O'Connor <kevin@koconnor.net>
|
2016-05-25 17:37:40 +02:00
|
|
|
#
|
|
|
|
# This file may be distributed under the terms of the GNU GPLv3 license.
|
2019-05-28 23:40:19 +02:00
|
|
|
import logging, math, collections
|
2016-05-25 17:37:40 +02:00
|
|
|
|
2019-01-10 18:17:57 +01:00
|
|
|
HOMING_START_DELAY = 0.001
|
2017-10-12 16:39:46 +02:00
|
|
|
ENDSTOP_SAMPLE_TIME = .000015
|
|
|
|
ENDSTOP_SAMPLE_COUNT = 4
|
2017-08-29 17:39:57 +02:00
|
|
|
|
2016-05-25 17:37:40 +02:00
|
|
|
class Homing:
|
2018-10-08 20:23:20 +02:00
|
|
|
def __init__(self, printer):
|
|
|
|
self.printer = printer
|
|
|
|
self.toolhead = printer.lookup_object('toolhead')
|
2017-12-06 15:59:00 +01:00
|
|
|
self.changed_axes = []
|
2016-11-28 17:23:26 +01:00
|
|
|
self.verify_retract = True
|
|
|
|
def set_no_verify_retract(self):
|
|
|
|
self.verify_retract = False
|
2016-11-18 17:27:16 +01:00
|
|
|
def set_axes(self, axes):
|
|
|
|
self.changed_axes = axes
|
|
|
|
def get_axes(self):
|
|
|
|
return self.changed_axes
|
2016-12-09 00:12:20 +01:00
|
|
|
def _fill_coord(self, coord):
|
2016-07-26 05:47:30 +02:00
|
|
|
# Fill in any None entries in 'coord' with current toolhead position
|
2016-11-18 17:49:05 +01:00
|
|
|
thcoord = list(self.toolhead.get_position())
|
2016-07-26 05:47:30 +02:00
|
|
|
for i in range(len(coord)):
|
2016-11-18 17:49:05 +01:00
|
|
|
if coord[i] is not None:
|
|
|
|
thcoord[i] = coord[i]
|
|
|
|
return thcoord
|
2017-12-06 05:33:23 +01:00
|
|
|
def set_homed_position(self, pos):
|
|
|
|
self.toolhead.set_position(self._fill_coord(pos))
|
2019-12-02 03:57:38 +01: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()])
|
|
|
|
return move_t / max_steps
|
2019-06-27 19:11:58 +02:00
|
|
|
def homing_move(self, movepos, endstops, speed,
|
2018-10-09 03:49:56 +02:00
|
|
|
probe_pos=False, verify_movement=False):
|
2020-02-12 19:03:42 +01:00
|
|
|
# Notify start of homing/probing move
|
|
|
|
self.printer.send_event("homing:homing_move_begin",
|
|
|
|
[es for es, name in endstops])
|
2019-11-14 00:34:46 +01:00
|
|
|
# Note start location
|
2019-11-14 05:34:21 +01:00
|
|
|
self.toolhead.flush_step_generation()
|
2019-11-14 00:34:46 +01:00
|
|
|
kin = self.toolhead.get_kinematics()
|
|
|
|
for s in kin.get_steppers():
|
|
|
|
s.set_tag_position(s.get_commanded_position())
|
2018-09-26 21:57:22 +02:00
|
|
|
start_mcu_pos = [(s, name, s.get_mcu_position())
|
|
|
|
for es, name in endstops for s in es.get_steppers()]
|
2019-11-14 00:34:46 +01:00
|
|
|
# Start endstop checking
|
2019-11-14 05:34:21 +01:00
|
|
|
print_time = self.toolhead.get_last_move_time()
|
2020-02-19 17:38:11 +01:00
|
|
|
endstop_triggers = []
|
2017-12-06 03:51:44 +01:00
|
|
|
for mcu_endstop, name in endstops:
|
2019-12-02 03:57:38 +01:00
|
|
|
rest_time = self._calc_endstop_rate(mcu_endstop, movepos, speed)
|
2020-02-19 17:38:11 +01:00
|
|
|
wait = mcu_endstop.home_start(print_time, ENDSTOP_SAMPLE_TIME,
|
|
|
|
ENDSTOP_SAMPLE_COUNT, rest_time)
|
|
|
|
endstop_triggers.append(wait)
|
|
|
|
all_endstop_trigger = multi_complete(self.printer, endstop_triggers)
|
2019-06-26 03:25:01 +02:00
|
|
|
self.toolhead.dwell(HOMING_START_DELAY)
|
2017-12-06 05:33:23 +01:00
|
|
|
# Issue move
|
|
|
|
error = None
|
|
|
|
try:
|
2020-02-19 17:38:11 +01:00
|
|
|
self.toolhead.drip_move(movepos, speed, all_endstop_trigger)
|
2019-06-03 19:01:57 +02:00
|
|
|
except CommandError as e:
|
2017-12-06 05:33:23 +01:00
|
|
|
error = "Error during homing move: %s" % (str(e),)
|
|
|
|
# Wait for endstops to trigger
|
2016-11-18 19:03:40 +01:00
|
|
|
move_end_print_time = self.toolhead.get_last_move_time()
|
2017-12-06 03:51:44 +01:00
|
|
|
for mcu_endstop, name in endstops:
|
2020-02-19 18:07:30 +01:00
|
|
|
did_trigger = mcu_endstop.home_wait(move_end_print_time)
|
|
|
|
if not did_trigger and error is None:
|
|
|
|
error = "Failed to home %s: Timeout during homing" % (name,)
|
2019-11-14 00:34:46 +01:00
|
|
|
# Determine stepper halt positions
|
2019-11-14 05:34:21 +01:00
|
|
|
self.toolhead.flush_step_generation()
|
2019-11-14 00:34:46 +01:00
|
|
|
end_mcu_pos = [(s, name, spos, s.get_mcu_position())
|
|
|
|
for s, name, spos in start_mcu_pos]
|
2017-12-06 07:00:33 +01:00
|
|
|
if probe_pos:
|
2019-11-14 00:34:46 +01:00
|
|
|
for s, name, spos, epos in end_mcu_pos:
|
|
|
|
md = (epos - spos) * s.get_step_dist()
|
|
|
|
s.set_tag_position(s.get_tag_position() + md)
|
2019-11-13 23:59:40 +01:00
|
|
|
self.set_homed_position(kin.calc_tag_position())
|
2017-12-06 07:00:33 +01:00
|
|
|
else:
|
|
|
|
self.toolhead.set_position(movepos)
|
2020-02-12 19:03:42 +01:00
|
|
|
# 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)
|
2017-12-06 05:33:23 +01:00
|
|
|
if error is not None:
|
2019-06-03 19:01:57 +02:00
|
|
|
raise CommandError(error)
|
2018-09-26 21:57:22 +02:00
|
|
|
# Check if some movement occurred
|
|
|
|
if verify_movement:
|
2019-11-14 00:34:46 +01:00
|
|
|
for s, name, spos, epos in end_mcu_pos:
|
|
|
|
if spos == epos:
|
2018-09-26 21:57:22 +02:00
|
|
|
if probe_pos:
|
|
|
|
raise EndstopError("Probe triggered prior to movement")
|
|
|
|
raise EndstopError(
|
|
|
|
"Endstop %s still triggered after retract" % (name,))
|
2019-06-27 19:11:58 +02:00
|
|
|
def home_rails(self, rails, forcepos, movepos):
|
2020-02-12 19:17:45 +01:00
|
|
|
# Notify of upcoming homing operation
|
2020-02-19 17:38:11 +01:00
|
|
|
self.printer.send_event("homing:home_rails_begin", rails)
|
2017-12-06 05:33:23 +01:00
|
|
|
# Alter kinematics class to think printer is at forcepos
|
2018-01-17 00:58:41 +01:00
|
|
|
homing_axes = [axis for axis in range(3) if forcepos[axis] is not None]
|
2018-10-09 03:49:56 +02:00
|
|
|
forcepos = self._fill_coord(forcepos)
|
|
|
|
movepos = self._fill_coord(movepos)
|
|
|
|
self.toolhead.set_position(forcepos, homing_axes=homing_axes)
|
2019-06-27 19:11:58 +02:00
|
|
|
# Perform first home
|
2018-10-09 03:49:56 +02:00
|
|
|
endstops = [es for rail in rails for es in rail.get_endstops()]
|
|
|
|
hi = rails[0].get_homing_info()
|
2019-06-27 19:11:58 +02:00
|
|
|
self.homing_move(movepos, endstops, hi.speed)
|
2018-10-09 03:49:56 +02:00
|
|
|
# Perform second home
|
|
|
|
if hi.retract_dist:
|
|
|
|
# Retract
|
2019-06-27 19:11:58 +02:00
|
|
|
axes_d = [mp - fp for mp, fp in zip(movepos, forcepos)]
|
2018-10-09 03:49:56 +02:00
|
|
|
move_d = math.sqrt(sum([d*d for d in axes_d[:3]]))
|
|
|
|
retract_r = min(1., hi.retract_dist / move_d)
|
|
|
|
retractpos = [mp - ad * retract_r
|
|
|
|
for mp, ad in zip(movepos, axes_d)]
|
2020-02-12 16:09:39 +01:00
|
|
|
self.toolhead.move(retractpos, hi.retract_speed)
|
2018-10-09 03:49:56 +02:00
|
|
|
# Home again
|
|
|
|
forcepos = [rp - ad * retract_r
|
|
|
|
for rp, ad in zip(retractpos, axes_d)]
|
|
|
|
self.toolhead.set_position(forcepos)
|
2019-06-27 19:11:58 +02:00
|
|
|
self.homing_move(movepos, endstops, hi.second_homing_speed,
|
2018-10-09 03:49:56 +02:00
|
|
|
verify_movement=self.verify_retract)
|
2018-10-10 03:36:02 +02:00
|
|
|
# Signal home operation complete
|
2019-11-14 05:34:21 +01:00
|
|
|
self.toolhead.flush_step_generation()
|
2019-11-14 00:11:13 +01:00
|
|
|
kin = self.toolhead.get_kinematics()
|
|
|
|
for s in kin.get_steppers():
|
|
|
|
s.set_tag_position(s.get_commanded_position())
|
2020-02-12 19:17:45 +01:00
|
|
|
ret = self.printer.send_event("homing:home_rails_end", rails)
|
2018-10-10 03:36:02 +02:00
|
|
|
if any(ret):
|
|
|
|
# Apply any homing offsets
|
2019-11-13 23:59:40 +01:00
|
|
|
adjustpos = kin.calc_tag_position()
|
2018-10-10 03:36:02 +02:00
|
|
|
for axis in homing_axes:
|
|
|
|
movepos[axis] = adjustpos[axis]
|
|
|
|
self.toolhead.set_position(movepos)
|
2017-12-06 15:59:00 +01:00
|
|
|
def home_axes(self, axes):
|
|
|
|
self.changed_axes = axes
|
|
|
|
try:
|
|
|
|
self.toolhead.get_kinematics().home(self)
|
2019-06-03 19:01:57 +02:00
|
|
|
except CommandError:
|
2019-11-12 17:41:41 +01:00
|
|
|
self.printer.lookup_object('stepper_enable').motor_off()
|
2017-12-06 15:59:00 +01:00
|
|
|
raise
|
2016-09-22 17:09:20 +02:00
|
|
|
|
2020-02-19 17:38:11 +01: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]
|
|
|
|
cb = (lambda e: all([c.wait() for c in completions]))
|
|
|
|
return printer.get_reactor().register_callback(cb)
|
|
|
|
|
2019-06-03 18:48:35 +02:00
|
|
|
class CommandError(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class EndstopError(CommandError):
|
2016-11-30 00:27:48 +01:00
|
|
|
pass
|
2016-11-18 18:42:39 +01:00
|
|
|
|
|
|
|
def EndstopMoveError(pos, msg="Move out of range"):
|
|
|
|
return EndstopError("%s: %.3f %.3f %.3f [%.3f]" % (
|
|
|
|
msg, pos[0], pos[1], pos[2], pos[3]))
|
2019-05-28 23:40:19 +02:00
|
|
|
|
|
|
|
Coord = collections.namedtuple('Coord', ('x', 'y', 'z', 'e'))
|