Skip to content

Commit

Permalink
Correctly reset cached z value lookups. MarlinFirmware#23843
Browse files Browse the repository at this point in the history
  • Loading branch information
tombrazier committed Mar 7, 2022
1 parent 7216e63 commit 7953c02
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 16 deletions.
22 changes: 13 additions & 9 deletions Marlin/src/feature/bedlevel/abl/bbl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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();
Expand All @@ -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
Expand All @@ -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;
Expand All @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions Marlin/src/feature/bedlevel/abl/bbl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
6 changes: 0 additions & 6 deletions Marlin/src/feature/bedlevel/bedlevel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion Marlin/src/gcode/bedlevel/M420.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 7953c02

Please sign in to comment.