From 7e0208940b97faacad63b7fe02242c9afed867ac Mon Sep 17 00:00:00 2001
From: narno2202 <130909513+narno2202@users.noreply.github.com>
Date: Wed, 22 Jan 2025 04:10:52 +0100
Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=B8=20Disable=20FT=20Motion=20during?=
 =?UTF-8?q?=20Biqu=20Microprobe=20use=20(#27368)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Co-authored-by: Scott Lahteine <thinkyhead@users.noreply.github.com>
---
 Marlin/Configuration.h                      |  1 -
 Marlin/src/feature/bedlevel/ubl/ubl_G29.cpp |  8 ++++++++
 Marlin/src/gcode/bedlevel/abl/G29.cpp       |  8 ++++++++
 Marlin/src/gcode/bedlevel/mbl/G29.cpp       |  8 ++++++++
 Marlin/src/gcode/calibrate/G28.cpp          | 13 +++++++++++++
 Marlin/src/gcode/probe/G30.cpp              |  8 ++++++++
 Marlin/src/inc/Conditionals-4-adv.h         |  3 +++
 Marlin/src/inc/SanityCheck.h                |  4 ----
 Marlin/src/module/ft_motion.h               | 13 ++++++++++++-
 buildroot/tests/STM32F103RC_btt             |  4 +++-
 10 files changed, 63 insertions(+), 7 deletions(-)

diff --git a/Marlin/Configuration.h b/Marlin/Configuration.h
index 6a4087529dd..f73ad0560d8 100644
--- a/Marlin/Configuration.h
+++ b/Marlin/Configuration.h
@@ -1511,7 +1511,6 @@
  * For information about this sensor https://github.com/bigtreetech/MicroProbe
  *
  * Also requires PROBE_ENABLE_DISABLE
- * With FT_MOTION requires ENDSTOP_INTERRUPTS_FEATURE
  */
 //#define BIQU_MICROPROBE_V1  // Triggers HIGH
 //#define BIQU_MICROPROBE_V2  // Triggers LOW
diff --git a/Marlin/src/feature/bedlevel/ubl/ubl_G29.cpp b/Marlin/src/feature/bedlevel/ubl/ubl_G29.cpp
index 2cea4968b78..1d765988ed1 100644
--- a/Marlin/src/feature/bedlevel/ubl/ubl_G29.cpp
+++ b/Marlin/src/feature/bedlevel/ubl/ubl_G29.cpp
@@ -48,6 +48,10 @@
   #include "../hilbert_curve.h"
 #endif
 
+#if FT_MOTION_DISABLE_FOR_PROBING
+  #include "../../../module/ft_motion.h"
+#endif
+
 #include <math.h>
 
 #define UBL_G29_P31
@@ -309,6 +313,10 @@ void unified_bed_leveling::G29() {
   const uint8_t p_val = parser.byteval('P');
   const bool may_move = p_val == 1 || p_val == 2 || p_val == 4 || parser.seen_test('J');
 
+  #if FT_MOTION_DISABLE_FOR_PROBING
+    FTMotionDisableInScope FT_Disabler; // Disable Fixed-Time Motion for probing
+  #endif
+
   // Check for commands that require the printer to be homed
   if (may_move) {
     planner.synchronize();
diff --git a/Marlin/src/gcode/bedlevel/abl/G29.cpp b/Marlin/src/gcode/bedlevel/abl/G29.cpp
index 386b805b6de..6225c243601 100644
--- a/Marlin/src/gcode/bedlevel/abl/G29.cpp
+++ b/Marlin/src/gcode/bedlevel/abl/G29.cpp
@@ -59,6 +59,10 @@
 #define DEBUG_OUT ENABLED(DEBUG_LEVELING_FEATURE)
 #include "../../../core/debug_out.h"
 
+#if DISABLED(PROBE_MANUALLY) && FT_MOTION_DISABLE_FOR_PROBING
+  #include "../../../module/ft_motion.h"
+#endif
+
 #if ABL_USES_GRID
   #if ENABLED(PROBE_Y_FIRST)
     #define PR_OUTER_VAR  abl.meshCount.x
@@ -280,6 +284,10 @@ G29_TYPE GcodeSuite::G29() {
   // Set and report "probing" state to host
   TERN_(FULL_REPORT_TO_HOST_FEATURE, set_and_report_grblstate(M_PROBE, false));
 
+  #if DISABLED(PROBE_MANUALLY) && FT_MOTION_DISABLE_FOR_PROBING
+    FTMotionDisableInScope FT_Disabler; // Disable Fixed-Time Motion for probing
+  #endif
+
   /**
    * On the initial G29 fetch command parameters.
    */
diff --git a/Marlin/src/gcode/bedlevel/mbl/G29.cpp b/Marlin/src/gcode/bedlevel/mbl/G29.cpp
index 6d23de4f77b..be0e5d7f180 100644
--- a/Marlin/src/gcode/bedlevel/mbl/G29.cpp
+++ b/Marlin/src/gcode/bedlevel/mbl/G29.cpp
@@ -45,6 +45,10 @@
 #define DEBUG_OUT ENABLED(DEBUG_LEVELING_FEATURE)
 #include "../../../core/debug_out.h"
 
+#if FT_MOTION_DISABLE_FOR_PROBING
+  #include "../../module/ft_motion.h"
+#endif
+
 // Save 130 bytes with non-duplication of PSTR
 inline void echo_not_entered(const char c) { SERIAL_CHAR(c); SERIAL_ECHOLNPGM(" not entered."); }
 
@@ -63,6 +67,10 @@ inline void echo_not_entered(const char c) { SERIAL_CHAR(c); SERIAL_ECHOLNPGM("
  */
 void GcodeSuite::G29() {
 
+  #if FT_MOTION_DISABLE_FOR_PROBING
+    FTMotionDisableInScope FT_Disabler; // Disable Fixed-Time Motion for probing
+  #endif
+
   DEBUG_SECTION(log_G29, "G29", true);
 
   // G29 Q is also available if debugging
diff --git a/Marlin/src/gcode/calibrate/G28.cpp b/Marlin/src/gcode/calibrate/G28.cpp
index ac90756ded8..85d27e14e8c 100644
--- a/Marlin/src/gcode/calibrate/G28.cpp
+++ b/Marlin/src/gcode/calibrate/G28.cpp
@@ -52,6 +52,10 @@
   #include "../../feature/bltouch.h"
 #endif
 
+#if FT_MOTION_DISABLE_FOR_PROBING
+  #include "../../module/ft_motion.h"
+#endif
+
 #include "../../lcd/marlinui.h"
 
 #if ENABLED(EXTENSIBLE_UI)
@@ -129,6 +133,11 @@
 #if ENABLED(Z_SAFE_HOMING)
 
   inline void home_z_safely() {
+
+    #if FT_MOTION_DISABLE_FOR_PROBING
+      FTMotionDisableInScope FT_Disabler; // Disable Fixed-Time Motion for homing
+    #endif
+
     DEBUG_SECTION(log_G28, "home_z_safely", DEBUGGING(LEVELING));
 
     // Disallow Z homing if X or Y homing is needed
@@ -284,6 +293,10 @@ void GcodeSuite::G28() {
       motion_state_t saved_motion_state = begin_slow_homing();
     #endif
 
+    #if FT_MOTION_DISABLE_FOR_PROBING
+      FTMotionDisableInScope FT_Disabler; // Disable Fixed-Time Motion for homing
+    #endif
+
     // Always home with tool 0 active
     #if HAS_MULTI_HOTEND
       #if DISABLED(DELTA) || ENABLED(DELTA_HOME_TO_SAFE_ZONE)
diff --git a/Marlin/src/gcode/probe/G30.cpp b/Marlin/src/gcode/probe/G30.cpp
index 29d6c1ad2ba..aa116f18816 100644
--- a/Marlin/src/gcode/probe/G30.cpp
+++ b/Marlin/src/gcode/probe/G30.cpp
@@ -34,6 +34,10 @@
   #include "../../feature/probe_temp_comp.h"
 #endif
 
+#if FT_MOTION_DISABLE_FOR_PROBING
+  #include "../../module/ft_motion.h"
+#endif
+
 #if ANY(DWIN_CREALITY_LCD_JYERSUI, EXTENSIBLE_UI)
   #define VERBOSE_SINGLE_PROBE
 #endif
@@ -76,6 +80,10 @@ void GcodeSuite::G30() {
     // Use 'C' to set Probe Temperature Compensation ON/OFF (on by default)
     TERN_(HAS_PTC, ptc.set_enabled(parser.boolval('C', true)));
 
+    #if FT_MOTION_DISABLE_FOR_PROBING
+      FTMotionDisableInScope FT_Disabler; // Disable Fixed-Time Motion for probing
+    #endif
+
     // Probe the bed, optionally raise, and return the measured height
     const float measured_z = probe.probe_at_point(probepos, raise_after);
 
diff --git a/Marlin/src/inc/Conditionals-4-adv.h b/Marlin/src/inc/Conditionals-4-adv.h
index be651206423..671c3590f41 100644
--- a/Marlin/src/inc/Conditionals-4-adv.h
+++ b/Marlin/src/inc/Conditionals-4-adv.h
@@ -1423,6 +1423,9 @@
     #define FTM_WINDOW_SIZE FTM_BW_SIZE
     #define FTM_BATCH_SIZE  FTM_BW_SIZE
   #endif
+  #if ANY(BIQU_MICROPROBE_V1, BIQU_MICROPROBE_V2)
+    #define FT_MOTION_DISABLE_FOR_PROBING 1
+  #endif
 #endif
 
 // Multi-Stepping Limit
diff --git a/Marlin/src/inc/SanityCheck.h b/Marlin/src/inc/SanityCheck.h
index 034878db66d..be1ab0db50e 100644
--- a/Marlin/src/inc/SanityCheck.h
+++ b/Marlin/src/inc/SanityCheck.h
@@ -1452,10 +1452,6 @@ static_assert(NUM_SERVOS <= NUM_SERVO_PLUGS, "NUM_SERVOS (or some servo index) i
       #error "BIQU MicroProbe requires a PROBE_ENABLE_PIN."
     #endif
 
-    #if ENABLED(FT_MOTION) && DISABLED(ENDSTOP_INTERRUPTS_FEATURE)
-      #error "BIQU Microprobe requires ENDSTOP_INTERRUPTS_FEATURE with FT_MOTION."
-    #endif
-
     #if ENABLED(BIQU_MICROPROBE_V1)
       #if ENABLED(INVERTED_PROBE_STATE)
         #if Z_MIN_PROBE_ENDSTOP_HIT_STATE != LOW
diff --git a/Marlin/src/module/ft_motion.h b/Marlin/src/module/ft_motion.h
index bff3ba1fd42..f6ce81af12f 100644
--- a/Marlin/src/module/ft_motion.h
+++ b/Marlin/src/module/ft_motion.h
@@ -213,7 +213,18 @@ class FTMotion {
 
     FORCE_INLINE static int32_t num_samples_shaper_settle() { return ( shaping.x.ena || shaping.y.ena ) ? FTM_ZMAX : 0; }
 
-
 }; // class FTMotion
 
 extern FTMotion ftMotion;
+
+typedef struct FTMotionDisableInScope {
+  bool isactive;
+  FTMotionDisableInScope() {
+    isactive = ftMotion.cfg.active;
+    ftMotion.cfg.active = false;
+  }
+  ~FTMotionDisableInScope() {
+    ftMotion.cfg.active = isactive;
+    if (isactive) ftMotion.init();
+  }
+} FTMotionDisableInScope_t;
diff --git a/buildroot/tests/STM32F103RC_btt b/buildroot/tests/STM32F103RC_btt
index 62ef7d96e5c..178170455fb 100755
--- a/buildroot/tests/STM32F103RC_btt
+++ b/buildroot/tests/STM32F103RC_btt
@@ -12,5 +12,7 @@ set -e
 restore_configs
 opt_set MOTHERBOARD BOARD_BTT_SKR_MINI_E3_V1_0 SERIAL_PORT 1 SERIAL_PORT_2 -1 \
         X_DRIVER_TYPE TMC2209 Y_DRIVER_TYPE TMC2209 Z_DRIVER_TYPE TMC2209 E0_DRIVER_TYPE TMC2209
-opt_enable CR10_STOCKDISPLAY PINS_DEBUGGING Z_IDLE_HEIGHT FT_MOTION FT_MOTION_MENU ADAPTIVE_STEP_SMOOTHING NONLINEAR_EXTRUSION
+opt_enable CR10_STOCKDISPLAY PINS_DEBUGGING Z_IDLE_HEIGHT \
+           FT_MOTION FT_MOTION_MENU BIQU_MICROPROBE_V1 PROBE_ENABLE_DISABLE Z_SAFE_HOMING AUTO_BED_LEVELING_BILINEAR \
+           ADAPTIVE_STEP_SMOOTHING NONLINEAR_EXTRUSION
 exec_test $1 $2 "BigTreeTech SKR Mini E3 1.0 - TMC2209 HW Serial, FT_MOTION" "$3"