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-07 21:23:48 +03:00
|
|
|
def __init__(self, toolhead, steppers):
|
|
|
|
self.toolhead = toolhead
|
2016-05-25 18:37:40 +03:00
|
|
|
self.steppers = steppers
|
|
|
|
|
|
|
|
self.states = []
|
|
|
|
self.endstops = []
|
|
|
|
self.changed_axis = []
|
|
|
|
def plan_home(self, axis, precise=False):
|
|
|
|
s = self.steppers[axis]
|
|
|
|
state = self.states
|
|
|
|
self.changed_axis.append(axis)
|
|
|
|
speed = s.homing_speed
|
|
|
|
if s.homing_positive_dir:
|
|
|
|
pos = s.position_endstop + 1.5*(s.position_min - s.position_endstop)
|
|
|
|
rpos = s.position_endstop - s.homing_retract_dist
|
|
|
|
r2pos = rpos - s.homing_retract_dist
|
|
|
|
else:
|
|
|
|
pos = s.position_endstop + 1.5*(s.position_max - s.position_endstop)
|
|
|
|
rpos = s.position_endstop + s.homing_retract_dist
|
|
|
|
r2pos = rpos + s.homing_retract_dist
|
|
|
|
# Initial homing
|
|
|
|
state.append((self.do_home, ({axis: pos}, speed)))
|
|
|
|
state.append((self.do_wait_endstop, ({axis: 1},)))
|
|
|
|
# Retract
|
|
|
|
state.append((self.do_move, ({axis: rpos}, speed)))
|
|
|
|
# Home again
|
|
|
|
state.append((self.do_home, ({axis: r2pos}, speed/2.0)))
|
|
|
|
state.append((self.do_wait_endstop, ({axis: 1},)))
|
|
|
|
def plan_axis_update(self, callback):
|
|
|
|
self.states.append((callback, (self.changed_axis,)))
|
|
|
|
def check_busy(self, eventtime):
|
|
|
|
while self.states:
|
|
|
|
first = self.states[0]
|
|
|
|
ret = first[0](*first[1])
|
|
|
|
if ret:
|
|
|
|
return True
|
|
|
|
self.states.pop(0)
|
|
|
|
return False
|
|
|
|
|
|
|
|
def do_move(self, axis_pos, speed):
|
|
|
|
# Issue a move command to axis_pos
|
2016-07-07 21:23:48 +03:00
|
|
|
newpos = self.toolhead.get_position()
|
2016-05-25 18:37:40 +03:00
|
|
|
for axis, pos in axis_pos.items():
|
|
|
|
newpos[axis] = pos
|
2016-07-07 21:23:48 +03:00
|
|
|
self.toolhead.move(newpos, speed)
|
2016-05-25 18:37:40 +03:00
|
|
|
return False
|
|
|
|
def do_home(self, axis_pos, speed):
|
|
|
|
# Alter kinematics class to think printer is at axis_pos
|
2016-07-07 21:23:48 +03:00
|
|
|
newpos = self.toolhead.get_position()
|
2016-05-25 18:37:40 +03:00
|
|
|
forcepos = list(newpos)
|
|
|
|
for axis, pos in axis_pos.items():
|
|
|
|
newpos[axis] = self.steppers[axis].position_endstop
|
|
|
|
forcepos[axis] = pos
|
2016-07-07 21:23:48 +03:00
|
|
|
self.toolhead.set_position(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-05-25 18:37:40 +03:00
|
|
|
for axis in axis_pos:
|
|
|
|
hz = speed * self.steppers[axis].inv_step_dist
|
|
|
|
es = self.steppers[axis].enable_endstop_checking(print_time, hz)
|
|
|
|
self.endstops.append(es)
|
2016-07-07 21:23:48 +03:00
|
|
|
self.toolhead.move(newpos, speed)
|
|
|
|
self.toolhead.reset_print_time()
|
2016-05-25 18:37:40 +03:00
|
|
|
for es in self.endstops:
|
|
|
|
es.home_finalize()
|
|
|
|
return False
|
|
|
|
def do_wait_endstop(self, axis_wait):
|
|
|
|
# Check if axis_wait endstops have triggered
|
|
|
|
for es in self.endstops:
|
|
|
|
if es.is_homing():
|
|
|
|
return True
|
|
|
|
# Finished
|
|
|
|
del self.endstops[:]
|
|
|
|
return False
|