From 6c96f32069f6246fcab0ce8954af5e9ffe28f6fd Mon Sep 17 00:00:00 2001
From: maverikou <maverikou@users.noreply.github.com>
Date: Sat, 21 Mar 2015 12:00:04 +0200
Subject: [PATCH 1/3] Blind fix for #1507

---
 Marlin/Marlin_main.cpp | 21 ++++++++++++++-------
 1 file changed, 14 insertions(+), 7 deletions(-)

diff --git a/Marlin/Marlin_main.cpp b/Marlin/Marlin_main.cpp
index fb7ae61457..9a63654623 100644
--- a/Marlin/Marlin_main.cpp
+++ b/Marlin/Marlin_main.cpp
@@ -1309,7 +1309,11 @@ static void engage_z_probe() {
 static void retract_z_probe() {
   // Retract Z Servo endstop if enabled
   #ifdef SERVO_ENDSTOPS
-    if (servo_endstops[Z_AXIS] > -1) {
+    if (servo_endstops[Z_AXIS] > -1)
+    {
+        do_blocking_move_to(current_position[X_AXIS], current_position[Y_AXIS], Z_RAISE_AFTER_PROBING);
+        st_synchronize();
+    
       #if SERVO_LEVELING
         servos[servo_endstops[Z_AXIS]].attach(0);
       #endif
@@ -1364,10 +1368,16 @@ static void retract_z_probe() {
 
 }
 
-enum ProbeAction { ProbeStay, ProbeEngage, ProbeRetract, ProbeEngageRetract };
+enum ProbeAction
+{
+    ProbeStay              = 0,
+    ProbeEngage            = (1 << 0),
+    ProbeRetract           = (1 << 1),
+    ProbeEngageAndRectract = (ProbeEngage | ProbeRetract),
+};
 
 /// Probe bed height at position (x,y), returns the measured z value
-static float probe_pt(float x, float y, float z_before, ProbeAction retract_action=ProbeEngageRetract, int verbose_level=1) {
+static float probe_pt(float x, float y, float z_before, ProbeAction retract_action=ProbeEngageAndRectract, int verbose_level=1) {
   // move to right place
   do_blocking_move_to(current_position[X_AXIS], current_position[Y_AXIS], z_before);
   do_blocking_move_to(x - X_PROBE_OFFSET_FROM_EXTRUDER, y - Y_PROBE_OFFSET_FROM_EXTRUDER, current_position[Z_AXIS]);
@@ -2330,7 +2340,7 @@ inline void gcode_G28() {
               act = ProbeStay;
           }
           else
-            act = ProbeEngageRetract;
+            act = ProbeEngageAndRectract;
 
           measured_z = probe_pt(xProbe, yProbe, z_before, act, verbose_level);
 
@@ -2445,9 +2455,6 @@ inline void gcode_G28() {
 
     #endif // !AUTO_BED_LEVELING_GRID
 
-    do_blocking_move_to(current_position[X_AXIS], current_position[Y_AXIS], Z_RAISE_AFTER_PROBING);
-    st_synchronize();
-
   #ifndef DELTA
     if (verbose_level > 0)
       plan_bed_level_matrix.debug(" \n\nBed Level Correction Matrix:");

From 0f034dd97e9969b1d65fa46eb48b067ea4254ab4 Mon Sep 17 00:00:00 2001
From: maverikou <maverikou@users.noreply.github.com>
Date: Sun, 22 Mar 2015 09:51:43 +0200
Subject: [PATCH 2/3] Clean up Z_RAISE_AFTER_PROBING to work the same in all
 code paths except Z_PROBE_SLED.

---
 Marlin/Marlin_main.cpp | 22 +++++++++-------------
 1 file changed, 9 insertions(+), 13 deletions(-)

diff --git a/Marlin/Marlin_main.cpp b/Marlin/Marlin_main.cpp
index 9a63654623..b272d73ef3 100644
--- a/Marlin/Marlin_main.cpp
+++ b/Marlin/Marlin_main.cpp
@@ -1326,7 +1326,7 @@ static void retract_z_probe() {
   #elif defined(Z_PROBE_ALLEN_KEY)
     // Move up for safety
     feedrate = homing_feedrate[X_AXIS];
-    destination[Z_AXIS] = current_position[Z_AXIS] + 20;
+    destination[Z_AXIS] = current_position[Z_AXIS] + Z_RAISE_AFTER_PROBING;
     prepare_move_raw();
 
     // Move to the start position to initiate retraction
@@ -1370,26 +1370,26 @@ static void retract_z_probe() {
 
 enum ProbeAction
 {
-    ProbeStay              = 0,
-    ProbeEngage            = (1 << 0),
-    ProbeRetract           = (1 << 1),
-    ProbeEngageAndRectract = (ProbeEngage | ProbeRetract),
+    ProbeStay             = 0,
+    ProbeEngage           = (1 << 0),
+    ProbeRetract          = (1 << 1),
+    ProbeEngageAndRetract = (ProbeEngage | ProbeRetract),
 };
 
 /// Probe bed height at position (x,y), returns the measured z value
-static float probe_pt(float x, float y, float z_before, ProbeAction retract_action=ProbeEngageAndRectract, int verbose_level=1) {
+static float probe_pt(float x, float y, float z_before, ProbeAction retract_action=ProbeEngageAndRetract, int verbose_level=1) {
   // move to right place
   do_blocking_move_to(current_position[X_AXIS], current_position[Y_AXIS], z_before);
   do_blocking_move_to(x - X_PROBE_OFFSET_FROM_EXTRUDER, y - Y_PROBE_OFFSET_FROM_EXTRUDER, current_position[Z_AXIS]);
 
-  #if !defined(Z_PROBE_SLED) && !defined(Z_PROBE_ALLEN_KEY)
+  #if !defined(Z_PROBE_SLED)
     if (retract_action & ProbeEngage) engage_z_probe();
   #endif
 
   run_z_probe();
   float measured_z = current_position[Z_AXIS];
 
-  #if !defined(Z_PROBE_SLED) && !defined(Z_PROBE_ALLEN_KEY)
+  #if !defined(Z_PROBE_SLED)
     if (retract_action & ProbeRetract) retract_z_probe();
   #endif
 
@@ -2231,8 +2231,6 @@ inline void gcode_G28() {
 
     #ifdef Z_PROBE_SLED
       dock_sled(false); // engage (un-dock) the probe
-    #elif not defined(SERVO_ENDSTOPS)
-      engage_z_probe();
     #endif
 
     st_synchronize();
@@ -2340,7 +2338,7 @@ inline void gcode_G28() {
               act = ProbeStay;
           }
           else
-            act = ProbeEngageAndRectract;
+            act = ProbeEngageAndRetract;
 
           measured_z = probe_pt(xProbe, yProbe, z_before, act, verbose_level);
 
@@ -2474,8 +2472,6 @@ inline void gcode_G28() {
 
   #ifdef Z_PROBE_SLED
     dock_sled(true, -SLED_DOCKING_OFFSET); // dock the probe, correcting for over-travel
-  #elif not defined(SERVO_ENDSTOPS)
-    retract_z_probe();
   #endif
     
   #ifdef Z_PROBE_END_SCRIPT

From 15345cc249925c79a90ff0126e1396a99f5e1ecf Mon Sep 17 00:00:00 2001
From: maverikou <maverikou@users.noreply.github.com>
Date: Sun, 22 Mar 2015 13:49:52 +0200
Subject: [PATCH 3/3] Corrected Z_PROBE_ALLEN_KEY behaviour.

---
 Marlin/Marlin_main.cpp | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/Marlin/Marlin_main.cpp b/Marlin/Marlin_main.cpp
index b272d73ef3..cdf9e5c6f3 100644
--- a/Marlin/Marlin_main.cpp
+++ b/Marlin/Marlin_main.cpp
@@ -1311,8 +1311,10 @@ static void retract_z_probe() {
   #ifdef SERVO_ENDSTOPS
     if (servo_endstops[Z_AXIS] > -1)
     {
+      #if Z_RAISE_AFTER_PROBING > 0
         do_blocking_move_to(current_position[X_AXIS], current_position[Y_AXIS], Z_RAISE_AFTER_PROBING);
         st_synchronize();
+      #endif
     
       #if SERVO_LEVELING
         servos[servo_endstops[Z_AXIS]].attach(0);
@@ -1382,14 +1384,14 @@ static float probe_pt(float x, float y, float z_before, ProbeAction retract_acti
   do_blocking_move_to(current_position[X_AXIS], current_position[Y_AXIS], z_before);
   do_blocking_move_to(x - X_PROBE_OFFSET_FROM_EXTRUDER, y - Y_PROBE_OFFSET_FROM_EXTRUDER, current_position[Z_AXIS]);
 
-  #if !defined(Z_PROBE_SLED)
+  #if !defined(Z_PROBE_SLED) && !defined(Z_PROBE_ALLEN_KEY)
     if (retract_action & ProbeEngage) engage_z_probe();
   #endif
 
   run_z_probe();
   float measured_z = current_position[Z_AXIS];
 
-  #if !defined(Z_PROBE_SLED)
+  #if !defined(Z_PROBE_SLED) && !defined(Z_PROBE_ALLEN_KEY)
     if (retract_action & ProbeRetract) retract_z_probe();
   #endif
 
@@ -2231,6 +2233,8 @@ inline void gcode_G28() {
 
     #ifdef Z_PROBE_SLED
       dock_sled(false); // engage (un-dock) the probe
+    #elif defined(Z_PROBE_ALLEN_KEY)
+      engage_z_probe();
     #endif
 
     st_synchronize();
@@ -2472,6 +2476,8 @@ inline void gcode_G28() {
 
   #ifdef Z_PROBE_SLED
     dock_sled(true, -SLED_DOCKING_OFFSET); // dock the probe, correcting for over-travel
+  #elif defined(Z_PROBE_ALLEN_KEY)
+    retract_z_probe();
   #endif
     
   #ifdef Z_PROBE_END_SCRIPT