homing: Move low-level coordinate manipulation to kinematic class

Rework the code so that the kinematic class (currently just
cartesian.py) has more control over the homing process.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2016-07-25 23:47:30 -04:00
parent e9c03f2e4a
commit 92f81d51f4
4 changed files with 70 additions and 64 deletions

View File

@ -33,11 +33,34 @@ class CartKinematics:
accel_factor = min([self.steppers[i].max_accel / abs(axes_d[i]) accel_factor = min([self.steppers[i].max_accel / abs(axes_d[i])
for i in StepList if axes_d[i]]) for i in StepList if axes_d[i]])
return velocity_factor * move_d, accel_factor * move_d return velocity_factor * move_d, accel_factor * move_d
def home(self, toolhead, axis): def home(self, toolhead, axes):
# Each axis is homed independently and in order # Each axis is homed independently and in order
homing_state = homing.Homing(toolhead, self.steppers) # XXX homing_state = homing.Homing(toolhead, axes)
for a in axis: for axis in axes:
homing_state.plan_home(a) s = self.steppers[axis]
# Determine moves
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
homepos = [None, None, None, None]
homepos[axis] = s.position_endstop
coord = [None, None, None, None]
coord[axis] = pos
homing_state.plan_home(list(coord), homepos, [s], s.homing_speed)
# Retract
coord[axis] = rpos
homing_state.plan_move(list(coord), s.homing_speed)
# Home again
coord[axis] = r2pos
homing_state.plan_home(list(coord), homepos, [s], s.homing_speed/2.0)
return homing_state return homing_state
def motor_off(self, move_time): def motor_off(self, move_time):
for stepper in self.steppers: for stepper in self.steppers:

View File

@ -227,19 +227,19 @@ class GCodeParser:
pass pass
def cmd_G28(self, params): def cmd_G28(self, params):
# Move to origin # Move to origin
axis = [] axes = []
for a in 'XYZ': for axis in 'XYZ':
if a in params: if axis in params:
axis.append(self.axis2pos[a]) axes.append(self.axis2pos[axis])
if not axis: if not axes:
axis = [0, 1, 2] axes = [0, 1, 2]
busy_handler = self.toolhead.home(axis) busy_handler = self.toolhead.home(axes)
def axis_update(axis): def axes_update(axes):
newpos = self.toolhead.get_position() newpos = self.toolhead.get_position()
for a in axis: for axis in axes:
self.last_position[a] = newpos[a] self.last_position[axis] = newpos[axis]
self.base_position[a] = -self.homing_add[a] self.base_position[axis] = -self.homing_add[axis]
busy_handler.plan_axis_update(axis_update) busy_handler.plan_axes_update(axes_update)
self.set_busy(busy_handler) self.set_busy(busy_handler)
def cmd_G90(self, params): def cmd_G90(self, params):
# Use absolute coordinates # Use absolute coordinates

View File

@ -7,36 +7,19 @@
import logging import logging
class Homing: class Homing:
def __init__(self, toolhead, steppers): def __init__(self, toolhead, changed_axes):
self.toolhead = toolhead self.toolhead = toolhead
self.steppers = steppers self.changed_axes = changed_axes
self.states = [] self.states = []
self.endstops = [] self.endstops = []
self.changed_axis = [] def plan_home(self, forcepos, movepos, steppers, speed):
def plan_home(self, axis, precise=False): self.states.append((self.do_home, (forcepos, movepos, steppers, speed)))
s = self.steppers[axis] self.states.append((self.do_wait_endstop, ()))
state = self.states def plan_move(self, newpos, speed):
self.changed_axis.append(axis) self.states.append((self.do_move, (newpos, speed)))
speed = s.homing_speed def plan_axes_update(self, callback):
if s.homing_positive_dir: self.states.append((callback, (self.changed_axes,)))
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): def check_busy(self, eventtime):
while self.states: while self.states:
first = self.states[0] first = self.states[0]
@ -46,34 +29,34 @@ class Homing:
self.states.pop(0) self.states.pop(0)
return False return False
def do_move(self, axis_pos, speed): def fill_coord(self, coord):
# Issue a move command to axis_pos # Fill in any None entries in 'coord' with current toolhead position
newpos = self.toolhead.get_position() thcoord = self.toolhead.get_position()
for axis, pos in axis_pos.items(): coord = list(coord)
newpos[axis] = pos for i in range(len(coord)):
self.toolhead.move(newpos, speed) 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)
return False return False
def do_home(self, axis_pos, speed): def do_home(self, forcepos, movepos, steppers, speed):
# Alter kinematics class to think printer is at axis_pos # Alter kinematics class to think printer is at forcepos
newpos = self.toolhead.get_position() self.toolhead.set_position(self.fill_coord(forcepos))
forcepos = list(newpos)
for axis, pos in axis_pos.items():
newpos[axis] = self.steppers[axis].position_endstop
forcepos[axis] = pos
self.toolhead.set_position(forcepos)
# Start homing and issue move # Start homing and issue move
print_time = self.toolhead.get_last_move_time() print_time = self.toolhead.get_last_move_time()
for axis in axis_pos: for s in steppers:
hz = speed * self.steppers[axis].inv_step_dist hz = speed * s.inv_step_dist
es = self.steppers[axis].enable_endstop_checking(print_time, hz) es = s.enable_endstop_checking(print_time, hz)
self.endstops.append(es) self.endstops.append(es)
self.toolhead.move(newpos, speed) self.toolhead.move(self.fill_coord(movepos), speed)
self.toolhead.reset_print_time() self.toolhead.reset_print_time()
for es in self.endstops: for es in self.endstops:
es.home_finalize() es.home_finalize()
return False return False
def do_wait_endstop(self, axis_wait): def do_wait_endstop(self):
# Check if axis_wait endstops have triggered # Check if endstops have triggered
for es in self.endstops: for es in self.endstops:
if es.is_homing(): if es.is_homing():
return True return True

View File

@ -253,8 +253,8 @@ class ToolHead:
speed = min(speed, self.max_xy_speed) speed = min(speed, self.max_xy_speed)
move = Move(self, newpos, move_d, axes_d, speed, self.max_xy_accel) move = Move(self, newpos, move_d, axes_d, speed, self.max_xy_accel)
self.move_queue.add_move(move) self.move_queue.add_move(move)
def home(self, axis): def home(self, axes):
return self.kin.home(self, axis) return self.kin.home(self, axes)
def dwell(self, delay): def dwell(self, delay):
self.get_last_move_time() self.get_last_move_time()
self.update_move_time(delay) self.update_move_time(delay)