Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to automatically apply hotend offsets to EVENT_GCODE_TOOLCHANGE #25399

Merged

Conversation

ansonl
Copy link
Contributor

@ansonl ansonl commented Feb 18, 2023

Description

This adds the option for firmware to automatically apply hotend offsets to toolchange gcode with a EVENT_GCODE_TOOLCHANGE_AUTO_HOTEND_OFFSET option in Configuration_adv.h. This feature fixes the missing link between HOTEND_OFFSET and EVENT_GCODE_TOOLCHANGE so both hotend offset and toolchange gcode can be changed at runtime and during a prints. Turning on this option makes hotend offsets affect toolchange gcode which simplifies the "offset" calculation and saves users massive time.

Toolchange gcode to extruders are executed within the offsets of the target hotend. The target hotend is used because the target hotend offset is always known while the previous hotend can be one of multiple hotends and multiple offsets (depending on the number of hotends).

All hotend offsets can be dynamically changed at runtime with M218 or defaults set during compile in Configuration.h.

M218 T1 Z-1.5 //Change Z offset of T1 to -1.5

#define HOTEND_OFFSET_X { 0.0, 19.85 } // (mm) relative X-offset for each nozzle
#define HOTEND_OFFSET_Y { 0.0, -0.45 }  // (mm) relative Y-offset for each nozzle
#define HOTEND_OFFSET_Z { 0.0, -1.05 }      // (mm) relative Z-offset for each nozzle

Toolchange gcode consists of a sequence of gcode commands of any length. The gcode is set during compile in Configuration_adv.h with EVENT_GCODE_TOOLCHANGE_T1. After testing toolchange gcode with T0 at zero offsets, users need to manually add hotend offsets in every axis for every individual gcode command in EVENT_GCODE_TOOLCHANGE_T1. In the below, the commented EVENT_GCODE_TOOLCHANGE_T1 can be easily developed and manually tested by the user in a safe zero offset environment (T0). There is currently no way to change toolchange gcode at runtime. The uncommented EVENT_GCODE_TOOLCHANGE_T1 is the actual gcode that must be hard coded into the firmware and can't be changed by the user while using the printer. When the user calibrates and adjusts their hotend offsets by actively using the printer, they can adjust hotend offset with M218 but any hotend offset changes are not reflected in EVENT_GCODE_TOOLCHANGE_T1 so the toolchange gcode will always have the wrong "offset" after using M218 until the user manually calculates new gcode and has access to a compiler.

#define EVENT_GCODE_TOOLCHANGE_T0 "G90\nG0 X210 F7200\nG0 Y165\nG0 X216\nG0 X216 Y184.5\nG0 X210" // run while executing tool-change command T0
//#define EVENT_GCODE_TOOLCHANGE_T1 "G1 X210.00 F7200\nG1 Y199.00\nG1 X216.00\nG1 X216.00 Y182.00\nG1 X210.00" // run while executing tool-change command T1
#define EVENT_GCODE_TOOLCHANGE_T1 "G0 X229.85 F7200\nG0 Y198.55\nG0 X235.85\nG0 Y185.05\nG0 X229.85" // run while executing tool-change command T1

M218 not useful for users without automatic toolchange gcode adjustment because any hotend offset adjustment requires manual gcode editing and compiling.

Requirements

Requires more than 1 extruder with offsets.

Benefits

See above.

This addresses the issue where users with toolchange gcode must change 2 settings (hotend offset + toolchange gcode) to actually change 1 setting (toolchange with offset hotends).

Configurations

Configuration.zip

Related Issues

This PR is compatible with #24553

Add EVENT_GCODE_TOOLCHANGE_AUTO_HOTEND_OFFSET options
Added apply_hotend_offset_and_run_gcode()
Rename to apply_hotend_offset_process_subcommands_now()
@thinkyhead
Copy link
Member

Q: Under what circumstances will a post-toolchange move want to use something other than the nozzle position as its reference? Is this to accommodate moves that are carriage-relative?

Note: Instead of modifying the user's custom G-code before executing it, it would probably be simpler to send a G92 command with the current position minus the hotend offset ahead of the custom G-code, followed by a G92 that sets the current position back to its previous value. This simply requires that NO_WORKSPACE_OFFSETS be disabled.

@ansonl
Copy link
Contributor Author

ansonl commented Feb 19, 2023

@thinkyhead Post toolchange move should use nozzle position as the reference in every case except for moving axes or motors that do not get affected by the change in hotend offset. The toolchange gcode that moves the printhead usually moves the nozzle to specific points relative to the carriage/frame so that the printhead can dock/undock, pickup/drop off, activate lever for tools. I can't think of too many cases where post toolchange move in gcode would not depend on the nozzle position.

I tried manually adding a G92 before (subtract offset) and after (add offset) the toolchange and it seems to work with limited testing. First G92 will need to be added after do_blocking_move_to_xy on line 1351 is run in tool_change() so the new offset adjusted position can be calculated after the printer logic handles the edge case where the nozzle is already at a position like (0,0) and the nozzle cannot fully shift further left like it normally does for the hotend offset adjustment if the nozzle was somewhere else with space like (100,100). Second G92 should be added after the toolchange gcode is run. Besides the edge cases where the nozzle doesn't have enough space for the normal hotend offset adjustment move, I'm not sure if there is any way for gcode.process_subcommands_now that runs the toolchange gcode to be interrupted and additional gcode from another source to be run in between?

Is there a way to generate a G92 command without creating a new command string and send the new positions values directly to the code? I saw the powerloss code do something like sprintf_P(cmd, PSTR("G92.9E%s"), dtostrf(info.current_position.e, 1, 3, str_1)); but that still creates a new string.

@thinkyhead thinkyhead force-pushed the auto-offset-toolchange-gcode branch 5 times, most recently from 73a4702 to 09c48fd Compare February 19, 2023 05:39
@ansonl
Copy link
Contributor Author

ansonl commented Feb 20, 2023

@thinkyhead Tested on the actual printer with 2 extruders and confirmed working.

thinkyhead added a commit to MarlinFirmware/Configurations that referenced this pull request Feb 21, 2023
@thinkyhead thinkyhead merged commit 4f212e5 into MarlinFirmware:bugfix-2.1.x Feb 21, 2023
thinkyhead added a commit to thinkyhead/Marlin that referenced this pull request Feb 22, 2023
Co-authored-by: Scott Lahteine <thinkyhead@users.noreply.github.com>
LMBernardo pushed a commit to LMBernardo/Marlin that referenced this pull request Mar 19, 2023
Co-authored-by: Scott Lahteine <thinkyhead@users.noreply.github.com>
thinkyhead added a commit that referenced this pull request Apr 7, 2023
Co-authored-by: Scott Lahteine <thinkyhead@users.noreply.github.com>
EvilGremlin pushed a commit to EvilGremlin/Marlin that referenced this pull request Apr 8, 2023
Co-authored-by: Scott Lahteine <thinkyhead@users.noreply.github.com>
thinkyhead added a commit that referenced this pull request Apr 10, 2023
Co-authored-by: Scott Lahteine <thinkyhead@users.noreply.github.com>
thinkyhead added a commit to thinkyhead/Marlin that referenced this pull request May 16, 2023
Co-authored-by: Scott Lahteine <thinkyhead@users.noreply.github.com>
EvilGremlin pushed a commit to EvilGremlin/Marlin that referenced this pull request May 17, 2023
Co-authored-by: Scott Lahteine <thinkyhead@users.noreply.github.com>
tspiva pushed a commit to tspiva/Marlin that referenced this pull request May 25, 2023
Co-authored-by: Scott Lahteine <thinkyhead@users.noreply.github.com>
Andy-Big pushed a commit to Andy-Big/Marlin_FB_Reborn that referenced this pull request Jul 9, 2023
Co-authored-by: Scott Lahteine <thinkyhead@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants