2016-05-25 18:37:40 +03:00
|
|
|
# Code for state tracking during homing operations
|
|
|
|
#
|
2017-08-29 18:39:57 +03:00
|
|
|
# Copyright (C) 2016,2017 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.
|
|
|
|
import logging
|
|
|
|
|
2017-11-30 03:53:16 +03:00
|
|
|
HOMING_STEP_DELAY = 0.00000025
|
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:
|
2016-07-26 06:47:30 +03:00
|
|
|
def __init__(self, toolhead, changed_axes):
|
2016-07-07 21:23:48 +03:00
|
|
|
self.toolhead = toolhead
|
2016-07-26 06:47:30 +03:00
|
|
|
self.changed_axes = 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
|
2016-12-09 02:12:20 +03:00
|
|
|
def retract(self, newpos, speed):
|
|
|
|
self.toolhead.move(self._fill_coord(newpos), speed)
|
|
|
|
def home(self, forcepos, movepos, steppers, speed, second_home=False):
|
2016-07-26 06:47:30 +03:00
|
|
|
# Alter kinematics class to think printer is at forcepos
|
2016-12-09 02:12:20 +03:00
|
|
|
self.toolhead.set_position(self._fill_coord(forcepos))
|
2016-05-25 18:37:40 +03:00
|
|
|
# Start homing and issue move
|
2017-08-29 18:39:57 +03:00
|
|
|
if not second_home:
|
2017-11-30 03:53:16 +03:00
|
|
|
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 / mcu_stepper.get_step_dist()
|
|
|
|
for s in steppers
|
|
|
|
for mcu_endstop, mcu_stepper, name in s.get_endstops()])
|
|
|
|
self.toolhead.dwell(est_steps * HOMING_STEP_DELAY, check_stall=False)
|
2016-07-07 21:23:48 +03:00
|
|
|
print_time = self.toolhead.get_last_move_time()
|
2016-12-09 02:12:20 +03:00
|
|
|
endstops = []
|
2016-07-26 06:47:30 +03:00
|
|
|
for s in steppers:
|
2017-11-07 20:29:51 +03:00
|
|
|
for mcu_endstop, mcu_stepper, name in s.get_endstops():
|
|
|
|
mcu_endstop.home_start(
|
|
|
|
print_time, ENDSTOP_SAMPLE_TIME, ENDSTOP_SAMPLE_COUNT,
|
|
|
|
mcu_stepper.get_step_dist() / speed)
|
|
|
|
endstops.append((mcu_endstop, mcu_stepper, name,
|
|
|
|
mcu_stepper.get_mcu_position()))
|
2016-12-09 02:12:20 +03:00
|
|
|
self.toolhead.move(self._fill_coord(movepos), speed)
|
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-11-07 20:29:51 +03:00
|
|
|
for mcu_endstop, mcu_stepper, name, last_pos in endstops:
|
|
|
|
mcu_endstop.home_finalize(move_end_print_time)
|
2016-12-09 02:12:20 +03:00
|
|
|
# Wait for endstops to trigger
|
2017-11-07 20:29:51 +03:00
|
|
|
for mcu_endstop, mcu_stepper, name, last_pos in endstops:
|
2016-11-18 21:03:40 +03:00
|
|
|
try:
|
2017-11-07 20:29:51 +03:00
|
|
|
mcu_endstop.home_wait()
|
|
|
|
except mcu_endstop.error as e:
|
2016-11-18 21:03:40 +03:00
|
|
|
raise EndstopError("Failed to home stepper %s: %s" % (
|
2017-11-07 20:29:51 +03:00
|
|
|
name, str(e)))
|
|
|
|
post_home_pos = mcu_stepper.get_mcu_position()
|
2016-12-09 02:12:20 +03:00
|
|
|
if second_home and self.verify_retract and last_pos == post_home_pos:
|
2016-11-18 21:39:48 +03:00
|
|
|
raise EndstopError("Endstop %s still triggered after retract" % (
|
2017-11-07 20:29:51 +03:00
|
|
|
name,))
|
2016-12-09 02:12:20 +03:00
|
|
|
def set_homed_position(self, pos):
|
|
|
|
self.toolhead.set_position(self._fill_coord(pos))
|
2016-09-22 18:09:20 +03:00
|
|
|
|
2017-10-04 02:28:20 +03:00
|
|
|
def query_endstops(print_time, query_flags, steppers):
|
|
|
|
if query_flags == "get_mcu_position":
|
|
|
|
# Only the commanded position is requested
|
2017-11-07 20:29:51 +03:00
|
|
|
return [(name.upper(), mcu_stepper.get_mcu_position())
|
|
|
|
for s in steppers
|
|
|
|
for mcu_endstop, mcu_stepper, name in s.get_endstops()]
|
2017-09-13 17:50:52 +03:00
|
|
|
for s in steppers:
|
2017-11-07 20:29:51 +03:00
|
|
|
for mcu_endstop, mcu_stepper, name in s.get_endstops():
|
|
|
|
mcu_endstop.query_endstop(print_time)
|
2017-09-13 17:50:52 +03:00
|
|
|
out = []
|
|
|
|
for s in steppers:
|
2017-11-07 20:29:51 +03:00
|
|
|
for mcu_endstop, mcu_stepper, name in s.get_endstops():
|
|
|
|
try:
|
|
|
|
out.append((name, mcu_endstop.query_endstop_wait()))
|
|
|
|
except mcu_endstop.error as e:
|
|
|
|
raise EndstopError(str(e))
|
2017-09-13 17:50:52 +03:00
|
|
|
return out
|
|
|
|
|
2016-09-30 23:00:32 +03:00
|
|
|
class EndstopError(Exception):
|
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]))
|