stepper: Replace PrinterHomingStepper with PrinterRail

Update the code to use the term "rail" when dealing with a motor
controlled "axis".  A rail has a series of steppers and endstops that
control that motor controlled "axis".

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2018-06-22 11:44:25 -04:00
parent 93d0526a77
commit 0791c69499
7 changed files with 200 additions and 194 deletions

View File

@ -1,6 +1,6 @@
# Code for handling the kinematics of cartesian robots
#
# Copyright (C) 2016 Kevin O'Connor <kevin@koconnor.net>
# Copyright (C) 2016-2018 Kevin O'Connor <kevin@koconnor.net>
#
# This file may be distributed under the terms of the GNU GPLv3 license.
import logging
@ -11,8 +11,7 @@ StepList = (0, 1, 2)
class CartKinematics:
def __init__(self, toolhead, config):
self.printer = config.get_printer()
self.steppers = [stepper.LookupMultiHomingStepper(
config.getsection('stepper_' + n))
self.rails = [stepper.LookupMultiRail(config.getsection('stepper_' + n))
for n in ['x', 'y', 'z']]
max_velocity, max_accel = toolhead.get_max_velocity()
self.max_z_velocity = config.getfloat(
@ -25,46 +24,45 @@ class CartKinematics:
ffi_main, ffi_lib = chelper.get_ffi()
self.cmove = ffi_main.gc(ffi_lib.move_alloc(), ffi_lib.free)
self.move_fill = ffi_lib.move_fill
for a, s in zip('xyz', self.steppers):
s.setup_cartesian_itersolve(a)
for axis, rail in zip('xyz', self.rails):
rail.setup_cartesian_itersolve(axis)
# Setup stepper max halt velocity
max_halt_velocity = toolhead.get_max_axis_halt()
self.steppers[0].set_max_jerk(max_halt_velocity, max_accel)
self.steppers[1].set_max_jerk(max_halt_velocity, max_accel)
self.steppers[2].set_max_jerk(
self.rails[0].set_max_jerk(max_halt_velocity, max_accel)
self.rails[1].set_max_jerk(max_halt_velocity, max_accel)
self.rails[2].set_max_jerk(
min(max_halt_velocity, self.max_z_velocity), max_accel)
# Check for dual carriage support
self.dual_carriage_axis = None
self.dual_carriage_steppers = []
self.dual_carriage_rails = []
if config.has_section('dual_carriage'):
dc_config = config.getsection('dual_carriage')
dc_axis = dc_config.getchoice('axis', {'x': 'x', 'y': 'y'})
self.dual_carriage_axis = {'x': 0, 'y': 1}[dc_axis]
dc_stepper = stepper.LookupMultiHomingStepper(dc_config)
dc_stepper.setup_cartesian_itersolve(dc_axis)
dc_stepper.set_max_jerk(max_halt_velocity, max_accel)
self.dual_carriage_steppers = [
self.steppers[self.dual_carriage_axis], dc_stepper]
dc_rail = stepper.LookupMultiRail(dc_config)
dc_rail.setup_cartesian_itersolve(dc_axis)
dc_rail.set_max_jerk(max_halt_velocity, max_accel)
self.dual_carriage_rails = [
self.rails[self.dual_carriage_axis], dc_rail]
self.printer.lookup_object('gcode').register_command(
'SET_DUAL_CARRIAGE', self.cmd_SET_DUAL_CARRIAGE,
desc=self.cmd_SET_DUAL_CARRIAGE_help)
def get_steppers(self, flags=""):
def get_rails(self, flags=""):
if flags == "Z":
return [self.steppers[2]]
return list(self.steppers)
return [self.rails[2]]
return list(self.rails)
def get_position(self):
return [s.get_commanded_position() for s in self.steppers]
return [rail.get_commanded_position() for rail in self.rails]
def set_position(self, newpos, homing_axes):
for i in StepList:
s = self.steppers[i]
s.set_position(newpos[i])
rail = self.rails[i]
rail.set_position(newpos[i])
if i in homing_axes:
self.limits[i] = s.get_range()
def _home_axis(self, homing_state, axis, stepper):
s = stepper
self.limits[i] = rail.get_range()
def _home_axis(self, homing_state, axis, rail):
# Determine moves
position_min, position_max = s.get_range()
hi = s.get_homing_info()
position_min, position_max = rail.get_range()
hi = rail.get_homing_info()
if hi.positive_dir:
pos = hi.position_endstop - 1.5*(hi.position_endstop - position_min)
rpos = hi.position_endstop - hi.retract_dist
@ -81,43 +79,43 @@ class CartKinematics:
homepos[axis] = hi.position_endstop
coord = [None, None, None, None]
coord[axis] = pos
homing_state.home(coord, homepos, s.get_endstops(), homing_speed)
homing_state.home(coord, homepos, rail.get_endstops(), homing_speed)
# Retract
coord[axis] = rpos
homing_state.retract(coord, homing_speed)
# Home again
coord[axis] = r2pos
homing_state.home(coord, homepos, s.get_endstops(),
homing_state.home(coord, homepos, rail.get_endstops(),
homing_speed/2.0, second_home=True)
# Set final homed position
coord[axis] = hi.position_endstop + s.get_homed_offset()
coord[axis] = hi.position_endstop + rail.get_homed_offset()
homing_state.set_homed_position(coord)
def home(self, homing_state):
# Each axis is homed independently and in order
for axis in homing_state.get_axes():
if axis == self.dual_carriage_axis:
dc1, dc2 = self.dual_carriage_steppers
altc = self.steppers[axis] == dc2
dc1, dc2 = self.dual_carriage_rails
altc = self.rails[axis] == dc2
self._activate_carriage(0)
self._home_axis(homing_state, axis, dc1)
self._activate_carriage(1)
self._home_axis(homing_state, axis, dc2)
self._activate_carriage(altc)
else:
self._home_axis(homing_state, axis, self.steppers[axis])
self._home_axis(homing_state, axis, self.rails[axis])
def motor_off(self, print_time):
self.limits = [(1.0, -1.0)] * 3
for stepper in self.steppers:
stepper.motor_enable(print_time, 0)
for stepper in self.dual_carriage_steppers:
stepper.motor_enable(print_time, 0)
for rail in self.rails:
rail.motor_enable(print_time, 0)
for rail in self.dual_carriage_rails:
rail.motor_enable(print_time, 0)
self.need_motor_enable = True
def _check_motor_enable(self, print_time, move):
need_motor_enable = False
for i in StepList:
if move.axes_d[i]:
self.steppers[i].motor_enable(print_time, 1)
need_motor_enable |= not self.steppers[i].is_motor_enabled()
self.rails[i].motor_enable(print_time, 1)
need_motor_enable |= not self.rails[i].is_motor_enabled()
self.need_motor_enable = need_motor_enable
def _check_endstops(self, move):
end_pos = move.end_pos
@ -154,18 +152,18 @@ class CartKinematics:
move.start_v, move.cruise_v, move.accel)
for i in StepList:
if move.axes_d[i]:
self.steppers[i].step_itersolve(self.cmove)
self.rails[i].step_itersolve(self.cmove)
# Dual carriage support
def _activate_carriage(self, carriage):
toolhead = self.printer.lookup_object('toolhead')
toolhead.get_last_move_time()
dc_stepper = self.dual_carriage_steppers[carriage]
dc_rail = self.dual_carriage_rails[carriage]
dc_axis = self.dual_carriage_axis
self.steppers[dc_axis] = dc_stepper
self.rails[dc_axis] = dc_rail
extruder_pos = toolhead.get_position()[3]
toolhead.set_position(self.get_position() + [extruder_pos])
if self.limits[dc_axis][0] <= self.limits[dc_axis][1]:
self.limits[dc_axis] = dc_stepper.get_range()
self.limits[dc_axis] = dc_rail.get_range()
self.need_motor_enable = True
cmd_SET_DUAL_CARRIAGE_help = "Set which carriage is active"
def cmd_SET_DUAL_CARRIAGE(self, params):

View File

@ -10,12 +10,11 @@ StepList = (0, 1, 2)
class CoreXYKinematics:
def __init__(self, toolhead, config):
self.steppers = [
stepper.PrinterHomingStepper(config.getsection('stepper_x')),
stepper.PrinterHomingStepper(config.getsection('stepper_y')),
stepper.LookupMultiHomingStepper(config.getsection('stepper_z'))]
self.steppers[0].add_to_endstop(self.steppers[1].get_endstops()[0][0])
self.steppers[1].add_to_endstop(self.steppers[0].get_endstops()[0][0])
self.rails = [ stepper.PrinterRail(config.getsection('stepper_x')),
stepper.PrinterRail(config.getsection('stepper_y')),
stepper.LookupMultiRail(config.getsection('stepper_z')) ]
self.rails[0].add_to_endstop(self.rails[1].get_endstops()[0][0])
self.rails[1].add_to_endstop(self.rails[0].get_endstops()[0][0])
max_velocity, max_accel = toolhead.get_max_velocity()
self.max_z_velocity = config.getfloat(
'max_z_velocity', max_velocity, above=0., maxval=max_velocity)
@ -27,39 +26,39 @@ class CoreXYKinematics:
ffi_main, ffi_lib = chelper.get_ffi()
self.cmove = ffi_main.gc(ffi_lib.move_alloc(), ffi_lib.free)
self.move_fill = ffi_lib.move_fill
self.steppers[0].setup_itersolve(ffi_main.gc(
self.rails[0].setup_itersolve(ffi_main.gc(
ffi_lib.corexy_stepper_alloc('+'), ffi_lib.free))
self.steppers[1].setup_itersolve(ffi_main.gc(
self.rails[1].setup_itersolve(ffi_main.gc(
ffi_lib.corexy_stepper_alloc('-'), ffi_lib.free))
self.steppers[2].setup_cartesian_itersolve('z')
self.rails[2].setup_cartesian_itersolve('z')
# Setup stepper max halt velocity
max_halt_velocity = toolhead.get_max_axis_halt()
max_xy_halt_velocity = max_halt_velocity * math.sqrt(2.)
self.steppers[0].set_max_jerk(max_xy_halt_velocity, max_accel)
self.steppers[1].set_max_jerk(max_xy_halt_velocity, max_accel)
self.steppers[2].set_max_jerk(
self.rails[0].set_max_jerk(max_xy_halt_velocity, max_accel)
self.rails[1].set_max_jerk(max_xy_halt_velocity, max_accel)
self.rails[2].set_max_jerk(
min(max_halt_velocity, self.max_z_velocity), self.max_z_accel)
def get_steppers(self, flags=""):
def get_rails(self, flags=""):
if flags == "Z":
return [self.steppers[2]]
return list(self.steppers)
return [self.rails[2]]
return list(self.rails)
def get_position(self):
pos = [s.get_commanded_position() for s in self.steppers]
pos = [rail.get_commanded_position() for rail in self.rails]
return [0.5 * (pos[0] + pos[1]), 0.5 * (pos[0] - pos[1]), pos[2]]
def set_position(self, newpos, homing_axes):
pos = (newpos[0] + newpos[1], newpos[0] - newpos[1], newpos[2])
for i in StepList:
s = self.steppers[i]
s.set_position(pos[i])
rail = self.rails[i]
rail.set_position(pos[i])
if i in homing_axes:
self.limits[i] = s.get_range()
self.limits[i] = rail.get_range()
def home(self, homing_state):
# Each axis is homed independently and in order
for axis in homing_state.get_axes():
s = self.steppers[axis]
rail = self.rails[axis]
# Determine moves
position_min, position_max = s.get_range()
hi = s.get_homing_info()
position_min, position_max = rail.get_range()
hi = rail.get_homing_info()
if hi.positive_dir:
pos = hi.position_endstop - 1.5*(
hi.position_endstop - position_min)
@ -78,32 +77,32 @@ class CoreXYKinematics:
homepos[axis] = hi.position_endstop
coord = [None, None, None, None]
coord[axis] = pos
homing_state.home(coord, homepos, s.get_endstops(), homing_speed)
homing_state.home(coord, homepos, rail.get_endstops(), homing_speed)
# Retract
coord[axis] = rpos
homing_state.retract(coord, homing_speed)
# Home again
coord[axis] = r2pos
homing_state.home(coord, homepos, s.get_endstops(),
homing_state.home(coord, homepos, rail.get_endstops(),
homing_speed/2.0, second_home=True)
if axis == 2:
# Support endstop phase detection on Z axis
coord[axis] = hi.position_endstop + s.get_homed_offset()
coord[axis] = hi.position_endstop + rail.get_homed_offset()
homing_state.set_homed_position(coord)
def motor_off(self, print_time):
self.limits = [(1.0, -1.0)] * 3
for stepper in self.steppers:
stepper.motor_enable(print_time, 0)
for rail in self.rails:
rail.motor_enable(print_time, 0)
self.need_motor_enable = True
def _check_motor_enable(self, print_time, move):
if move.axes_d[0] or move.axes_d[1]:
self.steppers[0].motor_enable(print_time, 1)
self.steppers[1].motor_enable(print_time, 1)
self.rails[0].motor_enable(print_time, 1)
self.rails[1].motor_enable(print_time, 1)
if move.axes_d[2]:
self.steppers[2].motor_enable(print_time, 1)
self.rails[2].motor_enable(print_time, 1)
need_motor_enable = False
for i in StepList:
need_motor_enable |= not self.steppers[i].is_motor_enabled()
need_motor_enable |= not self.rails[i].is_motor_enabled()
self.need_motor_enable = need_motor_enable
def _check_endstops(self, move):
end_pos = move.end_pos
@ -140,9 +139,9 @@ class CoreXYKinematics:
move.start_pos[0], move.start_pos[1], move.start_pos[2],
axes_d[0], axes_d[1], axes_d[2],
move.start_v, move.cruise_v, move.accel)
stepper_a, stepper_b, stepper_z = self.steppers
rail_x, rail_y, rail_z = self.rails
if axes_d[0] or axes_d[1]:
stepper_a.step_itersolve(cmove)
stepper_b.step_itersolve(cmove)
rail_x.step_itersolve(cmove)
rail_y.step_itersolve(cmove)
if axes_d[2]:
stepper_z.step_itersolve(cmove)
rail_z.step_itersolve(cmove)

View File

@ -1,6 +1,6 @@
# Code for handling the kinematics of linear delta robots
#
# Copyright (C) 2016,2017 Kevin O'Connor <kevin@koconnor.net>
# Copyright (C) 2016-2018 Kevin O'Connor <kevin@koconnor.net>
#
# This file may be distributed under the terms of the GNU GPLv3 license.
import math, logging
@ -15,16 +15,16 @@ class DeltaKinematics:
def __init__(self, toolhead, config):
stepper_configs = [config.getsection('stepper_' + n)
for n in ['a', 'b', 'c']]
stepper_a = stepper.PrinterHomingStepper(
rail_a = stepper.PrinterRail(
stepper_configs[0], need_position_minmax = False)
a_endstop = stepper_a.get_homing_info().position_endstop
stepper_b = stepper.PrinterHomingStepper(
a_endstop = rail_a.get_homing_info().position_endstop
rail_b = stepper.PrinterRail(
stepper_configs[1], need_position_minmax = False,
default_position_endstop=a_endstop)
stepper_c = stepper.PrinterHomingStepper(
rail_c = stepper.PrinterRail(
stepper_configs[2], need_position_minmax = False,
default_position_endstop=a_endstop)
self.steppers = [stepper_a, stepper_b, stepper_c]
self.rails = [rail_a, rail_b, rail_c]
self.need_motor_enable = self.need_home = True
self.radius = radius = config.getfloat('delta_radius', above=0.)
arm_length_a = stepper_configs[0].getfloat('arm_length', above=radius)
@ -32,12 +32,12 @@ class DeltaKinematics:
sconfig.getfloat('arm_length', arm_length_a, above=radius)
for sconfig in stepper_configs]
self.arm2 = [arm**2 for arm in arm_lengths]
self.endstops = [(s.get_homing_info().position_endstop
self.endstops = [(rail.get_homing_info().position_endstop
+ math.sqrt(arm2 - radius**2))
for s, arm2 in zip(self.steppers, self.arm2)]
for rail, arm2 in zip(self.rails, self.arm2)]
self.limit_xy2 = -1.
self.max_z = min([s.get_homing_info().position_endstop
for s in self.steppers])
self.max_z = min([rail.get_homing_info().position_endstop
for rail in self.rails])
self.min_z = config.getfloat('minimum_z_position', 0, maxval=self.max_z)
self.limit_z = min([ep - arm
for ep, arm in zip(self.endstops, arm_lengths)])
@ -50,8 +50,8 @@ class DeltaKinematics:
'max_z_velocity', self.max_velocity,
above=0., maxval=self.max_velocity)
max_halt_velocity = toolhead.get_max_axis_halt()
for s in self.steppers:
s.set_max_jerk(max_halt_velocity, self.max_accel)
for rail in self.rails:
rail.set_max_jerk(max_halt_velocity, self.max_accel)
# Determine tower locations in cartesian space
self.angles = [sconfig.getfloat('angle', angle)
for sconfig, angle in zip(stepper_configs,
@ -63,13 +63,14 @@ class DeltaKinematics:
ffi_main, ffi_lib = chelper.get_ffi()
self.cmove = ffi_main.gc(ffi_lib.move_alloc(), ffi_lib.free)
self.move_fill = ffi_lib.move_fill
for s, a, t in zip(self.steppers, self.arm2, self.towers):
for r, a, t in zip(self.rails, self.arm2, self.towers):
sk = ffi_main.gc(ffi_lib.delta_stepper_alloc(a, t[0], t[1]),
ffi_lib.free)
s.setup_itersolve(sk)
r.setup_itersolve(sk)
# Find the point where an XY move could result in excessive
# tower movement
half_min_step_dist = min([s.get_step_dist() for s in self.steppers]) * .5
half_min_step_dist = min([r.get_steppers()[0].get_step_dist()
for r in self.rails]) * .5
min_arm_length = min(arm_lengths)
def ratio_to_dist(ratio):
return (ratio * math.sqrt(min_arm_length**2 / (ratio**2 + 1.)
@ -84,8 +85,8 @@ class DeltaKinematics:
% (math.sqrt(self.max_xy2), math.sqrt(self.slow_xy2),
math.sqrt(self.very_slow_xy2)))
self.set_position([0., 0., 0.], ())
def get_steppers(self, flags=""):
return list(self.steppers)
def get_rails(self, flags=""):
return list(self.rails)
def _cartesian_to_actuator(self, coord):
return [math.sqrt(self.arm2[i] - (self.towers[i][0] - coord[0])**2
- (self.towers[i][1] - coord[1])**2) + coord[2]
@ -93,21 +94,21 @@ class DeltaKinematics:
def _actuator_to_cartesian(self, pos):
return actuator_to_cartesian(self.towers, self.arm2, pos)
def get_position(self):
spos = [s.get_commanded_position() for s in self.steppers]
spos = [rail.get_commanded_position() for rail in self.rails]
return self._actuator_to_cartesian(spos)
def set_position(self, newpos, homing_axes):
pos = self._cartesian_to_actuator(newpos)
for i in StepList:
self.steppers[i].set_position(pos[i])
self.rails[i].set_position(pos[i])
self.limit_xy2 = -1.
if tuple(homing_axes) == StepList:
self.need_home = False
def home(self, homing_state):
# All axes are homed simultaneously
homing_state.set_axes([0, 1, 2])
endstops = [es for s in self.steppers for es in s.get_endstops()]
endstops = [es for rail in self.rails for es in rail.get_endstops()]
# Initial homing - assume homing speed same for all steppers
hi = self.steppers[0].get_homing_info()
hi = self.rails[0].get_homing_info()
homing_speed = min(hi.speed, self.max_z_velocity)
homepos = [0., 0., self.max_z, None]
coord = list(homepos)
@ -121,17 +122,17 @@ class DeltaKinematics:
homing_state.home(coord, homepos, endstops,
homing_speed/2.0, second_home=True)
# Set final homed position
spos = [ep + s.get_homed_offset()
for ep, s in zip(self.endstops, self.steppers)]
spos = [ep + rail.get_homed_offset()
for ep, rail in zip(self.endstops, self.rails)]
homing_state.set_homed_position(self._actuator_to_cartesian(spos))
def motor_off(self, print_time):
self.limit_xy2 = -1.
for stepper in self.steppers:
stepper.motor_enable(print_time, 0)
for rail in self.rails:
rail.motor_enable(print_time, 0)
self.need_motor_enable = self.need_home = True
def _check_motor_enable(self, print_time):
for i in StepList:
self.steppers[i].motor_enable(print_time, 1)
self.rails[i].motor_enable(print_time, 1)
self.need_motor_enable = False
def check_move(self, move):
end_pos = move.end_pos
@ -171,18 +172,19 @@ class DeltaKinematics:
move.start_pos[0], move.start_pos[1], move.start_pos[2],
move.axes_d[0], move.axes_d[1], move.axes_d[2],
move.start_v, move.cruise_v, move.accel)
for stepper in self.steppers:
stepper.step_itersolve(self.cmove)
for rail in self.rails:
rail.step_itersolve(self.cmove)
# Helper functions for DELTA_CALIBRATE script
def get_stable_position(self):
steppers = [rail.get_steppers()[0] for rail in self.rails]
return [int((ep - s.get_commanded_position()) / s.get_step_dist() + .5)
* s.get_step_dist()
for ep, s in zip(self.endstops, self.steppers)]
for ep, s in zip(self.endstops, steppers)]
def get_calibrate_params(self):
return {
'endstop_a': self.steppers[0].position_endstop,
'endstop_b': self.steppers[1].position_endstop,
'endstop_c': self.steppers[2].position_endstop,
'endstop_a': self.rails[0].position_endstop,
'endstop_b': self.rails[1].position_endstop,
'endstop_c': self.rails[2].position_endstop,
'angle_a': self.angles[0], 'angle_b': self.angles[1],
'angle_c': self.angles[2], 'radius': self.radius,
'arm_a': self.arm_lengths[0], 'arm_b': self.arm_lengths[1],

View File

@ -44,9 +44,9 @@ class PrinterProbe:
'QUERY_PROBE', self.cmd_QUERY_PROBE, desc=self.cmd_QUERY_PROBE_help)
def build_config(self):
toolhead = self.printer.lookup_object('toolhead')
z_steppers = toolhead.get_kinematics().get_steppers("Z")
for s in z_steppers:
for mcu_endstop, name in s.get_endstops():
z_rails = toolhead.get_kinematics().get_rails("Z")
for rail in z_rails:
for mcu_endstop, name in rail.get_endstops():
for mcu_stepper in mcu_endstop.get_steppers():
self.mcu_probe.add_stepper(mcu_stepper)
def setup_pin(self, pin_params):

View File

@ -32,8 +32,7 @@ class ZTilt:
def handle_connect(self):
kin = self.printer.lookup_object('toolhead').get_kinematics()
try:
z_stepper = kin.get_steppers('Z')[0]
z_steppers = [z_stepper] + z_stepper.extras
z_steppers = kin.get_rails('Z')[0].get_steppers()
except:
logging.exception("z_tilt stepper lookup")
raise self.printer.config_error(

View File

@ -118,13 +118,13 @@ class Homing:
def query_endstops(toolhead):
print_time = toolhead.get_last_move_time()
steppers = toolhead.get_kinematics().get_steppers()
rails = toolhead.get_kinematics().get_rails()
out = []
for s in steppers:
for mcu_endstop, name in s.get_endstops():
for rail in rails:
for mcu_endstop, name in rail.get_endstops():
mcu_endstop.query_endstop(print_time)
for s in steppers:
for mcu_endstop, name in s.get_endstops():
for rail in rails:
for mcu_endstop, name in rail.get_endstops():
out.append((name, mcu_endstop.query_endstop_wait()))
return out

View File

@ -6,6 +6,11 @@
import math, logging, collections
import homing, chelper
######################################################################
# Stepper enable pins
######################################################################
# Tracking of shared stepper enable pins
class StepperEnablePin:
def __init__(self, mcu_enable, enable_count=0):
@ -32,6 +37,11 @@ def lookup_enable_pin(ppins, pin):
pin_params['class'] = enable = StepperEnablePin(mcu_enable)
return enable
######################################################################
# Steppers
######################################################################
# Code storing the definitions for a stepper motor
class PrinterStepper:
def __init__(self, config):
@ -53,6 +63,7 @@ class PrinterStepper:
self.step_itersolve = self.mcu_stepper.step_itersolve
self.setup_itersolve = self.mcu_stepper.setup_itersolve
self.set_ignore_move = self.mcu_stepper.set_ignore_move
self.set_position = self.mcu_stepper.set_position
self.get_mcu_position = self.mcu_stepper.get_mcu_position
self.get_commanded_position = self.mcu_stepper.get_commanded_position
self.get_step_dist = self.mcu_stepper.get_step_dist
@ -75,8 +86,6 @@ class PrinterStepper:
2. * step_dist, max_halt_velocity, max_accel)
min_stop_interval = second_last_step_time - last_step_time
self.mcu_stepper.setup_min_stop_interval(min_stop_interval)
def set_position(self, pos):
self.mcu_stepper.set_position(pos)
def motor_enable(self, print_time, enable=0):
if self.need_motor_enable != (not enable):
self.enable.set_enable(print_time, enable)
@ -84,15 +93,29 @@ class PrinterStepper:
def is_motor_enabled(self):
return not self.need_motor_enable
# Support for stepper controlled linear axis with an endstop
class PrinterHomingStepper(PrinterStepper):
######################################################################
# Stepper controlled rails
######################################################################
# A motor control "rail" with one (or more) steppers and one (or more)
# endstops.
class PrinterRail:
def __init__(self, config, need_position_minmax=True,
default_position_endstop=None):
PrinterStepper.__init__(self, config)
# Endstop and its position
# Primary stepper
stepper = PrinterStepper(config)
self.steppers = [stepper]
self.name = stepper.get_name(short=True)
self.step_itersolve = stepper.step_itersolve
self.setup_itersolve = stepper.setup_itersolve
self.get_commanded_position = stepper.get_commanded_position
self.is_motor_enabled = stepper.is_motor_enabled
# Primary endstop and its position
ppins = config.get_printer().lookup_object('pins')
self.mcu_endstop = ppins.setup_pin('endstop', config.get('endstop_pin'))
self.add_to_endstop(self.mcu_endstop)
mcu_endstop = ppins.setup_pin('endstop', config.get('endstop_pin'))
self.endstops = [(mcu_endstop, self.name)]
stepper.add_to_endstop(mcu_endstop)
if default_position_endstop is None:
self.position_endstop = config.getfloat('position_endstop')
else:
@ -133,7 +156,7 @@ class PrinterHomingStepper(PrinterStepper):
'homing_endstop_accuracy', None, above=0.)
self.homing_endstop_accuracy = self.homing_endstop_phase = None
if self.homing_stepper_phases:
step_dist = self.get_step_dist()
self.homing_step_dist = step_dist = stepper.get_step_dist()
self.homing_endstop_phase = config.getint(
'homing_endstop_phase', None, minval=0
, maxval=self.homing_stepper_phases-1)
@ -149,7 +172,7 @@ class PrinterHomingStepper(PrinterStepper):
+ phase_offset)
if es_pos != self.position_endstop:
logging.info("Changing %s endstop position to %.3f"
" (from %.3f)", self.get_name(short=True),
" (from %.3f)", self.name,
es_pos, self.position_endstop)
self.position_endstop = es_pos
if endstop_accuracy is None:
@ -162,32 +185,17 @@ class PrinterHomingStepper(PrinterStepper):
endstop_accuracy / step_dist))
if self.homing_endstop_accuracy >= self.homing_stepper_phases // 2:
logging.info("Endstop for %s is not accurate enough for stepper"
" phase adjustment", self.get_name(short=True))
" phase adjustment", self.name)
self.homing_stepper_phases = None
if self.mcu_endstop.get_mcu().is_fileoutput():
if mcu_endstop.get_mcu().is_fileoutput():
self.homing_endstop_accuracy = self.homing_stepper_phases
def setup_cartesian_itersolve(self, axis):
ffi_main, ffi_lib = chelper.get_ffi()
self.setup_itersolve(ffi_main.gc(
ffi_lib.cartesian_stepper_alloc(axis), ffi_lib.free))
def get_range(self):
return self.position_min, self.position_max
def get_homing_info(self):
homing_info = collections.namedtuple('homing_info', [
'speed', 'position_endstop', 'retract_dist', 'positive_dir'])(
self.homing_speed, self.position_endstop,
self.homing_retract_dist, self.homing_positive_dir)
return homing_info
def get_endstops(self):
return [(self.mcu_endstop, self.get_name(short=True))]
def get_homed_offset(self):
if not self.homing_stepper_phases or self.need_motor_enable:
if not self.homing_stepper_phases:
return 0.
pos = self.mcu_stepper.get_mcu_position()
pos = self.steppers[0].get_mcu_position()
pos %= self.homing_stepper_phases
if self.homing_endstop_phase is None:
logging.info("Setting %s endstop phase to %d",
self.get_name(short=True), pos)
logging.info("Setting %s endstop phase to %d", self.name, pos)
self.homing_endstop_phase = pos
return 0.
delta = (pos - self.homing_endstop_phase) % self.homing_stepper_phases
@ -196,57 +204,57 @@ class PrinterHomingStepper(PrinterStepper):
elif delta > self.homing_endstop_accuracy:
raise homing.EndstopError(
"Endstop %s incorrect phase (got %d vs %d)" % (
self.get_name(short=True), pos, self.homing_endstop_phase))
return delta * self.get_step_dist()
self.name, pos, self.homing_endstop_phase))
return delta * self.homing_step_dist
def get_range(self):
return self.position_min, self.position_max
def get_homing_info(self):
homing_info = collections.namedtuple('homing_info', [
'speed', 'position_endstop', 'retract_dist', 'positive_dir'])(
self.homing_speed, self.position_endstop,
self.homing_retract_dist, self.homing_positive_dir)
return homing_info
def get_steppers(self):
return list(self.steppers)
def get_endstops(self):
return list(self.endstops)
def add_extra_stepper(self, config):
stepper = PrinterStepper(config)
self.steppers.append(stepper)
self.step_itersolve = self.step_multi_itersolve
mcu_endstop = self.endstops[0][0]
endstop_pin = config.get('endstop_pin', None)
if endstop_pin is not None:
ppins = config.get_printer().lookup_object('pins')
mcu_endstop = ppins.setup_pin('endstop', endstop_pin)
self.endstops.append((mcu_endstop, stepper.get_name(short=True)))
stepper.add_to_endstop(mcu_endstop)
def add_to_endstop(self, mcu_endstop):
for stepper in self.steppers:
stepper.add_to_endstop(mcu_endstop)
def step_multi_itersolve(self, cmove):
for stepper in self.steppers:
stepper.step_itersolve(cmove)
def setup_cartesian_itersolve(self, axis):
ffi_main, ffi_lib = chelper.get_ffi()
for stepper in self.steppers:
stepper.setup_itersolve(ffi_main.gc(
ffi_lib.cartesian_stepper_alloc(axis), ffi_lib.free))
def set_max_jerk(self, max_halt_velocity, max_accel):
for stepper in self.steppers:
stepper.set_max_jerk(max_halt_velocity, max_accel)
def set_position(self, pos):
for stepper in self.steppers:
stepper.set_position(pos)
def motor_enable(self, print_time, enable=0):
for stepper in self.steppers:
stepper.motor_enable(print_time, enable)
# Wrapper for dual stepper motor support
class PrinterMultiStepper(PrinterHomingStepper):
def __init__(self, config):
PrinterHomingStepper.__init__(self, config)
self.endstops = PrinterHomingStepper.get_endstops(self)
self.extras = []
self.all_step_itersolve = [self.step_itersolve]
def LookupMultiRail(config):
rail = PrinterRail(config)
for i in range(1, 99):
if not config.has_section(config.get_name() + str(i)):
break
extraconfig = config.getsection(config.get_name() + str(i))
extra = PrinterStepper(extraconfig)
self.extras.append(extra)
self.all_step_itersolve.append(extra.step_itersolve)
mcu_endstop = self.mcu_endstop
extraendstop = extraconfig.get('endstop_pin', None)
if extraendstop is not None:
ppins = config.get_printer().lookup_object('pins')
mcu_endstop = ppins.setup_pin('endstop', extraendstop)
self.endstops.append((mcu_endstop, extra.get_name(short=True)))
extra.add_to_endstop(mcu_endstop)
self.step_itersolve = self.step_multi_itersolve
def step_multi_itersolve(self, cmove):
for step_itersolve in self.all_step_itersolve:
step_itersolve(cmove)
def setup_cartesian_itersolve(self, axis):
ffi_main, ffi_lib = chelper.get_ffi()
self.setup_itersolve(ffi_main.gc(
ffi_lib.cartesian_stepper_alloc(axis), ffi_lib.free))
for extra in self.extras:
extra.setup_itersolve(ffi_main.gc(
ffi_lib.cartesian_stepper_alloc(axis), ffi_lib.free))
def set_max_jerk(self, max_halt_velocity, max_accel):
PrinterHomingStepper.set_max_jerk(self, max_halt_velocity, max_accel)
for extra in self.extras:
extra.set_max_jerk(max_halt_velocity, max_accel)
def set_position(self, pos):
PrinterHomingStepper.set_position(self, pos)
for extra in self.extras:
extra.set_position(pos)
def motor_enable(self, print_time, enable=0):
PrinterHomingStepper.motor_enable(self, print_time, enable)
for extra in self.extras:
extra.motor_enable(print_time, enable)
def get_endstops(self):
return self.endstops
def LookupMultiHomingStepper(config):
if not config.has_section(config.get_name() + '1'):
return PrinterHomingStepper(config)
return PrinterMultiStepper(config)
rail.add_extra_stepper(config.getsection(config.get_name() + str(i)))
return rail