Преглед изворни кода

Merge pull request #4781 from thinkyhead/rc_final_z_correction

Use simplified Z correction in G29 when possible
Scott Lahteine пре 7 година
родитељ
комит
d41eeb62af
1 измењених фајлова са 46 додато и 36 уклоњено
  1. 46
    36
      Marlin/Marlin_main.cpp

+ 46
- 36
Marlin/Marlin_main.cpp Прегледај датотеку

@@ -3397,6 +3397,8 @@ inline void gcode_G28() {
3397 3397
 
3398 3398
     bed_leveling_in_progress = true;
3399 3399
 
3400
+    float xProbe, yProbe, measured_z = 0;
3401
+
3400 3402
     #if ENABLED(AUTO_BED_LEVELING_GRID)
3401 3403
 
3402 3404
       // probe at the points of a lattice grid
@@ -3434,8 +3436,8 @@ inline void gcode_G28() {
3434 3436
       bool zig = auto_bed_leveling_grid_points & 1; //always end at [RIGHT_PROBE_BED_POSITION, BACK_PROBE_BED_POSITION]
3435 3437
 
3436 3438
       for (uint8_t yCount = 0; yCount < auto_bed_leveling_grid_points; yCount++) {
3437
-        float yBase = front_probe_bed_position + yGridSpacing * yCount,
3438
-              yProbe = floor(yBase + (yBase < 0 ? 0 : 0.5));
3439
+        float yBase = front_probe_bed_position + yGridSpacing * yCount;
3440
+        yProbe = floor(yBase + (yBase < 0 ? 0 : 0.5));
3439 3441
         int8_t xStart, xStop, xInc;
3440 3442
 
3441 3443
         if (zig) {
@@ -3452,8 +3454,8 @@ inline void gcode_G28() {
3452 3454
         zig = !zig;
3453 3455
 
3454 3456
         for (int8_t xCount = xStart; xCount != xStop; xCount += xInc) {
3455
-          float xBase = left_probe_bed_position + xGridSpacing * xCount,
3456
-                xProbe = floor(xBase + (xBase < 0 ? 0 : 0.5));
3457
+          float xBase = left_probe_bed_position + xGridSpacing * xCount;
3458
+          xProbe = floor(xBase + (xBase < 0 ? 0 : 0.5));
3457 3459
 
3458 3460
           #if ENABLED(DELTA)
3459 3461
             // Avoid probing outside the round or hexagonal area of a delta printer
@@ -3497,12 +3499,12 @@ inline void gcode_G28() {
3497 3499
         vector_3(ABL_PROBE_PT_3_X, ABL_PROBE_PT_3_Y, 0)
3498 3500
       };
3499 3501
 
3500
-      for (uint8_t i = 0; i < 3; ++i)
3501
-        points[i].z = probe_pt(
3502
-          LOGICAL_X_POSITION(points[i].x),
3503
-          LOGICAL_Y_POSITION(points[i].y),
3504
-          stow_probe_after_each, verbose_level
3505
-        );
3502
+      for (uint8_t i = 0; i < 3; ++i) {
3503
+        // Retain the last probe position
3504
+        xProbe = LOGICAL_X_POSITION(points[i].x);
3505
+        yProbe = LOGICAL_Y_POSITION(points[i].y);
3506
+        measured_z = points[i].z = probe_pt(xProbe, yProbe, stow_probe_after_each, verbose_level);
3507
+      }
3506 3508
 
3507 3509
       if (!dryrun) {
3508 3510
         vector_3 planeNormal = vector_3::cross(points[0] - points[1], points[2] - points[1]).get_normal();
@@ -3635,42 +3637,50 @@ inline void gcode_G28() {
3635 3637
         // Correct the current XYZ position based on the tilted plane.
3636 3638
         //
3637 3639
 
3638
-        // Get the distance from the reference point to the current position
3639
-        // The current XY is in sync with the planner/steppers at this point
3640
-        // but the current Z is only known to the steppers.
3640
+        // 1. Get the distance from the current position to the reference point.
3641 3641
         float x_dist = RAW_CURRENT_POSITION(X_AXIS) - X_TILT_FULCRUM,
3642 3642
               y_dist = RAW_CURRENT_POSITION(Y_AXIS) - Y_TILT_FULCRUM,
3643
-              z_real = RAW_Z_POSITION(stepper.get_axis_position_mm(Z_AXIS));
3643
+              z_real = RAW_CURRENT_POSITION(Z_AXIS),
3644
+              z_zero = 0;
3644 3645
 
3645 3646
         #if ENABLED(DEBUG_LEVELING_FEATURE)
3646
-          if (DEBUGGING(LEVELING)) {
3647
-            SERIAL_ECHOPAIR("BEFORE ROTATION ... x_dist:", x_dist);
3648
-            SERIAL_ECHOPAIR("y_dist:", y_dist);
3649
-            SERIAL_ECHOPAIR("z_real:", z_real);
3650
-          }
3647
+          if (DEBUGGING(LEVELING)) DEBUG_POS("G29 uncorrected XYZ", current_position);
3651 3648
         #endif
3652 3649
 
3653
-        // Apply the matrix to the distance from the reference point to XY,
3654
-        // and from the homed Z to the current Z.
3655
-        apply_rotation_xyz(planner.bed_level_matrix, x_dist, y_dist, z_real);
3650
+        matrix_3x3 inverse = matrix_3x3::transpose(planner.bed_level_matrix);
3656 3651
 
3657
-        #if ENABLED(DEBUG_LEVELING_FEATURE)
3658
-          if (DEBUGGING(LEVELING)) {
3659
-            SERIAL_ECHOPAIR("AFTER ROTATION ... x_dist:", x_dist);
3660
-            SERIAL_ECHOPAIR("y_dist:", y_dist);
3661
-            SERIAL_ECHOPAIR("z_real:", z_real);
3662
-          }
3663
-        #endif
3652
+        // 2. Apply the inverse matrix to the distance
3653
+        //    from the reference point to X, Y, and zero.
3654
+        apply_rotation_xyz(inverse, x_dist, y_dist, z_zero);
3655
+
3656
+        // 3. Get the matrix-based corrected Z.
3657
+        //    (Even if not used, get it for comparison.)
3658
+        float new_z = z_real + z_zero;
3659
+
3660
+        // 4. Use the last measured distance to the bed, if possible
3661
+        if ( NEAR(current_position[X_AXIS], xProbe - (X_PROBE_OFFSET_FROM_EXTRUDER))
3662
+          && NEAR(current_position[Y_AXIS], yProbe - (Y_PROBE_OFFSET_FROM_EXTRUDER))
3663
+        ) {
3664
+          float simple_z = z_real - (measured_z - (-zprobe_zoffset));
3665
+          #if ENABLED(DEBUG_LEVELING_FEATURE)
3666
+            if (DEBUGGING(LEVELING)) {
3667
+              SERIAL_ECHOPAIR("Z from Probe:", simple_z);
3668
+              SERIAL_ECHOPAIR("  Matrix:", new_z);
3669
+              SERIAL_ECHOLNPAIR("  Discrepancy:", simple_z - new_z);
3670
+            }
3671
+          #endif
3672
+          new_z = simple_z;
3673
+        }
3664 3674
 
3665
-        // Apply the rotated distance and Z to the current position
3666
-        current_position[X_AXIS] = LOGICAL_X_POSITION(X_TILT_FULCRUM + x_dist);
3667
-        current_position[Y_AXIS] = LOGICAL_Y_POSITION(Y_TILT_FULCRUM + y_dist);
3668
-        current_position[Z_AXIS] = LOGICAL_Z_POSITION(z_real);
3675
+        // 5. The rotated XY and corrected Z are now current_position
3676
+        current_position[X_AXIS] = LOGICAL_X_POSITION(x_dist) + X_TILT_FULCRUM;
3677
+        current_position[Y_AXIS] = LOGICAL_Y_POSITION(y_dist) + Y_TILT_FULCRUM;
3678
+        current_position[Z_AXIS] = LOGICAL_Z_POSITION(new_z);
3669 3679
 
3670 3680
         SYNC_PLAN_POSITION_KINEMATIC();
3671 3681
 
3672 3682
         #if ENABLED(DEBUG_LEVELING_FEATURE)
3673
-          if (DEBUGGING(LEVELING)) DEBUG_POS("> corrected XYZ in G29", current_position);
3683
+          if (DEBUGGING(LEVELING)) DEBUG_POS("G29 corrected XYZ", current_position);
3674 3684
         #endif
3675 3685
       }
3676 3686
 
@@ -7962,8 +7972,8 @@ void set_current_from_steppers_for_axis(AxisEnum axis) {
7962 7972
     LOOP_XYZE(i) difference[i] = target[i] - current_position[i];
7963 7973
 
7964 7974
     float cartesian_mm = sqrt(sq(difference[X_AXIS]) + sq(difference[Y_AXIS]) + sq(difference[Z_AXIS]));
7965
-    if (cartesian_mm < 0.000001) cartesian_mm = abs(difference[E_AXIS]);
7966
-    if (cartesian_mm < 0.000001) return false;
7975
+    if (UNEAR_ZERO(cartesian_mm)) cartesian_mm = abs(difference[E_AXIS]);
7976
+    if (UNEAR_ZERO(cartesian_mm)) return false;
7967 7977
     float _feedrate_mm_s = MMS_SCALED(feedrate_mm_s);
7968 7978
     float seconds = cartesian_mm / _feedrate_mm_s;
7969 7979
     int steps = max(1, int(delta_segments_per_second * seconds));

Loading…
Откажи
Сачувај