stepper_enable: Move enable tracking from stepper.py to stepper_enable.py

Move the enable line tracking out of the main stepper.py code.  This
simplifies the main kinematic code.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2019-11-12 12:49:21 -05:00
parent bfb34e0701
commit 0e30b862c7
5 changed files with 111 additions and 92 deletions

View File

@ -11,7 +11,9 @@ class ControllerFan:
def __init__(self, config):
self.printer = config.get_printer()
self.printer.register_event_handler("klippy:ready", self.handle_ready)
self.steppers = []
self.stepper_names = []
self.stepper_enable = self.printer.try_load_module(config,
'stepper_enable')
self.heaters = []
self.fan = fan.PrinterFan(config)
self.mcu = self.fan.mcu_fan.get_mcu()
@ -29,14 +31,14 @@ class ControllerFan:
self.heaters = [pheater.lookup_heater(n.strip())
for n in self.heater_name.split(',')]
kin = self.printer.lookup_object('toolhead').get_kinematics()
self.steppers = kin.get_steppers()
self.stepper_names = [s.get_name() for s in kin.get_steppers()]
reactor = self.printer.get_reactor()
reactor.register_timer(self.callback, reactor.NOW)
def callback(self, eventtime):
power = 0.
active = False
for stepper in self.steppers:
active |= stepper.is_motor_enabled()
for name in self.stepper_names:
active |= self.stepper_enable.is_motor_enabled(name)
for heater in self.heaters:
_, target_temp = heater.get_temp(eventtime)
if target_temp:

View File

@ -53,9 +53,10 @@ class ForceMove:
def force_enable(self, stepper):
toolhead = self.printer.lookup_object('toolhead')
print_time = toolhead.get_last_move_time()
was_enable = stepper.is_motor_enabled()
stepper_enable = self.printer.lookup_object('stepper_enable')
was_enable = stepper_enable.is_motor_enabled(stepper.get_name())
if not was_enable:
stepper.motor_enable(print_time, 1)
stepper_enable.motor_enable(stepper.get_name(), print_time)
toolhead.dwell(STALL_TIME)
return was_enable
def restore_enable(self, stepper, was_enable):
@ -63,7 +64,8 @@ class ForceMove:
toolhead = self.printer.lookup_object('toolhead')
toolhead.dwell(STALL_TIME)
print_time = toolhead.get_last_move_time()
stepper.motor_enable(print_time, 0)
stepper_enable = self.printer.lookup_object('stepper_enable')
stepper_enable.motor_disable(stepper.get_name(), print_time)
toolhead.dwell(STALL_TIME)
def manual_move(self, stepper, dist, speed, accel=0.):
toolhead = self.printer.lookup_object('toolhead')

View File

@ -13,11 +13,13 @@ class ManualStepper:
self.printer = config.get_printer()
if config.get('endstop_pin', None) is not None:
self.can_home = True
self.stepper = stepper.PrinterRail(
self.rail = stepper.PrinterRail(
config, need_position_minmax=False, default_position_endstop=0.)
self.steppers = self.rail.get_steppers()
else:
self.can_home = False
self.stepper = stepper.PrinterStepper(config)
self.rail = stepper.PrinterStepper(config)
self.steppers = [self.rail]
self.velocity = config.getfloat('velocity', 5., above=0.)
self.accel = config.getfloat('accel', 0., minval=0.)
self.next_cmd_time = 0.
@ -26,9 +28,9 @@ class ManualStepper:
self.trapq = ffi_main.gc(ffi_lib.trapq_alloc(), ffi_lib.trapq_free)
self.trapq_append = ffi_lib.trapq_append
self.trapq_free_moves = ffi_lib.trapq_free_moves
self.stepper.setup_itersolve('cartesian_stepper_alloc', 'x')
self.stepper.set_trapq(self.trapq)
self.stepper.set_max_jerk(9999999.9, 9999999.9)
self.rail.setup_itersolve('cartesian_stepper_alloc', 'x')
self.rail.set_trapq(self.trapq)
self.rail.set_max_jerk(9999999.9, 9999999.9)
# Register commands
stepper_name = config.get_name().split()[1]
self.gcode = self.printer.lookup_object('gcode')
@ -44,13 +46,19 @@ class ManualStepper:
self.next_cmd_time = print_time
def do_enable(self, enable):
self.sync_print_time()
self.stepper.motor_enable(self.next_cmd_time, enable)
stepper_enable = self.printer.lookup_object('stepper_enable')
if enable:
for s in self.steppers:
stepper_enable.motor_enable(s.get_name(), self.next_cmd_time)
else:
for s in self.steppers:
stepper_enable.motor_disable(s.get_name(), self.next_cmd_time)
self.sync_print_time()
def do_set_position(self, setpos):
self.stepper.set_position([setpos, 0., 0.])
self.rail.set_position([setpos, 0., 0.])
def do_move(self, movepos, speed, accel):
self.sync_print_time()
cp = self.stepper.get_commanded_position()
cp = self.rail.get_commanded_position()
dist = movepos - cp
axis_r, accel_t, cruise_t, cruise_v = force_move.calc_move_time(
dist, speed, accel)
@ -59,14 +67,14 @@ class ManualStepper:
cp, 0., 0., axis_r, 0., 0.,
0., cruise_v, accel)
self.next_cmd_time += accel_t + cruise_t + accel_t
self.stepper.generate_steps(self.next_cmd_time)
self.rail.generate_steps(self.next_cmd_time)
self.trapq_free_moves(self.trapq, self.next_cmd_time)
self.sync_print_time()
def do_homing_move(self, movepos, speed, accel, triggered):
if not self.can_home:
raise self.gcode.error("No endstop for this manual stepper")
# Notify endstops of upcoming home
endstops = self.stepper.get_endstops()
endstops = self.rail.get_endstops()
for mcu_endstop, name in endstops:
mcu_endstop.home_prepare()
# Start endstop checking

View File

@ -7,24 +7,86 @@ import logging
DISABLE_STALL_TIME = 0.100
class StepperEnable:
# Tracking of shared stepper enable pins
class StepperEnablePin:
def __init__(self, mcu_enable, enable_count=0):
self.mcu_enable = mcu_enable
self.enable_count = enable_count
def set_enable(self, print_time):
if not self.enable_count:
self.mcu_enable.set_digital(print_time, 1)
self.enable_count += 1
def set_disable(self, print_time):
self.enable_count -= 1
if not self.enable_count:
self.mcu_enable.set_digital(print_time, 0)
# Automatic multiple pin enable lines
class StepperMultiEnablePin:
def __init__(self, enable_list):
self.enable_list = enable_list
def set_enable(self, print_time):
for en in self.enable_list:
en.set_enable(print_time)
def set_disable(self, print_time):
for en in self.enable_list:
en.set_disable(print_time)
# Resolve the 'enable_pin' stepper config setting
def lookup_enable_pin(ppins, pin_list):
if pin_list is None:
return StepperEnablePin(None, 9999)
enable_list = []
for pin in pin_list.split(','):
pin_params = ppins.lookup_pin(pin, can_invert=True,
share_type='stepper_enable')
enable = pin_params.get('class')
if enable is None:
mcu_enable = pin_params['chip'].setup_pin('digital_out', pin_params)
mcu_enable.setup_max_duration(0.)
pin_params['class'] = enable = StepperEnablePin(mcu_enable)
enable_list.append(enable)
if len(enable_list) == 1:
return enable_list[0]
return StepperMultiEnablePin(enable_list)
# Enable line tracking for each stepper motor
class EnableTracking:
def __init__(self, printer, stepper, pin):
self.stepper = stepper
self.is_motor_enabled = False
self.stepper.add_active_callback(self.motor_enable)
self.enable = lookup_enable_pin(printer.lookup_object('pins'), pin)
def motor_enable(self, print_time):
if not self.is_motor_enabled:
self.enable.set_enable(print_time)
self.is_motor_enabled = True
def motor_disable(self, print_time):
if self.is_motor_enabled:
# Enable stepper on future stepper movement
self.enable.set_disable(print_time)
self.is_motor_enabled = False
self.stepper.add_active_callback(self.motor_enable)
class PrinterStepperEnable:
def __init__(self, config):
self.printer = config.get_printer()
self.steppers = {}
self.enable_lines = {}
self.printer.register_event_handler("gcode:request_restart",
self._handle_request_restart)
# Register M18/M84 commands
gcode = self.printer.lookup_object('gcode')
gcode.register_command("M18", self.cmd_M18)
gcode.register_command("M84", self.cmd_M18)
def register_stepper(self, stepper):
self.steppers[stepper.get_name()] = stepper
def register_stepper(self, stepper, pin):
name = stepper.get_name()
self.enable_lines[name] = EnableTracking(self.printer, stepper, pin)
def motor_off(self):
toolhead = self.printer.lookup_object('toolhead')
toolhead.dwell(DISABLE_STALL_TIME)
print_time = toolhead.get_last_move_time()
for s in self.steppers.values():
s.motor_enable(print_time, 0)
for el in self.enable_lines.values():
el.motor_disable(print_time)
self.printer.send_event("stepper_enable:motor_off", print_time)
toolhead.dwell(DISABLE_STALL_TIME)
logging.debug('; Max time of %f', print_time)
@ -33,6 +95,12 @@ class StepperEnable:
def cmd_M18(self, params):
# Turn off motors
self.motor_off()
def is_motor_enabled(self, name):
return self.enable_lines[name].is_motor_enabled
def motor_enable(self, name, print_time):
self.enable_lines[name].motor_enable(print_time)
def motor_disable(self, name, print_time):
self.enable_lines[name].motor_disable(print_time)
def load_config(config):
return StepperEnable(config)
return PrinterStepperEnable(config)

View File

@ -7,50 +7,6 @@ import math, logging, collections
import homing
######################################################################
# Stepper enable pins
######################################################################
# Tracking of shared stepper enable pins
class StepperEnablePin:
def __init__(self, mcu_enable, enable_count=0):
self.mcu_enable = mcu_enable
self.enable_count = enable_count
def set_enable(self, print_time, enable):
if enable:
if not self.enable_count:
self.mcu_enable.set_digital(print_time, 1)
self.enable_count += 1
else:
self.enable_count -= 1
if not self.enable_count:
self.mcu_enable.set_digital(print_time, 0)
class StepperMultiEnablePin:
def __init__(self, enable_list):
self.enable_list = enable_list
def set_enable(self, print_time, enable):
for en in self.enable_list:
en.set_enable(print_time, enable)
def lookup_enable_pin(ppins, pin_list):
if pin_list is None:
return StepperEnablePin(None, 9999)
enable_list = []
for pin in pin_list.split(','):
pin_params = ppins.lookup_pin(pin, can_invert=True,
share_type='stepper_enable')
enable = pin_params.get('class')
if enable is None:
mcu_enable = pin_params['chip'].setup_pin('digital_out', pin_params)
mcu_enable.setup_max_duration(0.)
pin_params['class'] = enable = StepperEnablePin(mcu_enable)
enable_list.append(enable)
if len(enable_list) == 1:
return enable_list[0]
return StepperMultiEnablePin(enable_list)
######################################################################
# Steppers
######################################################################
@ -60,7 +16,6 @@ class PrinterStepper:
def __init__(self, config):
printer = config.get_printer()
self.name = config.get_name()
self.need_motor_enable = True
# Stepper definition
ppins = printer.lookup_object('pins')
step_pin = config.get('step_pin')
@ -70,19 +25,12 @@ class PrinterStepper:
mcu_stepper.setup_dir_pin(dir_pin_params)
step_dist = config.getfloat('step_distance', above=0.)
mcu_stepper.setup_step_distance(step_dist)
# Enable pin handling
stepper_enable = printer.try_load_module(config, 'stepper_enable')
stepper_enable.register_stepper(self)
mcu_stepper.add_active_callback(self._stepper_active)
self.enable = lookup_enable_pin(ppins, config.get('enable_pin', None))
# Register STEPPER_BUZZ command
force_move = printer.try_load_module(config, 'force_move')
force_move.register_stepper(self)
# Wrappers
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
@ -90,6 +38,12 @@ class PrinterStepper:
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.register_stepper(self, config.get('enable_pin', None))
# Register STEPPER_BUZZ command
force_move = printer.try_load_module(config, 'force_move')
force_move.register_stepper(self)
def get_name(self, short=False):
if short and self.name.startswith('stepper_'):
return self.name[8:]
@ -109,17 +63,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 _stepper_active(self, active_time):
self.motor_enable(active_time, 1)
def motor_enable(self, print_time, enable=0):
if self.need_motor_enable != (not enable):
self.enable.set_enable(print_time, enable)
self.need_motor_enable = not enable
if not enable:
# Enable stepper on future stepper movement
self.mcu_stepper.add_active_callback(self._stepper_active)
def is_motor_enabled(self):
return not self.need_motor_enable
######################################################################
@ -136,7 +79,6 @@ class PrinterRail:
self.steppers = [stepper]
self.name = stepper.get_name(short=True)
self.get_commanded_position = stepper.get_commanded_position
self.is_motor_enabled = stepper.is_motor_enabled
# Primary endstop and its position
printer = config.get_printer()
ppins = printer.lookup_object('pins')
@ -232,9 +174,6 @@ class PrinterRail:
def set_position(self, coord):
for stepper in self.steppers:
stepper.set_position(coord)
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
def LookupMultiRail(config):