Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keep stepcompress move history relative to current time #6439

Merged
merged 6 commits into from
Dec 30, 2023

Conversation

EiNSTeiN-
Copy link
Contributor

Currently the stepcompress history is removed as soon as it's older than 30 seconds before the furthest move in the stepcompress queue. If the furthest move is long and will end more than 30 seconds in the future, then the stepcompress history will return a "future" stepper position rather than a past position.

I found this issue while trying to graph the output of extruder.find_past_position(print_time) with a 45mm move at 1.33mm/s, which puts it around 33.8 seconds, and it would give me a graph like this one:

image

Note the purple line (the expected extruder position) is constant for the first ~4 seconds of the move rather than providing the expected stepper position. With this fix applied, the graph is as expected:

image

This was reported, for example, in: https://klipper.discourse.group/t/filament-sensor-triggers-on-long-g0-e-command/3771. It's hard to track real world problems which can be caused by flushing the history too early but I've personally observed false positives with my BTT SFS v1 which might be related t this. I have a 350mm printer so long moves on top/bottom layers are to be expected when printing large parts at slow speeds, as we do with the first layer and the top infill.

…ory queue

Signed-off-by: Francois Chagnon <fc@francoischagnon.net>
Copy link
Collaborator

@KevinOConnor KevinOConnor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on this! In general, it looks good to me. See some minor comments below.

-Kevin

Comment on lines 737 to 738
void __visible
steppersync_history_expire(struct steppersync *ss, uint64_t end_clock)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be static.

@@ -206,7 +204,7 @@ trapq_set_position(struct trapq *tq, double print_time
, double pos_x, double pos_y, double pos_z)
{
// Flush all moves from trapq
trapq_finalize_moves(tq, NEVER_TIME);
trapq_finalize_moves(tq, NEVER_TIME, 0);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would introduce a subtle change in behavior. All places that fully cleared the history should continue to fully clear the history - thus trapq_finalize_moves(tq, NEVER_TIME, NEVER_TIME);

@@ -86,7 +86,7 @@ def manual_move(self, stepper, dist, speed, accel=0.):
0., 0., 0., axis_r, 0., 0., 0., cruise_v, accel)
print_time = print_time + accel_t + cruise_t + accel_t
stepper.generate_steps(print_time)
self.trapq_finalize_moves(self.trapq, print_time + 99999.9)
self.trapq_finalize_moves(self.trapq, print_time + 99999.9, 0)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

@@ -67,7 +67,7 @@ def do_move(self, movepos, speed, accel, sync=True):
0., cruise_v, accel)
self.next_cmd_time = self.next_cmd_time + accel_t + cruise_t + accel_t
self.rail.generate_steps(self.next_cmd_time)
self.trapq_finalize_moves(self.trapq, self.next_cmd_time + 99999.9)
self.trapq_finalize_moves(self.trapq, self.next_cmd_time + 99999.9, 0)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

Comment on lines 525 to 529
self.trapq_finalize_moves(self.trapq, self.reactor.NEVER)
clear_history_time = self.reactor.monotonic() - MOVE_HISTORY_EXPIRE
self.trapq_finalize_moves(self.trapq, self.reactor.NEVER,
clear_history_time)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

@@ -289,13 +290,14 @@ def _advance_flush_time(self, flush_time):
for sg in self.step_generators:
sg(sg_flush_time)
self.last_sg_flush_time = sg_flush_time
clear_history_time = self.reactor.monotonic() - MOVE_HISTORY_EXPIRE
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The trapq times are relative to the "print_time", not the "system time". To go from a system time to a print_time use self.mcu.estimated_print_time(systime).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, I made everything a print time including the time provided to flush_moves and convert using print_time_to_clock rather than get_clock.

@KevinOConnor
Copy link
Collaborator

Okay, thanks. However, the flushing in DripModeEndSignal was not corrected. Also, it is confusing to use two different mechanisms to implement a full flush in force_move.py and manual_stepper.py (it would be preferable to consistently use either use "+ 99999.9" or "reactor.NEVER").

-Kevin

@@ -190,10 +189,9 @@ trapq_finalize_moves(struct trapq *tq, double print_time)
if (list_empty(&tq->history))
return;
struct move *latest = list_first_entry(&tq->history, struct move, node);
double expire_time = latest->print_time + latest->move_t - HISTORY_EXPIRE;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just for the record I think we're now clearing the history in places where we wouldn't have before which is a change in behavior. When print_time is NEVER_TIME (or a large value) all the moves would get transferred to the history, but this code would still only clear 30s prior to the last move because of the math on this line. This is why I had the new parameter as 0 initially (to keep the history until this is called from the toolhead code).

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I goofed. You are correct and I misread the original code. I think we do want to avoid a change in behavior here, because this is an area of the code where we really want to be able to use tools like git bisect if necessary.

If I'm reading the code correctly, the call in trapq_finalize_moves() doesn't need to flush the history (it can use 0), and the code in DripModeEndSignal doesn't need to flush (it can also use 0). However, the code in manual_stepper.py and force_move.py do need to fully flush (using the current + 99999.9 method) as nothing else will free the history (and nothing uses the history of these modules).

Does that sound correct?

Thanks.
-Kevin

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you mean trapq_set_position and not trapq_finalize_moves that looks correct to me (as in, it preserves the current behavior), however I'm not super familiar with this code. I left the other 2 calls with + 99999.9 to clear the history.

@EiNSTeiN-
Copy link
Contributor Author

I made the last changes, this should be good to go if you're OK with my other comment

@KevinOConnor KevinOConnor merged commit d7f6348 into Klipper3d:master Dec 30, 2023
1 check passed
@KevinOConnor
Copy link
Collaborator

Thanks. I committed this PR along with commit 9847b44 on top.

-Kevin

@KevinOConnor
Copy link
Collaborator

I just realized that the new history flushing code would not do the right thing when in a debugging "batch mode" ( https://www.klipper3d.org/Debugging.html#translating-gcode-files-to-micro-controller-commands ). I pushed commit 25bc649 to address this.

-Kevin

sbtoonz pushed a commit to K1-Klipper/klipper that referenced this pull request Feb 16, 2024
…ipper3d#6439)

Expire history relative to current time rather than last move in history queue

Signed-off-by: Francois Chagnon <fc@francoischagnon.net>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants