Browse Source

Fix fade bug in (un)apply_leveling

Scott Lahteine 7 years ago
parent
commit
8df42a84a4
1 changed files with 51 additions and 81 deletions
  1. 51
    81
      Marlin/planner.cpp

+ 51
- 81
Marlin/planner.cpp View File

@@ -554,34 +554,13 @@ void Planner::calculate_volumetric_multipliers() {
554 554
 
555 555
 #if PLANNER_LEVELING
556 556
   /**
557
-   * rx, ry, rz - cartesian position in mm
557
+   * rx, ry, rz - Cartesian positions in mm
558 558
    */
559 559
   void Planner::apply_leveling(float &rx, float &ry, float &rz) {
560 560
 
561 561
     if (!leveling_active) return;
562 562
 
563
-    #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
564
-      const float fade_scaling_factor = fade_scaling_factor_for_z(rz);
565
-      if (!fade_scaling_factor) return;
566
-    #else
567
-      constexpr float fade_scaling_factor = 1.0;
568
-    #endif
569
-
570
-    #if ENABLED(AUTO_BED_LEVELING_UBL)
571
-
572
-      rz += ubl.get_z_correction(rx, ry) * fade_scaling_factor;
573
-
574
-    #elif ENABLED(MESH_BED_LEVELING)
575
-
576
-      rz += mbl.get_z(rx, ry
577
-        #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
578
-          , fade_scaling_factor
579
-        #endif
580
-      );
581
-
582
-    #elif ABL_PLANAR
583
-
584
-      UNUSED(fade_scaling_factor);
563
+    #if ABL_PLANAR
585 564
 
586 565
       float dx = rx - (X_TILT_FULCRUM),
587 566
             dy = ry - (Y_TILT_FULCRUM);
@@ -591,68 +570,43 @@ void Planner::calculate_volumetric_multipliers() {
591 570
       rx = dx + X_TILT_FULCRUM;
592 571
       ry = dy + Y_TILT_FULCRUM;
593 572
 
594
-    #elif ENABLED(AUTO_BED_LEVELING_BILINEAR)
595
-
596
-      float tmp[XYZ] = { rx, ry, 0 };
597
-      rz += bilinear_z_offset(tmp) * fade_scaling_factor;
598
-
599
-    #endif
600
-  }
601
-
602
-  void Planner::unapply_leveling(float raw[XYZ]) {
603
-
604
-    #if HAS_LEVELING
605
-      if (!leveling_active) return;
606
-    #endif
607
-
608
-    #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
609
-      if (!leveling_active_at_z(raw[Z_AXIS])) return;
610
-    #endif
611
-
612
-    #if ENABLED(AUTO_BED_LEVELING_UBL)
613
-
614
-      const float z_physical = raw[Z_AXIS],
615
-                  z_correct = ubl.get_z_correction(raw[X_AXIS], raw[Y_AXIS]),
616
-                  z_virtual = z_physical - z_correct;
617
-            float z_raw = z_virtual;
573
+    #else
618 574
 
619 575
       #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
576
+        const float fade_scaling_factor = fade_scaling_factor_for_z(rz);
577
+        if (!fade_scaling_factor) return;
578
+      #elif HAS_MESH
579
+        constexpr float fade_scaling_factor = 1.0;
580
+      #endif
620 581
 
621
-        // for P=physical_z, L=logical_z, M=mesh_z, H=fade_height,
622
-        // Given P=L+M(1-L/H) (faded mesh correction formula for L<H)
623
-        //  then L=P-M(1-L/H)
624
-        //    so L=P-M+ML/H
625
-        //    so L-ML/H=P-M
626
-        //    so L(1-M/H)=P-M
627
-        //    so L=(P-M)/(1-M/H) for L<H
628
-
629
-        if (planner.z_fade_height) {
630
-          if (z_raw >= planner.z_fade_height)
631
-            z_raw = z_physical;
632
-          else
633
-            z_raw /= 1.0 - z_correct * planner.inverse_z_fade_height;
634
-        }
635
-
636
-      #endif // ENABLE_LEVELING_FADE_HEIGHT
637
-
638
-      raw[Z_AXIS] = z_raw;
582
+      #if ENABLED(AUTO_BED_LEVELING_BILINEAR)
583
+        const float raw[XYZ] = { rx, ry, 0 };
584
+      #endif
639 585
 
640
-      return; // don't fall thru to other ENABLE_LEVELING_FADE_HEIGHT logic
586
+      rz += (
587
+        #if ENABLED(AUTO_BED_LEVELING_UBL)
588
+          ubl.get_z_correction(rx, ry) * fade_scaling_factor
589
+        #elif ENABLED(MESH_BED_LEVELING)
590
+          mbl.get_z(rx, ry
591
+            #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
592
+              , fade_scaling_factor
593
+            #endif
594
+          )
595
+        #elif ENABLED(AUTO_BED_LEVELING_BILINEAR)
596
+          bilinear_z_offset(raw) * fade_scaling_factor
597
+        #else
598
+          0
599
+        #endif
600
+      );
641 601
 
642 602
     #endif
603
+  }
643 604
 
644
-    #if ENABLED(MESH_BED_LEVELING)
605
+  void Planner::unapply_leveling(float raw[XYZ]) {
645 606
 
646
-      if (leveling_active) {
647
-        #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
648
-          const float c = mbl.get_z(raw[X_AXIS], raw[Y_AXIS], 1.0);
649
-          raw[Z_AXIS] = (z_fade_height * (raw[Z_AXIS]) - c) / (z_fade_height - c);
650
-        #else
651
-          raw[Z_AXIS] -= mbl.get_z(raw[X_AXIS], raw[Y_AXIS]);
652
-        #endif
653
-      }
607
+    if (!leveling_active) return;
654 608
 
655
-    #elif ABL_PLANAR
609
+    #if ABL_PLANAR
656 610
 
657 611
       matrix_3x3 inverse = matrix_3x3::transpose(bed_level_matrix);
658 612
 
@@ -664,15 +618,31 @@ void Planner::calculate_volumetric_multipliers() {
664 618
       raw[X_AXIS] = dx + X_TILT_FULCRUM;
665 619
       raw[Y_AXIS] = dy + Y_TILT_FULCRUM;
666 620
 
667
-    #elif ENABLED(AUTO_BED_LEVELING_BILINEAR)
621
+    #else
668 622
 
669 623
       #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
670
-        const float c = bilinear_z_offset(raw);
671
-        raw[Z_AXIS] = (z_fade_height * (raw[Z_AXIS]) - c) / (z_fade_height - c);
672
-      #else
673
-        raw[Z_AXIS] -= bilinear_z_offset(raw);
624
+        const float fade_scaling_factor = fade_scaling_factor_for_z(raw[Z_AXIS]);
625
+        if (!fade_scaling_factor) return;
626
+      #elif HAS_MESH
627
+        constexpr float fade_scaling_factor = 1.0;
674 628
       #endif
675 629
 
630
+      raw[Z_AXIS] -= (
631
+        #if ENABLED(AUTO_BED_LEVELING_UBL)
632
+          ubl.get_z_correction(raw[X_AXIS], raw[Y_AXIS]) * fade_scaling_factor
633
+        #elif ENABLED(MESH_BED_LEVELING)
634
+          mbl.get_z(raw[X_AXIS], raw[Y_AXIS]
635
+            #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
636
+              , fade_scaling_factor
637
+            #endif
638
+          )
639
+        #elif ENABLED(AUTO_BED_LEVELING_BILINEAR)
640
+          bilinear_z_offset(raw) * fade_scaling_factor
641
+        #else
642
+          0
643
+        #endif
644
+      );
645
+
676 646
     #endif
677 647
   }
678 648
 

Loading…
Cancel
Save