|
@@ -46,275 +46,275 @@ struct mesh_index_pair;
|
46
|
46
|
#endif
|
47
|
47
|
|
48
|
48
|
class unified_bed_leveling {
|
49
|
|
- private:
|
50
|
|
-
|
51
|
|
- static int g29_verbose_level,
|
52
|
|
- g29_phase_value,
|
53
|
|
- g29_repetition_cnt,
|
54
|
|
- g29_storage_slot,
|
55
|
|
- g29_map_type;
|
56
|
|
- static bool g29_c_flag;
|
57
|
|
- static float g29_card_thickness,
|
58
|
|
- g29_constant;
|
59
|
|
- static xy_pos_t g29_pos;
|
60
|
|
- static xy_bool_t xy_seen;
|
61
|
|
-
|
62
|
|
- #if HAS_BED_PROBE
|
63
|
|
- static int g29_grid_size;
|
64
|
|
- #endif
|
65
|
|
-
|
66
|
|
- #if IS_NEWPANEL
|
67
|
|
- static void move_z_with_encoder(const float &multiplier);
|
68
|
|
- static float measure_point_with_encoder();
|
69
|
|
- static float measure_business_card_thickness();
|
70
|
|
- static void manually_probe_remaining_mesh(const xy_pos_t&, const float&, const float&, const bool) _O0;
|
71
|
|
- static void fine_tune_mesh(const xy_pos_t &pos, const bool do_ubl_mesh_map) _O0;
|
72
|
|
- #endif
|
|
49
|
+private:
|
|
50
|
+
|
|
51
|
+ static int g29_verbose_level,
|
|
52
|
+ g29_phase_value,
|
|
53
|
+ g29_repetition_cnt,
|
|
54
|
+ g29_storage_slot,
|
|
55
|
+ g29_map_type;
|
|
56
|
+ static bool g29_c_flag;
|
|
57
|
+ static float g29_card_thickness,
|
|
58
|
+ g29_constant;
|
|
59
|
+ static xy_pos_t g29_pos;
|
|
60
|
+ static xy_bool_t xy_seen;
|
|
61
|
+
|
|
62
|
+ #if HAS_BED_PROBE
|
|
63
|
+ static int g29_grid_size;
|
|
64
|
+ #endif
|
|
65
|
+
|
|
66
|
+ #if IS_NEWPANEL
|
|
67
|
+ static void move_z_with_encoder(const float &multiplier);
|
|
68
|
+ static float measure_point_with_encoder();
|
|
69
|
+ static float measure_business_card_thickness();
|
|
70
|
+ static void manually_probe_remaining_mesh(const xy_pos_t&, const float&, const float&, const bool) _O0;
|
|
71
|
+ static void fine_tune_mesh(const xy_pos_t &pos, const bool do_ubl_mesh_map) _O0;
|
|
72
|
+ #endif
|
|
73
|
+
|
|
74
|
+ static bool g29_parameter_parsing() _O0;
|
|
75
|
+ static void shift_mesh_height();
|
|
76
|
+ static void probe_entire_mesh(const xy_pos_t &near, const bool do_ubl_mesh_map, const bool stow_probe, const bool do_furthest) _O0;
|
|
77
|
+ static void tilt_mesh_based_on_3pts(const float &z1, const float &z2, const float &z3);
|
|
78
|
+ static void tilt_mesh_based_on_probed_grid(const bool do_ubl_mesh_map);
|
|
79
|
+ static bool smart_fill_one(const uint8_t x, const uint8_t y, const int8_t xdir, const int8_t ydir);
|
|
80
|
+ static inline bool smart_fill_one(const xy_uint8_t &pos, const xy_uint8_t &dir) {
|
|
81
|
+ return smart_fill_one(pos.x, pos.y, dir.x, dir.y);
|
|
82
|
+ }
|
|
83
|
+ static void smart_fill_mesh();
|
|
84
|
+
|
|
85
|
+ #if ENABLED(UBL_DEVEL_DEBUGGING)
|
|
86
|
+ static void g29_what_command();
|
|
87
|
+ static void g29_eeprom_dump();
|
|
88
|
+ static void g29_compare_current_mesh_to_stored_mesh();
|
|
89
|
+ #endif
|
|
90
|
+
|
|
91
|
+public:
|
|
92
|
+
|
|
93
|
+ static void echo_name();
|
|
94
|
+ static void report_current_mesh();
|
|
95
|
+ static void report_state();
|
|
96
|
+ static void save_ubl_active_state_and_disable();
|
|
97
|
+ static void restore_ubl_active_state_and_leave();
|
|
98
|
+ static void display_map(const int) _O0;
|
|
99
|
+ static mesh_index_pair find_closest_mesh_point_of_type(const MeshPointType, const xy_pos_t&, const bool=false, MeshFlags *done_flags=nullptr) _O0;
|
|
100
|
+ static mesh_index_pair find_furthest_invalid_mesh_point() _O0;
|
|
101
|
+ static void reset();
|
|
102
|
+ static void invalidate();
|
|
103
|
+ static void set_all_mesh_points_to_value(const float value);
|
|
104
|
+ static void adjust_mesh_to_mean(const bool cflag, const float value);
|
|
105
|
+ static bool sanity_check();
|
|
106
|
+
|
|
107
|
+ static void G29() _O0; // O0 for no optimization
|
|
108
|
+ static void smart_fill_wlsf(const float &) _O2; // O2 gives smaller code than Os on A2560
|
|
109
|
+
|
|
110
|
+ static int8_t storage_slot;
|
|
111
|
+
|
|
112
|
+ static bed_mesh_t z_values;
|
|
113
|
+ #if ENABLED(OPTIMIZED_MESH_STORAGE)
|
|
114
|
+ static void set_store_from_mesh(const bed_mesh_t &in_values, mesh_store_t &stored_values);
|
|
115
|
+ static void set_mesh_from_store(const mesh_store_t &stored_values, bed_mesh_t &out_values);
|
|
116
|
+ #endif
|
|
117
|
+ static const float _mesh_index_to_xpos[GRID_MAX_POINTS_X],
|
|
118
|
+ _mesh_index_to_ypos[GRID_MAX_POINTS_Y];
|
|
119
|
+
|
|
120
|
+ #if HAS_LCD_MENU
|
|
121
|
+ static bool lcd_map_control;
|
|
122
|
+ static void steppers_were_disabled();
|
|
123
|
+ #else
|
|
124
|
+ static inline void steppers_were_disabled() {}
|
|
125
|
+ #endif
|
|
126
|
+
|
|
127
|
+ static volatile int16_t encoder_diff; // Volatile because buttons may changed it at interrupt time
|
|
128
|
+
|
|
129
|
+ unified_bed_leveling();
|
|
130
|
+
|
|
131
|
+ FORCE_INLINE static void set_z(const int8_t px, const int8_t py, const float &z) { z_values[px][py] = z; }
|
|
132
|
+
|
|
133
|
+ static int8_t cell_index_x_raw(const float &x) {
|
|
134
|
+ return FLOOR((x - (MESH_MIN_X)) * RECIPROCAL(MESH_X_DIST));
|
|
135
|
+ }
|
|
136
|
+
|
|
137
|
+ static int8_t cell_index_y_raw(const float &y) {
|
|
138
|
+ return FLOOR((y - (MESH_MIN_Y)) * RECIPROCAL(MESH_Y_DIST));
|
|
139
|
+ }
|
|
140
|
+
|
|
141
|
+ static int8_t cell_index_x_valid(const float &x) {
|
|
142
|
+ return WITHIN(cell_index_x_raw(x), 0, (GRID_MAX_POINTS_X - 2));
|
|
143
|
+ }
|
|
144
|
+
|
|
145
|
+ static int8_t cell_index_y_valid(const float &y) {
|
|
146
|
+ return WITHIN(cell_index_y_raw(y), 0, (GRID_MAX_POINTS_Y - 2));
|
|
147
|
+ }
|
|
148
|
+
|
|
149
|
+ static int8_t cell_index_x(const float &x) {
|
|
150
|
+ return constrain(cell_index_x_raw(x), 0, (GRID_MAX_POINTS_X) - 2);
|
|
151
|
+ }
|
|
152
|
+
|
|
153
|
+ static int8_t cell_index_y(const float &y) {
|
|
154
|
+ return constrain(cell_index_y_raw(y), 0, (GRID_MAX_POINTS_Y) - 2);
|
|
155
|
+ }
|
|
156
|
+
|
|
157
|
+ static inline xy_int8_t cell_indexes(const float &x, const float &y) {
|
|
158
|
+ return { cell_index_x(x), cell_index_y(y) };
|
|
159
|
+ }
|
|
160
|
+ static inline xy_int8_t cell_indexes(const xy_pos_t &xy) { return cell_indexes(xy.x, xy.y); }
|
|
161
|
+
|
|
162
|
+ static int8_t closest_x_index(const float &x) {
|
|
163
|
+ const int8_t px = (x - (MESH_MIN_X) + (MESH_X_DIST) * 0.5) * RECIPROCAL(MESH_X_DIST);
|
|
164
|
+ return WITHIN(px, 0, GRID_MAX_POINTS_X - 1) ? px : -1;
|
|
165
|
+ }
|
|
166
|
+ static int8_t closest_y_index(const float &y) {
|
|
167
|
+ const int8_t py = (y - (MESH_MIN_Y) + (MESH_Y_DIST) * 0.5) * RECIPROCAL(MESH_Y_DIST);
|
|
168
|
+ return WITHIN(py, 0, GRID_MAX_POINTS_Y - 1) ? py : -1;
|
|
169
|
+ }
|
|
170
|
+ static inline xy_int8_t closest_indexes(const xy_pos_t &xy) {
|
|
171
|
+ return { closest_x_index(xy.x), closest_y_index(xy.y) };
|
|
172
|
+ }
|
|
173
|
+
|
|
174
|
+ /**
|
|
175
|
+ * z2 --|
|
|
176
|
+ * z0 | |
|
|
177
|
+ * | | + (z2-z1)
|
|
178
|
+ * z1 | | |
|
|
179
|
+ * ---+-------------+--------+-- --|
|
|
180
|
+ * a1 a0 a2
|
|
181
|
+ * |<---delta_a---------->|
|
|
182
|
+ *
|
|
183
|
+ * calc_z0 is the basis for all the Mesh Based correction. It is used to
|
|
184
|
+ * find the expected Z Height at a position between two known Z-Height locations.
|
|
185
|
+ *
|
|
186
|
+ * It is fairly expensive with its 4 floating point additions and 2 floating point
|
|
187
|
+ * multiplications.
|
|
188
|
+ */
|
|
189
|
+ FORCE_INLINE static float calc_z0(const float &a0, const float &a1, const float &z1, const float &a2, const float &z2) {
|
|
190
|
+ return z1 + (z2 - z1) * (a0 - a1) / (a2 - a1);
|
|
191
|
+ }
|
|
192
|
+
|
|
193
|
+ #ifdef UBL_Z_RAISE_WHEN_OFF_MESH
|
|
194
|
+ #define _UBL_OUTER_Z_RAISE UBL_Z_RAISE_WHEN_OFF_MESH
|
|
195
|
+ #else
|
|
196
|
+ #define _UBL_OUTER_Z_RAISE NAN
|
|
197
|
+ #endif
|
|
198
|
+
|
|
199
|
+ /**
|
|
200
|
+ * z_correction_for_x_on_horizontal_mesh_line is an optimization for
|
|
201
|
+ * the case where the printer is making a vertical line that only crosses horizontal mesh lines.
|
|
202
|
+ */
|
|
203
|
+ static inline float z_correction_for_x_on_horizontal_mesh_line(const float &rx0, const int x1_i, const int yi) {
|
|
204
|
+ if (!WITHIN(x1_i, 0, GRID_MAX_POINTS_X - 1) || !WITHIN(yi, 0, GRID_MAX_POINTS_Y - 1)) {
|
|
205
|
+
|
|
206
|
+ if (DEBUGGING(LEVELING)) {
|
|
207
|
+ if (WITHIN(x1_i, 0, GRID_MAX_POINTS_X - 1)) DEBUG_ECHOPGM("yi"); else DEBUG_ECHOPGM("x1_i");
|
|
208
|
+ DEBUG_ECHOLNPAIR(" out of bounds in z_correction_for_x_on_horizontal_mesh_line(rx0=", rx0, ",x1_i=", x1_i, ",yi=", yi, ")");
|
|
209
|
+ }
|
73
|
210
|
|
74
|
|
- static bool g29_parameter_parsing() _O0;
|
75
|
|
- static void shift_mesh_height();
|
76
|
|
- static void probe_entire_mesh(const xy_pos_t &near, const bool do_ubl_mesh_map, const bool stow_probe, const bool do_furthest) _O0;
|
77
|
|
- static void tilt_mesh_based_on_3pts(const float &z1, const float &z2, const float &z3);
|
78
|
|
- static void tilt_mesh_based_on_probed_grid(const bool do_ubl_mesh_map);
|
79
|
|
- static bool smart_fill_one(const uint8_t x, const uint8_t y, const int8_t xdir, const int8_t ydir);
|
80
|
|
- static inline bool smart_fill_one(const xy_uint8_t &pos, const xy_uint8_t &dir) {
|
81
|
|
- return smart_fill_one(pos.x, pos.y, dir.x, dir.y);
|
|
211
|
+ // The requested location is off the mesh. Return UBL_Z_RAISE_WHEN_OFF_MESH or NAN.
|
|
212
|
+ return _UBL_OUTER_Z_RAISE;
|
82
|
213
|
}
|
83
|
|
- static void smart_fill_mesh();
|
84
|
|
-
|
85
|
|
- #if ENABLED(UBL_DEVEL_DEBUGGING)
|
86
|
|
- static void g29_what_command();
|
87
|
|
- static void g29_eeprom_dump();
|
88
|
|
- static void g29_compare_current_mesh_to_stored_mesh();
|
89
|
|
- #endif
|
90
|
214
|
|
91
|
|
- public:
|
92
|
|
-
|
93
|
|
- static void echo_name();
|
94
|
|
- static void report_current_mesh();
|
95
|
|
- static void report_state();
|
96
|
|
- static void save_ubl_active_state_and_disable();
|
97
|
|
- static void restore_ubl_active_state_and_leave();
|
98
|
|
- static void display_map(const int) _O0;
|
99
|
|
- static mesh_index_pair find_closest_mesh_point_of_type(const MeshPointType, const xy_pos_t&, const bool=false, MeshFlags *done_flags=nullptr) _O0;
|
100
|
|
- static mesh_index_pair find_furthest_invalid_mesh_point() _O0;
|
101
|
|
- static void reset();
|
102
|
|
- static void invalidate();
|
103
|
|
- static void set_all_mesh_points_to_value(const float value);
|
104
|
|
- static void adjust_mesh_to_mean(const bool cflag, const float value);
|
105
|
|
- static bool sanity_check();
|
106
|
|
-
|
107
|
|
- static void G29() _O0; // O0 for no optimization
|
108
|
|
- static void smart_fill_wlsf(const float &) _O2; // O2 gives smaller code than Os on A2560
|
109
|
|
-
|
110
|
|
- static int8_t storage_slot;
|
111
|
|
-
|
112
|
|
- static bed_mesh_t z_values;
|
113
|
|
- #if ENABLED(OPTIMIZED_MESH_STORAGE)
|
114
|
|
- static void set_store_from_mesh(const bed_mesh_t &in_values, mesh_store_t &stored_values);
|
115
|
|
- static void set_mesh_from_store(const mesh_store_t &stored_values, bed_mesh_t &out_values);
|
116
|
|
- #endif
|
117
|
|
- static const float _mesh_index_to_xpos[GRID_MAX_POINTS_X],
|
118
|
|
- _mesh_index_to_ypos[GRID_MAX_POINTS_Y];
|
119
|
|
-
|
120
|
|
- #if HAS_LCD_MENU
|
121
|
|
- static bool lcd_map_control;
|
122
|
|
- static void steppers_were_disabled();
|
123
|
|
- #else
|
124
|
|
- static inline void steppers_were_disabled() {}
|
125
|
|
- #endif
|
126
|
|
-
|
127
|
|
- static volatile int16_t encoder_diff; // Volatile because buttons may changed it at interrupt time
|
|
215
|
+ const float xratio = (rx0 - mesh_index_to_xpos(x1_i)) * RECIPROCAL(MESH_X_DIST),
|
|
216
|
+ z1 = z_values[x1_i][yi];
|
128
|
217
|
|
129
|
|
- unified_bed_leveling();
|
|
218
|
+ return z1 + xratio * (z_values[_MIN(x1_i, GRID_MAX_POINTS_X - 2) + 1][yi] - z1); // Don't allow x1_i+1 to be past the end of the array
|
|
219
|
+ // If it is, it is clamped to the last element of the
|
|
220
|
+ // z_values[][] array and no correction is applied.
|
|
221
|
+ }
|
130
|
222
|
|
131
|
|
- FORCE_INLINE static void set_z(const int8_t px, const int8_t py, const float &z) { z_values[px][py] = z; }
|
|
223
|
+ //
|
|
224
|
+ // See comments above for z_correction_for_x_on_horizontal_mesh_line
|
|
225
|
+ //
|
|
226
|
+ static inline float z_correction_for_y_on_vertical_mesh_line(const float &ry0, const int xi, const int y1_i) {
|
|
227
|
+ if (!WITHIN(xi, 0, GRID_MAX_POINTS_X - 1) || !WITHIN(y1_i, 0, GRID_MAX_POINTS_Y - 1)) {
|
132
|
228
|
|
133
|
|
- static int8_t cell_index_x_raw(const float &x) {
|
134
|
|
- return FLOOR((x - (MESH_MIN_X)) * RECIPROCAL(MESH_X_DIST));
|
135
|
|
- }
|
136
|
|
-
|
137
|
|
- static int8_t cell_index_y_raw(const float &y) {
|
138
|
|
- return FLOOR((y - (MESH_MIN_Y)) * RECIPROCAL(MESH_Y_DIST));
|
139
|
|
- }
|
140
|
|
-
|
141
|
|
- static int8_t cell_index_x_valid(const float &x) {
|
142
|
|
- return WITHIN(cell_index_x_raw(x), 0, (GRID_MAX_POINTS_X - 2));
|
143
|
|
- }
|
|
229
|
+ if (DEBUGGING(LEVELING)) {
|
|
230
|
+ if (WITHIN(xi, 0, GRID_MAX_POINTS_X - 1)) DEBUG_ECHOPGM("y1_i"); else DEBUG_ECHOPGM("xi");
|
|
231
|
+ DEBUG_ECHOLNPAIR(" out of bounds in z_correction_for_y_on_vertical_mesh_line(ry0=", ry0, ", xi=", xi, ", y1_i=", y1_i, ")");
|
|
232
|
+ }
|
144
|
233
|
|
145
|
|
- static int8_t cell_index_y_valid(const float &y) {
|
146
|
|
- return WITHIN(cell_index_y_raw(y), 0, (GRID_MAX_POINTS_Y - 2));
|
|
234
|
+ // The requested location is off the mesh. Return UBL_Z_RAISE_WHEN_OFF_MESH or NAN.
|
|
235
|
+ return _UBL_OUTER_Z_RAISE;
|
147
|
236
|
}
|
148
|
237
|
|
149
|
|
- static int8_t cell_index_x(const float &x) {
|
150
|
|
- return constrain(cell_index_x_raw(x), 0, (GRID_MAX_POINTS_X) - 2);
|
151
|
|
- }
|
|
238
|
+ const float yratio = (ry0 - mesh_index_to_ypos(y1_i)) * RECIPROCAL(MESH_Y_DIST),
|
|
239
|
+ z1 = z_values[xi][y1_i];
|
152
|
240
|
|
153
|
|
- static int8_t cell_index_y(const float &y) {
|
154
|
|
- return constrain(cell_index_y_raw(y), 0, (GRID_MAX_POINTS_Y) - 2);
|
155
|
|
- }
|
|
241
|
+ return z1 + yratio * (z_values[xi][_MIN(y1_i, GRID_MAX_POINTS_Y - 2) + 1] - z1); // Don't allow y1_i+1 to be past the end of the array
|
|
242
|
+ // If it is, it is clamped to the last element of the
|
|
243
|
+ // z_values[][] array and no correction is applied.
|
|
244
|
+ }
|
156
|
245
|
|
157
|
|
- static inline xy_int8_t cell_indexes(const float &x, const float &y) {
|
158
|
|
- return { cell_index_x(x), cell_index_y(y) };
|
159
|
|
- }
|
160
|
|
- static inline xy_int8_t cell_indexes(const xy_pos_t &xy) { return cell_indexes(xy.x, xy.y); }
|
161
|
|
-
|
162
|
|
- static int8_t closest_x_index(const float &x) {
|
163
|
|
- const int8_t px = (x - (MESH_MIN_X) + (MESH_X_DIST) * 0.5) * RECIPROCAL(MESH_X_DIST);
|
164
|
|
- return WITHIN(px, 0, GRID_MAX_POINTS_X - 1) ? px : -1;
|
165
|
|
- }
|
166
|
|
- static int8_t closest_y_index(const float &y) {
|
167
|
|
- const int8_t py = (y - (MESH_MIN_Y) + (MESH_Y_DIST) * 0.5) * RECIPROCAL(MESH_Y_DIST);
|
168
|
|
- return WITHIN(py, 0, GRID_MAX_POINTS_Y - 1) ? py : -1;
|
169
|
|
- }
|
170
|
|
- static inline xy_int8_t closest_indexes(const xy_pos_t &xy) {
|
171
|
|
- return { closest_x_index(xy.x), closest_y_index(xy.y) };
|
172
|
|
- }
|
|
246
|
+ /**
|
|
247
|
+ * This is the generic Z-Correction. It works anywhere within a Mesh Cell. It first
|
|
248
|
+ * does a linear interpolation along both of the bounding X-Mesh-Lines to find the
|
|
249
|
+ * Z-Height at both ends. Then it does a linear interpolation of these heights based
|
|
250
|
+ * on the Y position within the cell.
|
|
251
|
+ */
|
|
252
|
+ static float get_z_correction(const float &rx0, const float &ry0) {
|
|
253
|
+ const int8_t cx = cell_index_x(rx0), cy = cell_index_y(ry0); // return values are clamped
|
173
|
254
|
|
174
|
255
|
/**
|
175
|
|
- * z2 --|
|
176
|
|
- * z0 | |
|
177
|
|
- * | | + (z2-z1)
|
178
|
|
- * z1 | | |
|
179
|
|
- * ---+-------------+--------+-- --|
|
180
|
|
- * a1 a0 a2
|
181
|
|
- * |<---delta_a---------->|
|
182
|
|
- *
|
183
|
|
- * calc_z0 is the basis for all the Mesh Based correction. It is used to
|
184
|
|
- * find the expected Z Height at a position between two known Z-Height locations.
|
185
|
|
- *
|
186
|
|
- * It is fairly expensive with its 4 floating point additions and 2 floating point
|
187
|
|
- * multiplications.
|
|
256
|
+ * Check if the requested location is off the mesh. If so, and
|
|
257
|
+ * UBL_Z_RAISE_WHEN_OFF_MESH is specified, that value is returned.
|
188
|
258
|
*/
|
189
|
|
- FORCE_INLINE static float calc_z0(const float &a0, const float &a1, const float &z1, const float &a2, const float &z2) {
|
190
|
|
- return z1 + (z2 - z1) * (a0 - a1) / (a2 - a1);
|
191
|
|
- }
|
192
|
|
-
|
193
|
259
|
#ifdef UBL_Z_RAISE_WHEN_OFF_MESH
|
194
|
|
- #define _UBL_OUTER_Z_RAISE UBL_Z_RAISE_WHEN_OFF_MESH
|
195
|
|
- #else
|
196
|
|
- #define _UBL_OUTER_Z_RAISE NAN
|
|
260
|
+ if (!WITHIN(rx0, MESH_MIN_X, MESH_MAX_X) || !WITHIN(ry0, MESH_MIN_Y, MESH_MAX_Y))
|
|
261
|
+ return UBL_Z_RAISE_WHEN_OFF_MESH;
|
197
|
262
|
#endif
|
198
|
263
|
|
199
|
|
- /**
|
200
|
|
- * z_correction_for_x_on_horizontal_mesh_line is an optimization for
|
201
|
|
- * the case where the printer is making a vertical line that only crosses horizontal mesh lines.
|
202
|
|
- */
|
203
|
|
- static inline float z_correction_for_x_on_horizontal_mesh_line(const float &rx0, const int x1_i, const int yi) {
|
204
|
|
- if (!WITHIN(x1_i, 0, GRID_MAX_POINTS_X - 1) || !WITHIN(yi, 0, GRID_MAX_POINTS_Y - 1)) {
|
205
|
|
-
|
206
|
|
- if (DEBUGGING(LEVELING)) {
|
207
|
|
- if (WITHIN(x1_i, 0, GRID_MAX_POINTS_X - 1)) DEBUG_ECHOPGM("yi"); else DEBUG_ECHOPGM("x1_i");
|
208
|
|
- DEBUG_ECHOLNPAIR(" out of bounds in z_correction_for_x_on_horizontal_mesh_line(rx0=", rx0, ",x1_i=", x1_i, ",yi=", yi, ")");
|
209
|
|
- }
|
|
264
|
+ const float z1 = calc_z0(rx0,
|
|
265
|
+ mesh_index_to_xpos(cx), z_values[cx][cy],
|
|
266
|
+ mesh_index_to_xpos(cx + 1), z_values[_MIN(cx, GRID_MAX_POINTS_X - 2) + 1][cy]);
|
210
|
267
|
|
211
|
|
- // The requested location is off the mesh. Return UBL_Z_RAISE_WHEN_OFF_MESH or NAN.
|
212
|
|
- return _UBL_OUTER_Z_RAISE;
|
213
|
|
- }
|
|
268
|
+ const float z2 = calc_z0(rx0,
|
|
269
|
+ mesh_index_to_xpos(cx), z_values[cx][_MIN(cy, GRID_MAX_POINTS_Y - 2) + 1],
|
|
270
|
+ mesh_index_to_xpos(cx + 1), z_values[_MIN(cx, GRID_MAX_POINTS_X - 2) + 1][_MIN(cy, GRID_MAX_POINTS_Y - 2) + 1]);
|
214
|
271
|
|
215
|
|
- const float xratio = (rx0 - mesh_index_to_xpos(x1_i)) * RECIPROCAL(MESH_X_DIST),
|
216
|
|
- z1 = z_values[x1_i][yi];
|
|
272
|
+ float z0 = calc_z0(ry0,
|
|
273
|
+ mesh_index_to_ypos(cy), z1,
|
|
274
|
+ mesh_index_to_ypos(cy + 1), z2);
|
217
|
275
|
|
218
|
|
- return z1 + xratio * (z_values[_MIN(x1_i, GRID_MAX_POINTS_X - 2) + 1][yi] - z1); // Don't allow x1_i+1 to be past the end of the array
|
219
|
|
- // If it is, it is clamped to the last element of the
|
220
|
|
- // z_values[][] array and no correction is applied.
|
|
276
|
+ if (DEBUGGING(MESH_ADJUST)) {
|
|
277
|
+ DEBUG_ECHOPAIR(" raw get_z_correction(", rx0);
|
|
278
|
+ DEBUG_CHAR(','); DEBUG_ECHO(ry0);
|
|
279
|
+ DEBUG_ECHOPAIR_F(") = ", z0, 6);
|
|
280
|
+ DEBUG_ECHOLNPAIR_F(" >>>---> ", z0, 6);
|
221
|
281
|
}
|
222
|
282
|
|
223
|
|
- //
|
224
|
|
- // See comments above for z_correction_for_x_on_horizontal_mesh_line
|
225
|
|
- //
|
226
|
|
- static inline float z_correction_for_y_on_vertical_mesh_line(const float &ry0, const int xi, const int y1_i) {
|
227
|
|
- if (!WITHIN(xi, 0, GRID_MAX_POINTS_X - 1) || !WITHIN(y1_i, 0, GRID_MAX_POINTS_Y - 1)) {
|
228
|
|
-
|
229
|
|
- if (DEBUGGING(LEVELING)) {
|
230
|
|
- if (WITHIN(xi, 0, GRID_MAX_POINTS_X - 1)) DEBUG_ECHOPGM("y1_i"); else DEBUG_ECHOPGM("xi");
|
231
|
|
- DEBUG_ECHOLNPAIR(" out of bounds in z_correction_for_y_on_vertical_mesh_line(ry0=", ry0, ", xi=", xi, ", y1_i=", y1_i, ")");
|
232
|
|
- }
|
233
|
|
-
|
234
|
|
- // The requested location is off the mesh. Return UBL_Z_RAISE_WHEN_OFF_MESH or NAN.
|
235
|
|
- return _UBL_OUTER_Z_RAISE;
|
236
|
|
- }
|
237
|
|
-
|
238
|
|
- const float yratio = (ry0 - mesh_index_to_ypos(y1_i)) * RECIPROCAL(MESH_Y_DIST),
|
239
|
|
- z1 = z_values[xi][y1_i];
|
240
|
|
-
|
241
|
|
- return z1 + yratio * (z_values[xi][_MIN(y1_i, GRID_MAX_POINTS_Y - 2) + 1] - z1); // Don't allow y1_i+1 to be past the end of the array
|
242
|
|
- // If it is, it is clamped to the last element of the
|
243
|
|
- // z_values[][] array and no correction is applied.
|
244
|
|
- }
|
245
|
|
-
|
246
|
|
- /**
|
247
|
|
- * This is the generic Z-Correction. It works anywhere within a Mesh Cell. It first
|
248
|
|
- * does a linear interpolation along both of the bounding X-Mesh-Lines to find the
|
249
|
|
- * Z-Height at both ends. Then it does a linear interpolation of these heights based
|
250
|
|
- * on the Y position within the cell.
|
251
|
|
- */
|
252
|
|
- static float get_z_correction(const float &rx0, const float &ry0) {
|
253
|
|
- const int8_t cx = cell_index_x(rx0), cy = cell_index_y(ry0); // return values are clamped
|
254
|
|
-
|
255
|
|
- /**
|
256
|
|
- * Check if the requested location is off the mesh. If so, and
|
257
|
|
- * UBL_Z_RAISE_WHEN_OFF_MESH is specified, that value is returned.
|
258
|
|
- */
|
259
|
|
- #ifdef UBL_Z_RAISE_WHEN_OFF_MESH
|
260
|
|
- if (!WITHIN(rx0, MESH_MIN_X, MESH_MAX_X) || !WITHIN(ry0, MESH_MIN_Y, MESH_MAX_Y))
|
261
|
|
- return UBL_Z_RAISE_WHEN_OFF_MESH;
|
262
|
|
- #endif
|
263
|
|
-
|
264
|
|
- const float z1 = calc_z0(rx0,
|
265
|
|
- mesh_index_to_xpos(cx), z_values[cx][cy],
|
266
|
|
- mesh_index_to_xpos(cx + 1), z_values[_MIN(cx, GRID_MAX_POINTS_X - 2) + 1][cy]);
|
267
|
|
-
|
268
|
|
- const float z2 = calc_z0(rx0,
|
269
|
|
- mesh_index_to_xpos(cx), z_values[cx][_MIN(cy, GRID_MAX_POINTS_Y - 2) + 1],
|
270
|
|
- mesh_index_to_xpos(cx + 1), z_values[_MIN(cx, GRID_MAX_POINTS_X - 2) + 1][_MIN(cy, GRID_MAX_POINTS_Y - 2) + 1]);
|
271
|
|
-
|
272
|
|
- float z0 = calc_z0(ry0,
|
273
|
|
- mesh_index_to_ypos(cy), z1,
|
274
|
|
- mesh_index_to_ypos(cy + 1), z2);
|
|
283
|
+ if (isnan(z0)) { // if part of the Mesh is undefined, it will show up as NAN
|
|
284
|
+ z0 = 0.0; // in ubl.z_values[][] and propagate through the
|
|
285
|
+ // calculations. If our correction is NAN, we throw it out
|
|
286
|
+ // because part of the Mesh is undefined and we don't have the
|
|
287
|
+ // information we need to complete the height correction.
|
275
|
288
|
|
276
|
289
|
if (DEBUGGING(MESH_ADJUST)) {
|
277
|
|
- DEBUG_ECHOPAIR(" raw get_z_correction(", rx0);
|
278
|
|
- DEBUG_CHAR(','); DEBUG_ECHO(ry0);
|
279
|
|
- DEBUG_ECHOPAIR_F(") = ", z0, 6);
|
280
|
|
- DEBUG_ECHOLNPAIR_F(" >>>---> ", z0, 6);
|
281
|
|
- }
|
282
|
|
-
|
283
|
|
- if (isnan(z0)) { // if part of the Mesh is undefined, it will show up as NAN
|
284
|
|
- z0 = 0.0; // in ubl.z_values[][] and propagate through the
|
285
|
|
- // calculations. If our correction is NAN, we throw it out
|
286
|
|
- // because part of the Mesh is undefined and we don't have the
|
287
|
|
- // information we need to complete the height correction.
|
288
|
|
-
|
289
|
|
- if (DEBUGGING(MESH_ADJUST)) {
|
290
|
|
- DEBUG_ECHOPAIR("??? Yikes! NAN in get_z_correction(", rx0);
|
291
|
|
- DEBUG_CHAR(',');
|
292
|
|
- DEBUG_ECHO(ry0);
|
293
|
|
- DEBUG_CHAR(')');
|
294
|
|
- DEBUG_EOL();
|
295
|
|
- }
|
|
290
|
+ DEBUG_ECHOPAIR("??? Yikes! NAN in get_z_correction(", rx0);
|
|
291
|
+ DEBUG_CHAR(',');
|
|
292
|
+ DEBUG_ECHO(ry0);
|
|
293
|
+ DEBUG_CHAR(')');
|
|
294
|
+ DEBUG_EOL();
|
296
|
295
|
}
|
297
|
|
- return z0;
|
298
|
|
- }
|
299
|
|
- static inline float get_z_correction(const xy_pos_t &pos) { return get_z_correction(pos.x, pos.y); }
|
300
|
|
-
|
301
|
|
- static inline float mesh_index_to_xpos(const uint8_t i) {
|
302
|
|
- return i < GRID_MAX_POINTS_X ? pgm_read_float(&_mesh_index_to_xpos[i]) : MESH_MIN_X + i * (MESH_X_DIST);
|
303
|
|
- }
|
304
|
|
- static inline float mesh_index_to_ypos(const uint8_t i) {
|
305
|
|
- return i < GRID_MAX_POINTS_Y ? pgm_read_float(&_mesh_index_to_ypos[i]) : MESH_MIN_Y + i * (MESH_Y_DIST);
|
306
|
|
- }
|
307
|
|
-
|
308
|
|
- #if UBL_SEGMENTED
|
309
|
|
- static bool line_to_destination_segmented(const feedRate_t &scaled_fr_mm_s);
|
310
|
|
- #else
|
311
|
|
- static void line_to_destination_cartesian(const feedRate_t &scaled_fr_mm_s, const uint8_t e);
|
312
|
|
- #endif
|
313
|
|
-
|
314
|
|
- static inline bool mesh_is_valid() {
|
315
|
|
- GRID_LOOP(x, y) if (isnan(z_values[x][y])) return false;
|
316
|
|
- return true;
|
317
|
296
|
}
|
|
297
|
+ return z0;
|
|
298
|
+ }
|
|
299
|
+ static inline float get_z_correction(const xy_pos_t &pos) { return get_z_correction(pos.x, pos.y); }
|
|
300
|
+
|
|
301
|
+ static inline float mesh_index_to_xpos(const uint8_t i) {
|
|
302
|
+ return i < GRID_MAX_POINTS_X ? pgm_read_float(&_mesh_index_to_xpos[i]) : MESH_MIN_X + i * (MESH_X_DIST);
|
|
303
|
+ }
|
|
304
|
+ static inline float mesh_index_to_ypos(const uint8_t i) {
|
|
305
|
+ return i < GRID_MAX_POINTS_Y ? pgm_read_float(&_mesh_index_to_ypos[i]) : MESH_MIN_Y + i * (MESH_Y_DIST);
|
|
306
|
+ }
|
|
307
|
+
|
|
308
|
+ #if UBL_SEGMENTED
|
|
309
|
+ static bool line_to_destination_segmented(const feedRate_t &scaled_fr_mm_s);
|
|
310
|
+ #else
|
|
311
|
+ static void line_to_destination_cartesian(const feedRate_t &scaled_fr_mm_s, const uint8_t e);
|
|
312
|
+ #endif
|
|
313
|
+
|
|
314
|
+ static inline bool mesh_is_valid() {
|
|
315
|
+ GRID_LOOP(x, y) if (isnan(z_values[x][y])) return false;
|
|
316
|
+ return true;
|
|
317
|
+ }
|
318
|
318
|
|
319
|
319
|
}; // class unified_bed_leveling
|
320
|
320
|
|