10 Commits

Author SHA1 Message Date
Kevin O'Connor
037a59d38f 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-08-20 17:21:27 -04:00
Kevin O'Connor
2d489e85de motion_queuing: Move flushing logic from toolhead.py to motion_queuing module
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
2025-08-20 17:21:27 -04:00
Kevin O'Connor
a1e3964878 toolhead: Avoid using print_time when calling mcu.check_active()
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
2025-08-20 17:21:27 -04:00
Kevin O'Connor
2a15a3011b 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-08-20 17:21:27 -04:00
Kevin O'Connor
135e992031 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-08-20 17:21:27 -04:00
Kevin O'Connor
d6a7f406e2 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-08-20 17:21:27 -04:00
Kevin O'Connor
d8d29b46c5 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-08-20 17:21:27 -04:00
Kevin O'Connor
89584ff2a3 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-08-20 17:21:27 -04:00
Kevin O'Connor
f04e5f82e8 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-08-20 17:21:27 -04:00
Kevin O'Connor
ea05c40449 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-08-20 17:21:27 -04:00
14 changed files with 250 additions and 185 deletions

View File

@@ -56,6 +56,8 @@ defs_stepcompress = """
, uint64_t start_clock, uint64_t end_clock);
void stepcompress_set_stepper_kinematics(struct stepcompress *sc
, struct stepper_kinematics *sk);
struct stepper_kinematics *stepcompress_get_stepper_kinematics(
struct stepcompress *sc);
"""
defs_steppersync = """
@@ -76,11 +78,14 @@ defs_itersolve = """
int32_t itersolve_is_active_axis(struct stepper_kinematics *sk, char axis);
void itersolve_set_trapq(struct stepper_kinematics *sk, struct trapq *tq
, double step_dist);
struct trapq *itersolve_get_trapq(struct stepper_kinematics *sk);
double itersolve_calc_position_from_coord(struct stepper_kinematics *sk
, double x, double y, double z);
void itersolve_set_position(struct stepper_kinematics *sk
, double x, double y, double z);
double itersolve_get_commanded_pos(struct stepper_kinematics *sk);
double itersolve_get_gen_steps_pre_active(struct stepper_kinematics *sk);
double itersolve_get_gen_steps_post_active(struct stepper_kinematics *sk);
"""
defs_trapq = """
@@ -157,8 +162,6 @@ defs_kin_extruder = """
"""
defs_kin_shaper = """
double input_shaper_get_step_generation_window(
struct stepper_kinematics *sk);
int input_shaper_set_shaper_params(struct stepper_kinematics *sk, char axis
, int n, double a[], double t[]);
int input_shaper_set_sk(struct stepper_kinematics *sk

View File

@@ -248,6 +248,12 @@ itersolve_set_trapq(struct stepper_kinematics *sk, struct trapq *tq
sk->step_dist = step_dist;
}
struct trapq * __visible
itersolve_get_trapq(struct stepper_kinematics *sk)
{
return sk->tq;
}
double __visible
itersolve_calc_position_from_coord(struct stepper_kinematics *sk
, double x, double y, double z)
@@ -273,3 +279,15 @@ itersolve_get_commanded_pos(struct stepper_kinematics *sk)
{
return sk->commanded_pos;
}
double __visible
itersolve_get_gen_steps_pre_active(struct stepper_kinematics *sk)
{
return sk->gen_steps_pre_active;
}
double __visible
itersolve_get_gen_steps_post_active(struct stepper_kinematics *sk)
{
return sk->gen_steps_post_active;
}

View File

@@ -31,10 +31,13 @@ double itersolve_check_active(struct stepper_kinematics *sk, double flush_time);
int32_t itersolve_is_active_axis(struct stepper_kinematics *sk, char axis);
void itersolve_set_trapq(struct stepper_kinematics *sk, struct trapq *tq
, double step_dist);
struct trapq *itersolve_get_trapq(struct stepper_kinematics *sk);
double itersolve_calc_position_from_coord(struct stepper_kinematics *sk
, double x, double y, double z);
void itersolve_set_position(struct stepper_kinematics *sk
, double x, double y, double z);
double itersolve_get_commanded_pos(struct stepper_kinematics *sk);
double itersolve_get_gen_steps_pre_active(struct stepper_kinematics *sk);
double itersolve_get_gen_steps_post_active(struct stepper_kinematics *sk);
#endif // itersolve.h

View File

@@ -239,14 +239,6 @@ input_shaper_set_shaper_params(struct stepper_kinematics *sk, char axis
return status;
}
double __visible
input_shaper_get_step_generation_window(struct stepper_kinematics *sk)
{
struct input_shaper *is = container_of(sk, struct input_shaper, sk);
return is->sk.gen_steps_pre_active > is->sk.gen_steps_post_active
? is->sk.gen_steps_pre_active : is->sk.gen_steps_post_active;
}
struct stepper_kinematics * __visible
input_shaper_alloc(void)
{

View File

@@ -674,6 +674,13 @@ stepcompress_set_stepper_kinematics(struct stepcompress *sc
sc->sk = sk;
}
// Report current stepper_kinematics
struct stepper_kinematics * __visible
stepcompress_get_stepper_kinematics(struct stepcompress *sc)
{
return sc->sk;
}
// Generate steps (via itersolve) and flush
int32_t
stepcompress_generate_steps(struct stepcompress *sc, double gen_steps_time

View File

@@ -41,6 +41,8 @@ int stepcompress_extract_old(struct stepcompress *sc
struct stepper_kinematics;
void stepcompress_set_stepper_kinematics(struct stepcompress *sc
, struct stepper_kinematics *sk);
struct stepper_kinematics *stepcompress_get_stepper_kinematics(
struct stepcompress *sc);
int32_t stepcompress_generate_steps(struct stepcompress *sc
, double gen_steps_time
, uint64_t flush_clock);

View File

@@ -85,7 +85,7 @@ class ForceMove:
self.trapq_append(self.trapq, print_time, accel_t, cruise_t, accel_t,
0., 0., 0., axis_r, 0., 0., 0., cruise_v, accel)
print_time = print_time + accel_t + cruise_t + accel_t
toolhead.note_mcu_movequeue_activity(print_time)
self.motion_queuing.note_mcu_movequeue_activity(print_time)
toolhead.dwell(accel_t + cruise_t + accel_t)
toolhead.flush_step_generation()
stepper.set_trapq(prev_trapq)

View File

@@ -146,12 +146,8 @@ class InputShaper:
is_sk = self._get_input_shaper_stepper_kinematics(s)
if is_sk is None:
continue
old_delay = ffi_lib.input_shaper_get_step_generation_window(is_sk)
self.toolhead.flush_step_generation()
ffi_lib.input_shaper_update_sk(is_sk)
new_delay = ffi_lib.input_shaper_get_step_generation_window(is_sk)
if old_delay != new_delay:
self.toolhead.note_step_generation_scan_time(new_delay,
old_delay)
def _update_input_shaping(self, error=None):
self.toolhead.flush_step_generation()
ffi_main, ffi_lib = chelper.get_ffi()
@@ -163,16 +159,11 @@ class InputShaper:
is_sk = self._get_input_shaper_stepper_kinematics(s)
if is_sk is None:
continue
old_delay = ffi_lib.input_shaper_get_step_generation_window(is_sk)
for shaper in self.shapers:
if shaper in failed_shapers:
continue
if not shaper.set_shaper_kinematics(is_sk):
failed_shapers.append(shaper)
new_delay = ffi_lib.input_shaper_get_step_generation_window(is_sk)
if old_delay != new_delay:
self.toolhead.note_step_generation_scan_time(new_delay,
old_delay)
if failed_shapers:
error = error or self.printer.command_error
raise error("Failed to configure shaper(s) %s with given parameters"

View File

@@ -80,8 +80,7 @@ class ManualStepper:
self.sync_print_time()
self.next_cmd_time = self._submit_move(self.next_cmd_time, movepos,
speed, accel)
toolhead = self.printer.lookup_object('toolhead')
toolhead.note_mcu_movequeue_activity(self.next_cmd_time)
self.motion_queuing.note_mcu_movequeue_activity(self.next_cmd_time)
if sync:
self.sync_print_time()
def do_homing_move(self, movepos, speed, accel, triggered, check_trigger):
@@ -205,11 +204,12 @@ class ManualStepper:
def drip_move(self, newpos, speed, drip_completion):
# Submit move to trapq
self.sync_print_time()
maxtime = self._submit_move(self.next_cmd_time, newpos[0],
speed, self.homing_accel)
start_time = self.next_cmd_time
end_time = self._submit_move(start_time, newpos[0],
speed, self.homing_accel)
# Drip updates to motors
toolhead = self.printer.lookup_object('toolhead')
toolhead.drip_update_time(maxtime, drip_completion)
self.motion_queuing.drip_update_time(start_time, end_time,
drip_completion)
# Clear trapq of any remaining parts of movement
reactor = self.printer.get_reactor()
self.motion_queuing.wipe_trapq(self.trapq)

View File

@@ -6,23 +6,53 @@
import logging
import chelper
BGFLUSH_LOW_TIME = 0.200
BGFLUSH_BATCH_TIME = 0.200
BGFLUSH_EXTRA_TIME = 0.250
MOVE_HISTORY_EXPIRE = 30.
MIN_KIN_TIME = 0.100
MOVE_BATCH_TIME = 0.500
STEPCOMPRESS_FLUSH_TIME = 0.050
SDS_CHECK_TIME = 0.001 # step+dir+step filter in stepcompress.c
DRIP_SEGMENT_TIME = 0.050
DRIP_TIME = 0.100
class PrinterMotionQueuing:
def __init__(self, config):
self.printer = config.get_printer()
self.printer = printer = config.get_printer()
self.reactor = printer.get_reactor()
# Low level C allocations
self.trapqs = []
self.stepcompress = []
self.steppersyncs = []
self.flush_callbacks = []
# Low-level C flushing calls
ffi_main, ffi_lib = chelper.get_ffi()
self.trapq_finalize_moves = ffi_lib.trapq_finalize_moves
self.steppersync_generate_steps = ffi_lib.steppersync_generate_steps
self.steppersync_flush = ffi_lib.steppersync_flush
self.steppersync_history_expire = ffi_lib.steppersync_history_expire
# Flush notification callbacks
self.flush_callbacks = []
# History expiration
self.clear_history_time = 0.
is_debug = self.printer.get_start_args().get('debugoutput') is not None
self.is_debugoutput = is_debug
# Flush tracking
self.flush_timer = self.reactor.register_timer(self._flush_handler)
self.do_kick_flush_timer = True
self.last_flush_time = self.last_step_gen_time = 0.
self.need_flush_time = self.need_step_gen_time = 0.
self.check_flush_lookahead_cb = (lambda e: None)
# MCU tracking
self.all_mcus = [m for n, m in printer.lookup_objects(module='mcu')]
self.mcu = self.all_mcus[0]
self.can_pause = True
if self.mcu.is_fileoutput():
self.can_pause = False
# Kinematic step generation scan window time tracking
self.need_calc_kin_flush_delay = True
self.kin_flush_delay = SDS_CHECK_TIME
# Register handlers
printer.register_event_handler("klippy:shutdown", self._handle_shutdown)
def allocate_trapq(self):
ffi_main, ffi_lib = chelper.get_ffi()
trapq = ffi_main.gc(ffi_lib.trapq_alloc(), ffi_lib.trapq_free)
@@ -53,8 +83,7 @@ class PrinterMotionQueuing:
fcbs = list(self.flush_callbacks)
fcbs.remove(callback)
self.flush_callbacks = fcbs
def flush_motion_queues(self, must_flush_time, max_step_gen_time,
trapq_free_time):
def _flush_motion_queues(self, must_flush_time, max_step_gen_time):
# Invoke flush callbacks (if any)
for cb in self.flush_callbacks:
cb(must_flush_time, max_step_gen_time)
@@ -72,8 +101,9 @@ class PrinterMotionQueuing:
raise mcu.error("Internal error in MCU '%s' stepcompress"
% (mcu.get_name(),))
# Determine maximum history to keep
trapq_free_time = max_step_gen_time - self.kin_flush_delay
clear_history_time = self.clear_history_time
if self.is_debugoutput:
if not self.can_pause:
clear_history_time = trapq_free_time - MOVE_HISTORY_EXPIRE
# Move processed trapq moves to history list, and expire old history
for trapq in self.trapqs:
@@ -85,16 +115,128 @@ class PrinterMotionQueuing:
self.steppersync_history_expire(ss, clock)
def wipe_trapq(self, trapq):
# Expire any remaining movement in the trapq (force to history list)
NEVER = 9999999999999999.
self.trapq_finalize_moves(trapq, NEVER, 0.)
self.trapq_finalize_moves(trapq, self.reactor.NEVER, 0.)
def lookup_trapq_append(self):
ffi_main, ffi_lib = chelper.get_ffi()
return ffi_lib.trapq_append
def stats(self, eventtime):
mcu = self.printer.lookup_object('mcu')
est_print_time = mcu.estimated_print_time(eventtime)
# Hack to globally invoke mcu check_active()
for m in self.all_mcus:
m.check_active(self.last_step_gen_time, eventtime)
# Calculate history expiration
est_print_time = self.mcu.estimated_print_time(eventtime)
self.clear_history_time = est_print_time - MOVE_HISTORY_EXPIRE
return False, ""
# Kinematic step generation scan window time tracking
def get_kin_flush_delay(self):
return self.kin_flush_delay
def _calc_kin_flush_delay(self):
self.need_calc_kin_flush_delay = False
ffi_main, ffi_lib = chelper.get_ffi()
kin_flush_delay = SDS_CHECK_TIME
for mcu, sc in self.stepcompress:
sk = ffi_lib.stepcompress_get_stepper_kinematics(sc)
if sk == ffi_main.NULL:
continue
trapq = ffi_lib.itersolve_get_trapq(sk)
if trapq == ffi_main.NULL:
continue
pre_active = ffi_lib.itersolve_get_gen_steps_pre_active(sk)
post_active = ffi_lib.itersolve_get_gen_steps_post_active(sk)
kin_flush_delay = max(kin_flush_delay, pre_active, post_active)
self.kin_flush_delay = kin_flush_delay
# Flush tracking
def _handle_shutdown(self):
self.can_pause = False
def setup_lookahead_flush_callback(self, check_flush_lookahead_cb):
self.check_flush_lookahead_cb = check_flush_lookahead_cb
def advance_flush_time(self, target_time=None, lazy_target=False):
if target_time is None:
# This is a full flush
target_time = self.need_step_gen_time
self.need_calc_kin_flush_delay = True
want_flush_time = want_step_gen_time = target_time
if lazy_target:
# Account for step gen scan windows and optimize step compression
want_step_gen_time -= self.kin_flush_delay
want_flush_time = want_step_gen_time - STEPCOMPRESS_FLUSH_TIME
want_flush_time = max(want_flush_time, self.last_flush_time)
flush_time = self.last_flush_time
if want_flush_time > flush_time + 10. * MOVE_BATCH_TIME:
# Use closer startup time when coming out of idle state
curtime = self.reactor.monotonic()
est_print_time = self.mcu.estimated_print_time(curtime)
flush_time = max(flush_time, est_print_time)
while 1:
flush_time = min(flush_time + MOVE_BATCH_TIME, want_flush_time)
# Generate steps via itersolve
want_sg_wave = min(flush_time + STEPCOMPRESS_FLUSH_TIME,
want_step_gen_time)
step_gen_time = max(self.last_step_gen_time, want_sg_wave,
flush_time)
self._flush_motion_queues(flush_time, step_gen_time)
self.last_flush_time = flush_time
self.last_step_gen_time = step_gen_time
if flush_time >= want_flush_time:
break
def calc_step_gen_restart(self, est_print_time):
if self.need_calc_kin_flush_delay:
self._calc_kin_flush_delay()
kin_time = max(est_print_time + MIN_KIN_TIME, self.last_step_gen_time)
return kin_time + self.kin_flush_delay
def _flush_handler(self, eventtime):
try:
# Check if flushing is done via lookahead queue
ret = self.check_flush_lookahead_cb(eventtime)
if ret is not None:
return ret
# Flush motion queues
est_print_time = self.mcu.estimated_print_time(eventtime)
while 1:
end_flush = self.need_flush_time + BGFLUSH_EXTRA_TIME
if self.last_flush_time >= end_flush:
self.do_kick_flush_timer = True
return self.reactor.NEVER
buffer_time = self.last_flush_time - est_print_time
if buffer_time > BGFLUSH_LOW_TIME:
return eventtime + buffer_time - BGFLUSH_LOW_TIME
ftime = est_print_time + BGFLUSH_LOW_TIME + BGFLUSH_BATCH_TIME
self.advance_flush_time(min(end_flush, ftime))
except:
logging.exception("Exception in flush_handler")
self.printer.invoke_shutdown("Exception in flush_handler")
return self.reactor.NEVER
def note_mcu_movequeue_activity(self, mq_time, is_step_gen=True):
if is_step_gen:
mq_time += self.kin_flush_delay
self.need_step_gen_time = max(self.need_step_gen_time, mq_time)
self.need_flush_time = max(self.need_flush_time, mq_time)
if self.do_kick_flush_timer:
self.do_kick_flush_timer = False
self.reactor.update_timer(self.flush_timer, self.reactor.NOW)
def drip_update_time(self, start_time, end_time, drip_completion):
# Disable background flushing from timer
self.reactor.update_timer(self.flush_timer, self.reactor.NEVER)
self.do_kick_flush_timer = False
# Flush in segments until drip_completion signal
flush_delay = DRIP_TIME + STEPCOMPRESS_FLUSH_TIME + self.kin_flush_delay
flush_time = start_time
while flush_time < end_time:
if drip_completion.test():
break
curtime = self.reactor.monotonic()
est_print_time = self.mcu.estimated_print_time(curtime)
wait_time = flush_time - est_print_time - flush_delay
if wait_time > 0. and self.can_pause:
# Pause before sending more steps
drip_completion.wait(curtime + wait_time)
continue
flush_time = min(flush_time + DRIP_SEGMENT_TIME, end_time)
self.note_mcu_movequeue_activity(flush_time)
self.advance_flush_time(flush_time, lazy_target=True)
# Restore background flushing
self.reactor.update_timer(self.flush_timer, self.reactor.NOW)
self.advance_flush_time()
def load_config(config):
return PrinterMotionQueuing(config)

View File

@@ -20,8 +20,8 @@ class GCodeRequestQueue:
self.rqueue = []
self.next_min_flush_time = 0.
self.toolhead = None
motion_queuing = printer.load_object(config, 'motion_queuing')
motion_queuing.register_flush_callback(self._flush_notification)
self.motion_queuing = printer.load_object(config, 'motion_queuing')
self.motion_queuing.register_flush_callback(self._flush_notification)
printer.register_event_handler("klippy:connect", self._handle_connect)
def _handle_connect(self):
self.toolhead = self.printer.lookup_object('toolhead')
@@ -51,11 +51,12 @@ class GCodeRequestQueue:
del rqueue[:pos+1]
self.next_min_flush_time = next_time + max(min_wait, min_sched_time)
# Ensure following queue items are flushed
self.toolhead.note_mcu_movequeue_activity(self.next_min_flush_time,
is_step_gen=False)
self.motion_queuing.note_mcu_movequeue_activity(
self.next_min_flush_time, is_step_gen=False)
def _queue_request(self, print_time, value):
self.rqueue.append((print_time, value))
self.toolhead.note_mcu_movequeue_activity(print_time, is_step_gen=False)
self.motion_queuing.note_mcu_movequeue_activity(
print_time, is_step_gen=False)
def queue_gcode_request(self, value):
self.toolhead.register_lookahead_callback(
(lambda pt: self._queue_request(pt, value)))

View File

@@ -16,8 +16,8 @@ class MCU_queued_pwm:
self._max_duration = 2.
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)
self._motion_queuing = printer.load_object(config, 'motion_queuing')
self._stepqueue = self._motion_queuing.allocate_stepcompress(mcu, oid)
ffi_main, ffi_lib = chelper.get_ffi()
self._stepcompress_queue_mq_msg = ffi_lib.stepcompress_queue_mq_msg
mcu.register_config_callback(self._build_config)
@@ -62,8 +62,8 @@ class MCU_queued_pwm:
if self._duration_ticks >= 1<<31:
raise config_error("PWM pin max duration too large")
if self._duration_ticks:
motion_queuing = printer.lookup_object('motion_queuing')
motion_queuing.register_flush_callback(self._flush_notification)
self._motion_queuing.register_flush_callback(
self._flush_notification)
if self._hardware_pwm:
self._pwm_max = self._mcu.get_constant_float("PWM_MAX")
self._default_value = self._shutdown_value * self._pwm_max
@@ -116,8 +116,8 @@ class MCU_queued_pwm:
# Continue flushing to resend time
wakeclock += self._duration_ticks
wake_print_time = self._mcu.clock_to_print_time(wakeclock)
self._toolhead.note_mcu_movequeue_activity(wake_print_time,
is_step_gen=False)
self._motion_queuing.note_mcu_movequeue_activity(wake_print_time,
is_step_gen=False)
def set_pwm(self, print_time, value):
clock = self._mcu.print_time_to_clock(print_time)
if self._invert:

View File

@@ -69,14 +69,16 @@ class ExtruderStepper:
if not pressure_advance:
new_smooth_time = 0.
toolhead = self.printer.lookup_object("toolhead")
if new_smooth_time != old_smooth_time:
toolhead.note_step_generation_scan_time(
new_smooth_time * .5, old_delay=old_smooth_time * .5)
ffi_main, ffi_lib = chelper.get_ffi()
espa = ffi_lib.extruder_set_pressure_advance
toolhead.register_lookahead_callback(
lambda print_time: espa(self.sk_extruder, print_time,
pressure_advance, new_smooth_time))
if new_smooth_time != old_smooth_time:
# Need full kinematic flush to change the smooth time
toolhead.flush_step_generation()
espa(self.sk_extruder, 0., pressure_advance, new_smooth_time)
else:
toolhead.register_lookahead_callback(
lambda print_time: espa(self.sk_extruder, print_time,
pressure_advance, new_smooth_time))
self.pressure_advance = pressure_advance
self.pressure_advance_smooth_time = smooth_time
cmd_SET_PRESSURE_ADVANCE_help = "Set pressure advance parameters"

View File

@@ -193,25 +193,13 @@ class LookAheadQueue:
BUFFER_TIME_LOW = 1.0
BUFFER_TIME_HIGH = 2.0
BUFFER_TIME_START = 0.250
BGFLUSH_LOW_TIME = 0.200
BGFLUSH_BATCH_TIME = 0.200
BGFLUSH_EXTRA_TIME = 0.250
MIN_KIN_TIME = 0.100
MOVE_BATCH_TIME = 0.500
STEPCOMPRESS_FLUSH_TIME = 0.050
SDS_CHECK_TIME = 0.001 # step+dir+step filter in stepcompress.c
DRIP_SEGMENT_TIME = 0.050
DRIP_TIME = 0.100
# Main code to track events (and their timing) on the printer toolhead
class ToolHead:
def __init__(self, config):
self.printer = config.get_printer()
self.reactor = self.printer.get_reactor()
self.all_mcus = [
m for n, m in self.printer.lookup_objects(module='mcu')]
self.mcu = self.all_mcus[0]
self.mcu = self.printer.lookup_object('mcu')
self.lookahead = LookAheadQueue()
self.lookahead.set_flush_time(BUFFER_TIME_HIGH)
self.commanded_pos = [0., 0., 0., 0.]
@@ -236,16 +224,10 @@ class ToolHead:
self.print_time = 0.
self.special_queuing_state = "NeedPrime"
self.priming_timer = None
# Flush tracking
self.flush_timer = self.reactor.register_timer(self._flush_handler)
self.do_kick_flush_timer = True
self.last_flush_time = self.min_restart_time = 0.
self.need_flush_time = self.step_gen_time = 0.
# Kinematic step generation scan window time tracking
self.kin_flush_delay = SDS_CHECK_TIME
self.kin_flush_times = []
# Setup for generating moves
self.motion_queuing = self.printer.load_object(config, 'motion_queuing')
self.motion_queuing.setup_lookahead_flush_callback(
self._check_flush_lookahead)
self.trapq = self.motion_queuing.allocate_trapq()
self.trapq_append = self.motion_queuing.lookup_trapq_append()
# Create kinematics class
@@ -268,33 +250,15 @@ class ToolHead:
# Register handlers
self.printer.register_event_handler("klippy:shutdown",
self._handle_shutdown)
# Print time and flush tracking
def _advance_flush_time(self, flush_time):
flush_time = max(flush_time, self.last_flush_time)
# Generate steps via itersolve
sg_flush_want = min(flush_time + STEPCOMPRESS_FLUSH_TIME,
self.print_time - self.kin_flush_delay)
sg_flush_time = max(sg_flush_want, flush_time)
trapq_free_time = sg_flush_time - self.kin_flush_delay
self.motion_queuing.flush_motion_queues(flush_time, sg_flush_time,
trapq_free_time)
self.min_restart_time = max(self.min_restart_time, sg_flush_time)
self.last_flush_time = flush_time
# Print time tracking
def _advance_move_time(self, next_print_time):
pt_delay = self.kin_flush_delay + STEPCOMPRESS_FLUSH_TIME
flush_time = max(self.last_flush_time, self.print_time - pt_delay)
self.print_time = max(self.print_time, next_print_time)
want_flush_time = max(flush_time, self.print_time - pt_delay)
while 1:
flush_time = min(flush_time + MOVE_BATCH_TIME, want_flush_time)
self._advance_flush_time(flush_time)
if flush_time >= want_flush_time:
break
self.motion_queuing.advance_flush_time(self.print_time,
lazy_target=True)
def _calc_print_time(self):
curtime = self.reactor.monotonic()
est_print_time = self.mcu.estimated_print_time(curtime)
kin_time = max(est_print_time + MIN_KIN_TIME, self.min_restart_time)
kin_time += self.kin_flush_delay
kin_time = self.motion_queuing.calc_step_gen_restart(est_print_time)
min_print_time = max(est_print_time + BUFFER_TIME_START, kin_time)
if min_print_time > self.print_time:
self.print_time = min_print_time
@@ -328,10 +292,10 @@ class ToolHead:
for cb in move.timing_callbacks:
cb(next_move_time)
# Generate steps for moves
self.note_mcu_movequeue_activity(next_move_time + self.kin_flush_delay)
self.motion_queuing.note_mcu_movequeue_activity(next_move_time)
self._advance_move_time(next_move_time)
def _flush_lookahead(self):
# Transit from "NeedPrime"/"Priming"/"Drip"/main state to "NeedPrime"
# Transit from "NeedPrime"/"Priming"/main state to "NeedPrime"
self._process_lookahead()
self.special_queuing_state = "NeedPrime"
self.need_check_pause = -1.
@@ -339,8 +303,7 @@ class ToolHead:
self.check_stall_time = 0.
def flush_step_generation(self):
self._flush_lookahead()
self._advance_flush_time(self.step_gen_time)
self.min_restart_time = max(self.min_restart_time, self.print_time)
self.motion_queuing.advance_flush_time()
def get_last_move_time(self):
if self.special_queuing_state:
self._flush_lookahead()
@@ -391,35 +354,21 @@ class ToolHead:
logging.exception("Exception in priming_handler")
self.printer.invoke_shutdown("Exception in priming_handler")
return self.reactor.NEVER
def _flush_handler(self, eventtime):
try:
est_print_time = self.mcu.estimated_print_time(eventtime)
if not self.special_queuing_state:
# In "main" state - flush lookahead if buffer runs low
print_time = self.print_time
buffer_time = print_time - est_print_time
if buffer_time > BUFFER_TIME_LOW:
# Running normally - reschedule check
return eventtime + buffer_time - BUFFER_TIME_LOW
# Under ran low buffer mark - flush lookahead queue
self._flush_lookahead()
if print_time != self.print_time:
self.check_stall_time = self.print_time
# In "NeedPrime"/"Priming" state - flush queues if needed
while 1:
end_flush = self.need_flush_time + BGFLUSH_EXTRA_TIME
if self.last_flush_time >= end_flush:
self.do_kick_flush_timer = True
return self.reactor.NEVER
buffer_time = self.last_flush_time - est_print_time
if buffer_time > BGFLUSH_LOW_TIME:
return eventtime + buffer_time - BGFLUSH_LOW_TIME
ftime = est_print_time + BGFLUSH_LOW_TIME + BGFLUSH_BATCH_TIME
self._advance_flush_time(min(end_flush, ftime))
except:
logging.exception("Exception in flush_handler")
self.printer.invoke_shutdown("Exception in flush_handler")
return self.reactor.NEVER
def _check_flush_lookahead(self, eventtime):
if self.special_queuing_state:
return None
# In "main" state - flush lookahead if buffer runs low
est_print_time = self.mcu.estimated_print_time(eventtime)
print_time = self.print_time
buffer_time = print_time - est_print_time
if buffer_time > BUFFER_TIME_LOW:
# Running normally - reschedule check
return eventtime + buffer_time - BUFFER_TIME_LOW
# Under ran low buffer mark - flush lookahead queue
self._flush_lookahead()
if print_time != self.print_time:
self.check_stall_time = self.print_time
return None
# Movement commands
def get_position(self):
return list(self.commanded_pos)
@@ -491,32 +440,6 @@ class ToolHead:
def get_extra_axes(self):
return [None, None, None] + self.extra_axes
# Homing "drip move" handling
def drip_update_time(self, next_print_time, drip_completion):
# Transition from "NeedPrime"/"Priming"/main state to "Drip" state
self.special_queuing_state = "Drip"
self.need_check_pause = self.reactor.NEVER
self.reactor.update_timer(self.flush_timer, self.reactor.NEVER)
self.do_kick_flush_timer = False
self.lookahead.set_flush_time(BUFFER_TIME_HIGH)
self.check_stall_time = 0.
# Update print_time in segments until drip_completion signal
flush_delay = DRIP_TIME + STEPCOMPRESS_FLUSH_TIME + self.kin_flush_delay
while self.print_time < next_print_time:
if drip_completion.test():
break
curtime = self.reactor.monotonic()
est_print_time = self.mcu.estimated_print_time(curtime)
wait_time = self.print_time - est_print_time - flush_delay
if wait_time > 0. and self.can_pause:
# Pause before sending more steps
drip_completion.wait(curtime + wait_time)
continue
npt = min(self.print_time + DRIP_SEGMENT_TIME, next_print_time)
self.note_mcu_movequeue_activity(npt + self.kin_flush_delay)
self._advance_move_time(npt)
# Exit "Drip" state
self.reactor.update_timer(self.flush_timer, self.reactor.NOW)
self.flush_step_generation()
def _drip_load_trapq(self, submit_move):
# Queue move into trapezoid motion queue (trapq)
if submit_move.move_d:
@@ -524,18 +447,17 @@ class ToolHead:
self.lookahead.add_move(submit_move)
moves = self.lookahead.flush()
self._calc_print_time()
next_move_time = self.print_time
start_time = end_time = self.print_time
for move in moves:
self.trapq_append(
self.trapq, next_move_time,
self.trapq, end_time,
move.accel_t, move.cruise_t, move.decel_t,
move.start_pos[0], move.start_pos[1], move.start_pos[2],
move.axes_r[0], move.axes_r[1], move.axes_r[2],
move.start_v, move.cruise_v, move.accel)
next_move_time = (next_move_time + move.accel_t
+ move.cruise_t + move.decel_t)
end_time = end_time + move.accel_t + move.cruise_t + move.decel_t
self.lookahead.reset()
return next_move_time
return start_time, end_time
def drip_move(self, newpos, speed, drip_completion):
# Create and verify move is valid
newpos = newpos[:3] + self.commanded_pos[3:]
@@ -543,23 +465,20 @@ class ToolHead:
if move.move_d:
self.kin.check_move(move)
# Make sure stepper movement doesn't start before nominal start time
self.dwell(self.kin_flush_delay)
kin_flush_delay = self.motion_queuing.get_kin_flush_delay()
self.dwell(kin_flush_delay)
# Transmit move in "drip" mode
self._process_lookahead()
next_move_time = self._drip_load_trapq(move)
self.drip_update_time(next_move_time, drip_completion)
start_time, end_time = self._drip_load_trapq(move)
self.motion_queuing.drip_update_time(start_time, end_time,
drip_completion)
# Move finished; cleanup any remnants on trapq
self.motion_queuing.wipe_trapq(self.trapq)
# Misc commands
def stats(self, eventtime):
max_queue_time = max(self.print_time, self.last_flush_time)
for m in self.all_mcus:
m.check_active(max_queue_time, eventtime)
est_print_time = self.mcu.estimated_print_time(eventtime)
buffer_time = self.print_time - est_print_time
is_active = buffer_time > -60. or not self.special_queuing_state
if self.special_queuing_state == "Drip":
buffer_time = 0.
return is_active, "print_time=%.3f buffer_time=%.3f print_stall=%d" % (
self.print_time, max(buffer_time, 0.), self.print_stall)
def check_busy(self, eventtime):
@@ -588,27 +507,12 @@ class ToolHead:
return self.kin
def get_trapq(self):
return self.trapq
def note_step_generation_scan_time(self, delay, old_delay=0.):
self.flush_step_generation()
if old_delay:
self.kin_flush_times.pop(self.kin_flush_times.index(old_delay))
if delay:
self.kin_flush_times.append(delay)
new_delay = max(self.kin_flush_times + [SDS_CHECK_TIME])
self.kin_flush_delay = new_delay
def register_lookahead_callback(self, callback):
last_move = self.lookahead.get_last()
if last_move is None:
callback(self.get_last_move_time())
return
last_move.timing_callbacks.append(callback)
def note_mcu_movequeue_activity(self, mq_time, is_step_gen=True):
self.need_flush_time = max(self.need_flush_time, mq_time)
if is_step_gen:
self.step_gen_time = max(self.step_gen_time, mq_time)
if self.do_kick_flush_timer:
self.do_kick_flush_timer = False
self.reactor.update_timer(self.flush_timer, self.reactor.NOW)
def get_max_velocity(self):
return self.max_velocity, self.max_accel
def _calc_junction_deviation(self):