247 Commits

Author SHA1 Message Date
Kevin O'Connor
7a036a6ba7 toolhead: Verify nothing attempts to pause in lookahead callbacks
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
2025-10-18 12:28:06 -04:00
Kevin O'Connor
8de426d244 toolhead: Reduce target buffer time to 1 second from 2 seconds
During normal printing the host software would attempt to stay ahead
of the micro-controller by 2 full seconds.  Change that time to 1
second.  This should make the software more responsive to user
requests (such as pause requests).

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
2025-10-10 11:07:50 -04:00
Kevin O'Connor
16fc46fe5f toolhead: Reduce LOOKAHEAD_FLUSH_TIME to 0.150 seconds
The current code is likely to perform a lazy flush of the lookahead
queue around 4 times a second.  Increase that to around 6-7 times a
second.  This change may slightly improve the responsiveness to user
requests mid-print (eg, changing extrusion ratio) and may make a
"print stall" less likely in some corner cases.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
2025-10-10 11:07:50 -04:00
Kevin O'Connor
50cb362234 toolhead: Make sure to periodically yield to other tasks when buffering moves
Normally the toolhead code will flush the lookahead buffer every
~250ms and will briefly pause to avoid buffering too much data.  That
pause allows other tasks to run.  Make sure to periodically yield to
other tasks on each lookahead buffer flush even if a delay isn't
needed.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
2025-10-10 11:07:50 -04:00
Kevin O'Connor
4415b988c1 toolhead: Clarify priming timer scheduling
Make sure each command gets an additional 100ms before flushing via
the priming timer.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
2025-10-10 11:07:50 -04:00
Kevin O'Connor
b85b92fdfb toolhead: Don't enter "Priming" state on a dwell()
After a toolhead dwell, there is no reason to enter the priming state
and to create the priming exiration timer.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
2025-10-08 23:08:36 -04:00
Kevin O'Connor
7f177aad1a toolhead: Minor code movement
Move flushing and priming code together.  No code changes - only code
movement.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
2025-10-08 23:08:36 -04:00
Kevin O'Connor
3f9733d04d toolhead: Move priming logic from _check_pause() to new _check_priming_state()
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
2025-10-08 23:08:36 -04:00
Kevin O'Connor
8fd263ca69 toolhead: Add a lookahead.is_empty() helper function
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
2025-10-08 23:08:36 -04:00
Kevin O'Connor
0c86b388a9 toolhead: Remove extra batching time added in _check_pause()
The code currently adds an additional 100ms to BUFFER_TIME_HIGH in
_check_pause() to reduce the number of calls to _check_pause().
However, LOOKAHEAD_FLUSH_TIME should already provide sufficient
batching so adding more is not necessary.  This change should
hopefully make configuring BUFFER_TIME_HIGH a little more transparent.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
2025-10-03 13:13:56 -04:00
Kevin O'Connor
4cd786fe08 toolhead: Avoid pausing an infinitesimal amount of time
Due to differences in mcu clock vs system clock it's possible to
repeatedly underestimate a system delay needed to bring about a
sufficient mcu time - which just wastes cpu cycles retrying a pause.
It's okay to sleep a slightly longer time to avoid the issue.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
2025-10-03 13:13:56 -04:00
Kevin O'Connor
6118525c19 toolhead: Allow more entries to flush from "lazy" lookahead flush
Previously the code would always keep at least 2 items on the
lookahead queue after a "lazy" flush.  In most cases it's okay to
leave only a single item.  Update the code to better handle flushing
of items that are fully ready.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
2025-09-30 09:37:35 -04:00
Kevin O'Connor
41901ec382 toolhead: Simplify LookAheadQueue.flush() code
Replace "delayed" storage with a full pass through the queue.  This
simplifies the lookahead processing logic.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
2025-09-30 09:37:35 -04:00
Kevin O'Connor
ff8c8eab55 toolhead: Clarify internal minimum_cruise_ratio variable names
Avoid using "smoothed" and "accel_to_decel" for variables associated
with minimum_cruise_ratio.  Instead introduce the prefix "mcr" for use
with these variables.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
2025-09-30 09:37:35 -04:00
Kevin O'Connor
184ba4080c toolhead: Flush lookahead on dwell - fix flushing bug after long delays
Commit 7ea5f5d2 changed how the lookahead queue is flushed.
Previously, the main flush timer would always run while the toolhead
was considered in an active state (print_time).  After that commit,
the flush timer could sleep if there were no steps generated (no call
to note_mcu_movequeue_activity() ).  This could lead to a situation
where a G4 command (or series of commands) could cause the toolhead to
be considered in an active state while the flush timer was disabled.
The result was that a future command may not be properly flushed (the
toolhead would fail to transition to a "Priming" state).

Fix by ensuring that all dwell() requests fully flush the lookahead
queue.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
2025-09-29 11:31:31 -04:00
Kevin O'Connor
636380e4f3 toolhead: Avoid numerical stability in _handle_step_flush() comparison
Don't rely on an exact floating point number match to detect when a
forced lookahead flush is needed.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
2025-09-15 19:20:00 -04:00
Kevin O'Connor
7ea5f5d25e motion_queuing: Generate steps from timer instead of from lookahead
Don't tie the step generation logic to the toolhead lookahead logic.
Instead, use regular timers to generate steps with a goal of staying
500-750ms ahead of the micro-controllers.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
2025-09-12 22:41:30 -04:00
Kevin O'Connor
cde57bdcfd toolhead: Set check_stall_time from _flush_lookahead()
Add a new is_runout parameter to _flush_lookahead() and use that in
places that could set check_stall_time.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
2025-09-12 22:41:30 -04:00
Kevin O'Connor
8a833175a5 motion_queuing: Introduce flush_all_steps() helper
Move the "full flush" code from advance_flush_time() to a new
flush_all_steps() method.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
2025-09-03 12:07:24 -04:00
Kevin O'Connor
5426943501 motion_queuing: Automatically detect changes to kin_flush_delay
Remove the toolhead note_step_generation_scan_time() code and
automatically detect the itersolve scan windows that are in use.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
2025-09-03 12:00:31 -04:00
Kevin O'Connor
d1974c0d3d motion_queuing: Move flushing logic from toolhead to motion_queuing module
Move low-level step generation timing code to the motion_queing
module.  This helps simplify the toolhead module.  It also helps
centralize the step generation code into the motion_queing module.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
2025-09-03 12:00:31 -04:00
Kevin O'Connor
8c13811c3b toolhead: Avoid using print_time when calling mcu.check_active()
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
2025-09-03 12:00:31 -04:00
Kevin O'Connor
db5cbe56d3 toolhead: Do not modify print_time in drip_update_time()
Implement drip_update_time() using _advance_flush_time() instead of
_advance_move_time().

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
2025-09-03 12:00:31 -04:00
Kevin O'Connor
872615cfcf toolhead: Add new _calc_step_gen_restart() helper
Separate out step generation specific handling from _calc_print_time()
code.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
2025-09-03 12:00:31 -04:00
Kevin O'Connor
4b9a0b4f82 toolhead: Separate lookahead timer flushing to new _check_flush_lookahead()
Separate out the lookahead specific flushing logic from the
_flush_handler() code.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
2025-09-03 12:00:31 -04:00
Kevin O'Connor
a64207aac3 toolhead: Implement flush "waves" in _advance_flush_time()
Move the code that implements flushing in waves from
_advance_move_time() to _advance_flush_time().  This also separates
print_time tracking from the _advance_flush_time() implementation.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
2025-09-03 12:00:31 -04:00
Kevin O'Connor
b0a642a8ea toolhead: Add kin_flush_delay in note_mcu_movequeue_activity()
Automatically add kin_flush_delay to the requested flush time if
is_step_gen=True.  This simplifies the callers.

Also, rename self.step_gen_time to self.need_step_gen_time.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
2025-09-03 12:00:31 -04:00
Kevin O'Connor
1e7c67919e toolhead: Rework min_restart_time to last_step_gen_time
Commit 3d3b87f9 renamed last_sg_flush_time to min_restart_time to
ensure that flush_step_generation() would fully flush out moves
generated from the force_move module.  However, now that force_move
calls note_mcu_movequeue_activity() with is_step_gen=True, this is no
longer necessary.

Rework min_restart_time to last_step_gen_time.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
2025-09-03 12:00:31 -04:00
Kevin O'Connor
a5218619b7 motion_queuing: Track kin_flush_delay locally
Track the kin_flush_delay in both toolhead.py and motion_queuing.py .

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
2025-09-03 12:00:31 -04:00
Kevin O'Connor
20d9c84a9f toolhead: Fix incorrect response message in SET_VELOCITY_LIMIT
Commit f8da8099 incorrectly changed the order of variables in the
log/response message of the SET_VELOCITY_LIMIT command.  Restore the
correct order.

Reported by @berkakinci.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
2025-09-01 21:20:00 -04:00
Kevin O'Connor
d6d8587289 motion_queuing: Remove clean_motion_queues()
Merge the clean_motion_queues() code into the existing
flush_motion_queues() code.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
2025-08-11 19:43:35 -04:00
Kevin O'Connor
b5e573957c motion_queuing: Move clear_history_time from toolhead to motion_queuing
Implement the 30 second clear_history_time offset checking directly in
the motion_queuing module.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
2025-08-11 19:43:35 -04:00
Kevin O'Connor
1d569a6631 motion_queuing: Remove flush_steppersync()
Move code from flush_steppersync() to existing flush_motion_queues()
and clean_motion_queues() functions.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
2025-08-11 19:43:35 -04:00
Kevin O'Connor
864c78f24a motion_queueing: Add flush_steppersync()
Move the mcu.flush_moves() code to motion_queuing.flush_steppersync().

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
2025-08-11 19:43:35 -04:00
Kevin O'Connor
5cbe7d83e8 motion_queuing: Track all trapqs and globally flush all trapqs
Add an allocate_trapq() helper function to facilitate the creation of
a low-level C trapq object.  Track all trapq objects and clear history
on them globally when the main motion queues are flushed.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
2025-08-11 19:43:34 -04:00
Kevin O'Connor
9399e738bc motion_queuing: Add new module to help with motion queues and flushing
Create a new module to assist with host management of motion queues.
Register all MCU_stepper objects with this module and use the module
for step generation.

All steppers will now automatically generate steps whenever
toolhead._advance_flush_time() is invoked.  It is no longer necessary
for callers to individually call stepper.generate_steps().

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
2025-08-11 19:43:34 -04:00
Kevin O'Connor
126275d1f4 toolhead: Default note_mcu_movequeue_activity() to set step_gen_time
Change note_mcu_movequeue_activity() to default to setting the
step_gen_time (instead of the previous default to not set it).

Most users of the mcu "move queue" will be for stepper activity.
There is also little harm in incrementing the tracking of the last
possible step generation time, but accidentally generating a step
without incrementing the tracking can lead to very hard to debug
failures.

The two cases (output_pin.py and pwm_tool.py) where
note_mcu_movequeue_activity() is called and definitely not related to
step generation can explicitly pass 'is_step_gen=False'.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
2025-08-11 19:43:34 -04:00
Kevin O'Connor
f8da8099d5 toolhead: Move g-code command handlers to new ToolHeadCommandHelper() class
Move the g-code command handlers to a new class.  This reduces the
size of the main Toolhead() class.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
2025-08-11 19:43:34 -04:00
Kevin O'Connor
bcd4510958 toolhead: Move extra module loading out of core Toolhead() class
Load these extra modules from add_printer_objects() instead.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
2025-08-11 19:43:34 -04:00
Kevin O'Connor
3ef760c18f toolhead: Remove support for max_accel_to_decel
This support was deprecated on 20240313.  Remove the remaining
compatibility code.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
2025-08-11 19:14:37 -04:00
Kevin O'Connor
bb281834b0 toolhead: Initial support for adding extra axes to toolhead moves
Add a new add_extra_axes() to support adding additional axes.  Once
called, toolhead.get_position() will return a list object with more
than 4 items, and toolhead.move() requires the same size list.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
2025-05-12 20:15:03 -04:00
Kevin O'Connor
ae536b4786 toolhead: Only alter XYZ coordinates on set_position() calls
It's not valid to alter the extruder position from a call to
set_position(), so don't allow callers to attempt that.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
2025-05-12 20:15:03 -04:00
Kevin O'Connor
8928c394cf toolhead: Support unregister_step_generator() call
Allow both registering and unregistering step generation callbacks.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
2025-05-12 20:15:03 -04:00
Kevin O'Connor
01422da951 extruder: Remove update_move_time() call
The toolhead can obtain the underlying extruder trapq via
extruder.get_trapq().

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
2025-05-12 20:15:03 -04:00
Kevin O'Connor
f06eeb5c7a extruder: Rename extruder.move() to extruder.process_move()
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
2025-05-12 20:15:03 -04:00
Kevin O'Connor
db7a9cf071 manual_stepper: Implement "drip moves" for manual stepper STOP_ON_ENDSTOP
Currently, `MANUAL_STEPPER STOP_ON_ENDSTOP=1` type commands will move
until hitting the endstop, but it will still always consume the total
amount of move time.  That is, following moves can't be started until
the total possible time of the homing move is completed.

Implement "drip moves" so that the code only schedules the movement in
small segments.  This allows following movements to be scheduled
without a significant delay.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
2025-04-18 14:32:23 -04:00
Kevin O'Connor
765de72f9e toolhead: Avoid toolhead.move() and toolhead._process_moves() in drip_move()
Implement move checking and trapq loading directly from drip_move().
This simplifies the interactions between these components.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
2025-04-18 14:32:23 -04:00
Kevin O'Connor
6202a0f3bc toolhead: Avoid LookAheadQueue calling back into toolhead class
Avoid lookahead.flush() calling back into toolhead._process_moves().
Instead, rename toolhead._process_moves() to
toolhead._process_lookahead(), have it call lookahead.flush(), and
consistently use it when flushing the lookahead queue.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
2025-04-18 14:32:23 -04:00
Branden Cash
d57fe4395e garbage_collection: freeze objects on klippy ready (#6794)
This significantly reduces the amount of data in the generation 2 garbage collection bucket from the initial startup of klipper.

Signed-off-by: Branden Cash <203336+ammmze@users.noreply.github.com>
2025-02-02 18:40:43 -05:00
Kevin O'Connor
4aa550837f toolhead: Pass set_position() homing_axes parameter as a string
Use strings such as "xyz" to specify which axes are to be considered
homing during a set_position() call.  This makes the parameter a
little less cryptic.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
2025-01-21 18:58:23 -05:00