Skip to content

Commit f2f7a6a

Browse files
ansonlthinkyhead
andcommitted
✨ TC_GCODE_USE_GLOBAL_* (MarlinFirmware#25399)
Co-authored-by: Scott Lahteine <[email protected]>
1 parent b304e56 commit f2f7a6a

File tree

5 files changed

+80
-18
lines changed

5 files changed

+80
-18
lines changed

Marlin/Configuration_adv.h

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2556,9 +2556,17 @@
25562556
* Extra G-code to run while executing tool-change commands. Can be used to use an additional
25572557
* stepper motor (e.g., I axis in Configuration.h) to drive the tool-changer.
25582558
*/
2559-
//#define EVENT_GCODE_TOOLCHANGE_T0 "G28 A\nG1 A0" // Extra G-code to run while executing tool-change command T0
2560-
//#define EVENT_GCODE_TOOLCHANGE_T1 "G1 A10" // Extra G-code to run while executing tool-change command T1
2561-
//#define EVENT_GCODE_TOOLCHANGE_ALWAYS_RUN // Always execute above G-code sequences. Use with caution!
2559+
//#define EVENT_GCODE_TOOLCHANGE_T0 "G28 A\nG1 A0" // Extra G-code to run while executing tool-change command T0
2560+
//#define EVENT_GCODE_TOOLCHANGE_T1 "G1 A10" // Extra G-code to run while executing tool-change command T1
2561+
//#define EVENT_GCODE_TOOLCHANGE_ALWAYS_RUN // Always execute above G-code sequences. Use with caution!
2562+
2563+
/**
2564+
* Consider coordinates for EVENT_GCODE_TOOLCHANGE_Tx as relative to T0
2565+
* so that moves in the specified axes are the same for all tools.
2566+
*/
2567+
//#define TC_GCODE_USE_GLOBAL_X // Use X position relative to Tool 0
2568+
//#define TC_GCODE_USE_GLOBAL_Y // Use Y position relative to Tool 0
2569+
//#define TC_GCODE_USE_GLOBAL_Z // Use Z position relative to Tool 0
25622570

25632571
/**
25642572
* Tool Sensors detect when tools have been picked up or dropped.

Marlin/src/inc/Conditionals_adv.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1152,3 +1152,10 @@
11521152
#if EITHER(INPUT_SHAPING_X, INPUT_SHAPING_Y)
11531153
#define HAS_SHAPING 1
11541154
#endif
1155+
1156+
// Toolchange Event G-code
1157+
#if !HAS_MULTI_EXTRUDER || !(defined(EVENT_GCODE_TOOLCHANGE_T0) || defined(EVENT_GCODE_TOOLCHANGE_T1) || defined(EVENT_GCODE_TOOLCHANGE_T2) || defined(EVENT_GCODE_TOOLCHANGE_T3) || defined(EVENT_GCODE_TOOLCHANGE_T4) || defined(EVENT_GCODE_TOOLCHANGE_T5) || defined(EVENT_GCODE_TOOLCHANGE_T6) || defined(EVENT_GCODE_TOOLCHANGE_T7))
1158+
#undef TC_GCODE_USE_GLOBAL_X
1159+
#undef TC_GCODE_USE_GLOBAL_Y
1160+
#undef TC_GCODE_USE_GLOBAL_Z
1161+
#endif

Marlin/src/inc/SanityCheck.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,9 +1015,15 @@ static_assert(X_MAX_LENGTH >= X_BED_SIZE, "Movement bounds (X_MIN_POS, X_MAX_POS
10151015
#endif
10161016
#endif
10171017

1018+
/**
1019+
* Custom Event G-code
1020+
*/
10181021
#if defined(EVENT_GCODE_SD_ABORT) && DISABLED(NOZZLE_PARK_FEATURE)
10191022
static_assert(nullptr == strstr(EVENT_GCODE_SD_ABORT, "G27"), "NOZZLE_PARK_FEATURE is required to use G27 in EVENT_GCODE_SD_ABORT.");
10201023
#endif
1024+
#if ANY(TC_GCODE_USE_GLOBAL_X, TC_GCODE_USE_GLOBAL_Y, TC_GCODE_USE_GLOBAL_Z) && ENABLED(NO_WORKSPACE_OFFSETS)
1025+
#error "TC_GCODE_USE_GLOBAL_* options are incompatible with NO_WORKSPACE_OFFSETS."
1026+
#endif
10211027

10221028
/**
10231029
* I2C Position Encoders

Marlin/src/module/tool_change.cpp

Lines changed: 55 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "temperature.h"
3131

3232
#include "../MarlinCore.h"
33+
#include "../gcode/gcode.h"
3334

3435
//#define DEBUG_TOOL_CHANGE
3536
//#define DEBUG_TOOLCHANGE_FILAMENT_SWAP
@@ -49,12 +50,6 @@
4950
Flags<EXTRUDERS> toolchange_extruder_ready;
5051
#endif
5152

52-
#if EITHER(MAGNETIC_PARKING_EXTRUDER, TOOL_SENSOR) \
53-
|| defined(EVENT_GCODE_TOOLCHANGE_T0) || defined(EVENT_GCODE_TOOLCHANGE_T1) || defined(EVENT_GCODE_AFTER_TOOLCHANGE) \
54-
|| (ENABLED(PARKING_EXTRUDER) && PARKING_EXTRUDER_SOLENOIDS_DELAY > 0)
55-
#include "../gcode/gcode.h"
56-
#endif
57-
5853
#if ENABLED(TOOL_SENSOR)
5954
#include "../lcd/marlinui.h"
6055
#endif
@@ -98,7 +93,6 @@
9893
#endif
9994

10095
#if ENABLED(TOOLCHANGE_FILAMENT_SWAP)
101-
#include "../gcode/gcode.h"
10296
#if TOOLCHANGE_FS_WIPE_RETRACT <= 0
10397
#undef TOOLCHANGE_FS_WIPE_RETRACT
10498
#define TOOLCHANGE_FS_WIPE_RETRACT 0
@@ -986,7 +980,7 @@ void fast_line_to_current(const AxisEnum fr_axis) { _line_to_current(fr_axis, 0.
986980
}
987981
#endif
988982

989-
//Calculate and perform the priming distance
983+
// Calculate and perform the priming distance
990984
if (toolchange_settings.extra_prime >= 0) {
991985
// Positive extra_prime value
992986
// - Return filament at speed (fr) then extra_prime at prime speed
@@ -1409,14 +1403,61 @@ void tool_change(const uint8_t new_tool, bool no_move/*=false*/) {
14091403
TERN_(HAS_FANMUX, fanmux_switch(active_extruder));
14101404

14111405
if (ENABLED(EVENT_GCODE_TOOLCHANGE_ALWAYS_RUN) || !no_move) {
1412-
#ifdef EVENT_GCODE_TOOLCHANGE_T0
1413-
if (new_tool == 0)
1414-
gcode.process_subcommands_now(F(EVENT_GCODE_TOOLCHANGE_T0));
1406+
1407+
#if ANY(TC_GCODE_USE_GLOBAL_X, TC_GCODE_USE_GLOBAL_Y, TC_GCODE_USE_GLOBAL_Z)
1408+
// G0/G1/G2/G3/G5 moves are relative to the active tool.
1409+
// Shift the workspace to make custom moves relative to T0.
1410+
xyz_pos_t old_position_shift;
1411+
if (new_tool > 0) {
1412+
old_position_shift = position_shift;
1413+
const xyz_pos_t &he = hotend_offset[new_tool];
1414+
#if ENABLED(TC_GCODE_USE_GLOBAL_X)
1415+
position_shift.x -= he.x; update_workspace_offset(X_AXIS);
1416+
#endif
1417+
#if ENABLED(TC_GCODE_USE_GLOBAL_Y)
1418+
position_shift.y -= he.y; update_workspace_offset(Y_AXIS);
1419+
#endif
1420+
#if ENABLED(TC_GCODE_USE_GLOBAL_Z)
1421+
position_shift.z -= he.z; update_workspace_offset(Z_AXIS);
1422+
#endif
1423+
}
14151424
#endif
14161425

1417-
#ifdef EVENT_GCODE_TOOLCHANGE_T1
1418-
if (new_tool == 1)
1419-
gcode.process_subcommands_now(F(EVENT_GCODE_TOOLCHANGE_T1));
1426+
switch (new_tool) {
1427+
default: break;
1428+
#ifdef EVENT_GCODE_TOOLCHANGE_T0
1429+
case 0: gcode.process_subcommands_now(F(EVENT_GCODE_TOOLCHANGE_T0)); break;
1430+
#endif
1431+
#ifdef EVENT_GCODE_TOOLCHANGE_T1
1432+
case 1: gcode.process_subcommands_now(F(EVENT_GCODE_TOOLCHANGE_T1)); break;
1433+
#endif
1434+
#ifdef EVENT_GCODE_TOOLCHANGE_T2
1435+
case 2: gcode.process_subcommands_now(F(EVENT_GCODE_TOOLCHANGE_T2)); break;
1436+
#endif
1437+
#ifdef EVENT_GCODE_TOOLCHANGE_T3
1438+
case 3: gcode.process_subcommands_now(F(EVENT_GCODE_TOOLCHANGE_T3)); break;
1439+
#endif
1440+
#ifdef EVENT_GCODE_TOOLCHANGE_T4
1441+
case 4: gcode.process_subcommands_now(F(EVENT_GCODE_TOOLCHANGE_T4)); break;
1442+
#endif
1443+
#ifdef EVENT_GCODE_TOOLCHANGE_T5
1444+
case 5: gcode.process_subcommands_now(F(EVENT_GCODE_TOOLCHANGE_T5)); break;
1445+
#endif
1446+
#ifdef EVENT_GCODE_TOOLCHANGE_T6
1447+
case 6: gcode.process_subcommands_now(F(EVENT_GCODE_TOOLCHANGE_T6)); break;
1448+
#endif
1449+
#ifdef EVENT_GCODE_TOOLCHANGE_T7
1450+
case 7: gcode.process_subcommands_now(F(EVENT_GCODE_TOOLCHANGE_T7)); break;
1451+
#endif
1452+
}
1453+
1454+
#if ANY(TC_GCODE_USE_GLOBAL_X, TC_GCODE_USE_GLOBAL_Y, TC_GCODE_USE_GLOBAL_Z)
1455+
if (new_tool > 0) {
1456+
position_shift = old_position_shift;
1457+
TERN_(TC_GCODE_USE_GLOBAL_X, update_workspace_offset(X_AXIS));
1458+
TERN_(TC_GCODE_USE_GLOBAL_Y, update_workspace_offset(Y_AXIS));
1459+
TERN_(TC_GCODE_USE_GLOBAL_Z, update_workspace_offset(Z_AXIS));
1460+
}
14201461
#endif
14211462

14221463
#ifdef EVENT_GCODE_AFTER_TOOLCHANGE

buildroot/tests/BIGTREE_GTR_V1_0

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ opt_set MOTHERBOARD BOARD_BTT_GTR_V1_0 SERIAL_PORT -1 \
2525
Z_DRIVER_TYPE A4988 Z2_DRIVER_TYPE A4988 Z3_DRIVER_TYPE A4988 Z4_DRIVER_TYPE A4988 \
2626
DEFAULT_Kp_LIST '{ 22.2, 20.0, 21.0, 19.0, 18.0 }' DEFAULT_Ki_LIST '{ 1.08 }' DEFAULT_Kd_LIST '{ 114.0, 112.0, 110.0, 108.0 }'
2727
opt_enable TOOLCHANGE_FILAMENT_SWAP TOOLCHANGE_MIGRATION_FEATURE TOOLCHANGE_FS_SLOW_FIRST_PRIME TOOLCHANGE_FS_PRIME_FIRST_USED \
28-
PID_PARAMS_PER_HOTEND Z_MULTI_ENDSTOPS
28+
PID_PARAMS_PER_HOTEND Z_MULTI_ENDSTOPS TC_GCODE_USE_GLOBAL_X TC_GCODE_USE_GLOBAL_Y
2929
exec_test $1 $2 "BigTreeTech GTR | 6 Extruders | Quad Z + Endstops" "$3"
3030

3131
restore_configs

0 commit comments

Comments
 (0)