Skip to content

Instantly share code, notes, and snippets.

@bistory
Created February 11, 2023 21:06
Show Gist options
  • Save bistory/629b596c5c49d6b01f5cf06ea2f2690f to your computer and use it in GitHub Desktop.
Save bistory/629b596c5c49d6b01f5cf06ea2f2690f to your computer and use it in GitHub Desktop.
Macros for loading and unloading filament with Klipper
[gcode_macro LOAD_FILAMENT]
gcode:
{% set speed = params.SPEED|default(300) %}
{% set max_velocity = printer.configfile.settings['extruder'].max_extrude_only_velocity %}
SAVE_GCODE_STATE NAME=load_state
M300 # beep
G91
G92 E0
G1 E350 F{max_velocity} # fast-load
G1 E25 F{speed} # purge
M300
M300
RESTORE_GCODE_STATE NAME=load_state
[gcode_macro UNLOAD_FILAMENT]
gcode:
{% set speed = params.SPEED|default(300) %}
{% set max_velocity = printer.configfile.settings['extruder'].max_extrude_only_velocity %}
SAVE_GCODE_STATE NAME=unload_state
G91
M300 # beep
G92 E0
G1 E25 F{speed} # purge
G1 E-420 F{max_velocity} # fast-unload
M300
M300
RESTORE_GCODE_STATE NAME=unload_state
@DVSVIDEO
Copy link

DVSVIDEO commented Feb 9, 2024

And where is the nozzle heating? Can't you load or unload the nozzle when it's cold...

@bistory
Copy link
Author

bistory commented Feb 9, 2024

And where is the nozzle heating? Can't you load or unload the nozzle when it's cold...

Pre-heat the nozzle...

@Martin-Schnebbe
Copy link

Martin-Schnebbe commented Dec 11, 2024

Here is a version with heating included:

[gcode_macro LOAD_FILAMENT]
variable_load_distance: 40         # Distance to load filament into the extruder
variable_purge_distance: 50        # Distance to purge filament after loading
variable_nozzle_preheat_temp: 250  # Default preheat temperature for the nozzle
variable_turn_off_extruder: True   # Option to turn off the extruder after loading (True/False)
gcode:
    # Parameters and settings
    {% set load_speed = params.LOAD_SPEED|default(600) %}  # Speed in mm/min for fast filament loading
    {% set purge_speed = params.PURGE_SPEED|default(300) %}  # Speed in mm/min for purging filament
    {% set target_temp = params.TARGET_TEMP|default(nozzle_preheat_temp) %}  # Target temperature for the nozzle
    {% set min_temp = params.MIN_TEMP|default(180) %}  # Minimum safe temperature for extrusion

    # Save current state of the printer
    SAVE_GCODE_STATE NAME=load_state

    # Heat directly to the target temperature if it's above the minimum temperature
    {% if target_temp >= min_temp %}
        M104 S{target_temp} ; Set extruder to target temperature
        M109 S{target_temp} ; Wait for extruder to reach target temperature
    {% else %}
        # Heat to minimum temperature first if target is too low
        M104 S{min_temp} ; Set extruder to safe minimum temperature
        M109 S{min_temp} ; Wait for extruder to reach safe minimum temperature
    {% endif %}

    # Begin filament loading process
    G91 ; Set relative positioning for extrusion
    G92 E0 ; Reset extruder position
    G1 E{load_distance} F{load_speed} ; Load filament at the specified loading speed
    G1 E{purge_distance} F{purge_speed} ; Purge filament at the slower purging speed

    # Optionally turn off the extruder heater after loading
    {% if turn_off_extruder %}
        M104 S0 ; Turn off extruder heater
    {% endif %}

    # Restore previous state of the printer
    G90 ; Restore absolute positioning
    RESTORE_GCODE_STATE NAME=load_state

    # Completion message
    M117 Filament load complete
[gcode_macro UNLOAD_FILAMENT]
variable_unload_distance: 100      # Distance to retract filament from the extruder
variable_nozzle_preheat_temp: 250  # Default preheat temperature for unloading
variable_turn_off_extruder: True   # Option to turn off the extruder after unloading (True/False)
gcode:
    # Parameters and settings
    {% set retract_speed = params.RETRACT_SPEED|default(600) %}  # Speed for retracting filament
    {% set target_temp = params.TARGET_TEMP|default(nozzle_preheat_temp) %}  # Target temperature for the nozzle
    {% set min_temp = params.MIN_TEMP|default(180) %}  # Minimum safe temperature for extrusion

    # Save current state of the printer
    SAVE_GCODE_STATE NAME=unload_state

    # Heat the nozzle to the target temperature if required
    {% if printer.extruder.temperature < target_temp %}
        # Ensure the nozzle is heated to the target temperature or at least the minimum safe temperature
        {% if target_temp >= min_temp %}
            M104 S{target_temp} ; Set extruder to target temperature
            M109 S{target_temp} ; Wait for extruder to reach target temperature
        {% else %}
            M104 S{min_temp} ; Set extruder to safe minimum temperature
            M109 S{min_temp} ; Wait for extruder to reach safe minimum temperature
        {% endif %}
    {% endif %}

    # Begin filament unloading process
    G91 ; Set relative positioning for extrusion
    G92 E0 ; Reset extruder position
    G1 E-{unload_distance} F{retract_speed} ; Retract filament at the specified speed

    # Optionally turn off the extruder heater after unloading
    {% if turn_off_extruder %}
        M104 S0 ; Turn off extruder heater
    {% endif %}

    # Restore previous state of the printer
    G90 ; Restore absolute positioning
    RESTORE_GCODE_STATE NAME=unload_state

    # Completion message
    M117 Filament unload complete

@DrLabel
Copy link

DrLabel commented Dec 14, 2024

if I use Martin-Schnebbe macro i get :
Section 'gcode_macro` load_filament' is not a valid config section

Once the underlying issue is corrected, use the "RESTART"
command to reload the config and restart the host software.
Printer is halted

What i'm doing wrong

@DrLabel
Copy link

DrLabel commented Dec 14, 2024

I find the error:
[gcode_macro` LOAD_FILAMENT] have to be:
[gcode_macro LOAD_FILAMENT]

@Martin-Schnebbe
Copy link

Thanks - I edited my previous post!

@eboston
Copy link

eboston commented Jan 31, 2025

I have hated this change ever since it was implemented because I cannot get it to work. I create these macros and when I try using them from the extrude menu, I get a temperature too low error. I put messages in the macros to see if they get called, but never see the message. Is there something I am missing? The macros I have will heat the extruder.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment