Browse Source

🐛 Fix Leveling apply/unapply (#24188)

Co-authored-by: Scott Lahteine <github@thinkyhead.com>
tombrazier 2 years ago
parent
commit
62057d3204
No account linked to committer's email address

+ 4
- 10
Marlin/src/feature/bedlevel/bedlevel.cpp View File

@@ -74,16 +74,10 @@ void set_bed_leveling_enabled(const bool enable/*=true*/) {
74 74
     _report_leveling();
75 75
     planner.synchronize();
76 76
 
77
-    if (planner.leveling_active) {      // leveling from on to off
78
-      // change unleveled current_position to physical current_position without moving steppers.
79
-      planner.apply_leveling(current_position);
80
-      planner.leveling_active = false;  // disable only AFTER calling apply_leveling
81
-    }
82
-    else {                              // leveling from off to on
83
-      planner.leveling_active = true;   // enable BEFORE calling unapply_leveling, otherwise ignored
84
-      // change physical current_position to unleveled current_position without moving steppers.
85
-      planner.unapply_leveling(current_position);
86
-    }
77
+    // Get the corrected leveled / unleveled position
78
+    planner.apply_modifiers(current_position);    // Physical position with all modifiers
79
+    planner.leveling_active ^= true;              // Toggle leveling between apply and unapply
80
+    planner.unapply_modifiers(current_position);  // Logical position with modifiers removed
87 81
 
88 82
     sync_plan_position();
89 83
     _report_leveling();

+ 27
- 26
Marlin/src/gcode/bedlevel/abl/G29.cpp View File

@@ -74,14 +74,18 @@
74 74
   #endif
75 75
 #endif
76 76
 
77
+static void pre_g29_return(const bool retry, const bool did) {
78
+  if (!retry) {
79
+    TERN_(FULL_REPORT_TO_HOST_FEATURE, set_and_report_grblstate(M_IDLE, false));
80
+  }
81
+  if (did) {
82
+    TERN_(HAS_DWIN_E3V2_BASIC, DWIN_LevelingDone());
83
+    TERN_(EXTENSIBLE_UI, ExtUI::onLevelingDone());
84
+  }
85
+}
86
+
77 87
 #define G29_RETURN(retry, did) do{ \
78
-  if (TERN(G29_RETRY_AND_RECOVER, !retry, true)) { \
79
-    TERN_(FULL_REPORT_TO_HOST_FEATURE, set_and_report_grblstate(M_IDLE, false)); \
80
-  } \
81
-  if (did) { \
82
-    TERN_(HAS_DWIN_E3V2_BASIC, DWIN_LevelingDone()); \
83
-    TERN_(EXTENSIBLE_UI, ExtUI::onLevelingDone()); \
84
-  } \
88
+  pre_g29_return(TERN0(G29_RETRY_AND_RECOVER, retry), did); \
85 89
   return TERN_(G29_RETRY_AND_RECOVER, retry); \
86 90
 }while(0)
87 91
 
@@ -326,8 +330,10 @@ G29_TYPE GcodeSuite::G29() {
326 330
           bedlevel.z_values[i][j] = rz;
327 331
           bedlevel.refresh_bed_level();
328 332
           TERN_(EXTENSIBLE_UI, ExtUI::onMeshUpdate(i, j, rz));
329
-          set_bed_leveling_enabled(abl.reenable);
330
-          if (abl.reenable) report_current_position();
333
+          if (abl.reenable) {
334
+            set_bed_leveling_enabled(true);
335
+            report_current_position();
336
+          }
331 337
         }
332 338
         G29_RETURN(false, false);
333 339
       } // parser.seen_test('W')
@@ -729,7 +735,7 @@ G29_TYPE GcodeSuite::G29() {
729 735
 
730 736
           #endif
731 737
 
732
-          abl.reenable = false;
738
+          abl.reenable = false; // Don't re-enable after modifying the mesh
733 739
           idle_no_sleep();
734 740
 
735 741
         } // inner
@@ -914,33 +920,28 @@ G29_TYPE GcodeSuite::G29() {
914 920
         current_position = converted;
915 921
 
916 922
         if (DEBUGGING(LEVELING)) DEBUG_POS("G29 corrected XYZ", current_position);
917
-      }
918 923
 
919
-    #elif ENABLED(AUTO_BED_LEVELING_BILINEAR)
924
+        abl.reenable = true;
925
+      }
920 926
 
921
-      if (!abl.dryrun) {
922
-        if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("G29 uncorrected Z:", current_position.z);
927
+      // Auto Bed Leveling is complete! Enable if possible.
928
+      if (abl.reenable) {
929
+        planner.leveling_active = true;
930
+        sync_plan_position();
931
+      }
923 932
 
924
-        // Unapply the offset because it is going to be immediately applied
925
-        // and cause compensation movement in Z
926
-        current_position.z -= bedlevel.get_z_correction(current_position)
927
-          TERN_(ENABLE_LEVELING_FADE_HEIGHT, * planner.fade_scaling_factor_for_z(current_position.z));
933
+    #elif ENABLED(AUTO_BED_LEVELING_BILINEAR)
928 934
 
929
-        if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM(" corrected Z:", current_position.z);
930
-      }
935
+      // Auto Bed Leveling is complete! Enable if possible.
936
+      if (!abl.dryrun || abl.reenable) set_bed_leveling_enabled(true);
931 937
 
932
-    #endif // ABL_PLANAR
938
+    #endif
933 939
 
934
-    // Auto Bed Leveling is complete! Enable if possible.
935
-    planner.leveling_active = !abl.dryrun || abl.reenable;
936 940
   } // !isnan(abl.measured_z)
937 941
 
938 942
   // Restore state after probing
939 943
   if (!faux) restore_feedrate_and_scaling();
940 944
 
941
-  // Sync the planner from the current_position
942
-  if (planner.leveling_active) sync_plan_position();
943
-
944 945
   TERN_(HAS_BED_PROBE, probe.move_z_after_probing());
945 946
 
946 947
   #ifdef Z_PROBE_END_SCRIPT

+ 21
- 17
Marlin/src/module/planner.cpp View File

@@ -1598,30 +1598,34 @@ void Planner::check_axes_activity() {
1598 1598
   }
1599 1599
 
1600 1600
   void Planner::unapply_leveling(xyz_pos_t &raw) {
1601
+    if (!leveling_active) return;
1601 1602
 
1602
-    if (leveling_active) {
1603
-
1604
-      #if ABL_PLANAR
1605
-
1606
-        matrix_3x3 inverse = matrix_3x3::transpose(bed_level_matrix);
1603
+    #if ABL_PLANAR
1607 1604
 
1608
-        xy_pos_t d = raw - level_fulcrum;
1609
-        inverse.apply_rotation_xyz(d.x, d.y, raw.z);
1610
-        raw = d + level_fulcrum;
1605
+      matrix_3x3 inverse = matrix_3x3::transpose(bed_level_matrix);
1611 1606
 
1612
-      #elif HAS_MESH
1607
+      xy_pos_t d = raw - level_fulcrum;
1608
+      inverse.apply_rotation_xyz(d.x, d.y, raw.z);
1609
+      raw = d + level_fulcrum;
1613 1610
 
1614
-        TERN_(MESH_BED_LEVELING, raw.z -= bedlevel.get_z_offset());
1611
+    #elif HAS_MESH
1615 1612
 
1616
-        #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
1617
-          const float fade_scaling_factor = fade_scaling_factor_for_z(raw.z);
1618
-          if (fade_scaling_factor) raw.z -= fade_scaling_factor * bedlevel.get_z_correction(raw);
1619
-        #else
1620
-          raw.z -= bedlevel.get_z_correction(raw);
1621
-        #endif
1613
+      const float z_correction = bedlevel.get_z_correction(raw),
1614
+                  z_full_fade = DIFF_TERN(MESH_BED_LEVELING, raw.z, bedlevel.get_z_offset()),
1615
+                  z_no_fade = z_full_fade - z_correction;
1622 1616
 
1617
+      #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
1618
+        if (!z_fade_height || z_no_fade <= 0.0f)                              // Not fading or at bed level?
1619
+          raw.z = z_no_fade;                                                  //  Unapply full mesh Z.
1620
+        else if (z_full_fade >= z_fade_height)                                // Above the fade height?
1621
+          raw.z = z_full_fade;                                                //  Nothing more to unapply.
1622
+        else                                                                  // Within the fade zone?
1623
+          raw.z = z_no_fade / (1.0f - z_correction * inverse_z_fade_height);  // Unapply the faded Z offset
1624
+      #else
1625
+        raw.z = z_no_fade;
1623 1626
       #endif
1624
-    }
1627
+
1628
+    #endif
1625 1629
   }
1626 1630
 
1627 1631
 #endif // HAS_LEVELING

Loading…
Cancel
Save