Procházet zdrojové kódy

Merge pull request #3812 from thinkyhead/rc_mbl_index_finders

Two index finding functions for MBL
Scott Lahteine před 8 roky
rodič
revize
30f6b84561

+ 45
- 49
Marlin/Marlin_main.cpp Zobrazit soubor

@@ -3008,7 +3008,7 @@ inline void gcode_G28() {
3008 3008
       return;
3009 3009
     }
3010 3010
 
3011
-    int8_t ix, iy;
3011
+    int8_t px, py;
3012 3012
     float z;
3013 3013
 
3014 3014
     switch (state) {
@@ -3023,10 +3023,10 @@ inline void gcode_G28() {
3023 3023
           SERIAL_PROTOCOLPGM("\nZ offset: ");
3024 3024
           SERIAL_PROTOCOL_F(mbl.z_offset, 5);
3025 3025
           SERIAL_PROTOCOLLNPGM("\nMeasured points:");
3026
-          for (int y = 0; y < MESH_NUM_Y_POINTS; y++) {
3027
-            for (int x = 0; x < MESH_NUM_X_POINTS; x++) {
3026
+          for (py = 0; py < MESH_NUM_Y_POINTS; py++) {
3027
+            for (px = 0; px < MESH_NUM_X_POINTS; px++) {
3028 3028
               SERIAL_PROTOCOLPGM("  ");
3029
-              SERIAL_PROTOCOL_F(mbl.z_values[y][x], 5);
3029
+              SERIAL_PROTOCOL_F(mbl.z_values[py][px], 5);
3030 3030
             }
3031 3031
             SERIAL_EOL;
3032 3032
           }
@@ -3058,8 +3058,8 @@ inline void gcode_G28() {
3058 3058
         }
3059 3059
         // If there's another point to sample, move there with optional lift.
3060 3060
         if (probe_point < (MESH_NUM_X_POINTS) * (MESH_NUM_Y_POINTS)) {
3061
-          mbl.zigzag(probe_point, ix, iy);
3062
-          _mbl_goto_xy(mbl.get_x(ix), mbl.get_y(iy));
3061
+          mbl.zigzag(probe_point, px, py);
3062
+          _mbl_goto_xy(mbl.get_probe_x(px), mbl.get_probe_y(py));
3063 3063
           probe_point++;
3064 3064
         }
3065 3065
         else {
@@ -3082,8 +3082,8 @@ inline void gcode_G28() {
3082 3082
 
3083 3083
       case MeshSet:
3084 3084
         if (code_seen('X')) {
3085
-          ix = code_value_long() - 1;
3086
-          if (ix < 0 || ix >= MESH_NUM_X_POINTS) {
3085
+          px = code_value_long() - 1;
3086
+          if (px < 0 || px >= MESH_NUM_X_POINTS) {
3087 3087
             SERIAL_PROTOCOLPGM("X out of range (1-" STRINGIFY(MESH_NUM_X_POINTS) ").\n");
3088 3088
             return;
3089 3089
           }
@@ -3093,8 +3093,8 @@ inline void gcode_G28() {
3093 3093
           return;
3094 3094
         }
3095 3095
         if (code_seen('Y')) {
3096
-          iy = code_value_long() - 1;
3097
-          if (iy < 0 || iy >= MESH_NUM_Y_POINTS) {
3096
+          py = code_value_long() - 1;
3097
+          if (py < 0 || py >= MESH_NUM_Y_POINTS) {
3098 3098
             SERIAL_PROTOCOLPGM("Y out of range (1-" STRINGIFY(MESH_NUM_Y_POINTS) ").\n");
3099 3099
             return;
3100 3100
           }
@@ -3110,7 +3110,7 @@ inline void gcode_G28() {
3110 3110
           SERIAL_PROTOCOLPGM("Z not entered.\n");
3111 3111
           return;
3112 3112
         }
3113
-        mbl.z_values[iy][ix] = z;
3113
+        mbl.z_values[py][px] = z;
3114 3114
         break;
3115 3115
 
3116 3116
       case MeshSetZOffset:
@@ -5904,39 +5904,35 @@ inline void gcode_M410() { stepper.quick_stop(); }
5904 5904
    * Use either 'M421 X<mm> Y<mm> Z<mm>' or 'M421 I<xindex> J<yindex> Z<mm>'
5905 5905
    */
5906 5906
   inline void gcode_M421() {
5907
-    float x = 0, y = 0, z = 0;
5908
-    int8_t i = 0, j = 0;
5909
-    bool err = false, hasX, hasY, hasZ, hasI, hasJ;
5910
-    if ((hasX = code_seen('X'))) x = code_value();
5911
-    if ((hasY = code_seen('Y'))) y = code_value();
5912
-    if ((hasI = code_seen('I'))) i = code_value();
5913
-    if ((hasJ = code_seen('J'))) j = code_value();
5907
+    int8_t px, py;
5908
+    float z = 0;
5909
+    bool hasX, hasY, hasZ, hasI, hasJ;
5910
+    if ((hasX = code_seen('X'))) px = mbl.probe_index_x(code_value());
5911
+    if ((hasY = code_seen('Y'))) py = mbl.probe_index_y(code_value());
5912
+    if ((hasI = code_seen('I'))) px = code_value();
5913
+    if ((hasJ = code_seen('J'))) py = code_value();
5914 5914
     if ((hasZ = code_seen('Z'))) z = code_value();
5915 5915
 
5916 5916
     if (hasX && hasY && hasZ) {
5917 5917
 
5918
-      int8_t ix = mbl.select_x_index(x),
5919
-             iy = mbl.select_y_index(y);
5920
-
5921
-      if (ix >= 0 && iy >= 0)
5922
-        mbl.set_z(ix, iy, z);
5918
+      if (px >= 0 && py >= 0)
5919
+        mbl.set_z(px, py, z);
5923 5920
       else {
5924 5921
         SERIAL_ERROR_START;
5925 5922
         SERIAL_ERRORLNPGM(MSG_ERR_MESH_XY);
5926 5923
       }
5927 5924
     }
5928 5925
     else if (hasI && hasJ && hasZ) {
5929
-      if (i >= 0 && i < MESH_NUM_X_POINTS && j >= 0 && j < MESH_NUM_Y_POINTS)
5930
-        mbl.set_z(i, j, z);
5926
+      if (px >= 0 && px < MESH_NUM_X_POINTS && py >= 0 && py < MESH_NUM_Y_POINTS)
5927
+        mbl.set_z(px, py, z);
5931 5928
       else {
5932 5929
         SERIAL_ERROR_START;
5933 5930
         SERIAL_ERRORLNPGM(MSG_ERR_MESH_XY);
5934 5931
       }
5935 5932
     }
5936
-    else 
5937
-    {
5933
+    else {
5938 5934
       SERIAL_ERROR_START;
5939
-      SERIAL_ERRORLNPGM(MSG_ERR_M421_REQUIRES_XYZ);
5935
+      SERIAL_ERRORLNPGM(MSG_ERR_M421_PARAMETERS);
5940 5936
     }
5941 5937
   }
5942 5938
 
@@ -7303,52 +7299,52 @@ void mesh_buffer_line(float x, float y, float z, const float e, float feed_rate,
7303 7299
     set_current_to_destination();
7304 7300
     return;
7305 7301
   }
7306
-  int pix = mbl.select_x_index(current_position[X_AXIS] - home_offset[X_AXIS]);
7307
-  int piy = mbl.select_y_index(current_position[Y_AXIS] - home_offset[Y_AXIS]);
7308
-  int ix = mbl.select_x_index(x - home_offset[X_AXIS]);
7309
-  int iy = mbl.select_y_index(y - home_offset[Y_AXIS]);
7310
-  pix = min(pix, MESH_NUM_X_POINTS - 2);
7311
-  piy = min(piy, MESH_NUM_Y_POINTS - 2);
7312
-  ix = min(ix, MESH_NUM_X_POINTS - 2);
7313
-  iy = min(iy, MESH_NUM_Y_POINTS - 2);
7314
-  if (pix == ix && piy == iy) {
7302
+  int pcx = mbl.cel_index_x(current_position[X_AXIS] - home_offset[X_AXIS]);
7303
+  int pcy = mbl.cel_index_y(current_position[Y_AXIS] - home_offset[Y_AXIS]);
7304
+  int cx = mbl.cel_index_x(x - home_offset[X_AXIS]);
7305
+  int cy = mbl.cel_index_y(y - home_offset[Y_AXIS]);
7306
+  NOMORE(pcx, MESH_NUM_X_POINTS - 2);
7307
+  NOMORE(pcy, MESH_NUM_Y_POINTS - 2);
7308
+  NOMORE(cx,  MESH_NUM_X_POINTS - 2);
7309
+  NOMORE(cy,  MESH_NUM_Y_POINTS - 2);
7310
+  if (pcx == cx && pcy == cy) {
7315 7311
     // Start and end on same mesh square
7316 7312
     planner.buffer_line(x, y, z, e, feed_rate, extruder);
7317 7313
     set_current_to_destination();
7318 7314
     return;
7319 7315
   }
7320 7316
   float nx, ny, nz, ne, normalized_dist;
7321
-  if (ix > pix && TEST(x_splits, ix)) {
7322
-    nx = mbl.get_x(ix) + home_offset[X_AXIS];
7317
+  if (cx > pcx && TEST(x_splits, cx)) {
7318
+    nx = mbl.get_probe_x(cx) + home_offset[X_AXIS];
7323 7319
     normalized_dist = (nx - current_position[X_AXIS]) / (x - current_position[X_AXIS]);
7324 7320
     ny = current_position[Y_AXIS] + (y - current_position[Y_AXIS]) * normalized_dist;
7325 7321
     nz = current_position[Z_AXIS] + (z - current_position[Z_AXIS]) * normalized_dist;
7326 7322
     ne = current_position[E_AXIS] + (e - current_position[E_AXIS]) * normalized_dist;
7327
-    CBI(x_splits, ix);
7323
+    CBI(x_splits, cx);
7328 7324
   }
7329
-  else if (ix < pix && TEST(x_splits, pix)) {
7330
-    nx = mbl.get_x(pix) + home_offset[X_AXIS];
7325
+  else if (cx < pcx && TEST(x_splits, pcx)) {
7326
+    nx = mbl.get_probe_x(pcx) + home_offset[X_AXIS];
7331 7327
     normalized_dist = (nx - current_position[X_AXIS]) / (x - current_position[X_AXIS]);
7332 7328
     ny = current_position[Y_AXIS] + (y - current_position[Y_AXIS]) * normalized_dist;
7333 7329
     nz = current_position[Z_AXIS] + (z - current_position[Z_AXIS]) * normalized_dist;
7334 7330
     ne = current_position[E_AXIS] + (e - current_position[E_AXIS]) * normalized_dist;
7335
-    CBI(x_splits, pix);
7331
+    CBI(x_splits, pcx);
7336 7332
   }
7337
-  else if (iy > piy && TEST(y_splits, iy)) {
7338
-    ny = mbl.get_y(iy) + home_offset[Y_AXIS];
7333
+  else if (cy > pcy && TEST(y_splits, cy)) {
7334
+    ny = mbl.get_probe_y(cy) + home_offset[Y_AXIS];
7339 7335
     normalized_dist = (ny - current_position[Y_AXIS]) / (y - current_position[Y_AXIS]);
7340 7336
     nx = current_position[X_AXIS] + (x - current_position[X_AXIS]) * normalized_dist;
7341 7337
     nz = current_position[Z_AXIS] + (z - current_position[Z_AXIS]) * normalized_dist;
7342 7338
     ne = current_position[E_AXIS] + (e - current_position[E_AXIS]) * normalized_dist;
7343
-    CBI(y_splits, iy);
7339
+    CBI(y_splits, cy);
7344 7340
   }
7345
-  else if (iy < piy && TEST(y_splits, piy)) {
7346
-    ny = mbl.get_y(piy) + home_offset[Y_AXIS];
7341
+  else if (cy < pcy && TEST(y_splits, pcy)) {
7342
+    ny = mbl.get_probe_y(pcy) + home_offset[Y_AXIS];
7347 7343
     normalized_dist = (ny - current_position[Y_AXIS]) / (y - current_position[Y_AXIS]);
7348 7344
     nx = current_position[X_AXIS] + (x - current_position[X_AXIS]) * normalized_dist;
7349 7345
     nz = current_position[Z_AXIS] + (z - current_position[Z_AXIS]) * normalized_dist;
7350 7346
     ne = current_position[E_AXIS] + (e - current_position[E_AXIS]) * normalized_dist;
7351
-    CBI(y_splits, piy);
7347
+    CBI(y_splits, pcy);
7352 7348
   }
7353 7349
   else {
7354 7350
     // Already split on a border

+ 6
- 6
Marlin/configuration_store.cpp Zobrazit soubor

@@ -62,7 +62,7 @@
62 62
  *  201            z_offset (float) (added in V23)
63 63
  *  205            mesh_num_x (uint8 as set in firmware)
64 64
  *  206            mesh_num_y (uint8 as set in firmware)
65
- *  207  M421 XYZ  z_values[][] (float x9, by default)
65
+ *  207 G29 S3 XYZ z_values[][] (float x9, by default)
66 66
  *
67 67
  * AUTO BED LEVELING
68 68
  *  243  M851      zprobe_zoffset (float)
@@ -733,13 +733,13 @@ void Config_PrintSettings(bool forReplay) {
733 733
     SERIAL_ECHOPAIR(" X", MESH_NUM_X_POINTS);
734 734
     SERIAL_ECHOPAIR(" Y", MESH_NUM_Y_POINTS);
735 735
     SERIAL_EOL;
736
-    for (uint8_t y = 0; y < MESH_NUM_Y_POINTS; y++) {
737
-      for (uint8_t x = 0; x < MESH_NUM_X_POINTS; x++) {
736
+    for (uint8_t py = 1; py <= MESH_NUM_Y_POINTS; py++) {
737
+      for (uint8_t px = 1; px <= MESH_NUM_X_POINTS; px++) {
738 738
         CONFIG_ECHO_START;
739
-        SERIAL_ECHOPAIR("  M421 X", mbl.get_x(x));
740
-        SERIAL_ECHOPAIR(" Y", mbl.get_y(y));
739
+        SERIAL_ECHOPAIR("  G29 S3 X", px);
740
+        SERIAL_ECHOPAIR(" Y", py);
741 741
         SERIAL_ECHOPGM(" Z");
742
-        SERIAL_PROTOCOL_F(mbl.z_values[y][x], 5);
742
+        SERIAL_PROTOCOL_F(mbl.z_values[py-1][px-1], 5);
743 743
         SERIAL_EOL;
744 744
       }
745 745
     }

+ 1
- 1
Marlin/language.h Zobrazit soubor

@@ -147,7 +147,7 @@
147 147
 #define MSG_Z2_MAX                          "z2_max: "
148 148
 #define MSG_Z_PROBE                         "z_probe: "
149 149
 #define MSG_ERR_MATERIAL_INDEX              "M145 S<index> out of range (0-1)"
150
-#define MSG_ERR_M421_REQUIRES_XYZ           "M421 requires XYZ or IJZ parameters"
150
+#define MSG_ERR_M421_PARAMETERS             "M421 requires XYZ or IJZ parameters"
151 151
 #define MSG_ERR_MESH_XY                     "Mesh XY or IJ cannot be resolved"
152 152
 #define MSG_ERR_M428_TOO_FAR                "Too far from reference point"
153 153
 #define MSG_ERR_M303_DISABLED               "PIDTEMP disabled"

+ 36
- 30
Marlin/mesh_bed_leveling.h Zobrazit soubor

@@ -37,34 +37,40 @@
37 37
 
38 38
     void reset();
39 39
 
40
-    static FORCE_INLINE float get_x(int8_t i) { return MESH_MIN_X + (MESH_X_DIST) * i; }
41
-    static FORCE_INLINE float get_y(int8_t i) { return MESH_MIN_Y + (MESH_Y_DIST) * i; }
42
-    void set_z(int8_t ix, int8_t iy, float z) { z_values[iy][ix] = z; }
43
-
44
-    inline void zigzag(int8_t index, int8_t &ix, int8_t &iy) {
45
-      ix = index % (MESH_NUM_X_POINTS);
46
-      iy = index / (MESH_NUM_X_POINTS);
47
-      if (iy & 1) ix = (MESH_NUM_X_POINTS - 1) - ix; // Zig zag
40
+    static FORCE_INLINE float get_probe_x(int8_t i) { return MESH_MIN_X + (MESH_X_DIST) * i; }
41
+    static FORCE_INLINE float get_probe_y(int8_t i) { return MESH_MIN_Y + (MESH_Y_DIST) * i; }
42
+    void set_z(const int8_t px, const int8_t py, const float z) { z_values[py][px] = z; }
43
+
44
+    inline void zigzag(int8_t index, int8_t &px, int8_t &py) {
45
+      px = index % (MESH_NUM_X_POINTS);
46
+      py = index / (MESH_NUM_X_POINTS);
47
+      if (py & 1) px = (MESH_NUM_X_POINTS - 1) - px; // Zig zag
48 48
     }
49 49
 
50 50
     void set_zigzag_z(int8_t index, float z) {
51
-      int8_t ix, iy;
52
-      zigzag(index, ix, iy);
53
-      set_z(ix, iy, z);
51
+      int8_t px, py;
52
+      zigzag(index, px, py);
53
+      set_z(px, py, z);
54
+    }
55
+
56
+    int8_t cel_index_x(float x) {
57
+      int8_t cx = int(x - (MESH_MIN_X)) / (MESH_X_DIST);
58
+      return constrain(cx, 0, (MESH_NUM_X_POINTS) - 2);
59
+    }
60
+
61
+    int8_t cel_index_y(float y) {
62
+      int8_t cy = int(y - (MESH_MIN_Y)) / (MESH_Y_DIST);
63
+      return constrain(cy, 0, (MESH_NUM_Y_POINTS) - 2);
54 64
     }
55 65
 
56
-    int8_t select_x_index(float x) {
57
-      for (uint8_t i = MESH_NUM_X_POINTS; i--;)
58
-        if (fabs(x - get_x(i)) <= (MESH_X_DIST) / 2)
59
-          return i;
60
-      return -1;
66
+    int8_t probe_index_x(float x) {
67
+      int8_t px = int(x - (MESH_MIN_X) + (MESH_X_DIST) / 2) / (MESH_X_DIST);
68
+      return (px >= 0 && px < (MESH_NUM_X_POINTS)) ? px : -1;
61 69
     }
62 70
 
63
-    int8_t select_y_index(float y) {
64
-      for (uint8_t i = MESH_NUM_Y_POINTS; i--;)
65
-        if (fabs(y - get_y(i)) <= (MESH_Y_DIST) / 2)
66
-          return i;
67
-      return -1;
71
+    int8_t probe_index_y(float y) {
72
+      int8_t py = int(y - (MESH_MIN_Y) + (MESH_Y_DIST) / 2) / (MESH_Y_DIST);
73
+      return (py >= 0 && py < (MESH_NUM_Y_POINTS)) ? py : -1;
68 74
     }
69 75
 
70 76
     float calc_z0(float a0, float a1, float z1, float a2, float z2) {
@@ -74,18 +80,18 @@
74 80
     }
75 81
 
76 82
     float get_z(float x0, float y0) {
77
-      int8_t x_index = select_x_index(x0);
78
-      int8_t y_index = select_y_index(y0);
79
-      if (x_index < 0 || y_index < 0) return z_offset;
83
+      int8_t cx = cel_index_x(x0),
84
+             cy = cel_index_y(y0);
85
+      if (cx < 0 || cy < 0) return z_offset;
80 86
       float z1 = calc_z0(x0,
81
-                         get_x(x_index), z_values[y_index][x_index],
82
-                         get_x(x_index + 1), z_values[y_index][x_index + 1]);
87
+                         get_probe_x(cx), z_values[cy][cx],
88
+                         get_probe_x(cx + 1), z_values[cy][cx + 1]);
83 89
       float z2 = calc_z0(x0,
84
-                         get_x(x_index), z_values[y_index + 1][x_index],
85
-                         get_x(x_index + 1), z_values[y_index + 1][x_index + 1]);
90
+                         get_probe_x(cx), z_values[cy + 1][cx],
91
+                         get_probe_x(cx + 1), z_values[cy + 1][cx + 1]);
86 92
       float z0 = calc_z0(y0,
87
-                         get_y(y_index), z1,
88
-                         get_y(y_index + 1), z2);
93
+                         get_probe_y(cy), z1,
94
+                         get_probe_y(cy + 1), z2);
89 95
       return z0 + z_offset;
90 96
     }
91 97
   };

+ 3
- 3
Marlin/ultralcd.cpp Zobrazit soubor

@@ -1013,9 +1013,9 @@ void lcd_cooldown() {
1013 1013
     lcd_goto_menu(_lcd_level_bed_moving);
1014 1014
 
1015 1015
     // _mbl_goto_xy runs the menu loop until the move is done
1016
-    int8_t ix, iy;
1017
-    mbl.zigzag(_lcd_level_bed_position, ix, iy);
1018
-    _mbl_goto_xy(mbl.get_x(ix), mbl.get_y(iy));
1016
+    int8_t px, py;
1017
+    mbl.zigzag(_lcd_level_bed_position, px, py);
1018
+    _mbl_goto_xy(mbl.get_probe_x(px), mbl.get_probe_y(py));
1019 1019
 
1020 1020
     // After the blocking function returns, change menus
1021 1021
     lcd_goto_menu(_lcd_level_bed_get_z);

Loading…
Zrušit
Uložit