From c1dcc56a0b5bc110289ac9c4aeefa8263c5174d4 Mon Sep 17 00:00:00 2001
From: InsanityAutomation
 <38436470+InsanityAutomation@users.noreply.github.com>
Date: Sat, 27 Jun 2020 22:57:01 -0400
Subject: [PATCH] Tool Change Migration fixes and debugging (#18448)

---
 Marlin/src/feature/runout.cpp     | 15 ++++++++++++--
 Marlin/src/gcode/config/M217.cpp  |  2 +-
 Marlin/src/module/tool_change.cpp | 34 +++++++++++++++++++++++++++----
 Marlin/src/module/tool_change.h   |  2 +-
 4 files changed, 45 insertions(+), 8 deletions(-)

diff --git a/Marlin/src/feature/runout.cpp b/Marlin/src/feature/runout.cpp
index 452781b7f2..80bce6d758 100644
--- a/Marlin/src/feature/runout.cpp
+++ b/Marlin/src/feature/runout.cpp
@@ -40,6 +40,7 @@ bool FilamentMonitorBase::enabled = true,
 #endif
 
 #if ENABLED(TOOLCHANGE_MIGRATION_FEATURE)
+  //#define DEBUG_TOOLCHANGE_MIGRATION_FEATURE
   #include "../module/tool_change.h"
 #endif
 
@@ -80,8 +81,18 @@ void event_filament_runout() {
   if (TERN0(ADVANCED_PAUSE_FEATURE, did_pause_print)) return;  // Action already in progress. Purge triggered repeated runout.
 
   #if ENABLED(TOOLCHANGE_MIGRATION_FEATURE)
-    if (migration.in_progress) return;  // Action already in progress. Purge triggered repeated runout.
-    if (migration.automode) { extruder_migration(); return; }
+    if (migration.in_progress) {
+      #if ENABLED(DEBUG_TOOLCHANGE_MIGRATION_FEATURE)
+        SERIAL_ECHOLN("Migration Already In Progress");
+      #endif
+      return;  // Action already in progress. Purge triggered repeated runout.
+    }
+    if (migration.automode) {
+      #if ENABLED(DEBUG_TOOLCHANGE_MIGRATION_FEATURE)
+        SERIAL_ECHOLN("Migration Starting");
+      #endif
+      if (extruder_migration()) return;
+    }
   #endif
 
   TERN_(EXTENSIBLE_UI, ExtUI::onFilamentRunout(ExtUI::getActiveTool()));
diff --git a/Marlin/src/gcode/config/M217.cpp b/Marlin/src/gcode/config/M217.cpp
index a1e53e5ecb..a3a9d7b0aa 100644
--- a/Marlin/src/gcode/config/M217.cpp
+++ b/Marlin/src/gcode/config/M217.cpp
@@ -49,7 +49,7 @@ void M217_report(const bool eeprom=false) {
                     " G", toolchange_settings.fan_time);
 
     #if ENABLED(TOOLCHANGE_MIGRATION_FEATURE)
-      SERIAL_ECHOPAIR(" N", int(migration.automode));
+      SERIAL_ECHOPAIR(" A", int(migration.automode));
       SERIAL_ECHOPAIR(" L", LINEAR_UNIT(migration.last));
     #endif
 
diff --git a/Marlin/src/module/tool_change.cpp b/Marlin/src/module/tool_change.cpp
index 8aa367555d..3a283f8357 100644
--- a/Marlin/src/module/tool_change.cpp
+++ b/Marlin/src/module/tool_change.cpp
@@ -1222,16 +1222,27 @@ void tool_change(const uint8_t new_tool, bool no_move/*=false*/) {
 
 #if ENABLED(TOOLCHANGE_MIGRATION_FEATURE)
 
-  void extruder_migration() {
+  bool extruder_migration() {
 
     #if ENABLED(PREVENT_COLD_EXTRUSION)
-      if (thermalManager.targetTooColdToExtrude(active_extruder)) return;
+      if (thermalManager.targetTooColdToExtrude(active_extruder)) {
+        #if ENABLED(DEBUG_TOOLCHANGE_MIGRATION_FEATURE)
+          SERIAL_ECHOLN("Migration Source Too Cold");
+        #endif
+        return false;
+      }
     #endif
 
     // No auto-migration or specified target?
     if (!migration.target && active_extruder >= migration.last) {
+      #if ENABLED(DEBUG_TOOLCHANGE_MIGRATION_FEATURE)
+        SERIAL_ECHO_MSG("No Migration Target");
+        SERIAL_ECHO_MSG("Target: ", migration.target,
+                        " Last: ", migration.last,
+                        " Active: ", active_extruder);
+      #endif
       migration.automode = false;
-      return;
+      return false;
     }
 
     // Migrate to a target or the next extruder
@@ -1239,6 +1250,9 @@ void tool_change(const uint8_t new_tool, bool no_move/*=false*/) {
     uint8_t migration_extruder = active_extruder;
 
     if (migration.target) {
+      #if ENABLED(DEBUG_TOOLCHANGE_MIGRATION_FEATURE)
+        SERIAL_ECHOLN("Migration using fixed target");
+      #endif
       // Specified target ok?
       const int16_t t = migration.target - 1;
       if (t != active_extruder) migration_extruder = t;
@@ -1246,9 +1260,17 @@ void tool_change(const uint8_t new_tool, bool no_move/*=false*/) {
     else if (migration.automode && migration_extruder < migration.last && migration_extruder < EXTRUDERS - 1)
       migration_extruder++;
 
-    if (migration_extruder == active_extruder) return;
+    if (migration_extruder == active_extruder) {
+      #if ENABLED(DEBUG_TOOLCHANGE_MIGRATION_FEATURE)
+        SERIAL_ECHOLN("Migration source matches active");
+      #endif
+      return false;
+    }
 
     // Migration begins
+    #if ENABLED(DEBUG_TOOLCHANGE_MIGRATION_FEATURE)
+      SERIAL_ECHOLN("Beginning migration");
+    #endif
 
     migration.in_progress = true; // Prevent runout script
     planner.synchronize();
@@ -1294,6 +1316,10 @@ void tool_change(const uint8_t new_tool, bool no_move/*=false*/) {
 
     planner.synchronize();
     planner.set_e_position_mm(current_position.e); // New extruder primed and ready
+    #if ENABLED(DEBUG_TOOLCHANGE_MIGRATION_FEATURE)
+      SERIAL_ECHOLN("Migration Complete");
+    #endif
+    return true;
   }
 
 #endif // TOOLCHANGE_MIGRATION_FEATURE
diff --git a/Marlin/src/module/tool_change.h b/Marlin/src/module/tool_change.h
index 4b004950ab..d39d7bc783 100644
--- a/Marlin/src/module/tool_change.h
+++ b/Marlin/src/module/tool_change.h
@@ -59,7 +59,7 @@
     } migration_settings_t;
     constexpr migration_settings_t migration_defaults = { 0, 0, false, false };
     extern migration_settings_t migration;
-    void extruder_migration();
+    bool extruder_migration();
   #endif
 #endif