From 511bbd72e2ae89a6db0f4a599062b053111b9e27 Mon Sep 17 00:00:00 2001 From: Andrew Date: Sat, 20 Dec 2025 23:19:10 -0500 Subject: [PATCH] fix/rearrange `VALIDATE_MESH_TILT` --- Marlin/src/feature/bedlevel/ubl/ubl_G29.cpp | 79 ++++++++++----------- 1 file changed, 39 insertions(+), 40 deletions(-) diff --git a/Marlin/src/feature/bedlevel/ubl/ubl_G29.cpp b/Marlin/src/feature/bedlevel/ubl/ubl_G29.cpp index 1433606caf..12aaf80ebb 100644 --- a/Marlin/src/feature/bedlevel/ubl/ubl_G29.cpp +++ b/Marlin/src/feature/bedlevel/ubl/ubl_G29.cpp @@ -1495,8 +1495,17 @@ void unified_bed_leveling::smart_fill_mesh() { struct linear_fit_data lsf_results; incremental_LSF_reset(&lsf_results); + #if DISABLED(DISABLE_UBL_SERIAL_CHITCHAT) || ENABLED(VALIDATE_MESH_TILT) + vector_3 normal = vector_3(lsf_results.A, lsf_results.B, 1).get_normal(); + #endif + + #if ENABLED(VALIDATE_MESH_TILT) + float gotz[3]; // Used for algorithm validation below + #endif + + xy_float_t points[3]; + if (do_3_pt_leveling) { - xy_float_t points[3]; probe.get_three_points(points); #if ENABLED(UBL_TILT_ON_MESH_POINTS_3POINT) @@ -1507,10 +1516,6 @@ void unified_bed_leveling::smart_fill_mesh() { } #endif - #if ENABLED(VALIDATE_MESH_TILT) - float gotz[3]; // Used for algorithm validation below - #endif - for (uint8_t i = 0; i < 3; ++i) { IF_DISABLED(DISABLE_UBL_SERIAL_CHITCHAT, SERIAL_ECHOLNPGM("Tilting mesh (", i + 1, "/3)")); TERN_(HAS_STATUS_MESSAGE, ui.status_printf(0, F(S_FMT " %i/3"), GET_TEXT(MSG_LCD_TILTING_MESH), i + 1)); @@ -1531,6 +1536,34 @@ void unified_bed_leveling::smart_fill_mesh() { incremental_LSF(&lsf_results, points[i], measured_z); } + /** + * Use the code below to check the validity of the mesh tilting algorithm. + * 3-Point Mesh Tilt uses the same algorithm as grid-based tilting, but only + * three points are used in the calculation. This guarantees that each probed point + * has an exact match when get_z_correction() for that location is calculated. + * The Z error between the probed point locations and the get_z_correction() + * numbers for those locations should be 0. + */ + #if ENABLED(VALIDATE_MESH_TILT) + auto d_from = []{ DEBUG_ECHOPGM("D from "); }; + auto normed = [&](const xy_pos_t &pos, const float zadd) { + return normal.x * pos.x + normal.y * pos.y + zadd; + }; + auto debug_pt = [&](const int num, const xy_pos_t &pos, const float zadd) { + d_from(); + DEBUG_ECHOLN(F("Point "), num, C(':'), p_float_t(normed(pos, zadd), 6), F(" Z error = "), p_float_t(zadd - get_z_correction(pos), 6)); + }; + debug_pt(1, points[0], normal.z * gotz[0]); + debug_pt(2, points[1], normal.z * gotz[1]); + debug_pt(3, points[2], normal.z * gotz[2]); + #if ENABLED(Z_SAFE_HOMING) + constexpr xy_float_t safe_xy = { Z_SAFE_HOMING_X_POINT, Z_SAFE_HOMING_Y_POINT }; + d_from(); DEBUG_ECHOLN(F("safe home with Z="), F("0 : "), p_float_t(normed(safe_xy, 0), 6)); + d_from(); DEBUG_ECHOLN(F("safe home with Z="), F("mesh value "), p_float_t(normed(safe_xy, get_z_correction(safe_xy)), 6)); + DEBUG_ECHO(F(" Z error = ("), Z_SAFE_HOMING_X_POINT, C(','), Z_SAFE_HOMING_Y_POINT, F(") = "), p_float_t(get_z_correction(safe_xy), 6)); + #endif + #endif + probe.stow(); probe.move_z_after_probing(); @@ -1619,6 +1652,7 @@ void unified_bed_leveling::smart_fill_mesh() { FLIP(zig_zag); } } + probe.stow(); probe.move_z_after_probing(); @@ -1627,10 +1661,6 @@ void unified_bed_leveling::smart_fill_mesh() { return; } - #if DISABLED(DISABLE_UBL_SERIAL_CHITCHAT) || ENABLED(VALIDATE_MESH_TILT) - vector_3 normal = vector_3(lsf_results.A, lsf_results.B, 1).get_normal(); - #endif - if (param.V_verbosity > 2) IF_DISABLED(DISABLE_UBL_SERIAL_CHITCHAT, SERIAL_ECHOLN(F("bed plane normal = ["), p_float_t(normal.x, 7), C(','), p_float_t(normal.y, 7), C(','), p_float_t(normal.z, 7), C(']'))); @@ -1661,43 +1691,13 @@ void unified_bed_leveling::smart_fill_mesh() { DEBUG_DELAY(55); DEBUG_ECHOLN(F("bed plane normal = ["), p_float_t(normal.x, 7), C(','), p_float_t(normal.y, 7), C(','), p_float_t(normal.z, 7), C(']')); DEBUG_EOL(); - - /** - * Use the code below to check the validity of the mesh tilting algorithm. - * 3-Point Mesh Tilt uses the same algorithm as grid-based tilting, but only - * three points are used in the calculation. This guarantees that each probed point - * has an exact match when get_z_correction() for that location is calculated. - * The Z error between the probed point locations and the get_z_correction() - * numbers for those locations should be 0. - */ - #if ENABLED(VALIDATE_MESH_TILT) - auto d_from = []{ DEBUG_ECHOPGM("D from "); }; - auto normed = [&](const xy_pos_t &pos, const float zadd) { - return normal.x * pos.x + normal.y * pos.y + zadd; - }; - auto debug_pt = [](const int num, const xy_pos_t &pos, const float zadd) { - d_from(); - DEBUG_ECHOLN(F("Point "), num, C(':'), p_float_t(normed(pos, zadd), 6), F(" Z error = "), p_float_t(zadd - get_z_correction(pos), 6)); - }; - debug_pt(1, probe_pt[0], normal.z * gotz[0]); - debug_pt(2, probe_pt[1], normal.z * gotz[1]); - debug_pt(3, probe_pt[2], normal.z * gotz[2]); - #if ENABLED(Z_SAFE_HOMING) - constexpr xy_float_t safe_xy = { Z_SAFE_HOMING_X_POINT, Z_SAFE_HOMING_Y_POINT }; - d_from(); DEBUG_ECHOLN(F("safe home with Z="), F("0 : "), p_float_t(normed(safe_xy, 0), 6)); - d_from(); DEBUG_ECHOLN(F("safe home with Z="), F("mesh value "), p_float_t(normed(safe_xy, get_z_correction(safe_xy)), 6)); - DEBUG_ECHO(F(" Z error = ("), Z_SAFE_HOMING_X_POINT, C(','), Z_SAFE_HOMING_Y_POINT, F(") = "), p_float_t(get_z_correction(safe_xy), 6)); - #endif - #endif } // DEBUGGING(LEVELING) - } #endif // HAS_BED_PROBE #if ENABLED(UBL_G29_P31) void unified_bed_leveling::smart_fill_wlsf(const float weight_factor) { - // For each undefined mesh point, compute a distance-weighted least squares fit // from all the originally populated mesh points, weighted toward the point // being extrapolated so that nearby points will have greater influence on @@ -1744,7 +1744,6 @@ void unified_bed_leveling::smart_fill_mesh() { } } } - IF_DISABLED(DISABLE_UBL_SERIAL_CHITCHAT, SERIAL_ECHOLNPGM(" done.")); } #endif // UBL_G29_P31