Procházet zdrojové kódy

Patches to bring UBL closer to compliance

Scott Lahteine před 7 roky
rodič
revize
b5711a99a2

+ 1
- 1
Marlin/Conditionals_post.h Zobrazit soubor

@@ -669,7 +669,7 @@
669 669
   #define ABL_GRID   (ENABLED(AUTO_BED_LEVELING_LINEAR) || ENABLED(AUTO_BED_LEVELING_BILINEAR))
670 670
   #define HAS_ABL    (ABL_PLANAR || ABL_GRID || ENABLED(AUTO_BED_LEVELING_UBL))
671 671
 
672
-  #define PLANNER_LEVELING      ((HAS_ABL && DISABLED(AUTO_BED_LEVELING_UBL)) || ENABLED(MESH_BED_LEVELING))
672
+  #define PLANNER_LEVELING      (HAS_ABL || ENABLED(MESH_BED_LEVELING))
673 673
   #define HAS_PROBING_PROCEDURE (HAS_ABL || ENABLED(Z_MIN_PROBE_REPEATABILITY_TEST))
674 674
 
675 675
   #if HAS_PROBING_PROCEDURE

+ 40
- 122
Marlin/G26_Mesh_Validation_Tool.cpp Zobrazit soubor

@@ -24,25 +24,27 @@
24 24
  * Marlin Firmware -- G26 - Mesh Validation Tool
25 25
  */
26 26
 
27
-#define EXTRUSION_MULTIPLIER 1.0    // This is too much clutter for the main Configuration.h file  But
28
-#define RETRACTION_MULTIPLIER 1.0   // some user have expressed an interest in being able to customize
29
-#define NOZZLE 0.3                  // these numbers for thier printer so they don't need to type all
30
-#define FILAMENT 1.75               // the options every time they do a Mesh Validation Print.
31
-#define LAYER_HEIGHT 0.2
32
-#define PRIME_LENGTH 10.0           // So, we put these number in an easy to find and change place.
33
-#define BED_TEMP 60.0
34
-#define HOTEND_TEMP 205.0
35
-#define OOZE_AMOUNT 0.3
36
-
37
-#include "Marlin.h"
38
-#include "Configuration.h"
39
-#include "planner.h"
40
-#include "stepper.h"
41
-#include "temperature.h"
42
-#include "UBL.h"
43
-#include "ultralcd.h"
44
-
45
-#if ENABLED(AUTO_BED_LEVELING_UBL)
27
+#include "MarlinConfig.h"
28
+
29
+#if ENABLED(AUTO_BED_LEVELING_UBL) && ENABLED(UBL_MESH_EDIT_ENABLED)
30
+
31
+  #include "Marlin.h"
32
+  #include "Configuration.h"
33
+  #include "planner.h"
34
+  #include "stepper.h"
35
+  #include "temperature.h"
36
+  #include "UBL.h"
37
+  #include "ultralcd.h"
38
+
39
+  #define EXTRUSION_MULTIPLIER 1.0    // This is too much clutter for the main Configuration.h file  But
40
+  #define RETRACTION_MULTIPLIER 1.0   // some user have expressed an interest in being able to customize
41
+  #define NOZZLE 0.3                  // these numbers for thier printer so they don't need to type all
42
+  #define FILAMENT 1.75               // the options every time they do a Mesh Validation Print.
43
+  #define LAYER_HEIGHT 0.2
44
+  #define PRIME_LENGTH 10.0           // So, we put these number in an easy to find and change place.
45
+  #define BED_TEMP 60.0
46
+  #define HOTEND_TEMP 205.0
47
+  #define OOZE_AMOUNT 0.3
46 48
 
47 49
   #define SIZE_OF_INTERSECTION_CIRCLES 5
48 50
   #define SIZE_OF_CROSS_HAIRS 3 // cross hairs inside the circle.  This number should be
@@ -50,64 +52,64 @@
50 52
 
51 53
   /**
52 54
    *   Roxy's G26 Mesh Validation Tool
53
-   *  
55
+   *
54 56
    *   G26 Is a Mesh Validation Tool intended to provide support for the Marlin Unified Bed Leveling System.
55 57
    *   In order to fully utilize and benefit from the Marlin Unified Bed Leveling System an accurate Mesh must
56 58
    *   be defined.  G29 is designed to allow the user to quickly validate the correctness of her Mesh.  It will
57 59
    *   first heat the bed and nozzle. It will then print lines and circles along the Mesh Cell boundaries and
58 60
    *   the intersections of those lines (respectively).
59
-   *  
61
+   *
60 62
    *   This action allows the user to immediately see where the Mesh is properly defined and where it needs to
61 63
    *   be edited.  The command will generate the Mesh lines closest to the nozzle's starting position.  Alternatively
62 64
    *   the user can specify the X and Y position of interest with command parameters.  This allows the user to
63 65
    *   focus on a particular area of the Mesh where attention is needed.
64
-   *  
66
+   *
65 67
    *   B #  Bed   Set the Bed Temperature.  If not specified, a default of 60 C. will be assumed.
66
-   *  
68
+   *
67 69
    *   C    Current   When searching for Mesh Intersection points to draw, use the current nozzle location
68 70
    *        as the base for any distance comparison.
69
-   *  
71
+   *
70 72
    *   D    Disable   Disable the Unified Bed Leveling System.  In the normal case the user is invoking this
71 73
    *        command to see how well a Mesh as been adjusted to match a print surface.  In order to do
72 74
    *        this the Unified Bed Leveling System is turned on by the G26 command.  The D parameter
73 75
    *        alters the command's normal behaviour and disables the Unified Bed Leveling System even if
74 76
    *        it is on.
75
-   *  
77
+   *
76 78
    *   H #  Hotend    Set the Nozzle Temperature.  If not specified, a default of 205 C. will be assumed.
77
-   *  
79
+   *
78 80
    *   F #  Filament  Used to specify the diameter of the filament being used.  If not specified
79 81
    *        1.75mm filament is assumed.  If you are not getting acceptable results by using the
80 82
    *        'correct' numbers, you can scale this number up or down a little bit to change the amount
81 83
    *        of filament that is being extruded during the printing of the various lines on the bed.
82
-   *  
84
+   *
83 85
    *   K    Keep-On   Keep the heaters turned on at the end of the command.
84
-   *  
86
+   *
85 87
    *   L #  Layer   Layer height.  (Height of nozzle above bed)  If not specified .20mm will be used.
86
-   *  
88
+   *
87 89
    *   Q #  Multiplier  Retraction Multiplier.  Normally not needed.  Retraction defaults to 1.0mm and
88 90
    *        un-retraction is at 1.2mm   These numbers will be scaled by the specified amount
89
-   *  
91
+   *
90 92
    *   N #  Nozzle    Used to control the size of nozzle diameter.  If not specified, a .4mm nozzle is assumed.
91
-   *  
93
+   *
92 94
    *   O #  Ooooze    How much your nozzle will Ooooze filament while getting in position to print.  This
93 95
    *        is over kill, but using this parameter will let you get the very first 'cicle' perfect
94 96
    *        so you have a trophy to peel off of the bed and hang up to show how perfectly you have your
95 97
    *        Mesh calibrated.  If not specified, a filament length of .3mm is assumed.
96
-   *  
98
+   *
97 99
    *   P #  Prime   Prime the nozzle with specified length of filament.  If this parameter is not
98 100
    *        given, no prime action will take place.  If the parameter specifies an amount, that much
99 101
    *        will be purged before continuing.  If no amount is specified the command will start
100 102
    *        purging filament until the user provides an LCD Click and then it will continue with
101 103
    *        printing the Mesh.  You can carefully remove the spent filament with a needle nose
102 104
    *        pliers while holding the LCD Click wheel in a depressed state.
103
-   *  
105
+   *
104 106
    *   R #  Random    Randomize the order that the circles are drawn on the bed.  The search for the closest
105 107
    *        undrawn cicle is still done.  But the distance to the location for each circle has a
106 108
    *        random number of the size specified added to it.  Specifying R50 will give an interesting
107 109
    *        deviation from the normal behaviour on a 10 x 10 Mesh.
108
-   *  
110
+   *
109 111
    *   X #  X coordinate  Specify the starting location of the drawing activity.
110
-   *  
112
+   *
111 113
    *   Y #  Y coordinate  Specify the starting location of the drawing activity.
112 114
    */
113 115
 
@@ -156,7 +158,6 @@
156 158
 
157 159
   float valid_trig_angle(float);
158 160
   mesh_index_pair find_closest_circle_to_print(float, float);
159
-  void debug_current_and_destination(char *title);
160 161
   void ubl_line_to_destination(const float&, const float&, const float&, const float&, const float&, uint8_t);
161 162
   //uint16_t x_splits = 0xFFFF, uint16_t y_splits = 0xFFFF);  /* needed for the old mesh_buffer_line() routine */
162 163
 
@@ -172,19 +173,8 @@
172 173
 
173 174
   int8_t prime_flag = 0;
174 175
 
175
-  bool keep_heaters_on = false;
176
-
177
-  bool g26_debug_flag = false;
178
-
179
-  /**
180
-   * These support functions allow the use of large bit arrays of flags that take very
181
-   * little RAM. Currently they are limited to being 16x16 in size. Changing the declaration
182
-   * to unsigned long will allow us to go to 32x32 if higher resolution Mesh's are needed
183
-   * in the future.
184
-   */
185
-  void bit_clear(uint16_t bits[16], uint8_t x, uint8_t y) { CBI(bits[y], x); }
186
-  void bit_set(uint16_t bits[16], uint8_t x, uint8_t y) { SBI(bits[y], x); }
187
-  bool is_bit_set(uint16_t bits[16], uint8_t x, uint8_t y) { return TEST(bits[y], x); }
176
+  bool keep_heaters_on = false,
177
+       g26_debug_flag = false;
188 178
 
189 179
   /**
190 180
    * G26: Mesh Validation Pattern generation.
@@ -544,78 +534,6 @@
544 534
     }
545 535
   }
546 536
 
547
-  void debug_current_and_destination(char *title) {
548
-    float dx, dy, de, xy_dist, fpmm;
549
-
550
-    // if the title message starts with a '!' it is so important, we are going to
551
-    // ignore the status of the g26_debug_flag
552
-    if (*title != '!' && !g26_debug_flag) return;
553
-
554
-    dx = current_position[X_AXIS] - destination[X_AXIS];
555
-    dy = current_position[Y_AXIS] - destination[Y_AXIS];
556
-    de = destination[E_AXIS] - current_position[E_AXIS];
557
-    if (de == 0.0) return;
558
-
559
-    xy_dist = HYPOT(dx, dy);
560
-    if (xy_dist == 0.0) {
561
-      return;
562
-      //SERIAL_ECHOPGM("   FPMM=");
563
-      //fpmm = de;
564
-      //SERIAL_PROTOCOL_F(fpmm, 6);
565
-    }
566
-    else {
567
-      SERIAL_ECHOPGM("   fpmm=");
568
-      fpmm = de / xy_dist;
569
-      SERIAL_ECHO_F(fpmm, 6);
570
-    }
571
-
572
-    SERIAL_ECHOPGM("    current=( ");
573
-    SERIAL_ECHO_F(current_position[X_AXIS], 6);
574
-    SERIAL_ECHOPGM(", ");
575
-    SERIAL_ECHO_F(current_position[Y_AXIS], 6);
576
-    SERIAL_ECHOPGM(", ");
577
-    SERIAL_ECHO_F(current_position[Z_AXIS], 6);
578
-    SERIAL_ECHOPGM(", ");
579
-    SERIAL_ECHO_F(current_position[E_AXIS], 6);
580
-    SERIAL_ECHOPGM(" )   destination=( ");
581
-    if (current_position[X_AXIS] == destination[X_AXIS])
582
-      SERIAL_ECHOPGM("-------------");
583
-    else
584
-      SERIAL_ECHO_F(destination[X_AXIS], 6);
585
-
586
-    SERIAL_ECHOPGM(", ");
587
-
588
-    if (current_position[Y_AXIS] == destination[Y_AXIS])
589
-      SERIAL_ECHOPGM("-------------");
590
-    else
591
-      SERIAL_ECHO_F(destination[Y_AXIS], 6);
592
-
593
-    SERIAL_ECHOPGM(", ");
594
-
595
-    if (current_position[Z_AXIS] == destination[Z_AXIS])
596
-      SERIAL_ECHOPGM("-------------");
597
-    else
598
-      SERIAL_ECHO_F(destination[Z_AXIS], 6);
599
-
600
-    SERIAL_ECHOPGM(", ");
601
-
602
-    if (current_position[E_AXIS] == destination[E_AXIS])
603
-      SERIAL_ECHOPGM("-------------");
604
-    else
605
-      SERIAL_ECHO_F(destination[E_AXIS], 6);
606
-
607
-    SERIAL_ECHOPGM(" )   ");
608
-    SERIAL_ECHO(title);
609
-    SERIAL_EOL;
610
-
611
-    SET_INPUT_PULLUP(66); // Roxy's Left Switch is on pin 66.  Right Switch is on pin 65
612
-
613
-    //if (been_to_2_6) {
614
-    //while ((digitalRead(66) & 0x01) != 0)
615
-    //  idle();
616
-    //}
617
-  }
618
-
619 537
   void move_to(const float &x, const float &y, const float &z, const float &e_delta) {
620 538
     float feed_value;
621 539
     static float last_z = -999.99;
@@ -1002,4 +920,4 @@
1002 920
     return UBL_OK;
1003 921
   }
1004 922
 
1005
-#endif // AUTO_BED_LEVELING_UBL
923
+#endif // AUTO_BED_LEVELING_UBL && UBL_MESH_EDIT_ENABLED

+ 30
- 22
Marlin/Marlin_main.cpp Zobrazit soubor

@@ -233,10 +233,6 @@
233 233
 #include "duration_t.h"
234 234
 #include "types.h"
235 235
 
236
-#if ENABLED(AUTO_BED_LEVELING_UBL)
237
-  #include "UBL.h"
238
-#endif
239
-
240 236
 #if HAS_ABL
241 237
   #include "vector_3.h"
242 238
   #if ENABLED(AUTO_BED_LEVELING_LINEAR)
@@ -301,7 +297,13 @@
301 297
 #endif
302 298
 
303 299
 #if ENABLED(AUTO_BED_LEVELING_UBL)
300
+  #include "UBL.h"
304 301
   unified_bed_leveling ubl;
302
+#define UBL_MESH_VALID !(   z_values[0][0] == z_values[0][1] && z_values[0][1] == z_values[0][2] \
303
+                         && z_values[1][0] == z_values[1][1] && z_values[1][1] == z_values[1][2] \
304
+                         && z_values[2][0] == z_values[2][1] && z_values[2][1] == z_values[2][2] \
305
+                         && z_values[0][0] == 0 && z_values[1][0] == 0 && z_values[2][0] == 0    \
306
+                         || isnan(z_values[0][0]))
305 307
 #endif
306 308
 
307 309
 bool Running = true;
@@ -2266,7 +2268,7 @@ static void clean_up_after_endstop_or_probe_move() {
2266 2268
 
2267 2269
 #endif // HAS_BED_PROBE
2268 2270
 
2269
-#if PLANNER_LEVELING || ENABLED(AUTO_BED_LEVELING_UBL)
2271
+#if PLANNER_LEVELING
2270 2272
   /**
2271 2273
    * Turn bed leveling on or off, fixing the current
2272 2274
    * position as-needed.
@@ -2309,7 +2311,8 @@ static void clean_up_after_endstop_or_probe_move() {
2309 2311
           planner.unapply_leveling(current_position);
2310 2312
       }
2311 2313
     #elif ENABLED(AUTO_BED_LEVELING_UBL)
2312
-        ubl.state.active = enable;
2314
+      ubl.state.active = enable;
2315
+      //set_current_from_steppers_for_axis(Z_AXIS);
2313 2316
     #endif
2314 2317
   }
2315 2318
 
@@ -3481,11 +3484,6 @@ inline void gcode_G4() {
3481 3484
  *
3482 3485
  */
3483 3486
 inline void gcode_G28() {
3484
-  #if ENABLED(AUTO_BED_LEVELING_UBL)
3485
-  bool bed_leveling_state_at_entry=0;
3486
-    bed_leveling_state_at_entry = ubl.state.active;
3487
-    set_bed_leveling_enabled(false);
3488
-  #endif
3489 3487
 
3490 3488
   #if ENABLED(DEBUG_LEVELING_FEATURE)
3491 3489
     if (DEBUGGING(LEVELING)) {
@@ -3498,7 +3496,10 @@ inline void gcode_G28() {
3498 3496
   stepper.synchronize();
3499 3497
 
3500 3498
   // Disable the leveling matrix before homing
3501
-  #if PLANNER_LEVELING || ENABLED(MESH_BED_LEVELING)
3499
+  #if PLANNER_LEVELING
3500
+    #if ENABLED(AUTO_BED_LEVELING_UBL)
3501
+      const bool bed_leveling_state_at_entry = ubl.state.active;
3502
+    #endif
3502 3503
     set_bed_leveling_enabled(false);
3503 3504
   #endif
3504 3505
 
@@ -5305,6 +5306,18 @@ inline void gcode_M42() {
5305 5306
 
5306 5307
 #endif // Z_MIN_PROBE_REPEATABILITY_TEST
5307 5308
 
5309
+#if ENABLED(AUTO_BED_LEVELING_UBL) && ENABLED(UBL_MESH_EDIT_ENABLED)
5310
+
5311
+  inline void gcode_M49() {
5312
+    SERIAL_PROTOCOLPGM("UBL Debug Flag turned ");
5313
+    if ((g26_debug_flag = !g26_debug_flag))
5314
+      SERIAL_PROTOCOLLNPGM("on.");
5315
+    else
5316
+      SERIAL_PROTOCOLLNPGM("off.");
5317
+  }
5318
+
5319
+#endif // AUTO_BED_LEVELING_UBL && UBL_MESH_EDIT_ENABLED
5320
+
5308 5321
 /**
5309 5322
  * M75: Start print timer
5310 5323
  */
@@ -8512,7 +8525,7 @@ void process_next_command() {
8512 8525
           break;
8513 8526
       #endif // INCH_MODE_SUPPORT
8514 8527
 
8515
-      #if ENABLED(AUTO_BED_LEVELING_UBL)
8528
+      #if ENABLED(AUTO_BED_LEVELING_UBL) && ENABLED(UBL_MESH_EDIT_ENABLED)
8516 8529
         case 26: // G26: Mesh Validation Pattern generation
8517 8530
           gcode_G26();
8518 8531
           break;
@@ -8644,16 +8657,11 @@ void process_next_command() {
8644 8657
           break;
8645 8658
       #endif // Z_MIN_PROBE_REPEATABILITY_TEST
8646 8659
 
8647
-      #if ENABLED(AUTO_BED_LEVELING_UBL)
8660
+      #if ENABLED(AUTO_BED_LEVELING_UBL) && ENABLED(UBL_MESH_EDIT_ENABLED)
8648 8661
         case 49: // M49: Turn on or off g26_debug_flag for verbose output
8649
-    if (g26_debug_flag) {
8650
-            SERIAL_PROTOCOLPGM("UBL Debug Flag turned off.\n");
8651
-            g26_debug_flag = 0; }
8652
-    else {
8653
-            SERIAL_PROTOCOLPGM("UBL Debug Flag turned on.\n");
8654
-            g26_debug_flag++; }
8662
+          gcode_M49();
8655 8663
           break;
8656
-      #endif // Z_MIN_PROBE_REPEATABILITY_TEST
8664
+      #endif // AUTO_BED_LEVELING_UBL && UBL_MESH_EDIT_ENABLED
8657 8665
 
8658 8666
       case 75: // M75: Start print timer
8659 8667
         gcode_M75(); break;
@@ -9547,7 +9555,7 @@ void get_cartesian_from_steppers() {
9547 9555
  */
9548 9556
 void set_current_from_steppers_for_axis(const AxisEnum axis) {
9549 9557
   get_cartesian_from_steppers();
9550
-  #if PLANNER_LEVELING
9558
+  #if PLANNER_LEVELING && DISABLED(AUTO_BED_LEVELING_UBL)
9551 9559
     planner.unapply_leveling(cartes);
9552 9560
   #endif
9553 9561
   if (axis == ALL_AXES)

+ 87
- 73
Marlin/UBL.h Zobrazit soubor

@@ -22,6 +22,7 @@
22 22
 
23 23
 #include "Marlin.h"
24 24
 #include "math.h"
25
+#include "vector_3.h"
25 26
 
26 27
 #ifndef UNIFIED_BED_LEVELING_H
27 28
 #define UNIFIED_BED_LEVELING_H
@@ -32,32 +33,29 @@
32 33
     #define UBL_ERR true
33 34
 
34 35
     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
36
+      int8_t x_index, y_index;
37
+      float distance; // When populated, the distance from the search location
38 38
     } mesh_index_pair;
39 39
 
40
-    typedef struct { double dx, dy, dz; } vector;
41
-
42 40
     enum MeshPointType { INVALID, REAL, SET_IN_BITMAP };
43 41
 
44 42
     bool axis_unhomed_error(bool, bool, bool);
45
-    void dump(char *str, float f);
43
+    void dump(char * const str, const float &f);
46 44
     bool ubl_lcd_clicked();
47
-    void probe_entire_mesh(float, float, bool, bool);
45
+    void probe_entire_mesh(const float&, const float&, const bool, const bool);
46
+    void debug_current_and_destination(char *title);
48 47
     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
-    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(MeshPointType, float, float, bool, unsigned int[16]);
48
+    void manually_probe_remaining_mesh(const float&, const float&, const float&, const float&, const bool);
49
+    vector_3 tilt_mesh_based_on_3pts(const float&, const float&, const float&);
50
+    float measure_business_card_thickness(const float&);
51
+    mesh_index_pair find_closest_mesh_point_of_type(const MeshPointType, const float&, const float&, const bool, unsigned int[16]);
54 52
     void find_mean_mesh_height();
55 53
     void shift_mesh_height();
56 54
     bool g29_parameter_parsing();
57 55
     void g29_what_command();
58 56
     void g29_eeprom_dump();
59 57
     void g29_compare_current_mesh_to_stored_mesh();
60
-    void fine_tune_mesh(float, float, bool);
58
+    void fine_tune_mesh(const float&, const float&, const bool);
61 59
     void bit_clear(uint16_t bits[16], uint8_t x, uint8_t y);
62 60
     void bit_set(uint16_t bits[16], uint8_t x, uint8_t y);
63 61
     bool is_bit_set(uint16_t bits[16], uint8_t x, uint8_t y);
@@ -83,7 +81,11 @@
83 81
     #define MESH_X_DIST ((float(UBL_MESH_MAX_X) - float(UBL_MESH_MIN_X)) / (float(UBL_MESH_NUM_X_POINTS) - 1.0))
84 82
     #define MESH_Y_DIST ((float(UBL_MESH_MAX_Y) - float(UBL_MESH_MIN_Y)) / (float(UBL_MESH_NUM_Y_POINTS) - 1.0))
85 83
 
86
-    extern bool g26_debug_flag;
84
+    #if ENABLED(UBL_MESH_EDIT_ENABLED)
85
+      extern bool g26_debug_flag;
86
+    #else
87
+      constexpr bool g26_debug_flag = false;
88
+    #endif
87 89
     extern float last_specified_z;
88 90
     extern float fade_scaling_factor_for_current_height;
89 91
     extern float z_values[UBL_MESH_NUM_X_POINTS][UBL_MESH_NUM_Y_POINTS];
@@ -103,12 +105,15 @@
103 105
               mesh_x_max = UBL_MESH_MAX_X,
104 106
               mesh_y_max = UBL_MESH_MAX_Y,
105 107
               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.
108
+              mesh_y_dist = MESH_Y_DIST;
109
+
110
+        #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
111
+          float g29_correction_fade_height = 10.0,
112
+                g29_fade_height_multiplier = 1.0 / 10.0; // It is cheaper to do a floating point multiply than a floating
113
+                                                         // point divide. So, we keep this number in both forms. The first
114
+                                                         // is for the user. The second one is the one that is actually used
115
+                                                         // again and again and again during the correction calculations.
116
+        #endif
112 117
 
113 118
         unsigned char padding[24];  // This is just to allow room to add state variables without
114 119
                                     // changing the location of data structures in the EEPROM.
@@ -122,45 +127,45 @@
122 127
       unified_bed_leveling();
123 128
       //  ~unified_bed_leveling();  // No destructor because this object never goes away!
124 129
 
125
-      void display_map(int);
130
+      void display_map(const int);
126 131
 
127 132
       void reset();
128 133
       void invalidate();
129 134
 
130 135
       void store_state();
131 136
       void load_state();
132
-      void store_mesh(int);
133
-      void load_mesh(int);
137
+      void store_mesh(const int16_t);
138
+      void load_mesh(const int16_t);
134 139
 
135 140
       bool sanity_check();
136 141
 
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); };
142
+      FORCE_INLINE static float map_x_index_to_bed_location(const int8_t i) { return ((float) UBL_MESH_MIN_X) + (((float) MESH_X_DIST) * (float) i); };
143
+      FORCE_INLINE static float map_y_index_to_bed_location(const int8_t i) { return ((float) UBL_MESH_MIN_Y) + (((float) MESH_Y_DIST) * (float) i); };
139 144
 
140
-      void set_z(const int8_t px, const int8_t py, const float z) { z_values[px][py] = z; }
145
+      FORCE_INLINE void set_z(const int8_t px, const int8_t py, const float &z) { z_values[px][py] = z; }
141 146
 
142
-      int8_t get_cell_index_x(float x) {
143
-        int8_t cx = (x - (UBL_MESH_MIN_X)) * (1.0 / (MESH_X_DIST));
147
+      static int8_t get_cell_index_x(const float &x) {
148
+        const int8_t cx = (x - (UBL_MESH_MIN_X)) * (1.0 / (MESH_X_DIST));
144 149
         return constrain(cx, 0, (UBL_MESH_NUM_X_POINTS) - 1);   // -1 is appropriate if we want all movement to the X_MAX
145 150
       }                                                         // position. But with this defined this way, it is possible
146 151
                                                                 // to extrapolate off of this point even further out. Probably
147 152
                                                                 // that is OK because something else should be keeping that from
148 153
                                                                 // 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));
154
+      static int8_t get_cell_index_y(const float &y) {
155
+        const int8_t cy = (y - (UBL_MESH_MIN_Y)) * (1.0 / (MESH_Y_DIST));
151 156
         return constrain(cy, 0, (UBL_MESH_NUM_Y_POINTS) - 1);   // -1 is appropriate if we want all movement to the Y_MAX
152 157
       }                                                         // position. But with this defined this way, it is possible
153 158
                                                                 // to extrapolate off of this point even further out. Probably
154 159
                                                                 // that is OK because something else should be keeping that from
155 160
                                                                 // happening and should not be worried about at this level.
156 161
 
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));
162
+      static int8_t find_closest_x_index(const float &x) {
163
+        const int8_t px = (x - (UBL_MESH_MIN_X) + (MESH_X_DIST) * 0.5) * (1.0 / (MESH_X_DIST));
159 164
         return (px >= 0 && px < (UBL_MESH_NUM_X_POINTS)) ? px : -1;
160 165
       }
161 166
 
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));
167
+      static int8_t find_closest_y_index(const float &y) {
168
+        const int8_t py = (y - (UBL_MESH_MIN_Y) + (MESH_Y_DIST) * 0.5) * (1.0 / (MESH_Y_DIST));
164 169
         return (py >= 0 && py < (UBL_MESH_NUM_Y_POINTS)) ? py : -1;
165 170
       }
166 171
 
@@ -174,14 +179,14 @@
174 179
        *    |<---delta_a---------->|
175 180
        *
176 181
        *  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
182
+       *  find the expected Z Height at a position between two known Z-Height locations.
178 183
        *
179
-       *  It is farly expensive with its 4 floating point additions and 2 floating point
184
+       *  It is fairly expensive with its 4 floating point additions and 2 floating point
180 185
        *  multiplications.
181 186
        */
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);
187
+      static FORCE_INLINE float calc_z0(const float &a0, const float &a1, const float &z1, const float &a2, const float &z2) {
188
+        const float delta_z = (z2 - z1),
189
+                    delta_a = (a0 - a1) / (a2 - a1);
185 190
         return z1 + delta_a * delta_z;
186 191
       }
187 192
 
@@ -193,7 +198,7 @@
193 198
        * the X index of the x0 intersection available and we don't want to perform any extra floating
194 199
        * point operations.
195 200
        */
196
-      inline float get_z_correction_along_horizontal_mesh_line_at_specific_X(float x0, int x1_i, int yi) {
201
+      inline float get_z_correction_along_horizontal_mesh_line_at_specific_X(const float &x0, const int x1_i, const int yi) {
197 202
         if (x1_i < 0 || yi < 0 || x1_i >= UBL_MESH_NUM_X_POINTS || yi >= UBL_MESH_NUM_Y_POINTS) {
198 203
           SERIAL_ECHOPAIR("? in get_z_correction_along_horizontal_mesh_line_at_specific_X(x0=", x0);
199 204
           SERIAL_ECHOPAIR(",x1_i=", x1_i);
@@ -203,18 +208,18 @@
203 208
           return NAN;
204 209
         }
205 210
 
206
-        const float a0ma1diva2ma1 = (x0 - mesh_index_to_x_location[x1_i]) * (1.0 / (MESH_X_DIST)),
211
+        const float xratio = (RAW_X_POSITION(x0) - mesh_index_to_x_location[x1_i]) * (1.0 / (MESH_X_DIST)),
207 212
                     z1 = z_values[x1_i][yi],
208 213
                     z2 = z_values[x1_i + 1][yi],
209 214
                     dz = (z2 - z1);
210 215
 
211
-        return z1 + a0ma1diva2ma1 * dz;
216
+        return z1 + xratio * dz;
212 217
       }
213 218
 
214 219
       //
215 220
       // See comments above for get_z_correction_along_horizontal_mesh_line_at_specific_X
216 221
       //
217
-      inline float get_z_correction_along_vertical_mesh_line_at_specific_Y(float y0, int xi, int y1_i) {
222
+      inline float get_z_correction_along_vertical_mesh_line_at_specific_Y(const float &y0, const int xi, const int y1_i) {
218 223
         if (xi < 0 || y1_i < 0 || xi >= UBL_MESH_NUM_X_POINTS || y1_i >= UBL_MESH_NUM_Y_POINTS) {
219 224
           SERIAL_ECHOPAIR("? in get_z_correction_along_vertical_mesh_line_at_specific_X(y0=", y0);
220 225
           SERIAL_ECHOPAIR(", x1_i=", xi);
@@ -224,12 +229,12 @@
224 229
           return NAN;
225 230
         }
226 231
 
227
-        const float a0ma1diva2ma1 = (y0 - mesh_index_to_y_location[y1_i]) * (1.0 / (MESH_Y_DIST)),
232
+        const float yratio = (RAW_Y_POSITION(y0) - mesh_index_to_y_location[y1_i]) * (1.0 / (MESH_Y_DIST)),
228 233
                     z1 = z_values[xi][y1_i],
229 234
                     z2 = z_values[xi][y1_i + 1],
230 235
                     dz = (z2 - z1);
231 236
 
232
-        return z1 + a0ma1diva2ma1 * dz;
237
+        return z1 + yratio * dz;
233 238
       }
234 239
 
235 240
       /**
@@ -238,9 +243,9 @@
238 243
        * Z-Height at both ends. Then it does a linear interpolation of these heights based
239 244
        * on the Y position within the cell.
240 245
        */
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);
246
+      float get_z_correction(const float &x0, const float &y0) const {
247
+        const int8_t cx = get_cell_index_x(RAW_X_POSITION(x0)),
248
+                     cy = get_cell_index_y(RAW_Y_POSITION(y0));
244 249
 
245 250
         if (cx < 0 || cy < 0 || cx >= UBL_MESH_NUM_X_POINTS || cy >= UBL_MESH_NUM_Y_POINTS) {
246 251
 
@@ -256,15 +261,15 @@
256 261
           return 0.0; // this used to return state.z_offset
257 262
         }
258 263
 
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);
264
+        const float z1 = calc_z0(RAW_X_POSITION(x0),
265
+                      map_x_index_to_bed_location(cx), z_values[cx][cy],
266
+                      map_x_index_to_bed_location(cx + 1), z_values[cx + 1][cy]),
267
+                    z2 = calc_z0(RAW_X_POSITION(x0),
268
+                      map_x_index_to_bed_location(cx), z_values[cx][cy + 1],
269
+                      map_x_index_to_bed_location(cx + 1), z_values[cx + 1][cy + 1]);
270
+              float z0 = calc_z0(RAW_Y_POSITION(y0),
271
+                  map_y_index_to_bed_location(cy), z1,
272
+                  map_y_index_to_bed_location(cy + 1), z2);
268 273
 
269 274
         #if ENABLED(DEBUG_LEVELING_FEATURE)
270 275
           if (DEBUGGING(MESH_ADJUST)) {
@@ -308,27 +313,36 @@
308 313
        * factor is going to be the same as the last time the function calculated a value. If so, it just
309 314
        * returns it.
310 315
        *
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'
316
+       * It returns a scaling factor of 1.0 if UBL is inactive.
317
+       * It returns a scaling factor of 0.0 if Z is past the specified 'Fade Height'
313 318
        */
314
-      FORCE_INLINE float fade_scaling_factor_for_z(float current_z) {
315
-      #ifndef ENABLE_LEVELING_FADE_HEIGHT   // if turned off, just return 0.000    Note that we assume the
316
-        return 0.000;                       // compiler will do 'Dead Code' elimination so there is no need
317
-      #endif                                // for an #else clause here.
318
-        if (last_specified_z == current_z)
319
+      #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
320
+
321
+        FORCE_INLINE float fade_scaling_factor_for_z(const float &lz) const {
322
+          const float rz = RAW_Z_POSITION(lz);
323
+          if (last_specified_z != rz) {
324
+            last_specified_z = rz;
325
+            fade_scaling_factor_for_current_height =
326
+              state.active && rz < state.g29_correction_fade_height
327
+                ? 1.0 - (rz * state.g29_fade_height_multiplier)
328
+                : 0.0;
329
+          }
319 330
           return fade_scaling_factor_for_current_height;
331
+        }
320 332
 
321
-        last_specified_z = current_z;
322
-        fade_scaling_factor_for_current_height =
323
-          state.active && current_z < state.g29_correction_fade_height
324
-          ? 1.0 - (current_z * state.g29_fade_height_multiplier)
325
-          : 0.0;
326
-        return fade_scaling_factor_for_current_height;
327
-      }
328
-    };
333
+      #else
334
+
335
+        static constexpr float fade_scaling_factor_for_z(const float &lz) { UNUSED(lz); return 1.0; }
336
+
337
+      #endif
338
+
339
+    }; // class unified_bed_leveling
329 340
 
330 341
     extern unified_bed_leveling ubl;
331 342
     extern int ubl_eeprom_start;
332 343
 
333
-#endif // AUTO_BED_LEVELING_UBL
344
+    #define UBL_LAST_EEPROM_INDEX (E2END - sizeof(unified_bed_leveling::state))
345
+
346
+  #endif // AUTO_BED_LEVELING_UBL
347
+
334 348
 #endif // UNIFIED_BED_LEVELING_H

+ 39
- 31
Marlin/UBL_Bed_Leveling.cpp Zobrazit soubor

@@ -24,10 +24,21 @@
24 24
 #include "math.h"
25 25
 
26 26
 #if ENABLED(AUTO_BED_LEVELING_UBL)
27
+
27 28
   #include "UBL.h"
28 29
   #include "hex_print_routines.h"
29 30
 
30 31
   /**
32
+   * These support functions allow the use of large bit arrays of flags that take very
33
+   * little RAM. Currently they are limited to being 16x16 in size. Changing the declaration
34
+   * to unsigned long will allow us to go to 32x32 if higher resolution Mesh's are needed
35
+   * in the future.
36
+   */
37
+  void bit_clear(uint16_t bits[16], uint8_t x, uint8_t y) { CBI(bits[y], x); }
38
+  void bit_set(uint16_t bits[16], uint8_t x, uint8_t y) { SBI(bits[y], x); }
39
+  bool is_bit_set(uint16_t bits[16], uint8_t x, uint8_t y) { return TEST(bits[y], x); }
40
+
41
+  /**
31 42
    * These variables used to be declared inside the unified_bed_leveling class. We are going to
32 43
    * still declare them within the .cpp file for bed leveling. But there is only one instance of
33 44
    * the bed leveling object and we can get rid of a level of inderection by not making them
@@ -51,36 +62,36 @@
51 62
   }
52 63
 
53 64
   void unified_bed_leveling::store_state() {
54
-    int k = E2END - sizeof(ubl.state);
55
-    eeprom_write_block((void *)&ubl.state, (void *)k, sizeof(ubl.state));
65
+    const uint16_t i = UBL_LAST_EEPROM_INDEX;
66
+    eeprom_write_block((void *)&ubl.state, (void *)i, sizeof(state));
56 67
   }
57 68
 
58 69
   void unified_bed_leveling::load_state() {
59
-    int k = E2END - sizeof(ubl.state);
60
-    eeprom_read_block((void *)&ubl.state, (void *)k, sizeof(ubl.state));
70
+    const uint16_t i = UBL_LAST_EEPROM_INDEX;
71
+    eeprom_read_block((void *)&ubl.state, (void *)i, sizeof(state));
61 72
 
62 73
     if (sanity_check())
63 74
       SERIAL_PROTOCOLLNPGM("?In load_state() sanity_check() failed.\n");
64 75
 
65
-    /**
66
-     * These lines can go away in a few weeks.  They are just
67
-     * to make sure people updating thier firmware won't be using
68
-     * an incomplete Bed_Leveling.state structure. For speed
69
-     * we now multiply by the inverse of the Fade Height instead of
70
-     * dividing by it. Soon... all of the old structures will be
71
-     * updated, but until then, we try to ease the transition
72
-     * for our Beta testers.
73
-     */
74
-    if (ubl.state.g29_fade_height_multiplier != 1.0 / ubl.state.g29_correction_fade_height) {
75
-      ubl.state.g29_fade_height_multiplier = 1.0 / ubl.state.g29_correction_fade_height;
76
-      store_state();
77
-    }
78
-
76
+    #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
77
+      /**
78
+       * These lines can go away in a few weeks.  They are just
79
+       * to make sure people updating thier firmware won't be using
80
+       * an incomplete Bed_Leveling.state structure. For speed
81
+       * we now multiply by the inverse of the Fade Height instead of
82
+       * dividing by it. Soon... all of the old structures will be
83
+       * updated, but until then, we try to ease the transition
84
+       * for our Beta testers.
85
+       */
86
+      if (ubl.state.g29_fade_height_multiplier != 1.0 / ubl.state.g29_correction_fade_height) {
87
+        ubl.state.g29_fade_height_multiplier = 1.0 / ubl.state.g29_correction_fade_height;
88
+        store_state();
89
+      }
90
+    #endif
79 91
   }
80 92
 
81
-  void unified_bed_leveling::load_mesh(int m) {
82
-    int k = E2END - sizeof(ubl.state),
83
-        j = (k - ubl_eeprom_start) / sizeof(z_values);
93
+  void unified_bed_leveling::load_mesh(const int16_t m) {
94
+    int16_t j = (UBL_LAST_EEPROM_INDEX - ubl_eeprom_start) / sizeof(z_values);
84 95
 
85 96
     if (m == -1) {
86 97
       SERIAL_PROTOCOLLNPGM("?No mesh saved in EEPROM. Zeroing mesh in memory.\n");
@@ -93,7 +104,7 @@
93 104
       return;
94 105
     }
95 106
 
96
-    j = k - (m + 1) * sizeof(z_values);
107
+    j = UBL_LAST_EEPROM_INDEX - (m + 1) * sizeof(z_values);
97 108
     eeprom_read_block((void *)&z_values , (void *)j, sizeof(z_values));
98 109
 
99 110
     SERIAL_PROTOCOLPGM("Mesh loaded from slot ");
@@ -103,23 +114,22 @@
103 114
     SERIAL_EOL;
104 115
   }
105 116
 
106
-  void unified_bed_leveling:: store_mesh(int m) {
107
-    int k = E2END - sizeof(state),
108
-        j = (k - ubl_eeprom_start) / sizeof(z_values);
117
+  void unified_bed_leveling::store_mesh(const int16_t m) {
118
+    int16_t j = (UBL_LAST_EEPROM_INDEX - ubl_eeprom_start) / sizeof(z_values);
109 119
 
110 120
     if (m < 0 || m >= j || ubl_eeprom_start <= 0) {
111 121
       SERIAL_PROTOCOLLNPGM("?EEPROM storage not available to load mesh.\n");
112 122
       SERIAL_PROTOCOL(m);
113 123
       SERIAL_PROTOCOLLNPGM(" mesh slots available.\n");
114 124
       SERIAL_PROTOCOLLNPAIR("E2END     : ", E2END);
115
-      SERIAL_PROTOCOLLNPAIR("k         : ", k);
125
+      SERIAL_PROTOCOLLNPAIR("k         : ", (int)UBL_LAST_EEPROM_INDEX);
116 126
       SERIAL_PROTOCOLLNPAIR("j         : ", j);
117 127
       SERIAL_PROTOCOLLNPAIR("m         : ", m);
118 128
       SERIAL_EOL;
119 129
       return;
120 130
     }
121 131
 
122
-    j = k - (m + 1) * sizeof(z_values);
132
+    j = UBL_LAST_EEPROM_INDEX - (m + 1) * sizeof(z_values);
123 133
     eeprom_write_block((const void *)&z_values, (void *)j, sizeof(z_values));
124 134
 
125 135
     SERIAL_PROTOCOLPGM("Mesh saved in slot ");
@@ -151,7 +161,7 @@
151 161
         z_values[x][y] = NAN;
152 162
   }
153 163
 
154
-  void unified_bed_leveling::display_map(int map_type) {
164
+  void unified_bed_leveling::display_map(const int map_type) {
155 165
     float f, current_xi, current_yi;
156 166
     int8_t i, j;
157 167
     UNUSED(map_type);
@@ -287,9 +297,7 @@
287 297
       error_flag++;
288 298
     }
289 299
 
290
-    const int k = E2END - sizeof(ubl.state),
291
-              j = (k - ubl_eeprom_start) / sizeof(z_values);
292
-
300
+    const int j = (UBL_LAST_EEPROM_INDEX - ubl_eeprom_start) / sizeof(z_values);
293 301
     if (j < 1) {
294 302
       SERIAL_PROTOCOLLNPGM("?No EEPROM storage available for a mesh of this size.\n");
295 303
       error_flag++;

+ 110
- 157
Marlin/UBL_G29.cpp Zobrazit soubor

@@ -20,12 +20,14 @@
20 20
  *
21 21
  */
22 22
 
23
-#include "Marlin.h"
23
+#include "MarlinConfig.h"
24
+
24 25
 #if ENABLED(AUTO_BED_LEVELING_UBL)
25 26
   //#include "vector_3.h"
26 27
   //#include "qr_solve.h"
27 28
 
28 29
   #include "UBL.h"
30
+  #include "Marlin.h"
29 31
   #include "hex_print_routines.h"
30 32
   #include "configuration_store.h"
31 33
   #include "planner.h"
@@ -49,12 +51,13 @@
49 51
   #define DEPLOY_PROBE() set_probe_deployed(true)
50 52
   #define STOW_PROBE() set_probe_deployed(false)
51 53
   bool ProbeStay = true;
52
-  float ubl_3_point_1_X = UBL_PROBE_PT_1_X;
53
-  float ubl_3_point_1_Y = UBL_PROBE_PT_1_Y;
54
-  float ubl_3_point_2_X = UBL_PROBE_PT_2_X;
55
-  float ubl_3_point_2_Y = UBL_PROBE_PT_2_Y;
56
-  float ubl_3_point_3_X = UBL_PROBE_PT_3_X;
57
-  float ubl_3_point_3_Y = UBL_PROBE_PT_3_Y;
54
+
55
+  constexpr float ubl_3_point_1_X = UBL_PROBE_PT_1_X,
56
+                  ubl_3_point_1_Y = UBL_PROBE_PT_1_Y,
57
+                  ubl_3_point_2_X = UBL_PROBE_PT_2_X,
58
+                  ubl_3_point_2_Y = UBL_PROBE_PT_2_Y,
59
+                  ubl_3_point_3_X = UBL_PROBE_PT_3_X,
60
+                  ubl_3_point_3_Y = UBL_PROBE_PT_3_Y;
58 61
 
59 62
   #define SIZE_OF_LITTLE_RAISE 0
60 63
   #define BIG_RAISE_NOT_NEEDED 0
@@ -293,19 +296,16 @@
293 296
   volatile uint8_t ubl_encoderDiff = 0; // Volatile because it's changed by Temperature ISR button update
294 297
 
295 298
   // The simple parameter flags and values are 'static' so parameter parsing can be in a support routine.
296
-  static int g29_verbose_level = 0, test_value = 0,
297
-             phase_value = -1, repetition_cnt = 1;
299
+  static int g29_verbose_level = 0, phase_value = -1, repetition_cnt = 1,
300
+             storage_slot = 0, test_pattern = 0;
298 301
   static bool repeat_flag = UBL_OK, c_flag = false, x_flag = UBL_OK, y_flag = UBL_OK, statistics_flag = UBL_OK, business_card_mode = false;
299 302
   static float x_pos = 0.0, y_pos = 0.0, height_value = 5.0, measured_z, card_thickness = 0.0, constant = 0.0;
300
-  static int storage_slot = 0, test_pattern = 0;
301 303
 
302 304
   #if ENABLED(ULTRA_LCD)
303 305
     void lcd_setstatus(const char* message, bool persist);
304 306
   #endif
305 307
 
306 308
   void gcode_G29() {
307
-    mesh_index_pair location;
308
-    int j, k;
309 309
     float Z1, Z2, Z3;
310 310
 
311 311
     g29_verbose_level = 0;  // These may change, but let's get some reasonable values into them.
@@ -331,7 +331,7 @@
331 331
     if (code_seen('I')) {
332 332
       repetition_cnt = code_has_value() ? code_value_int() : 1;
333 333
       while (repetition_cnt--) {
334
-        location = find_closest_mesh_point_of_type(REAL, x_pos, y_pos, 0, NULL);  // The '0' says we want to use the nozzle's position
334
+        const mesh_index_pair location = find_closest_mesh_point_of_type(REAL, x_pos, y_pos, 0, NULL);  // The '0' says we want to use the nozzle's position
335 335
         if (location.x_index < 0) {
336 336
           SERIAL_PROTOCOLLNPGM("Entire Mesh invalidated.\n");
337 337
           break;            // No more invalid Mesh Points to populate
@@ -409,8 +409,8 @@
409 409
             SERIAL_ECHOPAIR(",", y_pos);
410 410
             SERIAL_PROTOCOLLNPGM(")\n");
411 411
           }
412
-          probe_entire_mesh( x_pos+X_PROBE_OFFSET_FROM_EXTRUDER, y_pos+Y_PROBE_OFFSET_FROM_EXTRUDER,
413
-                             code_seen('O') || code_seen('M'), code_seen('E'));
412
+          probe_entire_mesh(x_pos + X_PROBE_OFFSET_FROM_EXTRUDER, y_pos + Y_PROBE_OFFSET_FROM_EXTRUDER,
413
+                            code_seen('O') || code_seen('M'), code_seen('E'));
414 414
           break;
415 415
         //
416 416
         // Manually Probe Mesh in areas that can not be reached by the probe
@@ -455,7 +455,7 @@
455 455
           // If no repetition is specified, do the whole Mesh
456 456
           if (!repeat_flag) repetition_cnt = 9999;
457 457
           while (repetition_cnt--) {
458
-            location = find_closest_mesh_point_of_type(INVALID, x_pos, y_pos, 0, NULL); // The '0' says we want to use the nozzle's position
458
+            const mesh_index_pair location = find_closest_mesh_point_of_type(INVALID, x_pos, y_pos, 0, NULL); // The '0' says we want to use the nozzle's position
459 459
             if (location.x_index < 0) break; // No more invalid Mesh Points to populate
460 460
             z_values[location.x_index][location.y_index] = height_value;
461 461
           }
@@ -534,8 +534,7 @@
534 534
     if (code_seen('L')) {     // Load Current Mesh Data
535 535
       storage_slot = code_has_value() ? code_value_int() : ubl.state.eeprom_storage_slot;
536 536
 
537
-      k = E2END - sizeof(ubl.state);
538
-      j = (k - ubl_eeprom_start) / sizeof(z_values);
537
+      const int16_t j = (UBL_LAST_EEPROM_INDEX - ubl_eeprom_start) / sizeof(z_values);
539 538
 
540 539
       if (storage_slot < 0 || storage_slot >= j || ubl_eeprom_start <= 0) {
541 540
         SERIAL_PROTOCOLLNPGM("?EEPROM storage not available for use.\n");
@@ -569,8 +568,7 @@
569 568
         return;
570 569
       }
571 570
 
572
-      int k = E2END - sizeof(ubl.state),
573
-          j = (k - ubl_eeprom_start) / sizeof(z_values);
571
+      const int16_t j = (UBL_LAST_EEPROM_INDEX - ubl_eeprom_start) / sizeof(z_values);
574 572
 
575 573
       if (storage_slot < 0 || storage_slot >= j || ubl_eeprom_start <= 0) {
576 574
         SERIAL_PROTOCOLLNPGM("?EEPROM storage not available for use.\n");
@@ -691,7 +689,7 @@
691 689
             z_values[x][y] -= mean + constant;
692 690
   }
693 691
 
694
-  void shift_mesh_height( ) {
692
+  void shift_mesh_height() {
695 693
     for (uint8_t x = 0; x < UBL_MESH_NUM_X_POINTS; x++)
696 694
       for (uint8_t y = 0; y < UBL_MESH_NUM_Y_POINTS; y++)
697 695
         if (!isnan(z_values[x][y]))
@@ -702,9 +700,8 @@
702 700
    * Probe all invalidated locations of the mesh that can be reached by the probe.
703 701
    * This attempts to fill in locations closest to the nozzle's start location first.
704 702
    */
705
-  void probe_entire_mesh(float x_pos, float y_pos, bool do_ubl_mesh_map, bool stow_probe) {
703
+  void probe_entire_mesh(const float &lx, const float &ly, const bool do_ubl_mesh_map, const bool stow_probe) {
706 704
     mesh_index_pair location;
707
-    float xProbe, yProbe, measured_z;
708 705
 
709 706
     ubl_has_control_of_lcd_panel++;
710 707
     save_ubl_active_state_and_disable();   // we don't do bed level correction because we want the raw data when we probe
@@ -720,20 +717,22 @@
720 717
         restore_ubl_active_state_and_leave();
721 718
         return;
722 719
       }
723
-      location = find_closest_mesh_point_of_type(INVALID, x_pos, y_pos, 1, NULL);  // the '1' says we want the location to be relative to the probe
720
+
721
+      location = find_closest_mesh_point_of_type(INVALID, lx, ly, 1, NULL);  // the '1' says we want the location to be relative to the probe
724 722
       if (location.x_index >= 0 && location.y_index >= 0) {
725
-        xProbe = ubl.map_x_index_to_bed_location(location.x_index);
726
-        yProbe = ubl.map_y_index_to_bed_location(location.y_index);
723
+        const float xProbe = ubl.map_x_index_to_bed_location(location.x_index),
724
+                    yProbe = ubl.map_y_index_to_bed_location(location.y_index);
727 725
         if (xProbe < MIN_PROBE_X || xProbe > MAX_PROBE_X || yProbe < MIN_PROBE_Y || yProbe > MAX_PROBE_Y) {
728 726
           SERIAL_PROTOCOLLNPGM("?Error: Attempt to probe off the bed.");
729 727
           ubl_has_control_of_lcd_panel = false;
730 728
           goto LEAVE;
731 729
         }
732
-        measured_z = probe_pt(xProbe, yProbe, stow_probe, g29_verbose_level);
730
+        const float measured_z = probe_pt(xProbe, yProbe, stow_probe, g29_verbose_level);
733 731
         z_values[location.x_index][location.y_index] = measured_z + Z_PROBE_OFFSET_FROM_EXTRUDER;
734 732
       }
735 733
 
736 734
       if (do_ubl_mesh_map) ubl.display_map(1);
735
+
737 736
     } while (location.x_index >= 0 && location.y_index >= 0);
738 737
 
739 738
     LEAVE:
@@ -742,32 +741,27 @@
742 741
     STOW_PROBE();
743 742
     restore_ubl_active_state_and_leave();
744 743
 
745
-    x_pos = constrain(x_pos - (X_PROBE_OFFSET_FROM_EXTRUDER), X_MIN_POS, X_MAX_POS);
746
-    y_pos = constrain(y_pos - (Y_PROBE_OFFSET_FROM_EXTRUDER), Y_MIN_POS, Y_MAX_POS);
747
-
748
-    do_blocking_move_to_xy(x_pos, y_pos);
744
+    do_blocking_move_to_xy(
745
+      constrain(lx - (X_PROBE_OFFSET_FROM_EXTRUDER), X_MIN_POS, X_MAX_POS),
746
+      constrain(ly - (Y_PROBE_OFFSET_FROM_EXTRUDER), Y_MIN_POS, Y_MAX_POS)
747
+    );
749 748
   }
750 749
 
751
-  vector tilt_mesh_based_on_3pts(float pt1, float pt2, float pt3) {
752
-    vector v1, v2, normal;
750
+  vector_3 tilt_mesh_based_on_3pts(const float &pt1, const float &pt2, const float &pt3) {
753 751
     float c, d, t;
754 752
     int i, j;
755 753
 
756
-    v1.dx = (ubl_3_point_1_X - ubl_3_point_2_X);
757
-    v1.dy = (ubl_3_point_1_Y - ubl_3_point_2_Y);
758
-    v1.dz = (pt1 - pt2);
759
-
760
-    v2.dx = (ubl_3_point_3_X - ubl_3_point_2_X);
761
-    v2.dy = (ubl_3_point_3_Y - ubl_3_point_2_Y);
762
-    v2.dz = (pt3 - pt2);
754
+    vector_3 v1 = vector_3( (ubl_3_point_1_X - ubl_3_point_2_X),
755
+                            (ubl_3_point_1_Y - ubl_3_point_2_Y),
756
+                            (pt1 - pt2) ),
763 757
 
764
-    // do cross product
758
+             v2 = vector_3( (ubl_3_point_3_X - ubl_3_point_2_X),
759
+                            (ubl_3_point_3_Y - ubl_3_point_2_Y),
760
+                            (pt3 - pt2) ),
765 761
 
766
-    normal.dx = v1.dy * v2.dz - v1.dz * v2.dy;
767
-    normal.dy = v1.dz * v2.dx - v1.dx * v2.dz;
768
-    normal.dz = v1.dx * v2.dy - v1.dy * v2.dx;
762
+             normal = vector_3::cross(v1, v2);
769 763
 
770
-    // printf("[%f,%f,%f]    ", normal.dx, normal.dy, normal.dz);
764
+    // printf("[%f,%f,%f]    ", normal.x, normal.y, normal.z);
771 765
 
772 766
     /**
773 767
      * This code does two things. This vector is normal to the tilted plane.
@@ -776,31 +770,32 @@
776 770
      * We also need Z to be unity because we are going to be treating this triangle
777 771
      * as the sin() and cos() of the bed's tilt
778 772
      */
779
-    normal.dx /= normal.dz;
780
-    normal.dy /= normal.dz;
781
-    normal.dz /= normal.dz;
773
+    const float inv_z = 1.0 / normal.z;
774
+    normal.x *= inv_z;
775
+    normal.y *= inv_z;
776
+    normal.z = 1.0;
782 777
 
783 778
     //
784 779
     // All of 3 of these points should give us the same d constant
785 780
     //
786
-    t = normal.dx * ubl_3_point_1_X + normal.dy * ubl_3_point_1_Y;
787
-    d = t + normal.dz * pt1;
781
+    t = normal.x * ubl_3_point_1_X + normal.y * ubl_3_point_1_Y;
782
+    d = t + normal.z * pt1;
788 783
     c = d - t;
789 784
     SERIAL_ECHOPGM("d from 1st point: ");
790 785
     SERIAL_ECHO_F(d, 6);
791 786
     SERIAL_ECHOPGM("  c: ");
792 787
     SERIAL_ECHO_F(c, 6);
793 788
     SERIAL_EOL;
794
-    t = normal.dx * ubl_3_point_2_X + normal.dy * ubl_3_point_2_Y;
795
-    d = t + normal.dz * pt2;
789
+    t = normal.x * ubl_3_point_2_X + normal.y * ubl_3_point_2_Y;
790
+    d = t + normal.z * pt2;
796 791
     c = d - t;
797 792
     SERIAL_ECHOPGM("d from 2nd point: ");
798 793
     SERIAL_ECHO_F(d, 6);
799 794
     SERIAL_ECHOPGM("  c: ");
800 795
     SERIAL_ECHO_F(c, 6);
801 796
     SERIAL_EOL;
802
-    t = normal.dx * ubl_3_point_3_X + normal.dy * ubl_3_point_3_Y;
803
-    d = t + normal.dz * pt3;
797
+    t = normal.x * ubl_3_point_3_X + normal.y * ubl_3_point_3_Y;
798
+    d = t + normal.z * pt3;
804 799
     c = d - t;
805 800
     SERIAL_ECHOPGM("d from 3rd point: ");
806 801
     SERIAL_ECHO_F(d, 6);
@@ -810,7 +805,7 @@
810 805
 
811 806
     for (i = 0; i < UBL_MESH_NUM_X_POINTS; i++) {
812 807
       for (j = 0; j < UBL_MESH_NUM_Y_POINTS; j++) {
813
-        c = -((normal.dx * (UBL_MESH_MIN_X + i * (MESH_X_DIST)) + normal.dy * (UBL_MESH_MIN_Y + j * (MESH_Y_DIST))) - d);
808
+        c = -((normal.x * (UBL_MESH_MIN_X + i * (MESH_X_DIST)) + normal.y * (UBL_MESH_MIN_Y + j * (MESH_Y_DIST))) - d);
814 809
         z_values[i][j] += c;
815 810
       }
816 811
     }
@@ -829,7 +824,7 @@
829 824
     return current_position[Z_AXIS];
830 825
   }
831 826
 
832
-  float measure_business_card_thickness(float height_value) {
827
+  float measure_business_card_thickness(const float &height_value) {
833 828
 
834 829
     ubl_has_control_of_lcd_panel++;
835 830
     save_ubl_active_state_and_disable();   // we don't do bed level correction because we want the raw data when we probe
@@ -856,44 +851,45 @@
856 851
     return abs(Z1 - Z2);
857 852
   }
858 853
 
859
-  void manually_probe_remaining_mesh(float x_pos, float y_pos, float z_clearance, float card_thickness, bool do_ubl_mesh_map) {
860
-    mesh_index_pair location;
861
-    float last_x, last_y, dx, dy,
862
-          xProbe, yProbe;
854
+  void manually_probe_remaining_mesh(const float &lx, const float &ly, const float &z_clearance, const float &card_thickness, const bool do_ubl_mesh_map) {
863 855
 
864 856
     ubl_has_control_of_lcd_panel++;
865
-    last_x = last_y = -9999.99;
866 857
     save_ubl_active_state_and_disable();   // we don't do bed level correction because we want the raw data when we probe
867 858
     do_blocking_move_to_z(z_clearance);
868
-    do_blocking_move_to_xy(x_pos, y_pos);
859
+    do_blocking_move_to_xy(lx, ly);
869 860
 
861
+    float last_x = -9999.99, last_y = -9999.99;
862
+    mesh_index_pair location;
870 863
     do {
871 864
       if (do_ubl_mesh_map) ubl.display_map(1);
872 865
 
873
-      location = find_closest_mesh_point_of_type(INVALID, x_pos, y_pos, 0, NULL); // The '0' says we want to use the nozzle's position
866
+      location = find_closest_mesh_point_of_type(INVALID, lx, ly, 0, NULL); // The '0' says we want to use the nozzle's position
874 867
       // It doesn't matter if the probe can not reach the
875 868
       // NAN location. This is a manual probe.
876 869
       if (location.x_index < 0 && location.y_index < 0) continue;
877 870
 
878
-      xProbe = ubl.map_x_index_to_bed_location(location.x_index);
879
-      yProbe = ubl.map_y_index_to_bed_location(location.y_index);
871
+      const float xProbe = ubl.map_x_index_to_bed_location(location.x_index),
872
+                  yProbe = ubl.map_y_index_to_bed_location(location.y_index);
873
+
874
+      // Modify to use if (position_is_reachable(pos[XYZ]))
880 875
       if (xProbe < (X_MIN_POS) || xProbe > (X_MAX_POS) || yProbe < (Y_MIN_POS) || yProbe > (Y_MAX_POS)) {
881 876
         SERIAL_PROTOCOLLNPGM("?Error: Attempt to probe off the bed.");
882 877
         ubl_has_control_of_lcd_panel = false;
883 878
         goto LEAVE;
884 879
       }
885 880
 
886
-      dx = xProbe - last_x;
887
-      dy = yProbe - last_y;
881
+      const float dx = xProbe - last_x,
882
+                  dy = yProbe - last_y;
888 883
 
889 884
       if (HYPOT(dx, dy) < BIG_RAISE_NOT_NEEDED)
890 885
         do_blocking_move_to_z(current_position[Z_AXIS] + SIZE_OF_LITTLE_RAISE);
891 886
       else
892 887
         do_blocking_move_to_z(z_clearance);
893 888
 
889
+      do_blocking_move_to_xy(xProbe, yProbe);
890
+
894 891
       last_x = xProbe;
895 892
       last_y = yProbe;
896
-      do_blocking_move_to_xy(xProbe, yProbe);
897 893
 
898 894
       wait_for_user = true;
899 895
       while (wait_for_user) {     // we need the loop to move the nozzle based on the encoder wheel here!
@@ -931,7 +927,7 @@
931 927
     LEAVE:
932 928
     restore_ubl_active_state_and_leave();
933 929
     do_blocking_move_to_z(Z_CLEARANCE_DEPLOY_PROBE);
934
-    do_blocking_move_to_xy(x_pos, y_pos);
930
+    do_blocking_move_to_xy(lx, ly);
935 931
   }
936 932
 
937 933
   bool g29_parameter_parsing() {
@@ -983,7 +979,7 @@
983 979
       ubl.store_state();
984 980
     }
985 981
 
986
-    if ((c_flag = code_seen('C')) && code_has_value())
982
+    if ((c_flag = code_seen('C') && code_has_value()))
987 983
       constant = code_value_float();
988 984
 
989 985
     if (code_seen('D')) {     // Disable the Unified Bed Leveling System
@@ -992,19 +988,17 @@
992 988
       ubl.store_state();
993 989
     }
994 990
 
995
-    if (code_seen('F')) {
996
-      ubl.state.g29_correction_fade_height = 10.00;
997
-      if (code_has_value()) {
998
-        ubl.state.g29_correction_fade_height = code_value_float();
999
-        ubl.state.g29_fade_height_multiplier = 1.0 / ubl.state.g29_correction_fade_height;
1000
-      }
1001
-      if (ubl.state.g29_correction_fade_height < 0.0 || ubl.state.g29_correction_fade_height > 100.0) {
1002
-        SERIAL_PROTOCOLLNPGM("?Bed Level Correction Fade Height Not Plausible.\n");
1003
-        ubl.state.g29_correction_fade_height = 10.00;
1004
-        ubl.state.g29_fade_height_multiplier = 1.0 / ubl.state.g29_correction_fade_height;
1005
-        return UBL_ERR;
991
+    #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
992
+      if (code_seen('F') && code_has_value()) {
993
+        const float fh = code_value_float();
994
+        if (fh < 0.0 || fh > 100.0) {
995
+          SERIAL_PROTOCOLLNPGM("?Bed Level Correction Fade Height Not Plausible.\n");
996
+          return UBL_ERR;
997
+        }
998
+        ubl.state.g29_correction_fade_height = fh;
999
+        ubl.state.g29_fade_height_multiplier = 1.0 / fh;
1006 1000
       }
1007
-    }
1001
+    #endif
1008 1002
 
1009 1003
     if ((repeat_flag = code_seen('R'))) {
1010 1004
       repetition_cnt = code_has_value() ? code_value_int() : 9999;
@@ -1020,7 +1014,7 @@
1020 1014
    * This function goes away after G29 debug is complete. But for right now, it is a handy
1021 1015
    * routine to dump binary data structures.
1022 1016
    */
1023
-  void dump(char *str, float f) {
1017
+  void dump(char * const str, const float &f) {
1024 1018
     char *ptr;
1025 1019
 
1026 1020
     SERIAL_PROTOCOL(str);
@@ -1056,7 +1050,6 @@
1056 1050
     }
1057 1051
     ubl_state_at_invocation = ubl.state.active;
1058 1052
     ubl.state.active = 0;
1059
-    return;
1060 1053
   }
1061 1054
 
1062 1055
   void restore_ubl_active_state_and_leave() {
@@ -1075,33 +1068,27 @@
1075 1068
    * good to have the extra information. Soon... we prune this to just a few items
1076 1069
    */
1077 1070
   void g29_what_command() {
1078
-    int k = E2END - ubl_eeprom_start;
1071
+    const uint16_t k = E2END - ubl_eeprom_start;
1079 1072
     statistics_flag++;
1080 1073
 
1081 1074
     SERIAL_PROTOCOLPGM("Unified Bed Leveling System Version 1.00 ");
1082
-    if (ubl.state.active)
1083
-      SERIAL_PROTOCOLPGM("Active.\n");
1084
-    else
1085
-      SERIAL_PROTOCOLPGM("Inactive.\n");
1086
-    SERIAL_EOL;
1075
+    ubl.state.active ? SERIAL_PROTOCOLCHAR('A') : SERIAL_PROTOCOLPGM("In");
1076
+    SERIAL_PROTOCOLLNPGM("ctive.\n");
1087 1077
     delay(50);
1088 1078
 
1089
-    if (ubl.state.eeprom_storage_slot == 0xFFFF) {
1079
+    if (ubl.state.eeprom_storage_slot == -1)
1090 1080
       SERIAL_PROTOCOLPGM("No Mesh Loaded.");
1091
-    }
1092 1081
     else {
1093 1082
       SERIAL_PROTOCOLPGM("Mesh: ");
1094 1083
       prt_hex_word(ubl.state.eeprom_storage_slot);
1095
-      SERIAL_PROTOCOLPGM(" Loaded. ");
1084
+      SERIAL_PROTOCOLPGM(" Loaded.");
1096 1085
     }
1097
-
1098 1086
     SERIAL_EOL;
1099
-    delay(50);
1100 1087
 
1101
-    SERIAL_PROTOCOLPAIR("g29_correction_fade_height : ", ubl.state.g29_correction_fade_height );
1102
-    SERIAL_EOL;
1103
-
1104
-    idle();
1088
+    #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
1089
+      SERIAL_PROTOCOLPAIR("g29_correction_fade_height : ", ubl.state.g29_correction_fade_height);
1090
+      SERIAL_EOL;
1091
+    #endif
1105 1092
 
1106 1093
     SERIAL_PROTOCOLPGM("z_offset: ");
1107 1094
     SERIAL_PROTOCOL_F(ubl.state.z_offset, 6);
@@ -1111,28 +1098,20 @@
1111 1098
     for (uint8_t i = 0; i < UBL_MESH_NUM_X_POINTS; i++) {
1112 1099
       SERIAL_PROTOCOL_F( ubl.map_x_index_to_bed_location(i), 1);
1113 1100
       SERIAL_PROTOCOLPGM("  ");
1114
-      delay(10);
1115 1101
     }
1116 1102
     SERIAL_EOL;
1117
-    delay(50);
1118
-    idle();
1119 1103
 
1120 1104
     SERIAL_PROTOCOLPGM("Y-Axis Mesh Points at: ");
1121 1105
     for (uint8_t i = 0; i < UBL_MESH_NUM_Y_POINTS; i++) {
1122 1106
       SERIAL_PROTOCOL_F( ubl.map_y_index_to_bed_location(i), 1);
1123 1107
       SERIAL_PROTOCOLPGM("  ");
1124
-      delay(10);
1125 1108
     }
1126 1109
     SERIAL_EOL;
1127
-    delay(50);
1128
-    idle();
1129 1110
 
1130 1111
     #if HAS_KILL
1131 1112
       SERIAL_PROTOCOLPAIR("Kill pin on :", KILL_PIN);
1132 1113
       SERIAL_PROTOCOLLNPAIR("  state:", READ(KILL_PIN));
1133 1114
     #endif
1134
-    delay(50);
1135
-    idle();
1136 1115
     SERIAL_EOL;
1137 1116
 
1138 1117
     SERIAL_PROTOCOLLNPAIR("ubl_state_at_invocation :", ubl_state_at_invocation);
@@ -1142,54 +1121,39 @@
1142 1121
     SERIAL_PROTOCOLPGM("Free EEPROM space starts at: 0x");
1143 1122
     prt_hex_word(ubl_eeprom_start);
1144 1123
     SERIAL_EOL;
1145
-    delay(50);
1146
-    idle();
1147 1124
 
1148 1125
     SERIAL_PROTOCOLPGM("end of EEPROM              : ");
1149 1126
     prt_hex_word(E2END);
1150 1127
     SERIAL_EOL;
1151
-    delay(50);
1152
-    idle();
1153 1128
 
1154 1129
     SERIAL_PROTOCOLLNPAIR("sizeof(ubl) :  ", (int)sizeof(ubl));
1155 1130
     SERIAL_EOL;
1156 1131
     SERIAL_PROTOCOLLNPAIR("z_value[][] size: ", (int)sizeof(z_values));
1157 1132
     SERIAL_EOL;
1158
-    delay(50);
1159
-    idle();
1160 1133
 
1161 1134
     SERIAL_PROTOCOLPGM("EEPROM free for UBL: 0x");
1162 1135
     prt_hex_word(k);
1163 1136
     SERIAL_EOL;
1164
-    idle();
1165 1137
 
1166 1138
     SERIAL_PROTOCOLPGM("EEPROM can hold 0x");
1167 1139
     prt_hex_word(k / sizeof(z_values));
1168 1140
     SERIAL_PROTOCOLLNPGM(" meshes.\n");
1169
-    delay(50);
1170 1141
 
1171 1142
     SERIAL_PROTOCOLPGM("sizeof(ubl.state) :");
1172 1143
     prt_hex_word(sizeof(ubl.state));
1173
-    idle();
1174 1144
 
1175 1145
     SERIAL_PROTOCOLPAIR("\nUBL_MESH_NUM_X_POINTS  ", UBL_MESH_NUM_X_POINTS);
1176 1146
     SERIAL_PROTOCOLPAIR("\nUBL_MESH_NUM_Y_POINTS  ", UBL_MESH_NUM_Y_POINTS);
1177 1147
     SERIAL_PROTOCOLPAIR("\nUBL_MESH_MIN_X         ", UBL_MESH_MIN_X);
1178
-    delay(50);
1179
-    idle();
1180 1148
     SERIAL_PROTOCOLPAIR("\nUBL_MESH_MIN_Y         ", UBL_MESH_MIN_Y);
1181 1149
     SERIAL_PROTOCOLPAIR("\nUBL_MESH_MAX_X         ", UBL_MESH_MAX_X);
1182 1150
     SERIAL_PROTOCOLPAIR("\nUBL_MESH_MAX_Y         ", UBL_MESH_MAX_Y);
1183
-    delay(50);
1184
-    idle();
1185 1151
     SERIAL_PROTOCOLPGM("\nMESH_X_DIST        ");
1186 1152
     SERIAL_PROTOCOL_F(MESH_X_DIST, 6);
1187 1153
     SERIAL_PROTOCOLPGM("\nMESH_Y_DIST        ");
1188 1154
     SERIAL_PROTOCOL_F(MESH_Y_DIST, 6);
1189 1155
     SERIAL_EOL;
1190 1156
 
1191
-    idle();
1192
-
1193 1157
     if (!ubl.sanity_check())
1194 1158
       SERIAL_PROTOCOLLNPGM("Unified Bed Leveling sanity checks passed.");
1195 1159
   }
@@ -1205,7 +1169,7 @@
1205 1169
     SERIAL_ECHO_START;
1206 1170
     SERIAL_ECHOLNPGM("EEPROM Dump:");
1207 1171
     for (uint16_t i = 0; i < E2END + 1; i += 16) {
1208
-      if (i & 0x3 == 0) idle();
1172
+      if (!(i & 0x3)) idle();
1209 1173
       prt_hex_word(i);
1210 1174
       SERIAL_ECHOPGM(": ");
1211 1175
       for (uint16_t j = 0; j < 16; j++) {
@@ -1217,7 +1181,6 @@
1217 1181
       SERIAL_EOL;
1218 1182
     }
1219 1183
     SERIAL_EOL;
1220
-    return;
1221 1184
   }
1222 1185
 
1223 1186
   /**
@@ -1233,15 +1196,14 @@
1233 1196
     }
1234 1197
     storage_slot = code_value_int();
1235 1198
 
1236
-    uint16_t k = E2END - sizeof(ubl.state),
1237
-             j = (k - ubl_eeprom_start) / sizeof(tmp_z_values);
1199
+    int16_t j = (UBL_LAST_EEPROM_INDEX - ubl_eeprom_start) / sizeof(tmp_z_values);
1238 1200
 
1239 1201
     if (storage_slot < 0 || storage_slot > j || ubl_eeprom_start <= 0) {
1240 1202
       SERIAL_PROTOCOLLNPGM("?EEPROM storage not available for use.\n");
1241 1203
       return;
1242 1204
     }
1243 1205
 
1244
-    j = k - (storage_slot + 1) * sizeof(tmp_z_values);
1206
+    j = UBL_LAST_EEPROM_INDEX - (storage_slot + 1) * sizeof(tmp_z_values);
1245 1207
     eeprom_read_block((void *)&tmp_z_values, (void *)j, sizeof(tmp_z_values));
1246 1208
 
1247 1209
     SERIAL_ECHOPAIR("Subtracting Mesh ", storage_slot);
@@ -1254,23 +1216,19 @@
1254 1216
         z_values[x][y] = z_values[x][y] - tmp_z_values[x][y];
1255 1217
   }
1256 1218
 
1257
-  mesh_index_pair find_closest_mesh_point_of_type(MeshPointType type, float X, float Y, bool probe_as_reference, unsigned int bits[16]) {
1219
+  mesh_index_pair find_closest_mesh_point_of_type(const MeshPointType type, const float &lx, const float &ly, const bool probe_as_reference, unsigned int bits[16]) {
1258 1220
     int i, j;
1259
-    float f, px, py, mx, my, dx, dy, closest = 99999.99,
1260
-          current_x, current_y, distance;
1221
+    float closest = 99999.99;
1261 1222
     mesh_index_pair return_val;
1262 1223
 
1263 1224
     return_val.x_index = return_val.y_index = -1;
1264 1225
 
1265
-    current_x = current_position[X_AXIS];
1266
-    current_y = current_position[Y_AXIS];
1226
+    const float current_x = current_position[X_AXIS],
1227
+                current_y = current_position[Y_AXIS];
1267 1228
 
1268
-    px = X;       // Get our reference position. Either the nozzle or
1269
-    py = Y;       // the probe location.
1270
-    if (probe_as_reference) {
1271
-      px -= X_PROBE_OFFSET_FROM_EXTRUDER;
1272
-      py -= Y_PROBE_OFFSET_FROM_EXTRUDER;
1273
-    }
1229
+    // Get our reference position. Either the nozzle or probe location.
1230
+    const float px = lx - (probe_as_reference ? X_PROBE_OFFSET_FROM_EXTRUDER : 0),
1231
+                py = ly - (probe_as_reference ? Y_PROBE_OFFSET_FROM_EXTRUDER : 0);
1274 1232
 
1275 1233
     for (i = 0; i < UBL_MESH_NUM_X_POINTS; i++) {
1276 1234
       for (j = 0; j < UBL_MESH_NUM_Y_POINTS; j++) {
@@ -1282,24 +1240,20 @@
1282 1240
 
1283 1241
           // We only get here if we found a Mesh Point of the specified type
1284 1242
 
1285
-          mx = ubl.map_x_index_to_bed_location(i); // Check if we can probe this mesh location
1286
-          my = ubl.map_y_index_to_bed_location(j);
1243
+          const float mx = LOGICAL_X_POSITION(ubl.map_x_index_to_bed_location(i)), // Check if we can probe this mesh location
1244
+                      my = LOGICAL_Y_POSITION(ubl.map_y_index_to_bed_location(j));
1287 1245
 
1288 1246
           // If we are using the probe as the reference there are some locations we can't get to.
1289 1247
           // We prune these out of the list and ignore them until the next Phase where we do the
1290 1248
           // manual nozzle probing.
1291
-	  
1249
+
1292 1250
           if (probe_as_reference &&
1293
-            ( mx < (MIN_PROBE_X) || mx > (MAX_PROBE_X) || my < (MIN_PROBE_Y) || my > (MAX_PROBE_Y) )
1251
+            (mx < (MIN_PROBE_X) || mx > (MAX_PROBE_X) || my < (MIN_PROBE_Y) || my > (MAX_PROBE_Y))
1294 1252
           ) continue;
1295 1253
 
1296
-          dx = px - mx;         // We can get to it. Let's see if it is the
1297
-          dy = py - my;         // closest location to the nozzle.
1298
-          distance = HYPOT(dx, dy);
1299
-
1300
-          dx = current_x - mx;                    // We are going to add in a weighting factor that considers
1301
-          dy = current_y - my;                    // the current location of the nozzle. If two locations are equal
1302
-          distance += HYPOT(dx, dy) * 0.01;       // distance from the measurement location, we are going to give
1254
+          // We can get to it. Let's see if it is the closest location to the nozzle.
1255
+          // Add in a weighting factor that considers the current location of the nozzle.
1256
+          const float distance = HYPOT(px - mx, py - my) + HYPOT(current_x - mx, current_y - my) * 0.01;
1303 1257
 
1304 1258
           if (distance < closest) {
1305 1259
             closest = distance;       // We found a closer location with
@@ -1313,10 +1267,9 @@
1313 1267
     return return_val;
1314 1268
   }
1315 1269
 
1316
-  void fine_tune_mesh(float x_pos, float y_pos, bool do_ubl_mesh_map) {
1270
+  void fine_tune_mesh(const float &lx, const float &ly, const bool do_ubl_mesh_map) {
1317 1271
     mesh_index_pair location;
1318
-    float xProbe, yProbe;
1319
-    uint16_t i, not_done[16];
1272
+    uint16_t not_done[16];
1320 1273
     int32_t round_off;
1321 1274
 
1322 1275
     save_ubl_active_state_and_disable();
@@ -1327,11 +1280,11 @@
1327 1280
     #endif
1328 1281
 
1329 1282
     do_blocking_move_to_z(Z_CLEARANCE_DEPLOY_PROBE);
1330
-    do_blocking_move_to_xy(x_pos, y_pos);
1283
+    do_blocking_move_to_xy(lx, ly);
1331 1284
     do {
1332 1285
       if (do_ubl_mesh_map) ubl.display_map(1);
1333 1286
 
1334
-      location = find_closest_mesh_point_of_type( SET_IN_BITMAP, x_pos,  y_pos, 0, not_done); // The '0' says we want to use the nozzle's position
1287
+      location = find_closest_mesh_point_of_type( SET_IN_BITMAP, lx,  ly, 0, not_done); // The '0' says we want to use the nozzle's position
1335 1288
                                                                                               // It doesn't matter if the probe can not reach this
1336 1289
                                                                                               // location. This is a manual edit of the Mesh Point.
1337 1290
       if (location.x_index < 0 && location.y_index < 0) continue; // abort if we can't find any more points.
@@ -1339,8 +1292,8 @@
1339 1292
       bit_clear(not_done, location.x_index, location.y_index);  // Mark this location as 'adjusted' so we will find a
1340 1293
                                                                 // different location the next time through the loop
1341 1294
 
1342
-      xProbe = ubl.map_x_index_to_bed_location(location.x_index);
1343
-      yProbe = ubl.map_y_index_to_bed_location(location.y_index);
1295
+      const float xProbe = ubl.map_x_index_to_bed_location(location.x_index),
1296
+                  yProbe = ubl.map_y_index_to_bed_location(location.y_index);
1344 1297
       if (xProbe < X_MIN_POS || xProbe > X_MAX_POS || yProbe < Y_MIN_POS || yProbe > Y_MAX_POS) { // In theory, we don't need this check.
1345 1298
         SERIAL_PROTOCOLLNPGM("?Error: Attempt to edit off the bed.");                             // This really can't happen, but for now,
1346 1299
         ubl_has_control_of_lcd_panel = false;                                                         // Let's do the check.
@@ -1406,7 +1359,7 @@
1406 1359
     restore_ubl_active_state_and_leave();
1407 1360
     do_blocking_move_to_z(Z_CLEARANCE_DEPLOY_PROBE);
1408 1361
 
1409
-    do_blocking_move_to_xy(x_pos, y_pos);
1362
+    do_blocking_move_to_xy(lx, ly);
1410 1363
 
1411 1364
     #if ENABLED(ULTRA_LCD)
1412 1365
       lcd_setstatus("Done Editing Mesh", true);

+ 134
- 91
Marlin/UBL_line_to_destination.cpp Zobrazit soubor

@@ -29,35 +29,97 @@
29 29
   #include <avr/io.h>
30 30
   #include <math.h>
31 31
 
32
+  extern float destination[XYZE];
32 33
   extern void set_current_to_destination();
33
-  extern void debug_current_and_destination(char *title);
34 34
 
35
-  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) {
35
+  void debug_current_and_destination(char *title) {
36
+
37
+    // if the title message starts with a '!' it is so important, we are going to
38
+    // ignore the status of the g26_debug_flag
39
+    if (*title != '!' && !g26_debug_flag) return;
40
+
41
+    const float de = destination[E_AXIS] - current_position[E_AXIS];
42
+
43
+    if (de == 0.0) return;
44
+
45
+    const float dx = current_position[X_AXIS] - destination[X_AXIS],
46
+                dy = current_position[Y_AXIS] - destination[Y_AXIS],
47
+                xy_dist = HYPOT(dx, dy);
48
+
49
+    if (xy_dist == 0.0) {
50
+      return;
51
+      //SERIAL_ECHOPGM("   FPMM=");
52
+      //const float fpmm = de / xy_dist;
53
+      //SERIAL_PROTOCOL_F(fpmm, 6);
54
+    }
55
+    else {
56
+      SERIAL_ECHOPGM("   fpmm=");
57
+      const float fpmm = de / xy_dist;
58
+      SERIAL_ECHO_F(fpmm, 6);
59
+    }
36 60
 
37
-    int cell_start_xi, cell_start_yi, cell_dest_xi, cell_dest_yi,
38
-        current_xi, current_yi,
39
-        dxi, dyi, xi_cnt, yi_cnt;
40
-    float x_start, y_start,
41
-          x, y, z1, z2, z0 /*, z_optimized */,
42
-          next_mesh_line_x, next_mesh_line_y, a0ma1diva2ma1,
43
-          on_axis_distance, e_normalized_dist, e_position, e_start, z_normalized_dist, z_position, z_start,
44
-          dx, dy, adx, ady, m, c;
61
+    SERIAL_ECHOPGM("    current=( ");
62
+    SERIAL_ECHO_F(current_position[X_AXIS], 6);
63
+    SERIAL_ECHOPGM(", ");
64
+    SERIAL_ECHO_F(current_position[Y_AXIS], 6);
65
+    SERIAL_ECHOPGM(", ");
66
+    SERIAL_ECHO_F(current_position[Z_AXIS], 6);
67
+    SERIAL_ECHOPGM(", ");
68
+    SERIAL_ECHO_F(current_position[E_AXIS], 6);
69
+    SERIAL_ECHOPGM(" )   destination=( ");
70
+    if (current_position[X_AXIS] == destination[X_AXIS])
71
+      SERIAL_ECHOPGM("-------------");
72
+    else
73
+      SERIAL_ECHO_F(destination[X_AXIS], 6);
74
+
75
+    SERIAL_ECHOPGM(", ");
76
+
77
+    if (current_position[Y_AXIS] == destination[Y_AXIS])
78
+      SERIAL_ECHOPGM("-------------");
79
+    else
80
+      SERIAL_ECHO_F(destination[Y_AXIS], 6);
81
+
82
+    SERIAL_ECHOPGM(", ");
83
+
84
+    if (current_position[Z_AXIS] == destination[Z_AXIS])
85
+      SERIAL_ECHOPGM("-------------");
86
+    else
87
+      SERIAL_ECHO_F(destination[Z_AXIS], 6);
88
+
89
+    SERIAL_ECHOPGM(", ");
90
+
91
+    if (current_position[E_AXIS] == destination[E_AXIS])
92
+      SERIAL_ECHOPGM("-------------");
93
+    else
94
+      SERIAL_ECHO_F(destination[E_AXIS], 6);
95
+
96
+    SERIAL_ECHOPGM(" )   ");
97
+    SERIAL_ECHO(title);
98
+    SERIAL_EOL;
99
+
100
+    SET_INPUT_PULLUP(66); // Roxy's Left Switch is on pin 66.  Right Switch is on pin 65
101
+
102
+    //if (been_to_2_6) {
103
+    //while ((digitalRead(66) & 0x01) != 0)
104
+    //  idle();
105
+    //}
106
+  }
45 107
 
108
+  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) {
46 109
     /**
47 110
      * Much of the nozzle movement will be within the same cell. So we will do as little computation
48 111
      * as possible to determine if this is the case. If this move is within the same cell, we will
49 112
      * just do the required Z-Height correction, call the Planner's buffer_line() routine, and leave
50 113
      */
114
+    const float x_start = current_position[X_AXIS],
115
+                y_start = current_position[Y_AXIS],
116
+                z_start = current_position[Z_AXIS],
117
+                e_start = current_position[E_AXIS];
51 118
 
52
-    x_start = current_position[X_AXIS];
53
-    y_start = current_position[Y_AXIS];
54
-    z_start = current_position[Z_AXIS];
55
-    e_start = current_position[E_AXIS];
56
-
57
-    cell_start_xi = ubl.get_cell_index_x(x_start);
58
-    cell_start_yi = ubl.get_cell_index_y(y_start);
59
-    cell_dest_xi  = ubl.get_cell_index_x(x_end);
60
-    cell_dest_yi  = ubl.get_cell_index_y(y_end);
119
+    const int cell_start_xi = ubl.get_cell_index_x(RAW_X_POSITION(x_start)),
120
+              cell_start_yi = ubl.get_cell_index_y(RAW_Y_POSITION(y_start)),
121
+              cell_dest_xi  = ubl.get_cell_index_x(RAW_X_POSITION(x_end)),
122
+              cell_dest_yi  = ubl.get_cell_index_y(RAW_Y_POSITION(y_end));
61 123
 
62 124
     if (g26_debug_flag) {
63 125
       SERIAL_ECHOPGM(" ubl_line_to_destination(xe=");
@@ -68,7 +130,7 @@
68 130
       SERIAL_ECHO(z_end);
69 131
       SERIAL_ECHOPGM(", ee=");
70 132
       SERIAL_ECHO(e_end);
71
-      SERIAL_ECHOPGM(")\n");
133
+      SERIAL_ECHOLNPGM(")");
72 134
       debug_current_and_destination((char*)"Start of ubl_line_to_destination()");
73 135
     }
74 136
 
@@ -82,7 +144,7 @@
82 144
 
83 145
       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) {
84 146
 
85
-        // Note:  There is no Z Correction in this case. We are off the grid and don't know what
147
+        // Note: There is no Z Correction in this case. We are off the grid and don't know what
86 148
         // a reasonable correction would be.
87 149
 
88 150
         planner.buffer_line(x_end, y_end, z_end + ubl.state.z_offset, e_end, feed_rate, extruder);
@@ -105,20 +167,18 @@
105 167
        * to create a 1-over number for us. That will allow us to do a floating point multiply instead of a floating point divide.
106 168
        */
107 169
 
108
-      a0ma1diva2ma1 = (x_end - mesh_index_to_x_location[cell_dest_xi]) * 0.1 * (MESH_X_DIST);
109
-
110
-      z1 = z_values[cell_dest_xi    ][cell_dest_yi    ] + a0ma1diva2ma1 *
111
-          (z_values[cell_dest_xi + 1][cell_dest_yi    ] - z_values[cell_dest_xi][cell_dest_yi    ]);
112
-
113
-      z2 = z_values[cell_dest_xi    ][cell_dest_yi + 1] + a0ma1diva2ma1 *
114
-          (z_values[cell_dest_xi + 1][cell_dest_yi + 1] - z_values[cell_dest_xi][cell_dest_yi + 1]);
170
+      const float xratio = (RAW_X_POSITION(x_end) - mesh_index_to_x_location[cell_dest_xi]) * (1.0 / (MESH_X_DIST)),
171
+                  z1 = z_values[cell_dest_xi    ][cell_dest_yi    ] + xratio *
172
+                      (z_values[cell_dest_xi + 1][cell_dest_yi    ] - z_values[cell_dest_xi][cell_dest_yi    ]),
173
+                  z2 = z_values[cell_dest_xi    ][cell_dest_yi + 1] + xratio *
174
+                      (z_values[cell_dest_xi + 1][cell_dest_yi + 1] - z_values[cell_dest_xi][cell_dest_yi + 1]);
115 175
 
116 176
       // we are done with the fractional X distance into the cell. Now with the two Z-Heights we have calculated, we
117 177
       // are going to apply the Y-Distance into the cell to interpolate the final Z correction.
118 178
 
119
-      a0ma1diva2ma1 = (y_end - mesh_index_to_y_location[cell_dest_yi]) * 0.1 * (MESH_Y_DIST);
179
+      const float yratio = (RAW_Y_POSITION(y_end) - mesh_index_to_y_location[cell_dest_yi]) * (1.0 / (MESH_Y_DIST));
120 180
 
121
-      z0 = z1 + (z2 - z1) * a0ma1diva2ma1;
181
+      float z0 = z1 + (z2 - z1) * yratio;
122 182
 
123 183
       /**
124 184
        * Debug code to use non-optimized get_z_correction() and to do a sanity check
@@ -126,7 +186,7 @@
126 186
        */
127 187
       /*
128 188
         z_optimized = z0;
129
-        z0 = ubl.get_z_correction( x_end, y_end);
189
+        z0 = ubl.get_z_correction(x_end, y_end);
130 190
         if (fabs(z_optimized - z0) > .01 || isnan(z0) || isnan(z_optimized)) {
131 191
         debug_current_and_destination((char*)"FINAL_MOVE: z_correction()");
132 192
         if (isnan(z0)) SERIAL_ECHO(" z0==NAN  ");
@@ -139,7 +199,7 @@
139 199
         SERIAL_EOL;
140 200
         }
141 201
       //*/
142
-      z0 = z0 * ubl.fade_scaling_factor_for_z(z_end);
202
+      z0 *= ubl.fade_scaling_factor_for_z(z_end);
143 203
 
144 204
       /**
145 205
        * If part of the Mesh is undefined, it will show up as NAN
@@ -167,31 +227,17 @@
167 227
      * blocks of code:
168 228
      */
169 229
 
170
-    dx = x_end - x_start;
171
-    dy = y_end - y_start;
230
+    const float dx = x_end - x_start,
231
+                dy = y_end - y_start;
172 232
 
173 233
     const int left_flag = dx < 0.0 ? 1 : 0,
174 234
               down_flag = dy < 0.0 ? 1 : 0;
175 235
 
176
-    if (left_flag) { // figure out which way we need to move to get to the next cell
177
-      dxi = -1;
178
-      adx = -dx;  // absolute value of dx. We already need to check if dx and dy are negative.
179
-    }
180
-    else {      // We may as well generate the appropriate values for adx and ady right now
181
-      dxi = 1;  // to save setting up the abs() function call and actually doing the call.
182
-      adx = dx;
183
-    }
184
-    if (dy < 0.0) {
185
-      dyi = -1;
186
-      ady = -dy;  // absolute value of dy
187
-    }
188
-    else {
189
-      dyi = 1;
190
-      ady = dy;
191
-    }
236
+    const float adx = left_flag ? -dx : dx,
237
+                ady = down_flag ? -dy : dy;
192 238
 
193
-    if (cell_start_xi == cell_dest_xi) dxi = 0;
194
-    if (cell_start_yi == cell_dest_yi) dyi = 0;
239
+    const int dxi = cell_start_xi == cell_dest_xi ? 0 : left_flag ? -1 : 1,
240
+              dyi = cell_start_yi == cell_dest_yi ? 0 : down_flag ? -1 : 1;
195 241
 
196 242
     /**
197 243
      * Compute the scaling factor for the extruder for each partial move.
@@ -204,22 +250,20 @@
204 250
 
205 251
     const bool use_x_dist = adx > ady;
206 252
 
207
-    on_axis_distance = use_x_dist ? x_end - x_start : y_end - y_start;
208
-
209
-    e_position = e_end - e_start;
210
-    e_normalized_dist = e_position / on_axis_distance;
253
+    float on_axis_distance = use_x_dist ? dx : dy,
254
+          e_position = e_end - e_start,
255
+          z_position = z_end - z_start;
211 256
 
212
-    z_position = z_end - z_start;
213
-    z_normalized_dist = z_position / on_axis_distance;
257
+    const float e_normalized_dist = e_position / on_axis_distance,
258
+                z_normalized_dist = z_position / on_axis_distance;
214 259
 
215
-    const bool inf_normalized_flag = e_normalized_dist == INFINITY || e_normalized_dist == -INFINITY;
260
+    int current_xi = cell_start_xi, current_yi = cell_start_yi;
216 261
 
217
-    current_xi = cell_start_xi;
218
-    current_yi = cell_start_yi;
262
+    const float m = dy / dx,
263
+                c = y_start - m * x_start;
219 264
 
220
-    m = dy / dx;
221
-    c = y_start - m * x_start;
222
-    const bool inf_m_flag = (m == INFINITY || m == -INFINITY);
265
+    const bool inf_normalized_flag = NEAR_ZERO(on_axis_distance),
266
+               inf_m_flag = NEAR_ZERO(dx);
223 267
 
224 268
     /**
225 269
      * This block handles vertical lines. These are lines that stay within the same
@@ -230,16 +274,16 @@
230 274
       current_yi += down_flag;  // Line is heading down, we just want to go to the bottom
231 275
       while (current_yi != cell_dest_yi + down_flag) {
232 276
         current_yi += dyi;
233
-        next_mesh_line_y = mesh_index_to_y_location[current_yi];
277
+        const float next_mesh_line_y = LOGICAL_Y_POSITION(mesh_index_to_y_location[current_yi]);
234 278
 
235 279
         /**
236 280
          * inf_m_flag? the slope of the line is infinite, we won't do the calculations
237 281
          * else, we know the next X is the same so we can recover and continue!
238 282
          * Calculate X at the next Y mesh line
239 283
          */
240
-        x = inf_m_flag ? x_start : (next_mesh_line_y - c) / m;
284
+        const float x = inf_m_flag ? x_start : (next_mesh_line_y - c) / m;
241 285
 
242
-        z0 = ubl.get_z_correction_along_horizontal_mesh_line_at_specific_X(x, current_xi, current_yi);
286
+        float z0 = ubl.get_z_correction_along_horizontal_mesh_line_at_specific_X(x, current_xi, current_yi);
243 287
 
244 288
         /**
245 289
          * Debug code to use non-optimized get_z_correction() and to do a sanity check
@@ -247,7 +291,7 @@
247 291
          */
248 292
         /*
249 293
           z_optimized = z0;
250
-          z0 = ubl.get_z_correction( x, next_mesh_line_y);
294
+          z0 = ubl.get_z_correction(x, next_mesh_line_y);
251 295
           if (fabs(z_optimized - z0) > .01 || isnan(z0) || isnan(z_optimized)) {
252 296
             debug_current_and_destination((char*)"VERTICAL z_correction()");
253 297
           if (isnan(z0)) SERIAL_ECHO(" z0==NAN  ");
@@ -261,7 +305,7 @@
261 305
           }
262 306
         //*/
263 307
 
264
-        z0 = z0 * ubl.fade_scaling_factor_for_z(z_end);
308
+        z0 *= ubl.fade_scaling_factor_for_z(z_end);
265 309
 
266 310
         /**
267 311
          * If part of the Mesh is undefined, it will show up as NAN
@@ -272,7 +316,7 @@
272 316
          */
273 317
         if (isnan(z0)) z0 = 0.0;     
274 318
 
275
-        y = mesh_index_to_y_location[current_yi];
319
+        const float y = LOGICAL_Y_POSITION(mesh_index_to_y_location[current_yi]);
276 320
 
277 321
         /**
278 322
          * Without this check, it is possible for the algorithm to generate a zero length move in the case
@@ -321,10 +365,10 @@
321 365
                                 // edge of this cell for the first move.
322 366
       while (current_xi != cell_dest_xi + left_flag) {
323 367
         current_xi += dxi;
324
-        next_mesh_line_x = mesh_index_to_x_location[current_xi];
325
-        y = m * next_mesh_line_x + c;   // Calculate X at the next Y mesh line
368
+        const float next_mesh_line_x = LOGICAL_X_POSITION(mesh_index_to_x_location[current_xi]),
369
+                    y = m * next_mesh_line_x + c;   // Calculate X at the next Y mesh line
326 370
 
327
-        z0 = ubl.get_z_correction_along_vertical_mesh_line_at_specific_Y(y, current_xi, current_yi);
371
+        float z0 = ubl.get_z_correction_along_vertical_mesh_line_at_specific_Y(y, current_xi, current_yi);
328 372
 
329 373
         /**
330 374
          * Debug code to use non-optimized get_z_correction() and to do a sanity check
@@ -332,7 +376,7 @@
332 376
          */
333 377
         /*
334 378
           z_optimized = z0;
335
-          z0 = ubl.get_z_correction( next_mesh_line_x, y);
379
+          z0 = ubl.get_z_correction(next_mesh_line_x, y);
336 380
           if (fabs(z_optimized - z0) > .01 || isnan(z0) || isnan(z_optimized)) {
337 381
             debug_current_and_destination((char*)"HORIZONTAL z_correction()");
338 382
           if (isnan(z0)) SERIAL_ECHO(" z0==NAN  ");
@@ -357,7 +401,7 @@
357 401
          */
358 402
         if (isnan(z0)) z0 = 0.0;
359 403
 
360
-        x = mesh_index_to_x_location[current_xi];
404
+        const float x = LOGICAL_X_POSITION(mesh_index_to_x_location[current_xi]);
361 405
 
362 406
         /**
363 407
          * Without this check, it is possible for the algorithm to generate a zero length move in the case
@@ -396,10 +440,10 @@
396 440
      *
397 441
      */
398 442
 
399
-    xi_cnt = cell_start_xi - cell_dest_xi;
400
-    if (xi_cnt < 0) xi_cnt = -xi_cnt;
443
+    int xi_cnt = cell_start_xi - cell_dest_xi,
444
+        yi_cnt = cell_start_yi - cell_dest_yi;
401 445
 
402
-    yi_cnt = cell_start_yi - cell_dest_yi;
446
+    if (xi_cnt < 0) xi_cnt = -xi_cnt;
403 447
     if (yi_cnt < 0) yi_cnt = -yi_cnt;
404 448
 
405 449
     current_xi += left_flag;
@@ -407,20 +451,19 @@
407 451
 
408 452
     while (xi_cnt > 0 || yi_cnt > 0) {
409 453
 
410
-      next_mesh_line_x = mesh_index_to_x_location[current_xi + dxi];
411
-      next_mesh_line_y = mesh_index_to_y_location[current_yi + dyi];
412
-
413
-      y = m * next_mesh_line_x + c; // Calculate Y at the next X mesh line
414
-      x = (next_mesh_line_y - c) / m; // Calculate X at the next Y mesh line    (we don't have to worry
415
-      // about m being equal to 0.0  If this was the case, we would have
416
-      // detected this as a vertical line move up above and we wouldn't
417
-      // be down here doing a generic type of move.
454
+      const float next_mesh_line_x = LOGICAL_X_POSITION(mesh_index_to_x_location[current_xi + dxi]),
455
+                  next_mesh_line_y = LOGICAL_Y_POSITION(mesh_index_to_y_location[current_yi + dyi]),
456
+                  y = m * next_mesh_line_x + c,   // Calculate Y at the next X mesh line
457
+                  x = (next_mesh_line_y - c) / m; // Calculate X at the next Y mesh line    (we don't have to worry
458
+                                                  // about m being equal to 0.0  If this was the case, we would have
459
+                                                  // detected this as a vertical line move up above and we wouldn't
460
+                                                  // be down here doing a generic type of move.
418 461
 
419 462
       if (left_flag == (x > next_mesh_line_x)) { // Check if we hit the Y line first
420 463
         //
421 464
         // Yes!  Crossing a Y Mesh Line next
422 465
         //
423
-        z0 = ubl.get_z_correction_along_horizontal_mesh_line_at_specific_X(x, current_xi - left_flag, current_yi + dyi);
466
+        float z0 = ubl.get_z_correction_along_horizontal_mesh_line_at_specific_X(x, current_xi - left_flag, current_yi + dyi);
424 467
 
425 468
         /**
426 469
          * Debug code to use non-optimized get_z_correction() and to do a sanity check
@@ -428,7 +471,7 @@
428 471
          */
429 472
         /*
430 473
           z_optimized = z0;
431
-          z0 = ubl.get_z_correction( x, next_mesh_line_y);
474
+          z0 = ubl.get_z_correction(x, next_mesh_line_y);
432 475
           if (fabs(z_optimized - z0) > .01 || isnan(z0) || isnan(z_optimized)) {
433 476
             debug_current_and_destination((char*)"General_1: z_correction()");
434 477
             if (isnan(z0)) SERIAL_ECHO(" z0==NAN  ");
@@ -471,7 +514,7 @@
471 514
         //
472 515
         // Yes!  Crossing a X Mesh Line next
473 516
         //
474
-        z0 = ubl.get_z_correction_along_vertical_mesh_line_at_specific_Y(y, current_xi + dxi, current_yi - down_flag);
517
+        float z0 = ubl.get_z_correction_along_vertical_mesh_line_at_specific_Y(y, current_xi + dxi, current_yi - down_flag);
475 518
 
476 519
         /**
477 520
          * Debug code to use non-optimized get_z_correction() and to do a sanity check
@@ -479,7 +522,7 @@
479 522
          */
480 523
         /*
481 524
           z_optimized = z0;
482
-          z0 = ubl.get_z_correction( next_mesh_line_x, y);
525
+          z0 = ubl.get_z_correction(next_mesh_line_x, y);
483 526
           if (fabs(z_optimized - z0) > .01 || isnan(z0) || isnan(z_optimized)) {
484 527
           debug_current_and_destination((char*)"General_2: z_correction()");
485 528
           if (isnan(z0)) SERIAL_ECHO(" z0==NAN  ");
@@ -493,7 +536,7 @@
493 536
           }
494 537
         //*/
495 538
 
496
-        z0 = z0 * ubl.fade_scaling_factor_for_z(z_end);
539
+        z0 *= ubl.fade_scaling_factor_for_z(z_end);
497 540
 
498 541
         /**
499 542
          * If part of the Mesh is undefined, it will show up as NAN

+ 45
- 39
Marlin/configuration_store.cpp Zobrazit soubor

@@ -847,8 +847,8 @@ void Config_Postprocess() {
847 847
 
848 848
       #if ENABLED(AUTO_BED_LEVELING_UBL)
849 849
         ubl_eeprom_start = (eeprom_index + 32) & 0xFFF8; // Pad the end of configuration data so it
850
-                                                                          // can float up or down a little bit without
851
-                                                                          // disrupting the Unified Bed Leveling data
850
+                                                         // can float up or down a little bit without
851
+                                                         // disrupting the Unified Bed Leveling data
852 852
         ubl.load_state();
853 853
 
854 854
         SERIAL_ECHOPGM(" UBL ");
@@ -879,7 +879,7 @@ void Config_Postprocess() {
879 879
         }
880 880
         else {
881 881
           ubl.reset();
882
-          SERIAL_ECHOPGM("UBL System reset() \n");
882
+          SERIAL_ECHOLNPGM("UBL System reset()");
883 883
         }
884 884
       #endif
885 885
     }
@@ -1178,42 +1178,6 @@ void Config_ResetDefault() {
1178 1178
       SERIAL_ECHOPAIR(" Z", home_offset[Z_AXIS]);
1179 1179
       SERIAL_EOL;
1180 1180
     #endif
1181
-  #if ENABLED(AUTO_BED_LEVELING_UBL)
1182
-    SERIAL_ECHOLNPGM("Unified Bed Leveling:");
1183
-    CONFIG_ECHO_START;
1184
-
1185
-    SERIAL_ECHOPGM("System is: ");
1186
-    if (ubl.state.active)
1187
-       SERIAL_ECHOLNPGM("Active\n");
1188
-    else
1189
-       SERIAL_ECHOLNPGM("Deactive\n");
1190
-
1191
-    SERIAL_ECHOPAIR("Active Mesh Slot: ", ubl.state.eeprom_storage_slot);
1192
-    SERIAL_EOL;
1193
-
1194
-    SERIAL_ECHOPGM("z_offset: ");
1195
-    SERIAL_ECHO_F(ubl.state.z_offset, 6);
1196
-    SERIAL_EOL;
1197
-
1198
-    SERIAL_ECHOPAIR("EEPROM can hold ", (int)((E2END - sizeof(ubl.state) - ubl_eeprom_start) / sizeof(z_values)));
1199
-    SERIAL_ECHOLNPGM(" meshes. \n");
1200
-
1201
-    SERIAL_ECHOPAIR("\nUBL_MESH_NUM_X_POINTS  ", UBL_MESH_NUM_X_POINTS);
1202
-    SERIAL_ECHOPAIR("\nUBL_MESH_NUM_Y_POINTS  ", UBL_MESH_NUM_Y_POINTS);
1203
-
1204
-    SERIAL_ECHOPAIR("\nUBL_MESH_MIN_X         ", UBL_MESH_MIN_X);
1205
-    SERIAL_ECHOPAIR("\nUBL_MESH_MIN_Y         ", UBL_MESH_MIN_Y);
1206
-
1207
-    SERIAL_ECHOPAIR("\nUBL_MESH_MAX_X         ", UBL_MESH_MAX_X);
1208
-    SERIAL_ECHOPAIR("\nUBL_MESH_MAX_Y         ", UBL_MESH_MAX_Y);
1209
-
1210
-    SERIAL_ECHOPGM("\nMESH_X_DIST        ");
1211
-    SERIAL_ECHO_F(MESH_X_DIST, 6);
1212
-    SERIAL_ECHOPGM("\nMESH_Y_DIST        ");
1213
-    SERIAL_ECHO_F(MESH_Y_DIST, 6);
1214
-    SERIAL_EOL;
1215
-    SERIAL_EOL;
1216
-  #endif
1217 1181
 
1218 1182
     #if HOTENDS > 1
1219 1183
       CONFIG_ECHO_START;
@@ -1233,6 +1197,7 @@ void Config_ResetDefault() {
1233 1197
     #endif
1234 1198
 
1235 1199
     #if ENABLED(MESH_BED_LEVELING)
1200
+
1236 1201
       if (!forReplay) {
1237 1202
         SERIAL_ECHOLNPGM("Mesh Bed Leveling:");
1238 1203
         CONFIG_ECHO_START;
@@ -1248,12 +1213,53 @@ void Config_ResetDefault() {
1248 1213
           SERIAL_EOL;
1249 1214
         }
1250 1215
       }
1216
+
1217
+    #elif ENABLED(AUTO_BED_LEVELING_UBL)
1218
+
1219
+      if (!forReplay) {
1220
+        SERIAL_ECHOLNPGM("Unified Bed Leveling:");
1221
+        CONFIG_ECHO_START;
1222
+      }
1223
+
1224
+      SERIAL_ECHOLNPAIR("  M420 S", ubl.state.active ? 1 : 0);
1225
+
1226
+      if (!forReplay) {
1227
+        SERIAL_ECHOPGM("\nUBL is ");
1228
+        ubl.state.active ? SERIAL_CHAR('A') : SERIAL_ECHOPGM("Ina");
1229
+        SERIAL_ECHOLNPAIR("ctive\n\nActive Mesh Slot: ", ubl.state.eeprom_storage_slot);
1230
+
1231
+        SERIAL_ECHOPGM("z_offset: ");
1232
+        SERIAL_ECHO_F(ubl.state.z_offset, 6);
1233
+        SERIAL_EOL;
1234
+
1235
+        SERIAL_ECHOPAIR("EEPROM can hold ", (int)((UBL_LAST_EEPROM_INDEX - ubl_eeprom_start) / sizeof(z_values)));
1236
+        SERIAL_ECHOLNPGM(" meshes.\n");
1237
+
1238
+        SERIAL_ECHOPAIR("\nUBL_MESH_NUM_X_POINTS  ", UBL_MESH_NUM_X_POINTS);
1239
+        SERIAL_ECHOPAIR("\nUBL_MESH_NUM_Y_POINTS  ", UBL_MESH_NUM_Y_POINTS);
1240
+
1241
+        SERIAL_ECHOPAIR("\nUBL_MESH_MIN_X         ", UBL_MESH_MIN_X);
1242
+        SERIAL_ECHOPAIR("\nUBL_MESH_MIN_Y         ", UBL_MESH_MIN_Y);
1243
+
1244
+        SERIAL_ECHOPAIR("\nUBL_MESH_MAX_X         ", UBL_MESH_MAX_X);
1245
+        SERIAL_ECHOPAIR("\nUBL_MESH_MAX_Y         ", UBL_MESH_MAX_Y);
1246
+
1247
+        SERIAL_ECHOPGM("\nMESH_X_DIST        ");
1248
+        SERIAL_ECHO_F(MESH_X_DIST, 6);
1249
+        SERIAL_ECHOPGM("\nMESH_Y_DIST        ");
1250
+        SERIAL_ECHO_F(MESH_Y_DIST, 6);
1251
+        SERIAL_EOL;
1252
+        SERIAL_EOL;
1253
+      }
1254
+
1251 1255
     #elif HAS_ABL
1256
+
1252 1257
       if (!forReplay) {
1253 1258
         SERIAL_ECHOLNPGM("Auto Bed Leveling:");
1254 1259
         CONFIG_ECHO_START;
1255 1260
       }
1256 1261
       SERIAL_ECHOLNPAIR("  M420 S", planner.abl_enabled ? 1 : 0);
1262
+
1257 1263
     #endif
1258 1264
 
1259 1265
     #if ENABLED(DELTA)

+ 3
- 3
Marlin/planner.cpp Zobrazit soubor

@@ -530,7 +530,7 @@ void Planner::check_axes_activity() {
530 530
   #endif
531 531
 }
532 532
 
533
-#if PLANNER_LEVELING
533
+#if PLANNER_LEVELING && DISABLED(AUTO_BED_LEVELING_UBL)
534 534
   /**
535 535
    * lx, ly, lz - logical (cartesian, not delta) positions in mm
536 536
    */
@@ -634,7 +634,7 @@ void Planner::check_axes_activity() {
634 634
     #endif
635 635
   }
636 636
 
637
-#endif // PLANNER_LEVELING
637
+#endif // PLANNER_LEVELING && !AUTO_BED_LEVELING_UBL
638 638
 
639 639
 /**
640 640
  * Planner::_buffer_line
@@ -1408,7 +1408,7 @@ void Planner::_set_position_mm(const float &a, const float &b, const float &c, c
1408 1408
 }
1409 1409
 
1410 1410
 void Planner::set_position_mm_kinematic(const float position[NUM_AXIS]) {
1411
-  #if PLANNER_LEVELING
1411
+  #if PLANNER_LEVELING && DISABLED(AUTO_BED_LEVELING_UBL)
1412 1412
     float lpos[XYZ] = { position[X_AXIS], position[Y_AXIS], position[Z_AXIS] };
1413 1413
     apply_leveling(lpos);
1414 1414
   #else

+ 4
- 4
Marlin/planner.h Zobrazit soubor

@@ -244,7 +244,7 @@ class Planner {
244 244
 
245 245
     static bool is_full() { return (block_buffer_tail == BLOCK_MOD(block_buffer_head + 1)); }
246 246
 
247
-    #if PLANNER_LEVELING
247
+    #if PLANNER_LEVELING && DISABLED(AUTO_BED_LEVELING_UBL)
248 248
 
249 249
       #define ARG_X float lx
250 250
       #define ARG_Y float ly
@@ -300,7 +300,7 @@ class Planner {
300 300
      *  extruder     - target extruder
301 301
      */
302 302
     static FORCE_INLINE void buffer_line(ARG_X, ARG_Y, ARG_Z, const float &e, const float &fr_mm_s, const uint8_t extruder) {
303
-      #if PLANNER_LEVELING && IS_CARTESIAN
303
+      #if PLANNER_LEVELING && DISABLED(AUTO_BED_LEVELING_UBL) && IS_CARTESIAN
304 304
         apply_leveling(lx, ly, lz);
305 305
       #endif
306 306
       _buffer_line(lx, ly, lz, e, fr_mm_s, extruder);
@@ -316,7 +316,7 @@ class Planner {
316 316
      *  extruder - target extruder
317 317
      */
318 318
     static FORCE_INLINE void buffer_line_kinematic(const float ltarget[XYZE], const float &fr_mm_s, const uint8_t extruder) {
319
-      #if PLANNER_LEVELING
319
+      #if PLANNER_LEVELING && DISABLED(AUTO_BED_LEVELING_UBL)
320 320
         float lpos[XYZ] = { ltarget[X_AXIS], ltarget[Y_AXIS], ltarget[Z_AXIS] };
321 321
         apply_leveling(lpos);
322 322
       #else
@@ -340,7 +340,7 @@ class Planner {
340 340
      * Clears previous speed values.
341 341
      */
342 342
     static FORCE_INLINE void set_position_mm(ARG_X, ARG_Y, ARG_Z, const float &e) {
343
-      #if PLANNER_LEVELING && IS_CARTESIAN
343
+      #if PLANNER_LEVELING && DISABLED(AUTO_BED_LEVELING_UBL) && IS_CARTESIAN
344 344
         apply_leveling(lx, ly, lz);
345 345
       #endif
346 346
       _set_position_mm(lx, ly, lz, e);

Loading…
Zrušit
Uložit