Skip to content

Commit

Permalink
✨ EVENT_GCODE_BEFORE_G29 (#27566)
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkyhead authored Jan 9, 2025
1 parent 7eee772 commit acd8259
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
6 changes: 6 additions & 0 deletions Marlin/Configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -2108,6 +2108,12 @@
//#define AUTO_BED_LEVELING_UBL
//#define MESH_BED_LEVELING

/**
* Commands to execute at the start of G29 probing,
* after switching to the PROBING_TOOL.
*/
//#define EVENT_GCODE_BEFORE_G29 "M300 P440 S200"

/**
* Commands to execute at the end of G29 probing.
* Useful to retract or move the Z probe out of the way.
Expand Down
23 changes: 16 additions & 7 deletions Marlin/src/feature/bedlevel/ubl/ubl_G29.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,13 @@ G29_parameters_t unified_bed_leveling::param;

void unified_bed_leveling::G29() {

bool probe_deployed = false;
#ifdef EVENT_GCODE_AFTER_G29
bool probe_deployed = false;
#define SET_PROBE_DEPLOYED(N) probe_deployed = N
#else
#define SET_PROBE_DEPLOYED(N)
#endif

if (G29_parse_parameters()) return; // Abort on parameter error

const uint8_t p_val = parser.byteval('P');
Expand All @@ -316,6 +322,11 @@ void unified_bed_leveling::G29() {
#endif
probe.use_probing_tool();

#ifdef EVENT_GCODE_BEFORE_G29
if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Before G29 G-code: ", EVENT_GCODE_BEFORE_G29);
gcode.process_subcommands_now(F(EVENT_GCODE_BEFORE_G29));
#endif

// Position bed horizontally and Z probe vertically.
#if HAS_SAFE_BED_LEVELING
xyze_pos_t safe_position = current_position;
Expand Down Expand Up @@ -430,7 +441,7 @@ void unified_bed_leveling::G29() {
do_blocking_move_to_xy(0.5f * ((MESH_MIN_X) + (MESH_MAX_X)), 0.5f * ((MESH_MIN_Y) + (MESH_MAX_Y)));
#endif
report_current_position();
probe_deployed = true;
SET_PROBE_DEPLOYED(true);
}

#endif // HAS_BED_PROBE
Expand Down Expand Up @@ -465,7 +476,7 @@ void unified_bed_leveling::G29() {
probe_entire_mesh(param.XY_pos, parser.seen_test('T'), parser.seen_test('E'), parser.seen_test('U'));

report_current_position();
probe_deployed = true;
SET_PROBE_DEPLOYED(true);
} break;

#endif // HAS_BED_PROBE
Expand Down Expand Up @@ -503,7 +514,7 @@ void unified_bed_leveling::G29() {
SERIAL_ECHOLNPGM("?Error in Business Card measurement.");
return;
}
probe_deployed = true;
SET_PROBE_DEPLOYED(true);
}

if (!position_is_reachable(param.XY_pos)) {
Expand Down Expand Up @@ -681,13 +692,11 @@ void unified_bed_leveling::G29() {
#endif

#ifdef EVENT_GCODE_AFTER_G29
if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Z Probe End Script: ", EVENT_GCODE_AFTER_G29);
if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("After G29 G-code: ", EVENT_GCODE_AFTER_G29);
if (probe_deployed) {
planner.synchronize();
gcode.process_subcommands_now(F(EVENT_GCODE_AFTER_G29));
}
#else
UNUSED(probe_deployed);
#endif

probe.use_probing_tool(false);
Expand Down
7 changes: 6 additions & 1 deletion Marlin/src/gcode/bedlevel/abl/G29.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,11 @@ G29_TYPE GcodeSuite::G29() {

probe.use_probing_tool();

#ifdef EVENT_GCODE_BEFORE_G29
if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Before G29 G-code: ", EVENT_GCODE_BEFORE_G29);
gcode.process_subcommands_now(F(EVENT_GCODE_BEFORE_G29));
#endif

#if ANY(PROBE_MANUALLY, AUTO_BED_LEVELING_LINEAR)
abl.abl_probe_index = -1;
#endif
Expand Down Expand Up @@ -1007,7 +1012,7 @@ G29_TYPE GcodeSuite::G29() {
TERN_(HAS_BED_PROBE, probe.move_z_after_probing());

#ifdef EVENT_GCODE_AFTER_G29
if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Z Probe End Script: ", EVENT_GCODE_AFTER_G29);
if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("After G29 G-code: ", EVENT_GCODE_AFTER_G29);
planner.synchronize();
process_subcommands_now(F(EVENT_GCODE_AFTER_G29));
#endif
Expand Down

0 comments on commit acd8259

Please sign in to comment.