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
|
|
|
|
|
|
|
|
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-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-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)))
|
|
|
|
self.states.append((self.do_wait_endstop, ()))
|
|
|
|
def plan_move(self, newpos, speed):
|
|
|
|
self.states.append((self.do_move, (newpos, speed)))
|
|
|
|
def plan_axes_update(self, callback):
|
|
|
|
self.states.append((callback, (self.changed_axes,)))
|
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]
|
|
|
|
ret = first[0](*first[1])
|
|
|
|
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
|
|
|
|
thcoord = self.toolhead.get_position()
|
|
|
|
coord = list(coord)
|
|
|
|
for i in range(len(coord)):
|
|
|
|
if coord[i] is None:
|
|
|
|
coord[i] = thcoord[i]
|
|
|
|
return coord
|
|
|
|
def do_move(self, newpos, speed):
|
|
|
|
# Issue a move command
|
|
|
|
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-05-25 18:37:40 +03:00
|
|
|
self.endstops.append(es)
|
2016-07-26 06:47:30 +03:00
|
|
|
self.toolhead.move(self.fill_coord(movepos), speed)
|
2016-07-07 21:23:48 +03:00
|
|
|
self.toolhead.reset_print_time()
|
2016-05-25 18:37:40 +03:00
|
|
|
for es in self.endstops:
|
|
|
|
es.home_finalize()
|
|
|
|
return False
|
2016-07-26 06:47:30 +03:00
|
|
|
def do_wait_endstop(self):
|
|
|
|
# Check if endstops have triggered
|
2016-05-25 18:37:40 +03:00
|
|
|
for es in self.endstops:
|
2016-09-22 18:09:20 +03:00
|
|
|
if es.check_busy(self.eventtime):
|
2016-05-25 18:37:40 +03:00
|
|
|
return True
|
|
|
|
# Finished
|
|
|
|
del self.endstops[:]
|
|
|
|
return False
|
2016-09-22 18:09:20 +03:00
|
|
|
|
|
|
|
class QueryEndstops:
|
|
|
|
def __init__(self, names, steppers):
|
|
|
|
self.endstops = []
|
|
|
|
self.busy = []
|
|
|
|
for name, stepper in zip(names, steppers):
|
|
|
|
es = stepper.query_endstop()
|
|
|
|
if es is None:
|
|
|
|
continue
|
|
|
|
self.endstops.append((name, es))
|
|
|
|
self.busy.append(es)
|
|
|
|
def check_busy(self, eventtime):
|
|
|
|
while self.busy:
|
|
|
|
busy = self.busy[0].check_busy(eventtime)
|
|
|
|
if busy:
|
|
|
|
return True
|
|
|
|
self.busy.pop(0)
|
|
|
|
return False
|
|
|
|
def get_msg(self):
|
|
|
|
msgs = []
|
|
|
|
for name, es in self.endstops:
|
|
|
|
msg = "open"
|
|
|
|
if es.get_last_triggered():
|
|
|
|
msg = "TRIGGERED"
|
|
|
|
msgs.append("%s:%s" % (name, msg))
|
|
|
|
return " ".join(msgs)
|
2016-09-30 23:00:32 +03:00
|
|
|
|
|
|
|
class EndstopError(Exception):
|
|
|
|
def __init__(self, pos, msg="Move out of range"):
|
|
|
|
self.pos = pos
|
|
|
|
self.msg = msg
|
|
|
|
def __str__(self):
|
|
|
|
return "%s: %.3f %.3f %.3f [%.3f]" % (
|
|
|
|
self.msg, self.pos[0], self.pos[1], self.pos[2], self.pos[3])
|