Browse Source

Fix up bilinear_z_offset

Scott Lahteine 8 years ago
parent
commit
0d9efb24f3
1 changed files with 19 additions and 17 deletions
  1. 19
    17
      Marlin/Marlin_main.cpp

+ 19
- 17
Marlin/Marlin_main.cpp View File

@@ -7936,27 +7936,29 @@ void ok_to_send() {
7936 7936
   // Get the Z adjustment for non-linear bed leveling
7937 7937
   float bilinear_z_offset(float cartesian[XYZ]) {
7938 7938
 
7939
-    int gridx = (cartesian[X_AXIS] - bilinear_start[X_AXIS]) / bilinear_grid_spacing[X_AXIS],
7940
-        gridy = (cartesian[Y_AXIS] - bilinear_start[Y_AXIS]) / bilinear_grid_spacing[Y_AXIS];
7939
+    // XY relative to the probed area
7940
+    const float x = RAW_X_POSITION(cartesian[X_AXIS]) - bilinear_start[X_AXIS],
7941
+                y = RAW_Y_POSITION(cartesian[Y_AXIS]) - bilinear_start[Y_AXIS];
7941 7942
 
7942
-    // What grid box is xy inside?
7943
-    if (gridx < 0) gridx = 0;
7944
-    if (gridx > ABL_GRID_POINTS_X - 1) gridx = ABL_GRID_POINTS_X - 1;
7945
-    if (gridy < 0) gridy = 0;
7946
-    if (gridy > ABL_GRID_POINTS_Y - 1) gridy = ABL_GRID_POINTS_Y - 1;
7943
+    // Convert to grid box units
7944
+    float ratio_x = x / bilinear_grid_spacing[X_AXIS],
7945
+          ratio_y = y / bilinear_grid_spacing[Y_AXIS];
7947 7946
 
7948
-          // Ratio within the grid box
7949
-    float ratio_x = cartesian[X_AXIS] / bilinear_grid_spacing[X_AXIS] - gridx,
7950
-          ratio_y = cartesian[Y_AXIS] / bilinear_grid_spacing[Y_AXIS] - gridy,
7947
+    // Whole unit is the grid box index
7948
+    int gridx = constrain(int(ratio_x), 0, ABL_GRID_POINTS_X - 2),
7949
+        gridy = constrain(int(ratio_y), 0, ABL_GRID_POINTS_Y - 2);
7950
+
7951
+    // Subtract whole to get the ratio within the grid box
7952
+    ratio_x -= gridx, ratio_y -= gridy;
7951 7953
 
7952 7954
           // Z at the box corners
7953
-          z1 = bed_level_grid[gridx][gridy],         // left-front
7954
-          z2 = bed_level_grid[gridx][gridy + 1],     // left-back
7955
-          z3 = bed_level_grid[gridx + 1][gridy],     // right-front
7956
-          z4 = bed_level_grid[gridx + 1][gridy + 1], // right-back
7955
+    const float z1 = bed_level_grid[gridx][gridy],         // left-front
7956
+                z2 = bed_level_grid[gridx][gridy + 1],     // left-back
7957
+                z3 = bed_level_grid[gridx + 1][gridy],     // right-front
7958
+                z4 = bed_level_grid[gridx + 1][gridy + 1], // right-back
7957 7959
 
7958
-          L = z1 + (z2 - z1) * ratio_y,   // Linear interp. LF -> LB
7959
-          R = z3 + (z4 - z3) * ratio_y;   // Linear interp. RF -> RB
7960
+                L = z1 + (z2 - z1) * ratio_y,   // Linear interp. LF -> LB
7961
+                R = z3 + (z4 - z3) * ratio_y;   // Linear interp. RF -> RB
7960 7962
 
7961 7963
     /*
7962 7964
       SERIAL_ECHOPAIR("gridx=", gridx);
@@ -7969,7 +7971,7 @@ void ok_to_send() {
7969 7971
       SERIAL_ECHOPAIR(" z4=", z4);
7970 7972
       SERIAL_ECHOPAIR(" L=", L);
7971 7973
       SERIAL_ECHOPAIR(" R=", R);
7972
-      SERIAL_ECHOPAIR(" offset=", L + ratio_x * (R - L);
7974
+      SERIAL_ECHOPAIR(" offset=", L + ratio_x * (R - L));
7973 7975
     //*/
7974 7976
 
7975 7977
     return L + ratio_x * (R - L);

Loading…
Cancel
Save