From 3d3b87f97fc91d93ae4198b36198262207e1202b Mon Sep 17 00:00:00 2001
From: Kevin O'Connor <kevin@koconnor.net>
Date: Tue, 16 Jan 2024 20:40:55 -0500
Subject: [PATCH] toolhead: Ensure full kin_flush_delay after
 flush_step_generation()

Commit b7b13588 made it possible that the kinematic code could be
restarted after a flush_step_generation() call without a sufficient
delay.

Rename last_sg_flush_time to min_restart_time and use that to ensure
_calc_print_time() always pauses kin_flush_delay time since the last
flush_step_generation() call.

Also, update force_move to invoke flush_step_generation() after any
movements.  This is needed to ensure there is a sufficient delay
should force_move be called on a stepper motor that is part of the
toolhead kinematics and is using a step generation "scan time".

This fixes possible "internal error in stepcompress" reports when
using FORCE_MOVE.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
---
 klippy/extras/force_move.py | 2 +-
 klippy/toolhead.py          | 7 ++++---
 2 files changed, 5 insertions(+), 4 deletions(-)

diff --git a/klippy/extras/force_move.py b/klippy/extras/force_move.py
index 7501ea986..9cf495191 100644
--- a/klippy/extras/force_move.py
+++ b/klippy/extras/force_move.py
@@ -90,8 +90,8 @@ class ForceMove:
                                   print_time + 99999.9)
         stepper.set_trapq(prev_trapq)
         stepper.set_stepper_kinematics(prev_sk)
-        toolhead.note_kinematic_activity(print_time)
         toolhead.dwell(accel_t + cruise_t + accel_t)
+        toolhead.flush_step_generation()
     def _lookup_stepper(self, gcmd):
         name = gcmd.get('STEPPER')
         if name not in self.steppers:
diff --git a/klippy/toolhead.py b/klippy/toolhead.py
index 477b5fce0..55a39eff1 100644
--- a/klippy/toolhead.py
+++ b/klippy/toolhead.py
@@ -239,7 +239,7 @@ class ToolHead:
         # Flush tracking
         self.flush_timer = self.reactor.register_timer(self._flush_handler)
         self.do_kick_flush_timer = True
-        self.last_flush_time = self.last_sg_flush_time = 0.
+        self.last_flush_time = self.min_restart_time = 0.
         self.need_flush_time = self.step_gen_time = self.clear_history_time = 0.
         # Kinematic step generation scan window time tracking
         self.kin_flush_delay = SDS_CHECK_TIME
@@ -289,7 +289,7 @@ class ToolHead:
         sg_flush_time = max(sg_flush_want, flush_time)
         for sg in self.step_generators:
             sg(sg_flush_time)
-        self.last_sg_flush_time = sg_flush_time
+        self.min_restart_time = max(self.min_restart_time, sg_flush_time)
         # Free trapq entries that are no longer needed
         clear_history_time = self.clear_history_time
         if not self.can_pause:
@@ -314,7 +314,7 @@ class ToolHead:
     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.last_sg_flush_time)
+        kin_time = max(est_print_time + MIN_KIN_TIME, self.min_restart_time)
         kin_time += self.kin_flush_delay
         min_print_time = max(est_print_time + BUFFER_TIME_START, kin_time)
         if min_print_time > self.print_time:
@@ -361,6 +361,7 @@ class ToolHead:
     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)
     def get_last_move_time(self):
         if self.special_queuing_state:
             self._flush_lookahead()