Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use probe to level corners #20241

Merged
merged 14 commits into from
Nov 26, 2020
4 changes: 4 additions & 0 deletions Marlin/Configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -1392,6 +1392,10 @@
#define LEVEL_CORNERS_HEIGHT 0.0 // (mm) Z height of nozzle at leveling points
#define LEVEL_CORNERS_Z_HOP 4.0 // (mm) Z height of nozzle between leveling points
//#define LEVEL_CENTER_TOO // Move to the center after the last corner
//#define LEVEL_CORNERS_USE_PROBE
#if ENABLED(LEVEL_CORNERS_USE_PROBE)
#define LEVEL_CORNERS_PROBE_TOLERANCE 0.1
#endif
#endif

/**
Expand Down
4 changes: 3 additions & 1 deletion Marlin/src/inc/SanityCheck.h
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,8 @@
#error "LEVEL_CORNERS_INSET is now LEVEL_CORNERS_INSET_LFRB."
#elif ENABLED(LEVEL_BED_CORNERS) && !defined(LEVEL_CORNERS_INSET_LFRB)
#error "LEVEL_BED_CORNERS requires LEVEL_CORNERS_INSET_LFRB values."
#elif BOTH(LEVEL_CORNERS_USE_PROBE, SENSORLESS_PROBING)
#error "LEVEL_CORNERS_USE_PROBE is incompatible with SENSORLESS_PROBING."
#elif defined(BEZIER_JERK_CONTROL)
#error "BEZIER_JERK_CONTROL is now S_CURVE_ACCELERATION."
#elif HAS_JUNCTION_DEVIATION && defined(JUNCTION_DEVIATION_FACTOR)
Expand Down Expand Up @@ -1594,7 +1596,7 @@ static_assert(hbm[Z_AXIS] >= 0, "HOMING_BUMP_MM.Z must be greater than or equal
* Allen Key
* Deploying the Allen Key probe uses big moves in z direction. Too dangerous for an unhomed z-axis.
*/
#if ENABLED(Z_PROBE_ALLEN_KEY) && (Z_HOME_DIR < 0) && ENABLED(Z_MIN_PROBE_USES_Z_MIN_ENDSTOP_PIN)
#if BOTH(Z_PROBE_ALLEN_KEY, Z_MIN_PROBE_USES_Z_MIN_ENDSTOP_PIN) && (Z_HOME_DIR < 0)
#error "You can't home to a Z min endstop with a Z_PROBE_ALLEN_KEY."
#endif

Expand Down
3 changes: 3 additions & 0 deletions Marlin/src/lcd/language/language_en.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ namespace Language_en {
PROGMEM Language_Str MSG_BED_LEVELING = _UxGT("Bed Leveling");
PROGMEM Language_Str MSG_LEVEL_BED = _UxGT("Level Bed");
PROGMEM Language_Str MSG_LEVEL_CORNERS = _UxGT("Level Corners");
PROGMEM Language_Str MSG_LEVEL_CORNERS_RAISE = _UxGT("Raise Bed Until Probe Triggered");
PROGMEM Language_Str MSG_LEVEL_CORNERS_IN_RANGE = _UxGT("All Corners Within Tolerance. Level Bed");
PROGMEM Language_Str MSG_NEXT_CORNER = _UxGT("Next Corner");
PROGMEM Language_Str MSG_MESH_EDITOR = _UxGT("Mesh Editor");
PROGMEM Language_Str MSG_EDIT_MESH = _UxGT("Edit Mesh");
Expand Down Expand Up @@ -376,6 +378,7 @@ namespace Language_en {
PROGMEM Language_Str MSG_BUTTON_DONE = _UxGT("Done");
PROGMEM Language_Str MSG_BUTTON_BACK = _UxGT("Back");
PROGMEM Language_Str MSG_BUTTON_PROCEED = _UxGT("Proceed");
PROGMEM Language_Str MSG_BUTTON_SKIP = _UxGT("Skip");
PROGMEM Language_Str MSG_PAUSING = _UxGT("Pausing...");
PROGMEM Language_Str MSG_PAUSE_PRINT = _UxGT("Pause Print");
PROGMEM Language_Str MSG_RESUME_PRINT = _UxGT("Resume Print");
Expand Down
190 changes: 158 additions & 32 deletions Marlin/src/lcd/menu/menu_bed_corners.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,53 +44,179 @@
#define LEVEL_CORNERS_HEIGHT 0.0
#endif

#if ENABLED(LEVEL_CORNERS_USE_PROBE)
#include "../../module/probe.h"
#include "../../module/endstops.h"
#if ENABLED(BLTOUCH)
#include "../../feature/bltouch.h"
#endif
#ifndef LEVEL_CORNERS_PROBE_TOLERANCE
#define LEVEL_CORNERS_PROBE_TOLERANCE 0.1
#endif
static float last_z = LEVEL_CORNERS_HEIGHT;
bool wait_for_probe = false;
bool probe_triggered = false;
bool corner_probing_done = false;
int count_points_in_tolerance = 0;
#ifndef TEST_PROBE_PIN
#if HAS_CUSTOM_PROBE_PIN
#define TEST_PROBE_PIN (READ(Z_MIN_PROBE_PIN) != Z_MIN_PROBE_ENDSTOP_INVERTING)
#else
#define TEST_PROBE_PIN (READ(Z_MIN_PIN) != Z_MIN_ENDSTOP_INVERTING)
#endif
#endif
#endif

static_assert(LEVEL_CORNERS_Z_HOP >= 0, "LEVEL_CORNERS_Z_HOP must be >= 0. Please update your configuration.");

extern const char G28_STR[];

#if HAS_LEVELING
static bool leveling_was_active = false;
#endif

static int8_t bed_corner;

/**
* Level corners, starting in the front-left corner.
*/
static int8_t bed_corner;
static inline void _lcd_goto_next_corner() {
constexpr float lfrb[4] = LEVEL_CORNERS_INSET_LFRB;
constexpr xy_pos_t lf { (X_MIN_BED) + lfrb[0], (Y_MIN_BED) + lfrb[1] },
rb { (X_MAX_BED) - lfrb[2], (Y_MAX_BED) - lfrb[3] };
line_to_z(LEVEL_CORNERS_Z_HOP);
switch (bed_corner) {
case 0: current_position = lf; break; // copy xy
case 1: current_position.x = rb.x; break;
case 2: current_position.y = rb.y; break;
case 3: current_position.x = lf.x; break;
#if ENABLED(LEVEL_CENTER_TOO)
case 4: current_position.set(X_CENTER, Y_CENTER); break;
#if ENABLED(LEVEL_CORNERS_USE_PROBE)

static inline void _lcd_level_bed_corners_probing() {
ui.goto_screen([]{ MenuItem_static::draw((LCD_HEIGHT - 1) / 2, GET_TEXT(MSG_PROBING_MESH)); });

float lfrb[4] = LEVEL_CORNERS_INSET_LFRB;
xy_pos_t lf { (X_MIN_BED) + lfrb[0] - probe.offset_xy.x , (Y_MIN_BED) + lfrb[1] - probe.offset_xy.y },
rb { (X_MAX_BED) - lfrb[2] - probe.offset_xy.x , (Y_MAX_BED) - lfrb[3] - probe.offset_xy.y };

do_blocking_move_to_z(LEVEL_CORNERS_Z_HOP - probe.offset.z);

switch (bed_corner) {
case 0: current_position = lf; break; // copy xy
case 1: current_position.x = rb.x; break;
case 2: current_position.y = rb.y; break;
case 3: current_position.x = lf.x; break;
#if ENABLED(LEVEL_CENTER_TOO)
case 4: current_position.set(X_CENTER - probe.offset_xy.x, Y_CENTER - probe.offset_xy.y); count_points_in_tolerance--; break;
#endif
}

do_blocking_move_to_xy(current_position);

#if ENABLED(BLTOUCH) && DISABLED(BLTOUCH_HS_MODE)
bltouch.deploy(); // DEPLOY in LOW SPEED MODE on every probe action
#endif
TERN_(QUIET_PROBING, probe.set_probing_paused(true));

do_blocking_move_to_z(last_z - (LEVEL_CORNERS_PROBE_TOLERANCE), manual_feedrate_mm_s.z);// Move down until the probe is triggered
probe_triggered = TEST(endstops.trigger_state(), TERN(Z_MIN_PROBE_USES_Z_MIN_ENDSTOP_PIN, Z_MIN, Z_MIN_PROBE));// Check to see if the probe was triggered

if (!probe_triggered) {
ui.goto_screen([]{
MenuItem_confirm::select_screen(
GET_TEXT(MSG_BUTTON_DONE), GET_TEXT(MSG_BUTTON_SKIP)
, []{ corner_probing_done = true;
wait_for_probe = false;
TERN_(HAS_LEVELING, set_bed_leveling_enabled(leveling_was_active));
ui.goto_previous_screen_no_defer();
}
, []{ wait_for_probe = false; }
, GET_TEXT(MSG_LEVEL_CORNERS_RAISE)
, (const char*)nullptr, PSTR("")
);
});
ui.set_selection(true);

wait_for_probe = true;
while (wait_for_probe && !probe_triggered) {
probe_triggered = TEST_PROBE_PIN;
idle();
}
wait_for_probe = false;
}

TERN_(QUIET_PROBING, probe.set_probing_paused(false));

#if ENABLED(BLTOUCH) && DISABLED(BLTOUCH_HS_MODE)
bltouch.stow();
#endif

if (probe_triggered) {
endstops.hit_on_purpose();
if (!WITHIN(current_position.z, current_position.z - (LEVEL_CORNERS_PROBE_TOLERANCE), current_position.z + (LEVEL_CORNERS_PROBE_TOLERANCE))) {
thinkyhead marked this conversation as resolved.
Show resolved Hide resolved
last_z = current_position.z;
count_points_in_tolerance = 1;
}
count_points_in_tolerance++;
}

if (!corner_probing_done) {
if (++bed_corner > 3) bed_corner = 0;
if (count_points_in_tolerance < 4)
_lcd_level_bed_corners_probing();
else {
ui.goto_screen([]{
MenuItem_confirm::confirm_screen(
[]{ ui.goto_previous_screen_no_defer();
queue.inject_P(TERN(HAS_LEVELING, PSTR("G28\nG29"), G28_STR);
}
, []{ ui.goto_previous_screen_no_defer(); }
, GET_TEXT(MSG_LEVEL_CORNERS_IN_RANGE)
, (const char*)nullptr, PSTR("?")
);
});
ui.set_selection(true);
}
}
}

#else

static inline void _lcd_goto_next_corner() {
constexpr float lfrb[4] = LEVEL_CORNERS_INSET_LFRB;
constexpr xy_pos_t lf { (X_MIN_BED) + lfrb[0], (Y_MIN_BED) + lfrb[1] },
rb { (X_MAX_BED) - lfrb[2], (Y_MAX_BED) - lfrb[3] };
line_to_z(LEVEL_CORNERS_Z_HOP);
switch (bed_corner) {
case 0: current_position = lf; break; // copy xy
case 1: current_position.x = rb.x; break;
case 2: current_position.y = rb.y; break;
case 3: current_position.x = lf.x; break;
#if ENABLED(LEVEL_CENTER_TOO)
case 4: current_position.set(X_CENTER, Y_CENTER); break;
#endif
}
line_to_current_position(manual_feedrate_mm_s.x);
line_to_z(LEVEL_CORNERS_HEIGHT);
if (++bed_corner > 3 + ENABLED(LEVEL_CENTER_TOO)) bed_corner = 0;
}
line_to_current_position(manual_feedrate_mm_s.x);
line_to_z(LEVEL_CORNERS_HEIGHT);
if (++bed_corner > 3 + ENABLED(LEVEL_CENTER_TOO)) bed_corner = 0;
}

#endif

static inline void _lcd_level_bed_corners_homing() {
_lcd_draw_homing();
if (all_axes_homed()) {
bed_corner = 0;
ui.goto_screen([]{
MenuItem_confirm::select_screen(
GET_TEXT(MSG_BUTTON_NEXT), GET_TEXT(MSG_BUTTON_DONE)
, _lcd_goto_next_corner
, []{
TERN_(HAS_LEVELING, set_bed_leveling_enabled(leveling_was_active));
ui.goto_previous_screen_no_defer();
}
, GET_TEXT(TERN(LEVEL_CENTER_TOO, MSG_LEVEL_BED_NEXT_POINT, MSG_NEXT_CORNER))
, (const char*)nullptr, PSTR("?")
);
});
ui.set_selection(true);
_lcd_goto_next_corner();
#if ENABLED(LEVEL_CORNERS_USE_PROBE)
TERN_(LEVEL_CENTER_TOO, bed_corner = 4);
endstops.enable_z_probe(true);
ui.goto_screen(_lcd_level_bed_corners_probing);
#else
bed_corner = 0;
ui.goto_screen([]{
MenuItem_confirm::select_screen(
GET_TEXT(MSG_BUTTON_NEXT), GET_TEXT(MSG_BUTTON_DONE)
, _lcd_goto_next_corner
, []{
TERN_(HAS_LEVELING, set_bed_leveling_enabled(leveling_was_active));
ui.goto_previous_screen_no_defer();
}
, GET_TEXT(TERN(LEVEL_CENTER_TOO, MSG_LEVEL_BED_NEXT_POINT, MSG_NEXT_CORNER))
, (const char*)nullptr, PSTR("?")
);
});
ui.set_selection(true);
_lcd_goto_next_corner();
#endif
}
}

Expand Down