2016-05-25 18:37:40 +03:00
|
|
|
# Code for state tracking during homing operations
|
|
|
|
#
|
2019-05-29 00:40:19 +03:00
|
|
|
# Copyright (C) 2016-2019 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.
|
2019-05-29 00:40:19 +03:00
|
|
|
import logging, math, collections
|
2016-05-25 18:37:40 +03:00
|
|
|
|
2017-11-30 03:53:16 +03:00
|
|
|
HOMING_STEP_DELAY = 0.00000025
|
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
|
|
|
|
2016-05-25 18:37:40 +03:00
|
|
|
class Homing:
|
2018-10-08 21:23:20 +03:00
|
|
|
def __init__(self, printer):
|
|
|
|
self.printer = printer
|
|
|
|
self.toolhead = printer.lookup_object('toolhead')
|
2017-12-06 17:59:00 +03:00
|
|
|
self.changed_axes = []
|
2016-11-28 19:23:26 +03:00
|
|
|
self.verify_retract = True
|
|
|
|
def set_no_verify_retract(self):
|
|
|
|
self.verify_retract = False
|
2016-11-18 19:27:16 +03:00
|
|
|
def set_axes(self, axes):
|
|
|
|
self.changed_axes = axes
|
|
|
|
def get_axes(self):
|
|
|
|
return self.changed_axes
|
2016-12-09 02:12:20 +03:00
|
|
|
def _fill_coord(self, coord):
|
2016-07-26 06:47:30 +03:00
|
|
|
# Fill in any None entries in 'coord' with current toolhead position
|
2016-11-18 19:49:05 +03:00
|
|
|
thcoord = list(self.toolhead.get_position())
|
2016-07-26 06:47:30 +03:00
|
|
|
for i in range(len(coord)):
|
2016-11-18 19:49:05 +03:00
|
|
|
if coord[i] is not None:
|
|
|
|
thcoord[i] = coord[i]
|
|
|
|
return thcoord
|
2017-12-06 07:33:23 +03:00
|
|
|
def set_homed_position(self, pos):
|
|
|
|
self.toolhead.set_position(self._fill_coord(pos))
|
2017-12-20 22:11:38 +03:00
|
|
|
def _get_homing_speed(self, speed, endstops):
|
|
|
|
# Round the requested homing speed so that it is an even
|
|
|
|
# number of ticks per step.
|
|
|
|
mcu_stepper = endstops[0][0].get_steppers()[0]
|
|
|
|
adjusted_freq = mcu_stepper.get_mcu().get_adjusted_freq()
|
|
|
|
dist_ticks = adjusted_freq * mcu_stepper.get_step_dist()
|
|
|
|
ticks_per_step = math.ceil(dist_ticks / speed)
|
|
|
|
return dist_ticks / ticks_per_step
|
2018-10-09 04:49:56 +03:00
|
|
|
def homing_move(self, movepos, endstops, speed, dwell_t=0.,
|
|
|
|
probe_pos=False, verify_movement=False):
|
|
|
|
# Notify endstops of upcoming home
|
|
|
|
for mcu_endstop, name in endstops:
|
|
|
|
mcu_endstop.home_prepare()
|
|
|
|
if dwell_t:
|
|
|
|
self.toolhead.dwell(dwell_t, check_stall=False)
|
2017-12-06 07:33:23 +03:00
|
|
|
# Start endstop checking
|
2016-07-07 21:23:48 +03:00
|
|
|
print_time = self.toolhead.get_last_move_time()
|
2018-09-26 22:57:22 +03:00
|
|
|
start_mcu_pos = [(s, name, s.get_mcu_position())
|
|
|
|
for es, name in endstops for s in es.get_steppers()]
|
2017-12-06 05:51:44 +03:00
|
|
|
for mcu_endstop, name in endstops:
|
|
|
|
min_step_dist = min([s.get_step_dist()
|
|
|
|
for s in mcu_endstop.get_steppers()])
|
|
|
|
mcu_endstop.home_start(
|
|
|
|
print_time, ENDSTOP_SAMPLE_TIME, ENDSTOP_SAMPLE_COUNT,
|
|
|
|
min_step_dist / speed)
|
2019-01-10 20:17:57 +03:00
|
|
|
self.toolhead.dwell(HOMING_START_DELAY, check_stall=False)
|
2017-12-06 07:33:23 +03:00
|
|
|
# Issue move
|
|
|
|
error = None
|
|
|
|
try:
|
2017-12-06 09:00:33 +03:00
|
|
|
self.toolhead.move(movepos, speed)
|
2019-06-03 20:01:57 +03:00
|
|
|
except CommandError as e:
|
2017-12-06 07:33:23 +03:00
|
|
|
error = "Error during homing move: %s" % (str(e),)
|
|
|
|
# Wait for endstops to trigger
|
2016-11-18 21:03:40 +03:00
|
|
|
move_end_print_time = self.toolhead.get_last_move_time()
|
2017-09-28 02:21:26 +03:00
|
|
|
self.toolhead.reset_print_time(print_time)
|
2017-12-06 05:51:44 +03:00
|
|
|
for mcu_endstop, name in endstops:
|
2016-11-18 21:03:40 +03:00
|
|
|
try:
|
2017-12-06 07:33:23 +03:00
|
|
|
mcu_endstop.home_wait(move_end_print_time)
|
|
|
|
except mcu_endstop.TimeoutError as e:
|
|
|
|
if error is None:
|
|
|
|
error = "Failed to home %s: %s" % (name, str(e))
|
2017-12-06 09:00:33 +03:00
|
|
|
if probe_pos:
|
|
|
|
self.set_homed_position(
|
2018-06-22 19:27:37 +03:00
|
|
|
list(self.toolhead.get_kinematics().calc_position()) + [None])
|
2017-12-06 09:00:33 +03:00
|
|
|
else:
|
|
|
|
self.toolhead.set_position(movepos)
|
2018-01-23 20:26:18 +03:00
|
|
|
for mcu_endstop, name in endstops:
|
2019-01-05 03:12:50 +03:00
|
|
|
try:
|
|
|
|
mcu_endstop.home_finalize()
|
2019-06-03 20:01:57 +03:00
|
|
|
except CommandError as e:
|
2019-01-05 03:12:50 +03:00
|
|
|
if error is None:
|
|
|
|
error = str(e)
|
2017-12-06 07:33:23 +03:00
|
|
|
if error is not None:
|
2019-06-03 20:01:57 +03:00
|
|
|
raise CommandError(error)
|
2018-09-26 22:57:22 +03:00
|
|
|
# Check if some movement occurred
|
|
|
|
if verify_movement:
|
|
|
|
for s, name, pos in start_mcu_pos:
|
|
|
|
if s.get_mcu_position() == pos:
|
|
|
|
if probe_pos:
|
|
|
|
raise EndstopError("Probe triggered prior to movement")
|
|
|
|
raise EndstopError(
|
|
|
|
"Endstop %s still triggered after retract" % (name,))
|
2018-10-09 04:49:56 +03:00
|
|
|
def home_rails(self, rails, forcepos, movepos, limit_speed=None):
|
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]
|
2018-10-09 04:49:56 +03:00
|
|
|
forcepos = self._fill_coord(forcepos)
|
|
|
|
movepos = self._fill_coord(movepos)
|
|
|
|
self.toolhead.set_position(forcepos, homing_axes=homing_axes)
|
|
|
|
# Determine homing speed
|
|
|
|
endstops = [es for rail in rails for es in rail.get_endstops()]
|
|
|
|
hi = rails[0].get_homing_info()
|
|
|
|
max_velocity = self.toolhead.get_max_velocity()[0]
|
|
|
|
if limit_speed is not None and limit_speed < max_velocity:
|
|
|
|
max_velocity = limit_speed
|
|
|
|
homing_speed = min(hi.speed, max_velocity)
|
|
|
|
homing_speed = self._get_homing_speed(homing_speed, endstops)
|
|
|
|
second_homing_speed = min(hi.second_homing_speed, max_velocity)
|
|
|
|
# Calculate a CPU delay when homing a large axis
|
|
|
|
axes_d = [mp - fp for mp, fp in zip(movepos, forcepos)]
|
|
|
|
est_move_d = abs(axes_d[0]) + abs(axes_d[1]) + abs(axes_d[2])
|
|
|
|
est_steps = sum([est_move_d / s.get_step_dist()
|
|
|
|
for es, n in endstops for s in es.get_steppers()])
|
|
|
|
dwell_t = est_steps * HOMING_STEP_DELAY
|
|
|
|
# Perform first home
|
|
|
|
self.homing_move(movepos, endstops, homing_speed, dwell_t=dwell_t)
|
|
|
|
# Perform second home
|
|
|
|
if hi.retract_dist:
|
|
|
|
# Retract
|
|
|
|
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)]
|
|
|
|
self.toolhead.move(retractpos, homing_speed)
|
|
|
|
# Home again
|
|
|
|
forcepos = [rp - ad * retract_r
|
|
|
|
for rp, ad in zip(retractpos, axes_d)]
|
|
|
|
self.toolhead.set_position(forcepos)
|
|
|
|
self.homing_move(movepos, endstops, second_homing_speed,
|
|
|
|
verify_movement=self.verify_retract)
|
2018-10-10 04:36:02 +03:00
|
|
|
# Signal home operation complete
|
|
|
|
ret = self.printer.send_event("homing:homed_rails", self, rails)
|
|
|
|
if any(ret):
|
|
|
|
# Apply any homing offsets
|
|
|
|
adjustpos = self.toolhead.get_kinematics().calc_position()
|
|
|
|
for axis in homing_axes:
|
|
|
|
movepos[axis] = adjustpos[axis]
|
|
|
|
self.toolhead.set_position(movepos)
|
2017-12-06 17:59:00 +03:00
|
|
|
def home_axes(self, axes):
|
|
|
|
self.changed_axes = axes
|
|
|
|
try:
|
|
|
|
self.toolhead.get_kinematics().home(self)
|
2019-06-03 20:01:57 +03:00
|
|
|
except CommandError:
|
2017-12-06 17:59:00 +03:00
|
|
|
self.toolhead.motor_off()
|
|
|
|
raise
|
2016-09-22 18:09:20 +03:00
|
|
|
|
2019-06-03 19:48:35 +03:00
|
|
|
class CommandError(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class EndstopError(CommandError):
|
2016-11-30 02:27:48 +03:00
|
|
|
pass
|
2016-11-18 20:42:39 +03: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-29 00:40:19 +03:00
|
|
|
|
|
|
|
Coord = collections.namedtuple('Coord', ('x', 'y', 'z', 'e'))
|