Browse Source

Merge pull request #5381 from thinkyhead/rc_fix_fade_factor

Properly apply fade_factor to MBL
Scott Lahteine 7 years ago
parent
commit
200cf3e9f9
3 changed files with 54 additions and 34 deletions
  1. 25
    14
      Marlin/Marlin_main.cpp
  2. 27
    18
      Marlin/mesh_bed_leveling.h
  3. 2
    2
      Marlin/planner.cpp

+ 25
- 14
Marlin/Marlin_main.cpp View File

@@ -2224,11 +2224,15 @@ static void clean_up_after_endstop_or_probe_move() {
2224 2224
   void set_bed_leveling_enabled(bool enable=true) {
2225 2225
     #if ENABLED(MESH_BED_LEVELING)
2226 2226
 
2227
-      if (!enable && mbl.active())
2228
-        current_position[Z_AXIS] +=
2229
-          mbl.get_z(RAW_CURRENT_POSITION(X_AXIS), RAW_CURRENT_POSITION(Y_AXIS)) - (MESH_HOME_SEARCH_Z);
2227
+      if (enable != mbl.active()) {
2230 2228
 
2231
-      mbl.set_active(enable && mbl.has_mesh()); // was set_has_mesh(). Is this not correct?
2229
+        if (!enable)
2230
+          planner.apply_leveling(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS]);
2231
+
2232
+        mbl.set_active(enable && mbl.has_mesh());
2233
+
2234
+        if (enable) planner.unapply_leveling(current_position);
2235
+      }
2232 2236
 
2233 2237
     #elif HAS_ABL
2234 2238
 
@@ -3162,8 +3166,10 @@ inline void gcode_G4() {
3162 3166
     #elif ENABLED(MESH_BED_LEVELING)
3163 3167
       SERIAL_ECHOPGM("Mesh Bed Leveling");
3164 3168
       if (mbl.active()) {
3169
+        float lz = current_position[Z_AXIS];
3170
+        planner.apply_leveling(current_position[X_AXIS], current_position[Y_AXIS], lz);
3165 3171
         SERIAL_ECHOLNPGM(" (enabled)");
3166
-        SERIAL_ECHOPAIR("MBL Adjustment Z", mbl.get_z(RAW_CURRENT_POSITION(X_AXIS), RAW_CURRENT_POSITION(Y_AXIS)));
3172
+        SERIAL_ECHOPAIR("MBL Adjustment Z", lz);
3167 3173
       }
3168 3174
       SERIAL_EOL;
3169 3175
     #endif
@@ -3321,13 +3327,15 @@ inline void gcode_G28() {
3321 3327
       #if ENABLED(DEBUG_LEVELING_FEATURE)
3322 3328
         if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("MBL was active");
3323 3329
       #endif
3324
-      // Save known Z position if already homed
3330
+      // Use known Z position if already homed
3325 3331
       if (axis_homed[X_AXIS] && axis_homed[Y_AXIS] && axis_homed[Z_AXIS]) {
3332
+        set_bed_leveling_enabled(false);
3326 3333
         pre_home_z = current_position[Z_AXIS];
3327
-        pre_home_z += mbl.get_z(RAW_CURRENT_POSITION(X_AXIS), RAW_CURRENT_POSITION(Y_AXIS));
3328 3334
       }
3329
-      mbl.set_active(false);
3330
-      current_position[Z_AXIS] = pre_home_z;
3335
+      else {
3336
+        mbl.set_active(false);
3337
+        current_position[Z_AXIS] = pre_home_z;
3338
+      }
3331 3339
       #if ENABLED(DEBUG_LEVELING_FEATURE)
3332 3340
         if (DEBUGGING(LEVELING)) DEBUG_POS("Set Z to pre_home_z", current_position);
3333 3341
       #endif
@@ -3703,8 +3711,8 @@ inline void gcode_G28() {
3703 3711
 
3704 3712
       case MeshReset:
3705 3713
         if (mbl.active()) {
3706
-          current_position[Z_AXIS] +=
3707
-            mbl.get_z(RAW_CURRENT_POSITION(X_AXIS), RAW_CURRENT_POSITION(Y_AXIS)) - MESH_HOME_SEARCH_Z;
3714
+          current_position[Z_AXIS] -= MESH_HOME_SEARCH_Z;
3715
+          planner.apply_leveling(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS]);
3708 3716
           mbl.reset();
3709 3717
           SYNC_PLAN_POSITION_KINEMATIC();
3710 3718
         }
@@ -7640,9 +7648,12 @@ void tool_change(const uint8_t tmp_extruder, const float fr_mm_s/*=0.0*/, bool n
7640 7648
                 #if ENABLED(DEBUG_LEVELING_FEATURE)
7641 7649
                   if (DEBUGGING(LEVELING)) SERIAL_ECHOPAIR("Z before MBL: ", current_position[Z_AXIS]);
7642 7650
                 #endif
7643
-                float xpos = RAW_CURRENT_POSITION(X_AXIS),
7644
-                      ypos = RAW_CURRENT_POSITION(Y_AXIS);
7645
-                current_position[Z_AXIS] += mbl.get_z(xpos + xydiff[X_AXIS], ypos + xydiff[Y_AXIS]) - mbl.get_z(xpos, ypos);
7651
+                float x2 = current_position[X_AXIS] + xydiff[X_AXIS],
7652
+                      y2 = current_position[Y_AXIS] + xydiff[Y_AXIS],
7653
+                      z1 = current_position[Z_AXIS], z2 = z1;
7654
+                planner.apply_leveling(current_position[X_AXIS], current_position[Y_AXIS], z1);
7655
+                planner.apply_leveling(x2, y2, z2);
7656
+                current_position[Z_AXIS] += z2 - z1;
7646 7657
                 #if ENABLED(DEBUG_LEVELING_FEATURE)
7647 7658
                   if (DEBUGGING(LEVELING))
7648 7659
                     SERIAL_ECHOLNPAIR(" after: ", current_position[Z_AXIS]);

+ 27
- 18
Marlin/mesh_bed_leveling.h View File

@@ -36,54 +36,58 @@
36 36
 
37 37
     void reset();
38 38
 
39
-    static FORCE_INLINE float get_probe_x(int8_t i) { return MESH_MIN_X + (MESH_X_DIST) * i; }
40
-    static FORCE_INLINE float get_probe_y(int8_t i) { return MESH_MIN_Y + (MESH_Y_DIST) * i; }
41
-    void set_z(const int8_t px, const int8_t py, const float z) { z_values[py][px] = z; }
39
+    static FORCE_INLINE float get_probe_x(const int8_t i) { return MESH_MIN_X + (MESH_X_DIST) * i; }
40
+    static FORCE_INLINE float get_probe_y(const int8_t i) { return MESH_MIN_Y + (MESH_Y_DIST) * i; }
41
+    void set_z(const int8_t px, const int8_t py, const float &z) { z_values[py][px] = z; }
42 42
 
43
-    bool active()                 { return TEST(status, MBL_STATUS_ACTIVE_BIT); }
44
-    void set_active(bool onOff)   { if (onOff) SBI(status, MBL_STATUS_ACTIVE_BIT); else CBI(status, MBL_STATUS_ACTIVE_BIT); }
45
-    bool has_mesh()               { return TEST(status, MBL_STATUS_HAS_MESH_BIT); }
46
-    void set_has_mesh(bool onOff) { if (onOff) SBI(status, MBL_STATUS_HAS_MESH_BIT); else CBI(status, MBL_STATUS_HAS_MESH_BIT); }
43
+    bool active() const                 { return TEST(status, MBL_STATUS_ACTIVE_BIT); }
44
+    void set_active(const bool onOff)   { onOff ? SBI(status, MBL_STATUS_ACTIVE_BIT) : CBI(status, MBL_STATUS_ACTIVE_BIT); }
45
+    bool has_mesh() const               { return TEST(status, MBL_STATUS_HAS_MESH_BIT); }
46
+    void set_has_mesh(const bool onOff) { onOff ? SBI(status, MBL_STATUS_HAS_MESH_BIT) : CBI(status, MBL_STATUS_HAS_MESH_BIT); }
47 47
 
48
-    inline void zigzag(int8_t index, int8_t &px, int8_t &py) {
48
+    inline void zigzag(const int8_t index, int8_t &px, int8_t &py) const {
49 49
       px = index % (MESH_NUM_X_POINTS);
50 50
       py = index / (MESH_NUM_X_POINTS);
51 51
       if (py & 1) px = (MESH_NUM_X_POINTS - 1) - px; // Zig zag
52 52
     }
53 53
 
54
-    void set_zigzag_z(int8_t index, float z) {
54
+    void set_zigzag_z(const int8_t index, const float &z) {
55 55
       int8_t px, py;
56 56
       zigzag(index, px, py);
57 57
       set_z(px, py, z);
58 58
     }
59 59
 
60
-    int8_t cell_index_x(float x) {
60
+    int8_t cell_index_x(const float &x) const {
61 61
       int8_t cx = (x - (MESH_MIN_X)) * (1.0 / (MESH_X_DIST));
62 62
       return constrain(cx, 0, (MESH_NUM_X_POINTS) - 2);
63 63
     }
64 64
 
65
-    int8_t cell_index_y(float y) {
65
+    int8_t cell_index_y(const float &y) const {
66 66
       int8_t cy = (y - (MESH_MIN_Y)) * (1.0 / (MESH_Y_DIST));
67 67
       return constrain(cy, 0, (MESH_NUM_Y_POINTS) - 2);
68 68
     }
69 69
 
70
-    int8_t probe_index_x(float x) {
70
+    int8_t probe_index_x(const float &x) const {
71 71
       int8_t px = (x - (MESH_MIN_X) + (MESH_X_DIST) * 0.5) * (1.0 / (MESH_X_DIST));
72 72
       return (px >= 0 && px < (MESH_NUM_X_POINTS)) ? px : -1;
73 73
     }
74 74
 
75
-    int8_t probe_index_y(float y) {
75
+    int8_t probe_index_y(const float &y) const {
76 76
       int8_t py = (y - (MESH_MIN_Y) + (MESH_Y_DIST) * 0.5) * (1.0 / (MESH_Y_DIST));
77 77
       return (py >= 0 && py < (MESH_NUM_Y_POINTS)) ? py : -1;
78 78
     }
79 79
 
80
-    float calc_z0(float a0, float a1, float z1, float a2, float z2) {
81
-      float delta_z = (z2 - z1) / (a2 - a1);
82
-      float delta_a = a0 - a1;
80
+    float calc_z0(const float &a0, const float &a1, const float &z1, const float &a2, const float &z2) const {
81
+      const float delta_z = (z2 - z1) / (a2 - a1);
82
+      const float delta_a = a0 - a1;
83 83
       return z1 + delta_a * delta_z;
84 84
     }
85 85
 
86
-    float get_z(float x0, float y0) {
86
+    float get_z(const float &x0, const float &y0
87
+      #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
88
+        , const float &factor
89
+      #endif
90
+    ) const {
87 91
       int8_t cx = cell_index_x(x0),
88 92
              cy = cell_index_y(y0);
89 93
       if (cx < 0 || cy < 0) return z_offset;
@@ -96,7 +100,12 @@
96 100
       float z0 = calc_z0(y0,
97 101
                          get_probe_y(cy), z1,
98 102
                          get_probe_y(cy + 1), z2);
99
-      return z0 + z_offset;
103
+
104
+      #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
105
+        return z0 * factor + z_offset;
106
+      #else
107
+        return z0 + z_offset;
108
+      #endif
100 109
     }
101 110
   };
102 111
 

+ 2
- 2
Marlin/planner.cpp View File

@@ -559,7 +559,7 @@ void Planner::check_axes_activity() {
559 559
     #if ENABLED(MESH_BED_LEVELING)
560 560
 
561 561
       if (mbl.active())
562
-        lz += mbl.get_z(RAW_X_POSITION(lx), RAW_Y_POSITION(ly)) * z_fade_factor;
562
+        lz += mbl.get_z(RAW_X_POSITION(lx), RAW_Y_POSITION(ly), z_fade_factor);
563 563
 
564 564
     #elif ABL_PLANAR
565 565
 
@@ -595,7 +595,7 @@ void Planner::check_axes_activity() {
595 595
 
596 596
       if (mbl.active()) {
597 597
         #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
598
-          const float c = mbl.get_z(RAW_X_POSITION(logical[X_AXIS]), RAW_Y_POSITION(logical[Y_AXIS]));
598
+          const float c = mbl.get_z(RAW_X_POSITION(logical[X_AXIS]), RAW_Y_POSITION(logical[Y_AXIS]), 1.0);
599 599
           logical[Z_AXIS] = (z_fade_height * (RAW_Z_POSITION(logical[Z_AXIS]) - c)) / (z_fade_height - c);
600 600
         #else
601 601
           logical[Z_AXIS] -= mbl.get_z(RAW_X_POSITION(logical[X_AXIS]), RAW_Y_POSITION(logical[Y_AXIS]));

Loading…
Cancel
Save