From f6b09cf465ed0d05685028cb32d35c2488557b97 Mon Sep 17 00:00:00 2001
From: Scott Lahteine <sourcetree@thinkyhead.com>
Date: Mon, 20 Jun 2016 15:50:51 -0700
Subject: [PATCH] Always use deploy/stow for dock_sled

---
 Marlin/Marlin_main.cpp | 252 +++++++++++++++++++----------------------
 1 file changed, 117 insertions(+), 135 deletions(-)

diff --git a/Marlin/Marlin_main.cpp b/Marlin/Marlin_main.cpp
index 4c7c0aaae8..0eba959b15 100644
--- a/Marlin/Marlin_main.cpp
+++ b/Marlin/Marlin_main.cpp
@@ -1655,6 +1655,10 @@ static void setup_for_endstop_move() {
     feedrate = old_feedrate;
   }
 
+  inline void do_blocking_move_to_x(float x) {
+    do_blocking_move_to(x, current_position[Y_AXIS], current_position[Z_AXIS]);
+  }
+
   inline void do_blocking_move_to_z(float z) {
     do_blocking_move_to(current_position[X_AXIS], current_position[Y_AXIS], z);
   }
@@ -1684,6 +1688,63 @@ static void setup_for_endstop_move() {
 
 #endif
 
+#if HAS_BED_PROBE
+
+  inline void raise_z_after_probing() {
+    #if Z_RAISE_AFTER_PROBING > 0
+      #if ENABLED(DEBUG_LEVELING_FEATURE)
+        if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("raise_z_after_probing()");
+      #endif
+      do_blocking_move_to_z(current_position[Z_AXIS] + Z_RAISE_AFTER_PROBING);
+    #endif
+  }
+#endif
+
+#if ENABLED(Z_PROBE_SLED)
+
+  #ifndef SLED_DOCKING_OFFSET
+    #define SLED_DOCKING_OFFSET 0
+  #endif
+
+  /**
+   * Method to dock/undock a sled designed by Charles Bell.
+   *
+   * dock[in]     If true, move to MAX_X and engage the electromagnet
+   * offset[in]   The additional distance to move to adjust docking location
+   */
+  static void dock_sled(bool dock, int offset = 0) {
+    #if ENABLED(DEBUG_LEVELING_FEATURE)
+      if (DEBUGGING(LEVELING)) {
+        SERIAL_ECHOPAIR("dock_sled(", dock);
+        SERIAL_ECHOLNPGM(")");
+      }
+    #endif
+
+    if (!axis_homed[X_AXIS] || !axis_homed[Y_AXIS] || !axis_homed[Z_AXIS]) {
+      axis_unhomed_error(true);
+      return;
+    }
+
+    if (endstops.z_probe_enabled == !dock) return; // already docked/undocked?
+
+    float oldXpos = current_position[X_AXIS]; // save x position
+    if (dock) {
+      raise_z_after_probing(); // raise Z
+      // Dock sled a bit closer to ensure proper capturing
+      do_blocking_move_to_x(X_MAX_POS + SLED_DOCKING_OFFSET + offset - 1);
+      digitalWrite(SLED_PIN, LOW); // turn off magnet
+    }
+    else {
+      float z_loc = current_position[Z_AXIS];
+      if (z_loc < Z_RAISE_BEFORE_PROBING + 5) z_loc = Z_RAISE_BEFORE_PROBING;
+      do_blocking_move_to(X_MAX_POS + SLED_DOCKING_OFFSET + offset, current_position[Y_AXIS], z_loc); // this also updates current_position
+      digitalWrite(SLED_PIN, HIGH); // turn on magnet
+    }
+    do_blocking_move_to_x(oldXpos); // return to position before docking
+  }
+
+#endif // Z_PROBE_SLED
+
 #if HAS_BED_PROBE
 
   static void deploy_z_probe() {
@@ -1694,7 +1755,11 @@ static void setup_for_endstop_move() {
 
     if (endstops.z_probe_enabled) return;
 
-    #if HAS_Z_SERVO_ENDSTOP
+    #if ENABLED(Z_PROBE_SLED)
+
+      dock_sled(false);
+
+    #elif HAS_Z_SERVO_ENDSTOP
 
       // Make room for Z Servo
       raise_z_for_servo(Z_RAISE_BEFORE_PROBING);
@@ -1792,7 +1857,11 @@ static void setup_for_endstop_move() {
 
     if (!endstops.z_probe_enabled) return;
 
-    #if HAS_Z_SERVO_ENDSTOP
+    #if ENABLED(Z_PROBE_SLED)
+
+      dock_sled(true);
+
+    #elif HAS_Z_SERVO_ENDSTOP
 
       // Make room for the servo
       raise_z_for_servo(Z_RAISE_AFTER_PROBING);
@@ -2040,19 +2109,6 @@ static void setup_for_endstop_move() {
     do_blocking_move_to(x, y, current_position[Z_AXIS]);
   }
 
-  inline void do_blocking_move_to_x(float x) {
-    do_blocking_move_to(x, current_position[Y_AXIS], current_position[Z_AXIS]);
-  }
-
-  inline void raise_z_after_probing() {
-    #if Z_RAISE_AFTER_PROBING > 0
-      #if ENABLED(DEBUG_LEVELING_FEATURE)
-        if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("raise_z_after_probing()");
-      #endif
-      do_blocking_move_to_z(current_position[Z_AXIS] + Z_RAISE_AFTER_PROBING);
-    #endif
-  }
-
   static void clean_up_after_endstop_move() {
     #if ENABLED(DEBUG_LEVELING_FEATURE)
       if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("clean_up_after_endstop_move > ENDSTOPS_ONLY_FOR_HOMING > endstops.not_homing()");
@@ -2229,55 +2285,6 @@ static void setup_for_endstop_move() {
   }
 #endif
 
-#if ENABLED(Z_PROBE_SLED)
-
-  #ifndef SLED_DOCKING_OFFSET
-    #define SLED_DOCKING_OFFSET 0
-  #endif
-
-  /**
-   * Method to dock/undock a sled designed by Charles Bell.
-   *
-   * dock[in]     If true, move to MAX_X and engage the electromagnet
-   * offset[in]   The additional distance to move to adjust docking location
-   */
-  static void dock_sled(bool dock, int offset = 0) {
-    #if ENABLED(DEBUG_LEVELING_FEATURE)
-      if (DEBUGGING(LEVELING)) {
-        SERIAL_ECHOPAIR("dock_sled(", dock);
-        SERIAL_ECHOLNPGM(")");
-      }
-    #endif
-
-    if (!axis_homed[X_AXIS] || !axis_homed[Y_AXIS] || !axis_homed[Z_AXIS]) {
-      axis_unhomed_error(true);
-      return;
-    }
-
-    if (endstops.z_probe_enabled == !dock) return; // already docked/undocked?
-
-    float oldXpos = current_position[X_AXIS]; // save x position
-    if (dock) {
-      raise_z_after_probing(); // raise Z
-      // Dock sled a bit closer to ensure proper capturing
-      do_blocking_move_to_x(X_MAX_POS + SLED_DOCKING_OFFSET + offset - 1);
-      digitalWrite(SLED_PIN, LOW); // turn off magnet
-    }
-    else {
-      float z_loc = current_position[Z_AXIS];
-      if (z_loc < Z_RAISE_BEFORE_PROBING + 5) z_loc = Z_RAISE_BEFORE_PROBING;
-      do_blocking_move_to(X_MAX_POS + SLED_DOCKING_OFFSET + offset, current_position[Y_AXIS], z_loc); // this also updates current_position
-      digitalWrite(SLED_PIN, HIGH); // turn on magnet
-    }
-    do_blocking_move_to_x(oldXpos); // return to position before docking
-
-    endstops.enable_z_probe(!dock); // logically disable docked probe
-  }
-
-#endif // Z_PROBE_SLED
-
-
-
 /**
  * Home an individual axis
  */
@@ -2306,24 +2313,13 @@ static void homeaxis(AxisEnum axis) {
     current_position[axis] = 0;
     sync_plan_position();
 
-    #if ENABLED(Z_PROBE_SLED)
-      #define _Z_DEPLOY           (dock_sled(false))
-      #define _Z_STOW             (dock_sled(true))
-    #elif (ENABLED(AUTO_BED_LEVELING_FEATURE) && HAS_Z_SERVO_ENDSTOP) || ENABLED(FIX_MOUNTED_PROBE)
-      #define _Z_DEPLOY           (deploy_z_probe())
-      #define _Z_STOW             (stow_z_probe())
-    #elif HAS_Z_SERVO_ENDSTOP
-      #define _Z_DEPLOY           do{ raise_z_for_servo(Z_RAISE_BEFORE_PROBING); DEPLOY_Z_SERVO(); endstops.z_probe_enabled = true; }while(0)
-      #define _Z_STOW             do{ raise_z_for_servo(Z_RAISE_AFTER_PROBING); STOW_Z_SERVO(); endstops.z_probe_enabled = false; }while(0)
-    #endif
-
     // Homing Z towards the bed? Deploy the Z probe or endstop.
-    #if HAS_Z_SERVO_ENDSTOP || ENABLED(Z_PROBE_SLED) || ENABLED(FIX_MOUNTED_PROBE)
+    #if HAS_BED_PROBE
       if (axis == Z_AXIS && axis_home_dir < 0) {
         #if ENABLED(DEBUG_LEVELING_FEATURE)
-          if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM(" > " STRINGIFY(_Z_DEPLOY));
+          if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM(" > deploy_z_probe()");
         #endif
-        _Z_DEPLOY;
+        deploy_z_probe();
       }
     #endif
 
@@ -2441,12 +2437,12 @@ static void homeaxis(AxisEnum axis) {
     axis_homed[axis] = true;
 
     // Put away the Z probe
-    #if HAS_Z_SERVO_ENDSTOP || ENABLED(Z_PROBE_SLED) || ENABLED(FIX_MOUNTED_PROBE)
+    #if HAS_BED_PROBE
       if (axis == Z_AXIS && axis_home_dir < 0) {
         #if ENABLED(DEBUG_LEVELING_FEATURE)
-          if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM(" > " STRINGIFY(_Z_STOW));
+          if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM(" > stow_z_probe()");
         #endif
-        _Z_STOW;
+        stow_z_probe();
       }
     #endif
 
@@ -3468,9 +3464,7 @@ inline void gcode_G28() {
       #endif // !DELTA
     }
 
-    #if ENABLED(Z_PROBE_SLED)
-      dock_sled(false); // engage (un-dock) the Z probe
-    #elif ENABLED(FIX_MOUNTED_PROBE) || ENABLED(MECHANICAL_PROBE) || ENABLED(Z_PROBE_ALLEN_KEY) || (ENABLED(DELTA) && HAS_Z_SERVO_ENDSTOP)
+    #if HAS_BED_PROBE
       deploy_z_probe();
     #endif
 
@@ -3721,14 +3715,7 @@ inline void gcode_G28() {
 
     #endif // !AUTO_BED_LEVELING_GRID
 
-    #if ENABLED(DELTA)
-      // Allen Key Probe for Delta
-      #if ENABLED(Z_PROBE_ALLEN_KEY) || HAS_Z_SERVO_ENDSTOP
-        stow_z_probe();
-      #else
-        raise_z_after_probing(); // for non Allen Key probes, such as simple mechanical probe
-      #endif
-    #else // !DELTA
+    #if DISABLED(DELTA)
       if (verbose_level > 0)
         planner.bed_level_matrix.debug(" \n\nBed Level Correction Matrix:");
 
@@ -3788,7 +3775,7 @@ inline void gcode_G28() {
           #if HAS_Z_SERVO_ENDSTOP || ENABLED(Z_PROBE_ALLEN_KEY) || ENABLED(Z_PROBE_SLED)
              + Z_RAISE_AFTER_PROBING
           #endif
-          ;
+        ;
         // current_position[Z_AXIS] += home_offset[Z_AXIS]; // The Z probe determines Z=0, not "Z home"
         sync_plan_position();
 
@@ -3796,18 +3783,13 @@ inline void gcode_G28() {
           if (DEBUGGING(LEVELING)) DEBUG_POS("> corrected Z in G29", current_position);
         #endif
       }
-
-      // Sled assembly for Cartesian bots
-      #if ENABLED(Z_PROBE_SLED)
-        dock_sled(true); // dock the sled
-      #elif !HAS_Z_SERVO_ENDSTOP && DISABLED(Z_PROBE_ALLEN_KEY) && DISABLED(Z_PROBE_SLED)
-        // Raise Z axis for non-delta and non servo based probes
-        raise_z_after_probing();
-      #endif
-
     #endif // !DELTA
 
-    #if ENABLED(MECHANICAL_PROBE)
+    #if DISABLED(Z_PROBE_ALLEN_KEY) && DISABLED(Z_PROBE_SLED) && !HAS_Z_SERVO_ENDSTOP
+      raise_z_after_probing();
+    #endif
+
+    #if ENABLED(Z_PROBE_ALLEN_KEY) || ENABLED(Z_PROBE_SLED) || ENABLED(MECHANICAL_PROBE)
       stow_z_probe();
     #endif
 
@@ -3838,38 +3820,38 @@ inline void gcode_G28() {
     KEEPALIVE_STATE(IN_HANDLER);
   }
 
-  #if DISABLED(Z_PROBE_SLED) // could be avoided
+#endif //AUTO_BED_LEVELING_FEATURE
 
-    /**
-     * G30: Do a single Z probe at the current XY
-     */
-    inline void gcode_G30() {
-      deploy_z_probe(); // Engage Z Servo endstop if available. Z_PROBE_SLED is missed here.
+#if HAS_BED_PROBE
 
-      stepper.synchronize();
-      // TODO: clear the leveling matrix or the planner will be set incorrectly
-      setup_for_endstop_move(); // Too late. Must be done before deploying.
+  /**
+   * G30: Do a single Z probe at the current XY
+   */
+  inline void gcode_G30() {
+    deploy_z_probe();
 
-      run_z_probe();
+    stepper.synchronize();
+    // TODO: clear the leveling matrix or the planner will be set incorrectly
+    setup_for_endstop_move(); // Too late. Must be done before deploying.
 
-      SERIAL_PROTOCOLPGM("Bed X: ");
-      SERIAL_PROTOCOL(current_position[X_AXIS] + X_PROBE_OFFSET_FROM_EXTRUDER + 0.0001);
-      SERIAL_PROTOCOLPGM(" Y: ");
-      SERIAL_PROTOCOL(current_position[Y_AXIS] + Y_PROBE_OFFSET_FROM_EXTRUDER + 0.0001);
-      SERIAL_PROTOCOLPGM(" Z: ");
-      SERIAL_PROTOCOL(current_position[Z_AXIS] + 0.0001);
-      SERIAL_EOL;
+    run_z_probe();
 
-      clean_up_after_endstop_move(); // Too early. must be done after the stowing.
+    SERIAL_PROTOCOLPGM("Bed X: ");
+    SERIAL_PROTOCOL(current_position[X_AXIS] + X_PROBE_OFFSET_FROM_EXTRUDER + 0.0001);
+    SERIAL_PROTOCOLPGM(" Y: ");
+    SERIAL_PROTOCOL(current_position[Y_AXIS] + Y_PROBE_OFFSET_FROM_EXTRUDER + 0.0001);
+    SERIAL_PROTOCOLPGM(" Z: ");
+    SERIAL_PROTOCOL(current_position[Z_AXIS] + 0.0001);
+    SERIAL_EOL;
 
-      stow_z_probe(); // Retract Z Servo endstop if available. Z_PROBE_SLED is missed here.
+    clean_up_after_endstop_move(); // Too early. must be done after the stowing.
 
-      report_current_position();
-    }
+    stow_z_probe();
 
-  #endif //!Z_PROBE_SLED
+    report_current_position();
+  }
 
-#endif //AUTO_BED_LEVELING_FEATURE
+#endif // HAS_BED_PROBE
 
 /**
  * G92: Set current position to given X Y Z E
@@ -6875,24 +6857,24 @@ void process_next_command() {
           break;
       #endif
 
-      #if ENABLED(AUTO_BED_LEVELING_FEATURE)
-
-        #if DISABLED(Z_PROBE_SLED)
+      #if HAS_BED_PROBE
 
-          case 30: // G30 Single Z probe
-            gcode_G30();
-            break;
+        case 30: // G30 Single Z probe
+          gcode_G30();
+          break;
 
-        #else // Z_PROBE_SLED
+        #if ENABLED(Z_PROBE_SLED)
 
             case 31: // G31: dock the sled
+              stow_z_probe();
+              break;
             case 32: // G32: undock the sled
-              dock_sled(codenum == 31);
+              deploy_z_probe();
               break;
 
         #endif // Z_PROBE_SLED
 
-      #endif // AUTO_BED_LEVELING_FEATURE
+      #endif // HAS_BED_PROBE
 
       case 90: // G90
         relative_mode = false;
-- 
GitLab