2016-05-25 18:37:40 +03:00
|
|
|
# Code for state tracking during homing operations
|
|
|
|
#
|
|
|
|
# Copyright (C) 2016 Kevin O'Connor <kevin@koconnor.net>
|
|
|
|
#
|
|
|
|
# This file may be distributed under the terms of the GNU GPLv3 license.
|
|
|
|
import logging
|
2016-11-18 21:03:40 +03:00
|
|
|
import mcu
|
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
|
2016-05-25 18:37:40 +03:00
|
|
|
|
2016-09-22 18:09:20 +03:00
|
|
|
self.eventtime = 0.
|
2016-05-25 18:37:40 +03:00
|
|
|
self.states = []
|
|
|
|
self.endstops = []
|
2016-11-28 19:23:26 +03:00
|
|
|
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-07-26 06:47:30 +03:00
|
|
|
def plan_home(self, forcepos, movepos, steppers, speed):
|
|
|
|
self.states.append((self.do_home, (forcepos, movepos, steppers, speed)))
|
2016-11-28 19:23:26 +03:00
|
|
|
self.states.append((self.do_wait_endstop, (False,)))
|
|
|
|
def plan_second_home(self, forcepos, movepos, steppers, speed):
|
|
|
|
self.states.append((self.do_home, (forcepos, movepos, steppers, speed)))
|
|
|
|
self.states.append((self.do_wait_endstop, (self.verify_retract,)))
|
2016-11-18 21:39:48 +03:00
|
|
|
def plan_retract(self, newpos, steppers, speed):
|
|
|
|
self.states.append((self.do_retract, (newpos, steppers, speed)))
|
2016-11-18 19:49:05 +03:00
|
|
|
def plan_calc_position(self, callback):
|
|
|
|
self.states.append((self.do_calc_position, (callback,)))
|
2016-07-26 06:47:30 +03:00
|
|
|
def plan_axes_update(self, callback):
|
2016-11-18 19:27:16 +03:00
|
|
|
self.states.append((callback, (self,)))
|
2016-05-25 18:37:40 +03:00
|
|
|
def check_busy(self, eventtime):
|
2016-09-22 18:09:20 +03:00
|
|
|
self.eventtime = eventtime
|
2016-05-25 18:37:40 +03:00
|
|
|
while self.states:
|
|
|
|
first = self.states[0]
|
2016-11-18 21:03:40 +03:00
|
|
|
try:
|
|
|
|
ret = first[0](*first[1])
|
|
|
|
except EndstopError, e:
|
|
|
|
self.toolhead.motor_off()
|
|
|
|
raise
|
2016-05-25 18:37:40 +03:00
|
|
|
if ret:
|
|
|
|
return True
|
|
|
|
self.states.pop(0)
|
|
|
|
return False
|
|
|
|
|
2016-07-26 06:47:30 +03:00
|
|
|
def fill_coord(self, coord):
|
|
|
|
# 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-11-18 21:39:48 +03:00
|
|
|
def do_retract(self, newpos, steppers, speed):
|
2016-07-26 06:47:30 +03:00
|
|
|
self.toolhead.move(self.fill_coord(newpos), speed)
|
2016-05-25 18:37:40 +03:00
|
|
|
return False
|
2016-07-26 06:47:30 +03:00
|
|
|
def do_home(self, forcepos, movepos, steppers, speed):
|
|
|
|
# Alter kinematics class to think printer is at forcepos
|
|
|
|
self.toolhead.set_position(self.fill_coord(forcepos))
|
2016-05-25 18:37:40 +03:00
|
|
|
# Start homing and issue move
|
2016-07-07 21:23:48 +03:00
|
|
|
print_time = self.toolhead.get_last_move_time()
|
2016-07-26 06:47:30 +03:00
|
|
|
for s in steppers:
|
2016-08-24 23:42:25 +03:00
|
|
|
es = s.enable_endstop_checking(print_time, s.step_dist / speed)
|
2016-11-28 19:23:26 +03:00
|
|
|
self.endstops.append((s, es, s.mcu_stepper.get_mcu_position()))
|
2016-07-26 06:47:30 +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()
|
2016-07-07 21:23:48 +03:00
|
|
|
self.toolhead.reset_print_time()
|
2016-11-28 19:23:26 +03:00
|
|
|
for s, es, last_pos in self.endstops:
|
2016-11-18 21:03:40 +03:00
|
|
|
es.home_finalize(es.print_to_mcu_time(move_end_print_time))
|
2016-05-25 18:37:40 +03:00
|
|
|
return False
|
2016-11-28 19:23:26 +03:00
|
|
|
def do_wait_endstop(self, verify_retract):
|
2016-07-26 06:47:30 +03:00
|
|
|
# Check if endstops have triggered
|
2016-11-18 21:39:48 +03:00
|
|
|
while self.endstops:
|
2016-11-28 19:23:26 +03:00
|
|
|
stepper, es, last_pos = self.endstops[0]
|
2016-11-18 21:03:40 +03:00
|
|
|
try:
|
|
|
|
if es.check_busy(self.eventtime):
|
|
|
|
return True
|
2016-11-30 02:27:48 +03:00
|
|
|
except mcu.error, e:
|
2016-11-18 21:03:40 +03:00
|
|
|
raise EndstopError("Failed to home stepper %s: %s" % (
|
2016-11-28 19:23:26 +03:00
|
|
|
stepper.name, str(e)))
|
|
|
|
post_home_pos = stepper.mcu_stepper.get_mcu_position()
|
|
|
|
if verify_retract and last_pos == post_home_pos:
|
2016-11-18 21:39:48 +03:00
|
|
|
raise EndstopError("Endstop %s still triggered after retract" % (
|
2016-11-28 19:23:26 +03:00
|
|
|
stepper.name,))
|
2016-11-18 21:39:48 +03:00
|
|
|
self.endstops.pop(0)
|
2016-05-25 18:37:40 +03:00
|
|
|
return False
|
2016-11-18 19:49:05 +03:00
|
|
|
def do_calc_position(self, callback):
|
|
|
|
self.toolhead.set_position(self.fill_coord(callback(self)))
|
2016-09-22 18:09:20 +03:00
|
|
|
|
2016-11-18 01:24:03 +03:00
|
|
|
# Helper code for querying and reporting the status of the endstops
|
2016-09-22 18:09:20 +03:00
|
|
|
class QueryEndstops:
|
2016-11-18 01:24:03 +03:00
|
|
|
def __init__(self, print_time, respond_cb):
|
|
|
|
self.print_time = print_time
|
|
|
|
self.respond_cb = respond_cb
|
2016-09-22 18:09:20 +03:00
|
|
|
self.endstops = []
|
|
|
|
self.busy = []
|
2016-11-18 01:24:03 +03:00
|
|
|
def set_steppers(self, steppers):
|
|
|
|
for stepper in steppers:
|
|
|
|
es = stepper.query_endstop(self.print_time)
|
2016-09-22 18:09:20 +03:00
|
|
|
if es is None:
|
|
|
|
continue
|
2016-11-18 01:24:03 +03:00
|
|
|
self.endstops.append((stepper.name, es))
|
2016-11-18 21:03:40 +03:00
|
|
|
self.busy.append((stepper.name, es))
|
2016-09-22 18:09:20 +03:00
|
|
|
def check_busy(self, eventtime):
|
2016-11-18 01:24:03 +03:00
|
|
|
# Check if all endstop queries have been received
|
2016-09-22 18:09:20 +03:00
|
|
|
while self.busy:
|
2016-11-18 21:03:40 +03:00
|
|
|
try:
|
|
|
|
if self.busy[0][1].check_busy(eventtime):
|
|
|
|
return True
|
2016-11-30 02:27:48 +03:00
|
|
|
except mcu.error, e:
|
2016-11-18 21:03:40 +03:00
|
|
|
raise EndstopError("Failed to query endstop %s: %s" % (
|
|
|
|
self.busy[0][0], str(e)))
|
2016-09-22 18:09:20 +03:00
|
|
|
self.busy.pop(0)
|
2016-11-18 01:24:03 +03:00
|
|
|
# All responses received - report status
|
2016-09-22 18:09:20 +03:00
|
|
|
msgs = []
|
|
|
|
for name, es in self.endstops:
|
|
|
|
msg = "open"
|
|
|
|
if es.get_last_triggered():
|
|
|
|
msg = "TRIGGERED"
|
|
|
|
msgs.append("%s:%s" % (name, msg))
|
2016-11-18 01:24:03 +03:00
|
|
|
self.respond_cb(" ".join(msgs))
|
|
|
|
return False
|
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]))
|