2016-05-25 17:37:40 +02: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 19:03:40 +01:00
|
|
|
import mcu
|
2016-05-25 17:37:40 +02:00
|
|
|
|
|
|
|
class Homing:
|
2016-07-26 05:47:30 +02:00
|
|
|
def __init__(self, toolhead, changed_axes):
|
2016-07-07 20:23:48 +02:00
|
|
|
self.toolhead = toolhead
|
2016-07-26 05:47:30 +02:00
|
|
|
self.changed_axes = changed_axes
|
2016-05-25 17:37:40 +02:00
|
|
|
|
2016-09-22 17:09:20 +02:00
|
|
|
self.eventtime = 0.
|
2016-05-25 17:37:40 +02:00
|
|
|
self.states = []
|
|
|
|
self.endstops = []
|
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-07-26 05:47:30 +02:00
|
|
|
def plan_home(self, forcepos, movepos, steppers, speed):
|
|
|
|
self.states.append((self.do_home, (forcepos, movepos, steppers, speed)))
|
|
|
|
self.states.append((self.do_wait_endstop, ()))
|
|
|
|
def plan_move(self, newpos, speed):
|
|
|
|
self.states.append((self.do_move, (newpos, speed)))
|
2016-11-18 17:49:05 +01:00
|
|
|
def plan_calc_position(self, callback):
|
|
|
|
self.states.append((self.do_calc_position, (callback,)))
|
2016-07-26 05:47:30 +02:00
|
|
|
def plan_axes_update(self, callback):
|
2016-11-18 17:27:16 +01:00
|
|
|
self.states.append((callback, (self,)))
|
2016-05-25 17:37:40 +02:00
|
|
|
def check_busy(self, eventtime):
|
2016-09-22 17:09:20 +02:00
|
|
|
self.eventtime = eventtime
|
2016-05-25 17:37:40 +02:00
|
|
|
while self.states:
|
|
|
|
first = self.states[0]
|
2016-11-18 19:03:40 +01:00
|
|
|
try:
|
|
|
|
ret = first[0](*first[1])
|
|
|
|
except EndstopError, e:
|
|
|
|
self.toolhead.motor_off()
|
|
|
|
raise
|
2016-05-25 17:37:40 +02:00
|
|
|
if ret:
|
|
|
|
return True
|
|
|
|
self.states.pop(0)
|
|
|
|
return False
|
|
|
|
|
2016-07-26 05:47:30 +02:00
|
|
|
def fill_coord(self, coord):
|
|
|
|
# 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-07-26 05:47:30 +02:00
|
|
|
def do_move(self, newpos, speed):
|
|
|
|
# Issue a move command
|
|
|
|
self.toolhead.move(self.fill_coord(newpos), speed)
|
2016-05-25 17:37:40 +02:00
|
|
|
return False
|
2016-07-26 05:47:30 +02: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 17:37:40 +02:00
|
|
|
# Start homing and issue move
|
2016-07-07 20:23:48 +02:00
|
|
|
print_time = self.toolhead.get_last_move_time()
|
2016-07-26 05:47:30 +02:00
|
|
|
for s in steppers:
|
2016-08-24 22:42:25 +02:00
|
|
|
es = s.enable_endstop_checking(print_time, s.step_dist / speed)
|
2016-11-18 19:03:40 +01:00
|
|
|
self.endstops.append((s.name, es))
|
2016-07-26 05:47:30 +02:00
|
|
|
self.toolhead.move(self.fill_coord(movepos), speed)
|
2016-11-18 19:03:40 +01:00
|
|
|
move_end_print_time = self.toolhead.get_last_move_time()
|
2016-07-07 20:23:48 +02:00
|
|
|
self.toolhead.reset_print_time()
|
2016-11-18 19:03:40 +01:00
|
|
|
for name, es in self.endstops:
|
|
|
|
es.home_finalize(es.print_to_mcu_time(move_end_print_time))
|
2016-05-25 17:37:40 +02:00
|
|
|
return False
|
2016-07-26 05:47:30 +02:00
|
|
|
def do_wait_endstop(self):
|
|
|
|
# Check if endstops have triggered
|
2016-11-18 19:03:40 +01:00
|
|
|
for name, es in self.endstops:
|
|
|
|
try:
|
|
|
|
if es.check_busy(self.eventtime):
|
|
|
|
return True
|
|
|
|
except mcu.MCUError, e:
|
|
|
|
raise EndstopError("Failed to home stepper %s: %s" % (
|
|
|
|
name, str(e)))
|
2016-05-25 17:37:40 +02:00
|
|
|
# Finished
|
|
|
|
del self.endstops[:]
|
|
|
|
return False
|
2016-11-18 17:49:05 +01:00
|
|
|
def do_calc_position(self, callback):
|
|
|
|
self.toolhead.set_position(self.fill_coord(callback(self)))
|
2016-09-22 17:09:20 +02:00
|
|
|
|
2016-11-17 23:24:03 +01:00
|
|
|
# Helper code for querying and reporting the status of the endstops
|
2016-09-22 17:09:20 +02:00
|
|
|
class QueryEndstops:
|
2016-11-17 23:24:03 +01:00
|
|
|
def __init__(self, print_time, respond_cb):
|
|
|
|
self.print_time = print_time
|
|
|
|
self.respond_cb = respond_cb
|
2016-09-22 17:09:20 +02:00
|
|
|
self.endstops = []
|
|
|
|
self.busy = []
|
2016-11-17 23:24:03 +01:00
|
|
|
def set_steppers(self, steppers):
|
|
|
|
for stepper in steppers:
|
|
|
|
es = stepper.query_endstop(self.print_time)
|
2016-09-22 17:09:20 +02:00
|
|
|
if es is None:
|
|
|
|
continue
|
2016-11-17 23:24:03 +01:00
|
|
|
self.endstops.append((stepper.name, es))
|
2016-11-18 19:03:40 +01:00
|
|
|
self.busy.append((stepper.name, es))
|
2016-09-22 17:09:20 +02:00
|
|
|
def check_busy(self, eventtime):
|
2016-11-17 23:24:03 +01:00
|
|
|
# Check if all endstop queries have been received
|
2016-09-22 17:09:20 +02:00
|
|
|
while self.busy:
|
2016-11-18 19:03:40 +01:00
|
|
|
try:
|
|
|
|
if self.busy[0][1].check_busy(eventtime):
|
|
|
|
return True
|
|
|
|
except mcu.MCUError, e:
|
|
|
|
raise EndstopError("Failed to query endstop %s: %s" % (
|
|
|
|
self.busy[0][0], str(e)))
|
2016-09-22 17:09:20 +02:00
|
|
|
self.busy.pop(0)
|
2016-11-17 23:24:03 +01:00
|
|
|
# All responses received - report status
|
2016-09-22 17:09:20 +02: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-17 23:24:03 +01:00
|
|
|
self.respond_cb(" ".join(msgs))
|
|
|
|
return False
|
2016-09-30 22:00:32 +02:00
|
|
|
|
|
|
|
class EndstopError(Exception):
|
2016-11-18 18:42:39 +01:00
|
|
|
def __init__(self, msg="Endstop error"):
|
|
|
|
self._msg = msg
|
2016-09-30 22:00:32 +02:00
|
|
|
def __str__(self):
|
2016-11-18 18:42:39 +01:00
|
|
|
return self._msg
|
|
|
|
|
|
|
|
def EndstopMoveError(pos, msg="Move out of range"):
|
|
|
|
return EndstopError("%s: %.3f %.3f %.3f [%.3f]" % (
|
|
|
|
msg, pos[0], pos[1], pos[2], pos[3]))
|