From c8db766dceb85b2b543720896e30341b555630f3 Mon Sep 17 00:00:00 2001 From: lukasradek Date: Sat, 15 Jul 2023 21:16:47 +0200 Subject: [PATCH] Basic implementation Works for Bilinear G29 LRFB syntax with square beds and without BILINEAR_SUBDIVISION. --- Marlin/Configuration.h | 1 + Marlin/src/feature/bedlevel/abl/bbl.cpp | 11 +++--- Marlin/src/feature/bedlevel/abl/bbl.h | 7 +++- Marlin/src/gcode/bedlevel/abl/G29.cpp | 45 ++++++++++++++++++++----- 4 files changed, 50 insertions(+), 14 deletions(-) diff --git a/Marlin/Configuration.h b/Marlin/Configuration.h index c59bbba32a70..c46048b3f162 100644 --- a/Marlin/Configuration.h +++ b/Marlin/Configuration.h @@ -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 diff --git a/Marlin/src/feature/bedlevel/abl/bbl.cpp b/Marlin/src/feature/bedlevel/abl/bbl.cpp index 14c4bd24bcf0..57d7bcd4a484 100644 --- a/Marlin/src/feature/bedlevel/abl/bbl.cpp +++ b/Marlin/src/feature/bedlevel/abl/bbl.cpp @@ -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; @@ -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; } /** @@ -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) { @@ -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 diff --git a/Marlin/src/feature/bedlevel/abl/bbl.h b/Marlin/src/feature/bedlevel/abl/bbl.h index ca2e96593fde..d3f5d3b4502c 100644 --- a/Marlin/src/feature/bedlevel/abl/bbl.h +++ b/Marlin/src/feature/bedlevel/abl/bbl.h @@ -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; @@ -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(); diff --git a/Marlin/src/gcode/bedlevel/abl/G29.cpp b/Marlin/src/gcode/bedlevel/abl/G29.cpp index 5ecb6af66c58..4ce2538cec71 100644 --- a/Marlin/src/gcode/bedlevel/abl/G29.cpp +++ b/Marlin/src/gcode/bedlevel/abl/G29.cpp @@ -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 @@ -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) @@ -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 @@ -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'); @@ -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. */ @@ -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 @@ -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) { @@ -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();