2017-09-06 11:28:31 +00:00
|
|
|
/**
|
|
|
|
* Marlin 3D Printer Firmware
|
2020-02-03 14:00:57 +00:00
|
|
|
* Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
|
2017-09-06 11:28:31 +00:00
|
|
|
*
|
|
|
|
* Based on Sprinter and grbl.
|
2019-06-28 04:57:50 +00:00
|
|
|
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
|
2017-09-06 11:28:31 +00: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
|
2020-07-23 03:20:14 +00:00
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2017-09-06 11:28:31 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2017-09-08 20:35:25 +00:00
|
|
|
#include "../../inc/MarlinConfig.h"
|
|
|
|
|
|
|
|
#if HAS_LEVELING
|
|
|
|
|
|
|
|
#include "../gcode.h"
|
|
|
|
#include "../../feature/bedlevel/bedlevel.h"
|
|
|
|
#include "../../module/planner.h"
|
2019-10-16 20:51:38 +00:00
|
|
|
#include "../../module/probe.h"
|
2017-09-08 20:35:25 +00:00
|
|
|
|
|
|
|
#if ENABLED(EEPROM_SETTINGS)
|
2020-08-08 01:51:40 +00:00
|
|
|
#include "../../module/settings.h"
|
2017-09-08 20:35:25 +00:00
|
|
|
#endif
|
|
|
|
|
2019-04-26 07:32:01 +00:00
|
|
|
#if ENABLED(EXTENSIBLE_UI)
|
2020-03-13 21:29:29 +00:00
|
|
|
#include "../../lcd/extui/ui_api.h"
|
2019-04-26 07:32:01 +00:00
|
|
|
#endif
|
|
|
|
|
2018-04-25 11:43:45 +00:00
|
|
|
//#define M420_C_USE_MEAN
|
|
|
|
|
2017-09-06 11:28:31 +00:00
|
|
|
/**
|
|
|
|
* M420: Enable/Disable Bed Leveling and/or set the Z fade height.
|
|
|
|
*
|
|
|
|
* S[bool] Turns leveling on or off
|
|
|
|
* Z[height] Sets the Z fade height (0 or none to disable)
|
|
|
|
* V[bool] Verbose - Print the leveling grid
|
|
|
|
*
|
|
|
|
* With AUTO_BED_LEVELING_UBL only:
|
|
|
|
*
|
|
|
|
* L[index] Load UBL mesh from index (0 is default)
|
2018-04-17 20:52:56 +00:00
|
|
|
* T[map] 0:Human-readable 1:CSV 2:"LCD" 4:Compact
|
2018-04-25 11:43:45 +00:00
|
|
|
*
|
|
|
|
* With mesh-based leveling only:
|
|
|
|
*
|
|
|
|
* C Center mesh on the mean of the lowest and highest
|
2018-11-05 01:04:16 +00:00
|
|
|
*
|
|
|
|
* With MARLIN_DEV_MODE:
|
|
|
|
* S2 Create a simple random mesh and enable
|
2017-09-06 11:28:31 +00:00
|
|
|
*/
|
2017-09-08 20:35:25 +00:00
|
|
|
void GcodeSuite::M420() {
|
2018-11-05 01:04:16 +00:00
|
|
|
const bool seen_S = parser.seen('S'),
|
|
|
|
to_enable = seen_S ? parser.value_bool() : planner.leveling_active;
|
|
|
|
|
|
|
|
#if ENABLED(MARLIN_DEV_MODE)
|
|
|
|
if (parser.intval('S') == 2) {
|
2020-02-01 10:21:36 +00:00
|
|
|
const float x_min = probe.min_x(), x_max = probe.max_x(),
|
|
|
|
y_min = probe.min_y(), y_max = probe.max_y();
|
2018-11-05 01:04:16 +00:00
|
|
|
#if ENABLED(AUTO_BED_LEVELING_BILINEAR)
|
2019-09-29 09:25:39 +00:00
|
|
|
bilinear_start.set(x_min, y_min);
|
2021-04-02 00:12:00 +00:00
|
|
|
bilinear_grid_spacing.set((x_max - x_min) / (GRID_MAX_CELLS_X),
|
|
|
|
(y_max - y_min) / (GRID_MAX_CELLS_Y));
|
2018-11-05 01:04:16 +00:00
|
|
|
#endif
|
2020-03-14 04:18:16 +00:00
|
|
|
GRID_LOOP(x, y) {
|
|
|
|
Z_VALUES(x, y) = 0.001 * random(-200, 200);
|
2020-04-22 21:35:03 +00:00
|
|
|
TERN_(EXTENSIBLE_UI, ExtUI::onMeshUpdate(x, y, Z_VALUES(x, y)));
|
2020-03-14 04:18:16 +00:00
|
|
|
}
|
2019-09-25 02:29:21 +00:00
|
|
|
SERIAL_ECHOPGM("Simulated " STRINGIFY(GRID_MAX_POINTS_X) "x" STRINGIFY(GRID_MAX_POINTS_Y) " mesh ");
|
|
|
|
SERIAL_ECHOPAIR(" (", x_min);
|
|
|
|
SERIAL_CHAR(','); SERIAL_ECHO(y_min);
|
|
|
|
SERIAL_ECHOPAIR(")-(", x_max);
|
|
|
|
SERIAL_CHAR(','); SERIAL_ECHO(y_max);
|
2018-11-05 01:04:16 +00:00
|
|
|
SERIAL_ECHOLNPGM(")");
|
|
|
|
}
|
|
|
|
#endif
|
2018-04-25 11:43:45 +00:00
|
|
|
|
2020-05-17 02:14:04 +00:00
|
|
|
xyz_pos_t oldpos = current_position;
|
|
|
|
|
2018-04-25 11:43:45 +00:00
|
|
|
// If disabling leveling do it right away
|
|
|
|
// (Don't disable for just M420 or M420 V)
|
|
|
|
if (seen_S && !to_enable) set_bed_leveling_enabled(false);
|
2017-09-06 11:28:31 +00:00
|
|
|
|
|
|
|
#if ENABLED(AUTO_BED_LEVELING_UBL)
|
|
|
|
|
|
|
|
// L to load a mesh from the EEPROM
|
|
|
|
if (parser.seen('L')) {
|
|
|
|
|
2018-04-25 11:43:45 +00:00
|
|
|
set_bed_leveling_enabled(false);
|
|
|
|
|
2017-09-06 11:28:31 +00:00
|
|
|
#if ENABLED(EEPROM_SETTINGS)
|
2017-10-14 02:56:27 +00:00
|
|
|
const int8_t storage_slot = parser.has_value() ? parser.value_int() : ubl.storage_slot;
|
2017-12-25 14:48:54 +00:00
|
|
|
const int16_t a = settings.calc_num_meshes();
|
2017-09-06 11:28:31 +00:00
|
|
|
|
|
|
|
if (!a) {
|
2018-11-29 22:58:58 +00:00
|
|
|
SERIAL_ECHOLNPGM("?EEPROM storage not available.");
|
2017-09-06 11:28:31 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!WITHIN(storage_slot, 0, a - 1)) {
|
2018-11-29 22:58:58 +00:00
|
|
|
SERIAL_ECHOLNPGM("?Invalid storage slot.");
|
|
|
|
SERIAL_ECHOLNPAIR("?Use 0 to ", a - 1);
|
2017-09-06 11:28:31 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
settings.load_mesh(storage_slot);
|
2017-10-14 02:56:27 +00:00
|
|
|
ubl.storage_slot = storage_slot;
|
2017-09-06 11:28:31 +00:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
2018-11-29 22:58:58 +00:00
|
|
|
SERIAL_ECHOLNPGM("?EEPROM storage not available.");
|
2017-09-06 11:28:31 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-09-08 20:35:25 +00:00
|
|
|
// L or V display the map info
|
2019-09-26 08:47:26 +00:00
|
|
|
if (parser.seen("LV")) {
|
2018-04-17 20:52:56 +00:00
|
|
|
ubl.display_map(parser.byteval('T'));
|
2018-05-15 03:36:03 +00:00
|
|
|
SERIAL_ECHOPGM("Mesh is ");
|
|
|
|
if (!ubl.mesh_is_valid()) SERIAL_ECHOPGM("in");
|
|
|
|
SERIAL_ECHOLNPAIR("valid\nStorage slot: ", ubl.storage_slot);
|
2017-09-06 11:28:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // AUTO_BED_LEVELING_UBL
|
|
|
|
|
2018-12-07 22:14:11 +00:00
|
|
|
const bool seenV = parser.seen('V');
|
|
|
|
|
2018-04-25 11:43:45 +00:00
|
|
|
#if HAS_MESH
|
|
|
|
|
2018-12-07 22:14:11 +00:00
|
|
|
if (leveling_is_valid()) {
|
2018-04-25 11:43:45 +00:00
|
|
|
|
2018-12-07 22:14:11 +00:00
|
|
|
// Subtract the given value or the mean from all mesh values
|
|
|
|
if (parser.seen('C')) {
|
|
|
|
const float cval = parser.value_float();
|
|
|
|
#if ENABLED(AUTO_BED_LEVELING_UBL)
|
2018-04-25 11:43:45 +00:00
|
|
|
|
2018-12-07 22:14:11 +00:00
|
|
|
set_bed_leveling_enabled(false);
|
|
|
|
ubl.adjust_mesh_to_mean(true, cval);
|
2018-04-25 11:43:45 +00:00
|
|
|
|
2018-12-07 22:14:11 +00:00
|
|
|
#else
|
2018-04-25 11:43:45 +00:00
|
|
|
|
2018-12-07 22:14:11 +00:00
|
|
|
#if ENABLED(M420_C_USE_MEAN)
|
2018-04-25 11:43:45 +00:00
|
|
|
|
2018-12-07 22:14:11 +00:00
|
|
|
// Get the sum and average of all mesh values
|
|
|
|
float mesh_sum = 0;
|
2020-04-02 19:32:49 +00:00
|
|
|
GRID_LOOP(x, y) mesh_sum += Z_VALUES(x, y);
|
2018-12-07 22:14:11 +00:00
|
|
|
const float zmean = mesh_sum / float(GRID_MAX_POINTS);
|
2018-04-25 11:43:45 +00:00
|
|
|
|
2021-03-24 08:28:48 +00:00
|
|
|
#else // midrange
|
2018-04-25 11:43:45 +00:00
|
|
|
|
2021-03-24 08:28:48 +00:00
|
|
|
// Find the low and high mesh values.
|
2018-12-07 22:14:11 +00:00
|
|
|
float lo_val = 100, hi_val = -100;
|
2020-04-02 19:32:49 +00:00
|
|
|
GRID_LOOP(x, y) {
|
|
|
|
const float z = Z_VALUES(x, y);
|
|
|
|
NOMORE(lo_val, z);
|
|
|
|
NOLESS(hi_val, z);
|
|
|
|
}
|
2021-03-24 08:28:48 +00:00
|
|
|
// Get the midrange plus C value. (The median may be better.)
|
2018-12-07 22:14:11 +00:00
|
|
|
const float zmean = (lo_val + hi_val) / 2.0 + cval;
|
2018-04-25 11:43:45 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2018-12-07 22:14:11 +00:00
|
|
|
// If not very close to 0, adjust the mesh
|
|
|
|
if (!NEAR_ZERO(zmean)) {
|
|
|
|
set_bed_leveling_enabled(false);
|
|
|
|
// Subtract the mean from all values
|
2020-04-02 19:32:49 +00:00
|
|
|
GRID_LOOP(x, y) {
|
|
|
|
Z_VALUES(x, y) -= zmean;
|
2020-04-22 21:35:03 +00:00
|
|
|
TERN_(EXTENSIBLE_UI, ExtUI::onMeshUpdate(x, y, Z_VALUES(x, y)));
|
2020-04-02 19:32:49 +00:00
|
|
|
}
|
2020-04-22 21:35:03 +00:00
|
|
|
TERN_(ABL_BILINEAR_SUBDIVISION, bed_level_virt_interpolate());
|
2018-12-07 22:14:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
else if (to_enable || seenV) {
|
2019-02-04 07:54:43 +00:00
|
|
|
SERIAL_ECHO_MSG("Invalid mesh.");
|
2018-12-07 22:14:11 +00:00
|
|
|
goto EXIT_M420;
|
2018-04-25 11:43:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // HAS_MESH
|
|
|
|
|
2017-09-06 11:28:31 +00:00
|
|
|
// V to print the matrix or mesh
|
2018-12-07 22:14:11 +00:00
|
|
|
if (seenV) {
|
2017-09-06 11:28:31 +00:00
|
|
|
#if ABL_PLANAR
|
|
|
|
planner.bed_level_matrix.debug(PSTR("Bed Level Correction Matrix:"));
|
|
|
|
#else
|
|
|
|
if (leveling_is_valid()) {
|
|
|
|
#if ENABLED(AUTO_BED_LEVELING_BILINEAR)
|
|
|
|
print_bilinear_leveling_grid();
|
2020-04-22 21:35:03 +00:00
|
|
|
TERN_(ABL_BILINEAR_SUBDIVISION, print_bilinear_leveling_grid_virt());
|
2017-09-06 11:28:31 +00:00
|
|
|
#elif ENABLED(MESH_BED_LEVELING)
|
|
|
|
SERIAL_ECHOLNPGM("Mesh Bed Level data:");
|
2018-01-07 02:50:21 +00:00
|
|
|
mbl.report_mesh();
|
2017-09-06 11:28:31 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
|
2017-12-11 03:17:07 +00:00
|
|
|
if (parser.seen('Z')) set_z_fade_height(parser.value_linear_units(), false);
|
2017-09-06 11:28:31 +00:00
|
|
|
#endif
|
|
|
|
|
2018-04-25 11:43:45 +00:00
|
|
|
// Enable leveling if specified, or if previously active
|
|
|
|
set_bed_leveling_enabled(to_enable);
|
2017-09-06 11:28:31 +00:00
|
|
|
|
2019-01-03 14:36:43 +00:00
|
|
|
#if HAS_MESH
|
|
|
|
EXIT_M420:
|
|
|
|
#endif
|
2018-12-07 22:14:11 +00:00
|
|
|
|
2018-04-25 11:43:45 +00:00
|
|
|
// Error if leveling failed to enable or reenable
|
2018-11-29 22:58:58 +00:00
|
|
|
if (to_enable && !planner.leveling_active)
|
2020-02-26 09:02:03 +00:00
|
|
|
SERIAL_ERROR_MSG(STR_ERR_M420_FAILED);
|
2017-09-06 11:28:31 +00:00
|
|
|
|
|
|
|
SERIAL_ECHO_START();
|
2018-11-13 01:53:56 +00:00
|
|
|
SERIAL_ECHOPGM("Bed Leveling ");
|
2018-11-12 23:04:00 +00:00
|
|
|
serialprintln_onoff(planner.leveling_active);
|
2017-09-06 11:28:31 +00:00
|
|
|
|
|
|
|
#if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
|
|
|
|
SERIAL_ECHO_START();
|
|
|
|
SERIAL_ECHOPGM("Fade Height ");
|
|
|
|
if (planner.z_fade_height > 0.0)
|
|
|
|
SERIAL_ECHOLN(planner.z_fade_height);
|
|
|
|
else
|
2020-02-26 09:02:03 +00:00
|
|
|
SERIAL_ECHOLNPGM(STR_OFF);
|
2017-09-06 11:28:31 +00:00
|
|
|
#endif
|
2017-12-11 03:26:11 +00:00
|
|
|
|
|
|
|
// Report change in position
|
2019-09-29 09:25:39 +00:00
|
|
|
if (oldpos != current_position)
|
2017-12-11 03:26:11 +00:00
|
|
|
report_current_position();
|
2017-09-06 11:28:31 +00:00
|
|
|
}
|
2017-09-08 20:35:25 +00:00
|
|
|
|
2018-06-29 03:58:57 +00:00
|
|
|
#endif // HAS_LEVELING
|