Bladeren bron

Add M420 C to adjust the whole mesh (#10522)

Scott Lahteine 7 jaren geleden
bovenliggende
commit
eef0248a1c
No account linked to committer's email address

+ 2
- 2
Marlin/src/feature/bedlevel/ubl/ubl.h Bestand weergeven

@@ -89,7 +89,6 @@ class unified_bed_leveling {
89 89
     #endif
90 90
 
91 91
     static bool g29_parameter_parsing() _O0;
92
-    static void find_mean_mesh_height();
93 92
     static void shift_mesh_height();
94 93
     static void probe_entire_mesh(const float &rx, const float &ry, const bool do_ubl_mesh_map, const bool stow_probe, const bool do_furthest) _O0;
95 94
     static void tilt_mesh_based_on_3pts(const float &z1, const float &z2, const float &z3);
@@ -124,7 +123,8 @@ class unified_bed_leveling {
124 123
     static mesh_index_pair find_furthest_invalid_mesh_point() _O0;
125 124
     static void reset();
126 125
     static void invalidate();
127
-    static void set_all_mesh_points_to_value(const float);
126
+    static void set_all_mesh_points_to_value(const float value);
127
+    static void adjust_mesh_to_mean(const float value);
128 128
     static bool sanity_check();
129 129
 
130 130
     static void G29() _O0;                          // O0 for no optimization

+ 4
- 4
Marlin/src/feature/bedlevel/ubl/ubl_G29.cpp Bestand weergeven

@@ -544,7 +544,7 @@
544 544
           #endif
545 545
           break;
546 546
 
547
-        case 5: find_mean_mesh_height(); break;
547
+        case 5: adjust_mesh_to_mean(g29_constant); break;
548 548
 
549 549
         case 6: shift_mesh_height(); break;
550 550
       }
@@ -634,7 +634,7 @@
634 634
     return;
635 635
   }
636 636
 
637
-  void unified_bed_leveling::find_mean_mesh_height() {
637
+  void unified_bed_leveling::adjust_mesh_to_mean(const float value) {
638 638
     float sum = 0.0;
639 639
     int n = 0;
640 640
     for (uint8_t x = 0; x < GRID_MAX_POINTS_X; x++)
@@ -669,7 +669,7 @@
669 669
       for (uint8_t x = 0; x < GRID_MAX_POINTS_X; x++)
670 670
         for (uint8_t y = 0; y < GRID_MAX_POINTS_Y; y++)
671 671
           if (!isnan(z_values[x][y]))
672
-            z_values[x][y] -= mean + g29_constant;
672
+            z_values[x][y] -= mean + value;
673 673
   }
674 674
 
675 675
   void unified_bed_leveling::shift_mesh_height() {
@@ -1081,7 +1081,7 @@
1081 1081
       SERIAL_EOL();
1082 1082
     #endif
1083 1083
 
1084
-    find_mean_mesh_height();
1084
+    adjust_mesh_to_mean(g29_constant);
1085 1085
 
1086 1086
     #if HAS_BED_PROBE
1087 1087
       SERIAL_PROTOCOLPGM("zprobe_zoffset: ");

+ 78
- 9
Marlin/src/gcode/bedlevel/M420.cpp Bestand weergeven

@@ -32,6 +32,8 @@
32 32
   #include "../../module/configuration_store.h"
33 33
 #endif
34 34
 
35
+//#define M420_C_USE_MEAN
36
+
35 37
 /**
36 38
  * M420: Enable/Disable Bed Leveling and/or set the Z fade height.
37 39
  *
@@ -43,8 +45,18 @@
43 45
  *
44 46
  *   L[index]  Load UBL mesh from index (0 is default)
45 47
  *   T[map]    0:Human-readable 1:CSV 2:"LCD" 4:Compact
48
+ *
49
+ * With mesh-based leveling only:
50
+ *
51
+ *   C         Center mesh on the mean of the lowest and highest
46 52
  */
47 53
 void GcodeSuite::M420() {
54
+  const bool seen_S = parser.seen('S');
55
+  bool to_enable = seen_S ? parser.value_bool() : planner.leveling_active;
56
+
57
+  // If disabling leveling do it right away
58
+  // (Don't disable for just M420 or M420 V)
59
+  if (seen_S && !to_enable) set_bed_leveling_enabled(false);
48 60
 
49 61
   const float oldpos[] = { current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS] };
50 62
 
@@ -53,6 +65,8 @@ void GcodeSuite::M420() {
53 65
     // L to load a mesh from the EEPROM
54 66
     if (parser.seen('L')) {
55 67
 
68
+      set_bed_leveling_enabled(false);
69
+
56 70
       #if ENABLED(EEPROM_SETTINGS)
57 71
         const int8_t storage_slot = parser.has_value() ? parser.value_int() : ubl.storage_slot;
58 72
         const int16_t a = settings.calc_num_meshes();
@@ -88,6 +102,65 @@ void GcodeSuite::M420() {
88 102
 
89 103
   #endif // AUTO_BED_LEVELING_UBL
90 104
 
105
+  #if HAS_MESH
106
+
107
+    #if ENABLED(MESH_BED_LEVELING)
108
+      #define Z_VALUES(X,Y) mbl.z_values[X][Y]
109
+    #else
110
+      #define Z_VALUES(X,Y) z_values[X][Y]
111
+    #endif
112
+
113
+    // Subtract the given value or the mean from all mesh values
114
+    if (leveling_is_valid() && parser.seen('C')) {
115
+      const float cval = parser.value_float();
116
+      #if ENABLED(AUTO_BED_LEVELING_UBL)
117
+
118
+        set_bed_leveling_enabled(false);
119
+        ubl.adjust_mesh_to_mean(cval);
120
+
121
+      #else
122
+
123
+        #if ENABLED(M420_C_USE_MEAN)
124
+
125
+          // Get the sum and average of all mesh values
126
+          float mesh_sum = 0;
127
+          for (uint8_t x = GRID_MAX_POINTS_X; x--;)
128
+            for (uint8_t y = GRID_MAX_POINTS_Y; y--;)
129
+              mesh_sum += Z_VALUES(x, y);
130
+          const float zmean = mesh_sum / float(GRID_MAX_POINTS);
131
+
132
+        #else
133
+
134
+          // Find the low and high mesh values
135
+          float lo_val = 100, hi_val = -100;
136
+          for (uint8_t x = GRID_MAX_POINTS_X; x--;)
137
+            for (uint8_t y = GRID_MAX_POINTS_Y; y--;) {
138
+              const float z = Z_VALUES(x, y);
139
+              NOMORE(lo_val, z);
140
+              NOLESS(hi_val, z);
141
+            }
142
+          // Take the mean of the lowest and highest
143
+          const float zmean = (lo_val + hi_val) / 2.0 + cval;
144
+
145
+        #endif
146
+
147
+        // If not very close to 0, adjust the mesh
148
+        if (!NEAR_ZERO(zmean)) {
149
+          set_bed_leveling_enabled(false);
150
+          // Subtract the mean from all values
151
+          for (uint8_t x = GRID_MAX_POINTS_X; x--;)
152
+            for (uint8_t y = GRID_MAX_POINTS_Y; y--;)
153
+              Z_VALUES(x, y) -= zmean;
154
+          #if ENABLED(ABL_BILINEAR_SUBDIVISION)
155
+            bed_level_virt_interpolate();
156
+          #endif
157
+        }
158
+
159
+      #endif
160
+    }
161
+
162
+  #endif // HAS_MESH
163
+
91 164
   // V to print the matrix or mesh
92 165
   if (parser.seen('V')) {
93 166
     #if ABL_PLANAR
@@ -111,21 +184,17 @@ void GcodeSuite::M420() {
111 184
     if (parser.seen('Z')) set_z_fade_height(parser.value_linear_units(), false);
112 185
   #endif
113 186
 
114
-  bool to_enable = false;
115
-  if (parser.seen('S')) {
116
-    to_enable = parser.value_bool();
117
-    set_bed_leveling_enabled(to_enable);
118
-  }
119
-
120
-  const bool new_status = planner.leveling_active;
187
+  // Enable leveling if specified, or if previously active
188
+  set_bed_leveling_enabled(to_enable);
121 189
 
122
-  if (to_enable && !new_status) {
190
+  // Error if leveling failed to enable or reenable
191
+  if (to_enable && !planner.leveling_active) {
123 192
     SERIAL_ERROR_START();
124 193
     SERIAL_ERRORLNPGM(MSG_ERR_M420_FAILED);
125 194
   }
126 195
 
127 196
   SERIAL_ECHO_START();
128
-  SERIAL_ECHOLNPAIR("Bed Leveling ", new_status ? MSG_ON : MSG_OFF);
197
+  SERIAL_ECHOLNPAIR("Bed Leveling ", planner.leveling_active ? MSG_ON : MSG_OFF);
129 198
 
130 199
   #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
131 200
     SERIAL_ECHO_START();

Laden…
Annuleren
Opslaan