From 468e437390afdb1de7059e7f9049d0b30c312024 Mon Sep 17 00:00:00 2001 From: deirdreobyrne Date: Thu, 25 Feb 2021 10:49:34 +0000 Subject: [PATCH] Allow Zero Endstops (e.g., for CNC) (#21120) Co-authored-by: Scott Lahteine --- Marlin/src/feature/bedlevel/ubl/ubl_G29.cpp | 4 +- Marlin/src/gcode/bedlevel/G26.cpp | 22 +- Marlin/src/gcode/calibrate/M48.cpp | 2 +- Marlin/src/inc/Conditionals_LCD.h | 7 + Marlin/src/inc/SanityCheck.h | 68 +- Marlin/src/libs/L64XX/L64XX_Marlin.cpp | 12 +- Marlin/src/module/motion.cpp | 1265 +++++++++---------- Marlin/src/module/motion.h | 71 +- Marlin/src/module/planner.h | 8 +- Marlin/src/module/probe.h | 8 +- buildroot/tests/teensy31-tests | 8 + 11 files changed, 757 insertions(+), 718 deletions(-) diff --git a/Marlin/src/feature/bedlevel/ubl/ubl_G29.cpp b/Marlin/src/feature/bedlevel/ubl/ubl_G29.cpp index 9bd7f63a7e..6ceb571ee2 100644 --- a/Marlin/src/feature/bedlevel/ubl/ubl_G29.cpp +++ b/Marlin/src/feature/bedlevel/ubl/ubl_G29.cpp @@ -1118,8 +1118,8 @@ bool unified_bed_leveling::g29_parameter_parsing() { } // If X or Y are not valid, use center of the bed values - if (!WITHIN(sx, X_MIN_BED, X_MAX_BED)) sx = X_CENTER; - if (!WITHIN(sy, Y_MIN_BED, Y_MAX_BED)) sy = Y_CENTER; + if (!COORDINATE_OKAY(sx, X_MIN_BED, X_MAX_BED)) sx = X_CENTER; + if (!COORDINATE_OKAY(sy, Y_MIN_BED, Y_MAX_BED)) sy = Y_CENTER; if (err_flag) return UBL_ERR; diff --git a/Marlin/src/gcode/bedlevel/G26.cpp b/Marlin/src/gcode/bedlevel/G26.cpp index 9e3cad34f2..ed29959055 100644 --- a/Marlin/src/gcode/bedlevel/G26.cpp +++ b/Marlin/src/gcode/bedlevel/G26.cpp @@ -319,9 +319,13 @@ inline bool look_for_lines_to_connect() { s.x = _GET_MESH_X( i ) + (INTERSECTION_CIRCLE_RADIUS - (CROSSHAIRS_SIZE)); // right edge e.x = _GET_MESH_X(i + 1) - (INTERSECTION_CIRCLE_RADIUS - (CROSSHAIRS_SIZE)); // left edge - LIMIT(s.x, X_MIN_POS + 1, X_MAX_POS - 1); - s.y = e.y = constrain(_GET_MESH_Y(j), Y_MIN_POS + 1, Y_MAX_POS - 1); - LIMIT(e.x, X_MIN_POS + 1, X_MAX_POS - 1); + #if HAS_ENDSTOPS + LIMIT(s.x, X_MIN_POS + 1, X_MAX_POS - 1); + s.y = e.y = constrain(_GET_MESH_Y(j), Y_MIN_POS + 1, Y_MAX_POS - 1); + LIMIT(e.x, X_MIN_POS + 1, X_MAX_POS - 1); + #else + s.y = e.y = _GET_MESH_Y(j); + #endif if (position_is_reachable(s.x, s.y) && position_is_reachable(e.x, e.y)) print_line_from_here_to_there(s, e); @@ -339,9 +343,13 @@ inline bool look_for_lines_to_connect() { s.y = _GET_MESH_Y( j ) + (INTERSECTION_CIRCLE_RADIUS - (CROSSHAIRS_SIZE)); // top edge e.y = _GET_MESH_Y(j + 1) - (INTERSECTION_CIRCLE_RADIUS - (CROSSHAIRS_SIZE)); // bottom edge - s.x = e.x = constrain(_GET_MESH_X(i), X_MIN_POS + 1, X_MAX_POS - 1); - LIMIT(s.y, Y_MIN_POS + 1, Y_MAX_POS - 1); - LIMIT(e.y, Y_MIN_POS + 1, Y_MAX_POS - 1); + #if HAS_ENDSTOPS + s.x = e.x = constrain(_GET_MESH_X(i), X_MIN_POS + 1, X_MAX_POS - 1); + LIMIT(s.y, Y_MIN_POS + 1, Y_MAX_POS - 1); + LIMIT(e.y, Y_MIN_POS + 1, Y_MAX_POS - 1); + #else + s.x = e.x = _GET_MESH_X(i); + #endif if (position_is_reachable(s.x, s.y) && position_is_reachable(e.x, e.y)) print_line_from_here_to_there(s, e); @@ -826,7 +834,7 @@ void GcodeSuite::G26() { #if IS_KINEMATIC // Check to make sure this segment is entirely on the bed, skip if not. if (!position_is_reachable(p) || !position_is_reachable(q)) continue; - #else + #elif HAS_ENDSTOPS LIMIT(p.x, X_MIN_POS + 1, X_MAX_POS - 1); // Prevent hitting the endstops LIMIT(p.y, Y_MIN_POS + 1, Y_MAX_POS - 1); LIMIT(q.x, X_MIN_POS + 1, X_MAX_POS - 1); diff --git a/Marlin/src/gcode/calibrate/M48.cpp b/Marlin/src/gcode/calibrate/M48.cpp index 63d2370697..0c6176173c 100644 --- a/Marlin/src/gcode/calibrate/M48.cpp +++ b/Marlin/src/gcode/calibrate/M48.cpp @@ -202,7 +202,7 @@ void GcodeSuite::M48() { if (verbose_level > 3) SERIAL_ECHOLNPAIR_P(PSTR("Moving inward: X"), next_pos.x, SP_Y_STR, next_pos.y); } - #else + #elif HAS_ENDSTOPS // For a rectangular bed just keep the probe in bounds LIMIT(next_pos.x, X_MIN_POS, X_MAX_POS); LIMIT(next_pos.y, Y_MIN_POS, Y_MAX_POS); diff --git a/Marlin/src/inc/Conditionals_LCD.h b/Marlin/src/inc/Conditionals_LCD.h index 7ac4fe4c31..cff1424eb3 100644 --- a/Marlin/src/inc/Conditionals_LCD.h +++ b/Marlin/src/inc/Conditionals_LCD.h @@ -1195,3 +1195,10 @@ #define TOUCH_ORIENTATION TOUCH_LANDSCAPE #endif #endif + +#if ANY(USE_XMIN_PLUG, USE_YMIN_PLUG, USE_ZMIN_PLUG, USE_XMAX_PLUG, USE_YMAX_PLUG, USE_ZMAX_PLUG) + #define HAS_ENDSTOPS 1 + #define COORDINATE_OKAY(N,L,H) WITHIN(N,L,H) +#else + #define COORDINATE_OKAY(N,L,H) true +#endif diff --git a/Marlin/src/inc/SanityCheck.h b/Marlin/src/inc/SanityCheck.h index ba69b4792e..d6d2a600a0 100644 --- a/Marlin/src/inc/SanityCheck.h +++ b/Marlin/src/inc/SanityCheck.h @@ -2016,39 +2016,41 @@ static_assert(hbm[Z_AXIS] >= 0, "HOMING_BUMP_MM.Z must be greater than or equal && !(ENABLED(A##_MULTI_ENDSTOPS) && WITHIN(A##2_USE_ENDSTOP, _##P##MAX_, _##P##MIN_)) ) #define _AXIS_PLUG_UNUSED_TEST(A) (_PLUG_UNUSED_TEST(A,X) && _PLUG_UNUSED_TEST(A,Y) && _PLUG_UNUSED_TEST(A,Z)) -// At least 3 endstop plugs must be used -#if _AXIS_PLUG_UNUSED_TEST(X) - #error "You must enable USE_XMIN_PLUG or USE_XMAX_PLUG." -#endif -#if _AXIS_PLUG_UNUSED_TEST(Y) - #error "You must enable USE_YMIN_PLUG or USE_YMAX_PLUG." -#endif -#if _AXIS_PLUG_UNUSED_TEST(Z) - #error "You must enable USE_ZMIN_PLUG or USE_ZMAX_PLUG." -#endif - -// Delta and Cartesian use 3 homing endstops -#if NONE(IS_SCARA, SPI_ENDSTOPS) - #if X_HOME_DIR < 0 && DISABLED(USE_XMIN_PLUG) - #error "Enable USE_XMIN_PLUG when homing X to MIN." - #elif X_HOME_DIR > 0 && DISABLED(USE_XMAX_PLUG) - #error "Enable USE_XMAX_PLUG when homing X to MAX." - #elif Y_HOME_DIR < 0 && DISABLED(USE_YMIN_PLUG) - #error "Enable USE_YMIN_PLUG when homing Y to MIN." - #elif Y_HOME_DIR > 0 && DISABLED(USE_YMAX_PLUG) - #error "Enable USE_YMAX_PLUG when homing Y to MAX." - #endif -#endif - -// Z homing direction and plug usage flags -#if Z_HOME_DIR < 0 && NONE(USE_ZMIN_PLUG, HOMING_Z_WITH_PROBE) - #error "Enable USE_ZMIN_PLUG when homing Z to MIN." -#elif Z_HOME_DIR > 0 && ENABLED(USE_PROBE_FOR_Z_HOMING) - #error "Z_HOME_DIR must be -1 when homing Z with the probe." -#elif BOTH(HOMING_Z_WITH_PROBE, Z_MULTI_ENDSTOPS) - #error "Z_MULTI_ENDSTOPS is incompatible with USE_PROBE_FOR_Z_HOMING." -#elif Z_HOME_DIR > 0 && DISABLED(USE_ZMAX_PLUG) - #error "Enable USE_ZMAX_PLUG when homing Z to MAX." +// A machine with endstops must have a minimum of 3 +#if HAS_ENDSTOPS + #if _AXIS_PLUG_UNUSED_TEST(X) + #error "You must enable USE_XMIN_PLUG or USE_XMAX_PLUG." + #endif + #if _AXIS_PLUG_UNUSED_TEST(Y) + #error "You must enable USE_YMIN_PLUG or USE_YMAX_PLUG." + #endif + #if _AXIS_PLUG_UNUSED_TEST(Z) + #error "You must enable USE_ZMIN_PLUG or USE_ZMAX_PLUG." + #endif + + // Delta and Cartesian use 3 homing endstops + #if NONE(IS_SCARA, SPI_ENDSTOPS) + #if X_HOME_DIR < 0 && DISABLED(USE_XMIN_PLUG) + #error "Enable USE_XMIN_PLUG when homing X to MIN." + #elif X_HOME_DIR > 0 && DISABLED(USE_XMAX_PLUG) + #error "Enable USE_XMAX_PLUG when homing X to MAX." + #elif Y_HOME_DIR < 0 && DISABLED(USE_YMIN_PLUG) + #error "Enable USE_YMIN_PLUG when homing Y to MIN." + #elif Y_HOME_DIR > 0 && DISABLED(USE_YMAX_PLUG) + #error "Enable USE_YMAX_PLUG when homing Y to MAX." + #endif + #endif + + // Z homing direction and plug usage flags + #if Z_HOME_DIR < 0 && NONE(USE_ZMIN_PLUG, HOMING_Z_WITH_PROBE) + #error "Enable USE_ZMIN_PLUG when homing Z to MIN." + #elif Z_HOME_DIR > 0 && ENABLED(USE_PROBE_FOR_Z_HOMING) + #error "Z_HOME_DIR must be -1 when homing Z with the probe." + #elif BOTH(HOMING_Z_WITH_PROBE, Z_MULTI_ENDSTOPS) + #error "Z_MULTI_ENDSTOPS is incompatible with USE_PROBE_FOR_Z_HOMING." + #elif Z_HOME_DIR > 0 && DISABLED(USE_ZMAX_PLUG) + #error "Enable USE_ZMAX_PLUG when homing Z to MAX." + #endif #endif #if BOTH(HOME_Z_FIRST, USE_PROBE_FOR_Z_HOMING) diff --git a/Marlin/src/libs/L64XX/L64XX_Marlin.cpp b/Marlin/src/libs/L64XX/L64XX_Marlin.cpp index 7d36a02d67..0358f3f59a 100644 --- a/Marlin/src/libs/L64XX/L64XX_Marlin.cpp +++ b/Marlin/src/libs/L64XX/L64XX_Marlin.cpp @@ -446,10 +446,8 @@ uint8_t L64XX_Marlin::get_user_input(uint8_t &driver_count, L64XX_axis_t axis_in position_max = X_center + displacement; echo_min_max('X', position_min, position_max); if (false - #ifdef X_MIN_POS + #if HAS_ENDSTOPS || position_min < (X_MIN_POS) - #endif - #ifdef X_MAX_POS || position_max > (X_MAX_POS) #endif ) { @@ -463,10 +461,8 @@ uint8_t L64XX_Marlin::get_user_input(uint8_t &driver_count, L64XX_axis_t axis_in position_max = Y_center + displacement; echo_min_max('Y', position_min, position_max); if (false - #ifdef Y_MIN_POS + #if HAS_ENDSTOPS || position_min < (Y_MIN_POS) - #endif - #ifdef Y_MAX_POS || position_max > (Y_MAX_POS) #endif ) { @@ -480,10 +476,8 @@ uint8_t L64XX_Marlin::get_user_input(uint8_t &driver_count, L64XX_axis_t axis_in position_max = Z_center + displacement; echo_min_max('Z', position_min, position_max); if (false - #ifdef Z_MIN_POS + #if HAS_ENDSTOPS || position_min < (Z_MIN_POS) - #endif - #ifdef Z_MAX_POS || position_max > (Z_MAX_POS) #endif ) { diff --git a/Marlin/src/module/motion.cpp b/Marlin/src/module/motion.cpp index 8dc84c33d6..10d3585a73 100644 --- a/Marlin/src/module/motion.cpp +++ b/Marlin/src/module/motion.cpp @@ -74,17 +74,6 @@ #define DEBUG_OUT ENABLED(DEBUG_LEVELING_FEATURE) #include "../core/debug_out.h" -/** - * axis_homed - * Flags that each linear axis was homed. - * XYZ on cartesian, ABC on delta, ABZ on SCARA. - * - * axis_trusted - * Flags that the position is trusted in each linear axis. Set when homed. - * Cleared whenever a stepper powers off, potentially losing its position. - */ -uint8_t axis_homed, axis_trusted; // = 0 - // Relative Mode. Enable with G91, disable with G90. bool relative_mode; // = false; @@ -1122,760 +1111,768 @@ void prepare_line_to_destination() { current_position = destination; } -uint8_t axes_should_home(uint8_t axis_bits/*=0x07*/) { - #define SHOULD_HOME(A) TERN(HOME_AFTER_DEACTIVATE, axis_is_trusted, axis_was_homed)(A) - // Clear test bits that are trusted - if (TEST(axis_bits, X_AXIS) && SHOULD_HOME(X_AXIS)) CBI(axis_bits, X_AXIS); - if (TEST(axis_bits, Y_AXIS) && SHOULD_HOME(Y_AXIS)) CBI(axis_bits, Y_AXIS); - if (TEST(axis_bits, Z_AXIS) && SHOULD_HOME(Z_AXIS)) CBI(axis_bits, Z_AXIS); - return axis_bits; -} +#if HAS_ENDSTOPS -bool homing_needed_error(uint8_t axis_bits/*=0x07*/) { - if ((axis_bits = axes_should_home(axis_bits))) { - PGM_P home_first = GET_TEXT(MSG_HOME_FIRST); - char msg[strlen_P(home_first)+1]; - sprintf_P(msg, home_first, - TEST(axis_bits, X_AXIS) ? "X" : "", - TEST(axis_bits, Y_AXIS) ? "Y" : "", - TEST(axis_bits, Z_AXIS) ? "Z" : "" - ); - SERIAL_ECHO_START(); - SERIAL_ECHOLN(msg); - TERN_(HAS_DISPLAY, ui.set_status(msg)); - return true; + uint8_t axis_homed, axis_trusted; // = 0 + + uint8_t axes_should_home(uint8_t axis_bits/*=0x07*/) { + #define SHOULD_HOME(A) TERN(HOME_AFTER_DEACTIVATE, axis_is_trusted, axis_was_homed)(A) + // Clear test bits that are trusted + if (TEST(axis_bits, X_AXIS) && SHOULD_HOME(X_AXIS)) CBI(axis_bits, X_AXIS); + if (TEST(axis_bits, Y_AXIS) && SHOULD_HOME(Y_AXIS)) CBI(axis_bits, Y_AXIS); + if (TEST(axis_bits, Z_AXIS) && SHOULD_HOME(Z_AXIS)) CBI(axis_bits, Z_AXIS); + return axis_bits; } - return false; -} -/** - * Homing bump feedrate (mm/s) - */ -feedRate_t get_homing_bump_feedrate(const AxisEnum axis) { - #if HOMING_Z_WITH_PROBE - if (axis == Z_AXIS) return MMM_TO_MMS(Z_PROBE_SPEED_SLOW); - #endif - static const uint8_t homing_bump_divisor[] PROGMEM = HOMING_BUMP_DIVISOR; - uint8_t hbd = pgm_read_byte(&homing_bump_divisor[axis]); - if (hbd < 1) { - hbd = 10; - SERIAL_ECHO_MSG("Warning: Homing Bump Divisor < 1"); + bool homing_needed_error(uint8_t axis_bits/*=0x07*/) { + if ((axis_bits = axes_should_home(axis_bits))) { + PGM_P home_first = GET_TEXT(MSG_HOME_FIRST); + char msg[strlen_P(home_first)+1]; + sprintf_P(msg, home_first, + TEST(axis_bits, X_AXIS) ? "X" : "", + TEST(axis_bits, Y_AXIS) ? "Y" : "", + TEST(axis_bits, Z_AXIS) ? "Z" : "" + ); + SERIAL_ECHO_START(); + SERIAL_ECHOLN(msg); + TERN_(HAS_DISPLAY, ui.set_status(msg)); + return true; + } + return false; } - return homing_feedrate(axis) / float(hbd); -} -#if ENABLED(SENSORLESS_HOMING) /** - * Set sensorless homing if the axis has it, accounting for Core Kinematics. + * Homing bump feedrate (mm/s) */ - sensorless_t start_sensorless_homing_per_axis(const AxisEnum axis) { - sensorless_t stealth_states { false }; - - switch (axis) { - default: break; - #if X_SENSORLESS - case X_AXIS: - stealth_states.x = tmc_enable_stallguard(stepperX); - #if AXIS_HAS_STALLGUARD(X2) - stealth_states.x2 = tmc_enable_stallguard(stepperX2); - #endif - #if EITHER(CORE_IS_XY, MARKFORGED_XY) && Y_SENSORLESS - stealth_states.y = tmc_enable_stallguard(stepperY); - #elif CORE_IS_XZ && Z_SENSORLESS - stealth_states.z = tmc_enable_stallguard(stepperZ); - #endif - break; - #endif - #if Y_SENSORLESS - case Y_AXIS: - stealth_states.y = tmc_enable_stallguard(stepperY); - #if AXIS_HAS_STALLGUARD(Y2) - stealth_states.y2 = tmc_enable_stallguard(stepperY2); - #endif - #if EITHER(CORE_IS_XY, MARKFORGED_XY) && X_SENSORLESS - stealth_states.x = tmc_enable_stallguard(stepperX); - #elif CORE_IS_YZ && Z_SENSORLESS - stealth_states.z = tmc_enable_stallguard(stepperZ); - #endif - break; - #endif - #if Z_SENSORLESS - case Z_AXIS: - stealth_states.z = tmc_enable_stallguard(stepperZ); - #if AXIS_HAS_STALLGUARD(Z2) - stealth_states.z2 = tmc_enable_stallguard(stepperZ2); - #endif - #if AXIS_HAS_STALLGUARD(Z3) - stealth_states.z3 = tmc_enable_stallguard(stepperZ3); - #endif - #if AXIS_HAS_STALLGUARD(Z4) - stealth_states.z4 = tmc_enable_stallguard(stepperZ4); - #endif - #if CORE_IS_XZ && X_SENSORLESS - stealth_states.x = tmc_enable_stallguard(stepperX); - #elif CORE_IS_YZ && Y_SENSORLESS - stealth_states.y = tmc_enable_stallguard(stepperY); - #endif - break; - #endif + feedRate_t get_homing_bump_feedrate(const AxisEnum axis) { + #if HOMING_Z_WITH_PROBE + if (axis == Z_AXIS) return MMM_TO_MMS(Z_PROBE_SPEED_SLOW); + #endif + static const uint8_t homing_bump_divisor[] PROGMEM = HOMING_BUMP_DIVISOR; + uint8_t hbd = pgm_read_byte(&homing_bump_divisor[axis]); + if (hbd < 1) { + hbd = 10; + SERIAL_ECHO_MSG("Warning: Homing Bump Divisor < 1"); } + return homing_feedrate(axis) / float(hbd); + } + + #if ENABLED(SENSORLESS_HOMING) + /** + * Set sensorless homing if the axis has it, accounting for Core Kinematics. + */ + sensorless_t start_sensorless_homing_per_axis(const AxisEnum axis) { + sensorless_t stealth_states { false }; - #if ENABLED(SPI_ENDSTOPS) switch (axis) { - case X_AXIS: if (ENABLED(X_SPI_SENSORLESS)) endstops.tmc_spi_homing.x = true; break; - case Y_AXIS: if (ENABLED(Y_SPI_SENSORLESS)) endstops.tmc_spi_homing.y = true; break; - case Z_AXIS: if (ENABLED(Z_SPI_SENSORLESS)) endstops.tmc_spi_homing.z = true; break; default: break; + #if X_SENSORLESS + case X_AXIS: + stealth_states.x = tmc_enable_stallguard(stepperX); + #if AXIS_HAS_STALLGUARD(X2) + stealth_states.x2 = tmc_enable_stallguard(stepperX2); + #endif + #if EITHER(CORE_IS_XY, MARKFORGED_XY) && Y_SENSORLESS + stealth_states.y = tmc_enable_stallguard(stepperY); + #elif CORE_IS_XZ && Z_SENSORLESS + stealth_states.z = tmc_enable_stallguard(stepperZ); + #endif + break; + #endif + #if Y_SENSORLESS + case Y_AXIS: + stealth_states.y = tmc_enable_stallguard(stepperY); + #if AXIS_HAS_STALLGUARD(Y2) + stealth_states.y2 = tmc_enable_stallguard(stepperY2); + #endif + #if EITHER(CORE_IS_XY, MARKFORGED_XY) && X_SENSORLESS + stealth_states.x = tmc_enable_stallguard(stepperX); + #elif CORE_IS_YZ && Z_SENSORLESS + stealth_states.z = tmc_enable_stallguard(stepperZ); + #endif + break; + #endif + #if Z_SENSORLESS + case Z_AXIS: + stealth_states.z = tmc_enable_stallguard(stepperZ); + #if AXIS_HAS_STALLGUARD(Z2) + stealth_states.z2 = tmc_enable_stallguard(stepperZ2); + #endif + #if AXIS_HAS_STALLGUARD(Z3) + stealth_states.z3 = tmc_enable_stallguard(stepperZ3); + #endif + #if AXIS_HAS_STALLGUARD(Z4) + stealth_states.z4 = tmc_enable_stallguard(stepperZ4); + #endif + #if CORE_IS_XZ && X_SENSORLESS + stealth_states.x = tmc_enable_stallguard(stepperX); + #elif CORE_IS_YZ && Y_SENSORLESS + stealth_states.y = tmc_enable_stallguard(stepperY); + #endif + break; + #endif } - #endif - TERN_(IMPROVE_HOMING_RELIABILITY, sg_guard_period = millis() + default_sg_guard_duration); + #if ENABLED(SPI_ENDSTOPS) + switch (axis) { + case X_AXIS: if (ENABLED(X_SPI_SENSORLESS)) endstops.tmc_spi_homing.x = true; break; + case Y_AXIS: if (ENABLED(Y_SPI_SENSORLESS)) endstops.tmc_spi_homing.y = true; break; + case Z_AXIS: if (ENABLED(Z_SPI_SENSORLESS)) endstops.tmc_spi_homing.z = true; break; + default: break; + } + #endif - return stealth_states; - } + TERN_(IMPROVE_HOMING_RELIABILITY, sg_guard_period = millis() + default_sg_guard_duration); - void end_sensorless_homing_per_axis(const AxisEnum axis, sensorless_t enable_stealth) { - switch (axis) { - default: break; - #if X_SENSORLESS - case X_AXIS: - tmc_disable_stallguard(stepperX, enable_stealth.x); - #if AXIS_HAS_STALLGUARD(X2) - tmc_disable_stallguard(stepperX2, enable_stealth.x2); - #endif - #if EITHER(CORE_IS_XY, MARKFORGED_XY) && Y_SENSORLESS - tmc_disable_stallguard(stepperY, enable_stealth.y); - #elif CORE_IS_XZ && Z_SENSORLESS - tmc_disable_stallguard(stepperZ, enable_stealth.z); - #endif - break; - #endif - #if Y_SENSORLESS - case Y_AXIS: - tmc_disable_stallguard(stepperY, enable_stealth.y); - #if AXIS_HAS_STALLGUARD(Y2) - tmc_disable_stallguard(stepperY2, enable_stealth.y2); - #endif - #if EITHER(CORE_IS_XY, MARKFORGED_XY) && X_SENSORLESS - tmc_disable_stallguard(stepperX, enable_stealth.x); - #elif CORE_IS_YZ && Z_SENSORLESS - tmc_disable_stallguard(stepperZ, enable_stealth.z); - #endif - break; - #endif - #if Z_SENSORLESS - case Z_AXIS: - tmc_disable_stallguard(stepperZ, enable_stealth.z); - #if AXIS_HAS_STALLGUARD(Z2) - tmc_disable_stallguard(stepperZ2, enable_stealth.z2); - #endif - #if AXIS_HAS_STALLGUARD(Z3) - tmc_disable_stallguard(stepperZ3, enable_stealth.z3); - #endif - #if AXIS_HAS_STALLGUARD(Z4) - tmc_disable_stallguard(stepperZ4, enable_stealth.z4); - #endif - #if CORE_IS_XZ && X_SENSORLESS - tmc_disable_stallguard(stepperX, enable_stealth.x); - #elif CORE_IS_YZ && Y_SENSORLESS - tmc_disable_stallguard(stepperY, enable_stealth.y); - #endif - break; - #endif + return stealth_states; } - #if ENABLED(SPI_ENDSTOPS) + void end_sensorless_homing_per_axis(const AxisEnum axis, sensorless_t enable_stealth) { switch (axis) { - case X_AXIS: if (ENABLED(X_SPI_SENSORLESS)) endstops.tmc_spi_homing.x = false; break; - case Y_AXIS: if (ENABLED(Y_SPI_SENSORLESS)) endstops.tmc_spi_homing.y = false; break; - case Z_AXIS: if (ENABLED(Z_SPI_SENSORLESS)) endstops.tmc_spi_homing.z = false; break; default: break; + #if X_SENSORLESS + case X_AXIS: + tmc_disable_stallguard(stepperX, enable_stealth.x); + #if AXIS_HAS_STALLGUARD(X2) + tmc_disable_stallguard(stepperX2, enable_stealth.x2); + #endif + #if EITHER(CORE_IS_XY, MARKFORGED_XY) && Y_SENSORLESS + tmc_disable_stallguard(stepperY, enable_stealth.y); + #elif CORE_IS_XZ && Z_SENSORLESS + tmc_disable_stallguard(stepperZ, enable_stealth.z); + #endif + break; + #endif + #if Y_SENSORLESS + case Y_AXIS: + tmc_disable_stallguard(stepperY, enable_stealth.y); + #if AXIS_HAS_STALLGUARD(Y2) + tmc_disable_stallguard(stepperY2, enable_stealth.y2); + #endif + #if EITHER(CORE_IS_XY, MARKFORGED_XY) && X_SENSORLESS + tmc_disable_stallguard(stepperX, enable_stealth.x); + #elif CORE_IS_YZ && Z_SENSORLESS + tmc_disable_stallguard(stepperZ, enable_stealth.z); + #endif + break; + #endif + #if Z_SENSORLESS + case Z_AXIS: + tmc_disable_stallguard(stepperZ, enable_stealth.z); + #if AXIS_HAS_STALLGUARD(Z2) + tmc_disable_stallguard(stepperZ2, enable_stealth.z2); + #endif + #if AXIS_HAS_STALLGUARD(Z3) + tmc_disable_stallguard(stepperZ3, enable_stealth.z3); + #endif + #if AXIS_HAS_STALLGUARD(Z4) + tmc_disable_stallguard(stepperZ4, enable_stealth.z4); + #endif + #if CORE_IS_XZ && X_SENSORLESS + tmc_disable_stallguard(stepperX, enable_stealth.x); + #elif CORE_IS_YZ && Y_SENSORLESS + tmc_disable_stallguard(stepperY, enable_stealth.y); + #endif + break; + #endif } - #endif - } -#endif // SENSORLESS_HOMING - -/** - * Home an individual linear axis - */ -void do_homing_move(const AxisEnum axis, const float distance, const feedRate_t fr_mm_s=0.0, const bool final_approach=true) { - DEBUG_SECTION(log_move, "do_homing_move", DEBUGGING(LEVELING)); - - const feedRate_t home_fr_mm_s = fr_mm_s ?: homing_feedrate(axis); - - if (DEBUGGING(LEVELING)) { - DEBUG_ECHOPAIR("...(", AS_CHAR(axis_codes[axis]), ", ", distance, ", "); - if (fr_mm_s) - DEBUG_ECHO(fr_mm_s); - else - DEBUG_ECHOPAIR("[", home_fr_mm_s, "]"); - DEBUG_ECHOLNPGM(")"); - } - - // Only do some things when moving towards an endstop - const int8_t axis_home_dir = TERN0(DUAL_X_CARRIAGE, axis == X_AXIS) - ? x_home_dir(active_extruder) : home_dir(axis); - const bool is_home_dir = (axis_home_dir > 0) == (distance > 0); - - #if ENABLED(SENSORLESS_HOMING) - sensorless_t stealth_states; - #endif - - if (is_home_dir) { - - if (TERN0(HOMING_Z_WITH_PROBE, axis == Z_AXIS)) { - #if ALL(HAS_HEATED_BED, WAIT_FOR_BED_HEATER) - // Wait for bed to heat back up between probing points - thermalManager.wait_for_bed_heating(); + #if ENABLED(SPI_ENDSTOPS) + switch (axis) { + case X_AXIS: if (ENABLED(X_SPI_SENSORLESS)) endstops.tmc_spi_homing.x = false; break; + case Y_AXIS: if (ENABLED(Y_SPI_SENSORLESS)) endstops.tmc_spi_homing.y = false; break; + case Z_AXIS: if (ENABLED(Z_SPI_SENSORLESS)) endstops.tmc_spi_homing.z = false; break; + default: break; + } #endif - - TERN_(HAS_QUIET_PROBING, if (final_approach) probe.set_probing_paused(true)); } - // Disable stealthChop if used. Enable diag1 pin on driver. - TERN_(SENSORLESS_HOMING, stealth_states = start_sensorless_homing_per_axis(axis)); - } - - #if IS_SCARA - // Tell the planner the axis is at 0 - current_position[axis] = 0; - sync_plan_position(); - current_position[axis] = distance; - line_to_current_position(home_fr_mm_s); - #else - // Get the ABC or XYZ positions in mm - abce_pos_t target = planner.get_axis_positions_mm(); + #endif // SENSORLESS_HOMING - target[axis] = 0; // Set the single homing axis to 0 - planner.set_machine_position_mm(target); // Update the machine position - - #if HAS_DIST_MM_ARG - const xyze_float_t cart_dist_mm{0}; - #endif + /** + * Home an individual linear axis + */ + void do_homing_move(const AxisEnum axis, const float distance, const feedRate_t fr_mm_s=0.0, const bool final_approach=true) { + DEBUG_SECTION(log_move, "do_homing_move", DEBUGGING(LEVELING)); - // Set delta/cartesian axes directly - target[axis] = distance; // The move will be towards the endstop - planner.buffer_segment(target - #if HAS_DIST_MM_ARG - , cart_dist_mm - #endif - , home_fr_mm_s, active_extruder - ); - #endif + const feedRate_t home_fr_mm_s = fr_mm_s ?: homing_feedrate(axis); - planner.synchronize(); + if (DEBUGGING(LEVELING)) { + DEBUG_ECHOPAIR("...(", AS_CHAR(axis_codes[axis]), ", ", distance, ", "); + if (fr_mm_s) + DEBUG_ECHO(fr_mm_s); + else + DEBUG_ECHOPAIR("[", home_fr_mm_s, "]"); + DEBUG_ECHOLNPGM(")"); + } - if (is_home_dir) { + // Only do some things when moving towards an endstop + const int8_t axis_home_dir = TERN0(DUAL_X_CARRIAGE, axis == X_AXIS) + ? x_home_dir(active_extruder) : home_dir(axis); + const bool is_home_dir = (axis_home_dir > 0) == (distance > 0); - #if HOMING_Z_WITH_PROBE && HAS_QUIET_PROBING - if (axis == Z_AXIS && final_approach) probe.set_probing_paused(false); + #if ENABLED(SENSORLESS_HOMING) + sensorless_t stealth_states; #endif - endstops.validate_homing_move(); + if (is_home_dir) { - // Re-enable stealthChop if used. Disable diag1 pin on driver. - TERN_(SENSORLESS_HOMING, end_sensorless_homing_per_axis(axis, stealth_states)); - } -} - -/** - * Set an axis' current position to its home position (after homing). - * - * For Core and Cartesian robots this applies one-to-one when an - * individual axis has been homed. - * - * DELTA should wait until all homing is done before setting the XYZ - * current_position to home, because homing is a single operation. - * In the case where the axis positions are trusted and previously - * homed, DELTA could home to X or Y individually by moving either one - * to the center. However, homing Z always homes XY and Z. - * - * SCARA should wait until all XY homing is done before setting the XY - * current_position to home, because neither X nor Y is at home until - * both are at home. Z can however be homed individually. - * - * Callers must sync the planner position after calling this! - */ -void set_axis_is_at_home(const AxisEnum axis) { - if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR(">>> set_axis_is_at_home(", AS_CHAR(axis_codes[axis]), ")"); + if (TERN0(HOMING_Z_WITH_PROBE, axis == Z_AXIS)) { + #if ALL(HAS_HEATED_BED, WAIT_FOR_BED_HEATER) + // Wait for bed to heat back up between probing points + thermalManager.wait_for_bed_heating(); + #endif - set_axis_trusted(axis); - set_axis_homed(axis); + TERN_(HAS_QUIET_PROBING, if (final_approach) probe.set_probing_paused(true)); + } - #if ENABLED(DUAL_X_CARRIAGE) - if (axis == X_AXIS && (active_extruder == 1 || dual_x_carriage_mode == DXC_DUPLICATION_MODE)) { - current_position.x = x_home_pos(active_extruder); - return; + // Disable stealthChop if used. Enable diag1 pin on driver. + TERN_(SENSORLESS_HOMING, stealth_states = start_sensorless_homing_per_axis(axis)); } - #endif - #if ENABLED(MORGAN_SCARA) - scara_set_axis_is_at_home(axis); - #elif ENABLED(DELTA) - current_position[axis] = (axis == Z_AXIS) ? delta_height - TERN0(HAS_BED_PROBE, probe.offset.z) : base_home_pos(axis); - #else - current_position[axis] = base_home_pos(axis); - #endif + #if IS_SCARA + // Tell the planner the axis is at 0 + current_position[axis] = 0; + sync_plan_position(); + current_position[axis] = distance; + line_to_current_position(home_fr_mm_s); + #else + // Get the ABC or XYZ positions in mm + abce_pos_t target = planner.get_axis_positions_mm(); - /** - * Z Probe Z Homing? Account for the probe's Z offset. - */ - #if HAS_BED_PROBE && Z_HOME_DIR < 0 - if (axis == Z_AXIS) { - #if HOMING_Z_WITH_PROBE + target[axis] = 0; // Set the single homing axis to 0 + planner.set_machine_position_mm(target); // Update the machine position - current_position.z -= probe.offset.z; + #if HAS_DIST_MM_ARG + const xyze_float_t cart_dist_mm{0}; + #endif - if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("*** Z HOMED WITH PROBE (Z_MIN_PROBE_USES_Z_MIN_ENDSTOP_PIN) ***\n> probe.offset.z = ", probe.offset.z); + // Set delta/cartesian axes directly + target[axis] = distance; // The move will be towards the endstop + planner.buffer_segment(target + #if HAS_DIST_MM_ARG + , cart_dist_mm + #endif + , home_fr_mm_s, active_extruder + ); + #endif - #else + planner.synchronize(); - if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("*** Z HOMED TO ENDSTOP ***"); + if (is_home_dir) { + #if HOMING_Z_WITH_PROBE && HAS_QUIET_PROBING + if (axis == Z_AXIS && final_approach) probe.set_probing_paused(false); #endif - } - #endif - - TERN_(I2C_POSITION_ENCODERS, I2CPEM.homed(axis)); - - TERN_(BABYSTEP_DISPLAY_TOTAL, babystep.reset_total(axis)); - #if HAS_POSITION_SHIFT - position_shift[axis] = 0; - update_workspace_offset(axis); - #endif + endstops.validate_homing_move(); - if (DEBUGGING(LEVELING)) { - #if HAS_HOME_OFFSET - DEBUG_ECHOLNPAIR("> home_offset[", AS_CHAR(axis_codes[axis]), "] = ", home_offset[axis]); - #endif - DEBUG_POS("", current_position); - DEBUG_ECHOLNPAIR("<<< set_axis_is_at_home(", axis_codes[axis], ")"); + // Re-enable stealthChop if used. Disable diag1 pin on driver. + TERN_(SENSORLESS_HOMING, end_sensorless_homing_per_axis(axis, stealth_states)); + } } -} -/** - * Set an axis to be unhomed. - */ -void set_axis_never_homed(const AxisEnum axis) { - if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR(">>> set_axis_never_homed(", axis_codes[axis], ")"); - - set_axis_untrusted(axis); - set_axis_unhomed(axis); - - if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("<<< set_axis_never_homed(", axis_codes[axis], ")"); - - TERN_(I2C_POSITION_ENCODERS, I2CPEM.unhomed(axis)); -} - -#ifdef TMC_HOME_PHASE /** - * Move the axis back to its home_phase if set and driver is capable (TMC) - * - * Improves homing repeatability by homing to stepper coil's nearest absolute - * phase position. Trinamic drivers use a stepper phase table with 1024 values - * spanning 4 full steps with 256 positions each (ergo, 1024 positions). + * Set an axis to be unhomed. (Unless we are on a machine - e.g. a cheap Chinese CNC machine - + * that has no endstops. Such machines should always be considered to be in a "known" and + * "trusted" position). */ - void backout_to_tmc_homing_phase(const AxisEnum axis) { - const xyz_long_t home_phase = TMC_HOME_PHASE; + void set_axis_never_homed(const AxisEnum axis) { + if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR(">>> set_axis_never_homed(", axis_codes[axis], ")"); - // check if home phase is disabled for this axis. - if (home_phase[axis] < 0) return; + set_axis_untrusted(axis); + set_axis_unhomed(axis); - int16_t phasePerUStep, // TMC µsteps(phase) per Marlin µsteps - phaseCurrent, // The TMC µsteps(phase) count of the current position - effectorBackoutDir, // Direction in which the effector mm coordinates move away from endstop. - stepperBackoutDir; // Direction in which the TMC µstep count(phase) move away from endstop. + if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("<<< set_axis_never_homed(", axis_codes[axis], ")"); - #define PHASE_PER_MICROSTEP(N) (256 / _MAX(1, N##_MICROSTEPS)) + TERN_(I2C_POSITION_ENCODERS, I2CPEM.unhomed(axis)); + } - switch (axis) { - #ifdef X_MICROSTEPS - case X_AXIS: - phasePerUStep = PHASE_PER_MICROSTEP(X); - phaseCurrent = stepperX.get_microstep_counter(); - effectorBackoutDir = -X_HOME_DIR; - stepperBackoutDir = INVERT_X_DIR ? effectorBackoutDir : -effectorBackoutDir; - break; - #endif - #ifdef Y_MICROSTEPS - case Y_AXIS: - phasePerUStep = PHASE_PER_MICROSTEP(Y); - phaseCurrent = stepperY.get_microstep_counter(); - effectorBackoutDir = -Y_HOME_DIR; - stepperBackoutDir = INVERT_Y_DIR ? effectorBackoutDir : -effectorBackoutDir; - break; - #endif - #ifdef Z_MICROSTEPS - case Z_AXIS: - phasePerUStep = PHASE_PER_MICROSTEP(Z); - phaseCurrent = stepperZ.get_microstep_counter(); - effectorBackoutDir = -Z_HOME_DIR; - stepperBackoutDir = INVERT_Z_DIR ? effectorBackoutDir : -effectorBackoutDir; - break; - #endif - default: return; - } + #if ENABLED(TMC_HOME_PHASE) + /** + * Move the axis back to its home_phase if set and driver is capable (TMC) + * + * Improves homing repeatability by homing to stepper coil's nearest absolute + * phase position. Trinamic drivers use a stepper phase table with 1024 values + * spanning 4 full steps with 256 positions each (ergo, 1024 positions). + */ + void backout_to_tmc_homing_phase(const AxisEnum axis) { + const xyz_long_t home_phase = TMC_HOME_PHASE; - // Phase distance to nearest home phase position when moving in the backout direction from endstop(may be negative). - int16_t phaseDelta = (home_phase[axis] - phaseCurrent) * stepperBackoutDir; + // check if home phase is disabled for this axis. + if (home_phase[axis] < 0) return; - // Check if home distance within endstop assumed repeatability noise of .05mm and warn. - if (ABS(phaseDelta) * planner.steps_to_mm[axis] / phasePerUStep < 0.05f) - SERIAL_ECHOLNPAIR("Selected home phase ", home_phase[axis], - " too close to endstop trigger phase ", phaseCurrent, - ". Pick a different phase for ", axis_codes[axis]); + int16_t phasePerUStep, // TMC µsteps(phase) per Marlin µsteps + phaseCurrent, // The TMC µsteps(phase) count of the current position + effectorBackoutDir, // Direction in which the effector mm coordinates move away from endstop. + stepperBackoutDir; // Direction in which the TMC µstep count(phase) move away from endstop. - // Skip to next if target position is behind current. So it only moves away from endstop. - if (phaseDelta < 0) phaseDelta += 1024; + #define PHASE_PER_MICROSTEP(N) (256 / _MAX(1, N##_MICROSTEPS)) - // Convert TMC µsteps(phase) to whole Marlin µsteps to effector backout direction to mm - const float mmDelta = int16_t(phaseDelta / phasePerUStep) * effectorBackoutDir * planner.steps_to_mm[axis]; + switch (axis) { + #ifdef X_MICROSTEPS + case X_AXIS: + phasePerUStep = PHASE_PER_MICROSTEP(X); + phaseCurrent = stepperX.get_microstep_counter(); + effectorBackoutDir = -X_HOME_DIR; + stepperBackoutDir = INVERT_X_DIR ? effectorBackoutDir : -effectorBackoutDir; + break; + #endif + #ifdef Y_MICROSTEPS + case Y_AXIS: + phasePerUStep = PHASE_PER_MICROSTEP(Y); + phaseCurrent = stepperY.get_microstep_counter(); + effectorBackoutDir = -Y_HOME_DIR; + stepperBackoutDir = INVERT_Y_DIR ? effectorBackoutDir : -effectorBackoutDir; + break; + #endif + #ifdef Z_MICROSTEPS + case Z_AXIS: + phasePerUStep = PHASE_PER_MICROSTEP(Z); + phaseCurrent = stepperZ.get_microstep_counter(); + effectorBackoutDir = -Z_HOME_DIR; + stepperBackoutDir = INVERT_Z_DIR ? effectorBackoutDir : -effectorBackoutDir; + break; + #endif + default: return; + } - // Optional debug messages - if (DEBUGGING(LEVELING)) { - DEBUG_ECHOLNPAIR( - "Endstop ", axis_codes[axis], " hit at Phase:", phaseCurrent, - " Delta:", phaseDelta, " Distance:", mmDelta - ); - } + // Phase distance to nearest home phase position when moving in the backout direction from endstop(may be negative). + int16_t phaseDelta = (home_phase[axis] - phaseCurrent) * stepperBackoutDir; - if (mmDelta != 0) { - // Retrace by the amount computed in mmDelta. - do_homing_move(axis, mmDelta, get_homing_bump_feedrate(axis)); - } - } -#endif + // Check if home distance within endstop assumed repeatability noise of .05mm and warn. + if (ABS(phaseDelta) * planner.steps_to_mm[axis] / phasePerUStep < 0.05f) + SERIAL_ECHOLNPAIR("Selected home phase ", home_phase[axis], + " too close to endstop trigger phase ", phaseCurrent, + ". Pick a different phase for ", axis_codes[axis]); -/** - * Home an individual "raw axis" to its endstop. - * This applies to XYZ on Cartesian and Core robots, and - * to the individual ABC steppers on DELTA and SCARA. - * - * At the end of the procedure the axis is marked as - * homed and the current position of that axis is updated. - * Kinematic robots should wait till all axes are homed - * before updating the current position. - */ + // Skip to next if target position is behind current. So it only moves away from endstop. + if (phaseDelta < 0) phaseDelta += 1024; -void homeaxis(const AxisEnum axis) { + // Convert TMC µsteps(phase) to whole Marlin µsteps to effector backout direction to mm + const float mmDelta = int16_t(phaseDelta / phasePerUStep) * effectorBackoutDir * planner.steps_to_mm[axis]; - #if IS_SCARA - // Only Z homing (with probe) is permitted - if (axis != Z_AXIS) { BUZZ(100, 880); return; } - #else - #define _CAN_HOME(A) (axis == _AXIS(A) && ( \ - ENABLED(A##_SPI_SENSORLESS) \ - || (_AXIS(A) == Z_AXIS && ENABLED(HOMING_Z_WITH_PROBE)) \ - || (A##_MIN_PIN > -1 && A##_HOME_DIR < 0) \ - || (A##_MAX_PIN > -1 && A##_HOME_DIR > 0) \ - )) - if (!_CAN_HOME(X) && !_CAN_HOME(Y) && !_CAN_HOME(Z)) return; - #endif + // Optional debug messages + if (DEBUGGING(LEVELING)) { + DEBUG_ECHOLNPAIR( + "Endstop ", axis_codes[axis], " hit at Phase:", phaseCurrent, + " Delta:", phaseDelta, " Distance:", mmDelta + ); + } - if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR(">>> homeaxis(", axis_codes[axis], ")"); - - const int axis_home_dir = TERN0(DUAL_X_CARRIAGE, axis == X_AXIS) - ? x_home_dir(active_extruder) : home_dir(axis); - - // - // Homing Z with a probe? Raise Z (maybe) and deploy the Z probe. - // - if (TERN0(HOMING_Z_WITH_PROBE, axis == Z_AXIS && probe.deploy())) - return; - - // Set flags for X, Y, Z motor locking - #if HAS_EXTRA_ENDSTOPS - switch (axis) { - TERN_(X_DUAL_ENDSTOPS, case X_AXIS:) - TERN_(Y_DUAL_ENDSTOPS, case Y_AXIS:) - TERN_(Z_MULTI_ENDSTOPS, case Z_AXIS:) - stepper.set_separate_multi_axis(true); - default: break; + if (mmDelta != 0) { + // Retrace by the amount computed in mmDelta. + do_homing_move(axis, mmDelta, get_homing_bump_feedrate(axis)); + } } #endif - // - // Deploy BLTouch or tare the probe just before probing - // - #if HOMING_Z_WITH_PROBE - if (axis == Z_AXIS) { - if (TERN0(BLTOUCH, bltouch.deploy())) return; // BLTouch was deployed above, but get the alarm state. - if (TERN0(PROBE_TARE, probe.tare())) return; - } - #endif + /** + * Home an individual "raw axis" to its endstop. + * This applies to XYZ on Cartesian and Core robots, and + * to the individual ABC steppers on DELTA and SCARA. + * + * At the end of the procedure the axis is marked as + * homed and the current position of that axis is updated. + * Kinematic robots should wait till all axes are homed + * before updating the current position. + */ - // - // Back away to prevent an early X/Y sensorless trigger - // - #if DISABLED(DELTA) && defined(SENSORLESS_BACKOFF_MM) - const xy_float_t backoff = SENSORLESS_BACKOFF_MM; - if ((TERN0(X_SENSORLESS, axis == X_AXIS) || TERN0(Y_SENSORLESS, axis == Y_AXIS)) && backoff[axis]) { - const float backoff_length = -ABS(backoff[axis]) * axis_home_dir; - if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("Sensorless backoff: ", backoff_length, "mm"); - do_homing_move(axis, backoff_length, homing_feedrate(axis)); - } - #endif + void homeaxis(const AxisEnum axis) { - // 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(Z_AXIS)); - const float bump = axis_home_dir * ( - use_probe_bump ? _MAX(TERN0(HOMING_Z_WITH_PROBE, Z_CLEARANCE_BETWEEN_PROBES), home_bump_mm(Z_AXIS)) : home_bump_mm(axis) - ); - - // - // Fast move towards endstop until triggered - // - const float move_length = 1.5f * max_length(TERN(DELTA, Z_AXIS, axis)) * axis_home_dir; - if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("Home Fast: ", move_length, "mm"); - do_homing_move(axis, move_length, 0.0, !use_probe_bump); - - #if BOTH(HOMING_Z_WITH_PROBE, BLTOUCH_SLOW_MODE) - if (axis == Z_AXIS) bltouch.stow(); // Intermediate STOW (in LOW SPEED MODE) - #endif + #if IS_SCARA + // Only Z homing (with probe) is permitted + if (axis != Z_AXIS) { BUZZ(100, 880); return; } + #else + #define _CAN_HOME(A) (axis == _AXIS(A) && ( \ + ENABLED(A##_SPI_SENSORLESS) \ + || (_AXIS(A) == Z_AXIS && ENABLED(HOMING_Z_WITH_PROBE)) \ + || (A##_MIN_PIN > -1 && A##_HOME_DIR < 0) \ + || (A##_MAX_PIN > -1 && A##_HOME_DIR > 0) \ + )) + if (!_CAN_HOME(X) && !_CAN_HOME(Y) && !_CAN_HOME(Z)) return; + #endif - // If a second homing move is configured... - if (bump) { - // Move away from the endstop by the axis HOMING_BUMP_MM - if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("Move Away: ", -bump, "mm"); - do_homing_move(axis, -bump, TERN0(HOMING_Z_WITH_PROBE, axis == Z_AXIS) ? MMM_TO_MMS(Z_PROBE_SPEED_FAST) : 0, false); + if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR(">>> homeaxis(", axis_codes[axis], ")"); - #if ENABLED(DETECT_BROKEN_ENDSTOP) - // Check for a broken endstop - EndstopEnum es; + const int axis_home_dir = TERN0(DUAL_X_CARRIAGE, axis == X_AXIS) + ? x_home_dir(active_extruder) : home_dir(axis); + + // + // Homing Z with a probe? Raise Z (maybe) and deploy the Z probe. + // + if (TERN0(HOMING_Z_WITH_PROBE, axis == Z_AXIS && probe.deploy())) + return; + + // Set flags for X, Y, Z motor locking + #if HAS_EXTRA_ENDSTOPS switch (axis) { - default: - case X_AXIS: es = X_ENDSTOP; break; - case Y_AXIS: es = Y_ENDSTOP; break; - case Z_AXIS: es = Z_ENDSTOP; break; + TERN_(X_DUAL_ENDSTOPS, case X_AXIS:) + TERN_(Y_DUAL_ENDSTOPS, case Y_AXIS:) + TERN_(Z_MULTI_ENDSTOPS, case Z_AXIS:) + stepper.set_separate_multi_axis(true); + default: break; } - if (TEST(endstops.state(), es)) { - SERIAL_ECHO_MSG("Bad ", axis_codes[axis], " Endstop?"); - kill(GET_TEXT(MSG_KILL_HOMING_FAILED)); + #endif + + // + // Deploy BLTouch or tare the probe just before probing + // + #if HOMING_Z_WITH_PROBE + if (axis == Z_AXIS) { + if (TERN0(BLTOUCH, bltouch.deploy())) return; // BLTouch was deployed above, but get the alarm state. + if (TERN0(PROBE_TARE, probe.tare())) return; } #endif - #if BOTH(HOMING_Z_WITH_PROBE, BLTOUCH_SLOW_MODE) - if (axis == Z_AXIS && bltouch.deploy()) return; // Intermediate DEPLOY (in LOW SPEED MODE) + // + // Back away to prevent an early X/Y sensorless trigger + // + #if DISABLED(DELTA) && defined(SENSORLESS_BACKOFF_MM) + const xy_float_t backoff = SENSORLESS_BACKOFF_MM; + if ((TERN0(X_SENSORLESS, axis == X_AXIS) || TERN0(Y_SENSORLESS, axis == Y_AXIS)) && backoff[axis]) { + const float backoff_length = -ABS(backoff[axis]) * axis_home_dir; + if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("Sensorless backoff: ", backoff_length, "mm"); + do_homing_move(axis, backoff_length, homing_feedrate(axis)); + } #endif - // Slow move towards endstop until triggered - const float rebump = bump * 2; - if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("Re-bump: ", rebump, "mm"); - do_homing_move(axis, rebump, get_homing_bump_feedrate(axis), true); + // 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(Z_AXIS)); + const float bump = axis_home_dir * ( + use_probe_bump ? _MAX(TERN0(HOMING_Z_WITH_PROBE, Z_CLEARANCE_BETWEEN_PROBES), home_bump_mm(Z_AXIS)) : home_bump_mm(axis) + ); + + // + // Fast move towards endstop until triggered + // + const float move_length = 1.5f * max_length(TERN(DELTA, Z_AXIS, axis)) * axis_home_dir; + if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("Home Fast: ", move_length, "mm"); + do_homing_move(axis, move_length, 0.0, !use_probe_bump); - #if BOTH(HOMING_Z_WITH_PROBE, BLTOUCH) - if (axis == Z_AXIS) bltouch.stow(); // The final STOW + #if BOTH(HOMING_Z_WITH_PROBE, BLTOUCH_SLOW_MODE) + if (axis == Z_AXIS) bltouch.stow(); // Intermediate STOW (in LOW SPEED MODE) #endif - } - #if HAS_EXTRA_ENDSTOPS - const bool pos_dir = axis_home_dir > 0; - #if ENABLED(X_DUAL_ENDSTOPS) - if (axis == X_AXIS) { - const float adj = ABS(endstops.x2_endstop_adj); - if (adj) { - if (pos_dir ? (endstops.x2_endstop_adj > 0) : (endstops.x2_endstop_adj < 0)) stepper.set_x_lock(true); else stepper.set_x2_lock(true); - do_homing_move(axis, pos_dir ? -adj : adj); - stepper.set_x_lock(false); - stepper.set_x2_lock(false); + // If a second homing move is configured... + if (bump) { + // Move away from the endstop by the axis HOMING_BUMP_MM + if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("Move Away: ", -bump, "mm"); + do_homing_move(axis, -bump, TERN0(HOMING_Z_WITH_PROBE, axis == Z_AXIS) ? MMM_TO_MMS(Z_PROBE_SPEED_FAST) : 0, false); + + #if ENABLED(DETECT_BROKEN_ENDSTOP) + // Check for a broken endstop + EndstopEnum es; + switch (axis) { + default: + case X_AXIS: es = X_ENDSTOP; break; + case Y_AXIS: es = Y_ENDSTOP; break; + case Z_AXIS: es = Z_ENDSTOP; break; } - } - #endif - #if ENABLED(Y_DUAL_ENDSTOPS) - if (axis == Y_AXIS) { - const float adj = ABS(endstops.y2_endstop_adj); - if (adj) { - if (pos_dir ? (endstops.y2_endstop_adj > 0) : (endstops.y2_endstop_adj < 0)) stepper.set_y_lock(true); else stepper.set_y2_lock(true); - do_homing_move(axis, pos_dir ? -adj : adj); - stepper.set_y_lock(false); - stepper.set_y2_lock(false); + if (TEST(endstops.state(), es)) { + SERIAL_ECHO_MSG("Bad ", axis_codes[axis], " Endstop?"); + kill(GET_TEXT(MSG_KILL_HOMING_FAILED)); } - } - #endif + #endif - #if ENABLED(Z_MULTI_ENDSTOPS) - if (axis == Z_AXIS) { + #if BOTH(HOMING_Z_WITH_PROBE, BLTOUCH_SLOW_MODE) + if (axis == Z_AXIS && bltouch.deploy()) return; // Intermediate DEPLOY (in LOW SPEED MODE) + #endif + + // Slow move towards endstop until triggered + const float rebump = bump * 2; + if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("Re-bump: ", rebump, "mm"); + do_homing_move(axis, rebump, get_homing_bump_feedrate(axis), true); - #if NUM_Z_STEPPER_DRIVERS == 2 + #if BOTH(HOMING_Z_WITH_PROBE, BLTOUCH) + if (axis == Z_AXIS) bltouch.stow(); // The final STOW + #endif + } - const float adj = ABS(endstops.z2_endstop_adj); + #if HAS_EXTRA_ENDSTOPS + const bool pos_dir = axis_home_dir > 0; + #if ENABLED(X_DUAL_ENDSTOPS) + if (axis == X_AXIS) { + const float adj = ABS(endstops.x2_endstop_adj); if (adj) { - if (pos_dir ? (endstops.z2_endstop_adj > 0) : (endstops.z2_endstop_adj < 0)) stepper.set_z1_lock(true); else stepper.set_z2_lock(true); + if (pos_dir ? (endstops.x2_endstop_adj > 0) : (endstops.x2_endstop_adj < 0)) stepper.set_x_lock(true); else stepper.set_x2_lock(true); do_homing_move(axis, pos_dir ? -adj : adj); - stepper.set_z1_lock(false); - stepper.set_z2_lock(false); + stepper.set_x_lock(false); + stepper.set_x2_lock(false); } + } + #endif + #if ENABLED(Y_DUAL_ENDSTOPS) + if (axis == Y_AXIS) { + const float adj = ABS(endstops.y2_endstop_adj); + if (adj) { + if (pos_dir ? (endstops.y2_endstop_adj > 0) : (endstops.y2_endstop_adj < 0)) stepper.set_y_lock(true); else stepper.set_y2_lock(true); + do_homing_move(axis, pos_dir ? -adj : adj); + stepper.set_y_lock(false); + stepper.set_y2_lock(false); + } + } + #endif - #else - - // Handy arrays of stepper lock function pointers + #if ENABLED(Z_MULTI_ENDSTOPS) + if (axis == Z_AXIS) { - typedef void (*adjustFunc_t)(const bool); + #if NUM_Z_STEPPER_DRIVERS == 2 - adjustFunc_t lock[] = { - stepper.set_z1_lock, stepper.set_z2_lock, stepper.set_z3_lock - #if NUM_Z_STEPPER_DRIVERS >= 4 - , stepper.set_z4_lock - #endif - }; - float adj[] = { - 0, endstops.z2_endstop_adj, endstops.z3_endstop_adj - #if NUM_Z_STEPPER_DRIVERS >= 4 - , endstops.z4_endstop_adj - #endif - }; + const float adj = ABS(endstops.z2_endstop_adj); + if (adj) { + if (pos_dir ? (endstops.z2_endstop_adj > 0) : (endstops.z2_endstop_adj < 0)) stepper.set_z1_lock(true); else stepper.set_z2_lock(true); + do_homing_move(axis, pos_dir ? -adj : adj); + stepper.set_z1_lock(false); + stepper.set_z2_lock(false); + } - adjustFunc_t tempLock; - float tempAdj; + #else - // Manual bubble sort by adjust value - if (adj[1] < adj[0]) { - tempLock = lock[0], tempAdj = adj[0]; - lock[0] = lock[1], adj[0] = adj[1]; - lock[1] = tempLock, adj[1] = tempAdj; - } - if (adj[2] < adj[1]) { - tempLock = lock[1], tempAdj = adj[1]; - lock[1] = lock[2], adj[1] = adj[2]; - lock[2] = tempLock, adj[2] = tempAdj; - } - #if NUM_Z_STEPPER_DRIVERS >= 4 - if (adj[3] < adj[2]) { - tempLock = lock[2], tempAdj = adj[2]; - lock[2] = lock[3], adj[2] = adj[3]; - lock[3] = tempLock, adj[3] = tempAdj; + // Handy arrays of stepper lock function pointers + + typedef void (*adjustFunc_t)(const bool); + + adjustFunc_t lock[] = { + stepper.set_z1_lock, stepper.set_z2_lock, stepper.set_z3_lock + #if NUM_Z_STEPPER_DRIVERS >= 4 + , stepper.set_z4_lock + #endif + }; + float adj[] = { + 0, endstops.z2_endstop_adj, endstops.z3_endstop_adj + #if NUM_Z_STEPPER_DRIVERS >= 4 + , endstops.z4_endstop_adj + #endif + }; + + adjustFunc_t tempLock; + float tempAdj; + + // Manual bubble sort by adjust value + if (adj[1] < adj[0]) { + tempLock = lock[0], tempAdj = adj[0]; + lock[0] = lock[1], adj[0] = adj[1]; + lock[1] = tempLock, adj[1] = tempAdj; } if (adj[2] < adj[1]) { tempLock = lock[1], tempAdj = adj[1]; lock[1] = lock[2], adj[1] = adj[2]; lock[2] = tempLock, adj[2] = tempAdj; } - #endif - if (adj[1] < adj[0]) { - tempLock = lock[0], tempAdj = adj[0]; - lock[0] = lock[1], adj[0] = adj[1]; - lock[1] = tempLock, adj[1] = tempAdj; - } - - if (pos_dir) { - // normalize adj to smallest value and do the first move - (*lock[0])(true); - do_homing_move(axis, adj[1] - adj[0]); - // lock the second stepper for the final correction - (*lock[1])(true); - do_homing_move(axis, adj[2] - adj[1]); #if NUM_Z_STEPPER_DRIVERS >= 4 - // lock the third stepper for the final correction - (*lock[2])(true); - do_homing_move(axis, adj[3] - adj[2]); + if (adj[3] < adj[2]) { + tempLock = lock[2], tempAdj = adj[2]; + lock[2] = lock[3], adj[2] = adj[3]; + lock[3] = tempLock, adj[3] = tempAdj; + } + if (adj[2] < adj[1]) { + tempLock = lock[1], tempAdj = adj[1]; + lock[1] = lock[2], adj[1] = adj[2]; + lock[2] = tempLock, adj[2] = tempAdj; + } #endif - } - else { + if (adj[1] < adj[0]) { + tempLock = lock[0], tempAdj = adj[0]; + lock[0] = lock[1], adj[0] = adj[1]; + lock[1] = tempLock, adj[1] = tempAdj; + } + + if (pos_dir) { + // normalize adj to smallest value and do the first move + (*lock[0])(true); + do_homing_move(axis, adj[1] - adj[0]); + // lock the second stepper for the final correction + (*lock[1])(true); + do_homing_move(axis, adj[2] - adj[1]); + #if NUM_Z_STEPPER_DRIVERS >= 4 + // lock the third stepper for the final correction + (*lock[2])(true); + do_homing_move(axis, adj[3] - adj[2]); + #endif + } + else { + #if NUM_Z_STEPPER_DRIVERS >= 4 + (*lock[3])(true); + do_homing_move(axis, adj[2] - adj[3]); + #endif + (*lock[2])(true); + do_homing_move(axis, adj[1] - adj[2]); + (*lock[1])(true); + do_homing_move(axis, adj[0] - adj[1]); + } + + stepper.set_z1_lock(false); + stepper.set_z2_lock(false); + stepper.set_z3_lock(false); #if NUM_Z_STEPPER_DRIVERS >= 4 - (*lock[3])(true); - do_homing_move(axis, adj[2] - adj[3]); + stepper.set_z4_lock(false); #endif - (*lock[2])(true); - do_homing_move(axis, adj[1] - adj[2]); - (*lock[1])(true); - do_homing_move(axis, adj[0] - adj[1]); - } - stepper.set_z1_lock(false); - stepper.set_z2_lock(false); - stepper.set_z3_lock(false); - #if NUM_Z_STEPPER_DRIVERS >= 4 - stepper.set_z4_lock(false); #endif + } + #endif - #endif + // Reset flags for X, Y, Z motor locking + switch (axis) { + default: break; + TERN_(X_DUAL_ENDSTOPS, case X_AXIS:) + TERN_(Y_DUAL_ENDSTOPS, case Y_AXIS:) + TERN_(Z_MULTI_ENDSTOPS, case Z_AXIS:) + stepper.set_separate_multi_axis(false); } #endif - // Reset flags for X, Y, Z motor locking - switch (axis) { - default: break; - TERN_(X_DUAL_ENDSTOPS, case X_AXIS:) - TERN_(Y_DUAL_ENDSTOPS, case Y_AXIS:) - TERN_(Z_MULTI_ENDSTOPS, case Z_AXIS:) - stepper.set_separate_multi_axis(false); - } - #endif + #ifdef TMC_HOME_PHASE + // move back to homing phase if configured and capable + backout_to_tmc_homing_phase(axis); + #endif - #ifdef TMC_HOME_PHASE - // move back to homing phase if configured and capable - backout_to_tmc_homing_phase(axis); - #endif + #if IS_SCARA - #if IS_SCARA + set_axis_is_at_home(axis); + sync_plan_position(); - set_axis_is_at_home(axis); - sync_plan_position(); + #elif ENABLED(DELTA) - #elif ENABLED(DELTA) + // Delta has already moved all three towers up in G28 + // so here it re-homes each tower in turn. + // Delta homing treats the axes as normal linear axes. - // Delta has already moved all three towers up in G28 - // so here it re-homes each tower in turn. - // Delta homing treats the axes as normal linear axes. + const float adjDistance = delta_endstop_adj[axis], + minDistance = (MIN_STEPS_PER_SEGMENT) * planner.steps_to_mm[axis]; - const float adjDistance = delta_endstop_adj[axis], - minDistance = (MIN_STEPS_PER_SEGMENT) * planner.steps_to_mm[axis]; + // Retrace by the amount specified in delta_endstop_adj if more than min steps. + if (adjDistance * (Z_HOME_DIR) < 0 && ABS(adjDistance) > minDistance) { // away from endstop, more than min distance + if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("adjDistance:", adjDistance); + do_homing_move(axis, adjDistance, get_homing_bump_feedrate(axis)); + } - // Retrace by the amount specified in delta_endstop_adj if more than min steps. - if (adjDistance * (Z_HOME_DIR) < 0 && ABS(adjDistance) > minDistance) { // away from endstop, more than min distance - if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("adjDistance:", adjDistance); - do_homing_move(axis, adjDistance, get_homing_bump_feedrate(axis)); - } + #else // CARTESIAN / CORE / MARKFORGED_XY + + set_axis_is_at_home(axis); + sync_plan_position(); - #else // CARTESIAN / CORE / MARKFORGED_XY + destination[axis] = current_position[axis]; - set_axis_is_at_home(axis); - sync_plan_position(); + if (DEBUGGING(LEVELING)) DEBUG_POS("> AFTER set_axis_is_at_home", current_position); - destination[axis] = current_position[axis]; + #endif - if (DEBUGGING(LEVELING)) DEBUG_POS("> AFTER set_axis_is_at_home", current_position); + // Put away the Z probe + #if HOMING_Z_WITH_PROBE + if (axis == Z_AXIS && probe.stow()) return; + #endif + #if DISABLED(DELTA) && defined(HOMING_BACKOFF_POST_MM) + const xyz_float_t endstop_backoff = HOMING_BACKOFF_POST_MM; + if (endstop_backoff[axis]) { + current_position[axis] -= ABS(endstop_backoff[axis]) * axis_home_dir; + line_to_current_position( + #if HOMING_Z_WITH_PROBE + (axis == Z_AXIS) ? z_probe_fast_mm_s : + #endif + homing_feedrate(axis) + ); + + #if ENABLED(SENSORLESS_HOMING) + planner.synchronize(); + if (false + #if EITHER(IS_CORE, MARKFORGED_XY) + || axis != NORMAL_AXIS + #endif + ) safe_delay(200); // Short delay to allow belts to spring back + #endif + } + #endif + + // Clear retracted status if homing the Z axis + #if ENABLED(FWRETRACT) + if (axis == Z_AXIS) fwretract.current_hop = 0.0; + #endif + + if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("<<< homeaxis(", axis_codes[axis], ")"); + + } // homeaxis() + +#endif // HAS_ENDSTOPS + +/** + * Set an axis' current position to its home position (after homing). + * + * For Core and Cartesian robots this applies one-to-one when an + * individual axis has been homed. + * + * DELTA should wait until all homing is done before setting the XYZ + * current_position to home, because homing is a single operation. + * In the case where the axis positions are trusted and previously + * homed, DELTA could home to X or Y individually by moving either one + * to the center. However, homing Z always homes XY and Z. + * + * SCARA should wait until all XY homing is done before setting the XY + * current_position to home, because neither X nor Y is at home until + * both are at home. Z can however be homed individually. + * + * Callers must sync the planner position after calling this! + */ +void set_axis_is_at_home(const AxisEnum axis) { + if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR(">>> set_axis_is_at_home(", AS_CHAR(axis_codes[axis]), ")"); + + set_axis_trusted(axis); + set_axis_homed(axis); + + #if ENABLED(DUAL_X_CARRIAGE) + if (axis == X_AXIS && (active_extruder == 1 || dual_x_carriage_mode == DXC_DUPLICATION_MODE)) { + current_position.x = x_home_pos(active_extruder); + return; + } #endif - // Put away the Z probe - #if HOMING_Z_WITH_PROBE - if (axis == Z_AXIS && probe.stow()) return; + #if ENABLED(MORGAN_SCARA) + scara_set_axis_is_at_home(axis); + #elif ENABLED(DELTA) + current_position[axis] = (axis == Z_AXIS) ? delta_height - TERN0(HAS_BED_PROBE, probe.offset.z) : base_home_pos(axis); + #else + current_position[axis] = base_home_pos(axis); #endif - #if DISABLED(DELTA) && defined(HOMING_BACKOFF_POST_MM) - const xyz_float_t endstop_backoff = HOMING_BACKOFF_POST_MM; - if (endstop_backoff[axis]) { - current_position[axis] -= ABS(endstop_backoff[axis]) * axis_home_dir; - line_to_current_position( - #if HOMING_Z_WITH_PROBE - (axis == Z_AXIS) ? z_probe_fast_mm_s : - #endif - homing_feedrate(axis) - ); + /** + * Z Probe Z Homing? Account for the probe's Z offset. + */ + #if HAS_BED_PROBE && Z_HOME_DIR < 0 + if (axis == Z_AXIS) { + #if HOMING_Z_WITH_PROBE + + current_position.z -= probe.offset.z; + + if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("*** Z HOMED WITH PROBE (Z_MIN_PROBE_USES_Z_MIN_ENDSTOP_PIN) ***\n> probe.offset.z = ", probe.offset.z); + + #else + + if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("*** Z HOMED TO ENDSTOP ***"); - #if ENABLED(SENSORLESS_HOMING) - planner.synchronize(); - if (false - #if EITHER(IS_CORE, MARKFORGED_XY) - || axis != NORMAL_AXIS - #endif - ) safe_delay(200); // Short delay to allow belts to spring back #endif } #endif - // Clear retracted status if homing the Z axis - #if ENABLED(FWRETRACT) - if (axis == Z_AXIS) fwretract.current_hop = 0.0; - #endif + TERN_(I2C_POSITION_ENCODERS, I2CPEM.homed(axis)); + + TERN_(BABYSTEP_DISPLAY_TOTAL, babystep.reset_total(axis)); - if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("<<< homeaxis(", axis_codes[axis], ")"); + #if HAS_POSITION_SHIFT + position_shift[axis] = 0; + update_workspace_offset(axis); + #endif -} // homeaxis() + if (DEBUGGING(LEVELING)) { + #if HAS_HOME_OFFSET + DEBUG_ECHOLNPAIR("> home_offset[", AS_CHAR(axis_codes[axis]), "] = ", home_offset[axis]); + #endif + DEBUG_POS("", current_position); + DEBUG_ECHOLNPAIR("<<< set_axis_is_at_home(", axis_codes[axis], ")"); + } +} #if HAS_WORKSPACE_OFFSET void update_workspace_offset(const AxisEnum axis) { @@ -1893,4 +1890,4 @@ void homeaxis(const AxisEnum axis) { home_offset[axis] = v; update_workspace_offset(axis); } -#endif // HAS_M206_COMMAND +#endif diff --git a/Marlin/src/module/motion.h b/Marlin/src/module/motion.h index 328bfe018d..2a23636d71 100644 --- a/Marlin/src/module/motion.h +++ b/Marlin/src/module/motion.h @@ -284,27 +284,51 @@ void do_z_clearance(const float &zclear, const bool z_trusted=true, const bool r * Homing and Trusted Axes */ constexpr uint8_t xyz_bits = _BV(X_AXIS) | _BV(Y_AXIS) | _BV(Z_AXIS); -extern uint8_t axis_homed, axis_trusted; -void homeaxis(const AxisEnum axis); void set_axis_is_at_home(const AxisEnum axis); -void set_axis_never_homed(const AxisEnum axis); -uint8_t axes_should_home(uint8_t axis_bits=0x07); -bool homing_needed_error(uint8_t axis_bits=0x07); - -FORCE_INLINE bool axis_was_homed(const AxisEnum axis) { return TEST(axis_homed, axis); } -FORCE_INLINE bool axis_is_trusted(const AxisEnum axis) { return TEST(axis_trusted, axis); } -FORCE_INLINE bool axis_should_home(const AxisEnum axis) { return (axes_should_home() & _BV(axis)) != 0; } -FORCE_INLINE bool no_axes_homed() { return !axis_homed; } -FORCE_INLINE bool all_axes_homed() { return xyz_bits == (axis_homed & xyz_bits); } -FORCE_INLINE bool homing_needed() { return !all_axes_homed(); } -FORCE_INLINE bool all_axes_trusted() { return xyz_bits == (axis_trusted & xyz_bits); } -FORCE_INLINE void set_axis_homed(const AxisEnum axis) { SBI(axis_homed, axis); } -FORCE_INLINE void set_axis_unhomed(const AxisEnum axis) { CBI(axis_homed, axis); } -FORCE_INLINE void set_axis_trusted(const AxisEnum axis) { SBI(axis_trusted, axis); } -FORCE_INLINE void set_axis_untrusted(const AxisEnum axis) { CBI(axis_trusted, axis); } -FORCE_INLINE void set_all_homed() { axis_homed = axis_trusted = xyz_bits; } -FORCE_INLINE void set_all_unhomed() { axis_homed = axis_trusted = 0; } + +#if HAS_ENDSTOPS + /** + * axis_homed + * Flags that each linear axis was homed. + * XYZ on cartesian, ABC on delta, ABZ on SCARA. + * + * axis_trusted + * Flags that the position is trusted in each linear axis. Set when homed. + * Cleared whenever a stepper powers off, potentially losing its position. + */ + extern uint8_t axis_homed, axis_trusted; + void homeaxis(const AxisEnum axis); + void set_axis_never_homed(const AxisEnum axis); + uint8_t axes_should_home(uint8_t axis_bits=0x07); + bool homing_needed_error(uint8_t axis_bits=0x07); + FORCE_INLINE void set_axis_unhomed(const AxisEnum axis) { CBI(axis_homed, axis); } + FORCE_INLINE void set_axis_untrusted(const AxisEnum axis) { CBI(axis_trusted, axis); } + FORCE_INLINE void set_all_unhomed() { axis_homed = axis_trusted = 0; } + FORCE_INLINE void set_axis_homed(const AxisEnum axis) { SBI(axis_homed, axis); } + FORCE_INLINE void set_axis_trusted(const AxisEnum axis) { SBI(axis_trusted, axis); } + FORCE_INLINE void set_all_homed() { axis_homed = axis_trusted = xyz_bits; } +#else + constexpr uint8_t axis_homed = xyz_bits, axis_trusted = xyz_bits; // Zero-endstop machines are always homed and trusted + FORCE_INLINE void homeaxis(const AxisEnum axis) {} + FORCE_INLINE void set_axis_never_homed(const AxisEnum) {} + FORCE_INLINE uint8_t axes_should_home(uint8_t=0x07) { return false; } + FORCE_INLINE bool homing_needed_error(uint8_t=0x07) { return false; } + FORCE_INLINE void set_axis_unhomed(const AxisEnum axis) {} + FORCE_INLINE void set_axis_untrusted(const AxisEnum axis) {} + FORCE_INLINE void set_all_unhomed() {} + FORCE_INLINE void set_axis_homed(const AxisEnum axis) {} + FORCE_INLINE void set_axis_trusted(const AxisEnum axis) {} + FORCE_INLINE void set_all_homed() {} +#endif + +FORCE_INLINE bool axis_was_homed(const AxisEnum axis) { return TEST(axis_homed, axis); } +FORCE_INLINE bool axis_is_trusted(const AxisEnum axis) { return TEST(axis_trusted, axis); } +FORCE_INLINE bool axis_should_home(const AxisEnum axis) { return (axes_should_home() & _BV(axis)) != 0; } +FORCE_INLINE bool no_axes_homed() { return !axis_homed; } +FORCE_INLINE bool all_axes_homed() { return xyz_bits == (axis_homed & xyz_bits); } +FORCE_INLINE bool homing_needed() { return !all_axes_homed(); } +FORCE_INLINE bool all_axes_trusted() { return xyz_bits == (axis_trusted & xyz_bits); } #if ENABLED(NO_MOTION_BEFORE_HOMING) #define MOTION_CONDITIONS (IsRunning() && !homing_needed_error()) @@ -360,7 +384,6 @@ FORCE_INLINE void set_all_unhomed() { axis_homed = axis_tr /** * position_is_reachable family of functions */ - #if IS_KINEMATIC // (DELTA or SCARA) #if HAS_SCARA_OFFSET @@ -390,14 +413,14 @@ FORCE_INLINE void set_all_unhomed() { axis_homed = axis_tr // Return true if the given position is within the machine bounds. inline bool position_is_reachable(const float &rx, const float &ry) { - if (!WITHIN(ry, Y_MIN_POS - fslop, Y_MAX_POS + fslop)) return false; + if (!COORDINATE_OKAY(ry, Y_MIN_POS - fslop, Y_MAX_POS + fslop)) return false; #if ENABLED(DUAL_X_CARRIAGE) if (active_extruder) - return WITHIN(rx, X2_MIN_POS - fslop, X2_MAX_POS + fslop); + return COORDINATE_OKAY(rx, X2_MIN_POS - fslop, X2_MAX_POS + fslop); else - return WITHIN(rx, X1_MIN_POS - fslop, X1_MAX_POS + fslop); + return COORDINATE_OKAY(rx, X1_MIN_POS - fslop, X1_MAX_POS + fslop); #else - return WITHIN(rx, X_MIN_POS - fslop, X_MAX_POS + fslop); + return COORDINATE_OKAY(rx, X_MIN_POS - fslop, X_MAX_POS + fslop); #endif } inline bool position_is_reachable(const xy_pos_t &pos) { return position_is_reachable(pos.x, pos.y); } diff --git a/Marlin/src/module/planner.h b/Marlin/src/module/planner.h index cd906c5d13..b7ff0ee932 100644 --- a/Marlin/src/module/planner.h +++ b/Marlin/src/module/planner.h @@ -564,10 +564,10 @@ class Planner { #if ENABLED(SKEW_CORRECTION) FORCE_INLINE static void skew(float &cx, float &cy, const float &cz) { - if (WITHIN(cx, X_MIN_POS + 1, X_MAX_POS) && WITHIN(cy, Y_MIN_POS + 1, Y_MAX_POS)) { + if (COORDINATE_OKAY(cx, X_MIN_POS + 1, X_MAX_POS) && COORDINATE_OKAY(cy, Y_MIN_POS + 1, Y_MAX_POS)) { const float sx = cx - cy * skew_factor.xy - cz * (skew_factor.xz - (skew_factor.xy * skew_factor.yz)), sy = cy - cz * skew_factor.yz; - if (WITHIN(sx, X_MIN_POS, X_MAX_POS) && WITHIN(sy, Y_MIN_POS, Y_MAX_POS)) { + if (COORDINATE_OKAY(sx, X_MIN_POS, X_MAX_POS) && COORDINATE_OKAY(sy, Y_MIN_POS, Y_MAX_POS)) { cx = sx; cy = sy; } } @@ -575,10 +575,10 @@ class Planner { FORCE_INLINE static void skew(xyz_pos_t &raw) { skew(raw.x, raw.y, raw.z); } FORCE_INLINE static void unskew(float &cx, float &cy, const float &cz) { - if (WITHIN(cx, X_MIN_POS, X_MAX_POS) && WITHIN(cy, Y_MIN_POS, Y_MAX_POS)) { + if (COORDINATE_OKAY(cx, X_MIN_POS, X_MAX_POS) && COORDINATE_OKAY(cy, Y_MIN_POS, Y_MAX_POS)) { const float sx = cx + cy * skew_factor.xy + cz * skew_factor.xz, sy = cy + cz * skew_factor.yz; - if (WITHIN(sx, X_MIN_POS, X_MAX_POS) && WITHIN(sy, Y_MIN_POS, Y_MAX_POS)) { + if (COORDINATE_OKAY(sx, X_MIN_POS, X_MAX_POS) && COORDINATE_OKAY(sy, Y_MIN_POS, Y_MAX_POS)) { cx = sx; cy = sy; } } diff --git a/Marlin/src/module/probe.h b/Marlin/src/module/probe.h index c8b214370c..6b3d990859 100644 --- a/Marlin/src/module/probe.h +++ b/Marlin/src/module/probe.h @@ -92,8 +92,8 @@ public: */ static bool can_reach(const float &rx, const float &ry) { return position_is_reachable(rx - offset_xy.x, ry - offset_xy.y) - && WITHIN(rx, min_x() - fslop, max_x() + fslop) - && WITHIN(ry, min_y() - fslop, max_y() + fslop); + && COORDINATE_OKAY(rx, min_x() - fslop, max_x() + fslop) + && COORDINATE_OKAY(ry, min_y() - fslop, max_y() + fslop); } #endif @@ -206,8 +206,8 @@ public: #if IS_KINEMATIC return HYPOT2(x, y) <= sq(probe_radius(default_probe_xy_offset)); #else - return WITHIN(x, _min_x(default_probe_xy_offset) - fslop, _max_x(default_probe_xy_offset) + fslop) - && WITHIN(y, _min_y(default_probe_xy_offset) - fslop, _max_y(default_probe_xy_offset) + fslop); + return COORDINATE_OKAY(x, _min_x(default_probe_xy_offset) - fslop, _max_x(default_probe_xy_offset) + fslop) + && COORDINATE_OKAY(y, _min_y(default_probe_xy_offset) - fslop, _max_y(default_probe_xy_offset) + fslop); #endif } diff --git a/buildroot/tests/teensy31-tests b/buildroot/tests/teensy31-tests index 448a8860c7..cf01d27461 100755 --- a/buildroot/tests/teensy31-tests +++ b/buildroot/tests/teensy31-tests @@ -10,6 +10,14 @@ restore_configs opt_set MOTHERBOARD BOARD_TEENSY31_32 exec_test $1 $2 "Teensy3.1 with default config" "$3" +# +# Zero endstops, as with a CNC +# +restore_configs +opt_set MOTHERBOARD BOARD_TEENSY31_32 +opt_disable USE_XMIN_PLUG USE_YMIN_PLUG USE_ZMIN_PLUG +exec_test $1 $2 "Teensy3.1 with Zero Endstops" "$3" + # # Test many features together #