From 458e1aea41db013a979019c32ca2b3c2c3806bc6 Mon Sep 17 00:00:00 2001
From: studiodyne <diabolomix@hotmail.fr>
Date: Sun, 28 Aug 2022 00:43:32 +0200
Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20XY=5FCOUNTERPART=5FBACKOFF=5FMM?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 Marlin/Configuration_adv.h   |  1 +
 Marlin/src/module/motion.cpp | 11 +++++++++++
 2 files changed, 12 insertions(+)

diff --git a/Marlin/Configuration_adv.h b/Marlin/Configuration_adv.h
index 85a69cc4832..df1c1d245f2 100644
--- a/Marlin/Configuration_adv.h
+++ b/Marlin/Configuration_adv.h
@@ -874,6 +874,7 @@
 #define HOMING_BUMP_DIVISOR { 2, 2, 4 }       // Re-Bump Speed Divisor (Divides the Homing Feedrate)
 
 //#define HOMING_BACKOFF_POST_MM { 2, 2, 2 }  // (linear=mm, rotational=°) Backoff from endstops after homing
+//#define XY_COUNTERPART_BACKOFF_MM 0         // (mm) Backoff X after homing Y, and vice-versa
 
 //#define QUICK_HOME                          // If G28 contains XY do a diagonal move first
 //#define HOME_Y_BEFORE_X                     // If G28 contains XY home Y before X
diff --git a/Marlin/src/module/motion.cpp b/Marlin/src/module/motion.cpp
index 6101022fd49..c0503c621df 100644
--- a/Marlin/src/module/motion.cpp
+++ b/Marlin/src/module/motion.cpp
@@ -1994,6 +1994,17 @@ void prepare_line_to_destination() {
       }
     #endif
 
+    //
+    // Back away to prevent opposite endstop damage
+    //
+    #if !defined(SENSORLESS_BACKOFF_MM) && XY_COUNTERPART_BACKOFF_MM
+      if (!(axis_was_homed(X_AXIS) || axis_was_homed(Y_AXIS)) && (axis == X_AXIS || axis == Y_AXIS)) {
+        const AxisEnum opposite_axis = axis == X_AXIS ? Y_AXIS : X_AXIS;
+        const float backoff_length = -ABS(XY_COUNTERPART_BACKOFF_MM) * home_dir(opposite_axis);
+        do_homing_move(opposite_axis, backoff_length, homing_feedrate(opposite_axis));
+      }
+    #endif
+
     // Determine if a homing bump will be done and the bumps distance
     // When homing Z with probe respect probe clearance
     const bool use_probe_bump = TERN0(HOMING_Z_WITH_PROBE, axis == Z_AXIS && home_bump_mm(axis));