Skip to content

Commit 9229357

Browse files
thinkyheadLCh-77
authored andcommitted
♻️ Common Bed Leveling object name, accessors (MarlinFirmware#24214)
1 parent 14327e0 commit 9229357

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+392
-441
lines changed

Marlin/src/HAL/AVR/inc/SanityCheck.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,16 @@
3737
|| X_ENA_PIN == N || Y_ENA_PIN == N || Z_ENA_PIN == N \
3838
)
3939
#if CONF_SERIAL_IS(0) // D0-D1. No known conflicts.
40-
#endif
40+
#endif
4141
#if CONF_SERIAL_IS(1) && (CHECK_SERIAL_PIN(18) || CHECK_SERIAL_PIN(19))
4242
#error "Serial Port 1 pin D18 and/or D19 conflicts with another pin on the board."
43-
#endif
43+
#endif
4444
#if CONF_SERIAL_IS(2) && (CHECK_SERIAL_PIN(16) || CHECK_SERIAL_PIN(17))
4545
#error "Serial Port 2 pin D16 and/or D17 conflicts with another pin on the board."
4646
#endif
4747
#if CONF_SERIAL_IS(3) && (CHECK_SERIAL_PIN(14) || CHECK_SERIAL_PIN(15))
4848
#error "Serial Port 3 pin D14 and/or D15 conflicts with another pin on the board."
49-
#endif
49+
#endif
5050
#undef CHECK_SERIAL_PIN
5151

5252
/**

Marlin/src/HAL/DUE/inc/SanityCheck.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,16 @@
3737
|| X_ENA_PIN == N || Y_ENA_PIN == N || Z_ENA_PIN == N \
3838
)
3939
#if CONF_SERIAL_IS(0) // D0-D1. No known conflicts.
40-
#endif
40+
#endif
4141
#if CONF_SERIAL_IS(1) && (CHECK_SERIAL_PIN(18) || CHECK_SERIAL_PIN(19))
4242
#error "Serial Port 1 pin D18 and/or D19 conflicts with another pin on the board."
43-
#endif
43+
#endif
4444
#if CONF_SERIAL_IS(2) && (CHECK_SERIAL_PIN(16) || CHECK_SERIAL_PIN(17))
4545
#error "Serial Port 2 pin D16 and/or D17 conflicts with another pin on the board."
4646
#endif
4747
#if CONF_SERIAL_IS(3) && (CHECK_SERIAL_PIN(14) || CHECK_SERIAL_PIN(15))
4848
#error "Serial Port 3 pin D14 and/or D15 conflicts with another pin on the board."
49-
#endif
49+
#endif
5050
#undef CHECK_SERIAL_PIN
5151

5252
/**

Marlin/src/MarlinCore.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ inline void manage_inactivity(const bool no_stepper_sleep=false) {
448448
TERN_(DISABLE_INACTIVE_W, stepper.disable_axis(W_AXIS));
449449
TERN_(DISABLE_INACTIVE_E, stepper.disable_e_steppers());
450450

451-
TERN_(AUTO_BED_LEVELING_UBL, ubl.steppers_were_disabled());
451+
TERN_(AUTO_BED_LEVELING_UBL, bedlevel.steppers_were_disabled());
452452
}
453453
}
454454
else

Marlin/src/core/utility.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,10 @@ void safe_delay(millis_t ms) {
132132
#else
133133
#if ENABLED(AUTO_BED_LEVELING_UBL)
134134
SERIAL_ECHOPGM("UBL Adjustment Z");
135-
const float rz = ubl.get_z_correction(current_position);
135+
const float rz = bedlevel.get_z_correction(current_position);
136136
#elif ENABLED(AUTO_BED_LEVELING_BILINEAR)
137137
SERIAL_ECHOPGM("ABL Adjustment Z");
138-
const float rz = bbl.get_z_correction(current_position);
138+
const float rz = bedlevel.get_z_correction(current_position);
139139
#endif
140140
SERIAL_ECHO(ftostr43sign(rz, '+'));
141141
#if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
@@ -156,11 +156,11 @@ void safe_delay(millis_t ms) {
156156
SERIAL_ECHOPGM("Mesh Bed Leveling");
157157
if (planner.leveling_active) {
158158
SERIAL_ECHOLNPGM(" (enabled)");
159-
SERIAL_ECHOPGM("MBL Adjustment Z", ftostr43sign(mbl.get_z(current_position), '+'));
159+
SERIAL_ECHOPGM("MBL Adjustment Z", ftostr43sign(bedlevel.get_z(current_position), '+'));
160160
#if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
161161
if (planner.z_fade_height) {
162162
SERIAL_ECHOPGM(" (", ftostr43sign(
163-
mbl.get_z(current_position, planner.fade_scaling_factor_for_z(current_position.z)), '+'
163+
bedlevel.get_z(current_position, planner.fade_scaling_factor_for_z(current_position.z)), '+'
164164
));
165165
SERIAL_CHAR(')');
166166
}

Marlin/src/feature/bedlevel/abl/bbl.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
#include "../../../lcd/extui/ui_api.h"
3636
#endif
3737

38-
LevelingBilinear bbl;
38+
LevelingBilinear bedlevel;
3939

4040
xy_pos_t LevelingBilinear::grid_spacing,
4141
LevelingBilinear::grid_start;
@@ -258,8 +258,8 @@ void LevelingBilinear::print_leveling_grid(const bed_mesh_t* _z_values /*= NULL*
258258
);
259259
}
260260
}
261-
#endif // ABL_BILINEAR_SUBDIVISION
262261

262+
#endif // ABL_BILINEAR_SUBDIVISION
263263

264264
// Refresh after other values have been updated
265265
void LevelingBilinear::refresh_bed_level() {

Marlin/src/feature/bedlevel/abl/bbl.h

+9-12
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,12 @@
2424
#include "../../../inc/MarlinConfigPre.h"
2525

2626
class LevelingBilinear {
27-
private:
27+
public:
28+
static bed_mesh_t z_values;
2829
static xy_pos_t grid_spacing, grid_start;
30+
31+
private:
2932
static xy_float_t grid_factor;
30-
static bed_mesh_t z_values;
3133
static xy_pos_t cached_rel;
3234
static xy_int8_t cached_g;
3335

@@ -54,20 +56,15 @@ class LevelingBilinear {
5456
static void print_leveling_grid(const bed_mesh_t* _z_values = NULL);
5557
static void refresh_bed_level();
5658
static bool has_mesh() { return !!grid_spacing.x; }
57-
static bed_mesh_t& get_z_values() { return z_values; }
58-
static const xy_pos_t& get_grid_spacing() { return grid_spacing; }
59-
static const xy_pos_t& get_grid_start() { return grid_start; }
60-
static float get_mesh_x(int16_t i) { return grid_start.x + i * grid_spacing.x; }
61-
static float get_mesh_y(int16_t j) { return grid_start.y + j * grid_spacing.y; }
59+
static bool mesh_is_valid() { return has_mesh(); }
60+
static float get_mesh_x(const uint8_t i) { return grid_start.x + i * grid_spacing.x; }
61+
static float get_mesh_y(const uint8_t j) { return grid_start.y + j * grid_spacing.y; }
6262
static float get_z_correction(const xy_pos_t &raw);
63+
static constexpr float get_z_offset() { return 0.0f; }
6364

6465
#if IS_CARTESIAN && DISABLED(SEGMENT_LEVELED_MOVES)
6566
static void line_to_destination(const_feedRate_t scaled_fr_mm_s, uint16_t x_splits=0xFFFF, uint16_t y_splits=0xFFFF);
6667
#endif
6768
};
6869

69-
extern LevelingBilinear bbl;
70-
71-
#define _GET_MESH_X(I) bbl.get_mesh_x(I)
72-
#define _GET_MESH_Y(J) bbl.get_mesh_y(J)
73-
#define Z_VALUES_ARR bbl.get_z_values()
70+
extern LevelingBilinear bedlevel;

Marlin/src/feature/bedlevel/bedlevel.cpp

+16-21
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,11 @@
4747
#endif
4848

4949
bool leveling_is_valid() {
50-
return TERN1(MESH_BED_LEVELING, mbl.has_mesh())
51-
&& TERN1(AUTO_BED_LEVELING_BILINEAR, bbl.has_mesh())
52-
&& TERN1(AUTO_BED_LEVELING_UBL, ubl.mesh_is_valid());
50+
return TERN1(HAS_MESH, bedlevel.mesh_is_valid());
5351
}
5452

5553
/**
56-
* Turn bed leveling on or off, fixing the current
57-
* position as-needed.
54+
* Turn bed leveling on or off, correcting the current position.
5855
*
5956
* Disable: Current position = physical position
6057
* Enable: Current position = "unleveled" physical position
@@ -65,24 +62,31 @@ void set_bed_leveling_enabled(const bool enable/*=true*/) {
6562

6663
if (can_change && enable != planner.leveling_active) {
6764

65+
auto _report_leveling = []{
66+
if (DEBUGGING(LEVELING)) {
67+
if (planner.leveling_active)
68+
DEBUG_POS("Leveling ON", current_position);
69+
else
70+
DEBUG_POS("Leveling OFF", current_position);
71+
}
72+
};
73+
74+
_report_leveling();
6875
planner.synchronize();
6976

7077
if (planner.leveling_active) { // leveling from on to off
71-
if (DEBUGGING(LEVELING)) DEBUG_POS("Leveling ON", current_position);
7278
// change unleveled current_position to physical current_position without moving steppers.
7379
planner.apply_leveling(current_position);
7480
planner.leveling_active = false; // disable only AFTER calling apply_leveling
75-
if (DEBUGGING(LEVELING)) DEBUG_POS("...Now OFF", current_position);
7681
}
7782
else { // leveling from off to on
78-
if (DEBUGGING(LEVELING)) DEBUG_POS("Leveling OFF", current_position);
7983
planner.leveling_active = true; // enable BEFORE calling unapply_leveling, otherwise ignored
8084
// change physical current_position to unleveled current_position without moving steppers.
8185
planner.unapply_leveling(current_position);
82-
if (DEBUGGING(LEVELING)) DEBUG_POS("...Now ON", current_position);
8386
}
8487

8588
sync_plan_position();
89+
_report_leveling();
8690
}
8791
}
8892

@@ -116,18 +120,9 @@ TemporaryBedLevelingState::TemporaryBedLevelingState(const bool enable) : saved(
116120
*/
117121
void reset_bed_level() {
118122
if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("reset_bed_level");
119-
#if ENABLED(AUTO_BED_LEVELING_UBL)
120-
ubl.reset();
121-
#else
122-
set_bed_leveling_enabled(false);
123-
#if ENABLED(MESH_BED_LEVELING)
124-
mbl.reset();
125-
#elif ENABLED(AUTO_BED_LEVELING_BILINEAR)
126-
bbl.reset();
127-
#elif ABL_PLANAR
128-
planner.bed_level_matrix.set_to_identity();
129-
#endif
130-
#endif
123+
IF_DISABLED(AUTO_BED_LEVELING_UBL, set_bed_leveling_enabled(false));
124+
TERN_(HAS_MESH, bedlevel.reset());
125+
TERN_(ABL_PLANAR, planner.bed_level_matrix.set_to_identity());
131126
}
132127

133128
#if EITHER(AUTO_BED_LEVELING_BILINEAR, MESH_BED_LEVELING)

Marlin/src/feature/bedlevel/bedlevel.h

+1-4
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,6 @@ class TemporaryBedLevelingState {
6969
#include "mbl/mesh_bed_leveling.h"
7070
#endif
7171

72-
#define Z_VALUES(X,Y) Z_VALUES_ARR[X][Y]
73-
#define _GET_MESH_POS(M) { _GET_MESH_X(M.a), _GET_MESH_Y(M.b) }
74-
7572
#if EITHER(AUTO_BED_LEVELING_BILINEAR, MESH_BED_LEVELING)
7673

7774
#include <stdint.h>
@@ -92,7 +89,7 @@ class TemporaryBedLevelingState {
9289
bool valid() const { return pos.x >= 0 && pos.y >= 0; }
9390
#if ENABLED(AUTO_BED_LEVELING_UBL)
9491
xy_pos_t meshpos() {
95-
return { ubl.mesh_index_to_xpos(pos.x), ubl.mesh_index_to_ypos(pos.y) };
92+
return { bedlevel.get_mesh_x(pos.x), bedlevel.get_mesh_y(pos.y) };
9693
}
9794
#endif
9895
operator xy_int8_t&() { return pos; }

Marlin/src/feature/bedlevel/mbl/mesh_bed_leveling.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
#include "../../../lcd/extui/ui_api.h"
3333
#endif
3434

35-
mesh_bed_leveling mbl;
35+
mesh_bed_leveling bedlevel;
3636

3737
float mesh_bed_leveling::z_offset,
3838
mesh_bed_leveling::z_values[GRID_MAX_POINTS_X][GRID_MAX_POINTS_Y],

Marlin/src/feature/bedlevel/mbl/mesh_bed_leveling.h

+10-11
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@ enum MeshLevelingState : char {
3434

3535
#define MESH_X_DIST (float(MESH_MAX_X - (MESH_MIN_X)) / (GRID_MAX_CELLS_X))
3636
#define MESH_Y_DIST (float(MESH_MAX_Y - (MESH_MIN_Y)) / (GRID_MAX_CELLS_Y))
37-
#define _GET_MESH_X(I) mbl.index_to_xpos[I]
38-
#define _GET_MESH_Y(J) mbl.index_to_ypos[J]
39-
#define Z_VALUES_ARR mbl.z_values
4037

4138
class mesh_bed_leveling {
4239
public:
@@ -56,6 +53,8 @@ class mesh_bed_leveling {
5653
return false;
5754
}
5855

56+
static bool mesh_is_valid() { return has_mesh(); }
57+
5958
static void set_z(const int8_t px, const int8_t py, const_float_t z) { z_values[px][py] = z; }
6059

6160
static void zigzag(const int8_t index, int8_t &px, int8_t &py) {
@@ -70,6 +69,9 @@ class mesh_bed_leveling {
7069
set_z(px, py, z);
7170
}
7271

72+
static float get_mesh_x(const uint8_t i) { return index_to_xpos[i]; }
73+
static float get_mesh_y(const uint8_t i) { return index_to_ypos[i]; }
74+
7375
static int8_t cell_index_x(const_float_t x) {
7476
int8_t cx = (x - (MESH_MIN_X)) * RECIPROCAL(MESH_X_DIST);
7577
return constrain(cx, 0, GRID_MAX_CELLS_X - 1);
@@ -102,25 +104,22 @@ class mesh_bed_leveling {
102104
return z1 + delta_a * delta_z;
103105
}
104106

105-
static float get_z(const xy_pos_t &pos
106-
OPTARG(ENABLE_LEVELING_FADE_HEIGHT, const_float_t factor=1.0f)
107-
) {
108-
#if DISABLED(ENABLE_LEVELING_FADE_HEIGHT)
109-
constexpr float factor = 1.0f;
110-
#endif
107+
static float get_z_offset() { return z_offset; }
108+
109+
static float get_z_correction(const xy_pos_t &pos) {
111110
const xy_int8_t ind = cell_indexes(pos);
112111
const float x1 = index_to_xpos[ind.x], x2 = index_to_xpos[ind.x+1],
113112
y1 = index_to_xpos[ind.y], y2 = index_to_xpos[ind.y+1],
114113
z1 = calc_z0(pos.x, x1, z_values[ind.x][ind.y ], x2, z_values[ind.x+1][ind.y ]),
115114
z2 = calc_z0(pos.x, x1, z_values[ind.x][ind.y+1], x2, z_values[ind.x+1][ind.y+1]),
116115
zf = calc_z0(pos.y, y1, z1, y2, z2);
117116

118-
return z_offset + zf * factor;
117+
return zf;
119118
}
120119

121120
#if IS_CARTESIAN && DISABLED(SEGMENT_LEVELED_MOVES)
122121
static void line_to_destination(const_feedRate_t scaled_fr_mm_s, uint8_t x_splits=0xFF, uint8_t y_splits=0xFF);
123122
#endif
124123
};
125124

126-
extern mesh_bed_leveling mbl;
125+
extern mesh_bed_leveling bedlevel;

Marlin/src/feature/bedlevel/ubl/ubl.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
#include "../bedlevel.h"
2828

29-
unified_bed_leveling ubl;
29+
unified_bed_leveling bedlevel;
3030

3131
#include "../../../MarlinCore.h"
3232
#include "../../../gcode/gcode.h"

Marlin/src/feature/bedlevel/ubl/ubl.h

+19-20
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ class unified_bed_leveling {
215215
return _UBL_OUTER_Z_RAISE;
216216
}
217217

218-
const float xratio = (rx0 - mesh_index_to_xpos(x1_i)) * RECIPROCAL(MESH_X_DIST),
218+
const float xratio = (rx0 - get_mesh_x(x1_i)) * RECIPROCAL(MESH_X_DIST),
219219
z1 = z_values[x1_i][yi];
220220

221221
return z1 + xratio * (z_values[_MIN(x1_i, (GRID_MAX_POINTS_X) - 2) + 1][yi] - z1); // Don't allow x1_i+1 to be past the end of the array
@@ -238,7 +238,7 @@ class unified_bed_leveling {
238238
return _UBL_OUTER_Z_RAISE;
239239
}
240240

241-
const float yratio = (ry0 - mesh_index_to_ypos(y1_i)) * RECIPROCAL(MESH_Y_DIST),
241+
const float yratio = (ry0 - get_mesh_y(y1_i)) * RECIPROCAL(MESH_Y_DIST),
242242
z1 = z_values[xi][y1_i];
243243

244244
return z1 + yratio * (z_values[xi][_MIN(y1_i, (GRID_MAX_POINTS_Y) - 2) + 1] - z1); // Don't allow y1_i+1 to be past the end of the array
@@ -264,16 +264,17 @@ class unified_bed_leveling {
264264
return UBL_Z_RAISE_WHEN_OFF_MESH;
265265
#endif
266266

267-
const uint8_t mx = _MIN(cx, (GRID_MAX_POINTS_X) - 2) + 1, my = _MIN(cy, (GRID_MAX_POINTS_Y) - 2) + 1;
268-
const float z1 = calc_z0(rx0, mesh_index_to_xpos(cx), z_values[cx][cy], mesh_index_to_xpos(cx + 1), z_values[mx][cy]);
269-
const float z2 = calc_z0(rx0, mesh_index_to_xpos(cx), z_values[cx][my], mesh_index_to_xpos(cx + 1), z_values[mx][my]);
270-
float z0 = calc_z0(ry0, mesh_index_to_ypos(cy), z1, mesh_index_to_ypos(cy + 1), z2);
267+
const uint8_t mx = _MIN(cx, (GRID_MAX_POINTS_X) - 2) + 1, my = _MIN(cy, (GRID_MAX_POINTS_Y) - 2) + 1,
268+
x0 = get_mesh_x(cx), x1 = get_mesh_x(cx + 1);
269+
const float z1 = calc_z0(rx0, x0, z_values[cx][cy], x1, z_values[mx][cy]),
270+
z2 = calc_z0(rx0, x0, z_values[cx][my], x1, z_values[mx][my]);
271+
float z0 = calc_z0(ry0, get_mesh_y(cy), z1, get_mesh_y(cy + 1), z2);
271272

272-
if (isnan(z0)) { // if part of the Mesh is undefined, it will show up as NAN
273-
z0 = 0.0; // in ubl.z_values[][] and propagate through the
274-
// calculations. If our correction is NAN, we throw it out
275-
// because part of the Mesh is undefined and we don't have the
276-
// information we need to complete the height correction.
273+
if (isnan(z0)) { // If part of the Mesh is undefined, it will show up as NAN
274+
z0 = 0.0; // in z_values[][] and propagate through the calculations.
275+
// If our correction is NAN, we throw it out because part of
276+
// the Mesh is undefined and we don't have the information
277+
// needed to complete the height correction.
277278

278279
if (DEBUGGING(MESH_ADJUST)) DEBUG_ECHOLNPGM("??? Yikes! NAN in ");
279280
}
@@ -287,14 +288,16 @@ class unified_bed_leveling {
287288
}
288289
static float get_z_correction(const xy_pos_t &pos) { return get_z_correction(pos.x, pos.y); }
289290

291+
static constexpr float get_z_offset() { return 0.0f; }
292+
290293
#if JYENHANCED
291-
static float mesh_index_to_xpos(const uint8_t i);
292-
static float mesh_index_to_ypos(const uint8_t i);
294+
static float get_mesh_x(const uint8_t i);
295+
static float get_mesh_y(const uint8_t i);
293296
#else
294-
static float mesh_index_to_xpos(const uint8_t i) {
297+
static float get_mesh_x(const uint8_t i) {
295298
return i < (GRID_MAX_POINTS_X) ? pgm_read_float(&_mesh_index_to_xpos[i]) : MESH_MIN_X + i * (MESH_X_DIST);
296299
}
297-
static float mesh_index_to_ypos(const uint8_t i) {
300+
static float get_mesh_y(const uint8_t i) {
298301
return i < (GRID_MAX_POINTS_Y) ? pgm_read_float(&_mesh_index_to_ypos[i]) : MESH_MIN_Y + i * (MESH_Y_DIST);
299302
}
300303
#endif
@@ -312,11 +315,7 @@ class unified_bed_leveling {
312315

313316
}; // class unified_bed_leveling
314317

315-
extern unified_bed_leveling ubl;
316-
317-
#define _GET_MESH_X(I) ubl.mesh_index_to_xpos(I)
318-
#define _GET_MESH_Y(J) ubl.mesh_index_to_ypos(J)
319-
#define Z_VALUES_ARR ubl.z_values
318+
extern unified_bed_leveling bedlevel;
320319

321320
// Prevent debugging propagating to other files
322321
#include "../../../core/debug_out.h"

0 commit comments

Comments
 (0)