Browse Source

Refactor 'Level Corners with Probe' (#20460)

Co-authored-by: Scott Lahteine <thinkyhead@users.noreply.github.com>
wmariz 3 years ago
parent
commit
c87c354403
No account linked to committer's email address

+ 4
- 0
Marlin/src/feature/bltouch.h View File

@@ -23,6 +23,10 @@
23 23
 
24 24
 #include "../inc/MarlinConfigPre.h"
25 25
 
26
+#if DISABLED(BLTOUCH_HS_MODE)
27
+  #define BLTOUCH_SLOW_MODE 1
28
+#endif
29
+
26 30
 // BLTouch commands are sent as servo angles
27 31
 typedef unsigned char BLTCommand;
28 32
 

+ 131
- 130
Marlin/src/lcd/menu/menu_bed_corners.cpp View File

@@ -51,18 +51,11 @@
51 51
     #include "../../feature/bltouch.h"
52 52
   #endif
53 53
   #ifndef LEVEL_CORNERS_PROBE_TOLERANCE
54
-    #define LEVEL_CORNERS_PROBE_TOLERANCE 0.1
54
+    #define LEVEL_CORNERS_PROBE_TOLERANCE 0.2
55 55
   #endif
56
-  #if ENABLED(LEVEL_CORNERS_AUDIO_FEEDBACK)
57
-    #include "../../libs/buzzer.h"
58
-    #define PROBE_BUZZ() BUZZ(200, 600)
59
-  #else
60
-    #define PROBE_BUZZ() NOOP
61
-  #endif
62
-  static float last_z;
63
-  static bool corner_probing_done;
64
-  static bool verify_corner;
65
-  static int good_points;
56
+  float last_z;
57
+  int good_points;
58
+  bool corner_probing_done, wait_for_probe;
66 59
 #endif
67 60
 
68 61
 static_assert(LEVEL_CORNERS_Z_HOP >= 0, "LEVEL_CORNERS_Z_HOP must be >= 0. Please update your configuration.");
@@ -75,115 +68,130 @@ extern const char G28_STR[];
75 68
 
76 69
 static int8_t bed_corner;
77 70
 
71
+constexpr float inset_lfrb[4] = LEVEL_CORNERS_INSET_LFRB;
72
+constexpr xy_pos_t lf { (X_MIN_BED) + inset_lfrb[0], (Y_MIN_BED) + inset_lfrb[1] },
73
+                   rb { (X_MAX_BED) - inset_lfrb[2], (Y_MAX_BED) - inset_lfrb[3] };
74
+
78 75
 /**
79 76
  * Level corners, starting in the front-left corner.
80 77
  */
81 78
 #if ENABLED(LEVEL_CORNERS_USE_PROBE)
82 79
 
83
-  static inline void _lcd_level_bed_corners_probing() {
84
-    ui.goto_screen([]{ MenuItem_static::draw((LCD_HEIGHT - 1) / 2, GET_TEXT(MSG_PROBING_MESH)); });
80
+  void _lcd_draw_probing() {
81
+    if (ui.should_draw()) MenuItem_static::draw((LCD_HEIGHT - 1) / 2, GET_TEXT(MSG_PROBING_MESH));
82
+  }
85 83
 
86
-    float lfrb[4] = LEVEL_CORNERS_INSET_LFRB;
87
-    xy_pos_t lf { (X_MIN_BED) + lfrb[0] - probe.offset_xy.x , (Y_MIN_BED) + lfrb[1] - probe.offset_xy.y },
88
-             rb { (X_MAX_BED) - lfrb[2] - probe.offset_xy.x , (Y_MAX_BED) - lfrb[3] - probe.offset_xy.y };
84
+  void _lcd_draw_raise() {
85
+    if (!ui.should_draw()) return;
86
+    MenuItem_confirm::select_screen(
87
+      GET_TEXT(MSG_BUTTON_DONE), GET_TEXT(MSG_BUTTON_SKIP)
88
+      , []{ corner_probing_done = true; wait_for_probe = false; }
89
+      , []{ wait_for_probe = false; }
90
+      , GET_TEXT(MSG_LEVEL_CORNERS_RAISE)
91
+      , (const char*)nullptr, PSTR("")
92
+    );
93
+  }
89 94
 
90
-    do_blocking_move_to_z(LEVEL_CORNERS_Z_HOP - probe.offset.z);
95
+  void _lcd_draw_level_prompt() {
96
+    if (!ui.should_draw()) return;
97
+    MenuItem_confirm::confirm_screen(
98
+      []{ queue.inject_P(TERN(HAS_LEVELING, PSTR("G28\nG29"), G28_STR));
99
+          ui.return_to_status();
100
+      }
101
+      , []{ ui.goto_previous_screen_no_defer(); }
102
+      , GET_TEXT(MSG_LEVEL_CORNERS_IN_RANGE)
103
+      , (const char*)nullptr, PSTR("?")
104
+    );
105
+  }
91 106
 
92
-    switch (bed_corner) {
93
-      case 0: current_position   = lf;   break; // copy xy
94
-      case 1: current_position.x = rb.x; break;
95
-      case 2: current_position.y = rb.y; break;
96
-      case 3: current_position.x = lf.x; break;
97
-      #if ENABLED(LEVEL_CENTER_TOO)
98
-        case 4: current_position.set(X_CENTER - probe.offset_xy.x, Y_CENTER - probe.offset_xy.y); good_points--; break;
99
-      #endif
107
+  bool _lcd_level_bed_corners_probe(bool verify=false) {
108
+    if (verify) do_blocking_move_to_z(current_position.z + LEVEL_CORNERS_Z_HOP); // do clearance if needed
109
+    TERN_(BLTOUCH_SLOW_MODE, bltouch.deploy()); // Deploy in LOW SPEED MODE on every probe action
110
+    do_blocking_move_to_z(last_z - LEVEL_CORNERS_PROBE_TOLERANCE, manual_feedrate_mm_s.z); // Move down to lower tolerance
111
+    if (TEST(endstops.trigger_state(), TERN(Z_MIN_PROBE_USES_Z_MIN_ENDSTOP_PIN, Z_MIN, Z_MIN_PROBE))) { // check if probe triggered
112
+      endstops.hit_on_purpose();
113
+      set_current_from_steppers_for_axis(Z_AXIS);
114
+      sync_plan_position();
115
+      TERN_(BLTOUCH_SLOW_MODE, bltouch.stow()); // Stow in LOW SPEED MODE on every trigger
116
+      // Triggered outside tolerance range?
117
+      if (ABS(current_position.z - last_z) > LEVEL_CORNERS_PROBE_TOLERANCE) {
118
+        last_z = current_position.z; // Above tolerance. Set a new Z for subsequent corners.
119
+        good_points = 0;             // ...and start over
120
+      }
121
+      return true; // probe triggered
100 122
     }
123
+    do_blocking_move_to_z(last_z); // go back to tolerance middle point before raise
124
+    return false; // probe not triggered
125
+  }
101 126
 
102
-    do_blocking_move_to_xy(current_position);
103
-
104
-    #if ENABLED(BLTOUCH) && DISABLED(BLTOUCH_HS_MODE)
105
-      bltouch.deploy(); // DEPLOY in LOW SPEED MODE on every probe action
106
-    #endif
107
-    TERN_(HAS_QUIET_PROBING, probe.set_probing_paused(true));
108
-
109
-    // Move down until the probe is triggered
110
-    do_blocking_move_to_z(last_z - (LEVEL_CORNERS_PROBE_TOLERANCE), manual_feedrate_mm_s.z);
111
-
112
-    // Check to see if the probe was triggered
113
-    bool probe_triggered = TEST(endstops.trigger_state(), TERN(Z_MIN_PROBE_USES_Z_MIN_ENDSTOP_PIN, Z_MIN, Z_MIN_PROBE));
114
-    if (!probe_triggered) {
115
-
116
-      static bool wait_for_probe;
117
-
118
-      ui.goto_screen([]{
119
-        MenuItem_confirm::select_screen(
120
-          GET_TEXT(MSG_BUTTON_DONE), GET_TEXT(MSG_BUTTON_SKIP)
121
-          , []{ corner_probing_done = true;
122
-                wait_for_probe = false;
123
-                TERN_(HAS_LEVELING, set_bed_leveling_enabled(leveling_was_active));
124
-                ui.goto_previous_screen_no_defer();
125
-            }
126
-          , []{ wait_for_probe = false; }
127
-          , GET_TEXT(MSG_LEVEL_CORNERS_RAISE)
128
-          , (const char*)nullptr, PSTR("")
129
-        );
130
-      });
131
-      ui.set_selection(true);
132
-
133
-      wait_for_probe = true;
134
-      while (wait_for_probe && !probe_triggered) {
135
-        probe_triggered = PROBE_TRIGGERED();
136
-        if (probe_triggered) PROBE_BUZZ();
137
-        idle();
127
+  bool _lcd_level_bed_corners_raise() {
128
+    bool probe_triggered = false;
129
+    corner_probing_done = false;
130
+    wait_for_probe = true;
131
+    ui.goto_screen(_lcd_draw_raise); // show raise screen
132
+    ui.set_selection(true);
133
+    while (wait_for_probe && !probe_triggered) { //loop while waiting to bed raise and probe trigger
134
+      probe_triggered = PROBE_TRIGGERED();
135
+      if (probe_triggered) {
136
+        endstops.hit_on_purpose();
137
+        TERN_(LEVEL_CORNERS_AUDIO_FEEDBACK, ui.buzz(200, 600));
138 138
       }
139
-      wait_for_probe = false;
140
-
141
-      TERN_(LEVEL_CORNERS_VERIFY_RAISED, verify_corner = true);
139
+      idle();
142 140
     }
141
+    TERN_(BLTOUCH_SLOW_MODE, bltouch.stow());
142
+    ui.goto_screen(_lcd_draw_probing);
143
+    return (probe_triggered);
144
+  }
143 145
 
144
-    TERN_(HAS_QUIET_PROBING, probe.set_probing_paused(false));
145
-
146
-    #if ENABLED(BLTOUCH) && DISABLED(BLTOUCH_HS_MODE)
147
-      bltouch.stow();
148
-    #endif
146
+  void _lcd_test_corners() {
147
+    ui.goto_screen(_lcd_draw_probing);
148
+    bed_corner = TERN(LEVEL_CENTER_TOO, 4, 0);
149
+    last_z = LEVEL_CORNERS_HEIGHT;
150
+    endstops.enable_z_probe(true);
151
+    good_points = 0;
149 152
 
150
-    if (probe_triggered) {
151
-      endstops.hit_on_purpose();
152
-      if (!WITHIN(current_position.z, last_z - (LEVEL_CORNERS_PROBE_TOLERANCE), last_z + (LEVEL_CORNERS_PROBE_TOLERANCE))) {
153
-        last_z = current_position.z;
154
-        good_points = 0;
153
+    do {
154
+      do_blocking_move_to_z(current_position.z + LEVEL_CORNERS_Z_HOP); // clearance
155
+      // Select next corner coordinates
156
+      xy_pos_t plf = lf - probe.offset_xy, prb = rb - probe.offset_xy;
157
+      switch (bed_corner) {
158
+        case 0: current_position   = plf;   break; // copy xy
159
+        case 1: current_position.x = prb.x; break;
160
+        case 2: current_position.y = prb.y; break;
161
+        case 3: current_position.x = plf.x; break;
162
+        #if ENABLED(LEVEL_CENTER_TOO)
163
+          case 4: current_position.set(X_CENTER - probe.offset_xy.x, Y_CENTER - probe.offset_xy.y); break;
164
+        #endif
155 165
       }
156
-      if (!verify_corner) good_points++;
157
-    }
158
-
159
-    if (!corner_probing_done) {
160
-      if (!verify_corner) bed_corner++;
161
-      if (bed_corner > 3) bed_corner = 0;
162
-      verify_corner = false;
163
-      if (good_points < 4)
164
-        _lcd_level_bed_corners_probing();
165
-      else {
166
-        ui.goto_screen([]{
167
-          MenuItem_confirm::confirm_screen(
168
-            []{ ui.goto_previous_screen_no_defer();
169
-                queue.inject_P(TERN(HAS_LEVELING, PSTR("G28\nG29"), G28_STR));
166
+      do_blocking_move_to_xy(current_position);           // Goto corner
167
+
168
+      if (!_lcd_level_bed_corners_probe()) {              // Probe down to tolerance
169
+        if (_lcd_level_bed_corners_raise()) {             // Prompt user to raise bed if needed
170
+          #if ENABLED(LEVEL_CORNERS_VERIFY_RAISED)        // Verify
171
+            while (!_lcd_level_bed_corners_probe(true)) { // Loop while corner verified
172
+              if (!_lcd_level_bed_corners_raise()) {      // Prompt user to raise bed if needed
173
+                if (corner_probing_done) return;          // Done was selected
174
+                break;                                    // Skip was selected
170 175
               }
171
-            , []{ ui.goto_previous_screen_no_defer(); }
172
-            , GET_TEXT(MSG_LEVEL_CORNERS_IN_RANGE)
173
-            , (const char*)nullptr, PSTR("?")
174
-          );
175
-        });
176
-        ui.set_selection(true);
176
+            }
177
+          #endif
178
+        }
179
+        else if (corner_probing_done)                     // Done was selected
180
+          return;
177 181
       }
178
-    }
182
+
183
+      if (bed_corner != 4) good_points++; // ignore center
184
+      if (++bed_corner > 3) bed_corner = 0;
185
+
186
+    } while (good_points < 4); // loop until all corners whitin tolerance
187
+
188
+    ui.goto_screen(_lcd_draw_level_prompt); // prompt for bed leveling
189
+    ui.set_selection(true);
179 190
   }
180 191
 
181
-#else
192
+#else // !LEVEL_CORNERS_USE_PROBE
182 193
 
183 194
   static inline void _lcd_goto_next_corner() {
184
-    constexpr float lfrb[4] = LEVEL_CORNERS_INSET_LFRB;
185
-    constexpr xy_pos_t lf { (X_MIN_BED) + lfrb[0], (Y_MIN_BED) + lfrb[1] },
186
-                       rb { (X_MAX_BED) - lfrb[2], (Y_MAX_BED) - lfrb[3] };
187 195
     line_to_z(LEVEL_CORNERS_Z_HOP);
188 196
     switch (bed_corner) {
189 197
       case 0: current_position   = lf;   break; // copy xy
@@ -199,33 +207,33 @@ static int8_t bed_corner;
199 207
     if (++bed_corner > 3 + ENABLED(LEVEL_CENTER_TOO)) bed_corner = 0;
200 208
   }
201 209
 
202
-#endif
210
+#endif // !LEVEL_CORNERS_USE_PROBE
203 211
 
204 212
 static inline void _lcd_level_bed_corners_homing() {
205 213
   _lcd_draw_homing();
206
-  if (all_axes_homed()) {
207
-    #if ENABLED(LEVEL_CORNERS_USE_PROBE)
208
-      TERN_(LEVEL_CENTER_TOO, bed_corner = 4);
209
-      endstops.enable_z_probe(true);
210
-      ui.goto_screen(_lcd_level_bed_corners_probing);
211
-    #else
212
-      bed_corner = 0;
213
-      ui.goto_screen([]{
214
-        MenuItem_confirm::select_screen(
215
-            GET_TEXT(MSG_BUTTON_NEXT), GET_TEXT(MSG_BUTTON_DONE)
216
-          , _lcd_goto_next_corner
217
-          , []{
218
-              TERN_(HAS_LEVELING, set_bed_leveling_enabled(leveling_was_active));
219
-              ui.goto_previous_screen_no_defer();
220
-            }
221
-          , GET_TEXT(TERN(LEVEL_CENTER_TOO, MSG_LEVEL_BED_NEXT_POINT, MSG_NEXT_CORNER))
222
-          , (const char*)nullptr, PSTR("?")
223
-        );
224
-      });
225
-      ui.set_selection(true);
226
-      _lcd_goto_next_corner();
227
-    #endif
228
-  }
214
+  if (!all_axes_homed()) return;
215
+  #if ENABLED(LEVEL_CORNERS_USE_PROBE)
216
+    _lcd_test_corners();
217
+    if (corner_probing_done) ui.goto_previous_screen_no_defer();
218
+    TERN_(HAS_LEVELING, set_bed_leveling_enabled(leveling_was_active));
219
+    endstops.enable_z_probe(false);
220
+  #else
221
+    bed_corner = 0;
222
+    ui.goto_screen([]{
223
+      MenuItem_confirm::select_screen(
224
+          GET_TEXT(MSG_BUTTON_NEXT), GET_TEXT(MSG_BUTTON_DONE)
225
+        , _lcd_goto_next_corner
226
+        , []{
227
+            TERN_(HAS_LEVELING, set_bed_leveling_enabled(leveling_was_active));
228
+            ui.goto_previous_screen_no_defer();
229
+          }
230
+        , GET_TEXT(TERN(LEVEL_CENTER_TOO, MSG_LEVEL_BED_NEXT_POINT, MSG_NEXT_CORNER))
231
+        , (const char*)nullptr, PSTR("?")
232
+      );
233
+    });
234
+    ui.set_selection(true);
235
+    _lcd_goto_next_corner();
236
+  #endif
229 237
 }
230 238
 
231 239
 void _lcd_level_bed_corners() {
@@ -241,13 +249,6 @@ void _lcd_level_bed_corners() {
241 249
     set_bed_leveling_enabled(false);
242 250
   #endif
243 251
 
244
-  #if ENABLED(LEVEL_CORNERS_USE_PROBE)
245
-    last_z = LEVEL_CORNERS_HEIGHT;
246
-    corner_probing_done = false;
247
-    verify_corner = false;
248
-    good_points = 0;
249
-  #endif
250
-
251 252
   ui.goto_screen(_lcd_level_bed_corners_homing);
252 253
 }
253 254
 

+ 2
- 2
Marlin/src/module/motion.cpp View File

@@ -1604,7 +1604,7 @@ void homeaxis(const AxisEnum axis) {
1604 1604
 
1605 1605
   do_homing_move(axis, 1.5f * max_length(TERN(DELTA, Z_AXIS, axis)) * axis_home_dir);
1606 1606
 
1607
-  #if BOTH(HOMING_Z_WITH_PROBE, BLTOUCH) && DISABLED(BLTOUCH_HS_MODE)
1607
+  #if BOTH(HOMING_Z_WITH_PROBE, BLTOUCH_SLOW_MODE)
1608 1608
     if (axis == Z_AXIS) bltouch.stow(); // Intermediate STOW (in LOW SPEED MODE)
1609 1609
   #endif
1610 1610
 
@@ -1642,7 +1642,7 @@ void homeaxis(const AxisEnum axis) {
1642 1642
     // Slow move towards endstop until triggered
1643 1643
     if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Home 2 Slow:");
1644 1644
 
1645
-    #if BOTH(HOMING_Z_WITH_PROBE, BLTOUCH) && DISABLED(BLTOUCH_HS_MODE)
1645
+    #if BOTH(HOMING_Z_WITH_PROBE, BLTOUCH_SLOW_MODE)
1646 1646
       if (axis == Z_AXIS && bltouch.deploy()) return; // Intermediate DEPLOY (in LOW SPEED MODE)
1647 1647
     #endif
1648 1648
 

+ 5
- 7
Marlin/src/module/probe.cpp View File

@@ -455,9 +455,7 @@ bool Probe::probe_down_to_z(const float z, const feedRate_t fr_mm_s) {
455 455
     thermalManager.wait_for_bed_heating();
456 456
   #endif
457 457
 
458
-  #if ENABLED(BLTOUCH) && DISABLED(BLTOUCH_HS_MODE)
459
-    if (bltouch.deploy()) return true; // DEPLOY in LOW SPEED MODE on every probe action
460
-  #endif
458
+  if (TERN0(BLTOUCH_SLOW_MODE, bltouch.deploy())) return true; // Deploy in LOW SPEED MODE on every probe action
461 459
 
462 460
   // Disable stealthChop if used. Enable diag1 pin on driver.
463 461
   #if ENABLED(SENSORLESS_PROBING)
@@ -496,9 +494,8 @@ bool Probe::probe_down_to_z(const float z, const feedRate_t fr_mm_s) {
496 494
     tmc_disable_stallguard(stepperZ, stealth_states.z);
497 495
   #endif
498 496
 
499
-  #if ENABLED(BLTOUCH) && DISABLED(BLTOUCH_HS_MODE)
500
-    if (probe_triggered && bltouch.stow()) return true; // STOW in LOW SPEED MODE on trigger on every probe action
501
-  #endif
497
+  if (probe_triggered && TERN0(BLTOUCH_SLOW_MODE, bltouch.stow())) // Stow in LOW SPEED MODE on every trigger
498
+    return true;
502 499
 
503 500
   // Clear endstop flags
504 501
   endstops.hit_on_purpose();
@@ -578,9 +575,10 @@ float Probe::run_z_probe(const bool sanity_check/*=true*/) {
578 575
   // Double-probing does a fast probe followed by a slow probe
579 576
   #if TOTAL_PROBING == 2
580 577
 
581
-    // Do a first probe at the fast speed
578
+    // Attempt to tare the probe
582 579
     if (TERN0(PROBE_TARE, tare())) return NAN;
583 580
 
581
+    // Do a first probe at the fast speed
584 582
     if (try_to_probe(PSTR("FAST"), z_probe_low_point, z_probe_fast_mm_s,
585 583
                      sanity_check, Z_CLEARANCE_BETWEEN_PROBES) ) return NAN;
586 584
 

Loading…
Cancel
Save