From 7953c02de3f3c243b11f23bb2b22634c211c9707 Mon Sep 17 00:00:00 2001 From: Tom Brazier Date: Sat, 5 Mar 2022 11:08:09 +0000 Subject: [PATCH] Correctly reset cached z value lookups. #23843 --- Marlin/src/feature/bedlevel/abl/bbl.cpp | 22 +++++++++++++--------- Marlin/src/feature/bedlevel/abl/bbl.h | 2 ++ Marlin/src/feature/bedlevel/bedlevel.cpp | 6 ------ Marlin/src/gcode/bedlevel/M420.cpp | 3 ++- 4 files changed, 17 insertions(+), 16 deletions(-) diff --git a/Marlin/src/feature/bedlevel/abl/bbl.cpp b/Marlin/src/feature/bedlevel/abl/bbl.cpp index 476d8e512a2a..b11c44c9ad3d 100644 --- a/Marlin/src/feature/bedlevel/abl/bbl.cpp +++ b/Marlin/src/feature/bedlevel/abl/bbl.cpp @@ -41,6 +41,8 @@ xy_pos_t bilinear_bed_leveling::grid_spacing, bilinear_bed_leveling::grid_start; xy_float_t bilinear_bed_leveling::grid_factor; bed_mesh_t bilinear_bed_leveling::z_values; +xy_pos_t bilinear_bed_leveling::cached_rel; +xy_int8_t bilinear_bed_leveling::cached_g; /** * Extrapolate a single point from its neighbors @@ -265,6 +267,8 @@ void bilinear_bed_leveling::print_leveling_grid() { // Refresh after other values have been updated void bilinear_bed_leveling::refresh_bed_level() { TERN_(ABL_BILINEAR_SUBDIVISION, bed_level_virt_interpolate()); + cached_rel.x = cached_rel.y = -999.999; + cached_g.x = cached_g.y = -99; } #if ENABLED(ABL_BILINEAR_SUBDIVISION) @@ -286,10 +290,10 @@ float bilinear_bed_leveling::get_z_correction(const xy_pos_t &raw) { static float z1, d2, z3, d4, L, D; - static xy_pos_t prev { -999.999, -999.999 }, ratio; + static xy_pos_t ratio; // Whole units for the grid line indices. Constrained within bounds. - static xy_int8_t thisg, nextg, lastg { -99, -99 }; + static xy_int8_t thisg, nextg; // XY relative to the probed area xy_pos_t rel = raw - grid_start.asFloat(); @@ -300,8 +304,8 @@ float bilinear_bed_leveling::get_z_correction(const xy_pos_t &raw) { #define FAR_EDGE_OR_BOX 1 // Just use the grid far edge #endif - if (prev.x != rel.x) { - prev.x = rel.x; + if (cached_rel.x != rel.x) { + cached_rel.x = rel.x; ratio.x = rel.x * ABL_BG_FACTOR(x); const float gx = constrain(FLOOR(ratio.x), 0, ABL_BG_POINTS_X - (FAR_EDGE_OR_BOX)); ratio.x -= gx; // Subtract whole to get the ratio within the grid box @@ -315,10 +319,10 @@ float bilinear_bed_leveling::get_z_correction(const xy_pos_t &raw) { nextg.x = _MIN(thisg.x + 1, ABL_BG_POINTS_X - 1); } - if (prev.y != rel.y || lastg.x != thisg.x) { + if (cached_rel.y != rel.y || cached_g.x != thisg.x) { - if (prev.y != rel.y) { - prev.y = rel.y; + if (cached_rel.y != rel.y) { + cached_rel.y = rel.y; ratio.y = rel.y * ABL_BG_FACTOR(y); const float gy = constrain(FLOOR(ratio.y), 0, ABL_BG_POINTS_Y - (FAR_EDGE_OR_BOX)); ratio.y -= gy; @@ -332,8 +336,8 @@ float bilinear_bed_leveling::get_z_correction(const xy_pos_t &raw) { nextg.y = _MIN(thisg.y + 1, ABL_BG_POINTS_Y - 1); } - if (lastg != thisg) { - lastg = thisg; + if (cached_g != thisg) { + cached_g = thisg; // Z at the box corners z1 = ABL_BG_GRID(thisg.x, thisg.y); // left-front d2 = ABL_BG_GRID(thisg.x, nextg.y) - z1; // left-back (delta) diff --git a/Marlin/src/feature/bedlevel/abl/bbl.h b/Marlin/src/feature/bedlevel/abl/bbl.h index c31ba257442a..6705f21f439b 100644 --- a/Marlin/src/feature/bedlevel/abl/bbl.h +++ b/Marlin/src/feature/bedlevel/abl/bbl.h @@ -27,6 +27,8 @@ class bilinear_bed_leveling { static xy_pos_t grid_spacing, grid_start; static xy_float_t grid_factor; static bed_mesh_t z_values; + static xy_pos_t cached_rel; + static xy_int8_t cached_g; static void extrapolate_one_point(const uint8_t x, const uint8_t y, const int8_t xdir, const int8_t ydir); diff --git a/Marlin/src/feature/bedlevel/bedlevel.cpp b/Marlin/src/feature/bedlevel/bedlevel.cpp index ec86d562156c..ff48fb16ad8f 100644 --- a/Marlin/src/feature/bedlevel/bedlevel.cpp +++ b/Marlin/src/feature/bedlevel/bedlevel.cpp @@ -67,12 +67,6 @@ void set_bed_leveling_enabled(const bool enable/*=true*/) { planner.synchronize(); - #if ENABLED(AUTO_BED_LEVELING_BILINEAR) - // Force bbl.get_z_correction to re-calculate next time - const xyz_pos_t reset { -9999.999, -9999.999, 0 }; - (void)bbl.get_z_correction(reset); - #endif - if (planner.leveling_active) { // leveling from on to off if (DEBUGGING(LEVELING)) DEBUG_POS("Leveling ON", current_position); // change unleveled current_position to physical current_position without moving steppers. diff --git a/Marlin/src/gcode/bedlevel/M420.cpp b/Marlin/src/gcode/bedlevel/M420.cpp index cfeb0793d88d..c8325b1fc5c1 100644 --- a/Marlin/src/gcode/bedlevel/M420.cpp +++ b/Marlin/src/gcode/bedlevel/M420.cpp @@ -77,6 +77,7 @@ void GcodeSuite::M420() { Z_VALUES(x, y) = 0.001 * random(-200, 200); TERN_(EXTENSIBLE_UI, ExtUI::onMeshUpdate(x, y, Z_VALUES(x, y))); } + TERN_(AUTO_BED_LEVELING_BILINEAR, bbl.refresh_bed_level()); SERIAL_ECHOPGM("Simulated " STRINGIFY(GRID_MAX_POINTS_X) "x" STRINGIFY(GRID_MAX_POINTS_Y) " mesh "); SERIAL_ECHOPGM(" (", x_min); SERIAL_CHAR(','); SERIAL_ECHO(y_min); @@ -180,7 +181,7 @@ void GcodeSuite::M420() { Z_VALUES(x, y) -= zmean; TERN_(EXTENSIBLE_UI, ExtUI::onMeshUpdate(x, y, Z_VALUES(x, y))); } - TERN_(ABL_BILINEAR_SUBDIVISION, bbl.refresh_bed_level()); + TERN_(AUTO_BED_LEVELING_BILINEAR, bbl.refresh_bed_level()); } #endif