From 25caae1e8c238422cb8ee00637d463ae837c5273 Mon Sep 17 00:00:00 2001
From: ellensp <530024+ellensp@users.noreply.github.com>
Date: Wed, 10 Jan 2024 15:55:20 +1300
Subject: [PATCH] =?UTF-8?q?=F0=9F=A9=B9=20Fix=20PID=20/=20MPC=20tune=20bac?=
 =?UTF-8?q?kground=20tasks=20(#26652)?=
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/module/temperature.cpp | 56 ++++++++++++++++++++++---------
 Marlin/src/module/temperature.h   |  2 ++
 2 files changed, 42 insertions(+), 16 deletions(-)

diff --git a/Marlin/src/module/temperature.cpp b/Marlin/src/module/temperature.cpp
index 5173db02a15..c4920c21002 100644
--- a/Marlin/src/module/temperature.cpp
+++ b/Marlin/src/module/temperature.cpp
@@ -628,6 +628,36 @@ volatile bool Temperature::raw_temps_ready = false;
  * Class and Instance Methods
  */
 
+#if ANY(HAS_PID_HEATING, MPC_AUTOTUNE)
+
+  /**
+   * Run the minimal required activities during a tuning loop.
+   * TODO: Allow tuning routines to call idle() for more complete keepalive.
+   */
+  bool Temperature::tuning_idle(const millis_t &ms) {
+
+    // Run HAL idle tasks
+    hal.idletask();
+
+    const bool temp_ready = updateTemperaturesIfReady();
+
+    #if HAS_FAN_LOGIC
+      if (temp_ready) manage_extruder_fans(ms);
+    #else
+      UNUSED(ms);
+    #endif
+
+    // Run Controller Fan check (normally handled by manage_inactivity)
+    TERN_(USE_CONTROLLER_FAN, controllerFan.update());
+
+    // Run UI update
+    ui.update();
+
+    return temp_ready;
+  }
+
+#endif
+
 #if HAS_PID_HEATING
 
   inline void say_default_() { SERIAL_ECHOPGM("#define DEFAULT_"); }
@@ -727,7 +757,11 @@ volatile bool Temperature::raw_temps_ready = false;
 
       const millis_t ms = millis();
 
-      if (updateTemperaturesIfReady()) { // temp sample ready
+      // Run minimal necessary machine tasks
+      const bool temp_ready = tuning_idle(ms);
+
+      // If a new sample has arrived process things
+      if (temp_ready) {
 
         // Get the current temperature and constrain it
         current_temp = GHV(degChamber(), degBed(), degHotend(heater_id));
@@ -738,8 +772,6 @@ volatile bool Temperature::raw_temps_ready = false;
           ONHEATING(start_temp, current_temp, target);
         #endif
 
-        TERN_(HAS_FAN_LOGIC, manage_extruder_fans(ms));
-
         if (heating && current_temp > target && ELAPSED(ms, t2 + 5000UL)) {
           heating = false;
           SHV((bias - d) >> 1);
@@ -885,12 +917,6 @@ volatile bool Temperature::raw_temps_ready = false;
 
         goto EXIT_M303;
       }
-
-      // Run HAL idle tasks
-      hal.idletask();
-
-      // Run UI update
-      ui.update();
     }
     wait_for_heatup = false;
 
@@ -1142,10 +1168,11 @@ volatile bool Temperature::raw_temps_ready = false;
     constexpr millis_t report_interval_ms = 1000UL;
     curr_time_ms = millis();
 
-    if (updateTemperaturesIfReady()) { // temp sample ready
-      current_temp = degHotend(e);
-      TERN_(HAS_FAN_LOGIC, manage_extruder_fans(curr_time_ms));
-    }
+    // Run minimal necessary machine tasks
+    const bool temp_ready = tuning_idle(curr_time_ms);
+
+    // Set MPC temp if a new sample is ready
+    if (temp_ready) current_temp = degHotend(e);
 
     if (ELAPSED(curr_time_ms, next_report_ms)) {
       next_report_ms += report_interval_ms;
@@ -1153,9 +1180,6 @@ volatile bool Temperature::raw_temps_ready = false;
       SERIAL_EOL();
     }
 
-    hal.idletask();
-    ui.update();
-
     if (!wait_for_heatup) {
       SERIAL_ECHOLNPGM(STR_MPC_AUTOTUNE_INTERRUPTED);
       TERN_(DWIN_LCD_PROUI, dwinMPCTuning(MPC_INTERRUPTED));
diff --git a/Marlin/src/module/temperature.h b/Marlin/src/module/temperature.h
index c4d718e148b..c69a272adfd 100644
--- a/Marlin/src/module/temperature.h
+++ b/Marlin/src/module/temperature.h
@@ -1197,6 +1197,8 @@ class Temperature {
       static constexpr bool adaptive_fan_slowing = true;
     #endif
 
+    static bool tuning_idle(const millis_t &ms);
+
     /**
      * M303 PID auto-tuning for hotends or bed
      */