Browse Source

Two index finding functions for MBL

Scott Lahteine 8 years ago
parent
commit
a4ed988c60
4 changed files with 90 additions and 79 deletions
  1. 42
    43
      Marlin/Marlin_main.cpp
  2. 5
    5
      Marlin/configuration_store.cpp
  3. 40
    28
      Marlin/mesh_bed_leveling.h
  4. 3
    3
      Marlin/ultralcd.cpp

+ 42
- 43
Marlin/Marlin_main.cpp View File

@@ -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:
@@ -5905,36 +5905,35 @@ inline void gcode_M410() { stepper.quick_stop(); }
5905 5905
    */
5906 5906
   inline void gcode_M421() {
5907 5907
     float x = 0, y = 0, z = 0;
5908
-    int8_t i = 0, j = 0;
5908
+    int8_t px = 0, py = 0;
5909 5909
     bool err = false, hasX, hasY, hasZ, hasI, hasJ;
5910 5910
     if ((hasX = code_seen('X'))) x = code_value();
5911 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();
5914 5912
     if ((hasZ = code_seen('Z'))) z = code_value();
5913
+    if ((hasI = code_seen('I'))) px = code_value();
5914
+    if ((hasJ = code_seen('J'))) py = 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);
5918
+      px = mbl.probe_index_x(x);
5919
+      py = mbl.probe_index_y(y);
5920 5920
 
5921
-      if (ix >= 0 && iy >= 0)
5922
-        mbl.set_z(ix, iy, z);
5921
+      if (px >= 0 && py >= 0)
5922
+        mbl.set_z(px, py, z);
5923 5923
       else {
5924 5924
         SERIAL_ERROR_START;
5925 5925
         SERIAL_ERRORLNPGM(MSG_ERR_MESH_XY);
5926 5926
       }
5927 5927
     }
5928 5928
     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);
5929
+      if (px >= 0 && px < MESH_NUM_X_POINTS && py >= 0 && py < MESH_NUM_Y_POINTS)
5930
+        mbl.set_z(px, py, z);
5931 5931
       else {
5932 5932
         SERIAL_ERROR_START;
5933 5933
         SERIAL_ERRORLNPGM(MSG_ERR_MESH_XY);
5934 5934
       }
5935 5935
     }
5936
-    else 
5937
-    {
5936
+    else {
5938 5937
       SERIAL_ERROR_START;
5939 5938
       SERIAL_ERRORLNPGM(MSG_ERR_M421_REQUIRES_XYZ);
5940 5939
     }
@@ -7303,52 +7302,52 @@ void mesh_buffer_line(float x, float y, float z, const float e, float feed_rate,
7303 7302
     set_current_to_destination();
7304 7303
     return;
7305 7304
   }
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) {
7305
+  int pcx = mbl.cel_index_x(current_position[X_AXIS] - home_offset[X_AXIS]);
7306
+  int pcy = mbl.cel_index_y(current_position[Y_AXIS] - home_offset[Y_AXIS]);
7307
+  int cx = mbl.cel_index_x(x - home_offset[X_AXIS]);
7308
+  int cy = mbl.cel_index_y(y - home_offset[Y_AXIS]);
7309
+  NOMORE(pcx, MESH_NUM_X_POINTS - 2);
7310
+  NOMORE(pcy, MESH_NUM_Y_POINTS - 2);
7311
+  NOMORE(cx,  MESH_NUM_X_POINTS - 2);
7312
+  NOMORE(cy,  MESH_NUM_Y_POINTS - 2);
7313
+  if (pcx == cx && pcy == cy) {
7315 7314
     // Start and end on same mesh square
7316 7315
     planner.buffer_line(x, y, z, e, feed_rate, extruder);
7317 7316
     set_current_to_destination();
7318 7317
     return;
7319 7318
   }
7320 7319
   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];
7320
+  if (cx > pcx && TEST(x_splits, cx)) {
7321
+    nx = mbl.get_probe_x(cx) + home_offset[X_AXIS];
7323 7322
     normalized_dist = (nx - current_position[X_AXIS]) / (x - current_position[X_AXIS]);
7324 7323
     ny = current_position[Y_AXIS] + (y - current_position[Y_AXIS]) * normalized_dist;
7325 7324
     nz = current_position[Z_AXIS] + (z - current_position[Z_AXIS]) * normalized_dist;
7326 7325
     ne = current_position[E_AXIS] + (e - current_position[E_AXIS]) * normalized_dist;
7327
-    CBI(x_splits, ix);
7326
+    CBI(x_splits, cx);
7328 7327
   }
7329
-  else if (ix < pix && TEST(x_splits, pix)) {
7330
-    nx = mbl.get_x(pix) + home_offset[X_AXIS];
7328
+  else if (cx < pcx && TEST(x_splits, pcx)) {
7329
+    nx = mbl.get_probe_x(pcx) + home_offset[X_AXIS];
7331 7330
     normalized_dist = (nx - current_position[X_AXIS]) / (x - current_position[X_AXIS]);
7332 7331
     ny = current_position[Y_AXIS] + (y - current_position[Y_AXIS]) * normalized_dist;
7333 7332
     nz = current_position[Z_AXIS] + (z - current_position[Z_AXIS]) * normalized_dist;
7334 7333
     ne = current_position[E_AXIS] + (e - current_position[E_AXIS]) * normalized_dist;
7335
-    CBI(x_splits, pix);
7334
+    CBI(x_splits, pcx);
7336 7335
   }
7337
-  else if (iy > piy && TEST(y_splits, iy)) {
7338
-    ny = mbl.get_y(iy) + home_offset[Y_AXIS];
7336
+  else if (cy > pcy && TEST(y_splits, cy)) {
7337
+    ny = mbl.get_probe_y(cy) + home_offset[Y_AXIS];
7339 7338
     normalized_dist = (ny - current_position[Y_AXIS]) / (y - current_position[Y_AXIS]);
7340 7339
     nx = current_position[X_AXIS] + (x - current_position[X_AXIS]) * normalized_dist;
7341 7340
     nz = current_position[Z_AXIS] + (z - current_position[Z_AXIS]) * normalized_dist;
7342 7341
     ne = current_position[E_AXIS] + (e - current_position[E_AXIS]) * normalized_dist;
7343
-    CBI(y_splits, iy);
7342
+    CBI(y_splits, cy);
7344 7343
   }
7345
-  else if (iy < piy && TEST(y_splits, piy)) {
7346
-    ny = mbl.get_y(piy) + home_offset[Y_AXIS];
7344
+  else if (cy < pcy && TEST(y_splits, pcy)) {
7345
+    ny = mbl.get_probe_y(pcy) + home_offset[Y_AXIS];
7347 7346
     normalized_dist = (ny - current_position[Y_AXIS]) / (y - current_position[Y_AXIS]);
7348 7347
     nx = current_position[X_AXIS] + (x - current_position[X_AXIS]) * normalized_dist;
7349 7348
     nz = current_position[Z_AXIS] + (z - current_position[Z_AXIS]) * normalized_dist;
7350 7349
     ne = current_position[E_AXIS] + (e - current_position[E_AXIS]) * normalized_dist;
7351
-    CBI(y_splits, piy);
7350
+    CBI(y_splits, pcy);
7352 7351
   }
7353 7352
   else {
7354 7353
     // Already split on a border

+ 5
- 5
Marlin/configuration_store.cpp View File

@@ -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 = 0; py < MESH_NUM_Y_POINTS; py++) {
737
+      for (uint8_t px = 0; 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("  M421 X", mbl.get_probe_x(px));
740
+        SERIAL_ECHOPAIR(" Y", mbl.get_probe_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][px], 5);
743 743
         SERIAL_EOL;
744 744
       }
745 745
     }

+ 40
- 28
Marlin/mesh_bed_leveling.h View File

@@ -37,33 +37,45 @@
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(int8_t px, int8_t py, 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 = 1;
58
+      while (x > get_probe_x(cx) && cx < MESH_NUM_X_POINTS - 1) cx++; // For 3x3 range is 1 to 2
59
+      return cx - 1; // so this will return 0 - 1
60
+    }
61
+
62
+    int8_t cel_index_y(float y) {
63
+      int8_t cy = 1;
64
+      while (y > get_probe_y(cy) && cy < MESH_NUM_Y_POINTS - 1) cy++;
65
+      return cy - 1;
54 66
     }
55 67
 
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;
68
+    int8_t probe_index_x(float x) {
69
+      for (int8_t px = MESH_NUM_X_POINTS; px--;)
70
+        if (fabs(x - get_probe_x(px)) <= (MESH_X_DIST) / 2)
71
+          return px;
60 72
       return -1;
61 73
     }
62 74
 
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;
75
+    int8_t probe_index_y(float y) {
76
+      for (int8_t py = MESH_NUM_Y_POINTS; py--;)
77
+        if (fabs(y - get_probe_y(py)) <= (MESH_Y_DIST) / 2)
78
+          return py;
67 79
       return -1;
68 80
     }
69 81
 
@@ -74,18 +86,18 @@
74 86
     }
75 87
 
76 88
     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;
89
+      int8_t cx = cel_index_x(x0),
90
+             cy = cel_index_y(y0);
91
+      if (cx < 0 || cy < 0) return z_offset;
80 92
       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]);
93
+                         get_probe_x(cx), z_values[cy][cx],
94
+                         get_probe_x(cx + 1), z_values[cy][cx + 1]);
83 95
       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]);
96
+                         get_probe_x(cx), z_values[cy + 1][cx],
97
+                         get_probe_x(cx + 1), z_values[cy + 1][cx + 1]);
86 98
       float z0 = calc_z0(y0,
87
-                         get_y(y_index), z1,
88
-                         get_y(y_index + 1), z2);
99
+                         get_probe_y(cy), z1,
100
+                         get_probe_y(cy + 1), z2);
89 101
       return z0 + z_offset;
90 102
     }
91 103
   };

+ 3
- 3
Marlin/ultralcd.cpp View File

@@ -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…
Cancel
Save