From 6f685e9e01ba1581473fd340a3c3d91765626870 Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Mon, 4 Aug 2025 23:52:59 -0400 Subject: [PATCH] motion_queuing: Add allocate_stepcompress() call Allocate the low-level C stepcompress object in the motion_queuing module. This simplifies the mcu.py code as it no longer needs to track the stepqueues for the steppersync object. Signed-off-by: Kevin O'Connor --- klippy/extras/motion_queuing.py | 13 ++++++++++++- klippy/extras/pwm_tool.py | 17 ++++++++--------- klippy/mcu.py | 6 +----- klippy/stepper.py | 27 +++++++++++++-------------- 4 files changed, 34 insertions(+), 29 deletions(-) diff --git a/klippy/extras/motion_queuing.py b/klippy/extras/motion_queuing.py index 21d5465e..91060019 100644 --- a/klippy/extras/motion_queuing.py +++ b/klippy/extras/motion_queuing.py @@ -11,6 +11,7 @@ class PrinterMotionQueuing: self.printer = config.get_printer() self.steppers = [] self.trapqs = [] + self.stepcompress = [] self.steppersyncs = [] ffi_main, ffi_lib = chelper.get_ffi() self.trapq_finalize_moves = ffi_lib.trapq_finalize_moves @@ -19,7 +20,17 @@ class PrinterMotionQueuing: trapq = ffi_main.gc(ffi_lib.trapq_alloc(), ffi_lib.trapq_free) self.trapqs.append(trapq) return trapq - def allocate_steppersync(self, mcu, serialqueue, stepqueues, move_count): + def allocate_stepcompress(self, mcu, oid): + ffi_main, ffi_lib = chelper.get_ffi() + sc = ffi_main.gc(ffi_lib.stepcompress_alloc(oid), + ffi_lib.stepcompress_free) + self.stepcompress.append((mcu, sc)) + return sc + def allocate_steppersync(self, mcu, serialqueue, move_count): + stepqueues = [] + for sc_mcu, sc in self.stepcompress: + if sc_mcu is mcu: + stepqueues.append(sc) ffi_main, ffi_lib = chelper.get_ffi() ss = ffi_main.gc( ffi_lib.steppersync_alloc(serialqueue, stepqueues, len(stepqueues), diff --git a/klippy/extras/pwm_tool.py b/klippy/extras/pwm_tool.py index 53e101ae..a0739b5b 100644 --- a/klippy/extras/pwm_tool.py +++ b/klippy/extras/pwm_tool.py @@ -9,18 +9,18 @@ class error(Exception): pass class MCU_queued_pwm: - def __init__(self, pin_params): - self._mcu = pin_params['chip'] + def __init__(self, config, pin_params): + self._mcu = mcu = pin_params['chip'] self._hardware_pwm = False self._cycle_time = 0.100 self._max_duration = 2. - self._oid = self._mcu.create_oid() + self._oid = oid = mcu.create_oid() + printer = mcu.get_printer() + motion_queuing = printer.load_object(config, 'motion_queuing') + self._stepqueue = motion_queuing.allocate_stepcompress(mcu, oid) ffi_main, ffi_lib = chelper.get_ffi() - self._stepqueue = ffi_main.gc(ffi_lib.stepcompress_alloc(self._oid), - ffi_lib.stepcompress_free) - self._mcu.register_stepqueue(self._stepqueue) self._stepcompress_queue_mq_msg = ffi_lib.stepcompress_queue_mq_msg - self._mcu.register_config_callback(self._build_config) + mcu.register_config_callback(self._build_config) self._pin = pin_params['pin'] self._invert = pin_params['invert'] self._start_value = self._shutdown_value = float(self._invert) @@ -29,7 +29,6 @@ class MCU_queued_pwm: self._pwm_max = 0. self._set_cmd_tag = None self._toolhead = None - printer = self._mcu.get_printer() printer.register_event_handler("klippy:connect", self._handle_connect) def _handle_connect(self): self._toolhead = self._mcu.get_printer().lookup_object("toolhead") @@ -135,7 +134,7 @@ class PrinterOutputPin: ppins = self.printer.lookup_object('pins') # Determine pin type pin_params = ppins.lookup_pin(config.get('pin'), can_invert=True) - self.mcu_pin = MCU_queued_pwm(pin_params) + self.mcu_pin = MCU_queued_pwm(config, pin_params) max_duration = self.mcu_pin.get_mcu().max_nominal_duration() cycle_time = config.getfloat('cycle_time', 0.100, above=0., maxval=max_duration) diff --git a/klippy/mcu.py b/klippy/mcu.py index 48eee4e5..d7665036 100644 --- a/klippy/mcu.py +++ b/klippy/mcu.py @@ -605,7 +605,6 @@ class MCU: self._max_stepper_error = config.getfloat('max_stepper_error', 0.000025, minval=0.) self._reserved_move_slots = 0 - self._stepqueues = [] self._steppersync = None self._flush_callbacks = [] # Stats @@ -773,8 +772,7 @@ class MCU: ss_move_count = move_count - self._reserved_move_slots motion_queuing = self._printer.lookup_object('motion_queuing') self._steppersync = motion_queuing.allocate_steppersync( - self, self._serial.get_serialqueue(), - self._stepqueues, ss_move_count) + self, self._serial.get_serialqueue(), ss_move_count) self._ffi_lib.steppersync_set_time(self._steppersync, 0., self._mcu_freq) # Log config information @@ -971,8 +969,6 @@ class MCU: def _firmware_restart_bridge(self): self._firmware_restart(True) # Move queue tracking - def register_stepqueue(self, stepqueue): - self._stepqueues.append(stepqueue) def request_move_queue_slot(self): self._reserved_move_slots += 1 def register_flush_callback(self, callback): diff --git a/klippy/stepper.py b/klippy/stepper.py index 5ba76a5b..6ce9130f 100644 --- a/klippy/stepper.py +++ b/klippy/stepper.py @@ -19,22 +19,23 @@ MIN_OPTIMIZED_BOTH_EDGE_DURATION = 0.000000150 # Interface to low-level mcu and chelper code class MCU_stepper: - def __init__(self, name, step_pin_params, dir_pin_params, + def __init__(self, config, step_pin_params, dir_pin_params, rotation_dist, steps_per_rotation, step_pulse_duration=None, units_in_radians=False): - self._name = name + self._name = config.get_name() self._rotation_dist = rotation_dist self._steps_per_rotation = steps_per_rotation self._step_pulse_duration = step_pulse_duration self._units_in_radians = units_in_radians self._step_dist = rotation_dist / steps_per_rotation - self._mcu = step_pin_params['chip'] - self._oid = oid = self._mcu.create_oid() - self._mcu.register_config_callback(self._build_config) + self._mcu = mcu = step_pin_params['chip'] + self._oid = oid = mcu.create_oid() + mcu.register_config_callback(self._build_config) 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( + printer = mcu.get_printer() + if dir_pin_params['chip'] is not mcu: + raise printer.config_error( "Stepper dir pin must be on same mcu as step pin") self._dir_pin = dir_pin_params['pin'] self._invert_dir = self._orig_invert_dir = dir_pin_params['invert'] @@ -42,17 +43,16 @@ class MCU_stepper: self._mcu_position_offset = 0. self._reset_cmd_tag = self._get_position_cmd = None self._active_callbacks = [] + motion_queuing = printer.load_object(config, 'motion_queuing') + self._stepqueue = motion_queuing.allocate_stepcompress(mcu, oid) ffi_main, ffi_lib = chelper.get_ffi() - self._stepqueue = ffi_main.gc(ffi_lib.stepcompress_alloc(oid), - ffi_lib.stepcompress_free) ffi_lib.stepcompress_set_invert_sdir(self._stepqueue, self._invert_dir) - self._mcu.register_stepqueue(self._stepqueue) self._stepper_kinematics = None self._itersolve_generate_steps = ffi_lib.itersolve_generate_steps self._itersolve_check_active = ffi_lib.itersolve_check_active self._trapq = ffi_main.NULL - self._mcu.get_printer().register_event_handler('klippy:connect', - self._query_mcu_position) + printer.register_event_handler('klippy:connect', + self._query_mcu_position) def get_mcu(self): return self._mcu def get_name(self, short=False): @@ -258,7 +258,6 @@ class MCU_stepper: # Helper code to build a stepper object from a config section def PrinterStepper(config, units_in_radians=False): printer = config.get_printer() - name = config.get_name() # Stepper definition ppins = printer.lookup_object('pins') step_pin = config.get('step_pin') @@ -269,7 +268,7 @@ def PrinterStepper(config, units_in_radians=False): config, units_in_radians, True) step_pulse_duration = config.getfloat('step_pulse_duration', None, minval=0., maxval=.001) - mcu_stepper = MCU_stepper(name, step_pin_params, dir_pin_params, + mcu_stepper = MCU_stepper(config, step_pin_params, dir_pin_params, rotation_dist, steps_per_rotation, step_pulse_duration, units_in_radians) # Register with helper modules