|
|
|
@ -32,6 +32,8 @@
|
|
|
|
|
#include "../../module/configuration_store.h"
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
//#define M420_C_USE_MEAN
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* M420: Enable/Disable Bed Leveling and/or set the Z fade height.
|
|
|
|
|
*
|
|
|
|
@ -43,8 +45,18 @@
|
|
|
|
|
*
|
|
|
|
|
* L[index] Load UBL mesh from index (0 is default)
|
|
|
|
|
* T[map] 0:Human-readable 1:CSV 2:"LCD" 4:Compact
|
|
|
|
|
*
|
|
|
|
|
* With mesh-based leveling only:
|
|
|
|
|
*
|
|
|
|
|
* C Center mesh on the mean of the lowest and highest
|
|
|
|
|
*/
|
|
|
|
|
void GcodeSuite::M420() {
|
|
|
|
|
const bool seen_S = parser.seen('S');
|
|
|
|
|
bool to_enable = seen_S ? parser.value_bool() : planner.leveling_active;
|
|
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
|
|
|
|
|
const float oldpos[] = { current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS] };
|
|
|
|
|
|
|
|
|
@ -53,6 +65,8 @@ void GcodeSuite::M420() {
|
|
|
|
|
// L to load a mesh from the EEPROM
|
|
|
|
|
if (parser.seen('L')) {
|
|
|
|
|
|
|
|
|
|
set_bed_leveling_enabled(false);
|
|
|
|
|
|
|
|
|
|
#if ENABLED(EEPROM_SETTINGS)
|
|
|
|
|
const int8_t storage_slot = parser.has_value() ? parser.value_int() : ubl.storage_slot;
|
|
|
|
|
const int16_t a = settings.calc_num_meshes();
|
|
|
|
@ -88,6 +102,65 @@ void GcodeSuite::M420() {
|
|
|
|
|
|
|
|
|
|
#endif // AUTO_BED_LEVELING_UBL
|
|
|
|
|
|
|
|
|
|
#if HAS_MESH
|
|
|
|
|
|
|
|
|
|
#if ENABLED(MESH_BED_LEVELING)
|
|
|
|
|
#define Z_VALUES(X,Y) mbl.z_values[X][Y]
|
|
|
|
|
#else
|
|
|
|
|
#define Z_VALUES(X,Y) z_values[X][Y]
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
// Subtract the given value or the mean from all mesh values
|
|
|
|
|
if (leveling_is_valid() && parser.seen('C')) {
|
|
|
|
|
const float cval = parser.value_float();
|
|
|
|
|
#if ENABLED(AUTO_BED_LEVELING_UBL)
|
|
|
|
|
|
|
|
|
|
set_bed_leveling_enabled(false);
|
|
|
|
|
ubl.adjust_mesh_to_mean(cval);
|
|
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
|
|
#if ENABLED(M420_C_USE_MEAN)
|
|
|
|
|
|
|
|
|
|
// Get the sum and average of all mesh values
|
|
|
|
|
float mesh_sum = 0;
|
|
|
|
|
for (uint8_t x = GRID_MAX_POINTS_X; x--;)
|
|
|
|
|
for (uint8_t y = GRID_MAX_POINTS_Y; y--;)
|
|
|
|
|
mesh_sum += Z_VALUES(x, y);
|
|
|
|
|
const float zmean = mesh_sum / float(GRID_MAX_POINTS);
|
|
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
|
|
// Find the low and high mesh values
|
|
|
|
|
float lo_val = 100, hi_val = -100;
|
|
|
|
|
for (uint8_t x = GRID_MAX_POINTS_X; x--;)
|
|
|
|
|
for (uint8_t y = GRID_MAX_POINTS_Y; y--;) {
|
|
|
|
|
const float z = Z_VALUES(x, y);
|
|
|
|
|
NOMORE(lo_val, z);
|
|
|
|
|
NOLESS(hi_val, z);
|
|
|
|
|
}
|
|
|
|
|
// Take the mean of the lowest and highest
|
|
|
|
|
const float zmean = (lo_val + hi_val) / 2.0 + cval;
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
// If not very close to 0, adjust the mesh
|
|
|
|
|
if (!NEAR_ZERO(zmean)) {
|
|
|
|
|
set_bed_leveling_enabled(false);
|
|
|
|
|
// Subtract the mean from all values
|
|
|
|
|
for (uint8_t x = GRID_MAX_POINTS_X; x--;)
|
|
|
|
|
for (uint8_t y = GRID_MAX_POINTS_Y; y--;)
|
|
|
|
|
Z_VALUES(x, y) -= zmean;
|
|
|
|
|
#if ENABLED(ABL_BILINEAR_SUBDIVISION)
|
|
|
|
|
bed_level_virt_interpolate();
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif // HAS_MESH
|
|
|
|
|
|
|
|
|
|
// V to print the matrix or mesh
|
|
|
|
|
if (parser.seen('V')) {
|
|
|
|
|
#if ABL_PLANAR
|
|
|
|
@ -111,21 +184,17 @@ void GcodeSuite::M420() {
|
|
|
|
|
if (parser.seen('Z')) set_z_fade_height(parser.value_linear_units(), false);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
bool to_enable = false;
|
|
|
|
|
if (parser.seen('S')) {
|
|
|
|
|
to_enable = parser.value_bool();
|
|
|
|
|
set_bed_leveling_enabled(to_enable);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const bool new_status = planner.leveling_active;
|
|
|
|
|
// Enable leveling if specified, or if previously active
|
|
|
|
|
set_bed_leveling_enabled(to_enable);
|
|
|
|
|
|
|
|
|
|
if (to_enable && !new_status) {
|
|
|
|
|
// Error if leveling failed to enable or reenable
|
|
|
|
|
if (to_enable && !planner.leveling_active) {
|
|
|
|
|
SERIAL_ERROR_START();
|
|
|
|
|
SERIAL_ERRORLNPGM(MSG_ERR_M420_FAILED);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SERIAL_ECHO_START();
|
|
|
|
|
SERIAL_ECHOLNPAIR("Bed Leveling ", new_status ? MSG_ON : MSG_OFF);
|
|
|
|
|
SERIAL_ECHOLNPAIR("Bed Leveling ", planner.leveling_active ? MSG_ON : MSG_OFF);
|
|
|
|
|
|
|
|
|
|
#if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
|
|
|
|
|
SERIAL_ECHO_START();
|
|
|
|
|