From 381aeb94c6ee0449be7d4553ca4b58edd172d446 Mon Sep 17 00:00:00 2001
From: narno2202 <130909513+narno2202@users.noreply.github.com>
Date: Sat, 17 Aug 2024 20:02:05 +0200
Subject: [PATCH] =?UTF-8?q?=F0=9F=A9=B9=20Fix=20FT=20Motion=20toggle=20in?=
 =?UTF-8?q?=20active=20job=20(#27335)?=
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/src/gcode/feature/ft_motion/M493.cpp | 42 ++++++-----------
 Marlin/src/lcd/menu/menu_motion.cpp         | 50 ++++++++++-----------
 Marlin/src/module/stepper.cpp               |  2 +-
 3 files changed, 40 insertions(+), 54 deletions(-)

diff --git a/Marlin/src/gcode/feature/ft_motion/M493.cpp b/Marlin/src/gcode/feature/ft_motion/M493.cpp
index 0229306f4b..d5cfc82005 100644
--- a/Marlin/src/gcode/feature/ft_motion/M493.cpp
+++ b/Marlin/src/gcode/feature/ft_motion/M493.cpp
@@ -190,25 +190,18 @@ void GcodeSuite::M493_report(const bool forReplay/*=true*/) {
  *    R 0.00  Set the vibration tolerance for the Y axis
  */
 void GcodeSuite::M493() {
-  struct { bool update:1, reset_ft:1, report_h:1; } flag = { false };
+  struct { bool update:1, report:1; } flag = { false };
 
   if (!parser.seen_any())
-    flag.report_h = true;
+    flag.report = true;
 
   // Parse 'S' mode parameter.
   if (parser.seen('S')) {
     const bool active = parser.value_bool();
-
     if (active != ftMotion.cfg.active) {
-      switch (active) {
-        case false:
-          flag.reset_ft = true;
-          // fall-thru
-        case true:
-          flag.report_h = true;
-          ftMotion.cfg.active = active;
-          break;
-      }
+      stepper.ftMotion_syncPosition();
+      ftMotion.cfg.active = active;
+      flag.report = true;
     }
   }
 
@@ -228,7 +221,7 @@ void GcodeSuite::M493() {
           case ftMotionShaper_3HEI:
           case ftMotionShaper_MZV:
             ftMotion.cfg.shaper[axis] = newsh;
-            flag.update = flag.report_h = true;
+            flag.update = flag.report = true;
             break;
         }
       }
@@ -249,7 +242,7 @@ void GcodeSuite::M493() {
     if (parser.seen('P')) {
       const bool val = parser.value_bool();
       ftMotion.cfg.linearAdvEna = val;
-      flag.report_h = true;
+      flag.report = true;
       SERIAL_ECHO_TERNARY(val, "Linear Advance ", "en", "dis", "abled.\n");
     }
 
@@ -258,7 +251,7 @@ void GcodeSuite::M493() {
       const float val = parser.value_float();
       if (val >= 0.0f) {
         ftMotion.cfg.linearAdvK = val;
-        flag.report_h = true;
+        flag.report = true;
       }
       else // Value out of range.
         SERIAL_ECHOLNPGM("Linear Advance gain out of range.");
@@ -281,7 +274,7 @@ void GcodeSuite::M493() {
           #endif
           case dynFreqMode_DISABLED:
             ftMotion.cfg.dynFreqMode = val;
-            flag.report_h = true;
+            flag.report = true;
             break;
           default:
             SERIAL_ECHOLNPGM("?Invalid Dynamic Frequency Mode [D] value.");
@@ -309,7 +302,7 @@ void GcodeSuite::M493() {
         // TODO: Frequency minimum is dependent on the shaper used; the above check isn't always correct.
         if (WITHIN(val, FTM_MIN_SHAPE_FREQ, (FTM_FS) / 2)) {
           ftMotion.cfg.baseFreq.x = val;
-          flag.update = flag.reset_ft = flag.report_h = true;
+          flag.update = flag.report = true;
         }
         else // Frequency out of range.
           SERIAL_ECHOLNPGM("Invalid [", C('A'), "] frequency value.");
@@ -323,7 +316,7 @@ void GcodeSuite::M493() {
       if (parser.seenval('F')) {
         if (modeUsesDynFreq) {
           ftMotion.cfg.dynFreqK.x = parser.value_float();
-          flag.report_h = true;
+          flag.report = true;
         }
         else
           SERIAL_ECHOLNPGM("Wrong mode for [", C('F'), "] frequency scaling.");
@@ -370,7 +363,7 @@ void GcodeSuite::M493() {
         const float val = parser.value_float();
         if (WITHIN(val, FTM_MIN_SHAPE_FREQ, (FTM_FS) / 2)) {
           ftMotion.cfg.baseFreq.y = val;
-          flag.update = flag.reset_ft = flag.report_h = true;
+          flag.update = flag.report = true;
         }
         else // Frequency out of range.
           SERIAL_ECHOLNPGM("Invalid frequency [", C('B'), "] value.");
@@ -384,7 +377,7 @@ void GcodeSuite::M493() {
       if (parser.seenval('H')) {
         if (modeUsesDynFreq) {
           ftMotion.cfg.dynFreqK.y = parser.value_float();
-          flag.report_h = true;
+          flag.report = true;
         }
         else
           SERIAL_ECHOLNPGM("Wrong mode for [", C('H'), "] frequency scaling.");
@@ -423,16 +416,9 @@ void GcodeSuite::M493() {
 
   #endif // HAS_Y_AXIS
 
-  planner.synchronize();
-
   if (flag.update) ftMotion.update_shaping_params();
 
-  if (flag.reset_ft) {
-    stepper.ftMotion_syncPosition();
-    ftMotion.reset();
-  }
-
-  if (flag.report_h) say_shaping();
+  if (flag.report) say_shaping();
 }
 
 #endif // FT_MOTION
diff --git a/Marlin/src/lcd/menu/menu_motion.cpp b/Marlin/src/lcd/menu/menu_motion.cpp
index ac53e01c11..0965e0e482 100644
--- a/Marlin/src/lcd/menu/menu_motion.cpp
+++ b/Marlin/src/lcd/menu/menu_motion.cpp
@@ -351,44 +351,44 @@ void menu_move() {
     }
   #endif
 
-  void ftm_menu_set_cmpn(const AxisEnum axis, const ftMotionShaper_t s) {
+  void ftm_menu_set_shaper(const AxisEnum axis, const ftMotionShaper_t s) {
     ftMotion.cfg.shaper[axis] = s;
     ftMotion.update_shaping_params();
     ui.go_back();
   }
 
-  inline void menu_ftm_cmpn_x() {
+  inline void menu_ftm_shaper_x() {
     const ftMotionShaper_t shaper = ftMotion.cfg.shaper.x;
     START_MENU();
     BACK_ITEM(MSG_FIXED_TIME_MOTION);
 
-    if (shaper != ftMotionShaper_NONE)   ACTION_ITEM(MSG_LCD_OFF,  []{ ftm_menu_set_cmpn(X_AXIS, ftMotionShaper_NONE); });
-    if (shaper != ftMotionShaper_ZV)     ACTION_ITEM(MSG_FTM_ZV,   []{ ftm_menu_set_cmpn(X_AXIS, ftMotionShaper_ZV); });
-    if (shaper != ftMotionShaper_ZVD)    ACTION_ITEM(MSG_FTM_ZVD,  []{ ftm_menu_set_cmpn(X_AXIS, ftMotionShaper_ZVD); });
-    if (shaper != ftMotionShaper_ZVDD)   ACTION_ITEM(MSG_FTM_ZVDD, []{ ftm_menu_set_cmpn(X_AXIS, ftMotionShaper_ZVDD); });
-    if (shaper != ftMotionShaper_ZVDDD)  ACTION_ITEM(MSG_FTM_ZVDDD,[]{ ftm_menu_set_cmpn(X_AXIS, ftMotionShaper_ZVDDD); });
-    if (shaper != ftMotionShaper_EI)     ACTION_ITEM(MSG_FTM_EI,   []{ ftm_menu_set_cmpn(X_AXIS, ftMotionShaper_EI); });
-    if (shaper != ftMotionShaper_2HEI)   ACTION_ITEM(MSG_FTM_2HEI, []{ ftm_menu_set_cmpn(X_AXIS, ftMotionShaper_2HEI); });
-    if (shaper != ftMotionShaper_3HEI)   ACTION_ITEM(MSG_FTM_3HEI, []{ ftm_menu_set_cmpn(X_AXIS, ftMotionShaper_3HEI); });
-    if (shaper != ftMotionShaper_MZV)    ACTION_ITEM(MSG_FTM_MZV,  []{ ftm_menu_set_cmpn(X_AXIS, ftMotionShaper_MZV); });
+    if (shaper != ftMotionShaper_NONE)   ACTION_ITEM(MSG_LCD_OFF,  []{ ftm_menu_set_shaper(X_AXIS, ftMotionShaper_NONE); });
+    if (shaper != ftMotionShaper_ZV)     ACTION_ITEM(MSG_FTM_ZV,   []{ ftm_menu_set_shaper(X_AXIS, ftMotionShaper_ZV); });
+    if (shaper != ftMotionShaper_ZVD)    ACTION_ITEM(MSG_FTM_ZVD,  []{ ftm_menu_set_shaper(X_AXIS, ftMotionShaper_ZVD); });
+    if (shaper != ftMotionShaper_ZVDD)   ACTION_ITEM(MSG_FTM_ZVDD, []{ ftm_menu_set_shaper(X_AXIS, ftMotionShaper_ZVDD); });
+    if (shaper != ftMotionShaper_ZVDDD)  ACTION_ITEM(MSG_FTM_ZVDDD,[]{ ftm_menu_set_shaper(X_AXIS, ftMotionShaper_ZVDDD); });
+    if (shaper != ftMotionShaper_EI)     ACTION_ITEM(MSG_FTM_EI,   []{ ftm_menu_set_shaper(X_AXIS, ftMotionShaper_EI); });
+    if (shaper != ftMotionShaper_2HEI)   ACTION_ITEM(MSG_FTM_2HEI, []{ ftm_menu_set_shaper(X_AXIS, ftMotionShaper_2HEI); });
+    if (shaper != ftMotionShaper_3HEI)   ACTION_ITEM(MSG_FTM_3HEI, []{ ftm_menu_set_shaper(X_AXIS, ftMotionShaper_3HEI); });
+    if (shaper != ftMotionShaper_MZV)    ACTION_ITEM(MSG_FTM_MZV,  []{ ftm_menu_set_shaper(X_AXIS, ftMotionShaper_MZV); });
 
     END_MENU();
   }
 
-  inline void menu_ftm_cmpn_y() {
+  inline void menu_ftm_shaper_y() {
     const ftMotionShaper_t shaper = ftMotion.cfg.shaper.y;
     START_MENU();
     BACK_ITEM(MSG_FIXED_TIME_MOTION);
 
-    if (shaper != ftMotionShaper_NONE)   ACTION_ITEM(MSG_LCD_OFF,  []{ ftm_menu_set_cmpn(Y_AXIS, ftMotionShaper_NONE); });
-    if (shaper != ftMotionShaper_ZV)     ACTION_ITEM(MSG_FTM_ZV,   []{ ftm_menu_set_cmpn(Y_AXIS, ftMotionShaper_ZV); });
-    if (shaper != ftMotionShaper_ZVD)    ACTION_ITEM(MSG_FTM_ZVD,  []{ ftm_menu_set_cmpn(Y_AXIS, ftMotionShaper_ZVD); });
-    if (shaper != ftMotionShaper_ZVDD)   ACTION_ITEM(MSG_FTM_ZVDD, []{ ftm_menu_set_cmpn(Y_AXIS, ftMotionShaper_ZVDD); });
-    if (shaper != ftMotionShaper_ZVDDD)  ACTION_ITEM(MSG_FTM_ZVDDD,[]{ ftm_menu_set_cmpn(Y_AXIS, ftMotionShaper_ZVDDD); });
-    if (shaper != ftMotionShaper_EI)     ACTION_ITEM(MSG_FTM_EI,   []{ ftm_menu_set_cmpn(Y_AXIS, ftMotionShaper_EI); });
-    if (shaper != ftMotionShaper_2HEI)   ACTION_ITEM(MSG_FTM_2HEI, []{ ftm_menu_set_cmpn(Y_AXIS, ftMotionShaper_2HEI); });
-    if (shaper != ftMotionShaper_3HEI)   ACTION_ITEM(MSG_FTM_3HEI, []{ ftm_menu_set_cmpn(Y_AXIS, ftMotionShaper_3HEI); });
-    if (shaper != ftMotionShaper_MZV)    ACTION_ITEM(MSG_FTM_MZV,  []{ ftm_menu_set_cmpn(Y_AXIS, ftMotionShaper_MZV); });
+    if (shaper != ftMotionShaper_NONE)   ACTION_ITEM(MSG_LCD_OFF,  []{ ftm_menu_set_shaper(Y_AXIS, ftMotionShaper_NONE); });
+    if (shaper != ftMotionShaper_ZV)     ACTION_ITEM(MSG_FTM_ZV,   []{ ftm_menu_set_shaper(Y_AXIS, ftMotionShaper_ZV); });
+    if (shaper != ftMotionShaper_ZVD)    ACTION_ITEM(MSG_FTM_ZVD,  []{ ftm_menu_set_shaper(Y_AXIS, ftMotionShaper_ZVD); });
+    if (shaper != ftMotionShaper_ZVDD)   ACTION_ITEM(MSG_FTM_ZVDD, []{ ftm_menu_set_shaper(Y_AXIS, ftMotionShaper_ZVDD); });
+    if (shaper != ftMotionShaper_ZVDDD)  ACTION_ITEM(MSG_FTM_ZVDDD,[]{ ftm_menu_set_shaper(Y_AXIS, ftMotionShaper_ZVDDD); });
+    if (shaper != ftMotionShaper_EI)     ACTION_ITEM(MSG_FTM_EI,   []{ ftm_menu_set_shaper(Y_AXIS, ftMotionShaper_EI); });
+    if (shaper != ftMotionShaper_2HEI)   ACTION_ITEM(MSG_FTM_2HEI, []{ ftm_menu_set_shaper(Y_AXIS, ftMotionShaper_2HEI); });
+    if (shaper != ftMotionShaper_3HEI)   ACTION_ITEM(MSG_FTM_3HEI, []{ ftm_menu_set_shaper(Y_AXIS, ftMotionShaper_3HEI); });
+    if (shaper != ftMotionShaper_MZV)    ACTION_ITEM(MSG_FTM_MZV,  []{ ftm_menu_set_shaper(Y_AXIS, ftMotionShaper_MZV); });
 
     END_MENU();
   }
@@ -438,7 +438,7 @@ void menu_move() {
 
     if (c.active) {
       #if HAS_X_AXIS
-        SUBMENU_N(X_AXIS, MSG_FTM_CMPN_MODE, menu_ftm_cmpn_x);
+        SUBMENU_N(X_AXIS, MSG_FTM_CMPN_MODE, menu_ftm_shaper_x);
         MENU_ITEM_ADDON_START_RJ(5); lcd_put_u8str(shaper_name[X_AXIS]); MENU_ITEM_ADDON_END();
 
         if (AXIS_HAS_SHAPER(X)) {
@@ -449,7 +449,7 @@ void menu_move() {
         }
       #endif
       #if HAS_Y_AXIS
-        SUBMENU_N(Y_AXIS, MSG_FTM_CMPN_MODE, menu_ftm_cmpn_y);
+        SUBMENU_N(Y_AXIS, MSG_FTM_CMPN_MODE, menu_ftm_shaper_y);
         MENU_ITEM_ADDON_START_RJ(5); lcd_put_u8str(shaper_name[Y_AXIS]); MENU_ITEM_ADDON_END();
 
         if (AXIS_HAS_SHAPER(Y)) {
@@ -495,11 +495,11 @@ void menu_move() {
     START_MENU();
 
     #if HAS_X_AXIS
-      SUBMENU_N(X_AXIS, MSG_FTM_CMPN_MODE, menu_ftm_cmpn_x);
+      SUBMENU_N(X_AXIS, MSG_FTM_CMPN_MODE, menu_ftm_shaper_x);
       MENU_ITEM_ADDON_START_RJ(5); lcd_put_u8str(shaper_name[X_AXIS]); MENU_ITEM_ADDON_END();
     #endif
     #if HAS_Y_AXIS
-      SUBMENU_N(Y_AXIS, MSG_FTM_CMPN_MODE, menu_ftm_cmpn_y);
+      SUBMENU_N(Y_AXIS, MSG_FTM_CMPN_MODE, menu_ftm_shaper_y);
       MENU_ITEM_ADDON_START_RJ(5); lcd_put_u8str(shaper_name[Y_AXIS]); MENU_ITEM_ADDON_END();
     #endif
 
diff --git a/Marlin/src/module/stepper.cpp b/Marlin/src/module/stepper.cpp
index 7153f152a5..2b5a75eff8 100644
--- a/Marlin/src/module/stepper.cpp
+++ b/Marlin/src/module/stepper.cpp
@@ -3422,7 +3422,7 @@ void Stepper::set_axis_position(const AxisEnum a, const int32_t &v) {
 #if ENABLED(FT_MOTION)
 
   void Stepper::ftMotion_syncPosition() {
-    //planner.synchronize(); planner already synchronized in M493
+    planner.synchronize();
 
     // Update stepper positions from the planner
     AVR_ATOMIC_SECTION_START();