Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 50 additions & 33 deletions Marlin/src/feature/bedlevel/abl/bbl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,20 +125,20 @@ void LevelingBilinear::set_grid(const xy_pos_t &_grid_spacing, const xy_pos_t &_
*/
void LevelingBilinear::extrapolate_unprobed_bed_level() {
#ifdef HALF_IN_X
constexpr uint8_t ctrx2 = 0, xend = nr_grid_points.x - 1;
constexpr uint8_t ctrx2 = 0, xend = TERN(VARIABLE_GRID_POINTS, nr_grid_points.x, GRID_MAX_POINTS_X) - 1;
#else
IF_DISABLED(VARIABLE_GRID_POINTS, constexpr) uint8_t
ctrx1 = (nr_grid_points.x - 1) / 2, // left-of-center
ctrx2 = nr_grid_points.x / 2, // right-of-center
ctrx1 = (TERN(VARIABLE_GRID_POINTS, (nr_grid_points.x - 1), GRID_MAX_CELLS_X)) / 2, // left-of-center
ctrx2 = TERN(VARIABLE_GRID_POINTS, nr_grid_points.x, GRID_MAX_POINTS_X) / 2, // right-of-center
xend = ctrx1;
#endif

#ifdef HALF_IN_Y
constexpr uint8_t ctry2 = 0, yend = nr_grid_points.y - 1;
constexpr uint8_t ctry2 = 0, yend = TERN(VARIABLE_GRID_POINTS, nr_grid_points.y, GRID_MAX_POINTS_Y) - 1;
#else
IF_DISABLED(VARIABLE_GRID_POINTS, constexpr) uint8_t
ctry1 = (nr_grid_points.y - 1) / 2, // top-of-center
ctry2 = nr_grid_points.y / 2, // bottom-of-center
ctry1 = (TERN(VARIABLE_GRID_POINTS, (nr_grid_points.y - 1), GRID_MAX_CELLS_Y)) / 2, // top-of-center
ctry2 = TERN(VARIABLE_GRID_POINTS, nr_grid_points.y, GRID_MAX_POINTS_Y) / 2, // bottom-of-center
yend = ctry1;
#endif

Expand All @@ -165,26 +165,43 @@ void LevelingBilinear::extrapolate_unprobed_bed_level() {
void LevelingBilinear::print_leveling_grid(const bed_mesh_t* _z_values/*=nullptr*/ OPTARG(VARIABLE_GRID_POINTS, const xy_uint8_t *_grid_points/*=nullptr*/)) {
// Print the passed mesh grid(s) or the current mesh
SERIAL_ECHOLNPGM("Bilinear Leveling Grid:");
PRINT_2D_ARRAY(
GRID_MAX_POINTS_X, GRID_MAX_POINTS_Y, 3, _z_values ? *_z_values[0] : z_values[0],
_grid_points ? _grid_points->x : nr_grid_points.x,
_grid_points ? _grid_points->y : nr_grid_points.y
);
#if DISABLED(VARIABLE_GRID_POINTS)
print_2d_array(GRID_MAX_POINTS_X, GRID_MAX_POINTS_Y, 3, _z_values ? *_z_values[0] : z_values[0]);
#endif
#if ENABLED(VARIABLE_GRID_POINTS)
PRINT_2D_ARRAY(
GRID_MAX_POINTS_X, GRID_MAX_POINTS_Y, 3, _z_values ? *_z_values[0] : z_values[0],
_grid_points ? _grid_points->x : nr_grid_points.x,
_grid_points ? _grid_points->y : nr_grid_points.y
);
#endif

#if ENABLED(ABL_BILINEAR_SUBDIVISION)
if (!_z_values) {
SERIAL_ECHOLNPGM("Subdivided with CATMULL ROM Leveling Grid:");
PRINT_2D_ARRAY(
ABL_MAX_POINTS_VIRT_X, ABL_MAX_POINTS_VIRT_Y, 5, z_values_virt[0],
nr_grid_points_virt.x, nr_grid_points_virt.y
);
#if DISABLED(VARIABLE_GRID_POINTS)
print_2d_array(ABL_GRID_POINTS_VIRT_X, ABL_GRID_POINTS_VIRT_Y, 5, z_values_virt[0]);
#endif
#if ENABLED(VARIABLE_GRID_POINTS)
PRINT_2D_ARRAY(
ABL_MAX_POINTS_VIRT_X, ABL_MAX_POINTS_VIRT_Y, 5, z_values_virt[0],
nr_grid_points_virt.x, nr_grid_points_virt.y
);
#endif
}
#endif
}

#if ENABLED(ABL_BILINEAR_SUBDIVISION)

float LevelingBilinear::z_values_virt[ABL_MAX_POINTS_VIRT_X][ABL_MAX_POINTS_VIRT_Y];

#if DISABLED(VARIABLE_GRID_POINTS)
#define ABL_TEMP_POINTS_X (GRID_MAX_POINTS_X + 2)
#define ABL_TEMP_POINTS_Y (GRID_MAX_POINTS_Y + 2)
float LevelingBilinear::z_values_virt[ABL_GRID_POINTS_VIRT_X][ABL_GRID_POINTS_VIRT_Y];
#endif
#if ENABLED(VARIABLE_GRID_POINTS)
float LevelingBilinear::z_values_virt[ABL_MAX_POINTS_VIRT_X][ABL_MAX_POINTS_VIRT_Y];
#endif
xy_pos_t LevelingBilinear::grid_spacing_virt;
xy_float_t LevelingBilinear::grid_factor_virt;
#if ENABLED(VARIABLE_GRID_POINTS)
Expand All @@ -194,20 +211,20 @@ void LevelingBilinear::print_leveling_grid(const bed_mesh_t* _z_values/*=nullptr
#define LINEAR_EXTRAPOLATION(E, I) ((E) * 2 - (I))
float LevelingBilinear::virt_coord(const uint8_t x, const uint8_t y) {
uint8_t ep = 0, ip = 1;
if (x > nr_grid_points.x + 1 || y > nr_grid_points.y + 1) {
if (x > (TERN(VARIABLE_GRID_POINTS, nr_grid_points.x, GRID_MAX_POINTS_X) + 1) || y > (TERN(VARIABLE_GRID_POINTS, nr_grid_points.y, GRID_MAX_POINTS_Y) + 1)) {
// The requested point requires extrapolating two points beyond the mesh.
// These values are only requested for the edges of the mesh, which are always an actual mesh point,
// and do not require interpolation. When interpolation is not needed, this "Mesh + 2" point is
// cancelled out in virt_cmr and does not impact the result. Return 0.0 rather than
// making this function more complex by extrapolating two points.
return 0.0;
}
if (!x || x == (nr_grid_points.x + 2) - 1) {
if (!x || x == TERN(VARIABLE_GRID_POINTS, (nr_grid_points.x + 2), ABL_TEMP_POINTS_X) - 1) {
if (x) {
ep = nr_grid_points.x - 1;
ip = (nr_grid_points.x - 1) - 1;
ep = TERN(VARIABLE_GRID_POINTS, nr_grid_points.x, GRID_MAX_POINTS_X) - 1;
ip = (TERN(VARIABLE_GRID_POINTS, (nr_grid_points.x - 1), GRID_MAX_CELLS_X)) - 1;
}
if (WITHIN(y, 1, (nr_grid_points.y + 2) - 2))
if (WITHIN(y, 1, TERN(VARIABLE_GRID_POINTS, (nr_grid_points.y + 2), ABL_TEMP_POINTS_Y) - 2))
return LINEAR_EXTRAPOLATION(
z_values[ep][y - 1],
z_values[ip][y - 1]
Expand All @@ -218,12 +235,12 @@ void LevelingBilinear::print_leveling_grid(const bed_mesh_t* _z_values/*=nullptr
virt_coord(ip + 1, y)
);
}
if (!y || y == (nr_grid_points.y + 2) - 1) {
if (!y || y == TERN(VARIABLE_GRID_POINTS, (nr_grid_points.y + 2), ABL_TEMP_POINTS_Y) - 1) {
if (y) {
ep = nr_grid_points.y - 1;
ip = (nr_grid_points.y - 1) - 1;
ep = TERN(VARIABLE_GRID_POINTS, nr_grid_points.y, GRID_MAX_POINTS_Y) - 1;
ip = (TERN(VARIABLE_GRID_POINTS, (nr_grid_points.y - 1), GRID_MAX_CELLS_Y)) - 1;
}
if (WITHIN(x, 1, (nr_grid_points.x + 2) - 2))
if (WITHIN(x, 1, TERN(VARIABLE_GRID_POINTS, (nr_grid_points.x + 2), ABL_TEMP_POINTS_X) - 2))
return LINEAR_EXTRAPOLATION(
z_values[x - 1][ep],
z_values[x - 1][ip]
Expand Down Expand Up @@ -263,11 +280,11 @@ void LevelingBilinear::print_leveling_grid(const bed_mesh_t* _z_values/*=nullptr
#if ENABLED(VARIABLE_GRID_POINTS)
nr_grid_points_virt.set((nr_grid_points.x - 1) * (uint16_t)(BILINEAR_SUBDIVISIONS) + (uint16_t)1, (nr_grid_points.y - 1) * (uint16_t)(BILINEAR_SUBDIVISIONS) + (uint16_t)1);
#endif
for (uint8_t y = 0; y < nr_grid_points.y; ++y)
for (uint8_t x = 0; x < nr_grid_points.x; ++x)
for (uint8_t y = 0; y < TERN(VARIABLE_GRID_POINTS, nr_grid_points.y, GRID_MAX_POINTS_Y); ++y)
for (uint8_t x = 0; x < TERN(VARIABLE_GRID_POINTS, nr_grid_points.x, GRID_MAX_POINTS_X); ++x)
for (uint8_t ty = 0; ty < BILINEAR_SUBDIVISIONS; ++ty)
for (uint8_t tx = 0; tx < BILINEAR_SUBDIVISIONS; ++tx) {
if ((ty && y == nr_grid_points.y - 1) || (tx && x == nr_grid_points.x - 1))
if ((ty && y == (TERN(VARIABLE_GRID_POINTS, nr_grid_points.y, (GRID_MAX_POINTS_Y))) - 1) || (tx && x == (TERN(VARIABLE_GRID_POINTS, nr_grid_points.y, (GRID_MAX_POINTS_X))) - 1))
continue;
z_values_virt[x * (BILINEAR_SUBDIVISIONS) + tx][y * (BILINEAR_SUBDIVISIONS) + ty] =
virt_2cmr(x + 1, y + 1, (float)tx / (BILINEAR_SUBDIVISIONS), (float)ty / (BILINEAR_SUBDIVISIONS));
Expand All @@ -290,14 +307,14 @@ void LevelingBilinear::refresh_bed_level() {
#if ENABLED(ABL_BILINEAR_SUBDIVISION)
#define ABL_BG_SPACING(A) grid_spacing_virt.A
#define ABL_BG_FACTOR(A) grid_factor_virt.A
#define ABL_BG_POINTS_X nr_grid_points_virt.x
#define ABL_BG_POINTS_Y nr_grid_points_virt.y
#define ABL_BG_POINTS_X TERN(VARIABLE_GRID_POINTS, nr_grid_points_virt.x, ABL_GRID_POINTS_VIRT_X)
#define ABL_BG_POINTS_Y TERN(VARIABLE_GRID_POINTS, nr_grid_points_virt.y, ABL_GRID_POINTS_VIRT_Y)
#define ABL_BG_GRID(X,Y) z_values_virt[X][Y]
#else
#define ABL_BG_SPACING(A) grid_spacing.A
#define ABL_BG_FACTOR(A) grid_factor.A
#define ABL_BG_POINTS_X nr_grid_points.x
#define ABL_BG_POINTS_Y nr_grid_points.y
#define ABL_BG_POINTS_X TERN(VARIABLE_GRID_POINTS, nr_grid_points.x, GRID_MAX_POINTS_X)
#define ABL_BG_POINTS_Y TERN(VARIABLE_GRID_POINTS, nr_grid_points.y, GRID_MAX_POINTS_Y)
#define ABL_BG_GRID(X,Y) z_values[X][Y]
#endif

Expand Down
13 changes: 10 additions & 3 deletions Marlin/src/feature/bedlevel/abl/bbl.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,17 @@ class LevelingBilinear {
static void extrapolate_one_point(const uint8_t x, const uint8_t y, const int8_t xdir, const int8_t ydir);

#if ENABLED(ABL_BILINEAR_SUBDIVISION)
#define ABL_MAX_POINTS_VIRT_X (GRID_MAX_CELLS_X * (BILINEAR_SUBDIVISIONS) + 1)
#define ABL_MAX_POINTS_VIRT_Y (GRID_MAX_CELLS_Y * (BILINEAR_SUBDIVISIONS) + 1)
#if DISABLED(VARIABLE_GRID_POINTS)
#define ABL_GRID_POINTS_VIRT_X (GRID_MAX_CELLS_X * (BILINEAR_SUBDIVISIONS) + 1)
#define ABL_GRID_POINTS_VIRT_Y (GRID_MAX_CELLS_Y * (BILINEAR_SUBDIVISIONS) + 1)
static float z_values_virt[ABL_GRID_POINTS_VIRT_X][ABL_GRID_POINTS_VIRT_Y];
#endif
#if ENABLED(VARIABLE_GRID_POINTS)
#define ABL_MAX_POINTS_VIRT_X (GRID_MAX_CELLS_X * (BILINEAR_SUBDIVISIONS) + 1)
#define ABL_MAX_POINTS_VIRT_Y (GRID_MAX_CELLS_Y * (BILINEAR_SUBDIVISIONS) + 1)
static float z_values_virt[ABL_MAX_POINTS_VIRT_X][ABL_MAX_POINTS_VIRT_Y];
#endif

static float z_values_virt[ABL_MAX_POINTS_VIRT_X][ABL_MAX_POINTS_VIRT_Y];
static xy_pos_t grid_spacing_virt;
static xy_float_t grid_factor_virt;
#if ENABLED(VARIABLE_GRID_POINTS)
Expand Down
23 changes: 6 additions & 17 deletions Marlin/src/feature/bedlevel/bedlevel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,21 +135,10 @@ void reset_bed_level() {
/**
* Print calibration results for plotting or manual frame adjustment.
*/
void print_2d_array(const uint8_t sx, const uint8_t sy, const uint8_t precision, const float *values
#if ENABLED(VARIABLE_GRID_POINTS)
, uint8_t print_x/*=0*/, uint8_t print_y/*=0*/
#endif
) {

#if ENABLED(VARIABLE_GRID_POINTS)
if (!print_x) print_x = sx;
if (!print_y) print_y = sy;
#else
const uint8_t print_x = sx, print_y = sy;
#endif
void print_2d_array(const uint8_t sx, const uint8_t sy, const uint8_t precision, const float *values OPTARG(VARIABLE_GRID_POINTS, uint8_t print_x/*=0*/, uint8_t print_y/*=0*/)) {

#ifndef SCAD_MESH_OUTPUT
for (uint8_t x = 0; x < print_x; ++x) {
for (uint8_t x = 0; x < TERN(VARIABLE_GRID_POINTS, print_x, sx); ++x) {
SERIAL_ECHO_SP(precision + (x < 10 ? 3 : 2));
SERIAL_ECHO(x);
}
Expand All @@ -158,14 +147,14 @@ void reset_bed_level() {
#ifdef SCAD_MESH_OUTPUT
SERIAL_ECHOLNPGM("measured_z = ["); // open 2D array
#endif
for (uint8_t y = 0; y < print_y; ++y) {
for (uint8_t y = 0; y < TERN(VARIABLE_GRID_POINTS, print_y, sy); ++y) {
#ifdef SCAD_MESH_OUTPUT
SERIAL_ECHOPGM(" ["); // open sub-array
#else
if (y < 10) SERIAL_CHAR(' ');
SERIAL_ECHO(y);
#endif
for (uint8_t x = 0; x < print_x; ++x) {
for (uint8_t x = 0; x < TERN(VARIABLE_GRID_POINTS, print_x, sx); ++x) {
SERIAL_CHAR(' ');
const float offset = values[x * sy + y];
if (!isnan(offset)) {
Expand All @@ -183,12 +172,12 @@ void reset_bed_level() {
#endif
}
#ifdef SCAD_MESH_OUTPUT
if (x < print_x - 1) SERIAL_CHAR(',');
if (x < TERN(VARIABLE_GRID_POINTS, print_x, sx) - 1) SERIAL_CHAR(',');
#endif
}
#ifdef SCAD_MESH_OUTPUT
SERIAL_ECHOPGM(" ]"); // close sub-array
if (y < print_y - 1) SERIAL_CHAR(',');
if (y < TERN(VARIABLE_GRID_POINTS, print_y, sy) - 1) SERIAL_CHAR(',');
#endif
SERIAL_EOL();
}
Expand Down
6 changes: 1 addition & 5 deletions Marlin/src/feature/bedlevel/bedlevel.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,7 @@ class TemporaryBedLevelingState {
/**
* Print calibration results for plotting or manual frame adjustment.
*/
void print_2d_array(const uint8_t sx, const uint8_t sy, const uint8_t precision, const float *values
#if ENABLED(VARIABLE_GRID_POINTS)
, uint8_t print_x=0, uint8_t print_y=0
#endif
);
void print_2d_array(const uint8_t sx, const uint8_t sy, const uint8_t precision, const float *values OPTARG(VARIABLE_GRID_POINTS, uint8_t print_x=0, uint8_t print_y=0));

#if ENABLED(VARIABLE_GRID_POINTS)
#define PRINT_2D_ARRAY(X, Y, P, V, M...) print_2d_array(X, Y, P, V, M)
Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/feature/bedlevel/mbl/mesh_bed_leveling.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class mesh_bed_leveling {
static void reset();

FORCE_INLINE static bool has_mesh() {
GRID_LOOP(x, y) if (z_values[x][y]) return true;
GRID_LOOP_COND(x, y) if (z_values[x][y]) return true;
return false;
}

Expand Down
8 changes: 5 additions & 3 deletions Marlin/src/feature/bedlevel/ubl/ubl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ float unified_bed_leveling::z_values[GRID_MAX_POINTS_X][GRID_MAX_POINTS_Y];
#endif

#if ENABLED(VARIABLE_GRID_POINTS)
xy_uint8_t unified_bed_leveling::grid_points;
xy_uint8_t unified_bed_leveling::nr_grid_points;
xy_float_t unified_bed_leveling::mesh_dist, // Initialized by settings.load
unified_bed_leveling::mesh_dist_recip;

Expand Down Expand Up @@ -114,7 +114,9 @@ void unified_bed_leveling::reset() {
set_bed_leveling_enabled(false);
storage_slot = -1;
ZERO(z_values);
set_grid_points(xy_uint8_t({ GRID_MAX_POINTS_X, GRID_MAX_POINTS_Y }));
#if ENABLED(VARIABLE_GRID_POINTS)
set_nr_grid_points(xy_uint8_t({ GRID_MAX_POINTS_X, GRID_MAX_POINTS_Y }));
#endif
#if ENABLED(EXTENSIBLE_UI)
GRID_LOOP(x, y) ExtUI::onMeshUpdate(x, y, 0);
#endif
Expand Down Expand Up @@ -170,7 +172,7 @@ static void serial_echo_xy(const uint8_t sp, const int16_t x, const int16_t y) {

static void serial_echo_column_labels(const uint8_t sp) {
SERIAL_ECHO_SP(7);
for (uint8_t i = 0; i < GRID_USED_POINTS_X; ++i) {
for (uint8_t i = 0; i < TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_X, GRID_MAX_POINTS_X); ++i) {
if (i < 10) SERIAL_CHAR(' ');
SERIAL_ECHO(i);
SERIAL_ECHO_SP(sp);
Expand Down
Loading
Loading