stepper: Merge PrinterStepper and MCU_stepper classes

There's no need to track the two classes separately - just combine
them.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2019-11-12 14:12:33 -05:00
parent 4ca190d393
commit 78153ca4c3
1 changed files with 48 additions and 69 deletions

View File

@ -14,16 +14,22 @@ class error(Exception):
# Steppers # Steppers
###################################################################### ######################################################################
# Interface to low-level mcu and chelper code
class MCU_stepper: class MCU_stepper:
def __init__(self, pin_params): def __init__(self, name, step_pin_params, dir_pin_params, step_dist):
self._mcu = pin_params['chip'] self._name = name
self._step_dist = step_dist
self._mcu = step_pin_params['chip']
self._oid = oid = self._mcu.create_oid() self._oid = oid = self._mcu.create_oid()
self._mcu.register_config_callback(self._build_config) self._mcu.register_config_callback(self._build_config)
self._step_pin = pin_params['pin'] self._step_pin = step_pin_params['pin']
self._invert_step = pin_params['invert'] self._invert_step = step_pin_params['invert']
self._dir_pin = self._invert_dir = None 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']
self._mcu_position_offset = 0. self._mcu_position_offset = 0.
self._step_dist = 0.
self._min_stop_interval = 0. self._min_stop_interval = 0.
self._reset_cmd_id = self._get_position_cmd = None self._reset_cmd_id = self._get_position_cmd = None
self._active_callbacks = [] self._active_callbacks = []
@ -37,16 +43,23 @@ class MCU_stepper:
self._trapq = ffi_main.NULL self._trapq = ffi_main.NULL
def get_mcu(self): def get_mcu(self):
return self._mcu return self._mcu
def setup_dir_pin(self, pin_params): def get_name(self, short=False):
if pin_params['chip'] is not self._mcu: if short and self._name.startswith('stepper_'):
raise self._mcu.get_printer().config_error( return self._name[8:]
"Stepper dir pin must be on same mcu as step pin") return self._name
self._dir_pin = pin_params['pin'] def add_to_endstop(self, mcu_endstop):
self._invert_dir = pin_params['invert'] mcu_endstop.add_stepper(self)
def setup_min_stop_interval(self, min_stop_interval): def _dist_to_time(self, dist, start_velocity, accel):
self._min_stop_interval = min_stop_interval # Calculate the time it takes to travel a distance with constant accel
def setup_step_distance(self, step_dist): time_offset = start_velocity / accel
self._step_dist = step_dist 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
def setup_itersolve(self, alloc_func, *params): def setup_itersolve(self, alloc_func, *params):
ffi_main, ffi_lib = chelper.get_ffi() ffi_main, ffi_lib = chelper.get_ffi()
sk = ffi_main.gc(getattr(ffi_lib, alloc_func)(*params), ffi_lib.free) sk = ffi_main.gc(getattr(ffi_lib, alloc_func)(*params), ffi_lib.free)
@ -147,59 +160,25 @@ class MCU_stepper:
if ret: if ret:
raise error("Internal error in stepcompress") raise error("Internal error in stepcompress")
# Code storing the definitions for a stepper motor # Helper code to build a stepper object from a config section
class PrinterStepper: def PrinterStepper(config):
def __init__(self, config):
printer = config.get_printer() printer = config.get_printer()
self.name = config.get_name() name = config.get_name()
# Stepper definition # Stepper definition
ppins = printer.lookup_object('pins') ppins = printer.lookup_object('pins')
step_pin = config.get('step_pin') step_pin = config.get('step_pin')
step_pin_params = ppins.lookup_pin(step_pin, can_invert=True) step_pin_params = ppins.lookup_pin(step_pin, can_invert=True)
self.mcu_stepper = mcu_stepper = MCU_stepper(step_pin_params)
dir_pin = config.get('dir_pin') dir_pin = config.get('dir_pin')
dir_pin_params = ppins.lookup_pin(dir_pin, can_invert=True) dir_pin_params = ppins.lookup_pin(dir_pin, can_invert=True)
mcu_stepper.setup_dir_pin(dir_pin_params)
step_dist = config.getfloat('step_distance', above=0.) step_dist = config.getfloat('step_distance', above=0.)
mcu_stepper.setup_step_distance(step_dist) mcu_stepper = MCU_stepper(name, step_pin_params, dir_pin_params, step_dist)
# Wrappers # Support for stepper enable pin handling
self.setup_itersolve = mcu_stepper.setup_itersolve
self.generate_steps = mcu_stepper.generate_steps
self.set_trapq = mcu_stepper.set_trapq
self.set_stepper_kinematics = mcu_stepper.set_stepper_kinematics
self.add_active_callback = mcu_stepper.add_active_callback
self.calc_position_from_coord = mcu_stepper.calc_position_from_coord
self.set_position = mcu_stepper.set_position
self.get_commanded_position = mcu_stepper.get_commanded_position
self.set_commanded_position = mcu_stepper.set_commanded_position
self.get_mcu_position = mcu_stepper.get_mcu_position
self.get_step_dist = mcu_stepper.get_step_dist
self.is_dir_inverted = mcu_stepper.is_dir_inverted
# Enable pin handling
stepper_enable = printer.try_load_module(config, 'stepper_enable') stepper_enable = printer.try_load_module(config, 'stepper_enable')
stepper_enable.register_stepper(self, config.get('enable_pin', None)) stepper_enable.register_stepper(mcu_stepper, config.get('enable_pin', None))
# Register STEPPER_BUZZ command # Register STEPPER_BUZZ command
force_move = printer.try_load_module(config, 'force_move') force_move = printer.try_load_module(config, 'force_move')
force_move.register_stepper(self) force_move.register_stepper(mcu_stepper)
def get_name(self, short=False): return mcu_stepper
if short and self.name.startswith('stepper_'):
return self.name[8:]
return self.name
def add_to_endstop(self, mcu_endstop):
mcu_endstop.add_stepper(self.mcu_stepper)
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
step_dist = self.get_step_dist()
last_step_time = self._dist_to_time(
step_dist, max_halt_velocity, max_accel)
second_last_step_time = self._dist_to_time(
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)
###################################################################### ######################################################################