2016-05-25 17:37:40 +02:00
|
|
|
# Printer stepper support
|
|
|
|
#
|
2019-06-13 05:06:41 +02:00
|
|
|
# Copyright (C) 2016-2019 Kevin O'Connor <kevin@koconnor.net>
|
2016-05-25 17:37:40 +02:00
|
|
|
#
|
|
|
|
# This file may be distributed under the terms of the GNU GPLv3 license.
|
2018-06-22 00:48:33 +02:00
|
|
|
import math, logging, collections
|
2019-11-12 19:55:50 +01:00
|
|
|
import homing, chelper
|
|
|
|
|
|
|
|
class error(Exception):
|
|
|
|
pass
|
2016-05-25 17:37:40 +02:00
|
|
|
|
2018-06-22 17:44:25 +02:00
|
|
|
|
|
|
|
######################################################################
|
|
|
|
# Steppers
|
|
|
|
######################################################################
|
|
|
|
|
2019-11-12 20:12:33 +01:00
|
|
|
# Interface to low-level mcu and chelper code
|
2019-11-12 19:55:50 +01:00
|
|
|
class MCU_stepper:
|
2019-12-05 00:42:59 +01:00
|
|
|
def __init__(self, name, step_pin_params, dir_pin_params, step_dist,
|
|
|
|
units_in_radians=False):
|
2019-11-12 20:12:33 +01:00
|
|
|
self._name = name
|
|
|
|
self._step_dist = step_dist
|
2019-12-05 00:42:59 +01:00
|
|
|
self._units_in_radians = units_in_radians
|
2019-11-12 20:12:33 +01:00
|
|
|
self._mcu = step_pin_params['chip']
|
2019-11-12 19:55:50 +01:00
|
|
|
self._oid = oid = self._mcu.create_oid()
|
|
|
|
self._mcu.register_config_callback(self._build_config)
|
2019-11-12 20:12:33 +01:00
|
|
|
self._step_pin = step_pin_params['pin']
|
|
|
|
self._invert_step = step_pin_params['invert']
|
|
|
|
if dir_pin_params['chip'] is not self._mcu:
|
|
|
|
raise self._mcu.get_printer().config_error(
|
|
|
|
"Stepper dir pin must be on same mcu as step pin")
|
|
|
|
self._dir_pin = dir_pin_params['pin']
|
|
|
|
self._invert_dir = dir_pin_params['invert']
|
2019-11-13 23:59:40 +01:00
|
|
|
self._mcu_position_offset = self._tag_position = 0.
|
2019-11-12 19:55:50 +01:00
|
|
|
self._min_stop_interval = 0.
|
|
|
|
self._reset_cmd_id = self._get_position_cmd = None
|
|
|
|
self._active_callbacks = []
|
|
|
|
ffi_main, self._ffi_lib = chelper.get_ffi()
|
|
|
|
self._stepqueue = ffi_main.gc(self._ffi_lib.stepcompress_alloc(oid),
|
|
|
|
self._ffi_lib.stepcompress_free)
|
|
|
|
self._mcu.register_stepqueue(self._stepqueue)
|
|
|
|
self._stepper_kinematics = None
|
|
|
|
self._itersolve_generate_steps = self._ffi_lib.itersolve_generate_steps
|
|
|
|
self._itersolve_check_active = self._ffi_lib.itersolve_check_active
|
|
|
|
self._trapq = ffi_main.NULL
|
|
|
|
def get_mcu(self):
|
|
|
|
return self._mcu
|
2019-11-12 20:12:33 +01:00
|
|
|
def get_name(self, short=False):
|
|
|
|
if short and self._name.startswith('stepper_'):
|
|
|
|
return self._name[8:]
|
|
|
|
return self._name
|
2019-12-05 00:42:59 +01:00
|
|
|
def units_in_radians(self):
|
|
|
|
# Returns true if distances are in radians instead of millimeters
|
|
|
|
return self._units_in_radians
|
2019-11-12 20:12:33 +01:00
|
|
|
def _dist_to_time(self, dist, start_velocity, accel):
|
|
|
|
# Calculate the time it takes to travel a distance with constant accel
|
|
|
|
time_offset = start_velocity / accel
|
|
|
|
return math.sqrt(2. * dist / accel + time_offset**2) - time_offset
|
|
|
|
def set_max_jerk(self, max_halt_velocity, max_accel):
|
|
|
|
# Calculate the firmware's maximum halt interval time
|
|
|
|
last_step_time = self._dist_to_time(self._step_dist,
|
|
|
|
max_halt_velocity, max_accel)
|
|
|
|
second_last_step_time = self._dist_to_time(2. * self._step_dist,
|
|
|
|
max_halt_velocity, max_accel)
|
|
|
|
self._min_stop_interval = second_last_step_time - last_step_time
|
2019-11-12 19:55:50 +01:00
|
|
|
def setup_itersolve(self, alloc_func, *params):
|
|
|
|
ffi_main, ffi_lib = chelper.get_ffi()
|
|
|
|
sk = ffi_main.gc(getattr(ffi_lib, alloc_func)(*params), ffi_lib.free)
|
|
|
|
self.set_stepper_kinematics(sk)
|
|
|
|
def _build_config(self):
|
|
|
|
max_error = self._mcu.get_max_stepper_error()
|
|
|
|
min_stop_interval = max(0., self._min_stop_interval - max_error)
|
|
|
|
self._mcu.add_config_cmd(
|
|
|
|
"config_stepper oid=%d step_pin=%s dir_pin=%s"
|
|
|
|
" min_stop_interval=%d invert_step=%d" % (
|
|
|
|
self._oid, self._step_pin, self._dir_pin,
|
|
|
|
self._mcu.seconds_to_clock(min_stop_interval),
|
|
|
|
self._invert_step))
|
|
|
|
self._mcu.add_config_cmd(
|
|
|
|
"reset_step_clock oid=%d clock=0" % (self._oid,), is_init=True)
|
|
|
|
step_cmd_id = self._mcu.lookup_command_id(
|
|
|
|
"queue_step oid=%c interval=%u count=%hu add=%hi")
|
|
|
|
dir_cmd_id = self._mcu.lookup_command_id(
|
|
|
|
"set_next_step_dir oid=%c dir=%c")
|
|
|
|
self._reset_cmd_id = self._mcu.lookup_command_id(
|
|
|
|
"reset_step_clock oid=%c clock=%u")
|
|
|
|
self._get_position_cmd = self._mcu.lookup_command(
|
|
|
|
"stepper_get_position oid=%c")
|
|
|
|
self._ffi_lib.stepcompress_fill(
|
|
|
|
self._stepqueue, self._mcu.seconds_to_clock(max_error),
|
|
|
|
self._invert_dir, step_cmd_id, dir_cmd_id)
|
|
|
|
def get_oid(self):
|
|
|
|
return self._oid
|
|
|
|
def get_step_dist(self):
|
|
|
|
return self._step_dist
|
|
|
|
def is_dir_inverted(self):
|
|
|
|
return self._invert_dir
|
2019-11-19 18:27:33 +01:00
|
|
|
def calc_position_from_coord(self, coord):
|
|
|
|
return self._ffi_lib.itersolve_calc_position_from_coord(
|
|
|
|
self._stepper_kinematics, coord[0], coord[1], coord[2])
|
2019-11-12 19:55:50 +01:00
|
|
|
def set_position(self, coord):
|
2019-11-14 00:58:51 +01:00
|
|
|
opos = self.get_commanded_position()
|
|
|
|
sk = self._stepper_kinematics
|
|
|
|
self._ffi_lib.itersolve_set_position(sk, coord[0], coord[1], coord[2])
|
|
|
|
self._mcu_position_offset += opos - self.get_commanded_position()
|
2019-11-12 19:55:50 +01:00
|
|
|
def get_commanded_position(self):
|
2019-11-14 00:58:51 +01:00
|
|
|
sk = self._stepper_kinematics
|
|
|
|
return self._ffi_lib.itersolve_get_commanded_pos(sk)
|
2019-11-12 19:55:50 +01:00
|
|
|
def get_mcu_position(self):
|
|
|
|
mcu_pos_dist = self.get_commanded_position() + self._mcu_position_offset
|
|
|
|
mcu_pos = mcu_pos_dist / self._step_dist
|
|
|
|
if mcu_pos >= 0.:
|
|
|
|
return int(mcu_pos + 0.5)
|
|
|
|
return int(mcu_pos - 0.5)
|
2019-11-13 23:59:40 +01:00
|
|
|
def get_tag_position(self):
|
|
|
|
return self._tag_position
|
|
|
|
def set_tag_position(self, position):
|
|
|
|
self._tag_position = position
|
2019-11-12 19:55:50 +01:00
|
|
|
def set_stepper_kinematics(self, sk):
|
|
|
|
old_sk = self._stepper_kinematics
|
|
|
|
self._stepper_kinematics = sk
|
|
|
|
if sk is not None:
|
|
|
|
self._ffi_lib.itersolve_set_stepcompress(
|
|
|
|
sk, self._stepqueue, self._step_dist)
|
|
|
|
return old_sk
|
|
|
|
def note_homing_end(self, did_trigger=False):
|
|
|
|
ret = self._ffi_lib.stepcompress_reset(self._stepqueue, 0)
|
|
|
|
if ret:
|
|
|
|
raise error("Internal error in stepcompress")
|
|
|
|
data = (self._reset_cmd_id, self._oid, 0)
|
|
|
|
ret = self._ffi_lib.stepcompress_queue_msg(
|
|
|
|
self._stepqueue, data, len(data))
|
|
|
|
if ret:
|
|
|
|
raise error("Internal error in stepcompress")
|
|
|
|
if not did_trigger or self._mcu.is_fileoutput():
|
|
|
|
return
|
|
|
|
params = self._get_position_cmd.send_with_response(
|
|
|
|
[self._oid], response='stepper_position', response_oid=self._oid)
|
|
|
|
mcu_pos_dist = params['pos'] * self._step_dist
|
|
|
|
if self._invert_dir:
|
|
|
|
mcu_pos_dist = -mcu_pos_dist
|
2019-11-14 00:42:07 +01:00
|
|
|
self._mcu_position_offset = mcu_pos_dist - self.get_commanded_position()
|
2019-11-12 19:55:50 +01:00
|
|
|
def set_trapq(self, tq):
|
|
|
|
if tq is None:
|
|
|
|
ffi_main, self._ffi_lib = chelper.get_ffi()
|
|
|
|
tq = ffi_main.NULL
|
|
|
|
self._ffi_lib.itersolve_set_trapq(self._stepper_kinematics, tq)
|
|
|
|
old_tq = self._trapq
|
|
|
|
self._trapq = tq
|
|
|
|
return old_tq
|
|
|
|
def add_active_callback(self, cb):
|
|
|
|
self._active_callbacks.append(cb)
|
|
|
|
def generate_steps(self, flush_time):
|
|
|
|
# Check for activity if necessary
|
|
|
|
if self._active_callbacks:
|
|
|
|
ret = self._itersolve_check_active(self._stepper_kinematics,
|
|
|
|
flush_time)
|
|
|
|
if ret:
|
|
|
|
cbs = self._active_callbacks
|
|
|
|
self._active_callbacks = []
|
|
|
|
for cb in cbs:
|
|
|
|
cb(ret)
|
|
|
|
# Generate steps
|
|
|
|
ret = self._itersolve_generate_steps(self._stepper_kinematics,
|
|
|
|
flush_time)
|
|
|
|
if ret:
|
|
|
|
raise error("Internal error in stepcompress")
|
2020-01-14 03:30:32 +01:00
|
|
|
def is_active_axis(self, axis):
|
|
|
|
return self._ffi_lib.itersolve_is_active_axis(
|
|
|
|
self._stepper_kinematics, axis)
|
2019-11-12 19:55:50 +01:00
|
|
|
|
2019-11-12 20:12:33 +01:00
|
|
|
# Helper code to build a stepper object from a config section
|
2019-12-05 00:42:59 +01:00
|
|
|
def PrinterStepper(config, units_in_radians=False):
|
2019-11-12 20:12:33 +01:00
|
|
|
printer = config.get_printer()
|
|
|
|
name = config.get_name()
|
|
|
|
# Stepper definition
|
|
|
|
ppins = printer.lookup_object('pins')
|
|
|
|
step_pin = config.get('step_pin')
|
|
|
|
step_pin_params = ppins.lookup_pin(step_pin, can_invert=True)
|
|
|
|
dir_pin = config.get('dir_pin')
|
|
|
|
dir_pin_params = ppins.lookup_pin(dir_pin, can_invert=True)
|
|
|
|
step_dist = config.getfloat('step_distance', above=0.)
|
2019-12-05 00:42:59 +01:00
|
|
|
mcu_stepper = MCU_stepper(name, step_pin_params, dir_pin_params, step_dist,
|
|
|
|
units_in_radians)
|
2019-11-12 20:12:33 +01:00
|
|
|
# Support for stepper enable pin handling
|
|
|
|
stepper_enable = printer.try_load_module(config, 'stepper_enable')
|
|
|
|
stepper_enable.register_stepper(mcu_stepper, config.get('enable_pin', None))
|
|
|
|
# Register STEPPER_BUZZ command
|
|
|
|
force_move = printer.try_load_module(config, 'force_move')
|
|
|
|
force_move.register_stepper(mcu_stepper)
|
|
|
|
return mcu_stepper
|
2017-07-24 19:54:46 +02:00
|
|
|
|
2018-06-22 17:44:25 +02:00
|
|
|
|
|
|
|
######################################################################
|
|
|
|
# Stepper controlled rails
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
# A motor control "rail" with one (or more) steppers and one (or more)
|
|
|
|
# endstops.
|
|
|
|
class PrinterRail:
|
2018-06-21 19:47:39 +02:00
|
|
|
def __init__(self, config, need_position_minmax=True,
|
2019-12-05 00:42:59 +01:00
|
|
|
default_position_endstop=None, units_in_radians=False):
|
2019-11-12 19:53:07 +01:00
|
|
|
# Primary stepper and endstop
|
2019-12-05 00:42:59 +01:00
|
|
|
self.stepper_units_in_radians = units_in_radians
|
2019-11-12 19:53:07 +01:00
|
|
|
self.steppers = []
|
|
|
|
self.endstops = []
|
|
|
|
self.add_extra_stepper(config)
|
2019-11-19 18:27:33 +01:00
|
|
|
mcu_stepper = self.steppers[0]
|
|
|
|
self.get_commanded_position = mcu_stepper.get_commanded_position
|
|
|
|
self.get_tag_position = mcu_stepper.get_tag_position
|
|
|
|
self.set_tag_position = mcu_stepper.set_tag_position
|
|
|
|
self.calc_position_from_coord = mcu_stepper.calc_position_from_coord
|
2019-11-12 19:53:07 +01:00
|
|
|
# Primary endstop position
|
|
|
|
mcu_endstop = self.endstops[0][0]
|
2018-09-27 01:29:58 +02:00
|
|
|
if hasattr(mcu_endstop, "get_position_endstop"):
|
|
|
|
self.position_endstop = mcu_endstop.get_position_endstop()
|
|
|
|
elif default_position_endstop is None:
|
2017-11-02 02:21:37 +01:00
|
|
|
self.position_endstop = config.getfloat('position_endstop')
|
|
|
|
else:
|
|
|
|
self.position_endstop = config.getfloat(
|
2018-05-19 01:34:17 +02:00
|
|
|
'position_endstop', default_position_endstop)
|
2017-11-07 00:37:35 +01:00
|
|
|
# Axis range
|
2018-05-19 01:34:17 +02:00
|
|
|
if need_position_minmax:
|
|
|
|
self.position_min = config.getfloat('position_min', 0.)
|
|
|
|
self.position_max = config.getfloat(
|
|
|
|
'position_max', above=self.position_min)
|
|
|
|
else:
|
|
|
|
self.position_min = 0.
|
|
|
|
self.position_max = self.position_endstop
|
|
|
|
if (self.position_endstop < self.position_min
|
|
|
|
or self.position_endstop > self.position_max):
|
|
|
|
raise config.error(
|
|
|
|
"position_endstop in section '%s' must be between"
|
|
|
|
" position_min and position_max" % config.get_name())
|
2017-11-07 00:37:35 +01:00
|
|
|
# Homing mechanics
|
2017-04-11 17:37:09 +02:00
|
|
|
self.homing_speed = config.getfloat('homing_speed', 5.0, above=0.)
|
2018-10-08 18:54:33 +02:00
|
|
|
self.second_homing_speed = config.getfloat(
|
|
|
|
'second_homing_speed', self.homing_speed/2., above=0.)
|
2020-02-12 16:09:39 +01:00
|
|
|
self.homing_retract_speed = config.getfloat(
|
|
|
|
'homing_retract_speed', self.homing_speed, above=0.)
|
2017-11-07 00:37:35 +01:00
|
|
|
self.homing_retract_dist = config.getfloat(
|
2018-03-06 07:59:13 +01:00
|
|
|
'homing_retract_dist', 5., minval=0.)
|
2018-07-26 16:11:56 +02:00
|
|
|
self.homing_positive_dir = config.getboolean(
|
|
|
|
'homing_positive_dir', None)
|
2017-07-24 20:12:17 +02:00
|
|
|
if self.homing_positive_dir is None:
|
|
|
|
axis_len = self.position_max - self.position_min
|
|
|
|
if self.position_endstop <= self.position_min + axis_len / 4.:
|
|
|
|
self.homing_positive_dir = False
|
|
|
|
elif self.position_endstop >= self.position_max - axis_len / 4.:
|
|
|
|
self.homing_positive_dir = True
|
|
|
|
else:
|
|
|
|
raise config.error(
|
|
|
|
"Unable to infer homing_positive_dir in section '%s'" % (
|
2018-01-20 04:22:17 +01:00
|
|
|
config.get_name(),))
|
2018-06-22 17:44:25 +02:00
|
|
|
def get_range(self):
|
|
|
|
return self.position_min, self.position_max
|
|
|
|
def get_homing_info(self):
|
|
|
|
homing_info = collections.namedtuple('homing_info', [
|
2020-02-12 16:09:39 +01:00
|
|
|
'speed', 'position_endstop', 'retract_speed', 'retract_dist',
|
|
|
|
'positive_dir', 'second_homing_speed'])(
|
2018-06-22 17:44:25 +02:00
|
|
|
self.homing_speed, self.position_endstop,
|
2020-02-12 16:09:39 +01:00
|
|
|
self.homing_retract_speed, self.homing_retract_dist,
|
|
|
|
self.homing_positive_dir, self.second_homing_speed)
|
2018-06-22 17:44:25 +02:00
|
|
|
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):
|
2019-12-05 00:42:59 +01:00
|
|
|
stepper = PrinterStepper(config, self.stepper_units_in_radians)
|
2018-06-22 17:44:25 +02:00
|
|
|
self.steppers.append(stepper)
|
2019-11-12 19:53:07 +01:00
|
|
|
if self.endstops and config.get('endstop_pin', None) is None:
|
|
|
|
# No endstop defined - use primary endstop
|
2019-11-12 20:21:15 +01:00
|
|
|
self.endstops[0][0].add_stepper(stepper)
|
2019-11-12 19:53:07 +01:00
|
|
|
return
|
|
|
|
printer = config.get_printer()
|
|
|
|
ppins = printer.lookup_object('pins')
|
|
|
|
mcu_endstop = ppins.setup_pin('endstop', config.get('endstop_pin'))
|
2019-11-12 20:21:15 +01:00
|
|
|
mcu_endstop.add_stepper(stepper)
|
2019-11-12 19:53:07 +01:00
|
|
|
name = stepper.get_name(short=True)
|
|
|
|
self.endstops.append((mcu_endstop, name))
|
|
|
|
query_endstops = printer.try_load_module(config, 'query_endstops')
|
|
|
|
query_endstops.register_endstop(mcu_endstop, name)
|
2018-07-13 17:24:36 +02:00
|
|
|
def setup_itersolve(self, alloc_func, *params):
|
2018-06-22 17:44:25 +02:00
|
|
|
for stepper in self.steppers:
|
2018-07-13 17:24:36 +02:00
|
|
|
stepper.setup_itersolve(alloc_func, *params)
|
2019-10-28 02:05:57 +01:00
|
|
|
def generate_steps(self, flush_time):
|
|
|
|
for stepper in self.steppers:
|
|
|
|
stepper.generate_steps(flush_time)
|
|
|
|
def set_trapq(self, trapq):
|
|
|
|
for stepper in self.steppers:
|
|
|
|
stepper.set_trapq(trapq)
|
2017-11-07 19:10:08 +01:00
|
|
|
def set_max_jerk(self, max_halt_velocity, max_accel):
|
2018-06-22 17:44:25 +02:00
|
|
|
for stepper in self.steppers:
|
|
|
|
stepper.set_max_jerk(max_halt_velocity, max_accel)
|
2018-10-09 02:50:45 +02:00
|
|
|
def set_position(self, coord):
|
2018-06-22 17:44:25 +02:00
|
|
|
for stepper in self.steppers:
|
2018-10-09 02:50:45 +02:00
|
|
|
stepper.set_position(coord)
|
2017-11-07 19:10:08 +01:00
|
|
|
|
2018-06-22 17:44:25 +02:00
|
|
|
# Wrapper for dual stepper motor support
|
|
|
|
def LookupMultiRail(config):
|
|
|
|
rail = PrinterRail(config)
|
|
|
|
for i in range(1, 99):
|
|
|
|
if not config.has_section(config.get_name() + str(i)):
|
|
|
|
break
|
|
|
|
rail.add_extra_stepper(config.getsection(config.get_name() + str(i)))
|
|
|
|
return rail
|