Skip to content

Commit b07a34e

Browse files
committed
🐛 Fix M_State_grbl when G29 calls G28
1 parent fd1a5fe commit b07a34e

File tree

4 files changed

+36
-18
lines changed

4 files changed

+36
-18
lines changed

Marlin/src/gcode/bedlevel/abl/G29.cpp

+23-10
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,12 @@
7777
#endif
7878
#endif
7979

80-
#define G29_RETURN(b) return TERN_(G29_RETRY_AND_RECOVER, b)
80+
#define G29_RETURN(retry) do{ \
81+
if (TERN(G29_RETRY_AND_RECOVER, !retry, true)) { \
82+
TERN_(FULL_REPORT_TO_HOST_FEATURE, set_and_report_grblstate(M_IDLE, false)); \
83+
} \
84+
return TERN_(G29_RETRY_AND_RECOVER, retry); \
85+
}while(0)
8186

8287
// For manual probing values persist over multiple G29
8388
class G29_State {
@@ -218,12 +223,13 @@ class G29_State {
218223
G29_TYPE GcodeSuite::G29() {
219224
DEBUG_SECTION(log_G29, "G29", DEBUGGING(LEVELING));
220225

226+
// Leveling state is persistent when done manually with multiple G29 commands
221227
TERN_(PROBE_MANUALLY, static) G29_State abl;
222228

223-
TERN_(FULL_REPORT_TO_HOST_FEATURE, set_and_report_grblstate(M_PROBE));
224-
229+
// Keep powered steppers from timing out
225230
reset_stepper_timeout();
226231

232+
// Q = Query leveling and G29 state
227233
const bool seenQ = EITHER(DEBUG_LEVELING_FEATURE, PROBE_MANUALLY) && parser.seen_test('Q');
228234

229235
// G29 Q is also available if debugging
@@ -232,11 +238,14 @@ G29_TYPE GcodeSuite::G29() {
232238
if (DISABLED(PROBE_MANUALLY) && seenQ) G29_RETURN(false);
233239
#endif
234240

241+
// A = Abort manual probing
242+
// C<bool> = Generate fake probe points (DEBUG_LEVELING_FEATURE)
235243
const bool seenA = TERN0(PROBE_MANUALLY, parser.seen_test('A')),
236244
no_action = seenA || seenQ,
237245
faux = ENABLED(DEBUG_LEVELING_FEATURE) && DISABLED(PROBE_MANUALLY) ? parser.boolval('C') : no_action;
238246

239-
if (!no_action && planner.leveling_active && parser.boolval('O')) { // Auto-level only if needed
247+
// O = Don't level if leveling is already active
248+
if (!no_action && planner.leveling_active && parser.boolval('O')) {
240249
if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("> Auto-level not needed, skip");
241250
G29_RETURN(false);
242251
}
@@ -248,15 +257,20 @@ G29_TYPE GcodeSuite::G29() {
248257
// Don't allow auto-leveling without homing first
249258
if (homing_needed_error()) G29_RETURN(false);
250259

260+
// 3-point leveling gets points from the probe class
251261
#if ENABLED(AUTO_BED_LEVELING_3POINT)
252262
vector_3 points[3];
253263
probe.get_three_points(points);
254264
#endif
255265

266+
// Storage for ABL Linear results
256267
#if ENABLED(AUTO_BED_LEVELING_LINEAR)
257268
struct linear_fit_data lsf_results;
258269
#endif
259270

271+
// Set and report "probing" state to host
272+
TERN_(FULL_REPORT_TO_HOST_FEATURE, set_and_report_grblstate(M_PROBE, false));
273+
260274
/**
261275
* On the initial G29 fetch command parameters.
262276
*/
@@ -429,10 +443,10 @@ G29_TYPE GcodeSuite::G29() {
429443
if (!no_action) set_bed_leveling_enabled(false);
430444

431445
// Deploy certain probes before starting probing
432-
#if HAS_BED_PROBE
433-
if (ENABLED(BLTOUCH))
434-
do_z_clearance(Z_CLEARANCE_DEPLOY_PROBE);
435-
else if (probe.deploy()) {
446+
#if ENABLED(BLTOUCH)
447+
do_z_clearance(Z_CLEARANCE_DEPLOY_PROBE);
448+
#elif HAS_BED_PROBE
449+
if (probe.deploy()) { // (returns true on deploy failure)
436450
set_bed_leveling_enabled(abl.reenable);
437451
G29_RETURN(false);
438452
}
@@ -483,6 +497,7 @@ G29_TYPE GcodeSuite::G29() {
483497
SERIAL_ECHOLNPGM("idle");
484498
}
485499

500+
// For 'A' or 'Q' exit with success state
486501
if (no_action) G29_RETURN(false);
487502

488503
if (abl.abl_probe_index == 0) {
@@ -893,8 +908,6 @@ G29_TYPE GcodeSuite::G29() {
893908

894909
report_current_position();
895910

896-
TERN_(FULL_REPORT_TO_HOST_FEATURE, set_and_report_grblstate(M_IDLE));
897-
898911
G29_RETURN(isnan(abl.measured_z));
899912

900913
}

Marlin/src/gcode/bedlevel/mbl/G29.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,6 @@ void GcodeSuite::G29() {
7575
}
7676
#endif
7777

78-
TERN_(FULL_REPORT_TO_HOST_FEATURE, set_and_report_grblstate(M_PROBE));
79-
8078
static int mbl_probe_index = -1;
8179

8280
MeshLevelingState state = (MeshLevelingState)parser.byteval('S', (int8_t)MeshReport);
@@ -85,6 +83,8 @@ void GcodeSuite::G29() {
8583
return;
8684
}
8785

86+
TERN_(FULL_REPORT_TO_HOST_FEATURE, set_and_report_grblstate(M_PROBE));
87+
8888
int8_t ix, iy;
8989
ix = iy = 0;
9090

Marlin/src/gcode/calibrate/G28.cpp

+6-3
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,6 @@ void GcodeSuite::G28() {
213213

214214
TERN_(LASER_MOVE_G28_OFF, cutter.set_inline_enabled(false)); // turn off laser
215215

216-
TERN_(FULL_REPORT_TO_HOST_FEATURE, set_and_report_grblstate(M_HOMING));
217-
218216
#if ENABLED(DUAL_X_CARRIAGE)
219217
bool IDEX_saved_duplication_state = extruder_duplication_enabled;
220218
DualXMode IDEX_saved_mode = dual_x_carriage_mode;
@@ -236,6 +234,11 @@ void GcodeSuite::G28() {
236234
return;
237235
}
238236

237+
#if ENABLED(FULL_REPORT_TO_HOST_FEATURE)
238+
const M_StateEnum old_grblstate = M_State_grbl;
239+
set_and_report_grblstate(M_HOMING);
240+
#endif
241+
239242
TERN_(HAS_DWIN_E3V2_BASIC, DWIN_StartHoming());
240243
TERN_(EXTENSIBLE_UI, ExtUI::onHomingStart());
241244

@@ -557,7 +560,7 @@ void GcodeSuite::G28() {
557560
if (ENABLED(NANODLP_Z_SYNC) && (doZ || ENABLED(NANODLP_ALL_AXIS)))
558561
SERIAL_ECHOLNPGM(STR_Z_MOVE_COMP);
559562

560-
TERN_(FULL_REPORT_TO_HOST_FEATURE, set_and_report_grblstate(M_IDLE));
563+
TERN_(FULL_REPORT_TO_HOST_FEATURE, set_and_report_grblstate(old_grblstate));
561564

562565
#if HAS_L64XX
563566
// Set L6470 absolute position registers to counts

Marlin/src/module/motion.h

+5-3
Original file line numberDiff line numberDiff line change
@@ -265,9 +265,11 @@ void report_current_position_projected();
265265
void report_current_position_moving();
266266

267267
#if ENABLED(FULL_REPORT_TO_HOST_FEATURE)
268-
inline void set_and_report_grblstate(const M_StateEnum state) {
269-
M_State_grbl = state;
270-
report_current_grblstate_moving();
268+
inline void set_and_report_grblstate(const M_StateEnum state, const bool force=true) {
269+
if (force || M_State_grbl != state) {
270+
M_State_grbl = state;
271+
report_current_grblstate_moving();
272+
}
271273
}
272274
#endif
273275

0 commit comments

Comments
 (0)