Subversion Repositories MK-Marlin

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 ron 1
/**
2
 * Marlin 3D Printer Firmware
3
 * Copyright (C) 2016, 2017 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
4
 *
5
 * Based on Sprinter and grbl.
6
 * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU General Public License as published by
10
 * the Free Software Foundation, either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 *
21
 */
22
 
23
#ifndef UNIFIED_BED_LEVELING_H
24
#define UNIFIED_BED_LEVELING_H
25
 
26
#include "MarlinConfig.h"
27
 
28
//#define UBL_DEVEL_DEBUGGING
29
 
30
#include "Marlin.h"
31
#include "planner.h"
32
#include "math.h"
33
#include "configuration_store.h"
34
 
35
#define UBL_VERSION "1.01"
36
#define UBL_OK false
37
#define UBL_ERR true
38
 
39
#define USE_NOZZLE_AS_REFERENCE 0
40
#define USE_PROBE_AS_REFERENCE 1
41
 
42
// ubl_motion.cpp
43
 
44
#if ENABLED(UBL_DEVEL_DEBUGGING)
45
  void debug_current_and_destination(const char * const title);
46
#else
47
  FORCE_INLINE void debug_current_and_destination(const char * const title) { UNUSED(title); }
48
#endif
49
 
50
// ubl_G29.cpp
51
 
52
enum MeshPointType : char { INVALID, REAL, SET_IN_BITMAP };
53
 
54
// External references
55
 
56
char *ftostr43sign(const float&, char);
57
 
58
extern uint8_t ubl_cnt;
59
 
60
///////////////////////////////////////////////////////////////////////////////////////////////////////
61
 
62
#if ENABLED(ULTRA_LCD)
63
  void lcd_quick_feedback(const bool clear_buttons);
64
#endif
65
 
66
#define MESH_X_DIST (float(MESH_MAX_X - (MESH_MIN_X)) / float(GRID_MAX_POINTS_X - 1))
67
#define MESH_Y_DIST (float(MESH_MAX_Y - (MESH_MIN_Y)) / float(GRID_MAX_POINTS_Y - 1))
68
 
69
class unified_bed_leveling {
70
  private:
71
 
72
    static int    g29_verbose_level,
73
                  g29_phase_value,
74
                  g29_repetition_cnt,
75
                  g29_storage_slot,
76
                  g29_map_type;
77
    static bool   g29_c_flag, g29_x_flag, g29_y_flag;
78
    static float  g29_x_pos, g29_y_pos,
79
                  g29_card_thickness,
80
                  g29_constant;
81
 
82
    #if HAS_BED_PROBE
83
      static int  g29_grid_size;
84
    #endif
85
 
86
    #if ENABLED(NEWPANEL)
87
      static void move_z_with_encoder(const float &multiplier);
88
      static float measure_point_with_encoder();
89
      static float measure_business_card_thickness(float in_height);
90
      static void manually_probe_remaining_mesh(const float&, const float&, const float&, const float&, const bool) _O0;
91
      static void fine_tune_mesh(const float &rx, const float &ry, const bool do_ubl_mesh_map) _O0;
92
    #endif
93
 
94
    static bool g29_parameter_parsing() _O0;
95
    static void shift_mesh_height();
96
    static void probe_entire_mesh(const float &rx, const float &ry, const bool do_ubl_mesh_map, const bool stow_probe, bool do_furthest) _O0;
97
    static void tilt_mesh_based_on_3pts(const float &z1, const float &z2, const float &z3);
98
    static void tilt_mesh_based_on_probed_grid(const bool do_ubl_mesh_map);
99
    static void g29_what_command();
100
    static void g29_eeprom_dump();
101
    static void g29_compare_current_mesh_to_stored_mesh();
102
    static bool smart_fill_one(const uint8_t x, const uint8_t y, const int8_t xdir, const int8_t ydir);
103
    static void smart_fill_mesh();
104
 
105
  public:
106
 
107
    static void echo_name();
108
    static void report_current_mesh();
109
    static void report_state();
110
    static void save_ubl_active_state_and_disable();
111
    static void restore_ubl_active_state_and_leave();
112
    static void display_map(const int) _O0;
113
    static mesh_index_pair find_closest_mesh_point_of_type(const MeshPointType, const float&, const float&, const bool, uint16_t[16]) _O0;
114
    static mesh_index_pair find_furthest_invalid_mesh_point() _O0;
115
    static void reset();
116
    static void invalidate();
117
    static void set_all_mesh_points_to_value(const float value);
118
    static void adjust_mesh_to_mean(const bool cflag, const float value);
119
    static bool sanity_check();
120
 
121
    static void G29() _O0;                          // O0 for no optimization
122
    static void smart_fill_wlsf(const float &) _O2; // O2 gives smaller code than Os on A2560
123
 
124
    static int8_t storage_slot;
125
 
126
    static float z_values[GRID_MAX_POINTS_X][GRID_MAX_POINTS_Y];
127
 
128
    // 15 is the maximum nubmer of grid points supported + 1 safety margin for now,
129
    // until determinism prevails
130
    static constexpr float _mesh_index_to_xpos[16] PROGMEM = {
131
                              MESH_MIN_X +  0 * (MESH_X_DIST), MESH_MIN_X +  1 * (MESH_X_DIST),
132
                              MESH_MIN_X +  2 * (MESH_X_DIST), MESH_MIN_X +  3 * (MESH_X_DIST),
133
                              MESH_MIN_X +  4 * (MESH_X_DIST), MESH_MIN_X +  5 * (MESH_X_DIST),
134
                              MESH_MIN_X +  6 * (MESH_X_DIST), MESH_MIN_X +  7 * (MESH_X_DIST),
135
                              MESH_MIN_X +  8 * (MESH_X_DIST), MESH_MIN_X +  9 * (MESH_X_DIST),
136
                              MESH_MIN_X + 10 * (MESH_X_DIST), MESH_MIN_X + 11 * (MESH_X_DIST),
137
                              MESH_MIN_X + 12 * (MESH_X_DIST), MESH_MIN_X + 13 * (MESH_X_DIST),
138
                              MESH_MIN_X + 14 * (MESH_X_DIST), MESH_MIN_X + 15 * (MESH_X_DIST)
139
                            };
140
 
141
    static constexpr float _mesh_index_to_ypos[16] PROGMEM = {
142
                              MESH_MIN_Y +  0 * (MESH_Y_DIST), MESH_MIN_Y +  1 * (MESH_Y_DIST),
143
                              MESH_MIN_Y +  2 * (MESH_Y_DIST), MESH_MIN_Y +  3 * (MESH_Y_DIST),
144
                              MESH_MIN_Y +  4 * (MESH_Y_DIST), MESH_MIN_Y +  5 * (MESH_Y_DIST),
145
                              MESH_MIN_Y +  6 * (MESH_Y_DIST), MESH_MIN_Y +  7 * (MESH_Y_DIST),
146
                              MESH_MIN_Y +  8 * (MESH_Y_DIST), MESH_MIN_Y +  9 * (MESH_Y_DIST),
147
                              MESH_MIN_Y + 10 * (MESH_Y_DIST), MESH_MIN_Y + 11 * (MESH_Y_DIST),
148
                              MESH_MIN_Y + 12 * (MESH_Y_DIST), MESH_MIN_Y + 13 * (MESH_Y_DIST),
149
                              MESH_MIN_Y + 14 * (MESH_Y_DIST), MESH_MIN_Y + 15 * (MESH_Y_DIST)
150
                            };
151
 
152
    #if ENABLED(ULTIPANEL)
153
      static bool lcd_map_control;
154
    #endif
155
 
156
    static volatile int encoder_diff; // Volatile because it's changed at interrupt time.
157
 
158
    unified_bed_leveling();
159
 
160
    FORCE_INLINE static void set_z(const int8_t px, const int8_t py, const float &z) { z_values[px][py] = z; }
161
 
162
    static int8_t get_cell_index_x(const float &x) {
163
      const int8_t cx = (x - (MESH_MIN_X)) * (1.0f / (MESH_X_DIST));
164
      return constrain(cx, 0, (GRID_MAX_POINTS_X) - 1);   // -1 is appropriate if we want all movement to the X_MAX
165
    }                                                     // position. But with this defined this way, it is possible
166
                                                          // to extrapolate off of this point even further out. Probably
167
                                                          // that is OK because something else should be keeping that from
168
                                                          // happening and should not be worried about at this level.
169
    static int8_t get_cell_index_y(const float &y) {
170
      const int8_t cy = (y - (MESH_MIN_Y)) * (1.0f / (MESH_Y_DIST));
171
      return constrain(cy, 0, (GRID_MAX_POINTS_Y) - 1);   // -1 is appropriate if we want all movement to the Y_MAX
172
    }                                                     // position. But with this defined this way, it is possible
173
                                                          // to extrapolate off of this point even further out. Probably
174
                                                          // that is OK because something else should be keeping that from
175
                                                          // happening and should not be worried about at this level.
176
 
177
    static int8_t find_closest_x_index(const float &x) {
178
      const int8_t px = (x - (MESH_MIN_X) + (MESH_X_DIST) * 0.5f) * (1.0f / (MESH_X_DIST));
179
      return WITHIN(px, 0, GRID_MAX_POINTS_X - 1) ? px : -1;
180
    }
181
 
182
    static int8_t find_closest_y_index(const float &y) {
183
      const int8_t py = (y - (MESH_MIN_Y) + (MESH_Y_DIST) * 0.5f) * (1.0f / (MESH_Y_DIST));
184
      return WITHIN(py, 0, GRID_MAX_POINTS_Y - 1) ? py : -1;
185
    }
186
 
187
    /**
188
     *                           z2   --|
189
     *                 z0        |      |
190
     *                  |        |      + (z2-z1)
191
     *   z1             |        |      |
192
     * ---+-------------+--------+--  --|
193
     *   a1            a0        a2
194
     *    |<---delta_a---------->|
195
     *
196
     *  calc_z0 is the basis for all the Mesh Based correction. It is used to
197
     *  find the expected Z Height at a position between two known Z-Height locations.
198
     *
199
     *  It is fairly expensive with its 4 floating point additions and 2 floating point
200
     *  multiplications.
201
     */
202
    FORCE_INLINE static float calc_z0(const float &a0, const float &a1, const float &z1, const float &a2, const float &z2) {
203
      return z1 + (z2 - z1) * (a0 - a1) / (a2 - a1);
204
    }
205
 
206
    /**
207
     * z_correction_for_x_on_horizontal_mesh_line is an optimization for
208
     * the case where the printer is making a vertical line that only crosses horizontal mesh lines.
209
     */
210
    inline static float z_correction_for_x_on_horizontal_mesh_line(const float &rx0, const int x1_i, const int yi) {
211
      if (!WITHIN(x1_i, 0, GRID_MAX_POINTS_X - 1) || !WITHIN(yi, 0, GRID_MAX_POINTS_Y - 1)) {
212
        #if ENABLED(DEBUG_LEVELING_FEATURE)
213
          if (DEBUGGING(LEVELING)) {
214
            serialprintPGM( !WITHIN(x1_i, 0, GRID_MAX_POINTS_X - 1) ? PSTR("x1_i") : PSTR("yi") );
215
            SERIAL_ECHOPAIR(" out of bounds in z_correction_for_x_on_horizontal_mesh_line(rx0=", rx0);
216
            SERIAL_ECHOPAIR(",x1_i=", x1_i);
217
            SERIAL_ECHOPAIR(",yi=", yi);
218
            SERIAL_CHAR(')');
219
            SERIAL_EOL();
220
          }
221
        #endif
222
 
223
        // The requested location is off the mesh. Return UBL_Z_RAISE_WHEN_OFF_MESH or NAN.
224
        return (
225
          #ifdef UBL_Z_RAISE_WHEN_OFF_MESH
226
            UBL_Z_RAISE_WHEN_OFF_MESH
227
          #else
228
            NAN
229
          #endif
230
        );
231
      }
232
 
233
      const float xratio = (rx0 - mesh_index_to_xpos(x1_i)) * (1.0 / (MESH_X_DIST)),
234
                  z1 = z_values[x1_i][yi];
235
 
236
      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
237
                                                                                      // If it is, it is clamped to the last element of the
238
                                                                                      // z_values[][] array and no correction is applied.
239
    }
240
 
241
    //
242
    // See comments above for z_correction_for_x_on_horizontal_mesh_line
243
    //
244
    inline static float z_correction_for_y_on_vertical_mesh_line(const float &ry0, const int xi, const int y1_i) {
245
      if (!WITHIN(xi, 0, GRID_MAX_POINTS_X - 1) || !WITHIN(y1_i, 0, GRID_MAX_POINTS_Y - 1)) {
246
        #if ENABLED(DEBUG_LEVELING_FEATURE)
247
          if (DEBUGGING(LEVELING)) {
248
            serialprintPGM( !WITHIN(xi, 0, GRID_MAX_POINTS_X - 1) ? PSTR("xi") : PSTR("y1_i") );
249
            SERIAL_ECHOPAIR(" out of bounds in z_correction_for_y_on_vertical_mesh_line(ry0=", ry0);
250
            SERIAL_ECHOPAIR(", xi=", xi);
251
            SERIAL_ECHOPAIR(", y1_i=", y1_i);
252
            SERIAL_CHAR(')');
253
            SERIAL_EOL();
254
          }
255
        #endif
256
 
257
        // The requested location is off the mesh. Return UBL_Z_RAISE_WHEN_OFF_MESH or NAN.
258
        return (
259
          #ifdef UBL_Z_RAISE_WHEN_OFF_MESH
260
            UBL_Z_RAISE_WHEN_OFF_MESH
261
          #else
262
            NAN
263
          #endif
264
        );
265
      }
266
 
267
      const float yratio = (ry0 - mesh_index_to_ypos(y1_i)) * (1.0 / (MESH_Y_DIST)),
268
                  z1 = z_values[xi][y1_i];
269
 
270
      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
271
                                                                                      // If it is, it is clamped to the last element of the
272
                                                                                      // z_values[][] array and no correction is applied.
273
    }
274
 
275
    /**
276
     * This is the generic Z-Correction. It works anywhere within a Mesh Cell. It first
277
     * does a linear interpolation along both of the bounding X-Mesh-Lines to find the
278
     * Z-Height at both ends. Then it does a linear interpolation of these heights based
279
     * on the Y position within the cell.
280
     */
281
    static float get_z_correction(const float &rx0, const float &ry0) {
282
      const int8_t cx = get_cell_index_x(rx0),
283
                   cy = get_cell_index_y(ry0); // return values are clamped
284
 
285
      /**
286
       * Check if the requested location is off the mesh.  If so, and
287
       * UBL_Z_RAISE_WHEN_OFF_MESH is specified, that value is returned.
288
       */
289
      #ifdef UBL_Z_RAISE_WHEN_OFF_MESH
290
        if (!WITHIN(rx0, MESH_MIN_X, MESH_MAX_X) || !WITHIN(ry0, MESH_MIN_Y, MESH_MAX_Y))
291
          return UBL_Z_RAISE_WHEN_OFF_MESH;
292
      #endif
293
 
294
      const float z1 = calc_z0(rx0,
295
                               mesh_index_to_xpos(cx), z_values[cx][cy],
296
                               mesh_index_to_xpos(cx + 1), z_values[MIN(cx, GRID_MAX_POINTS_X - 2) + 1][cy]);
297
 
298
      const float z2 = calc_z0(rx0,
299
                               mesh_index_to_xpos(cx), z_values[cx][MIN(cy, GRID_MAX_POINTS_Y - 2) + 1],
300
                               mesh_index_to_xpos(cx + 1), z_values[MIN(cx, GRID_MAX_POINTS_X - 2) + 1][MIN(cy, GRID_MAX_POINTS_Y - 2) + 1]);
301
 
302
      float z0 = calc_z0(ry0,
303
                         mesh_index_to_ypos(cy), z1,
304
                         mesh_index_to_ypos(cy + 1), z2);
305
 
306
      #if ENABLED(DEBUG_LEVELING_FEATURE)
307
        if (DEBUGGING(MESH_ADJUST)) {
308
          SERIAL_ECHOPAIR(" raw get_z_correction(", rx0);
309
          SERIAL_CHAR(',');
310
          SERIAL_ECHO(ry0);
311
          SERIAL_ECHOPGM(") = ");
312
          SERIAL_ECHO_F(z0, 6);
313
        }
314
      #endif
315
 
316
      #if ENABLED(DEBUG_LEVELING_FEATURE)
317
        if (DEBUGGING(MESH_ADJUST)) {
318
          SERIAL_ECHOPGM(" >>>---> ");
319
          SERIAL_ECHO_F(z0, 6);
320
          SERIAL_EOL();
321
        }
322
      #endif
323
 
324
      if (isnan(z0)) { // if part of the Mesh is undefined, it will show up as NAN
325
        z0 = 0.0;      // in ubl.z_values[][] and propagate through the
326
                       // calculations. If our correction is NAN, we throw it out
327
                       // because part of the Mesh is undefined and we don't have the
328
                       // information we need to complete the height correction.
329
 
330
        #if ENABLED(DEBUG_LEVELING_FEATURE)
331
          if (DEBUGGING(MESH_ADJUST)) {
332
            SERIAL_ECHOPAIR("??? Yikes!  NAN in get_z_correction(", rx0);
333
            SERIAL_CHAR(',');
334
            SERIAL_ECHO(ry0);
335
            SERIAL_CHAR(')');
336
            SERIAL_EOL();
337
          }
338
        #endif
339
      }
340
      return z0;
341
    }
342
 
343
    FORCE_INLINE static float mesh_index_to_xpos(const uint8_t i) {
344
      return i < GRID_MAX_POINTS_X ? pgm_read_float(&_mesh_index_to_xpos[i]) : MESH_MIN_X + i * (MESH_X_DIST);
345
    }
346
 
347
    FORCE_INLINE static float mesh_index_to_ypos(const uint8_t i) {
348
      return i < GRID_MAX_POINTS_Y ? pgm_read_float(&_mesh_index_to_ypos[i]) : MESH_MIN_Y + i * (MESH_Y_DIST);
349
    }
350
 
351
    #if UBL_SEGMENTED
352
      static bool prepare_segmented_line_to(const float (&rtarget)[XYZE], const float &feedrate);
353
    #else
354
      static void line_to_destination_cartesian(const float &fr, const uint8_t e);
355
    #endif
356
 
357
    inline static bool mesh_is_valid() {
358
      for (uint8_t x = 0; x < GRID_MAX_POINTS_X; x++)
359
        for (uint8_t y = 0; y < GRID_MAX_POINTS_Y; y++)
360
          if (isnan(z_values[x][y])) return false;
361
      return true;
362
    }
363
 
364
}; // class unified_bed_leveling
365
 
366
extern unified_bed_leveling ubl;
367
 
368
FORCE_INLINE void gcode_G29() { ubl.G29(); }
369
 
370
#endif // UNIFIED_BED_LEVELING_H