Skip to content

Commit

Permalink
Basic implementation
Browse files Browse the repository at this point in the history
Works for Bilinear G29 LRFB syntax with square beds and without BILINEAR_SUBDIVISION.
  • Loading branch information
lukasradek committed Jul 15, 2023
1 parent 28f69a0 commit c8db766
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 14 deletions.
1 change: 1 addition & 0 deletions Marlin/Configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -2056,6 +2056,7 @@
// Set the number of grid points per dimension.
#define GRID_MAX_POINTS_X 3
#define GRID_MAX_POINTS_Y GRID_MAX_POINTS_X
#define GRID_MIN_SPACING 25

// Probe along the Y axis, advancing X after each column
//#define PROBE_Y_FIRST
Expand Down
11 changes: 7 additions & 4 deletions Marlin/src/feature/bedlevel/abl/bbl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

LevelingBilinear bedlevel;

xy_uint8_t LevelingBilinear::grid_points;
xy_pos_t LevelingBilinear::grid_spacing,
LevelingBilinear::grid_start;
xy_float_t LevelingBilinear::grid_factor;
Expand Down Expand Up @@ -100,16 +101,18 @@ void LevelingBilinear::extrapolate_one_point(const uint8_t x, const uint8_t y, c
void LevelingBilinear::reset() {
grid_start.reset();
grid_spacing.reset();
grid_points.set(GRID_MAX_POINTS_X, GRID_MAX_POINTS_Y);
GRID_LOOP(x, y) {
z_values[x][y] = NAN;
TERN_(EXTENSIBLE_UI, ExtUI::onMeshUpdate(x, y, 0));
}
}

void LevelingBilinear::set_grid(const xy_pos_t& _grid_spacing, const xy_pos_t& _grid_start) {
void LevelingBilinear::set_grid(const xy_pos_t& _grid_spacing, const xy_pos_t& _grid_start, const xy_uint8_t& _grid_points) {
grid_spacing = _grid_spacing;
grid_start = _grid_start;
grid_factor = grid_spacing.reciprocal();
grid_points = _grid_points;
}

/**
Expand Down Expand Up @@ -156,7 +159,7 @@ void LevelingBilinear::extrapolate_unprobed_bed_level() {
void LevelingBilinear::print_leveling_grid(const bed_mesh_t* _z_values/*=nullptr*/) {
// print internal grid(s) or just the one passed as a parameter
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]);
print_2d_array(grid_points.x, grid_points.y, 3, _z_values ? *_z_values[0] : z_values[0]);

#if ENABLED(ABL_BILINEAR_SUBDIVISION)
if (!_z_values) {
Expand Down Expand Up @@ -272,8 +275,8 @@ void LevelingBilinear::refresh_bed_level() {
#else
#define ABL_BG_SPACING(A) grid_spacing.A
#define ABL_BG_FACTOR(A) grid_factor.A
#define ABL_BG_POINTS_X GRID_MAX_POINTS_X
#define ABL_BG_POINTS_Y GRID_MAX_POINTS_Y
#define ABL_BG_POINTS_X grid_points.x
#define ABL_BG_POINTS_Y grid_points.y
#define ABL_BG_GRID(X,Y) z_values[X][Y]
#endif

Expand Down
7 changes: 6 additions & 1 deletion Marlin/src/feature/bedlevel/abl/bbl.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,15 @@

#include "../../../inc/MarlinConfigPre.h"

#ifndef GRID_MIN_SPACING
#define GRID_MIN_SPACING 25
#endif

class LevelingBilinear {
public:
static bed_mesh_t z_values;
static xy_pos_t grid_spacing, grid_start;
static xy_uint8_t grid_points;

private:
static xy_float_t grid_factor;
Expand All @@ -51,7 +56,7 @@ class LevelingBilinear {

public:
static void reset();
static void set_grid(const xy_pos_t& _grid_spacing, const xy_pos_t& _grid_start);
static void set_grid(const xy_pos_t& _grid_spacing, const xy_pos_t& _grid_start, const xy_uint8_t& _grid_points = {GRID_MAX_POINTS_X , GRID_MAX_POINTS_Y});
static void extrapolate_unprobed_bed_level();
static void print_leveling_grid(const bed_mesh_t *_z_values=nullptr);
static void refresh_bed_level();
Expand Down
45 changes: 36 additions & 9 deletions Marlin/src/gcode/bedlevel/abl/G29.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
#include "../../../module/tool_change.h"
#endif

#define DEBUG_OUT ENABLED(DEBUG_LEVELING_FEATURE)
#define DEBUG_OUT 1
#include "../../../core/debug_out.h"

#if ABL_USES_GRID
Expand Down Expand Up @@ -125,7 +125,7 @@ class G29_State {
bool topography_map;
xy_uint8_t grid_points;
#else // Bilinear
static constexpr xy_uint8_t grid_points = { GRID_MAX_POINTS_X, GRID_MAX_POINTS_Y };
xy_uint8_t grid_points;
#endif

#if ENABLED(AUTO_BED_LEVELING_BILINEAR)
Expand All @@ -143,7 +143,7 @@ class G29_State {
};

#if ABL_USES_GRID && ANY(AUTO_BED_LEVELING_3POINT, AUTO_BED_LEVELING_BILINEAR)
constexpr xy_uint8_t G29_State::grid_points;
//constexpr xy_uint8_t G29_State::grid_points;
constexpr grid_count_t G29_State::abl_points;
#endif

Expand Down Expand Up @@ -230,9 +230,19 @@ G29_TYPE GcodeSuite::G29() {
// Leveling state is persistent when done manually with multiple G29 commands
TERN_(PROBE_MANUALLY, static) G29_State abl;

// Safely define abl.grid_points as soon as possible in the similar manner it was in class declaration (someone smarter will fix this later ;-) )
#if ABL_USES_GRID
#if ENABLED(AUTO_BED_LEVELING_LINEAR)
{}
#else // Bilinear
abl.grid_points.x = GRID_MAX_POINTS_X;
abl.grid_points.y = GRID_MAX_POINTS_Y;
#endif
#endif

// Keep powered steppers from timing out
reset_stepper_timeout();

// Q = Query leveling and G29 state
const bool seenQ = ANY(DEBUG_LEVELING_FEATURE, PROBE_MANUALLY) && parser.seen_test('Q');

Expand Down Expand Up @@ -275,6 +285,7 @@ G29_TYPE GcodeSuite::G29() {
// Set and report "probing" state to host
TERN_(FULL_REPORT_TO_HOST_FEATURE, set_and_report_grblstate(M_PROBE, false));


/**
* On the initial G29 fetch command parameters.
*/
Expand Down Expand Up @@ -412,8 +423,24 @@ G29_TYPE GcodeSuite::G29() {
}

// Probe at the points of a lattice grid
abl.gridSpacing.set((abl.probe_position_rb.x - abl.probe_position_lf.x) / (abl.grid_points.x - 1),
(abl.probe_position_rb.y - abl.probe_position_lf.y) / (abl.grid_points.y - 1));
float dX = (abl.probe_position_rb.x - abl.probe_position_lf.x),
dY = (abl.probe_position_rb.y - abl.probe_position_lf.y);

float
spacing_x = ((abl.probe_position_rb.x - abl.probe_position_lf.x) / (abl.grid_points.x - 1)),
spacing_y = ((abl.probe_position_rb.y - abl.probe_position_lf.y) / (abl.grid_points.y - 1));


if (spacing_x < GRID_MIN_SPACING) {
abl.grid_points.x = (CEIL(dX / GRID_MIN_SPACING)) + 1;
spacing_x = dX / (abl.grid_points.x - 1);
}
if (spacing_y < GRID_MIN_SPACING) {
abl.grid_points.y = (CEIL(dY / GRID_MIN_SPACING)) + 1;
spacing_y = dY / (abl.grid_points.y - 1);
}

abl.gridSpacing.set(spacing_x, spacing_y);

#endif // ABL_USES_GRID

Expand Down Expand Up @@ -693,8 +720,8 @@ G29_TYPE GcodeSuite::G29() {
// Avoid probing outside the round or hexagonal area
if (TERN0(IS_KINEMATIC, !probe.can_reach(abl.probePos))) continue;

if (abl.verbose_level) SERIAL_ECHOLNPGM("Probing mesh point ", pt_index, "/", abl.abl_points, ".");
TERN_(HAS_STATUS_MESSAGE, ui.status_printf(0, F(S_FMT " %i/%i"), GET_TEXT(MSG_PROBING_POINT), int(pt_index), int(abl.abl_points)));
if (abl.verbose_level) SERIAL_ECHOLNPGM("Probing mesh point ", pt_index, "/", abl.grid_points.x * abl.grid_points.y, ".");
TERN_(HAS_STATUS_MESSAGE, ui.status_printf(0, F(S_FMT " %i/%i"), GET_TEXT(MSG_PROBING_POINT), int(pt_index), int(abl.grid_points.x * abl.grid_points.y)));

#if ENABLED(BD_SENSOR_PROBE_NO_STOP)
if (PR_INNER_VAR == inStart) {
Expand Down Expand Up @@ -849,7 +876,7 @@ G29_TYPE GcodeSuite::G29() {
if (abl.dryrun)
bedlevel.print_leveling_grid(&abl.z_values);
else {
bedlevel.set_grid(abl.gridSpacing, abl.probe_position_lf);
bedlevel.set_grid(abl.gridSpacing, abl.probe_position_lf, abl.grid_points);
COPY(bedlevel.z_values, abl.z_values);
TERN_(IS_KINEMATIC, bedlevel.extrapolate_unprobed_bed_level());
bedlevel.refresh_bed_level();
Expand Down

0 comments on commit c8db766

Please sign in to comment.