|
@@ -32,6 +32,8 @@
|
32
|
32
|
#include "../../module/configuration_store.h"
|
33
|
33
|
#endif
|
34
|
34
|
|
|
35
|
+//#define M420_C_USE_MEAN
|
|
36
|
+
|
35
|
37
|
/**
|
36
|
38
|
* M420: Enable/Disable Bed Leveling and/or set the Z fade height.
|
37
|
39
|
*
|
|
@@ -43,8 +45,18 @@
|
43
|
45
|
*
|
44
|
46
|
* L[index] Load UBL mesh from index (0 is default)
|
45
|
47
|
* T[map] 0:Human-readable 1:CSV 2:"LCD" 4:Compact
|
|
48
|
+ *
|
|
49
|
+ * With mesh-based leveling only:
|
|
50
|
+ *
|
|
51
|
+ * C Center mesh on the mean of the lowest and highest
|
46
|
52
|
*/
|
47
|
53
|
void GcodeSuite::M420() {
|
|
54
|
+ const bool seen_S = parser.seen('S');
|
|
55
|
+ bool to_enable = seen_S ? parser.value_bool() : planner.leveling_active;
|
|
56
|
+
|
|
57
|
+ // If disabling leveling do it right away
|
|
58
|
+ // (Don't disable for just M420 or M420 V)
|
|
59
|
+ if (seen_S && !to_enable) set_bed_leveling_enabled(false);
|
48
|
60
|
|
49
|
61
|
const float oldpos[] = { current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS] };
|
50
|
62
|
|
|
@@ -53,6 +65,8 @@ void GcodeSuite::M420() {
|
53
|
65
|
// L to load a mesh from the EEPROM
|
54
|
66
|
if (parser.seen('L')) {
|
55
|
67
|
|
|
68
|
+ set_bed_leveling_enabled(false);
|
|
69
|
+
|
56
|
70
|
#if ENABLED(EEPROM_SETTINGS)
|
57
|
71
|
const int8_t storage_slot = parser.has_value() ? parser.value_int() : ubl.storage_slot;
|
58
|
72
|
const int16_t a = settings.calc_num_meshes();
|
|
@@ -88,6 +102,65 @@ void GcodeSuite::M420() {
|
88
|
102
|
|
89
|
103
|
#endif // AUTO_BED_LEVELING_UBL
|
90
|
104
|
|
|
105
|
+ #if HAS_MESH
|
|
106
|
+
|
|
107
|
+ #if ENABLED(MESH_BED_LEVELING)
|
|
108
|
+ #define Z_VALUES(X,Y) mbl.z_values[X][Y]
|
|
109
|
+ #else
|
|
110
|
+ #define Z_VALUES(X,Y) z_values[X][Y]
|
|
111
|
+ #endif
|
|
112
|
+
|
|
113
|
+ // Subtract the given value or the mean from all mesh values
|
|
114
|
+ if (leveling_is_valid() && parser.seen('C')) {
|
|
115
|
+ const float cval = parser.value_float();
|
|
116
|
+ #if ENABLED(AUTO_BED_LEVELING_UBL)
|
|
117
|
+
|
|
118
|
+ set_bed_leveling_enabled(false);
|
|
119
|
+ ubl.adjust_mesh_to_mean(cval);
|
|
120
|
+
|
|
121
|
+ #else
|
|
122
|
+
|
|
123
|
+ #if ENABLED(M420_C_USE_MEAN)
|
|
124
|
+
|
|
125
|
+ // Get the sum and average of all mesh values
|
|
126
|
+ float mesh_sum = 0;
|
|
127
|
+ for (uint8_t x = GRID_MAX_POINTS_X; x--;)
|
|
128
|
+ for (uint8_t y = GRID_MAX_POINTS_Y; y--;)
|
|
129
|
+ mesh_sum += Z_VALUES(x, y);
|
|
130
|
+ const float zmean = mesh_sum / float(GRID_MAX_POINTS);
|
|
131
|
+
|
|
132
|
+ #else
|
|
133
|
+
|
|
134
|
+ // Find the low and high mesh values
|
|
135
|
+ float lo_val = 100, hi_val = -100;
|
|
136
|
+ for (uint8_t x = GRID_MAX_POINTS_X; x--;)
|
|
137
|
+ for (uint8_t y = GRID_MAX_POINTS_Y; y--;) {
|
|
138
|
+ const float z = Z_VALUES(x, y);
|
|
139
|
+ NOMORE(lo_val, z);
|
|
140
|
+ NOLESS(hi_val, z);
|
|
141
|
+ }
|
|
142
|
+ // Take the mean of the lowest and highest
|
|
143
|
+ const float zmean = (lo_val + hi_val) / 2.0 + cval;
|
|
144
|
+
|
|
145
|
+ #endif
|
|
146
|
+
|
|
147
|
+ // If not very close to 0, adjust the mesh
|
|
148
|
+ if (!NEAR_ZERO(zmean)) {
|
|
149
|
+ set_bed_leveling_enabled(false);
|
|
150
|
+ // Subtract the mean from all values
|
|
151
|
+ for (uint8_t x = GRID_MAX_POINTS_X; x--;)
|
|
152
|
+ for (uint8_t y = GRID_MAX_POINTS_Y; y--;)
|
|
153
|
+ Z_VALUES(x, y) -= zmean;
|
|
154
|
+ #if ENABLED(ABL_BILINEAR_SUBDIVISION)
|
|
155
|
+ bed_level_virt_interpolate();
|
|
156
|
+ #endif
|
|
157
|
+ }
|
|
158
|
+
|
|
159
|
+ #endif
|
|
160
|
+ }
|
|
161
|
+
|
|
162
|
+ #endif // HAS_MESH
|
|
163
|
+
|
91
|
164
|
// V to print the matrix or mesh
|
92
|
165
|
if (parser.seen('V')) {
|
93
|
166
|
#if ABL_PLANAR
|
|
@@ -111,21 +184,17 @@ void GcodeSuite::M420() {
|
111
|
184
|
if (parser.seen('Z')) set_z_fade_height(parser.value_linear_units(), false);
|
112
|
185
|
#endif
|
113
|
186
|
|
114
|
|
- bool to_enable = false;
|
115
|
|
- if (parser.seen('S')) {
|
116
|
|
- to_enable = parser.value_bool();
|
117
|
|
- set_bed_leveling_enabled(to_enable);
|
118
|
|
- }
|
119
|
|
-
|
120
|
|
- const bool new_status = planner.leveling_active;
|
|
187
|
+ // Enable leveling if specified, or if previously active
|
|
188
|
+ set_bed_leveling_enabled(to_enable);
|
121
|
189
|
|
122
|
|
- if (to_enable && !new_status) {
|
|
190
|
+ // Error if leveling failed to enable or reenable
|
|
191
|
+ if (to_enable && !planner.leveling_active) {
|
123
|
192
|
SERIAL_ERROR_START();
|
124
|
193
|
SERIAL_ERRORLNPGM(MSG_ERR_M420_FAILED);
|
125
|
194
|
}
|
126
|
195
|
|
127
|
196
|
SERIAL_ECHO_START();
|
128
|
|
- SERIAL_ECHOLNPAIR("Bed Leveling ", new_status ? MSG_ON : MSG_OFF);
|
|
197
|
+ SERIAL_ECHOLNPAIR("Bed Leveling ", planner.leveling_active ? MSG_ON : MSG_OFF);
|
129
|
198
|
|
130
|
199
|
#if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
|
131
|
200
|
SERIAL_ECHO_START();
|