Browse Source

Merge pull request #3956 from thinkyhead/rc_mbl_single_axis_fix

MBL: Keep active when homing single axes (#3750)
Scott Lahteine 9 years ago
parent
commit
4bdcf3afe4

+ 62
- 24
Marlin/Marlin_main.cpp View File

@@ -2644,8 +2644,16 @@ inline void gcode_G28() {
2644 2644
    * on again when homing all axis
2645 2645
    */
2646 2646
   #if ENABLED(MESH_BED_LEVELING)
2647
-    uint8_t mbl_was_active = mbl.active;
2648
-    mbl.active = false;
2647
+    float pre_home_z = MESH_HOME_SEARCH_Z;
2648
+    if (mbl.active()) {
2649
+      // Save known Z position if already homed
2650
+      if (axis_homed[X_AXIS] && axis_homed[Y_AXIS] && axis_homed[Z_AXIS]) {
2651
+        pre_home_z = current_position[Z_AXIS];
2652
+        pre_home_z += mbl.get_z(current_position[X_AXIS] - home_offset[X_AXIS],
2653
+                                current_position[Y_AXIS] - home_offset[Y_AXIS]);
2654
+      }
2655
+      mbl.set_active(false);
2656
+    }
2649 2657
   #endif
2650 2658
 
2651 2659
   setup_for_endstop_move();
@@ -2945,20 +2953,31 @@ inline void gcode_G28() {
2945 2953
 
2946 2954
   // Enable mesh leveling again
2947 2955
   #if ENABLED(MESH_BED_LEVELING)
2948
-    if (mbl_was_active && home_all_axis) {
2949
-      current_position[Z_AXIS] = MESH_HOME_SEARCH_Z;
2950
-      sync_plan_position();
2951
-      mbl.active = 1;
2952
-      #if ENABLED(MESH_G28_REST_ORIGIN)
2953
-        current_position[Z_AXIS] = 0.0;
2954
-        set_destination_to_current();
2955
-        feedrate = homing_feedrate[Z_AXIS];
2956
-        line_to_destination();
2957
-        stepper.synchronize();
2958
-      #endif
2959
-      #if ENABLED(DEBUG_LEVELING_FEATURE)
2960
-        if (DEBUGGING(LEVELING)) DEBUG_POS("mbl_was_active", current_position);
2961
-      #endif
2956
+    if (mbl.has_mesh()) {
2957
+      if (home_all_axis || (axis_homed[X_AXIS] && axis_homed[Y_AXIS] && homeZ)) {
2958
+        current_position[Z_AXIS] = MESH_HOME_SEARCH_Z;
2959
+        sync_plan_position();
2960
+        mbl.set_active(true);
2961
+        #if ENABLED(MESH_G28_REST_ORIGIN)
2962
+          current_position[Z_AXIS] = 0.0;
2963
+          set_destination_to_current();
2964
+          feedrate = homing_feedrate[Z_AXIS];
2965
+          line_to_destination();
2966
+          stepper.synchronize();
2967
+        #else
2968
+          current_position[Z_AXIS] = MESH_HOME_SEARCH_Z -
2969
+            mbl.get_z(current_position[X_AXIS] - home_offset[X_AXIS],
2970
+                      current_position[Y_AXIS] - home_offset[Y_AXIS]);
2971
+        #endif
2972
+      }
2973
+      else if ((axis_homed[X_AXIS] && axis_homed[Y_AXIS] && axis_homed[Z_AXIS]) && (homeX || homeY)) {
2974
+        current_position[Z_AXIS] = pre_home_z;
2975
+        sync_plan_position();
2976
+        mbl.set_active(true);
2977
+        current_position[Z_AXIS] = pre_home_z -
2978
+          mbl.get_z(current_position[X_AXIS] - home_offset[X_AXIS],
2979
+                    current_position[Y_AXIS] - home_offset[Y_AXIS]);
2980
+      }
2962 2981
     }
2963 2982
   #endif
2964 2983
 
@@ -2978,7 +2997,7 @@ inline void gcode_G28() {
2978 2997
 
2979 2998
 #if ENABLED(MESH_BED_LEVELING)
2980 2999
 
2981
-  enum MeshLevelingState { MeshReport, MeshStart, MeshNext, MeshSet, MeshSetZOffset };
3000
+  enum MeshLevelingState { MeshReport, MeshStart, MeshNext, MeshSet, MeshSetZOffset, MeshReset };
2982 3001
 
2983 3002
   inline void _mbl_goto_xy(float x, float y) {
2984 3003
     saved_feedrate = feedrate;
@@ -3015,6 +3034,7 @@ inline void gcode_G28() {
3015 3034
    *  S2              Probe the next mesh point
3016 3035
    *  S3 Xn Yn Zn.nn  Manually modify a single point
3017 3036
    *  S4 Zn.nn        Set z offset. Positive away from bed, negative closer to bed.
3037
+   *  S5              Reset and disable mesh
3018 3038
    *
3019 3039
    * The S0 report the points as below
3020 3040
    *
@@ -3028,8 +3048,8 @@ inline void gcode_G28() {
3028 3048
 
3029 3049
     static int probe_point = -1;
3030 3050
     MeshLevelingState state = code_seen('S') ? (MeshLevelingState)code_value_short() : MeshReport;
3031
-    if (state < 0 || state > 4) {
3032
-      SERIAL_PROTOCOLLNPGM("S out of range (0-4).");
3051
+    if (state < 0 || state > 5) {
3052
+      SERIAL_PROTOCOLLNPGM("S out of range (0-5).");
3033 3053
       return;
3034 3054
     }
3035 3055
 
@@ -3038,8 +3058,13 @@ inline void gcode_G28() {
3038 3058
 
3039 3059
     switch (state) {
3040 3060
       case MeshReport:
3041
-        if (mbl.active) {
3042
-          SERIAL_PROTOCOLPGM("Num X,Y: ");
3061
+        if (mbl.has_mesh()) {
3062
+          SERIAL_PROTOCOLPGM("State: ");
3063
+          if (mbl.active())
3064
+            SERIAL_PROTOCOLPGM("On");
3065
+          else
3066
+            SERIAL_PROTOCOLPGM("Off");
3067
+          SERIAL_PROTOCOLPGM("\nNum X,Y: ");
3043 3068
           SERIAL_PROTOCOL(MESH_NUM_X_POINTS);
3044 3069
           SERIAL_PROTOCOLCHAR(',');
3045 3070
           SERIAL_PROTOCOL(MESH_NUM_Y_POINTS);
@@ -3100,7 +3125,7 @@ inline void gcode_G28() {
3100 3125
           // After recording the last point, activate the mbl and home
3101 3126
           SERIAL_PROTOCOLLNPGM("Mesh probing done.");
3102 3127
           probe_point = -1;
3103
-          mbl.active = true;
3128
+          mbl.set_has_mesh(true);
3104 3129
           enqueue_and_echo_commands_P(PSTR("G28"));
3105 3130
         }
3106 3131
         break;
@@ -3147,6 +3172,19 @@ inline void gcode_G28() {
3147 3172
           return;
3148 3173
         }
3149 3174
         mbl.z_offset = z;
3175
+        break;
3176
+
3177
+      case MeshReset:
3178
+        if (mbl.active()) {
3179
+          current_position[Z_AXIS] +=
3180
+            mbl.get_z(current_position[X_AXIS] - home_offset[X_AXIS],
3181
+                      current_position[Y_AXIS] - home_offset[Y_AXIS]) - MESH_HOME_SEARCH_Z;
3182
+          mbl.reset();
3183
+          sync_plan_position();
3184
+        }
3185
+        else
3186
+          mbl.reset();
3187
+
3150 3188
     } // switch(state)
3151 3189
 
3152 3190
     report_current_position();
@@ -5944,7 +5982,7 @@ inline void gcode_M410() {
5944 5982
   /**
5945 5983
    * M420: Enable/Disable Mesh Bed Leveling
5946 5984
    */
5947
-  inline void gcode_M420() { if (code_seen('S') && code_has_value()) mbl.active = !!code_value_short(); }
5985
+  inline void gcode_M420() { if (code_seen('S') && code_has_value()) mbl.set_has_mesh(!!code_value_short()); }
5948 5986
 
5949 5987
   /**
5950 5988
    * M421: Set a single Mesh Bed Leveling Z coordinate
@@ -7335,7 +7373,7 @@ void clamp_to_software_endstops(float target[3]) {
7335 7373
 
7336 7374
 // This function is used to split lines on mesh borders so each segment is only part of one mesh area
7337 7375
 void mesh_buffer_line(float x, float y, float z, const float e, float feed_rate, const uint8_t& extruder, uint8_t x_splits = 0xff, uint8_t y_splits = 0xff) {
7338
-  if (!mbl.active) {
7376
+  if (!mbl.active()) {
7339 7377
     planner.buffer_line(x, y, z, e, feed_rate, extruder);
7340 7378
     set_current_to_destination();
7341 7379
     return;

+ 12
- 11
Marlin/configuration_store.cpp View File

@@ -58,8 +58,8 @@
58 58
  *  188  M206 XYZ  home_offset (float x3)
59 59
  *
60 60
  * Mesh bed leveling:
61
- *  200  M420 S    active (bool)
62
- *  201            z_offset (float) (added in V23)
61
+ *  200  M420 S    status (uint8)
62
+ *  201            z_offset (float)
63 63
  *  205            mesh_num_x (uint8 as set in firmware)
64 64
  *  206            mesh_num_y (uint8 as set in firmware)
65 65
  *  207 G29 S3 XYZ z_values[][] (float x9, by default)
@@ -187,20 +187,21 @@ void Config_StoreSettings()  {
187 187
   EEPROM_WRITE_VAR(i, planner.max_e_jerk);
188 188
   EEPROM_WRITE_VAR(i, home_offset);
189 189
 
190
-  uint8_t mesh_num_x = 3;
191
-  uint8_t mesh_num_y = 3;
192 190
   #if ENABLED(MESH_BED_LEVELING)
193 191
     // Compile time test that sizeof(mbl.z_values) is as expected
194 192
     typedef char c_assert[(sizeof(mbl.z_values) == (MESH_NUM_X_POINTS) * (MESH_NUM_Y_POINTS) * sizeof(dummy)) ? 1 : -1];
195
-    mesh_num_x = MESH_NUM_X_POINTS;
196
-    mesh_num_y = MESH_NUM_Y_POINTS;
197
-    EEPROM_WRITE_VAR(i, mbl.active);
193
+    uint8_t mesh_num_x = MESH_NUM_X_POINTS,
194
+            mesh_num_y = MESH_NUM_Y_POINTS,
195
+            dummy_uint8 = mbl.status & _BV(MBL_STATUS_HAS_MESH_BIT);
196
+    EEPROM_WRITE_VAR(i, dummy_uint8);
198 197
     EEPROM_WRITE_VAR(i, mbl.z_offset);
199 198
     EEPROM_WRITE_VAR(i, mesh_num_x);
200 199
     EEPROM_WRITE_VAR(i, mesh_num_y);
201 200
     EEPROM_WRITE_VAR(i, mbl.z_values);
202 201
   #else
203
-    uint8_t dummy_uint8 = 0;
202
+    uint8_t mesh_num_x = 3,
203
+            mesh_num_y = 3,
204
+            dummy_uint8 = 0;
204 205
     dummy = 0.0f;
205 206
     EEPROM_WRITE_VAR(i, dummy_uint8);
206 207
     EEPROM_WRITE_VAR(i, dummy);
@@ -376,7 +377,7 @@ void Config_RetrieveSettings() {
376 377
     EEPROM_READ_VAR(i, mesh_num_x);
377 378
     EEPROM_READ_VAR(i, mesh_num_y);
378 379
     #if ENABLED(MESH_BED_LEVELING)
379
-      mbl.active = dummy_uint8;
380
+      mbl.status = dummy_uint8;
380 381
       mbl.z_offset = dummy;
381 382
       if (mesh_num_x == MESH_NUM_X_POINTS && mesh_num_y == MESH_NUM_Y_POINTS) {
382 383
         EEPROM_READ_VAR(i, mbl.z_values);
@@ -550,7 +551,7 @@ void Config_ResetDefault() {
550 551
   home_offset[X_AXIS] = home_offset[Y_AXIS] = home_offset[Z_AXIS] = 0;
551 552
 
552 553
   #if ENABLED(MESH_BED_LEVELING)
553
-    mbl.active = false;
554
+    mbl.reset();
554 555
   #endif
555 556
 
556 557
   #if ENABLED(AUTO_BED_LEVELING_FEATURE)
@@ -729,7 +730,7 @@ void Config_PrintSettings(bool forReplay) {
729 730
       SERIAL_ECHOLNPGM("Mesh bed leveling:");
730 731
       CONFIG_ECHO_START;
731 732
     }
732
-    SERIAL_ECHOPAIR("  M420 S", mbl.active);
733
+    SERIAL_ECHOPAIR("  M420 S", mbl.has_mesh() ? 1 : 0);
733 734
     SERIAL_ECHOPAIR(" X", MESH_NUM_X_POINTS);
734 735
     SERIAL_ECHOPAIR(" Y", MESH_NUM_Y_POINTS);
735 736
     SERIAL_EOL;

+ 1
- 1
Marlin/mesh_bed_leveling.cpp View File

@@ -29,7 +29,7 @@
29 29
   mesh_bed_leveling::mesh_bed_leveling() { reset(); }
30 30
 
31 31
   void mesh_bed_leveling::reset() {
32
-    active = 0;
32
+    status = MBL_STATUS_NONE;
33 33
     z_offset = 0;
34 34
     for (int8_t y = MESH_NUM_Y_POINTS; y--;)
35 35
       for (int8_t x = MESH_NUM_X_POINTS; x--;)

+ 8
- 1
Marlin/mesh_bed_leveling.h View File

@@ -24,12 +24,14 @@
24 24
 
25 25
 #if ENABLED(MESH_BED_LEVELING)
26 26
 
27
+  enum MBLStatus { MBL_STATUS_NONE = 0, MBL_STATUS_HAS_MESH_BIT = 0, MBL_STATUS_ACTIVE_BIT = 1 };
28
+
27 29
   #define MESH_X_DIST ((MESH_MAX_X - (MESH_MIN_X))/(MESH_NUM_X_POINTS - 1))
28 30
   #define MESH_Y_DIST ((MESH_MAX_Y - (MESH_MIN_Y))/(MESH_NUM_Y_POINTS - 1))
29 31
 
30 32
   class mesh_bed_leveling {
31 33
   public:
32
-    bool active;
34
+    uint8_t status; // Has Mesh and Is Active bits
33 35
     float z_offset;
34 36
     float z_values[MESH_NUM_Y_POINTS][MESH_NUM_X_POINTS];
35 37
 
@@ -41,6 +43,11 @@
41 43
     static FORCE_INLINE float get_probe_y(int8_t i) { return MESH_MIN_Y + (MESH_Y_DIST) * i; }
42 44
     void set_z(const int8_t px, const int8_t py, const float z) { z_values[py][px] = z; }
43 45
 
46
+    bool active()                 { return TEST(status, MBL_STATUS_ACTIVE_BIT); }
47
+    void set_active(bool onOff)   { if (onOff) SBI(status, MBL_STATUS_ACTIVE_BIT); else CBI(status, MBL_STATUS_ACTIVE_BIT); }
48
+    bool has_mesh()               { return TEST(status, MBL_STATUS_HAS_MESH_BIT); }
49
+    void set_has_mesh(bool onOff) { if (onOff) SBI(status, MBL_STATUS_HAS_MESH_BIT); else CBI(status, MBL_STATUS_HAS_MESH_BIT); }
50
+
44 51
     inline void zigzag(int8_t index, int8_t &px, int8_t &py) {
45 52
       px = index % (MESH_NUM_X_POINTS);
46 53
       py = index / (MESH_NUM_X_POINTS);

+ 4
- 2
Marlin/planner.cpp View File

@@ -539,7 +539,8 @@ void Planner::check_axes_activity() {
539 539
   while (block_buffer_tail == next_buffer_head) idle();
540 540
 
541 541
   #if ENABLED(MESH_BED_LEVELING)
542
-    if (mbl.active) z += mbl.get_z(x - home_offset[X_AXIS], y - home_offset[Y_AXIS]);
542
+    if (mbl.active()) 
543
+      z += mbl.get_z(x - home_offset[X_AXIS], y - home_offset[Y_AXIS]);
543 544
   #elif ENABLED(AUTO_BED_LEVELING_FEATURE)
544 545
     apply_rotation_xyz(bed_level_matrix, x, y, z);
545 546
   #endif
@@ -1120,7 +1121,8 @@ void Planner::check_axes_activity() {
1120 1121
 #endif // AUTO_BED_LEVELING_FEATURE || MESH_BED_LEVELING
1121 1122
   {
1122 1123
     #if ENABLED(MESH_BED_LEVELING)
1123
-      if (mbl.active) z += mbl.get_z(x - home_offset[X_AXIS], y - home_offset[Y_AXIS]);
1124
+      if (mbl.active())
1125
+        z += mbl.get_z(x - home_offset[X_AXIS], y - home_offset[Y_AXIS]);
1124 1126
     #elif ENABLED(AUTO_BED_LEVELING_FEATURE)
1125 1127
       apply_rotation_xyz(bed_level_matrix, x, y, z);
1126 1128
     #endif

+ 1
- 1
Marlin/ultralcd.cpp View File

@@ -970,7 +970,7 @@ void lcd_cooldown() {
970 970
           line_to_current(Z_AXIS);
971 971
           stepper.synchronize();
972 972
 
973
-          mbl.active = true;
973
+          mbl.set_has_mesh(true);
974 974
           enqueue_and_echo_commands_P(PSTR("G28"));
975 975
           lcd_return_to_status();
976 976
           //LCD_MESSAGEPGM(MSG_LEVEL_BED_DONE);

Loading…
Cancel
Save