From 4f212e50b6ad402ae34e7c9ed35ec9168bae6221 Mon Sep 17 00:00:00 2001 From: Anson Liu Date: Mon, 20 Feb 2023 19:49:15 -0500 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20TC=5FGCODE=5FUSE=5FGLOBAL=5F*=20(#2?= =?UTF-8?q?5399)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Scott Lahteine --- Marlin/Configuration_adv.h | 14 +++++-- Marlin/src/inc/Conditionals_adv.h | 7 ++++ Marlin/src/inc/SanityCheck.h | 6 +++ Marlin/src/module/tool_change.cpp | 69 ++++++++++++++++++++++++------- buildroot/tests/BIGTREE_GTR_V1_0 | 2 +- 5 files changed, 80 insertions(+), 18 deletions(-) diff --git a/Marlin/Configuration_adv.h b/Marlin/Configuration_adv.h index 7e224ae61c..cdf32e490a 100644 --- a/Marlin/Configuration_adv.h +++ b/Marlin/Configuration_adv.h @@ -2556,9 +2556,17 @@ * Extra G-code to run while executing tool-change commands. Can be used to use an additional * stepper motor (e.g., I axis in Configuration.h) to drive the tool-changer. */ - //#define EVENT_GCODE_TOOLCHANGE_T0 "G28 A\nG1 A0" // Extra G-code to run while executing tool-change command T0 - //#define EVENT_GCODE_TOOLCHANGE_T1 "G1 A10" // Extra G-code to run while executing tool-change command T1 - //#define EVENT_GCODE_TOOLCHANGE_ALWAYS_RUN // Always execute above G-code sequences. Use with caution! + //#define EVENT_GCODE_TOOLCHANGE_T0 "G28 A\nG1 A0" // Extra G-code to run while executing tool-change command T0 + //#define EVENT_GCODE_TOOLCHANGE_T1 "G1 A10" // Extra G-code to run while executing tool-change command T1 + //#define EVENT_GCODE_TOOLCHANGE_ALWAYS_RUN // Always execute above G-code sequences. Use with caution! + + /** + * Consider coordinates for EVENT_GCODE_TOOLCHANGE_Tx as relative to T0 + * so that moves in the specified axes are the same for all tools. + */ + //#define TC_GCODE_USE_GLOBAL_X // Use X position relative to Tool 0 + //#define TC_GCODE_USE_GLOBAL_Y // Use Y position relative to Tool 0 + //#define TC_GCODE_USE_GLOBAL_Z // Use Z position relative to Tool 0 /** * Tool Sensors detect when tools have been picked up or dropped. diff --git a/Marlin/src/inc/Conditionals_adv.h b/Marlin/src/inc/Conditionals_adv.h index ad89824347..6c6f1f585a 100644 --- a/Marlin/src/inc/Conditionals_adv.h +++ b/Marlin/src/inc/Conditionals_adv.h @@ -1152,3 +1152,10 @@ #if EITHER(INPUT_SHAPING_X, INPUT_SHAPING_Y) #define HAS_SHAPING 1 #endif + +// Toolchange Event G-code +#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)) + #undef TC_GCODE_USE_GLOBAL_X + #undef TC_GCODE_USE_GLOBAL_Y + #undef TC_GCODE_USE_GLOBAL_Z +#endif diff --git a/Marlin/src/inc/SanityCheck.h b/Marlin/src/inc/SanityCheck.h index 12d281b704..d011fb18f0 100644 --- a/Marlin/src/inc/SanityCheck.h +++ b/Marlin/src/inc/SanityCheck.h @@ -1015,9 +1015,15 @@ static_assert(X_MAX_LENGTH >= X_BED_SIZE, "Movement bounds (X_MIN_POS, X_MAX_POS #endif #endif +/** + * Custom Event G-code + */ #if defined(EVENT_GCODE_SD_ABORT) && DISABLED(NOZZLE_PARK_FEATURE) static_assert(nullptr == strstr(EVENT_GCODE_SD_ABORT, "G27"), "NOZZLE_PARK_FEATURE is required to use G27 in EVENT_GCODE_SD_ABORT."); #endif +#if ANY(TC_GCODE_USE_GLOBAL_X, TC_GCODE_USE_GLOBAL_Y, TC_GCODE_USE_GLOBAL_Z) && ENABLED(NO_WORKSPACE_OFFSETS) + #error "TC_GCODE_USE_GLOBAL_* options are incompatible with NO_WORKSPACE_OFFSETS." +#endif /** * I2C Position Encoders diff --git a/Marlin/src/module/tool_change.cpp b/Marlin/src/module/tool_change.cpp index 16b562e482..0defced87e 100644 --- a/Marlin/src/module/tool_change.cpp +++ b/Marlin/src/module/tool_change.cpp @@ -30,6 +30,7 @@ #include "temperature.h" #include "../MarlinCore.h" +#include "../gcode/gcode.h" //#define DEBUG_TOOL_CHANGE //#define DEBUG_TOOLCHANGE_FILAMENT_SWAP @@ -49,12 +50,6 @@ Flags toolchange_extruder_ready; #endif -#if EITHER(MAGNETIC_PARKING_EXTRUDER, TOOL_SENSOR) \ - || defined(EVENT_GCODE_TOOLCHANGE_T0) || defined(EVENT_GCODE_TOOLCHANGE_T1) || defined(EVENT_GCODE_AFTER_TOOLCHANGE) \ - || (ENABLED(PARKING_EXTRUDER) && PARKING_EXTRUDER_SOLENOIDS_DELAY > 0) - #include "../gcode/gcode.h" -#endif - #if ENABLED(TOOL_SENSOR) #include "../lcd/marlinui.h" #endif @@ -98,7 +93,6 @@ #endif #if ENABLED(TOOLCHANGE_FILAMENT_SWAP) - #include "../gcode/gcode.h" #if TOOLCHANGE_FS_WIPE_RETRACT <= 0 #undef TOOLCHANGE_FS_WIPE_RETRACT #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. } #endif - //Calculate and perform the priming distance + // Calculate and perform the priming distance if (toolchange_settings.extra_prime >= 0) { // Positive extra_prime value // - 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*/) { TERN_(HAS_FANMUX, fanmux_switch(active_extruder)); if (ENABLED(EVENT_GCODE_TOOLCHANGE_ALWAYS_RUN) || !no_move) { - #ifdef EVENT_GCODE_TOOLCHANGE_T0 - if (new_tool == 0) - gcode.process_subcommands_now(F(EVENT_GCODE_TOOLCHANGE_T0)); + + #if ANY(TC_GCODE_USE_GLOBAL_X, TC_GCODE_USE_GLOBAL_Y, TC_GCODE_USE_GLOBAL_Z) + // G0/G1/G2/G3/G5 moves are relative to the active tool. + // Shift the workspace to make custom moves relative to T0. + xyz_pos_t old_position_shift; + if (new_tool > 0) { + old_position_shift = position_shift; + const xyz_pos_t &he = hotend_offset[new_tool]; + #if ENABLED(TC_GCODE_USE_GLOBAL_X) + position_shift.x -= he.x; update_workspace_offset(X_AXIS); + #endif + #if ENABLED(TC_GCODE_USE_GLOBAL_Y) + position_shift.y -= he.y; update_workspace_offset(Y_AXIS); + #endif + #if ENABLED(TC_GCODE_USE_GLOBAL_Z) + position_shift.z -= he.z; update_workspace_offset(Z_AXIS); + #endif + } #endif - #ifdef EVENT_GCODE_TOOLCHANGE_T1 - if (new_tool == 1) - gcode.process_subcommands_now(F(EVENT_GCODE_TOOLCHANGE_T1)); + switch (new_tool) { + default: break; + #ifdef EVENT_GCODE_TOOLCHANGE_T0 + case 0: gcode.process_subcommands_now(F(EVENT_GCODE_TOOLCHANGE_T0)); break; + #endif + #ifdef EVENT_GCODE_TOOLCHANGE_T1 + case 1: gcode.process_subcommands_now(F(EVENT_GCODE_TOOLCHANGE_T1)); break; + #endif + #ifdef EVENT_GCODE_TOOLCHANGE_T2 + case 2: gcode.process_subcommands_now(F(EVENT_GCODE_TOOLCHANGE_T2)); break; + #endif + #ifdef EVENT_GCODE_TOOLCHANGE_T3 + case 3: gcode.process_subcommands_now(F(EVENT_GCODE_TOOLCHANGE_T3)); break; + #endif + #ifdef EVENT_GCODE_TOOLCHANGE_T4 + case 4: gcode.process_subcommands_now(F(EVENT_GCODE_TOOLCHANGE_T4)); break; + #endif + #ifdef EVENT_GCODE_TOOLCHANGE_T5 + case 5: gcode.process_subcommands_now(F(EVENT_GCODE_TOOLCHANGE_T5)); break; + #endif + #ifdef EVENT_GCODE_TOOLCHANGE_T6 + case 6: gcode.process_subcommands_now(F(EVENT_GCODE_TOOLCHANGE_T6)); break; + #endif + #ifdef EVENT_GCODE_TOOLCHANGE_T7 + case 7: gcode.process_subcommands_now(F(EVENT_GCODE_TOOLCHANGE_T7)); break; + #endif + } + + #if ANY(TC_GCODE_USE_GLOBAL_X, TC_GCODE_USE_GLOBAL_Y, TC_GCODE_USE_GLOBAL_Z) + if (new_tool > 0) { + position_shift = old_position_shift; + TERN_(TC_GCODE_USE_GLOBAL_X, update_workspace_offset(X_AXIS)); + TERN_(TC_GCODE_USE_GLOBAL_Y, update_workspace_offset(Y_AXIS)); + TERN_(TC_GCODE_USE_GLOBAL_Z, update_workspace_offset(Z_AXIS)); + } #endif #ifdef EVENT_GCODE_AFTER_TOOLCHANGE diff --git a/buildroot/tests/BIGTREE_GTR_V1_0 b/buildroot/tests/BIGTREE_GTR_V1_0 index d6eccaff07..09e36d6475 100755 --- a/buildroot/tests/BIGTREE_GTR_V1_0 +++ b/buildroot/tests/BIGTREE_GTR_V1_0 @@ -25,7 +25,7 @@ opt_set MOTHERBOARD BOARD_BTT_GTR_V1_0 SERIAL_PORT -1 \ Z_DRIVER_TYPE A4988 Z2_DRIVER_TYPE A4988 Z3_DRIVER_TYPE A4988 Z4_DRIVER_TYPE A4988 \ 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 }' opt_enable TOOLCHANGE_FILAMENT_SWAP TOOLCHANGE_MIGRATION_FEATURE TOOLCHANGE_FS_SLOW_FIRST_PRIME TOOLCHANGE_FS_PRIME_FIRST_USED \ - PID_PARAMS_PER_HOTEND Z_MULTI_ENDSTOPS + PID_PARAMS_PER_HOTEND Z_MULTI_ENDSTOPS TC_GCODE_USE_GLOBAL_X TC_GCODE_USE_GLOBAL_Y exec_test $1 $2 "BigTreeTech GTR | 6 Extruders | Quad Z + Endstops" "$3" restore_configs