Browse Source

Move fade_scaling_factor_for_z to Planner

Scott Lahteine 7 years ago
parent
commit
88857e8028

+ 59
- 75
Marlin/Marlin_main.cpp View File

544
 #if ENABLED(AUTO_BED_LEVELING_BILINEAR)
544
 #if ENABLED(AUTO_BED_LEVELING_BILINEAR)
545
   #if ENABLED(DELTA)
545
   #if ENABLED(DELTA)
546
     #define ADJUST_DELTA(V) \
546
     #define ADJUST_DELTA(V) \
547
-      if (planner.abl_enabled) { \
547
+      if (planner.leveling_active) { \
548
         const float zadj = bilinear_z_offset(V); \
548
         const float zadj = bilinear_z_offset(V); \
549
         delta[A_AXIS] += zadj; \
549
         delta[A_AXIS] += zadj; \
550
         delta[B_AXIS] += zadj; \
550
         delta[B_AXIS] += zadj; \
551
         delta[C_AXIS] += zadj; \
551
         delta[C_AXIS] += zadj; \
552
       }
552
       }
553
   #else
553
   #else
554
-    #define ADJUST_DELTA(V) if (planner.abl_enabled) { delta[Z_AXIS] += bilinear_z_offset(V); }
554
+    #define ADJUST_DELTA(V) if (planner.leveling_active) { delta[Z_AXIS] += bilinear_z_offset(V); }
555
   #endif
555
   #endif
556
 #elif IS_KINEMATIC
556
 #elif IS_KINEMATIC
557
   #define ADJUST_DELTA(V) NOOP
557
   #define ADJUST_DELTA(V) NOOP
2460
   bool leveling_is_valid() {
2460
   bool leveling_is_valid() {
2461
     return
2461
     return
2462
       #if ENABLED(MESH_BED_LEVELING)
2462
       #if ENABLED(MESH_BED_LEVELING)
2463
-        mbl.has_mesh()
2463
+        mbl.has_mesh
2464
       #elif ENABLED(AUTO_BED_LEVELING_BILINEAR)
2464
       #elif ENABLED(AUTO_BED_LEVELING_BILINEAR)
2465
         !!bilinear_grid_spacing[X_AXIS]
2465
         !!bilinear_grid_spacing[X_AXIS]
2466
       #elif ENABLED(AUTO_BED_LEVELING_UBL)
2466
       #elif ENABLED(AUTO_BED_LEVELING_UBL)
2486
       constexpr bool can_change = true;
2486
       constexpr bool can_change = true;
2487
     #endif
2487
     #endif
2488
 
2488
 
2489
-    if (can_change && enable != LEVELING_IS_ACTIVE()) {
2489
+    if (can_change && enable != planner.leveling_active) {
2490
 
2490
 
2491
       #if ENABLED(MESH_BED_LEVELING)
2491
       #if ENABLED(MESH_BED_LEVELING)
2492
 
2492
 
2494
           planner.apply_leveling(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS]);
2494
           planner.apply_leveling(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS]);
2495
 
2495
 
2496
         const bool enabling = enable && leveling_is_valid();
2496
         const bool enabling = enable && leveling_is_valid();
2497
-        mbl.set_active(enabling);
2497
+        planner.leveling_active = enabling;
2498
         if (enabling) planner.unapply_leveling(current_position);
2498
         if (enabling) planner.unapply_leveling(current_position);
2499
 
2499
 
2500
       #elif ENABLED(AUTO_BED_LEVELING_UBL)
2500
       #elif ENABLED(AUTO_BED_LEVELING_UBL)
2501
         #if PLANNER_LEVELING
2501
         #if PLANNER_LEVELING
2502
-          if (ubl.state.active) {                       // leveling from on to off
2502
+          if (planner.leveling_active) {                       // leveling from on to off
2503
             // change unleveled current_position to physical current_position without moving steppers.
2503
             // change unleveled current_position to physical current_position without moving steppers.
2504
             planner.apply_leveling(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS]);
2504
             planner.apply_leveling(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS]);
2505
-            ubl.state.active = false;                   // disable only AFTER calling apply_leveling
2505
+            planner.leveling_active = false;                   // disable only AFTER calling apply_leveling
2506
           }
2506
           }
2507
           else {                                        // leveling from off to on
2507
           else {                                        // leveling from off to on
2508
-            ubl.state.active = true;                    // enable BEFORE calling unapply_leveling, otherwise ignored
2508
+            planner.leveling_active = true;                    // enable BEFORE calling unapply_leveling, otherwise ignored
2509
             // change physical current_position to unleveled current_position without moving steppers.
2509
             // change physical current_position to unleveled current_position without moving steppers.
2510
             planner.unapply_leveling(current_position);
2510
             planner.unapply_leveling(current_position);
2511
           }
2511
           }
2512
         #else
2512
         #else
2513
-          ubl.state.active = enable;                    // just flip the bit, current_position will be wrong until next move.
2513
+          planner.leveling_active = enable;                    // just flip the bit, current_position will be wrong until next move.
2514
         #endif
2514
         #endif
2515
 
2515
 
2516
       #else // ABL
2516
       #else // ABL
2522
         #endif
2522
         #endif
2523
 
2523
 
2524
         // Enable or disable leveling compensation in the planner
2524
         // Enable or disable leveling compensation in the planner
2525
-        planner.abl_enabled = enable;
2525
+        planner.leveling_active = enable;
2526
 
2526
 
2527
         if (!enable)
2527
         if (!enable)
2528
           // When disabling just get the current position from the steppers.
2528
           // When disabling just get the current position from the steppers.
2547
 
2547
 
2548
     void set_z_fade_height(const float zfh) {
2548
     void set_z_fade_height(const float zfh) {
2549
 
2549
 
2550
-      const bool level_active = LEVELING_IS_ACTIVE();
2550
+      const bool level_active = planner.leveling_active;
2551
 
2551
 
2552
       #if ENABLED(AUTO_BED_LEVELING_UBL)
2552
       #if ENABLED(AUTO_BED_LEVELING_UBL)
2553
+        if (level_active) set_bed_leveling_enabled(false);  // turn off before changing fade height for proper apply/unapply leveling to maintain current_position
2554
+      #endif
2553
 
2555
 
2554
-        if (level_active)
2555
-          set_bed_leveling_enabled(false);  // turn off before changing fade height for proper apply/unapply leveling to maintain current_position
2556
-        planner.z_fade_height = zfh;
2557
-        planner.inverse_z_fade_height = RECIPROCAL(zfh);
2558
-        if (level_active)
2559
-          set_bed_leveling_enabled(true);  // turn back on after changing fade height
2560
-
2561
-      #else
2562
-
2563
-        planner.z_fade_height = zfh;
2564
-        planner.inverse_z_fade_height = RECIPROCAL(zfh);
2556
+      planner.set_z_fade_height(zfh);
2565
 
2557
 
2566
-        if (level_active) {
2558
+      if (level_active) {
2559
+        #if ENABLED(AUTO_BED_LEVELING_UBL)
2560
+          set_bed_leveling_enabled(true);  // turn back on after changing fade height
2561
+        #else
2567
           set_current_from_steppers_for_axis(
2562
           set_current_from_steppers_for_axis(
2568
             #if ABL_PLANAR
2563
             #if ABL_PLANAR
2569
               ALL_AXES
2564
               ALL_AXES
2571
               Z_AXIS
2566
               Z_AXIS
2572
             #endif
2567
             #endif
2573
           );
2568
           );
2574
-        }
2575
-      #endif
2569
+        #endif
2570
+      }
2576
     }
2571
     }
2577
 
2572
 
2578
   #endif // LEVELING_FADE_HEIGHT
2573
   #endif // LEVELING_FADE_HEIGHT
2585
     #if ENABLED(MESH_BED_LEVELING)
2580
     #if ENABLED(MESH_BED_LEVELING)
2586
       if (leveling_is_valid()) {
2581
       if (leveling_is_valid()) {
2587
         mbl.reset();
2582
         mbl.reset();
2588
-        mbl.set_has_mesh(false);
2583
+        mbl.has_mesh = false;
2589
       }
2584
       }
2590
     #else
2585
     #else
2591
       #if ENABLED(DEBUG_LEVELING_FEATURE)
2586
       #if ENABLED(DEBUG_LEVELING_FEATURE)
3759
       #elif ENABLED(AUTO_BED_LEVELING_UBL)
3754
       #elif ENABLED(AUTO_BED_LEVELING_UBL)
3760
         SERIAL_ECHOPGM("UBL");
3755
         SERIAL_ECHOPGM("UBL");
3761
       #endif
3756
       #endif
3762
-      if (LEVELING_IS_ACTIVE()) {
3757
+      if (planner.leveling_active) {
3763
         SERIAL_ECHOLNPGM(" (enabled)");
3758
         SERIAL_ECHOLNPGM(" (enabled)");
3764
         #if ABL_PLANAR
3759
         #if ABL_PLANAR
3765
           const float diff[XYZ] = {
3760
           const float diff[XYZ] = {
3790
     #elif ENABLED(MESH_BED_LEVELING)
3785
     #elif ENABLED(MESH_BED_LEVELING)
3791
 
3786
 
3792
       SERIAL_ECHOPGM("Mesh Bed Leveling");
3787
       SERIAL_ECHOPGM("Mesh Bed Leveling");
3793
-      if (LEVELING_IS_ACTIVE()) {
3788
+      if (planner.leveling_active) {
3794
         float lz = current_position[Z_AXIS];
3789
         float lz = current_position[Z_AXIS];
3795
         planner.apply_leveling(current_position[X_AXIS], current_position[Y_AXIS], lz);
3790
         planner.apply_leveling(current_position[X_AXIS], current_position[Y_AXIS], lz);
3796
         SERIAL_ECHOLNPGM(" (enabled)");
3791
         SERIAL_ECHOLNPGM(" (enabled)");
3959
   // Disable the leveling matrix before homing
3954
   // Disable the leveling matrix before homing
3960
   #if HAS_LEVELING
3955
   #if HAS_LEVELING
3961
     #if ENABLED(AUTO_BED_LEVELING_UBL)
3956
     #if ENABLED(AUTO_BED_LEVELING_UBL)
3962
-      const bool ubl_state_at_entry = LEVELING_IS_ACTIVE();
3957
+      const bool ubl_state_at_entry = planner.leveling_active;
3963
     #endif
3958
     #endif
3964
     set_bed_leveling_enabled(false);
3959
     set_bed_leveling_enabled(false);
3965
   #endif
3960
   #endif
4199
   }
4194
   }
4200
 
4195
 
4201
   void mesh_probing_done() {
4196
   void mesh_probing_done() {
4202
-    mbl.set_has_mesh(true);
4197
+    mbl.has_mesh = true;
4203
     home_all_axes();
4198
     home_all_axes();
4204
     set_bed_leveling_enabled(true);
4199
     set_bed_leveling_enabled(true);
4205
     #if ENABLED(MESH_G28_REST_ORIGIN)
4200
     #if ENABLED(MESH_G28_REST_ORIGIN)
4249
     switch (state) {
4244
     switch (state) {
4250
       case MeshReport:
4245
       case MeshReport:
4251
         if (leveling_is_valid()) {
4246
         if (leveling_is_valid()) {
4252
-          SERIAL_PROTOCOLLNPAIR("State: ", LEVELING_IS_ACTIVE() ? MSG_ON : MSG_OFF);
4247
+          SERIAL_PROTOCOLLNPAIR("State: ", planner.leveling_active ? MSG_ON : MSG_OFF);
4253
           mbl_mesh_report();
4248
           mbl_mesh_report();
4254
         }
4249
         }
4255
         else
4250
         else
4568
         abl_probe_index = -1;
4563
         abl_probe_index = -1;
4569
       #endif
4564
       #endif
4570
 
4565
 
4571
-      abl_should_enable = LEVELING_IS_ACTIVE();
4566
+      abl_should_enable = planner.leveling_active;
4572
 
4567
 
4573
       #if ENABLED(AUTO_BED_LEVELING_BILINEAR)
4568
       #if ENABLED(AUTO_BED_LEVELING_BILINEAR)
4574
 
4569
 
4708
       stepper.synchronize();
4703
       stepper.synchronize();
4709
 
4704
 
4710
       // Disable auto bed leveling during G29
4705
       // Disable auto bed leveling during G29
4711
-      planner.abl_enabled = false;
4706
+      planner.leveling_active = false;
4712
 
4707
 
4713
       if (!dryrun) {
4708
       if (!dryrun) {
4714
         // Re-orient the current position without leveling
4709
         // Re-orient the current position without leveling
4722
       #if HAS_BED_PROBE
4717
       #if HAS_BED_PROBE
4723
         // Deploy the probe. Probe will raise if needed.
4718
         // Deploy the probe. Probe will raise if needed.
4724
         if (DEPLOY_PROBE()) {
4719
         if (DEPLOY_PROBE()) {
4725
-          planner.abl_enabled = abl_should_enable;
4720
+          planner.leveling_active = abl_should_enable;
4726
           return;
4721
           return;
4727
         }
4722
         }
4728
       #endif
4723
       #endif
4741
         ) {
4736
         ) {
4742
           if (dryrun) {
4737
           if (dryrun) {
4743
             // Before reset bed level, re-enable to correct the position
4738
             // Before reset bed level, re-enable to correct the position
4744
-            planner.abl_enabled = abl_should_enable;
4739
+            planner.leveling_active = abl_should_enable;
4745
           }
4740
           }
4746
           // Reset grid to 0.0 or "not probed". (Also disables ABL)
4741
           // Reset grid to 0.0 or "not probed". (Also disables ABL)
4747
           reset_bed_level();
4742
           reset_bed_level();
4786
         #if HAS_SOFTWARE_ENDSTOPS
4781
         #if HAS_SOFTWARE_ENDSTOPS
4787
           soft_endstops_enabled = enable_soft_endstops;
4782
           soft_endstops_enabled = enable_soft_endstops;
4788
         #endif
4783
         #endif
4789
-        planner.abl_enabled = abl_should_enable;
4784
+        planner.leveling_active = abl_should_enable;
4790
         g29_in_progress = false;
4785
         g29_in_progress = false;
4791
         #if ENABLED(LCD_BED_LEVELING)
4786
         #if ENABLED(LCD_BED_LEVELING)
4792
           lcd_wait_for_move = false;
4787
           lcd_wait_for_move = false;
4987
             measured_z = faux ? 0.001 * random(-100, 101) : probe_pt(xProbe, yProbe, stow_probe_after_each, verbose_level);
4982
             measured_z = faux ? 0.001 * random(-100, 101) : probe_pt(xProbe, yProbe, stow_probe_after_each, verbose_level);
4988
 
4983
 
4989
             if (isnan(measured_z)) {
4984
             if (isnan(measured_z)) {
4990
-              planner.abl_enabled = abl_should_enable;
4985
+              planner.leveling_active = abl_should_enable;
4991
               break;
4986
               break;
4992
             }
4987
             }
4993
 
4988
 
5023
           yProbe = LOGICAL_Y_POSITION(points[i].y);
5018
           yProbe = LOGICAL_Y_POSITION(points[i].y);
5024
           measured_z = faux ? 0.001 * random(-100, 101) : probe_pt(xProbe, yProbe, stow_probe_after_each, verbose_level);
5019
           measured_z = faux ? 0.001 * random(-100, 101) : probe_pt(xProbe, yProbe, stow_probe_after_each, verbose_level);
5025
           if (isnan(measured_z)) {
5020
           if (isnan(measured_z)) {
5026
-            planner.abl_enabled = abl_should_enable;
5021
+            planner.leveling_active = abl_should_enable;
5027
             break;
5022
             break;
5028
           }
5023
           }
5029
           points[i].z = measured_z;
5024
           points[i].z = measured_z;
5046
 
5041
 
5047
       // Raise to _Z_CLEARANCE_DEPLOY_PROBE. Stow the probe.
5042
       // Raise to _Z_CLEARANCE_DEPLOY_PROBE. Stow the probe.
5048
       if (STOW_PROBE()) {
5043
       if (STOW_PROBE()) {
5049
-        planner.abl_enabled = abl_should_enable;
5044
+        planner.leveling_active = abl_should_enable;
5050
         measured_z = NAN;
5045
         measured_z = NAN;
5051
       }
5046
       }
5052
     }
5047
     }
5214
           float converted[XYZ];
5209
           float converted[XYZ];
5215
           COPY(converted, current_position);
5210
           COPY(converted, current_position);
5216
 
5211
 
5217
-          planner.abl_enabled = true;
5212
+          planner.leveling_active = true;
5218
           planner.unapply_leveling(converted); // use conversion machinery
5213
           planner.unapply_leveling(converted); // use conversion machinery
5219
-          planner.abl_enabled = false;
5214
+          planner.leveling_active = false;
5220
 
5215
 
5221
           // Use the last measured distance to the bed, if possible
5216
           // Use the last measured distance to the bed, if possible
5222
           if ( NEAR(current_position[X_AXIS], xProbe - (X_PROBE_OFFSET_FROM_EXTRUDER))
5217
           if ( NEAR(current_position[X_AXIS], xProbe - (X_PROBE_OFFSET_FROM_EXTRUDER))
5268
       #endif
5263
       #endif
5269
 
5264
 
5270
       // Auto Bed Leveling is complete! Enable if possible.
5265
       // Auto Bed Leveling is complete! Enable if possible.
5271
-      planner.abl_enabled = dryrun ? abl_should_enable : true;
5266
+      planner.leveling_active = dryrun ? abl_should_enable : true;
5272
     } // !isnan(measured_z)
5267
     } // !isnan(measured_z)
5273
 
5268
 
5274
     // Restore state after probing
5269
     // Restore state after probing
5282
 
5277
 
5283
     KEEPALIVE_STATE(IN_HANDLER);
5278
     KEEPALIVE_STATE(IN_HANDLER);
5284
 
5279
 
5285
-    if (planner.abl_enabled)
5280
+    if (planner.leveling_active)
5286
       SYNC_PLAN_POSITION_KINEMATIC();
5281
       SYNC_PLAN_POSITION_KINEMATIC();
5287
   }
5282
   }
5288
 
5283
 
7065
     // Disable bed level correction in M48 because we want the raw data when we probe
7060
     // Disable bed level correction in M48 because we want the raw data when we probe
7066
 
7061
 
7067
     #if HAS_LEVELING
7062
     #if HAS_LEVELING
7068
-      const bool was_enabled = LEVELING_IS_ACTIVE();
7063
+      const bool was_enabled = planner.leveling_active;
7069
       set_bed_leveling_enabled(false);
7064
       set_bed_leveling_enabled(false);
7070
     #endif
7065
     #endif
7071
 
7066
 
9401
       if (parser.seen('Z')) set_z_fade_height(parser.value_linear_units());
9396
       if (parser.seen('Z')) set_z_fade_height(parser.value_linear_units());
9402
     #endif
9397
     #endif
9403
 
9398
 
9404
-    const bool new_status = LEVELING_IS_ACTIVE();
9399
+    const bool new_status = planner.leveling_active;
9405
 
9400
 
9406
     if (to_enable && !new_status) {
9401
     if (to_enable && !new_status) {
9407
       SERIAL_ERROR_START();
9402
       SERIAL_ERROR_START();
9632
       #endif
9627
       #endif
9633
 
9628
 
9634
       #if ENABLED(BABYSTEP_ZPROBE_OFFSET)
9629
       #if ENABLED(BABYSTEP_ZPROBE_OFFSET)
9635
-        if (!no_babystep && LEVELING_IS_ACTIVE())
9630
+        if (!no_babystep && planner.leveling_active)
9636
           thermalManager.babystep_axis(Z_AXIS, -LROUND(diff * planner.axis_steps_per_mm[Z_AXIS]));
9631
           thermalManager.babystep_axis(Z_AXIS, -LROUND(diff * planner.axis_steps_per_mm[Z_AXIS]));
9637
       #else
9632
       #else
9638
         UNUSED(no_babystep);
9633
         UNUSED(no_babystep);
10679
 
10674
 
10680
             #if ENABLED(MESH_BED_LEVELING)
10675
             #if ENABLED(MESH_BED_LEVELING)
10681
 
10676
 
10682
-              if (LEVELING_IS_ACTIVE()) {
10677
+              if (planner.leveling_active) {
10683
                 #if ENABLED(DEBUG_LEVELING_FEATURE)
10678
                 #if ENABLED(DEBUG_LEVELING_FEATURE)
10684
                   if (DEBUGGING(LEVELING)) SERIAL_ECHOPAIR("Z before MBL: ", current_position[Z_AXIS]);
10679
                   if (DEBUGGING(LEVELING)) SERIAL_ECHOPAIR("Z before MBL: ", current_position[Z_AXIS]);
10685
                 #endif
10680
                 #endif
12377
    * Prepare a linear move in a Cartesian setup.
12372
    * Prepare a linear move in a Cartesian setup.
12378
    * If Mesh Bed Leveling is enabled, perform a mesh move.
12373
    * If Mesh Bed Leveling is enabled, perform a mesh move.
12379
    *
12374
    *
12380
-   * Returns true if the caller didn't update current_position.
12375
+   * Returns true if current_position[] was set to destination[]
12381
    */
12376
    */
12382
   inline bool prepare_move_to_destination_cartesian() {
12377
   inline bool prepare_move_to_destination_cartesian() {
12383
-    #if ENABLED(AUTO_BED_LEVELING_UBL)
12378
+    if (current_position[X_AXIS] != destination[X_AXIS] || current_position[Y_AXIS] != destination[Y_AXIS]) {
12384
       const float fr_scaled = MMS_SCALED(feedrate_mm_s);
12379
       const float fr_scaled = MMS_SCALED(feedrate_mm_s);
12385
-      if (ubl.state.active) { // direct use of ubl.state.active for speed
12386
-        ubl.line_to_destination_cartesian(fr_scaled, active_extruder);
12387
-        return true;
12388
-      }
12389
-      else
12390
-        line_to_destination(fr_scaled);
12391
-    #else
12392
-      // Do not use feedrate_percentage for E or Z only moves
12393
-      if (current_position[X_AXIS] == destination[X_AXIS] && current_position[Y_AXIS] == destination[Y_AXIS])
12394
-        line_to_destination();
12395
-      else {
12396
-        const float fr_scaled = MMS_SCALED(feedrate_mm_s);
12397
-        #if ENABLED(MESH_BED_LEVELING)
12398
-          if (mbl.active()) { // direct used of mbl.active() for speed
12380
+      #if HAS_LEVELING
12381
+        if (planner.leveling_active) {
12382
+          #if ENABLED(AUTO_BED_LEVELING_UBL)
12383
+            ubl.line_to_destination_cartesian(fr_scaled, active_extruder);
12384
+          #elif ENABLED(MESH_BED_LEVELING)
12399
             mesh_line_to_destination(fr_scaled);
12385
             mesh_line_to_destination(fr_scaled);
12400
-            return true;
12401
-          }
12402
-          else
12403
-        #elif ENABLED(AUTO_BED_LEVELING_BILINEAR)
12404
-          if (planner.abl_enabled) { // direct use of abl_enabled for speed
12386
+          #elif ENABLED(AUTO_BED_LEVELING_BILINEAR)
12405
             bilinear_line_to_destination(fr_scaled);
12387
             bilinear_line_to_destination(fr_scaled);
12406
-            return true;
12407
-          }
12408
-          else
12409
-        #endif
12410
-            line_to_destination(fr_scaled);
12411
-      }
12412
-    #endif
12388
+          #endif
12389
+          return true;
12390
+        }
12391
+      #endif // HAS_LEVELING
12392
+      line_to_destination(fr_scaled);
12393
+    }
12394
+    else
12395
+      line_to_destination();
12396
+
12413
     return false;
12397
     return false;
12414
   }
12398
   }
12415
 
12399
 

+ 63
- 53
Marlin/configuration_store.cpp View File

68
  *  219            z_fade_height                    (float)
68
  *  219            z_fade_height                    (float)
69
  *
69
  *
70
  * MESH_BED_LEVELING:                               43 bytes
70
  * MESH_BED_LEVELING:                               43 bytes
71
- *  223  M420 S    from mbl.status                  (bool)
71
+ *  223  M420 S    planner.leveling_active          (bool)
72
  *  224            mbl.z_offset                     (float)
72
  *  224            mbl.z_offset                     (float)
73
  *  228            GRID_MAX_POINTS_X                (uint8_t)
73
  *  228            GRID_MAX_POINTS_X                (uint8_t)
74
  *  229            GRID_MAX_POINTS_Y                (uint8_t)
74
  *  229            GRID_MAX_POINTS_Y                (uint8_t)
88
  *  316            z_values[][]                     (float x9, up to float x256) +988
88
  *  316            z_values[][]                     (float x9, up to float x256) +988
89
  *
89
  *
90
  * AUTO_BED_LEVELING_UBL:                           6 bytes
90
  * AUTO_BED_LEVELING_UBL:                           6 bytes
91
- *  324  G29 A     ubl.state.active                 (bool)
91
+ *  324  G29 A     planner.leveling_active          (bool)
92
  *  325  G29 S     ubl.state.storage_slot           (int8_t)
92
  *  325  G29 S     ubl.state.storage_slot           (int8_t)
93
  *
93
  *
94
  * DELTA:                                           48 bytes
94
  * DELTA:                                           48 bytes
204
   extern void refresh_bed_level();
204
   extern void refresh_bed_level();
205
 #endif
205
 #endif
206
 
206
 
207
+#if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
208
+  float new_z_fade_height;
209
+#endif
210
+
207
 /**
211
 /**
208
  * Post-process after Retrieve or Reset
212
  * Post-process after Retrieve or Reset
209
  */
213
  */
233
   #endif
237
   #endif
234
 
238
 
235
   #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
239
   #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
236
-    set_z_fade_height(planner.z_fade_height);
240
+    set_z_fade_height(new_z_fade_height);
237
   #endif
241
   #endif
238
 
242
 
239
   #if HAS_BED_PROBE
243
   #if HAS_BED_PROBE
372
         sizeof(mbl.z_values) == GRID_MAX_POINTS * sizeof(mbl.z_values[0][0]),
376
         sizeof(mbl.z_values) == GRID_MAX_POINTS * sizeof(mbl.z_values[0][0]),
373
         "MBL Z array is the wrong size."
377
         "MBL Z array is the wrong size."
374
       );
378
       );
375
-      const bool leveling_is_on = TEST(mbl.status, MBL_STATUS_HAS_MESH_BIT);
379
+      const bool leveling_is_on = mbl.has_mesh;
376
       const uint8_t mesh_num_x = GRID_MAX_POINTS_X, mesh_num_y = GRID_MAX_POINTS_Y;
380
       const uint8_t mesh_num_x = GRID_MAX_POINTS_X, mesh_num_y = GRID_MAX_POINTS_Y;
377
       EEPROM_WRITE(leveling_is_on);
381
       EEPROM_WRITE(leveling_is_on);
378
       EEPROM_WRITE(mbl.z_offset);
382
       EEPROM_WRITE(mbl.z_offset);
435
     #endif // AUTO_BED_LEVELING_BILINEAR
439
     #endif // AUTO_BED_LEVELING_BILINEAR
436
 
440
 
437
     #if ENABLED(AUTO_BED_LEVELING_UBL)
441
     #if ENABLED(AUTO_BED_LEVELING_UBL)
438
-      EEPROM_WRITE(ubl.state.active);
442
+      EEPROM_WRITE(planner.leveling_active);
439
       EEPROM_WRITE(ubl.state.storage_slot);
443
       EEPROM_WRITE(ubl.state.storage_slot);
440
     #else
444
     #else
441
       const bool ubl_active = false;
445
       const bool ubl_active = false;
751
       //
755
       //
752
 
756
 
753
       #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
757
       #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
754
-        EEPROM_READ(planner.z_fade_height);
758
+        EEPROM_READ(new_z_fade_height);
755
       #else
759
       #else
756
         EEPROM_READ(dummy);
760
         EEPROM_READ(dummy);
757
       #endif
761
       #endif
768
       EEPROM_READ(mesh_num_y);
772
       EEPROM_READ(mesh_num_y);
769
 
773
 
770
       #if ENABLED(MESH_BED_LEVELING)
774
       #if ENABLED(MESH_BED_LEVELING)
771
-        mbl.status = leveling_is_on ? _BV(MBL_STATUS_HAS_MESH_BIT) : 0;
775
+        mbl.has_mesh = leveling_is_on;
772
         mbl.z_offset = dummy;
776
         mbl.z_offset = dummy;
773
         if (mesh_num_x == GRID_MAX_POINTS_X && mesh_num_y == GRID_MAX_POINTS_Y) {
777
         if (mesh_num_x == GRID_MAX_POINTS_X && mesh_num_y == GRID_MAX_POINTS_Y) {
774
           // EEPROM data fits the current mesh
778
           // EEPROM data fits the current mesh
824
         }
828
         }
825
 
829
 
826
       #if ENABLED(AUTO_BED_LEVELING_UBL)
830
       #if ENABLED(AUTO_BED_LEVELING_UBL)
827
-        EEPROM_READ(ubl.state.active);
831
+        EEPROM_READ(planner.leveling_active);
828
         EEPROM_READ(ubl.state.storage_slot);
832
         EEPROM_READ(ubl.state.storage_slot);
829
       #else
833
       #else
830
         uint8_t dummyui8;
834
         uint8_t dummyui8;
1186
   planner.max_jerk[E_AXIS] = DEFAULT_EJERK;
1190
   planner.max_jerk[E_AXIS] = DEFAULT_EJERK;
1187
 
1191
 
1188
   #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
1192
   #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
1189
-    planner.z_fade_height = 0.0;
1193
+    new_z_fade_height = 10.0;
1190
   #endif
1194
   #endif
1191
 
1195
 
1192
   #if HAS_HOME_OFFSET
1196
   #if HAS_HOME_OFFSET
1563
       }
1567
       }
1564
     #endif
1568
     #endif
1565
 
1569
 
1566
-    #if ENABLED(MESH_BED_LEVELING)
1570
+    /**
1571
+     * Bed Leveling
1572
+     */
1573
+    #if HAS_LEVELING
1567
 
1574
 
1568
-      if (!forReplay) {
1575
+      #if ENABLED(MESH_BED_LEVELING)
1576
+
1577
+        if (!forReplay) {
1578
+          CONFIG_ECHO_START;
1579
+          SERIAL_ECHOLNPGM("Mesh Bed Leveling:");
1580
+        }
1569
         CONFIG_ECHO_START;
1581
         CONFIG_ECHO_START;
1570
-        SERIAL_ECHOLNPGM("Mesh Bed Leveling:");
1571
-      }
1572
-      CONFIG_ECHO_START;
1573
-      SERIAL_ECHOPAIR("  M420 S", leveling_is_valid() ? 1 : 0);
1574
-      #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
1575
-        SERIAL_ECHOPAIR(" Z", LINEAR_UNIT(planner.z_fade_height));
1576
-      #endif
1577
-      SERIAL_EOL();
1578
-      for (uint8_t py = 0; py < GRID_MAX_POINTS_Y; py++) {
1579
-        for (uint8_t px = 0; px < GRID_MAX_POINTS_X; px++) {
1582
+
1583
+      #elif ENABLED(AUTO_BED_LEVELING_UBL)
1584
+
1585
+        if (!forReplay) {
1580
           CONFIG_ECHO_START;
1586
           CONFIG_ECHO_START;
1581
-          SERIAL_ECHOPAIR("  G29 S3 X", (int)px + 1);
1582
-          SERIAL_ECHOPAIR(" Y", (int)py + 1);
1583
-          SERIAL_ECHOPGM(" Z");
1584
-          SERIAL_PROTOCOL_F(LINEAR_UNIT(mbl.z_values[px][py]), 5);
1585
-          SERIAL_EOL();
1587
+          ubl.echo_name();
1588
+          SERIAL_ECHOLNPGM(":");
1586
         }
1589
         }
1587
-      }
1590
+        CONFIG_ECHO_START;
1588
 
1591
 
1589
-    #elif ENABLED(AUTO_BED_LEVELING_UBL)
1592
+      #elif HAS_ABL
1590
 
1593
 
1591
-      if (!forReplay) {
1594
+        if (!forReplay) {
1595
+          CONFIG_ECHO_START;
1596
+          SERIAL_ECHOLNPGM("Auto Bed Leveling:");
1597
+        }
1592
         CONFIG_ECHO_START;
1598
         CONFIG_ECHO_START;
1593
-        ubl.echo_name();
1594
-        SERIAL_ECHOLNPGM(":");
1595
-      }
1599
+
1600
+      #endif
1601
+
1596
       CONFIG_ECHO_START;
1602
       CONFIG_ECHO_START;
1597
-      SERIAL_ECHOPAIR("  M420 S", LEVELING_IS_ACTIVE() ? 1 : 0);
1603
+      SERIAL_ECHOPAIR("  M420 S", planner.leveling_active ? 1 : 0);
1598
       #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
1604
       #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
1599
-        SERIAL_ECHOPAIR(" Z", planner.z_fade_height);
1605
+        SERIAL_ECHOPAIR(" Z", LINEAR_UNIT(planner.z_fade_height));
1600
       #endif
1606
       #endif
1601
       SERIAL_EOL();
1607
       SERIAL_EOL();
1602
 
1608
 
1603
-      if (!forReplay) {
1604
-        SERIAL_EOL();
1605
-        ubl.report_state();
1606
-        SERIAL_ECHOLNPAIR("\nActive Mesh Slot: ", ubl.state.storage_slot);
1607
-        SERIAL_ECHOPAIR("EEPROM can hold ", calc_num_meshes());
1608
-        SERIAL_ECHOLNPGM(" meshes.\n");
1609
-      }
1609
+      #if ENABLED(MESH_BED_LEVELING)
1610
 
1610
 
1611
-    #elif HAS_ABL
1611
+        for (uint8_t py = 0; py < GRID_MAX_POINTS_Y; py++) {
1612
+          for (uint8_t px = 0; px < GRID_MAX_POINTS_X; px++) {
1613
+            CONFIG_ECHO_START;
1614
+            SERIAL_ECHOPAIR("  G29 S3 X", (int)px + 1);
1615
+            SERIAL_ECHOPAIR(" Y", (int)py + 1);
1616
+            SERIAL_ECHOPGM(" Z");
1617
+            SERIAL_PROTOCOL_F(LINEAR_UNIT(mbl.z_values[px][py]), 5);
1618
+            SERIAL_EOL();
1619
+          }
1620
+        }
1621
+
1622
+      #elif ENABLED(AUTO_BED_LEVELING_UBL)
1623
+
1624
+        if (!forReplay) {
1625
+          SERIAL_EOL();
1626
+          ubl.report_state();
1627
+          SERIAL_ECHOLNPAIR("\nActive Mesh Slot: ", ubl.state.storage_slot);
1628
+          SERIAL_ECHOPAIR("EEPROM can hold ", calc_num_meshes());
1629
+          SERIAL_ECHOLNPGM(" meshes.\n");
1630
+        }
1612
 
1631
 
1613
-      if (!forReplay) {
1614
-        CONFIG_ECHO_START;
1615
-        SERIAL_ECHOLNPGM("Auto Bed Leveling:");
1616
-      }
1617
-      CONFIG_ECHO_START;
1618
-      SERIAL_ECHOPAIR("  M420 S", LEVELING_IS_ACTIVE() ? 1 : 0);
1619
-      #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
1620
-        SERIAL_ECHOPAIR(" Z", LINEAR_UNIT(planner.z_fade_height));
1621
       #endif
1632
       #endif
1622
-      SERIAL_EOL();
1623
 
1633
 
1624
-    #endif
1634
+    #endif // HAS_LEVELING
1625
 
1635
 
1626
     #if ENABLED(DELTA)
1636
     #if ENABLED(DELTA)
1627
       if (!forReplay) {
1637
       if (!forReplay) {
1757
     #endif // FWRETRACT
1767
     #endif // FWRETRACT
1758
 
1768
 
1759
     /**
1769
     /**
1760
-     * Auto Bed Leveling
1770
+     * Probe Offset
1761
      */
1771
      */
1762
     #if HAS_BED_PROBE
1772
     #if HAS_BED_PROBE
1763
       if (!forReplay) {
1773
       if (!forReplay) {

+ 2
- 2
Marlin/mesh_bed_leveling.cpp View File

26
 
26
 
27
   mesh_bed_leveling mbl;
27
   mesh_bed_leveling mbl;
28
 
28
 
29
-  uint8_t mesh_bed_leveling::status;
29
+  bool mesh_bed_leveling::has_mesh;
30
 
30
 
31
   float mesh_bed_leveling::z_offset,
31
   float mesh_bed_leveling::z_offset,
32
         mesh_bed_leveling::z_values[GRID_MAX_POINTS_X][GRID_MAX_POINTS_Y],
32
         mesh_bed_leveling::z_values[GRID_MAX_POINTS_X][GRID_MAX_POINTS_Y],
42
   }
42
   }
43
 
43
 
44
   void mesh_bed_leveling::reset() {
44
   void mesh_bed_leveling::reset() {
45
-    status = MBL_STATUS_NONE;
45
+    has_mesh = false;
46
     z_offset = 0;
46
     z_offset = 0;
47
     ZERO(z_values);
47
     ZERO(z_values);
48
   }
48
   }

+ 1
- 12
Marlin/mesh_bed_leveling.h View File

33
     MeshReset
33
     MeshReset
34
   };
34
   };
35
 
35
 
36
-  enum MBLStatus {
37
-    MBL_STATUS_NONE = 0,
38
-    MBL_STATUS_HAS_MESH_BIT = 0,
39
-    MBL_STATUS_ACTIVE_BIT = 1
40
-  };
41
-
42
   #define MESH_X_DIST ((MESH_MAX_X - (MESH_MIN_X)) / (GRID_MAX_POINTS_X - 1))
36
   #define MESH_X_DIST ((MESH_MAX_X - (MESH_MIN_X)) / (GRID_MAX_POINTS_X - 1))
43
   #define MESH_Y_DIST ((MESH_MAX_Y - (MESH_MIN_Y)) / (GRID_MAX_POINTS_Y - 1))
37
   #define MESH_Y_DIST ((MESH_MAX_Y - (MESH_MIN_Y)) / (GRID_MAX_POINTS_Y - 1))
44
 
38
 
45
   class mesh_bed_leveling {
39
   class mesh_bed_leveling {
46
   public:
40
   public:
47
-    static uint8_t status; // Has Mesh and Is Active bits
41
+    static bool has_mesh;
48
     static float z_offset,
42
     static float z_offset,
49
                  z_values[GRID_MAX_POINTS_X][GRID_MAX_POINTS_Y],
43
                  z_values[GRID_MAX_POINTS_X][GRID_MAX_POINTS_Y],
50
                  index_to_xpos[GRID_MAX_POINTS_X],
44
                  index_to_xpos[GRID_MAX_POINTS_X],
56
 
50
 
57
     static void set_z(const int8_t px, const int8_t py, const float &z) { z_values[px][py] = z; }
51
     static void set_z(const int8_t px, const int8_t py, const float &z) { z_values[px][py] = z; }
58
 
52
 
59
-    static bool active()                       { return TEST(status, MBL_STATUS_ACTIVE_BIT); }
60
-    static void set_active(const bool onOff)   { onOff ? SBI(status, MBL_STATUS_ACTIVE_BIT) : CBI(status, MBL_STATUS_ACTIVE_BIT); }
61
-    static bool has_mesh()                     { return TEST(status, MBL_STATUS_HAS_MESH_BIT); }
62
-    static void set_has_mesh(const bool onOff) { onOff ? SBI(status, MBL_STATUS_HAS_MESH_BIT) : CBI(status, MBL_STATUS_HAS_MESH_BIT); }
63
-
64
     static inline void zigzag(const int8_t index, int8_t &px, int8_t &py) {
53
     static inline void zigzag(const int8_t index, int8_t &px, int8_t &py) {
65
       px = index % (GRID_MAX_POINTS_X);
54
       px = index % (GRID_MAX_POINTS_X);
66
       py = index / (GRID_MAX_POINTS_X);
55
       py = index / (GRID_MAX_POINTS_X);

+ 58
- 81
Marlin/planner.cpp View File

104
       Planner::max_jerk[XYZE],       // The largest speed change requiring no acceleration
104
       Planner::max_jerk[XYZE],       // The largest speed change requiring no acceleration
105
       Planner::min_travel_feedrate_mm_s;
105
       Planner::min_travel_feedrate_mm_s;
106
 
106
 
107
-#if HAS_ABL
108
-  bool Planner::abl_enabled = false; // Flag that auto bed leveling is enabled
109
-#endif
110
-
111
-#if ABL_PLANAR
112
-  matrix_3x3 Planner::bed_level_matrix; // Transform to compensate for bed level
113
-#endif
114
-
115
-#if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
116
-  float Planner::z_fade_height, // Initialized by settings.load()
117
-        Planner::inverse_z_fade_height;
107
+#if HAS_LEVELING
108
+  bool Planner::leveling_active = false; // Flag that auto bed leveling is enabled
109
+  #if ABL_PLANAR
110
+    matrix_3x3 Planner::bed_level_matrix; // Transform to compensate for bed level
111
+  #endif
112
+  #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
113
+    float Planner::z_fade_height,      // Initialized by settings.load()
114
+          Planner::inverse_z_fade_height,
115
+          Planner::last_raw_lz;
116
+  #endif
118
 #endif
117
 #endif
119
 
118
 
120
 #if ENABLED(AUTOTEMP)
119
 #if ENABLED(AUTOTEMP)
528
    */
527
    */
529
   void Planner::apply_leveling(float &lx, float &ly, float &lz) {
528
   void Planner::apply_leveling(float &lx, float &ly, float &lz) {
530
 
529
 
531
-    #if ENABLED(AUTO_BED_LEVELING_UBL)
532
-      if (!ubl.state.active) return;
533
-      #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
534
-        // if z_fade_height enabled (nonzero) and raw_z above it, no leveling required
535
-        if (planner.z_fade_height && planner.z_fade_height <= RAW_Z_POSITION(lz)) return;
536
-        lz += ubl.get_z_correction(lx, ly) * ubl.fade_scaling_factor_for_z(lz);
537
-      #else // no fade
538
-        lz += ubl.get_z_correction(lx, ly);
539
-      #endif // FADE
540
-    #endif // UBL
541
-
542
-    #if HAS_ABL
543
-      if (!abl_enabled) return;
544
-    #endif
530
+    if (!leveling_active) return;
545
 
531
 
546
-    #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT) && DISABLED(AUTO_BED_LEVELING_UBL)
547
-      static float z_fade_factor = 1.0, last_raw_lz = -999.0;
548
-      if (z_fade_height) {
549
-        const float raw_lz = RAW_Z_POSITION(lz);
550
-        if (raw_lz >= z_fade_height) return;
551
-        if (last_raw_lz != raw_lz) {
552
-          last_raw_lz = raw_lz;
553
-          z_fade_factor = 1.0 - raw_lz * inverse_z_fade_height;
554
-        }
555
-      }
556
-      else
557
-        z_fade_factor = 1.0;
532
+    #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
533
+      const float fade_scaling_factor = fade_scaling_factor_for_z(lz);
534
+      if (!fade_scaling_factor) return;
535
+    #else
536
+      constexpr float fade_scaling_factor = 1.0;
558
     #endif
537
     #endif
559
 
538
 
560
-    #if ENABLED(MESH_BED_LEVELING)
539
+    #if ENABLED(AUTO_BED_LEVELING_UBL)
561
 
540
 
562
-      if (mbl.active())
563
-        lz += mbl.get_z(RAW_X_POSITION(lx), RAW_Y_POSITION(ly)
564
-          #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
565
-            , z_fade_factor
566
-          #endif
567
-          );
541
+      lz += ubl.get_z_correction(lx, ly) * fade_scaling_factor;
542
+
543
+    #elif ENABLED(MESH_BED_LEVELING)
544
+
545
+      lz += mbl.get_z(RAW_X_POSITION(lx), RAW_Y_POSITION(ly)
546
+        #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
547
+          , fade_scaling_factor
548
+        #endif
549
+      );
568
 
550
 
569
     #elif ABL_PLANAR
551
     #elif ABL_PLANAR
570
 
552
 
553
+      UNUSED(fade_scaling_factor);
554
+
571
       float dx = RAW_X_POSITION(lx) - (X_TILT_FULCRUM),
555
       float dx = RAW_X_POSITION(lx) - (X_TILT_FULCRUM),
572
             dy = RAW_Y_POSITION(ly) - (Y_TILT_FULCRUM),
556
             dy = RAW_Y_POSITION(ly) - (Y_TILT_FULCRUM),
573
             dz = RAW_Z_POSITION(lz);
557
             dz = RAW_Z_POSITION(lz);
581
     #elif ENABLED(AUTO_BED_LEVELING_BILINEAR)
565
     #elif ENABLED(AUTO_BED_LEVELING_BILINEAR)
582
 
566
 
583
       float tmp[XYZ] = { lx, ly, 0 };
567
       float tmp[XYZ] = { lx, ly, 0 };
584
-      lz += bilinear_z_offset(tmp)
585
-        #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
586
-          * z_fade_factor
587
-        #endif
588
-      ;
568
+      lz += bilinear_z_offset(tmp) * fade_scaling_factor;
589
 
569
 
590
     #endif
570
     #endif
591
   }
571
   }
592
 
572
 
593
   void Planner::unapply_leveling(float logical[XYZ]) {
573
   void Planner::unapply_leveling(float logical[XYZ]) {
594
 
574
 
595
-    #if ENABLED(AUTO_BED_LEVELING_UBL)
575
+    #if HAS_LEVELING
576
+      if (!leveling_active) return;
577
+    #endif
596
 
578
 
597
-      if (ubl.state.active) {
579
+    #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
580
+      if (!leveling_active_at_z(logical[Z_AXIS])) return;
581
+    #endif
598
 
582
 
599
-        const float z_physical = RAW_Z_POSITION(logical[Z_AXIS]),
600
-                    z_correct = ubl.get_z_correction(logical[X_AXIS], logical[Y_AXIS]),
601
-                    z_virtual = z_physical - z_correct;
602
-              float z_logical = LOGICAL_Z_POSITION(z_virtual);
583
+    #if ENABLED(AUTO_BED_LEVELING_UBL)
603
 
584
 
604
-        #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
585
+      const float z_physical = RAW_Z_POSITION(logical[Z_AXIS]),
586
+                  z_correct = ubl.get_z_correction(logical[X_AXIS], logical[Y_AXIS]),
587
+                  z_virtual = z_physical - z_correct;
588
+            float z_logical = LOGICAL_Z_POSITION(z_virtual);
605
 
589
 
606
-          // for P=physical_z, L=logical_z, M=mesh_z, H=fade_height,
607
-          // Given P=L+M(1-L/H) (faded mesh correction formula for L<H)
608
-          //  then L=P-M(1-L/H)
609
-          //    so L=P-M+ML/H
610
-          //    so L-ML/H=P-M
611
-          //    so L(1-M/H)=P-M
612
-          //    so L=(P-M)/(1-M/H) for L<H
590
+      #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
613
 
591
 
614
-          if (planner.z_fade_height) {
615
-            if (z_logical >= planner.z_fade_height)
616
-              z_logical = LOGICAL_Z_POSITION(z_physical);
617
-            else
618
-              z_logical /= 1.0 - z_correct * planner.inverse_z_fade_height;
619
-          }
592
+        // for P=physical_z, L=logical_z, M=mesh_z, H=fade_height,
593
+        // Given P=L+M(1-L/H) (faded mesh correction formula for L<H)
594
+        //  then L=P-M(1-L/H)
595
+        //    so L=P-M+ML/H
596
+        //    so L-ML/H=P-M
597
+        //    so L(1-M/H)=P-M
598
+        //    so L=(P-M)/(1-M/H) for L<H
599
+
600
+        if (planner.z_fade_height) {
601
+          if (z_logical >= planner.z_fade_height)
602
+            z_logical = LOGICAL_Z_POSITION(z_physical);
603
+          else
604
+            z_logical /= 1.0 - z_correct * planner.inverse_z_fade_height;
605
+        }
620
 
606
 
621
-        #endif // ENABLE_LEVELING_FADE_HEIGHT
607
+      #endif // ENABLE_LEVELING_FADE_HEIGHT
622
 
608
 
623
-        logical[Z_AXIS] = z_logical;
624
-      }
609
+      logical[Z_AXIS] = z_logical;
625
 
610
 
626
       return; // don't fall thru to other ENABLE_LEVELING_FADE_HEIGHT logic
611
       return; // don't fall thru to other ENABLE_LEVELING_FADE_HEIGHT logic
627
 
612
 
628
     #endif
613
     #endif
629
 
614
 
630
-    #if HAS_ABL
631
-      if (!abl_enabled) return;
632
-    #endif
633
-
634
-    #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
635
-      if (z_fade_height && RAW_Z_POSITION(logical[Z_AXIS]) >= z_fade_height) return;
636
-    #endif
637
-
638
     #if ENABLED(MESH_BED_LEVELING)
615
     #if ENABLED(MESH_BED_LEVELING)
639
 
616
 
640
-      if (mbl.active()) {
617
+      if (leveling_active) {
641
         #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
618
         #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
642
           const float c = mbl.get_z(RAW_X_POSITION(logical[X_AXIS]), RAW_Y_POSITION(logical[Y_AXIS]), 1.0);
619
           const float c = mbl.get_z(RAW_X_POSITION(logical[X_AXIS]), RAW_Y_POSITION(logical[Y_AXIS]), 1.0);
643
           logical[Z_AXIS] = (z_fade_height * (RAW_Z_POSITION(logical[Z_AXIS]) - c)) / (z_fade_height - c);
620
           logical[Z_AXIS] = (z_fade_height * (RAW_Z_POSITION(logical[Z_AXIS]) - c)) / (z_fade_height - c);

+ 55
- 16
Marlin/planner.h View File

154
                  max_jerk[XYZE],       // The largest speed change requiring no acceleration
154
                  max_jerk[XYZE],       // The largest speed change requiring no acceleration
155
                  min_travel_feedrate_mm_s;
155
                  min_travel_feedrate_mm_s;
156
 
156
 
157
-    #if HAS_ABL
158
-      static bool abl_enabled;              // Flag that bed leveling is enabled
157
+    #if HAS_LEVELING
158
+      static bool leveling_active;          // Flag that bed leveling is enabled
159
       #if ABL_PLANAR
159
       #if ABL_PLANAR
160
         static matrix_3x3 bed_level_matrix; // Transform to compensate for bed level
160
         static matrix_3x3 bed_level_matrix; // Transform to compensate for bed level
161
       #endif
161
       #endif
162
-    #endif
163
-
164
-    #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
165
-      static float z_fade_height, inverse_z_fade_height;
162
+      #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
163
+        static float z_fade_height, inverse_z_fade_height;
164
+      #endif
166
     #endif
165
     #endif
167
 
166
 
168
     #if ENABLED(LIN_ADVANCE)
167
     #if ENABLED(LIN_ADVANCE)
192
      */
191
      */
193
     static uint32_t cutoff_long;
192
     static uint32_t cutoff_long;
194
 
193
 
194
+    #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
195
+      static float last_raw_lz;
196
+    #endif
197
+
195
     #if ENABLED(DISABLE_INACTIVE_EXTRUDER)
198
     #if ENABLED(DISABLE_INACTIVE_EXTRUDER)
196
       /**
199
       /**
197
        * Counters to manage disabling inactive extruders
200
        * Counters to manage disabling inactive extruders
243
 
246
 
244
     static bool is_full() { return (block_buffer_tail == BLOCK_MOD(block_buffer_head + 1)); }
247
     static bool is_full() { return (block_buffer_tail == BLOCK_MOD(block_buffer_head + 1)); }
245
 
248
 
249
+    #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
250
+
251
+      /**
252
+       * Get the Z leveling fade factor based on the given Z height,
253
+       * re-calculating only when needed.
254
+       *
255
+       *  Returns 1.0 if planner.z_fade_height is 0.0.
256
+       *  Returns 0.0 if Z is past the specified 'Fade Height'.
257
+       */
258
+      inline static float fade_scaling_factor_for_z(const float &lz) {
259
+        static float z_fade_factor = 1.0;
260
+        if (z_fade_height) {
261
+          const float raw_lz = RAW_Z_POSITION(lz);
262
+          if (raw_lz >= z_fade_height) return 0.0;
263
+          if (last_raw_lz != raw_lz) {
264
+            last_raw_lz = raw_lz;
265
+            z_fade_factor = 1.0 - raw_lz * inverse_z_fade_height;
266
+          }
267
+          return z_fade_factor;
268
+        }
269
+        return 1.0;
270
+      }
271
+
272
+      FORCE_INLINE static void force_fade_recalc() { last_raw_lz = -999.999; }
273
+
274
+      FORCE_INLINE static void set_z_fade_height(const float &zfh) {
275
+        z_fade_height = zfh > 0 ? zfh : 0;
276
+        inverse_z_fade_height = RECIPROCAL(z_fade_height);
277
+        force_fade_recalc();
278
+      }
279
+
280
+      FORCE_INLINE static bool leveling_active_at_z(const float &lz) {
281
+        return !z_fade_height || RAW_Z_POSITION(lz) < z_fade_height;
282
+      }
283
+
284
+    #else
285
+
286
+      FORCE_INLINE static float fade_scaling_factor_for_z(const float &lz) {
287
+        UNUSED(lz);
288
+        return 1.0;
289
+      }
290
+
291
+      FORCE_INLINE static bool leveling_active_at_z(const float &lz) { return true; }
292
+
293
+    #endif
294
+
246
     #if PLANNER_LEVELING
295
     #if PLANNER_LEVELING
247
 
296
 
248
       #define ARG_X float lx
297
       #define ARG_X float lx
468
 
517
 
469
 #define PLANNER_XY_FEEDRATE() (min(planner.max_feedrate_mm_s[X_AXIS], planner.max_feedrate_mm_s[Y_AXIS]))
518
 #define PLANNER_XY_FEEDRATE() (min(planner.max_feedrate_mm_s[X_AXIS], planner.max_feedrate_mm_s[Y_AXIS]))
470
 
519
 
471
-#if ENABLED(MESH_BED_LEVELING)
472
-  #define LEVELING_IS_ACTIVE() (mesh_bed_leveling::active())
473
-#elif ENABLED(AUTO_BED_LEVELING_UBL)
474
-  #define LEVELING_IS_ACTIVE() (unified_bed_leveling::state.active)
475
-#elif HAS_ABL
476
-  #define LEVELING_IS_ACTIVE() (Planner::abl_enabled)
477
-#else
478
-  #define LEVELING_IS_ACTIVE() (false)
479
-#endif
480
-
481
 extern Planner planner;
520
 extern Planner planner;
482
 
521
 
483
 #endif // PLANNER_H
522
 #endif // PLANNER_H

+ 4
- 7
Marlin/ubl.cpp View File

28
   #include "ubl.h"
28
   #include "ubl.h"
29
   #include "hex_print_routines.h"
29
   #include "hex_print_routines.h"
30
   #include "temperature.h"
30
   #include "temperature.h"
31
-
32
-  extern Planner planner;
31
+  #include "planner.h"
33
 
32
 
34
   /**
33
   /**
35
    * These support functions allow the use of large bit arrays of flags that take very
34
    * These support functions allow the use of large bit arrays of flags that take very
48
   void unified_bed_leveling::report_state() {
47
   void unified_bed_leveling::report_state() {
49
     echo_name();
48
     echo_name();
50
     SERIAL_PROTOCOLPGM(" System v" UBL_VERSION " ");
49
     SERIAL_PROTOCOLPGM(" System v" UBL_VERSION " ");
51
-    if (!state.active) SERIAL_PROTOCOLPGM("in");
50
+    if (!planner.leveling_active) SERIAL_PROTOCOLPGM("in");
52
     SERIAL_PROTOCOLLNPGM("active.");
51
     SERIAL_PROTOCOLLNPGM("active.");
53
     safe_delay(50);
52
     safe_delay(50);
54
   }
53
   }
64
 
63
 
65
   ubl_state unified_bed_leveling::state;
64
   ubl_state unified_bed_leveling::state;
66
 
65
 
67
-  float unified_bed_leveling::z_values[GRID_MAX_POINTS_X][GRID_MAX_POINTS_Y],
68
-        unified_bed_leveling::last_specified_z;
66
+  float unified_bed_leveling::z_values[GRID_MAX_POINTS_X][GRID_MAX_POINTS_Y];
69
 
67
 
70
   // 15 is the maximum nubmer of grid points supported + 1 safety margin for now,
68
   // 15 is the maximum nubmer of grid points supported + 1 safety margin for now,
71
   // until determinism prevails
69
   // until determinism prevails
86
     set_bed_leveling_enabled(false);
84
     set_bed_leveling_enabled(false);
87
     state.storage_slot = -1;
85
     state.storage_slot = -1;
88
     #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
86
     #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
89
-      planner.z_fade_height = 10.0;
87
+      planner.set_z_fade_height(10.0);
90
     #endif
88
     #endif
91
     ZERO(z_values);
89
     ZERO(z_values);
92
-    last_specified_z = -999.9;
93
   }
90
   }
94
 
91
 
95
   void unified_bed_leveling::invalidate() {
92
   void unified_bed_leveling::invalidate() {

+ 0
- 27
Marlin/ubl.h View File

84
   class unified_bed_leveling {
84
   class unified_bed_leveling {
85
     private:
85
     private:
86
 
86
 
87
-      static float last_specified_z;
88
-
89
       static int    g29_verbose_level,
87
       static int    g29_verbose_level,
90
                     g29_phase_value,
88
                     g29_phase_value,
91
                     g29_repetition_cnt,
89
                     g29_repetition_cnt,
361
         return z0;
359
         return z0;
362
       }
360
       }
363
 
361
 
364
-      /**
365
-       * This function sets the Z leveling fade factor based on the given Z height,
366
-       * only re-calculating when necessary.
367
-       *
368
-       *  Returns 1.0 if planner.z_fade_height is 0.0.
369
-       *  Returns 0.0 if Z is past the specified 'Fade Height'.
370
-       */
371
-      #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
372
-        static inline float fade_scaling_factor_for_z(const float &lz) {
373
-          if (planner.z_fade_height == 0.0) return 1.0;
374
-          static float fade_scaling_factor = 1.0;
375
-          const float rz = RAW_Z_POSITION(lz);
376
-          if (last_specified_z != rz) {
377
-            last_specified_z = rz;
378
-            fade_scaling_factor =
379
-              rz < planner.z_fade_height
380
-                ? 1.0 - (rz * planner.inverse_z_fade_height)
381
-                : 0.0;
382
-          }
383
-          return fade_scaling_factor;
384
-        }
385
-      #else
386
-        FORCE_INLINE static float fade_scaling_factor_for_z(const float &lz) { return 1.0; }
387
-      #endif
388
-
389
       FORCE_INLINE static float mesh_index_to_xpos(const uint8_t i) {
362
       FORCE_INLINE static float mesh_index_to_xpos(const uint8_t i) {
390
         return i < GRID_MAX_POINTS_X ? pgm_read_float(&_mesh_index_to_xpos[i]) : UBL_MESH_MIN_X + i * (MESH_X_DIST);
363
         return i < GRID_MAX_POINTS_X ? pgm_read_float(&_mesh_index_to_xpos[i]) : UBL_MESH_MIN_X + i * (MESH_X_DIST);
391
       }
364
       }

+ 1
- 1
Marlin/ubl_G29.cpp View File

1165
 
1165
 
1166
       return;
1166
       return;
1167
     }
1167
     }
1168
-    ubl_state_at_invocation = state.active;
1168
+    ubl_state_at_invocation = planner.leveling_active;
1169
     set_bed_leveling_enabled(false);
1169
     set_bed_leveling_enabled(false);
1170
   }
1170
   }
1171
 
1171
 

+ 15
- 21
Marlin/ubl_motion.cpp View File

186
       // are going to apply the Y-Distance into the cell to interpolate the final Z correction.
186
       // are going to apply the Y-Distance into the cell to interpolate the final Z correction.
187
 
187
 
188
       const float yratio = (RAW_Y_POSITION(end[Y_AXIS]) - mesh_index_to_ypos(cell_dest_yi)) * (1.0 / (MESH_Y_DIST));
188
       const float yratio = (RAW_Y_POSITION(end[Y_AXIS]) - mesh_index_to_ypos(cell_dest_yi)) * (1.0 / (MESH_Y_DIST));
189
-      float z0 = cell_dest_yi < GRID_MAX_POINTS_Y - 1 ? (z1 + (z2 - z1) * yratio) * fade_scaling_factor_for_z(end[Z_AXIS]) : 0.0;
189
+      float z0 = cell_dest_yi < GRID_MAX_POINTS_Y - 1 ? (z1 + (z2 - z1) * yratio) * planner.fade_scaling_factor_for_z(end[Z_AXIS]) : 0.0;
190
 
190
 
191
       /**
191
       /**
192
        * If part of the Mesh is undefined, it will show up as NAN
192
        * If part of the Mesh is undefined, it will show up as NAN
270
          */
270
          */
271
         const float x = inf_m_flag ? start[X_AXIS] : (next_mesh_line_y - c) / m;
271
         const float x = inf_m_flag ? start[X_AXIS] : (next_mesh_line_y - c) / m;
272
 
272
 
273
-        float z0 = z_correction_for_x_on_horizontal_mesh_line(x, current_xi, current_yi);
274
-
275
-        z0 *= fade_scaling_factor_for_z(end[Z_AXIS]);
273
+        float z0 = z_correction_for_x_on_horizontal_mesh_line(x, current_xi, current_yi)
274
+                   * planner.fade_scaling_factor_for_z(end[Z_AXIS]);
276
 
275
 
277
         /**
276
         /**
278
          * If part of the Mesh is undefined, it will show up as NAN
277
          * If part of the Mesh is undefined, it will show up as NAN
335
         const float next_mesh_line_x = LOGICAL_X_POSITION(mesh_index_to_xpos(current_xi)),
334
         const float next_mesh_line_x = LOGICAL_X_POSITION(mesh_index_to_xpos(current_xi)),
336
                     y = m * next_mesh_line_x + c;   // Calculate Y at the next X mesh line
335
                     y = m * next_mesh_line_x + c;   // Calculate Y at the next X mesh line
337
 
336
 
338
-        float z0 = z_correction_for_y_on_vertical_mesh_line(y, current_xi, current_yi);
339
-
340
-        z0 *= fade_scaling_factor_for_z(end[Z_AXIS]);
337
+        float z0 = z_correction_for_y_on_vertical_mesh_line(y, current_xi, current_yi)
338
+                   * planner.fade_scaling_factor_for_z(end[Z_AXIS]);
341
 
339
 
342
         /**
340
         /**
343
          * If part of the Mesh is undefined, it will show up as NAN
341
          * If part of the Mesh is undefined, it will show up as NAN
408
 
406
 
409
       if (left_flag == (x > next_mesh_line_x)) { // Check if we hit the Y line first
407
       if (left_flag == (x > next_mesh_line_x)) { // Check if we hit the Y line first
410
         // Yes!  Crossing a Y Mesh Line next
408
         // Yes!  Crossing a Y Mesh Line next
411
-        float z0 = z_correction_for_x_on_horizontal_mesh_line(x, current_xi - left_flag, current_yi + dyi);
412
-
413
-        z0 *= fade_scaling_factor_for_z(end[Z_AXIS]);
409
+        float z0 = z_correction_for_x_on_horizontal_mesh_line(x, current_xi - left_flag, current_yi + dyi)
410
+                   * planner.fade_scaling_factor_for_z(end[Z_AXIS]);
414
 
411
 
415
         /**
412
         /**
416
          * If part of the Mesh is undefined, it will show up as NAN
413
          * If part of the Mesh is undefined, it will show up as NAN
436
       }
433
       }
437
       else {
434
       else {
438
         // Yes!  Crossing a X Mesh Line next
435
         // Yes!  Crossing a X Mesh Line next
439
-        float z0 = z_correction_for_y_on_vertical_mesh_line(y, current_xi + dxi, current_yi - down_flag);
440
-
441
-        z0 *= fade_scaling_factor_for_z(end[Z_AXIS]);
436
+        float z0 = z_correction_for_y_on_vertical_mesh_line(y, current_xi + dxi, current_yi - down_flag)
437
+                   * planner.fade_scaling_factor_for_z(end[Z_AXIS]);
442
 
438
 
443
         /**
439
         /**
444
          * If part of the Mesh is undefined, it will show up as NAN
440
          * If part of the Mesh is undefined, it will show up as NAN
603
 
599
 
604
       // Only compute leveling per segment if ubl active and target below z_fade_height.
600
       // Only compute leveling per segment if ubl active and target below z_fade_height.
605
 
601
 
606
-      if (!state.active || above_fade_height) {   // no mesh leveling
602
+      if (!planner.leveling_active || !planner.leveling_active_at_z(ltarget[Z_AXIS])) {   // no mesh leveling
607
 
603
 
608
         do {
604
         do {
609
 
605
 
629
       // Otherwise perform per-segment leveling
625
       // Otherwise perform per-segment leveling
630
 
626
 
631
       #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
627
       #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
632
-        const float fade_scaling_factor = fade_scaling_factor_for_z(ltarget[Z_AXIS]);
628
+        const float fade_scaling_factor = planner.fade_scaling_factor_for_z(ltarget[Z_AXIS]);
629
+      #else
630
+        constexpr float fade_scaling_factor = 1.0;
633
       #endif
631
       #endif
634
 
632
 
635
       // increment to first segment destination
633
       // increment to first segment destination
661
               z_x0y1 = z_values[cell_xi  ][cell_yi+1],  // z at lower right corner
659
               z_x0y1 = z_values[cell_xi  ][cell_yi+1],  // z at lower right corner
662
               z_x1y1 = z_values[cell_xi+1][cell_yi+1];  // z at upper right corner
660
               z_x1y1 = z_values[cell_xi+1][cell_yi+1];  // z at upper right corner
663
 
661
 
664
-        if (isnan(z_x0y0)) z_x0y0 = 0;              // ideally activating state.active (G29 A)
662
+        if (isnan(z_x0y0)) z_x0y0 = 0;              // ideally activating planner.leveling_active (G29 A)
665
         if (isnan(z_x1y0)) z_x1y0 = 0;              //   should refuse if any invalid mesh points
663
         if (isnan(z_x1y0)) z_x1y0 = 0;              //   should refuse if any invalid mesh points
666
         if (isnan(z_x0y1)) z_x0y1 = 0;              //   in order to avoid isnan tests per cell,
664
         if (isnan(z_x0y1)) z_x0y1 = 0;              //   in order to avoid isnan tests per cell,
667
         if (isnan(z_x1y1)) z_x1y1 = 0;              //   thus guessing zero for undefined points
665
         if (isnan(z_x1y1)) z_x1y1 = 0;              //   thus guessing zero for undefined points
690
 
688
 
691
         for(;;) {  // for all segments within this mesh cell
689
         for(;;) {  // for all segments within this mesh cell
692
 
690
 
693
-          float z_cxcy = z_cxy0 + z_cxym * cy;      // interpolated mesh z height along cx at cy
694
-
695
-          #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
696
-            z_cxcy *= fade_scaling_factor;          // apply fade factor to interpolated mesh height
697
-          #endif
691
+          float z_cxcy = (z_cxy0 + z_cxym * cy) * fade_scaling_factor; // interpolated mesh z height along cx at cy, scaled for fade
698
 
692
 
699
           if (--segments == 0) {                    // if this is last segment, use ltarget for exact
693
           if (--segments == 0) {                    // if this is last segment, use ltarget for exact
700
             seg_rx = RAW_X_POSITION(ltarget[X_AXIS]);
694
             seg_rx = RAW_X_POSITION(ltarget[X_AXIS]);

+ 3
- 4
Marlin/ultralcd.cpp View File

1089
           const float new_zoffset = zprobe_zoffset + planner.steps_to_mm[Z_AXIS] * babystep_increment;
1089
           const float new_zoffset = zprobe_zoffset + planner.steps_to_mm[Z_AXIS] * babystep_increment;
1090
           if (WITHIN(new_zoffset, Z_PROBE_OFFSET_RANGE_MIN, Z_PROBE_OFFSET_RANGE_MAX)) {
1090
           if (WITHIN(new_zoffset, Z_PROBE_OFFSET_RANGE_MIN, Z_PROBE_OFFSET_RANGE_MAX)) {
1091
 
1091
 
1092
-            if (planner.abl_enabled)
1092
+            if (planner.leveling_active)
1093
               thermalManager.babystep_axis(Z_AXIS, babystep_increment);
1093
               thermalManager.babystep_axis(Z_AXIS, babystep_increment);
1094
 
1094
 
1095
             zprobe_zoffset = new_zoffset;
1095
             zprobe_zoffset = new_zoffset;
1791
 
1791
 
1792
             _lcd_after_probing();
1792
             _lcd_after_probing();
1793
 
1793
 
1794
-            mbl.set_has_mesh(true);
1794
+            mbl.has_mesh = true;
1795
             mesh_probing_done();
1795
             mesh_probing_done();
1796
 
1796
 
1797
           #endif
1797
           #endif
1937
       if (!(axis_known_position[X_AXIS] && axis_known_position[Y_AXIS] && axis_known_position[Z_AXIS]))
1937
       if (!(axis_known_position[X_AXIS] && axis_known_position[Y_AXIS] && axis_known_position[Z_AXIS]))
1938
         MENU_ITEM(gcode, MSG_AUTO_HOME, PSTR("G28"));
1938
         MENU_ITEM(gcode, MSG_AUTO_HOME, PSTR("G28"));
1939
       else if (leveling_is_valid()) {
1939
       else if (leveling_is_valid()) {
1940
-        _level_state = LEVELING_IS_ACTIVE();
1940
+        _level_state = planner.leveling_active;
1941
         MENU_ITEM_EDIT_CALLBACK(bool, MSG_BED_LEVELING, &_level_state, _lcd_toggle_bed_leveling);
1941
         MENU_ITEM_EDIT_CALLBACK(bool, MSG_BED_LEVELING, &_level_state, _lcd_toggle_bed_leveling);
1942
       }
1942
       }
1943
 
1943
 
1944
       #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
1944
       #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
1945
-        set_z_fade_height(planner.z_fade_height);
1946
         MENU_MULTIPLIER_ITEM_EDIT_CALLBACK(float62, MSG_Z_FADE_HEIGHT, &planner.z_fade_height, 0.0, 100.0, _lcd_set_z_fade_height);
1945
         MENU_MULTIPLIER_ITEM_EDIT_CALLBACK(float62, MSG_Z_FADE_HEIGHT, &planner.z_fade_height, 0.0, 100.0, _lcd_set_z_fade_height);
1947
       #endif
1946
       #endif
1948
 
1947
 

+ 1
- 1
Marlin/ultralcd_impl_HD44780.h View File

792
     lcd.print(ftostr52sp(FIXFLOAT(current_position[Z_AXIS])));
792
     lcd.print(ftostr52sp(FIXFLOAT(current_position[Z_AXIS])));
793
 
793
 
794
     #if HAS_LEVELING
794
     #if HAS_LEVELING
795
-      lcd.write(LEVELING_IS_ACTIVE() || blink ? '_' : ' ');
795
+      lcd.write(planner.leveling_active || blink ? '_' : ' ');
796
     #endif
796
     #endif
797
 
797
 
798
   #endif // LCD_HEIGHT > 2
798
   #endif // LCD_HEIGHT > 2

Loading…
Cancel
Save