2017-09-08 15:35:25 -05:00
|
|
|
/**
|
|
|
|
* Marlin 3D Printer Firmware
|
2020-02-03 08:00:57 -06:00
|
|
|
* Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
|
2017-09-08 15:35:25 -05:00
|
|
|
*
|
|
|
|
* Based on Sprinter and grbl.
|
2019-06-27 23:57:50 -05:00
|
|
|
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
|
2017-09-08 15:35:25 -05:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "../../inc/MarlinConfig.h"
|
|
|
|
|
|
|
|
#if HAS_LEVELING
|
|
|
|
|
|
|
|
#include "bedlevel.h"
|
2018-09-16 21:24:15 -05:00
|
|
|
#include "../../module/planner.h"
|
2017-09-08 15:35:25 -05:00
|
|
|
|
2019-03-16 23:43:06 -05:00
|
|
|
#if EITHER(MESH_BED_LEVELING, PROBE_MANUALLY)
|
2017-11-08 22:13:19 -06:00
|
|
|
#include "../../module/motion.h"
|
2017-09-08 15:35:25 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if ENABLED(PROBE_MANUALLY)
|
|
|
|
bool g29_in_progress = false;
|
2017-11-27 02:12:29 -06:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if ENABLED(LCD_BED_LEVELING)
|
|
|
|
#include "../../lcd/ultralcd.h"
|
2017-09-08 15:35:25 -05:00
|
|
|
#endif
|
|
|
|
|
2019-03-14 02:25:42 -05:00
|
|
|
#define DEBUG_OUT ENABLED(DEBUG_LEVELING_FEATURE)
|
|
|
|
#include "../../core/debug_out.h"
|
|
|
|
|
2019-04-26 02:32:01 -05:00
|
|
|
#if ENABLED(EXTENSIBLE_UI)
|
|
|
|
#include "../../lcd/extensible_ui/ui_api.h"
|
|
|
|
#endif
|
|
|
|
|
2017-09-08 15:35:25 -05:00
|
|
|
bool leveling_is_valid() {
|
|
|
|
return
|
|
|
|
#if ENABLED(MESH_BED_LEVELING)
|
2018-01-06 23:47:29 -06:00
|
|
|
mbl.has_mesh()
|
2017-09-08 15:35:25 -05:00
|
|
|
#elif ENABLED(AUTO_BED_LEVELING_BILINEAR)
|
2019-09-29 04:25:39 -05:00
|
|
|
!!bilinear_grid_spacing.x
|
2017-09-08 15:35:25 -05:00
|
|
|
#elif ENABLED(AUTO_BED_LEVELING_UBL)
|
2018-05-14 22:36:03 -05:00
|
|
|
ubl.mesh_is_valid()
|
2017-09-08 15:35:25 -05:00
|
|
|
#else // 3POINT, LINEAR
|
|
|
|
true
|
|
|
|
#endif
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Turn bed leveling on or off, fixing the current
|
|
|
|
* position as-needed.
|
|
|
|
*
|
|
|
|
* Disable: Current position = physical position
|
|
|
|
* Enable: Current position = "unleveled" physical position
|
|
|
|
*/
|
|
|
|
void set_bed_leveling_enabled(const bool enable/*=true*/) {
|
|
|
|
|
|
|
|
#if ENABLED(AUTO_BED_LEVELING_BILINEAR)
|
|
|
|
const bool can_change = (!enable || leveling_is_valid());
|
|
|
|
#else
|
|
|
|
constexpr bool can_change = true;
|
|
|
|
#endif
|
|
|
|
|
2017-10-13 17:21:25 -05:00
|
|
|
if (can_change && enable != planner.leveling_active) {
|
2017-09-08 15:35:25 -05:00
|
|
|
|
2018-08-21 09:06:10 -05:00
|
|
|
planner.synchronize();
|
|
|
|
|
2018-09-16 21:24:15 -05:00
|
|
|
#if ENABLED(AUTO_BED_LEVELING_BILINEAR)
|
|
|
|
// Force bilinear_z_offset to re-calculate next time
|
2019-09-29 04:25:39 -05:00
|
|
|
const xyz_pos_t reset { -9999.999, -9999.999, 0 };
|
2018-09-16 21:24:15 -05:00
|
|
|
(void)bilinear_z_offset(reset);
|
|
|
|
#endif
|
2017-09-08 15:35:25 -05:00
|
|
|
|
2018-09-16 21:24:15 -05:00
|
|
|
if (planner.leveling_active) { // leveling from on to off
|
2020-01-23 13:45:13 -06:00
|
|
|
if (DEBUGGING(LEVELING)) DEBUG_POS("Leveling ON", current_position);
|
2018-09-16 21:24:15 -05:00
|
|
|
// change unleveled current_position to physical current_position without moving steppers.
|
2019-09-29 04:25:39 -05:00
|
|
|
planner.apply_leveling(current_position);
|
2018-09-16 21:24:15 -05:00
|
|
|
planner.leveling_active = false; // disable only AFTER calling apply_leveling
|
2020-01-23 13:45:13 -06:00
|
|
|
if (DEBUGGING(LEVELING)) DEBUG_POS("...Now OFF", current_position);
|
2018-09-16 21:24:15 -05:00
|
|
|
}
|
|
|
|
else { // leveling from off to on
|
2020-01-23 13:45:13 -06:00
|
|
|
if (DEBUGGING(LEVELING)) DEBUG_POS("Leveling OFF", current_position);
|
2018-09-16 21:24:15 -05:00
|
|
|
planner.leveling_active = true; // enable BEFORE calling unapply_leveling, otherwise ignored
|
|
|
|
// change physical current_position to unleveled current_position without moving steppers.
|
|
|
|
planner.unapply_leveling(current_position);
|
2020-01-23 13:45:13 -06:00
|
|
|
if (DEBUGGING(LEVELING)) DEBUG_POS("...Now ON", current_position);
|
2018-09-16 21:24:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
sync_plan_position();
|
2017-09-08 15:35:25 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-17 03:41:04 -05:00
|
|
|
TemporaryBedLevelingState::TemporaryBedLevelingState(const bool enable) : saved(planner.leveling_active) {
|
|
|
|
set_bed_leveling_enabled(enable);
|
|
|
|
}
|
|
|
|
|
2017-09-08 15:35:25 -05:00
|
|
|
#if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
|
|
|
|
|
2017-12-10 21:17:07 -06:00
|
|
|
void set_z_fade_height(const float zfh, const bool do_report/*=true*/) {
|
|
|
|
|
2018-03-29 18:16:20 -05:00
|
|
|
if (planner.z_fade_height == zfh) return;
|
2017-09-08 15:35:25 -05:00
|
|
|
|
2018-03-29 18:16:20 -05:00
|
|
|
const bool leveling_was_active = planner.leveling_active;
|
|
|
|
set_bed_leveling_enabled(false);
|
2017-09-08 15:35:25 -05:00
|
|
|
|
2017-10-13 17:21:25 -05:00
|
|
|
planner.set_z_fade_height(zfh);
|
2017-09-08 15:35:25 -05:00
|
|
|
|
2018-03-29 18:16:20 -05:00
|
|
|
if (leveling_was_active) {
|
2019-09-29 04:25:39 -05:00
|
|
|
const xyz_pos_t oldpos = current_position;
|
2018-03-29 18:16:20 -05:00
|
|
|
set_bed_leveling_enabled(true);
|
2019-09-29 04:25:39 -05:00
|
|
|
if (do_report && oldpos != current_position)
|
2017-12-10 21:17:07 -06:00
|
|
|
report_current_position();
|
2017-10-13 10:39:11 -05:00
|
|
|
}
|
2017-09-08 15:35:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // ENABLE_LEVELING_FADE_HEIGHT
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reset calibration results to zero.
|
|
|
|
*/
|
|
|
|
void reset_bed_level() {
|
2019-03-14 02:25:42 -05:00
|
|
|
if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("reset_bed_level");
|
2019-09-26 01:15:35 -05:00
|
|
|
#if ENABLED(AUTO_BED_LEVELING_UBL)
|
2017-12-25 03:32:31 -06:00
|
|
|
ubl.reset();
|
2019-09-26 01:15:35 -05:00
|
|
|
#else
|
|
|
|
set_bed_leveling_enabled(false);
|
|
|
|
#if ENABLED(MESH_BED_LEVELING)
|
|
|
|
mbl.reset();
|
|
|
|
#elif ENABLED(AUTO_BED_LEVELING_BILINEAR)
|
2019-09-29 04:25:39 -05:00
|
|
|
bilinear_start.reset();
|
|
|
|
bilinear_grid_spacing.reset();
|
2019-09-26 01:15:35 -05:00
|
|
|
for (uint8_t x = 0; x < GRID_MAX_POINTS_X; x++)
|
|
|
|
for (uint8_t y = 0; y < GRID_MAX_POINTS_Y; y++) {
|
|
|
|
z_values[x][y] = NAN;
|
|
|
|
#if ENABLED(EXTENSIBLE_UI)
|
|
|
|
ExtUI::onMeshUpdate(x, y, 0);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#elif ABL_PLANAR
|
|
|
|
planner.bed_level_matrix.set_to_identity();
|
|
|
|
#endif
|
2017-09-08 15:35:25 -05:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-03-16 23:43:06 -05:00
|
|
|
#if EITHER(AUTO_BED_LEVELING_BILINEAR, MESH_BED_LEVELING)
|
2017-09-08 15:35:25 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Enable to produce output in JSON format suitable
|
|
|
|
* for SCAD or JavaScript mesh visualizers.
|
|
|
|
*
|
|
|
|
* Visualize meshes in OpenSCAD using the included script.
|
|
|
|
*
|
|
|
|
* buildroot/shared/scripts/MarlinMesh.scad
|
|
|
|
*/
|
|
|
|
//#define SCAD_MESH_OUTPUT
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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, element_2d_fn fn) {
|
|
|
|
#ifndef SCAD_MESH_OUTPUT
|
|
|
|
for (uint8_t x = 0; x < sx; x++) {
|
2018-11-10 01:32:00 -06:00
|
|
|
serial_spaces(precision + (x < 10 ? 3 : 2));
|
2018-11-29 16:58:58 -06:00
|
|
|
SERIAL_ECHO(int(x));
|
2017-09-08 15:35:25 -05:00
|
|
|
}
|
|
|
|
SERIAL_EOL();
|
|
|
|
#endif
|
|
|
|
#ifdef SCAD_MESH_OUTPUT
|
2018-11-29 16:58:58 -06:00
|
|
|
SERIAL_ECHOLNPGM("measured_z = ["); // open 2D array
|
2017-09-08 15:35:25 -05:00
|
|
|
#endif
|
|
|
|
for (uint8_t y = 0; y < sy; y++) {
|
|
|
|
#ifdef SCAD_MESH_OUTPUT
|
2020-01-23 13:45:13 -06:00
|
|
|
SERIAL_ECHOPGM(" ["); // open sub-array
|
2017-09-08 15:35:25 -05:00
|
|
|
#else
|
2018-11-29 16:58:58 -06:00
|
|
|
if (y < 10) SERIAL_CHAR(' ');
|
|
|
|
SERIAL_ECHO(int(y));
|
2017-09-08 15:35:25 -05:00
|
|
|
#endif
|
|
|
|
for (uint8_t x = 0; x < sx; x++) {
|
2018-11-29 16:58:58 -06:00
|
|
|
SERIAL_CHAR(' ');
|
2017-09-08 15:35:25 -05:00
|
|
|
const float offset = fn(x, y);
|
|
|
|
if (!isnan(offset)) {
|
2018-11-29 16:58:58 -06:00
|
|
|
if (offset >= 0) SERIAL_CHAR('+');
|
|
|
|
SERIAL_ECHO_F(offset, int(precision));
|
2017-09-08 15:35:25 -05:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
#ifdef SCAD_MESH_OUTPUT
|
|
|
|
for (uint8_t i = 3; i < precision + 3; i++)
|
2018-11-29 16:58:58 -06:00
|
|
|
SERIAL_CHAR(' ');
|
|
|
|
SERIAL_ECHOPGM("NAN");
|
2017-09-08 15:35:25 -05:00
|
|
|
#else
|
|
|
|
for (uint8_t i = 0; i < precision + 3; i++)
|
2018-11-29 16:58:58 -06:00
|
|
|
SERIAL_CHAR(i ? '=' : ' ');
|
2017-09-08 15:35:25 -05:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#ifdef SCAD_MESH_OUTPUT
|
2018-11-29 16:58:58 -06:00
|
|
|
if (x < sx - 1) SERIAL_CHAR(',');
|
2017-09-08 15:35:25 -05:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#ifdef SCAD_MESH_OUTPUT
|
2020-01-23 13:45:13 -06:00
|
|
|
SERIAL_CHAR(' ', ']'); // close sub-array
|
2018-11-29 16:58:58 -06:00
|
|
|
if (y < sy - 1) SERIAL_CHAR(',');
|
2017-09-08 15:35:25 -05:00
|
|
|
#endif
|
|
|
|
SERIAL_EOL();
|
|
|
|
}
|
|
|
|
#ifdef SCAD_MESH_OUTPUT
|
2020-01-23 13:45:13 -06:00
|
|
|
SERIAL_ECHOPGM("];"); // close 2D array
|
2017-09-08 15:35:25 -05:00
|
|
|
#endif
|
|
|
|
SERIAL_EOL();
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // AUTO_BED_LEVELING_BILINEAR || MESH_BED_LEVELING
|
|
|
|
|
2019-03-16 23:43:06 -05:00
|
|
|
#if EITHER(MESH_BED_LEVELING, PROBE_MANUALLY)
|
2017-09-08 15:35:25 -05:00
|
|
|
|
2019-09-29 04:25:39 -05:00
|
|
|
void _manual_goto_xy(const xy_pos_t &pos) {
|
2017-11-08 22:13:19 -06:00
|
|
|
|
2018-07-18 21:17:15 -05:00
|
|
|
#ifdef MANUAL_PROBE_START_Z
|
2019-09-29 04:25:39 -05:00
|
|
|
constexpr float startz = _MAX(0, MANUAL_PROBE_START_Z);
|
2018-07-18 21:17:15 -05:00
|
|
|
#if MANUAL_PROBE_HEIGHT > 0
|
2019-10-06 23:58:19 -05:00
|
|
|
do_blocking_move_to_xy_z(pos, MANUAL_PROBE_HEIGHT);
|
2019-09-29 04:25:39 -05:00
|
|
|
do_blocking_move_to_z(startz);
|
2018-07-18 21:17:15 -05:00
|
|
|
#else
|
2019-10-06 23:58:19 -05:00
|
|
|
do_blocking_move_to_xy_z(pos, startz);
|
2018-07-18 21:17:15 -05:00
|
|
|
#endif
|
|
|
|
#elif MANUAL_PROBE_HEIGHT > 0
|
2019-09-29 04:25:39 -05:00
|
|
|
const float prev_z = current_position.z;
|
2019-10-06 23:58:19 -05:00
|
|
|
do_blocking_move_to_xy_z(pos, MANUAL_PROBE_HEIGHT);
|
2017-11-10 20:49:37 -06:00
|
|
|
do_blocking_move_to_z(prev_z);
|
|
|
|
#else
|
2019-09-29 04:25:39 -05:00
|
|
|
do_blocking_move_to_xy(pos);
|
2017-09-08 15:35:25 -05:00
|
|
|
#endif
|
|
|
|
|
2019-09-29 04:25:39 -05:00
|
|
|
current_position = pos;
|
2017-09-08 15:35:25 -05:00
|
|
|
|
2017-11-27 02:12:29 -06:00
|
|
|
#if ENABLED(LCD_BED_LEVELING)
|
2020-01-26 00:02:06 -06:00
|
|
|
ui.wait_for_move = false;
|
2017-09-08 15:35:25 -05:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif // HAS_LEVELING
|