2016-05-25 17:37:40 +02:00
|
|
|
# Code for state tracking during homing operations
|
|
|
|
#
|
2017-08-29 17:39:57 +02:00
|
|
|
# Copyright (C) 2016,2017 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.
|
2017-12-20 20:11:38 +01:00
|
|
|
import logging, math
|
2016-05-25 17:37:40 +02:00
|
|
|
|
2017-11-30 01:53:16 +01:00
|
|
|
HOMING_STEP_DELAY = 0.00000025
|
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:
|
2017-12-06 15:59:00 +01:00
|
|
|
def __init__(self, toolhead):
|
2016-07-07 20:23:48 +02:00
|
|
|
self.toolhead = 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
|
2016-12-09 00:12:20 +01:00
|
|
|
def retract(self, newpos, speed):
|
|
|
|
self.toolhead.move(self._fill_coord(newpos), speed)
|
2017-12-06 05:33:23 +01:00
|
|
|
def set_homed_position(self, pos):
|
|
|
|
self.toolhead.set_position(self._fill_coord(pos))
|
2017-12-20 20:11:38 +01:00
|
|
|
def _get_homing_speed(self, speed, endstops):
|
|
|
|
# Round the requested homing speed so that it is an even
|
|
|
|
# number of ticks per step.
|
|
|
|
speed = min(speed, self.toolhead.get_max_velocity()[0])
|
|
|
|
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-05-14 17:18:31 +02:00
|
|
|
def _homing_move(self, movepos, endstops, speed, probe_pos=False):
|
2017-12-06 05:33:23 +01:00
|
|
|
# Start endstop checking
|
2016-07-07 20:23:48 +02:00
|
|
|
print_time = self.toolhead.get_last_move_time()
|
2017-12-06 03:51:44 +01: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)
|
2017-12-06 05:33:23 +01:00
|
|
|
# Issue move
|
2017-12-06 07:00:33 +01:00
|
|
|
movepos = self._fill_coord(movepos)
|
2017-12-06 05:33:23 +01:00
|
|
|
error = None
|
|
|
|
try:
|
2017-12-06 07:00:33 +01:00
|
|
|
self.toolhead.move(movepos, speed)
|
2017-12-06 05:33:23 +01:00
|
|
|
except EndstopError as e:
|
|
|
|
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-09-28 01:21:26 +02:00
|
|
|
self.toolhead.reset_print_time(print_time)
|
2017-12-06 03:51:44 +01:00
|
|
|
for mcu_endstop, name in endstops:
|
2016-11-18 19:03:40 +01:00
|
|
|
try:
|
2017-12-06 05:33:23 +01: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 07:00:33 +01:00
|
|
|
if probe_pos:
|
|
|
|
self.set_homed_position(
|
2018-06-22 18:27:37 +02:00
|
|
|
list(self.toolhead.get_kinematics().calc_position()) + [None])
|
2017-12-06 07:00:33 +01:00
|
|
|
else:
|
|
|
|
self.toolhead.set_position(movepos)
|
2018-01-23 18:26:18 +01:00
|
|
|
for mcu_endstop, name in endstops:
|
|
|
|
mcu_endstop.home_finalize()
|
2017-12-06 05:33:23 +01:00
|
|
|
if error is not None:
|
|
|
|
raise EndstopError(error)
|
2018-05-14 17:18:31 +02:00
|
|
|
def homing_move(self, movepos, endstops, speed, probe_pos=False):
|
|
|
|
for mcu_endstop, name in endstops:
|
|
|
|
mcu_endstop.home_prepare()
|
|
|
|
self._homing_move(movepos, endstops, speed, probe_pos)
|
2017-12-06 05:33:23 +01:00
|
|
|
def home(self, forcepos, movepos, endstops, speed, second_home=False):
|
2018-03-06 07:59:13 +01:00
|
|
|
if second_home and forcepos == movepos:
|
|
|
|
return
|
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]
|
|
|
|
self.toolhead.set_position(
|
|
|
|
self._fill_coord(forcepos), homing_axes=homing_axes)
|
2018-05-14 17:18:31 +02:00
|
|
|
# Notify endstops of upcoming home
|
|
|
|
for mcu_endstop, name in endstops:
|
|
|
|
mcu_endstop.home_prepare()
|
2017-12-06 05:33:23 +01:00
|
|
|
# Add a CPU delay when homing a large axis
|
|
|
|
if not second_home:
|
|
|
|
est_move_d = sum([abs(forcepos[i]-movepos[i])
|
|
|
|
for i in range(3) if movepos[i] is not None])
|
|
|
|
est_steps = sum([est_move_d / s.get_step_dist()
|
|
|
|
for es, n in endstops for s in es.get_steppers()])
|
|
|
|
self.toolhead.dwell(est_steps * HOMING_STEP_DELAY, check_stall=False)
|
2017-12-20 20:11:38 +01:00
|
|
|
speed = self._get_homing_speed(speed, endstops)
|
2017-12-06 05:33:23 +01:00
|
|
|
# Setup for retract verification
|
|
|
|
self.toolhead.get_last_move_time()
|
|
|
|
start_mcu_pos = [(s, name, s.get_mcu_position())
|
|
|
|
for es, name in endstops for s in es.get_steppers()]
|
|
|
|
# Issue homing move
|
2018-05-14 17:18:31 +02:00
|
|
|
self._homing_move(movepos, endstops, speed)
|
2017-12-06 03:51:44 +01:00
|
|
|
# Verify retract led to some movement on second home
|
|
|
|
if second_home and self.verify_retract:
|
|
|
|
for s, name, pos in start_mcu_pos:
|
|
|
|
if s.get_mcu_position() == pos:
|
|
|
|
raise EndstopError(
|
|
|
|
"Endstop %s still triggered after retract" % (name,))
|
2017-12-06 15:59:00 +01:00
|
|
|
def home_axes(self, axes):
|
|
|
|
self.changed_axes = axes
|
|
|
|
try:
|
|
|
|
self.toolhead.get_kinematics().home(self)
|
|
|
|
except EndstopError:
|
|
|
|
self.toolhead.motor_off()
|
|
|
|
raise
|
2016-09-22 17:09:20 +02:00
|
|
|
|
2017-12-06 16:13:58 +01:00
|
|
|
def query_endstops(toolhead):
|
|
|
|
print_time = toolhead.get_last_move_time()
|
2018-06-22 17:44:25 +02:00
|
|
|
rails = toolhead.get_kinematics().get_rails()
|
2017-12-06 05:33:23 +01:00
|
|
|
out = []
|
2018-06-22 17:44:25 +02:00
|
|
|
for rail in rails:
|
|
|
|
for mcu_endstop, name in rail.get_endstops():
|
2017-11-07 18:29:51 +01:00
|
|
|
mcu_endstop.query_endstop(print_time)
|
2018-06-22 17:44:25 +02:00
|
|
|
for rail in rails:
|
|
|
|
for mcu_endstop, name in rail.get_endstops():
|
2017-12-06 05:33:23 +01:00
|
|
|
out.append((name, mcu_endstop.query_endstop_wait()))
|
2017-09-13 16:50:52 +02:00
|
|
|
return out
|
|
|
|
|
2016-09-30 22:00:32 +02:00
|
|
|
class EndstopError(Exception):
|
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]))
|