Browse Source

UBL core and support files

Scott Lahteine 7 years ago
parent
commit
238b8fd2a3

+ 1001
- 0
Marlin/G26_Mesh_Validation_Tool.cpp
File diff suppressed because it is too large
View File


+ 331
- 0
Marlin/UBL.h View File

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
+#include "Marlin.h"
24
+#include "math.h"
25
+
26
+#ifndef UNIFIED_BED_LEVELING_H
27
+#define UNIFIED_BED_LEVELING_H
28
+
29
+  #if ENABLED(AUTO_BED_LEVELING_UBL)
30
+
31
+    #define UBL_OK false
32
+    #define UBL_ERR true
33
+
34
+    typedef struct {
35
+      int x_index, y_index;
36
+      float distance; // Not always used. But when populated, it is the distance
37
+                      // from the search location
38
+    } mesh_index_pair;
39
+
40
+    struct vector { double dx, dy, dz; };
41
+
42
+    enum Mesh_Point_Type { INVALID, REAL, SET_IN_BITMAP };
43
+
44
+    bool axis_unhomed_error(bool, bool, bool);
45
+    void dump(char *str, float f);
46
+    bool G29_lcd_clicked();
47
+    void probe_entire_mesh(float, float, bool, bool);
48
+    void UBL_line_to_destination(const float&, const float&, const float&, const float&, const float&, uint8_t);
49
+    void manually_probe_remaining_mesh(float, float, float, float, bool);
50
+    struct vector tilt_mesh_based_on_3pts(float, float, float);
51
+    void new_set_bed_level_equation_3pts(float, float, float);
52
+    float measure_business_card_thickness(float);
53
+    mesh_index_pair find_closest_mesh_point_of_type(Mesh_Point_Type, float, float, bool, unsigned int[16]);
54
+    void Find_Mean_Mesh_Height();
55
+    void Shift_Mesh_Height();
56
+    bool G29_Parameter_Parsing();
57
+    void G29_What_Command();
58
+    void G29_EEPROM_Dump();
59
+    void G29_Kompare_Current_Mesh_to_Stored_Mesh();
60
+    void fine_tune_mesh(float, float, float, bool);
61
+    void bit_clear(uint16_t bits[16], uint8_t x, uint8_t y);
62
+    void bit_set(uint16_t bits[16], uint8_t x, uint8_t y);
63
+    bool is_bit_set(uint16_t bits[16], uint8_t x, uint8_t y);
64
+    char *ftostr43sign(const float&, char);
65
+
66
+    void gcode_G26();
67
+    void gcode_G28();
68
+    void gcode_G29();
69
+    extern char conv[9];
70
+
71
+    void save_UBL_active_state_and_disable();
72
+    void restore_UBL_active_state_and_leave();
73
+
74
+    ///////////////////////////////////////////////////////////////////////////////////////////////////////
75
+
76
+    #if ENABLED(ULTRA_LCD)
77
+      extern char lcd_status_message[];
78
+      void lcd_quick_feedback();
79
+    #endif
80
+
81
+    enum MBLStatus { MBL_STATUS_NONE = 0, MBL_STATUS_HAS_MESH_BIT = 0, MBL_STATUS_ACTIVE_BIT = 1 };
82
+
83
+    #define MESH_X_DIST ((float(UBL_MESH_MAX_X) - float(UBL_MESH_MIN_X)) / (float(UBL_MESH_NUM_X_POINTS) - 1.0))
84
+    #define MESH_Y_DIST ((float(UBL_MESH_MAX_Y) - float(UBL_MESH_MIN_Y)) / (float(UBL_MESH_NUM_Y_POINTS) - 1.0))
85
+
86
+    extern bool G26_Debug_flag;
87
+    extern float last_specified_z;
88
+    extern float fade_scaling_factor_for_current_height;
89
+    extern float z_values[UBL_MESH_NUM_X_POINTS][UBL_MESH_NUM_Y_POINTS];
90
+    extern float mesh_index_to_X_location[UBL_MESH_NUM_X_POINTS + 1]; // +1 just because of paranoia that we might end up on the
91
+    extern float mesh_index_to_Y_location[UBL_MESH_NUM_Y_POINTS + 1]; // the last Mesh Line and that is the start of a whole new cell
92
+
93
+    class bed_leveling {
94
+      public:
95
+      struct ubl_state {
96
+        bool active = false;
97
+        float z_offset = 0.0;
98
+        int EEPROM_storage_slot = -1,
99
+            n_x = UBL_MESH_NUM_X_POINTS,
100
+            n_y = UBL_MESH_NUM_Y_POINTS;
101
+        float mesh_x_min = UBL_MESH_MIN_X,
102
+              mesh_y_min = UBL_MESH_MIN_Y,
103
+              mesh_x_max = UBL_MESH_MAX_X,
104
+              mesh_y_max = UBL_MESH_MAX_Y,
105
+              mesh_x_dist = MESH_X_DIST,
106
+              mesh_y_dist = MESH_Y_DIST,
107
+              G29_Correction_Fade_Height = 10.0,
108
+              G29_Fade_Height_Multiplier = 1.0 / 10.0; // It is cheaper to do a floating point multiply than a floating
109
+                                                       // point divide. So, we keep this number in both forms. The first
110
+                                                       // is for the user. The second one is the one that is actually used
111
+                                                       // again and again and again during the correction calculations.
112
+
113
+        unsigned char padding[24];  // This is just to allow room to add state variables without
114
+                                    // changing the location of data structures in the EEPROM.
115
+                                    // This is for compatability with future versions to keep
116
+                                    // people from having to regenerate thier mesh data.
117
+                                    //
118
+                                    // If you change the contents of this struct, please adjust
119
+                                    // the padding[] to keep the size the same!
120
+      } state, pre_initialized;
121
+
122
+      bed_leveling();
123
+      //  ~bed_leveling();  // No destructor because this object never goes away!
124
+
125
+      void display_map(int);
126
+
127
+      void reset();
128
+      void invalidate();
129
+
130
+      void store_state();
131
+      void load_state();
132
+      void store_mesh(int);
133
+      void load_mesh(int);
134
+
135
+      bool sanity_check();
136
+
137
+      FORCE_INLINE float map_x_index_to_bed_location(int8_t i){ return ((float) UBL_MESH_MIN_X) + (((float) MESH_X_DIST) * (float) i); };
138
+      FORCE_INLINE float map_y_index_to_bed_location(int8_t i){ return ((float) UBL_MESH_MIN_Y) + (((float) MESH_Y_DIST) * (float) i); };
139
+
140
+      void set_z(const int8_t px, const int8_t py, const float z) { z_values[px][py] = z; }
141
+
142
+      int8_t get_cell_index_x(float x) {
143
+        int8_t cx = (x - (UBL_MESH_MIN_X)) * (1.0 / (MESH_X_DIST));
144
+        return constrain(cx, 0, (UBL_MESH_NUM_X_POINTS) - 1);   // -1 is appropriate if we want all movement to the X_MAX
145
+      }                                                         // position. But with this defined this way, it is possible
146
+                                                                // to extrapolate off of this point even further out. Probably
147
+                                                                // that is OK because something else should be keeping that from
148
+                                                                // happening and should not be worried about at this level.
149
+      int8_t get_cell_index_y(float y) {
150
+        int8_t cy = (y - (UBL_MESH_MIN_Y)) * (1.0 / (MESH_Y_DIST));
151
+        return constrain(cy, 0, (UBL_MESH_NUM_Y_POINTS) - 1);   // -1 is appropriate if we want all movement to the Y_MAX
152
+      }                                                         // position. But with this defined this way, it is possible
153
+                                                                // to extrapolate off of this point even further out. Probably
154
+                                                                // that is OK because something else should be keeping that from
155
+                                                                // happening and should not be worried about at this level.
156
+
157
+      int8_t find_closest_x_index(float x) {
158
+        int8_t px = (x - (UBL_MESH_MIN_X) + (MESH_X_DIST) * 0.5) * (1.0 / (MESH_X_DIST));
159
+        return (px >= 0 && px < (UBL_MESH_NUM_X_POINTS)) ? px : -1;
160
+      }
161
+
162
+      int8_t find_closest_y_index(float y) {
163
+        int8_t py = (y - (UBL_MESH_MIN_Y) + (MESH_Y_DIST) * 0.5) * (1.0 / (MESH_Y_DIST));
164
+        return (py >= 0 && py < (UBL_MESH_NUM_Y_POINTS)) ? py : -1;
165
+      }
166
+
167
+      /**
168
+       *                           z2   --|
169
+       *                 z0        |      |
170
+       *                  |        |      + (z2-z1)
171
+       *   z1             |        |      |
172
+       * ---+-------------+--------+--  --|
173
+       *   a1            a0        a2
174
+       *    |<---delta_a---------->|
175
+       *
176
+       *  calc_z0 is the basis for all the Mesh Based correction. It is used to
177
+       *  find the expected Z Height at a position between two known Z-Height locations
178
+       *
179
+       *  It is farly expensive with its 4 floating point additions and 2 floating point
180
+       *  multiplications.
181
+       */
182
+      inline float calc_z0(float a0, float a1, float z1, float a2, float z2) {
183
+        float delta_z = (z2 - z1);
184
+        float delta_a = (a0 - a1) / (a2 - a1);
185
+        return z1 + delta_a * delta_z;
186
+      }
187
+
188
+      /**
189
+       * get_z_correction_at_Y_intercept(float x0, int x1_i, int yi) only takes
190
+       * three parameters. It assumes the x0 point is on a Mesh line denoted by yi. In theory
191
+       * we could use get_cell_index_x(float x) to obtain the 2nd parameter x1_i but any code calling
192
+       * the get_z_correction_along_vertical_mesh_line_at_specific_X routine  will already have
193
+       * the X index of the x0 intersection available and we don't want to perform any extra floating
194
+       * point operations.
195
+       */
196
+      inline float get_z_correction_along_horizontal_mesh_line_at_specific_X(float x0, int x1_i, int yi) {
197
+        if (x1_i < 0 || yi < 0 || x1_i >= UBL_MESH_NUM_X_POINTS || yi >= UBL_MESH_NUM_Y_POINTS) {
198
+          SERIAL_ECHOPAIR("? in get_z_correction_along_horizontal_mesh_line_at_specific_X(x0=", x0);
199
+          SERIAL_ECHOPAIR(",x1_i=", x1_i);
200
+          SERIAL_ECHOPAIR(",yi=", yi);
201
+          SERIAL_CHAR(')');
202
+          SERIAL_EOL;
203
+          return NAN;
204
+        }
205
+
206
+        const float a0ma1diva2ma1 = (x0 - mesh_index_to_X_location[x1_i]) * (1.0 / (MESH_X_DIST)),
207
+                    z1 = z_values[x1_i][yi],
208
+                    z2 = z_values[x1_i + 1][yi],
209
+                    dz = (z2 - z1);
210
+
211
+        return z1 + a0ma1diva2ma1 * dz;
212
+      }
213
+
214
+      //
215
+      // See comments above for get_z_correction_along_horizontal_mesh_line_at_specific_X
216
+      //
217
+      inline float get_z_correction_along_vertical_mesh_line_at_specific_Y(float y0, int xi, int y1_i) {
218
+        if (xi < 0 || y1_i < 0 || xi >= UBL_MESH_NUM_X_POINTS || y1_i >= UBL_MESH_NUM_Y_POINTS) {
219
+          SERIAL_ECHOPAIR("? in get_z_correction_along_vertical_mesh_line_at_specific_X(y0=", y0);
220
+          SERIAL_ECHOPAIR(", x1_i=", xi);
221
+          SERIAL_ECHOPAIR(", yi=", y1_i);
222
+          SERIAL_CHAR(')');
223
+          SERIAL_EOL;
224
+          return NAN;
225
+        }
226
+
227
+        const float a0ma1diva2ma1 = (y0 - mesh_index_to_Y_location[y1_i]) * (1.0 / (MESH_Y_DIST)),
228
+                    z1 = z_values[xi][y1_i],
229
+                    z2 = z_values[xi][y1_i + 1],
230
+                    dz = (z2 - z1);
231
+
232
+        return z1 + a0ma1diva2ma1 * dz;
233
+      }
234
+
235
+      /**
236
+       * This is the generic Z-Correction. It works anywhere within a Mesh Cell. It first
237
+       * does a linear interpolation along both of the bounding X-Mesh-Lines to find the
238
+       * Z-Height at both ends. Then it does a linear interpolation of these heights based
239
+       * on the Y position within the cell.
240
+       */
241
+      float get_z_correction(float x0, float y0) {
242
+        int8_t cx = get_cell_index_x(x0),
243
+        cy = get_cell_index_y(y0);
244
+
245
+        if (cx < 0 || cy < 0 || cx >= UBL_MESH_NUM_X_POINTS || cy >= UBL_MESH_NUM_Y_POINTS) {
246
+
247
+          SERIAL_ECHOPAIR("? in get_z_correction(x0=", x0);
248
+          SERIAL_ECHOPAIR(", y0=", y0);
249
+          SERIAL_CHAR(')');
250
+          SERIAL_EOL;
251
+
252
+          #if ENABLED(ULTRA_LCD)
253
+            strcpy(lcd_status_message, "get_z_correction() indexes out of range.");
254
+            lcd_quick_feedback();
255
+          #endif
256
+          return 0.0; // this used to return state.z_offset
257
+        }
258
+
259
+        float z1 = calc_z0(x0,
260
+          map_x_index_to_bed_location(cx), z_values[cx][cy],
261
+          map_x_index_to_bed_location(cx + 1), z_values[cx + 1][cy]);
262
+        float z2 = calc_z0(x0,
263
+          map_x_index_to_bed_location(cx), z_values[cx][cy + 1],
264
+          map_x_index_to_bed_location(cx + 1), z_values[cx + 1][cy + 1]);
265
+        float z0 = calc_z0(y0,
266
+          map_y_index_to_bed_location(cy), z1,
267
+          map_y_index_to_bed_location(cy + 1), z2);
268
+
269
+        #if ENABLED(DEBUG_LEVELING_FEATURE)
270
+          if (DEBUGGING(MESH_ADJUST)) {
271
+            SERIAL_ECHOPAIR(" raw get_z_correction(", x0);
272
+            SERIAL_ECHOPAIR(",", y0);
273
+            SERIAL_ECHOPGM(")=");
274
+            SERIAL_PROTOCOL_F(z0, 6);
275
+          }
276
+        #endif
277
+
278
+        #if ENABLED(DEBUG_LEVELING_FEATURE)
279
+          if (DEBUGGING(MESH_ADJUST)) {
280
+            SERIAL_ECHOPGM(" >>>---> ");
281
+            SERIAL_PROTOCOL_F(z0, 6);
282
+            SERIAL_EOL;
283
+          }
284
+        #endif
285
+
286
+        if (isnan(z0)) { // if part of the Mesh is undefined, it will show up as NAN
287
+          z0 = 0.0;      // in blm.z_values[][] and propagate through the
288
+                         // calculations. If our correction is NAN, we throw it out
289
+                         // because part of the Mesh is undefined and we don't have the
290
+                         // information we need to complete the height correction.
291
+
292
+          #if ENABLED(DEBUG_LEVELING_FEATURE)
293
+            if (DEBUGGING(MESH_ADJUST)) {
294
+              SERIAL_ECHOPGM("??? Yikes!  NAN in get_z_correction( ");
295
+              SERIAL_ECHO(x0);
296
+              SERIAL_ECHOPGM(", ");
297
+              SERIAL_ECHO(y0);
298
+              SERIAL_ECHOLNPGM(" )");
299
+            }
300
+          #endif
301
+        }
302
+        return z0; // there used to be a +state.z_offset on this line
303
+      }
304
+
305
+      /**
306
+       * This routine is used to scale the Z correction depending upon the current nozzle height. It is
307
+       * optimized for speed. It avoids floating point operations by checking if the requested scaling
308
+       * factor is going to be the same as the last time the function calculated a value. If so, it just
309
+       * returns it.
310
+       *
311
+       * If it must do a calcuation, it will return a scaling factor of 0.0 if the UBL System is not active
312
+       * or if the current Z Height is past the specified 'Fade Height'
313
+       */
314
+      FORCE_INLINE float fade_scaling_factor_for_Z(float current_z) {
315
+        if (last_specified_z == current_z)
316
+          return fade_scaling_factor_for_current_height;
317
+
318
+        last_specified_z = current_z;
319
+        fade_scaling_factor_for_current_height =
320
+          state.active && current_z < state.G29_Correction_Fade_Height
321
+          ? 1.0 - (current_z * state.G29_Fade_Height_Multiplier)
322
+          : 0.0;
323
+        return fade_scaling_factor_for_current_height;
324
+      }
325
+    };
326
+
327
+    extern bed_leveling blm;
328
+    extern int Unified_Bed_Leveling_EEPROM_start;
329
+
330
+#endif // AUTO_BED_LEVELING_UBL
331
+#endif // UNIFIED_BED_LEVELING_H

+ 296
- 0
Marlin/UBL_Bed_Leveling.cpp View File

1
+/**
2
+ * Marlin 3D Printer Firmware
3
+ * Copyright (C) 2016 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
+#include "Marlin.h"
24
+#include "math.h"
25
+
26
+#if ENABLED(AUTO_BED_LEVELING_UBL)
27
+  #include "UBL.h"
28
+  #include "hex_print_routines.h"
29
+
30
+  /**
31
+   * These variables used to be declared inside the bed_leveling class.  We are going to still declare
32
+   * them within the .cpp file for bed leveling.   But there is only one instance of the bed leveling
33
+   * object and we can get rid of a level of inderection by not making them 'member data'.  So, in the
34
+   * interest of speed, we do it this way.    When we move to a 32-Bit processor, they can be moved
35
+   * back inside the bed leveling class.
36
+   */
37
+  float last_specified_z,
38
+        fade_scaling_factor_for_current_height,
39
+        z_values[UBL_MESH_NUM_X_POINTS][UBL_MESH_NUM_Y_POINTS],
40
+        mesh_index_to_X_location[UBL_MESH_NUM_X_POINTS + 1], // +1 just because of paranoia that we might end up on the
41
+        mesh_index_to_Y_location[UBL_MESH_NUM_Y_POINTS + 1]; // the last Mesh Line and that is the start of a whole new cell
42
+
43
+  bed_leveling::bed_leveling() {
44
+    for (uint8_t i = 0; i <= UBL_MESH_NUM_X_POINTS; i++)  // We go one past what we expect to ever need for safety
45
+      mesh_index_to_X_location[i] = double(UBL_MESH_MIN_X) + double(MESH_X_DIST) * double(i);
46
+
47
+    for (uint8_t i = 0; i <= UBL_MESH_NUM_Y_POINTS; i++)  // We go one past what we expect to ever need for safety
48
+      mesh_index_to_Y_location[i] = double(UBL_MESH_MIN_Y) + double(MESH_Y_DIST) * double(i);
49
+
50
+    reset();
51
+  }
52
+
53
+  void bed_leveling::store_state() {
54
+    int k = E2END - sizeof(blm.state);
55
+    eeprom_write_block((void *)&blm.state, (void *)k, sizeof(blm.state));
56
+  }
57
+
58
+  void bed_leveling::load_state() {
59
+    int k = E2END - sizeof(blm.state);
60
+    eeprom_read_block((void *)&blm.state, (void *)k, sizeof(blm.state));
61
+
62
+    if (sanity_check())
63
+      SERIAL_PROTOCOLLNPGM("?In load_state() sanity_check() failed.\n");
64
+
65
+    // These lines can go away in a few weeks.  They are just
66
+    // to make sure people updating thier firmware won't be using
67
+    if (blm.state.G29_Fade_Height_Multiplier != 1.0 / blm.state.G29_Correction_Fade_Height) { // an incomplete Bed_Leveling.state structure. For speed
68
+      blm.state.G29_Fade_Height_Multiplier = 1.0 / blm.state.G29_Correction_Fade_Height;      // we now multiply by the inverse of the Fade Height instead of
69
+      store_state();   // dividing by it. Soon... all of the old structures will be
70
+    }                  // updated, but until then, we try to ease the transition
71
+                       // for our Beta testers.
72
+  }
73
+
74
+  void bed_leveling::load_mesh(int m) {
75
+    int k = E2END - sizeof(blm.state),
76
+        j = (k - Unified_Bed_Leveling_EEPROM_start) / sizeof(z_values);
77
+
78
+    if (m == -1) {
79
+      SERIAL_PROTOCOLLNPGM("?No mesh saved in EEPROM. Zeroing mesh in memory.\n");
80
+      reset();
81
+      return;
82
+    }
83
+
84
+    if (m < 0 || m >= j || Unified_Bed_Leveling_EEPROM_start <= 0) {
85
+      SERIAL_PROTOCOLLNPGM("?EEPROM storage not available to load mesh.\n");
86
+      return;
87
+    }
88
+
89
+    j = k - (m + 1) * sizeof(z_values);
90
+    eeprom_read_block((void *)&z_values , (void *)j, sizeof(z_values));
91
+
92
+    SERIAL_PROTOCOLPGM("Mesh loaded from slot ");
93
+    SERIAL_PROTOCOL(m);
94
+    SERIAL_PROTOCOLPGM("  at offset 0x");
95
+    prt_hex_word(j);
96
+    SERIAL_EOL;
97
+  }
98
+
99
+  void bed_leveling:: store_mesh(int m) {
100
+    int k = E2END - sizeof(state),
101
+        j = (k - Unified_Bed_Leveling_EEPROM_start) / sizeof(z_values);
102
+
103
+    if (m < 0 || m >= j || Unified_Bed_Leveling_EEPROM_start <= 0) {
104
+      SERIAL_PROTOCOLLNPGM("?EEPROM storage not available to load mesh.\n");
105
+      SERIAL_PROTOCOL(m);
106
+      SERIAL_PROTOCOLLNPGM(" mesh slots available.\n");
107
+      SERIAL_PROTOCOLLNPAIR("E2END     : ", E2END);
108
+      SERIAL_PROTOCOLLNPAIR("k         : ", k);
109
+      SERIAL_PROTOCOLLNPAIR("j         : ", j);
110
+      SERIAL_PROTOCOLLNPAIR("m         : ", m);
111
+      SERIAL_EOL;
112
+      return;
113
+    }
114
+
115
+    j = k - (m + 1) * sizeof(z_values);
116
+    eeprom_write_block((const void *)&z_values, (void *)j, sizeof(z_values));
117
+
118
+    SERIAL_PROTOCOLPGM("Mesh saved in slot ");
119
+    SERIAL_PROTOCOL(m);
120
+    SERIAL_PROTOCOLPGM("  at offset 0x");
121
+    prt_hex_word(j);
122
+    SERIAL_EOL;
123
+  }
124
+
125
+  void bed_leveling::reset() {
126
+    state.active = false;
127
+    state.z_offset = 0;
128
+    state.EEPROM_storage_slot = -1;
129
+
130
+    ZERO(z_values);
131
+
132
+    last_specified_z = -999.9;        // We can't pre-initialize these values in the declaration
133
+    fade_scaling_factor_for_current_height = 0.0; // due to C++11 constraints
134
+  }
135
+
136
+  void bed_leveling::invalidate() {
137
+    prt_hex_word((unsigned int)this);
138
+    SERIAL_EOL;
139
+
140
+    state.active = false;
141
+    state.z_offset = 0;
142
+    for (int x = 0; x < UBL_MESH_NUM_X_POINTS; x++)
143
+      for (int y = 0; y < UBL_MESH_NUM_Y_POINTS; y++)
144
+        z_values[x][y] = NAN;
145
+  }
146
+
147
+  void bed_leveling::display_map(int map_type) {
148
+    float f, current_xi, current_yi;
149
+    int8_t i, j;
150
+    UNUSED(map_type);
151
+
152
+    SERIAL_PROTOCOLLNPGM("\nBed Topography Report:\n");
153
+
154
+    SERIAL_ECHOPAIR("(", 0);
155
+    SERIAL_ECHOPAIR(", ", UBL_MESH_NUM_Y_POINTS - 1);
156
+    SERIAL_ECHOPGM(")    ");
157
+
158
+    current_xi = blm.get_cell_index_x(current_position[X_AXIS] + (MESH_X_DIST) / 2.0);
159
+    current_yi = blm.get_cell_index_y(current_position[Y_AXIS] + (MESH_Y_DIST) / 2.0);
160
+
161
+    for (i = 0; i < UBL_MESH_NUM_X_POINTS - 1; i++)
162
+      SERIAL_ECHOPGM("                 ");
163
+
164
+    SERIAL_ECHOPAIR("(", UBL_MESH_NUM_X_POINTS - 1);
165
+    SERIAL_ECHOPAIR(",", UBL_MESH_NUM_Y_POINTS - 1);
166
+    SERIAL_ECHOLNPGM(")");
167
+
168
+    //  if (map_type || 1) {
169
+    SERIAL_ECHOPAIR("(", UBL_MESH_MIN_X);
170
+    SERIAL_ECHOPAIR(",", UBL_MESH_MAX_Y);
171
+    SERIAL_CHAR(')');
172
+
173
+    for (i = 0; i < UBL_MESH_NUM_X_POINTS - 1; i++)
174
+      SERIAL_ECHOPGM("                 ");
175
+
176
+    SERIAL_ECHOPAIR("(", UBL_MESH_MAX_X);
177
+    SERIAL_ECHOPAIR(",", UBL_MESH_MAX_Y);
178
+    SERIAL_ECHOLNPGM(")");
179
+    //  }
180
+
181
+    for (j = UBL_MESH_NUM_Y_POINTS - 1; j >= 0; j--) {
182
+      for (i = 0; i < UBL_MESH_NUM_X_POINTS; i++) {
183
+        f = z_values[i][j];
184
+
185
+        // is the nozzle here?  if so, mark the number
186
+        SERIAL_CHAR(i == current_xi && j == current_yi ? '[' : ' ');
187
+
188
+        if (isnan(f))
189
+          SERIAL_PROTOCOLPGM("      .       ");
190
+        else {
191
+          // if we don't do this, the columns won't line up nicely
192
+          if (f >= 0.0) SERIAL_CHAR(' ');
193
+          SERIAL_PROTOCOL_F(f, 5);
194
+          idle();
195
+        }
196
+        if (i == current_xi && j == current_yi) // is the nozzle here? if so, finish marking the number
197
+          SERIAL_CHAR(']');
198
+        else
199
+          SERIAL_PROTOCOL("  ");
200
+
201
+        SERIAL_CHAR(' ');
202
+      }
203
+      SERIAL_EOL;
204
+      if (j) { // we want the (0,0) up tight against the block of numbers
205
+        SERIAL_CHAR(' ');
206
+        SERIAL_EOL;
207
+      }
208
+    }
209
+
210
+    //  if (map_type) {
211
+    SERIAL_ECHOPAIR("(", int(UBL_MESH_MIN_X));
212
+    SERIAL_ECHOPAIR(",", int(UBL_MESH_MIN_Y));
213
+    SERIAL_ECHOPGM(")    ");
214
+
215
+    for (i = 0; i < UBL_MESH_NUM_X_POINTS - 1; i++)
216
+      SERIAL_ECHOPGM("                 ");
217
+
218
+    SERIAL_ECHOPAIR("(", int(UBL_MESH_MAX_X));
219
+    SERIAL_ECHOPAIR(",", int(UBL_MESH_MIN_Y));
220
+    SERIAL_CHAR(')');
221
+    //  }
222
+
223
+    SERIAL_ECHOPAIR("(", 0);
224
+    SERIAL_ECHOPAIR(",", 0);
225
+    SERIAL_ECHOPGM(")       ");
226
+
227
+    for (i = 0; i < UBL_MESH_NUM_X_POINTS - 1; i++)
228
+      SERIAL_ECHOPGM("                 ");
229
+
230
+    SERIAL_ECHOPAIR("(", UBL_MESH_NUM_X_POINTS-1);
231
+    SERIAL_ECHOPAIR(",", 0);
232
+    SERIAL_CHAR(')');
233
+
234
+    SERIAL_CHAR(' ');
235
+    SERIAL_EOL;
236
+  }
237
+
238
+  bool bed_leveling::sanity_check() {
239
+    uint8_t error_flag = 0;
240
+
241
+    if (state.n_x !=  UBL_MESH_NUM_X_POINTS)  {
242
+      SERIAL_PROTOCOLLNPGM("?UBL_MESH_NUM_X_POINTS set wrong\n");
243
+      error_flag++;
244
+    }
245
+
246
+    if (state.n_y !=  UBL_MESH_NUM_Y_POINTS)  {
247
+      SERIAL_PROTOCOLLNPGM("?UBL_MESH_NUM_Y_POINTS set wrong\n");
248
+      error_flag++;
249
+    }
250
+
251
+    if (state.mesh_x_min !=  UBL_MESH_MIN_X)  {
252
+      SERIAL_PROTOCOLLNPGM("?UBL_MESH_MIN_X set wrong\n");
253
+      error_flag++;
254
+    }
255
+
256
+    if (state.mesh_y_min !=  UBL_MESH_MIN_Y)  {
257
+      SERIAL_PROTOCOLLNPGM("?UBL_MESH_MIN_Y set wrong\n");
258
+      error_flag++;
259
+    }
260
+
261
+    if (state.mesh_x_max !=  UBL_MESH_MAX_X)  {
262
+      SERIAL_PROTOCOLLNPGM("?UBL_MESH_MAX_X set wrong\n");
263
+      error_flag++;
264
+    }
265
+
266
+    if (state.mesh_y_max !=  UBL_MESH_MAX_Y)  {
267
+      SERIAL_PROTOCOLLNPGM("?UBL_MESH_MAX_Y set wrong\n");
268
+      error_flag++;
269
+    }
270
+
271
+    if (state.mesh_x_dist !=  MESH_X_DIST)  {
272
+      SERIAL_PROTOCOLLNPGM("?MESH_X_DIST set wrong\n");
273
+      error_flag++;
274
+    }
275
+
276
+    if (state.mesh_y_dist !=  MESH_Y_DIST)  {
277
+      SERIAL_PROTOCOLLNPGM("?MESH_Y_DIST set wrong\n");
278
+      error_flag++;
279
+    }
280
+
281
+    int k = E2END - sizeof(blm.state),
282
+        j = (k - Unified_Bed_Leveling_EEPROM_start) / sizeof(z_values);
283
+
284
+    if (j < 1) {
285
+      SERIAL_PROTOCOLLNPGM("?No EEPROM storage available for a mesh of this size.\n");
286
+      error_flag++;
287
+    }
288
+
289
+    //  SERIAL_PROTOCOLPGM("?sanity_check() return value: ");
290
+    //  SERIAL_PROTOCOL(error_flag);
291
+    //  SERIAL_EOL;
292
+
293
+    return !!error_flag;
294
+  }
295
+
296
+#endif // AUTO_BED_LEVELING_UBL

+ 1455
- 0
Marlin/UBL_G29.cpp
File diff suppressed because it is too large
View File


+ 553
- 0
Marlin/UBL_line_to_destination.cpp View File

1
+/**
2
+ * Marlin 3D Printer Firmware
3
+ * Copyright (C) 2016 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
+#include "Marlin.h"
23
+
24
+#if ENABLED(AUTO_BED_LEVELING_UBL)
25
+
26
+  #include "UBL.h"
27
+  #include "planner.h"
28
+  #include <avr/io.h>
29
+  #include <math.h>
30
+
31
+  extern void set_current_to_destination();
32
+  extern bool G26_Debug_flag;
33
+  void debug_current_and_destination(char *title);
34
+
35
+  void wait_for_button_press();
36
+
37
+  void UBL_line_to_destination(const float &x_end, const float &y_end, const float &z_end, const float &e_end, const float &feed_rate, uint8_t extruder) {
38
+
39
+    int cell_start_xi, cell_start_yi, cell_dest_xi, cell_dest_yi;
40
+    int left_flag, down_flag;
41
+    int current_xi, current_yi;
42
+    int dxi, dyi, xi_cnt, yi_cnt;
43
+    bool use_X_dist, inf_normalized_flag, inf_m_flag;
44
+    float x_start, y_start;
45
+    float x, y, z1, z2, z0 /*, z_optimized */;
46
+    float next_mesh_line_x, next_mesh_line_y, a0ma1diva2ma1;
47
+    float on_axis_distance, e_normalized_dist, e_position, e_start, z_normalized_dist, z_position, z_start;
48
+    float dx, dy, adx, ady, m, c;
49
+
50
+    //
51
+    // Much of the nozzle movement will be within the same cell.  So we will do as little computation
52
+    // as possible to determine if this is the case.  If this move is within the same cell, we will
53
+    // just do the required Z-Height correction, call the Planner's buffer_line() routine, and leave
54
+    //
55
+
56
+    x_start = current_position[X_AXIS];
57
+    y_start = current_position[Y_AXIS];
58
+    z_start = current_position[Z_AXIS];
59
+    e_start = current_position[E_AXIS];
60
+
61
+    cell_start_xi = blm.get_cell_index_x(x_start);
62
+    cell_start_yi = blm.get_cell_index_y(y_start);
63
+    cell_dest_xi  = blm.get_cell_index_x(x_end);
64
+    cell_dest_yi  = blm.get_cell_index_y(y_end);
65
+
66
+    if (G26_Debug_flag!=0) {
67
+      SERIAL_ECHOPGM(" UBL_line_to_destination(xe=");
68
+      SERIAL_ECHO(x_end);
69
+      SERIAL_ECHOPGM(",ye=");
70
+      SERIAL_ECHO(y_end);
71
+      SERIAL_ECHOPGM(",ze=");
72
+      SERIAL_ECHO(z_end);
73
+      SERIAL_ECHOPGM(",ee=");
74
+      SERIAL_ECHO(e_end);
75
+      SERIAL_ECHOPGM(")\n");
76
+      debug_current_and_destination( (char *) "Start of UBL_line_to_destination()");
77
+    }
78
+
79
+    if ((cell_start_xi == cell_dest_xi) && (cell_start_yi == cell_dest_yi)) { // if the whole move is within the same cell,
80
+      // we don't need to break up the move
81
+      //
82
+      // If we are moving off the print bed, we are going to allow the move at this level.
83
+      // But we detect it and isolate it.   For now, we just pass along the request.
84
+      //
85
+
86
+      if (cell_dest_xi<0 || cell_dest_yi<0 || cell_dest_xi >= UBL_MESH_NUM_X_POINTS || cell_dest_yi >= UBL_MESH_NUM_Y_POINTS) {
87
+
88
+        // Note:  There is no Z Correction in this case.  We are off the grid and don't know what
89
+        // a reasonable correction would be.
90
+
91
+        planner.buffer_line(x_end, y_end, z_end + blm.state.z_offset, e_end, feed_rate, extruder);
92
+        set_current_to_destination();
93
+        if (G26_Debug_flag!=0) {
94
+          debug_current_and_destination( (char *) "out of bounds in UBL_line_to_destination()");
95
+        }
96
+        return;
97
+      }
98
+
99
+      // we can optimize some floating point operations here.  We could call float get_z_correction(float x0, float y0) to
100
+      // generate the correction for us.  But we can lighten the load on the CPU by doing a modified version of the function.
101
+      // We are going to only calculate the amount we are from the first mesh line towards the second mesh line once.
102
+      // We will use this fraction in both of the original two Z Height calculations for the bi-linear interpolation.  And,
103
+      // instead of doing a generic divide of the distance, we know the distance is MESH_X_DIST so we can use the preprocessor
104
+      // to create a 1-over number for us.  That will allow us to do a floating point multiply instead of a floating point divide.
105
+
106
+      FINAL_MOVE:
107
+      a0ma1diva2ma1 = (x_end - mesh_index_to_X_location[cell_dest_xi]) * (float) (1.0 / MESH_X_DIST);
108
+
109
+      z1 = z_values[cell_dest_xi][cell_dest_yi] +
110
+      (z_values[cell_dest_xi + 1][cell_dest_yi] - z_values[cell_dest_xi][cell_dest_yi]) * a0ma1diva2ma1;
111
+
112
+      z2 = z_values[cell_dest_xi][cell_dest_yi+1] +
113
+      (z_values[cell_dest_xi+1][cell_dest_yi+1] - z_values[cell_dest_xi][cell_dest_yi+1]) * a0ma1diva2ma1;
114
+
115
+      // we are done with the fractional X distance into the cell.  Now with the two Z-Heights we have calculated, we
116
+      // are going to apply the Y-Distance into the cell to interpolate the final Z correction.
117
+
118
+      a0ma1diva2ma1 = (y_end - mesh_index_to_Y_location[cell_dest_yi]) * (float) (1.0 / MESH_Y_DIST);
119
+
120
+      z0 = z1 + (z2 - z1) * a0ma1diva2ma1;
121
+
122
+      // debug code to use non-optimized get_z_correction() and to do a sanity check
123
+      // that the correct value is being passed to planner.buffer_line()
124
+      //
125
+      /*
126
+        z_optimized = z0;
127
+        z0 = blm.get_z_correction( x_end, y_end);
128
+        if ( fabs(z_optimized - z0) > .01 || isnan(z0) || isnan(z_optimized) )  {
129
+        debug_current_and_destination( (char *) "FINAL_MOVE: z_correction()");
130
+        if ( isnan(z0) ) SERIAL_ECHO(" z0==NAN  ");
131
+        if ( isnan(z_optimized) ) SERIAL_ECHO(" z_optimized==NAN  ");
132
+        SERIAL_ECHOPAIR("  x_end=", x_end);
133
+        SERIAL_ECHOPAIR("  y_end=", y_end);
134
+        SERIAL_ECHOPAIR("  z0=", z0);
135
+        SERIAL_ECHOPAIR("  z_optimized=", z_optimized);
136
+        SERIAL_ECHOPAIR("  err=",fabs(z_optimized - z0));
137
+        SERIAL_EOL;
138
+        }
139
+      */
140
+      z0 = z0 * blm.fade_scaling_factor_for_Z( z_end );
141
+
142
+      if (isnan(z0)) {  // if part of the Mesh is undefined, it will show up as NAN
143
+        z0 = 0.0; // in z_values[][] and propagate through the
144
+        // calculations. If our correction is NAN, we throw it out
145
+        // because part of the Mesh is undefined and we don't have the
146
+        // information we need to complete the height correction.
147
+      }
148
+
149
+      planner.buffer_line(x_end, y_end, z_end + z0 + blm.state.z_offset, e_end, feed_rate, extruder);
150
+      if (G26_Debug_flag!=0) {
151
+        debug_current_and_destination( (char *) "FINAL_MOVE in UBL_line_to_destination()");
152
+      }
153
+      set_current_to_destination();
154
+      return;
155
+    }
156
+
157
+    //
158
+    //  If we get here, we are processing a move that crosses at least one Mesh Line.   We will check
159
+    //  for the simple case of just crossing X or just crossing Y Mesh Lines after we get all the details
160
+    //  of the move figured out.  We can process the easy case of just crossing an X or Y Mesh Line with less
161
+    //  computation and in fact most lines are of this nature.  We will check for that in the following
162
+    //  blocks of code:
163
+
164
+    left_flag = 0;
165
+    down_flag = 0;
166
+    inf_m_flag = false;
167
+    inf_normalized_flag = false;
168
+
169
+    dx = x_end - x_start;
170
+    dy = y_end - y_start;
171
+
172
+    if (dx<0.0) {     // figure out which way we need to move to get to the next cell
173
+      dxi = -1;
174
+      adx = -dx;  // absolute value of dx.  We already need to check if dx and dy are negative.
175
+    }
176
+    else {   // We may as well generate the appropriate values for adx and ady right now
177
+      dxi = 1;  // to save setting up the abs() function call and actually doing the call.
178
+      adx = dx;
179
+    }
180
+    if (dy<0.0) {
181
+      dyi = -1;
182
+      ady = -dy;  // absolute value of dy
183
+    }
184
+    else {
185
+      dyi = 1;
186
+      ady = dy;
187
+    }
188
+
189
+    if (dx<0.0) left_flag = 1;
190
+    if (dy<0.0) down_flag = 1;
191
+    if (cell_start_xi == cell_dest_xi) dxi = 0;
192
+    if (cell_start_yi == cell_dest_yi) dyi = 0;
193
+
194
+    //
195
+    // Compute the scaling factor for the extruder for each partial move.
196
+    // We need to watch out for zero length moves because it will cause us to
197
+    // have an infinate scaling factor.  We are stuck doing a floating point
198
+    // divide to get our scaling factor, but after that, we just multiply by this
199
+    // number.   We also pick our scaling factor based on whether the X or Y
200
+    // component is larger.  We use the biggest of the two to preserve precision.
201
+    //
202
+    if ( adx > ady ) {
203
+      use_X_dist = true;
204
+      on_axis_distance   = x_end-x_start;
205
+    }
206
+    else {
207
+      use_X_dist = false;
208
+      on_axis_distance   = y_end-y_start;
209
+    }
210
+    e_position = e_end - e_start;
211
+    e_normalized_dist = e_position / on_axis_distance;
212
+
213
+    z_position = z_end - z_start;
214
+    z_normalized_dist = z_position / on_axis_distance;
215
+
216
+    if (e_normalized_dist==INFINITY || e_normalized_dist==-INFINITY) {
217
+      inf_normalized_flag = true;
218
+    }
219
+    current_xi = cell_start_xi;
220
+    current_yi = cell_start_yi;
221
+
222
+    m = dy / dx;
223
+    c = y_start - m*x_start;
224
+    if (m == INFINITY || m == -INFINITY) {
225
+      inf_m_flag = true;
226
+    }
227
+    //
228
+    // This block handles vertical lines.  These are lines that stay within the same
229
+    // X Cell column.  They do not need to be perfectly vertical.  They just can
230
+    // not cross into another X Cell column.
231
+    //
232
+    if (dxi == 0) {       // Check for a vertical line
233
+      current_yi += down_flag;  // Line is heading down, we just want to go to the bottom
234
+      while (current_yi != cell_dest_yi + down_flag) {
235
+        current_yi += dyi;
236
+        next_mesh_line_y = mesh_index_to_Y_location[current_yi];
237
+        if (inf_m_flag) {
238
+          x = x_start;  // if the slope of the line is infinite, we won't do the calculations
239
+        }
240
+        // we know the next X is the same so we can recover and continue!
241
+        else {
242
+          x = (next_mesh_line_y - c) / m; // Calculate X at the next Y mesh line
243
+        }
244
+
245
+        z0 = blm.get_z_correction_along_horizontal_mesh_line_at_specific_X(x, current_xi, current_yi);
246
+
247
+        //
248
+        // debug code to use non-optimized get_z_correction() and to do a sanity check
249
+        // that the correct value is being passed to planner.buffer_line()
250
+        //
251
+        /*
252
+          z_optimized = z0;
253
+          z0 = blm.get_z_correction( x, next_mesh_line_y);
254
+          if ( fabs(z_optimized - z0) > .01 || isnan(z0) || isnan(z_optimized) )  {
255
+          debug_current_and_destination( (char *) "VERTICAL z_correction()");
256
+          if ( isnan(z0) ) SERIAL_ECHO(" z0==NAN  ");
257
+          if ( isnan(z_optimized) ) SERIAL_ECHO(" z_optimized==NAN  ");
258
+          SERIAL_ECHOPAIR("  x=", x);
259
+          SERIAL_ECHOPAIR("  next_mesh_line_y=", next_mesh_line_y);
260
+          SERIAL_ECHOPAIR("  z0=", z0);
261
+          SERIAL_ECHOPAIR("  z_optimized=", z_optimized);
262
+          SERIAL_ECHOPAIR("  err=",fabs(z_optimized-z0));
263
+          SERIAL_ECHO("\n");
264
+          }
265
+        */
266
+
267
+        z0 = z0 * blm.fade_scaling_factor_for_Z( z_end );
268
+
269
+        if (isnan(z0)) {  // if part of the Mesh is undefined, it will show up as NAN
270
+          z0 = 0.0; // in z_values[][] and propagate through the
271
+          // calculations. If our correction is NAN, we throw it out
272
+          // because part of the Mesh is undefined and we don't have the
273
+          // information we need to complete the height correction.
274
+        }
275
+        y = mesh_index_to_Y_location[current_yi];
276
+
277
+        // Without this check, it is possible for the algorythm to generate a zero length move in the case
278
+        // where the line is heading down and it is starting right on a Mesh Line boundary.  For how often that
279
+        // happens, it might be best to remove the check and always 'schedule' the move because
280
+        // the planner.buffer_line() routine will filter it if that happens.
281
+        if ( y!=y_start)   {
282
+          if ( inf_normalized_flag == false ) {
283
+            on_axis_distance   = y - y_start;       // we don't need to check if the extruder position
284
+            e_position = e_start + on_axis_distance * e_normalized_dist;  // is based on X or Y because this is a vertical move
285
+            z_position = z_start + on_axis_distance * z_normalized_dist;
286
+          }
287
+          else {
288
+            e_position = e_start;
289
+            z_position = z_start;
290
+          }
291
+
292
+          planner.buffer_line(x, y, z_position + z0 + blm.state.z_offset, e_position, feed_rate, extruder);
293
+        } //else printf("FIRST MOVE PRUNED  ");
294
+      }
295
+      //
296
+      // Check if we are at the final destination.  Usually, we won't be, but if it is on a Y Mesh Line, we are done.
297
+      //
298
+      if (G26_Debug_flag!=0) {
299
+        debug_current_and_destination( (char *) "vertical move done in UBL_line_to_destination()");
300
+      }
301
+      if (current_position[X_AXIS] != x_end || current_position[Y_AXIS] != y_end) {
302
+        goto FINAL_MOVE;
303
+      }
304
+      set_current_to_destination();
305
+      return;
306
+    }
307
+
308
+    //
309
+    // This block handles horizontal lines.  These are lines that stay within the same
310
+    // Y Cell row.  They do not need to be perfectly horizontal.  They just can
311
+    // not cross into another Y Cell row.
312
+    //
313
+
314
+    if (dyi == 0) {       // Check for a horiziontal line
315
+      current_xi += left_flag;  // Line is heading left, we just want to go to the left
316
+      // edge of this cell for the first move.
317
+      while (current_xi != cell_dest_xi + left_flag) {
318
+        current_xi += dxi;
319
+        next_mesh_line_x = mesh_index_to_X_location[current_xi];
320
+        y = m * next_mesh_line_x + c;   // Calculate X at the next Y mesh line
321
+
322
+        z0 = blm.get_z_correction_along_vertical_mesh_line_at_specific_Y(y, current_xi, current_yi);
323
+
324
+        //
325
+        // debug code to use non-optimized get_z_correction() and to do a sanity check
326
+        // that the correct value is being passed to planner.buffer_line()
327
+        //
328
+        /*
329
+          z_optimized = z0;
330
+          z0 = blm.get_z_correction( next_mesh_line_x, y);
331
+          if ( fabs(z_optimized - z0) > .01 || isnan(z0) || isnan(z_optimized) )  {
332
+          debug_current_and_destination( (char *) "HORIZONTAL z_correction()");
333
+          if ( isnan(z0) ) SERIAL_ECHO(" z0==NAN  ");
334
+          if ( isnan(z_optimized) ) SERIAL_ECHO(" z_optimized==NAN  ");
335
+          SERIAL_ECHOPAIR("  next_mesh_line_x=", next_mesh_line_x);
336
+          SERIAL_ECHOPAIR("  y=", y);
337
+          SERIAL_ECHOPAIR("  z0=", z0);
338
+          SERIAL_ECHOPAIR("  z_optimized=", z_optimized);
339
+          SERIAL_ECHOPAIR("  err=",fabs(z_optimized-z0));
340
+          SERIAL_ECHO("\n");
341
+          }
342
+        */
343
+
344
+        z0 = z0 * blm.fade_scaling_factor_for_Z( z_end );
345
+
346
+        if (isnan(z0)) {  // if part of the Mesh is undefined, it will show up as NAN
347
+          z0 = 0.0; // in z_values[][] and propagate through the
348
+          // calculations. If our correction is NAN, we throw it out
349
+          // because part of the Mesh is undefined and we don't have the
350
+          // information we need to complete the height correction.
351
+        }
352
+        x = mesh_index_to_X_location[current_xi];
353
+
354
+        // Without this check, it is possible for the algorythm to generate a zero length move in the case
355
+        // where the line is heading left and it is starting right on a Mesh Line boundary.  For how often
356
+        // that happens, it might be best to remove the check and always 'schedule' the move because
357
+        // the planner.buffer_line() routine will filter it if that happens.
358
+        if ( x!=x_start)   {
359
+          if ( inf_normalized_flag == false ) {
360
+            on_axis_distance   = x - x_start;       // we don't need to check if the extruder position
361
+            e_position = e_start + on_axis_distance * e_normalized_dist;  // is based on X or Y because this is a horizontal move
362
+            z_position = z_start + on_axis_distance * z_normalized_dist;
363
+          }
364
+          else {
365
+            e_position = e_start;
366
+            z_position = z_start;
367
+          }
368
+
369
+          planner.buffer_line(x, y, z_position + z0 + blm.state.z_offset, e_position, feed_rate, extruder);
370
+        } //else printf("FIRST MOVE PRUNED  ");
371
+      }
372
+      if (G26_Debug_flag!=0) {
373
+        debug_current_and_destination( (char *) "horizontal move done in UBL_line_to_destination()");
374
+      }
375
+      if (current_position[X_AXIS] != x_end || current_position[Y_AXIS] != y_end) {
376
+        goto FINAL_MOVE;
377
+      }
378
+      set_current_to_destination();
379
+      return;
380
+    }
381
+
382
+    //
383
+    //
384
+    //
385
+    //
386
+    // This block handles the generic case of a line crossing both X and Y
387
+    // Mesh lines.
388
+    //
389
+    //
390
+    //
391
+    //
392
+
393
+    xi_cnt = cell_start_xi - cell_dest_xi;
394
+    if ( xi_cnt < 0 ) {
395
+      xi_cnt = -xi_cnt;
396
+    }
397
+
398
+    yi_cnt = cell_start_yi - cell_dest_yi;
399
+    if ( yi_cnt < 0 ) {
400
+      yi_cnt = -yi_cnt;
401
+    }
402
+
403
+    current_xi += left_flag;
404
+    current_yi += down_flag;
405
+
406
+    while ( xi_cnt>0 || yi_cnt>0 )    {
407
+
408
+      next_mesh_line_x = mesh_index_to_X_location[current_xi + dxi];
409
+      next_mesh_line_y = mesh_index_to_Y_location[current_yi + dyi];
410
+
411
+      y = m * next_mesh_line_x + c; // Calculate Y at the next X mesh line
412
+      x = (next_mesh_line_y-c) / m; // Calculate X at the next Y mesh line    (we don't have to worry
413
+      // about m being equal to 0.0  If this was the case, we would have
414
+      // detected this as a vertical line move up above and we wouldn't
415
+      // be down here doing a generic type of move.
416
+
417
+      if ((left_flag && (x>next_mesh_line_x)) || (!left_flag && (x<next_mesh_line_x))) { // Check if we hit the Y line first
418
+        //
419
+        // Yes!  Crossing a Y Mesh Line next
420
+        //
421
+        z0 = blm.get_z_correction_along_horizontal_mesh_line_at_specific_X(x, current_xi-left_flag, current_yi+dyi);
422
+
423
+        //
424
+        // debug code to use non-optimized get_z_correction() and to do a sanity check
425
+        // that the correct value is being passed to planner.buffer_line()
426
+        //
427
+
428
+        /*
429
+
430
+          z_optimized = z0;
431
+
432
+          z0 = blm.get_z_correction( x, next_mesh_line_y);
433
+          if ( fabs(z_optimized - z0) > .01 || isnan(z0) || isnan(z_optimized) )  {
434
+            debug_current_and_destination( (char *) "General_1: z_correction()");
435
+            if ( isnan(z0) ) SERIAL_ECHO(" z0==NAN  ");
436
+            if ( isnan(z_optimized) ) SERIAL_ECHO(" z_optimized==NAN  "); {
437
+              SERIAL_ECHOPAIR("  x=", x);
438
+            }
439
+            SERIAL_ECHOPAIR("  next_mesh_line_y=", next_mesh_line_y);
440
+            SERIAL_ECHOPAIR("  z0=", z0);
441
+            SERIAL_ECHOPAIR("  z_optimized=", z_optimized);
442
+            SERIAL_ECHOPAIR("  err=",fabs(z_optimized-z0));
443
+            SERIAL_ECHO("\n");
444
+          }
445
+        */
446
+
447
+        z0 = z0 * blm.fade_scaling_factor_for_Z( z_end );
448
+        if (isnan(z0)) {  // if part of the Mesh is undefined, it will show up as NAN
449
+          z0 = 0.0; // in z_values[][] and propagate through the
450
+          // calculations. If our correction is NAN, we throw it out
451
+          // because part of the Mesh is undefined and we don't have the
452
+          // information we need to complete the height correction.
453
+        }
454
+
455
+        if ( inf_normalized_flag == false ) {
456
+          if ( use_X_dist ) {
457
+            on_axis_distance   = x - x_start;
458
+          }
459
+          else {
460
+            on_axis_distance   = next_mesh_line_y - y_start;
461
+          }
462
+          e_position = e_start + on_axis_distance * e_normalized_dist;
463
+          z_position = z_start + on_axis_distance * z_normalized_dist;
464
+        }
465
+        else {
466
+          e_position = e_start;
467
+          z_position = z_start;
468
+        }
469
+        planner.buffer_line(x, next_mesh_line_y, z_position + z0 + blm.state.z_offset, e_position, feed_rate, extruder);
470
+        current_yi += dyi;
471
+        yi_cnt--;
472
+      }
473
+      else {
474
+        //
475
+        // Yes!  Crossing a X Mesh Line next
476
+        //
477
+        z0 = blm.get_z_correction_along_vertical_mesh_line_at_specific_Y(y, current_xi+dxi, current_yi-down_flag);
478
+
479
+
480
+        //
481
+        // debug code to use non-optimized get_z_correction() and to do a sanity check
482
+        // that the correct value is being passed to planner.buffer_line()
483
+        //
484
+        /*
485
+          z_optimized = z0;
486
+          z0 = blm.get_z_correction( next_mesh_line_x, y);
487
+          if ( fabs(z_optimized - z0) > .01 || isnan(z0) || isnan(z_optimized) )  {
488
+          debug_current_and_destination( (char *) "General_2: z_correction()");
489
+          if ( isnan(z0) ) SERIAL_ECHO(" z0==NAN  ");
490
+          if ( isnan(z_optimized) ) SERIAL_ECHO(" z_optimized==NAN  ");
491
+          SERIAL_ECHOPAIR("  next_mesh_line_x=", next_mesh_line_x);
492
+          SERIAL_ECHOPAIR("  y=", y);
493
+          SERIAL_ECHOPAIR("  z0=", z0);
494
+          SERIAL_ECHOPAIR("  z_optimized=", z_optimized);
495
+          SERIAL_ECHOPAIR("  err=",fabs(z_optimized-z0));
496
+          SERIAL_ECHO("\n");
497
+          }
498
+        */
499
+
500
+        z0 = z0 * blm.fade_scaling_factor_for_Z( z_end );
501
+
502
+        if (isnan(z0)) {  // if part of the Mesh is undefined, it will show up as NAN
503
+          z0 = 0.0; // in z_values[][] and propagate through the
504
+          // calculations. If our correction is NAN, we throw it out
505
+          // because part of the Mesh is undefined and we don't have the
506
+          // information we need to complete the height correction.
507
+        }
508
+        if ( inf_normalized_flag == false ) {
509
+          if ( use_X_dist ) {
510
+            on_axis_distance   = next_mesh_line_x - x_start;
511
+          }
512
+          else {
513
+            on_axis_distance   = y - y_start;
514
+          }
515
+          e_position = e_start + on_axis_distance * e_normalized_dist;
516
+          z_position = z_start + on_axis_distance * z_normalized_dist;
517
+        }
518
+        else {
519
+          e_position = e_start;
520
+          z_position = z_start;
521
+        }
522
+
523
+        planner.buffer_line(next_mesh_line_x, y, z_position + z0 + blm.state.z_offset, e_position, feed_rate, extruder);
524
+        current_xi += dxi;
525
+        xi_cnt--;
526
+      }
527
+    }
528
+    if (G26_Debug_flag) {
529
+      debug_current_and_destination( (char *) "generic move done in UBL_line_to_destination()");
530
+    }
531
+    if (current_position[0] != x_end || current_position[1] != y_end)  {
532
+      goto FINAL_MOVE;
533
+    }
534
+    set_current_to_destination();
535
+    return;
536
+  }
537
+
538
+  void wait_for_button_press() {
539
+    //  if ( !been_to_2_6 )
540
+    //return;   // bob - I think this should be commented out
541
+
542
+    SET_INPUT_PULLUP(66); // Roxy's Left Switch is on pin 66.  Right Switch is on pin 65
543
+    SET_OUTPUT(64);
544
+    while (READ(66) & 0x01) idle();
545
+
546
+    delay(50);
547
+    while (!(READ(66) & 0x01)) idle();
548
+    delay(50);
549
+  }
550
+
551
+#endif
552
+
553
+

+ 47
- 0
Marlin/hex_print_routines.cpp View File

1
+/**
2
+ * Marlin 3D Printer Firmware
3
+ * Copyright (C) 2016 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
+
24
+#include "Marlin.h"
25
+#if ENABLED(AUTO_BED_LEVELING_UBL) || ENABLED(M100_FREE_MEMORY_WATCHER)
26
+
27
+#include "hex_print_routines.h"
28
+
29
+void prt_hex_nibble(uint8_t n) {
30
+  if (n <= 9)
31
+    SERIAL_ECHO(n);
32
+  else
33
+    SERIAL_ECHO((char)('A' + n - 10));
34
+  delay(3);
35
+}
36
+
37
+void prt_hex_byte(uint8_t b) {
38
+  prt_hex_nibble((b & 0xF0) >> 4);
39
+  prt_hex_nibble(b & 0x0F);
40
+}
41
+
42
+void prt_hex_word(uint16_t w) {
43
+  prt_hex_byte((w & 0xFF00) >> 8);
44
+  prt_hex_byte(w & 0x0FF);
45
+}
46
+
47
+#endif // AUTO_BED_LEVELING_UBL || M100_FREE_MEMORY_WATCHER

+ 33
- 0
Marlin/hex_print_routines.h View File

1
+/**
2
+ * Marlin 3D Printer Firmware
3
+ * Copyright (C) 2016 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 HEX_PRINT_ROUTINES_H
24
+#define HEX_PRINT_ROUTINES_H
25
+
26
+//
27
+// 3 support routines to print hex numbers.  We can print a nibble, byte and word
28
+//
29
+void prt_hex_nibble(uint8_t n);
30
+void prt_hex_byte(uint8_t b);
31
+void prt_hex_word(uint16_t w);
32
+
33
+#endif // HEX_PRINT_ROUTINES_H

Loading…
Cancel
Save