Skip to content

Instantly share code, notes, and snippets.

@crysxd
Created September 6, 2024 22:25
Show Gist options
  • Save crysxd/dc47d28264c88f065edb96dfaae42476 to your computer and use it in GitHub Desktop.
Save crysxd/dc47d28264c88f065edb96dfaae42476 to your computer and use it in GitHub Desktop.
Switchwire macros
## LDO recommended macros to get you started
# These macros are recommended by LDO
# You can copy these entries into your printer.cfg.
#
# Alternatively,
# You can copy these entries into your printer.cfg.
#
[gcode_macro M300]
description: Custom M300 code to allow beeper to be used in gcode
gcode:
RESPOND PREFIX="BEEP"
# [gcode_macro T0]
# gcode:
# # Deactivate second extruder stepper
# SYNC_EXTRUDER_MOTION EXTRUDER=extruder1 MOTION_QUEUE=
# # Activate first extruder stepper
# SYNC_EXTRUDER_MOTION EXTRUDER=extruder MOTION_QUEUE=extruder
# SET_GCODE_VARIABLE MACRO=SET_PRESSURE_ADVANCE VARIABLE=active_extruder VALUE=0
# [gcode_macro T1]
# gcode:
# # Deactivate first extruder stepper
# SYNC_EXTRUDER_MOTION EXTRUDER=extruder MOTION_QUEUE=
# # Activate second extruder stepper
# SYNC_EXTRUDER_MOTION EXTRUDER=extruder1 MOTION_QUEUE=extruder
# SET_GCODE_VARIABLE MACRO=SET_PRESSURE_ADVANCE VARIABLE=active_extruder VALUE=1
# [gcode_macro SET_PRESSURE_ADVANCE]
# rename_existing: SET_PRESSURE_ADVANCE_BASE
# variable_active_extruder: -1
# gcode:
# {% set ADVANCE = params.ADVANCE|float %}
# {% set EXTRUDER = params.EXTRUDER|default("") %}
# {% set ACTIVE_EXTRUDER = printer["gcode_macro SET_PRESSURE_ADVANCE"].active_extruder %}
# {% if EXTRUDER == "" %}
# RESPOND PREFIX="Extruder not given, invering from active=" MSG={ACTIVE_EXTRUDER}
# {% if ACTIVE_EXTRUDER == 0 %}
# {% set EXTRUDER = "extruder" %}
# {% endif %}
# {% if ACTIVE_EXTRUDER == 1 %}
# {% set EXTRUDER = "extruder1" %}
# {% endif %}
# {% endif %}
# RESPOND PREFIX="Setting pressurece advance for" MSG={EXTRUDER}
# SET_PRESSURE_ADVANCE_BASE ADVANCE={ADVANCE} EXTRUDER={EXTRUDER}
[gcode_macro PAUSE]
rename_existing: PAUSE_BASE
gcode:
G1 E-15 F4000
PAUSE_BASE
{% set park_z = printer.toolhead.axis_maximum.z|float - 20 %}
{% set park_y = printer.toolhead.axis_maximum.y|float - 10 %}
{% set park_x = printer.toolhead.axis_maximum.x|float / 2.0 %}
G90
G1 X{park_x} Y{park_y} Z{park_z} F8000
[gcode_macro RESUME]
rename_existing: RESUME_BASE
gcode:
RESUME_BASE
G1 E13 F4000
[gcode_macro G28]
rename_existing: G990028
gcode:
{% set do_x = 0 %}
{% set do_y = 0 %}
{% set do_z = 0 %}
{% if params.Y is defined %}
## for y home just y
{% set do_y = 1 %}
{% endif %}
{% if params.X is defined %}
## for x home y first then X
{% set do_x = 1 %}
{% endif %}
{% if params.Z is defined %}
## for z home all
{% if "y" not in printer.toolhead.homed_axes %}
{% set do_y = 1 %}
{% endif %}
{% if "x" not in printer.toolhead.homed_axes %}
{% set do_x = 1 %}
{% endif %}
{% set do_z = 1 %}
{% endif %}
{% if do_y == 1 %}
RESPOND PREFIX="info" MSG="Home > Todo: Y"
{% endif %}
{% if do_x == 1 %}
RESPOND PREFIX="info" MSG="Home > Todo: X"
{% endif %}
{% if do_z == 1 %}
RESPOND PREFIX="info" MSG="Home > Todo: Z"
{% endif %}
{% if do_x == 0 and do_y == 0 and do_z == 0 %}
{% set do_x = 1 %}
{% set do_y = 1 %}
{% set do_z = 1 %}
RESPOND PREFIX="info" MSG="Home > Doing all XYZ"
{% endif %}
{% if do_z == 1 %}
{% set do_x = 1 %}
{% set do_y = 1 %}
RESPOND PREFIX="info" MSG="Home Z > Doing all XYZ"
{% if "z" not in printer.toolhead.homed_axes %}
SET_KINEMATIC_POSITION Z=0
{% endif %}
G91
G1 Z10 F2000
{% endif %}
{% if do_y == 1 %}
RESPOND PREFIX="info" MSG="Home > Y"
G90
G990028 Y0
G91
G0 Y5 F2000
G90
{% endif %}
{% if do_x == 1 %}
RESPOND PREFIX="info" MSG="Homing > X"
G90
G990028 X0
G91
# G0 X5 F2000
G90
{% endif %}
G90
{% set x_center = printer.toolhead.axis_maximum.x|float / 2.0 %}
{% set y_center = printer.toolhead.axis_maximum.y|float / 2.0 %}
G1 X{x_center} Y{x_center} F7800
{% if do_z == 1 %}
RESPOND PREFIX="info" MSG="Homing > Z"
G90
# G0 X125 Y125 F12000
G990028 Z0
G91
G0 Z20
G90
{% endif %}
RESPOND PREFIX="info" MSG="Homing > Done"
[gcode_macro PRINT_START]
# Use PRINT_START for the slicer starting script - please customize for your slicer of choice
# gcode:
# CHOME ; home all axes if not done already
# G90 ; absolute positioning
# G1 Z20 F3000 ; move nozzle away from bed
# G92 E0 ; prime nozzle
# G1 X2 Y1 Z0.28 F1500
# G1 Y110 E20 F1500
# G92 E0
gcode:
LIGHT_PENDING
{% set BED_TEMP = params.BED_TEMP|default(60)|float %}
{% set EXTRUDER_TEMP = params.EXTRUDER_TEMP|default(190)|float %}
# Slow down
M204 S2000
# Start TLM Start G-Code
# Clear Display
M117
# Clear Existing Feedrate
M220 S100
# Clear Existing Flowrate
M221 S100
# Heat up Bed and extruder, do not wait
M104 S150
M140 S{BED_TEMP}
# Home
CHOME
# Move to side and wait for final temps
CENTER
G1 Z120 F6000
M190 S{BED_TEMP}
# Probe print area
BED_MESH_CALIBRATE PROFILE=adaptive ADAPTIVE=1
# Let's Print
# Beep
M300 S100 P1
M300 S1000 P1
# Relative Extruder
M83
# Set units to millimeters
G21
# Use absolute coordinates
G90
# Use absolute distances for extrusion
M82
# Fan Off
M107
{% if BED_TEMP > 85 %}
FILTER_ON
{% endif %}
G1 X15 Y10 Z1 F4000
M109 S{EXTRUDER_TEMP}
NOZZLE_PURGE
LIGHT_PRINTING
[gcode_macro PRINT_END]
# Use PRINT_END for the slicer ending script - please customize for your slicer of choice
gcode:
M400 ; wait for buffer to clear
G92 E0 ; zero the extruder
G1 E-18.0 F3600 ; retract filament
G91 ; relative positioning
# Get Boundaries
{% set max_x = printer.configfile.config["stepper_x"]["position_max"]|float %}
{% set max_y = printer.configfile.config["stepper_y"]["position_max"]|float %}
{% set max_z = printer.configfile.config["stepper_z"]["position_max"]|float %}
# Check end position to determine safe direction to move
{% if printer.toolhead.position.x < (max_x - 20) %}
{% set x_safe = 20.0 %}
{% else %}
{% set x_safe = -20.0 %}
{% endif %}
{% if printer.toolhead.position.y < (max_y - 20) %}
{% set y_safe = 20.0 %}
{% else %}
{% set y_safe = -20.0 %}
{% endif %}
{% if printer.toolhead.position.z < (max_z - 20) %}
{% set z_safe = 20.0 %}
{% else %}
{% set z_safe = max_z - printer.toolhead.position.z %}
{% endif %}
G0 Z{z_safe} F3600 ; move nozzle up
G0 X{x_safe} Y{y_safe} F20000 ; move nozzle to remove stringing
TURN_OFF_HEATERS
M107 ; turn off fan
G90 ; absolute positioning
G0 X60 Y{max_y - 5} F3600 ; park nozzle at rear
LIGHT_OFF
FILTER_OFF
[gcode_macro FILTER_ON]
gcode:
SET_FAN_SPEED FAN=Nevermore SPEED=100
[gcode_macro FILTER_OFF]
gcode:
SET_FAN_SPEED FAN=Nevermore SPEED=0
[gcode_macro CHOME]
description: Homes XYZ axis only if printer is in a non-homed state
gcode:
{% if "xyz" not in printer.toolhead.homed_axes %}
G28
{% endif %}
[gcode_macro UNLOAD_FILAMENT]
description: Unloads filament from toolhead
gcode:
{% set EXTRUDER_TEMP = params.TEMP|default(230)|int %}
CHOME
G91 ; relative positioning
G1 Z20 ; move nozzle upwards
FRONT ; move the toolhead to the front
LIGHT_PENDING
M109 S{EXTRUDER_TEMP} ; heat up the hotend
LIGHT_PRINTING
M83 ; set extruder to relative mode
G1 E-8 F1800 ; quickly retract a small amount to elimate stringing
G4 P200 ; pause for a short amount of time
G1 E-50 F300 ; retract slowly the rest of the way
G1 E-20 F300
M400 ; wait for moves to finish
M117 Unload Complete!
LIGHT_OFF
[gcode_macro LOAD_FILAMENT]
description: Loads new filament into toolhead
gcode:
{% set EXTRUDER_TEMP = params.TEMP|default(230)|int %}
FRONT ; move the toolhead to the front
LIGHT_PENDING
M109 S{EXTRUDER_TEMP} ; heat up the hotend
LIGHT_PRINTING
M83 ; set extruder to relative mode
G1 E150 F300 ; extrude slowlyL
M400 ; wait for moves to finish
M117 Load Complete!
LIGHT_OFF
[gcode_macro CENTER]
description: Moves the toolhead to the center
gcode:
CHOME
{% set x_center = printer.toolhead.axis_maximum.x|float / 2.0 %}
{% set y_center = printer.toolhead.axis_maximum.y|float / 2.0 %}
G90
G1 X{x_center} Y{x_center} F7800
[gcode_macro FRONT]
description: Moves the toolhead to the front
gcode:
CHOME
{% set x_center = printer.toolhead.axis_maximum.x|float / 2.0 %}
{% set y_center = printer.toolhead.axis_maximum.y|float / 2.0 %}
G90
G1 X{x_center} Y10 F7800
[gcode_macro BACK]
description: Moves the toolhead to the back
gcode:
CHOME
{% set x_center = printer.toolhead.axis_maximum.x|float / 2.0 %}
{% set y_back = printer.toolhead.axis_maximum.y|float - 10 %}
G90
G1 X{x_center} Y{y_back} F7800
[gcode_macro NOZZLE_PURGE]
description: Draw a purge line at the front left edge of the build plate
gcode:
CHOME
G0 X5 Y10 F3000 ; Go to front
G0 Z0.3 ; Drop to bed
M83 ; Set extruder to relative mode
G1 E25 F500 ;
G1 X50 E15 F500 ; Extrude 25mm of filament in a 4cm line
G1 E-0.5 F400 ; Retract a little
G1 X85 F4000 ; Quickly wipe away from the filament line
G1 Z0.8 ; Raise and begin printing.
[gcode_macro LIGHT_PENDING]
gcode:
SET_LED LED=toolhead_light RED=1 GREEN=0.02 BLUE=0.02 WHITE=0
SET_LED LED=underground_lights RED=1 GREEN=0.02 BLUE=0 WHITE=0
SET_PIN PIN=bed_lights VALUE=0.2
[gcode_macro LIGHT_PRINTING]
gcode:
SET_LED LED=toolhead_light RED=1 GREEN=0.3 BLUE=0 WHITE=0 INDEX=1 TRANSMIT=0
SET_LED LED=toolhead_light RED=0 GREEN=0 BLUE=0 WHITE=1 INDEX=2 TRANSMIT=0
SET_LED LED=toolhead_light RED=0 GREEN=0 BLUE=0 WHITE=1 INDEX=3
SET_LED LED=underground_lights RED=0.14 GREEN=0.07 BLUE=0 WHITE=0
SET_PIN PIN=bed_lights VALUE=1
[gcode_macro LIGHT_OFF]
gcode:
SET_LED LED=toolhead_light RED=0 GREEN=0.02 BLUE=0 WHITE=0 INDEX=1 TRANSMIT=0
SET_LED LED=toolhead_light RED=0 GREEN=0 BLUE=0 WHITE=0 INDEX=2 TRANSMIT=0
SET_LED LED=toolhead_light RED=0 GREEN=0 BLUE=0 WHITE=0 INDEX=3
SET_LED LED=underground_lights RED=0 GREEN=0 BLUE=0 WHITE=0
SET_PIN PIN=bed_lights VALUE=0
[gcode_macro FAN_ON]
gcode:
SET_FAN_SPEED FAN=hotend_fan SPEED=0.5
SET_FAN_SPEED FAN=fan SPEED=0.5
[gcode_macro M600]
gcode:
PAUSE
FRONT
UNLOAD_FILAMENT TEMP={ printer.extruder.target }
[gcode_macro TEST_SPEED]
description: Test for max speed and acceleration parameters for the printer. Procedure: Home -> ReadPositionFromMCU -> MovesToolhead@Vel&Accel -> Home -> ReadPositionfromMCU
gcode:
# Speed
{% set speed = params.SPEED|default(printer.configfile.settings.printer.max_velocity)|int %}
# Iterations
{% set iterations = params.ITERATIONS|default(5)|int %}
# Acceleration
{% set accel = params.ACCEL|default(printer.configfile.settings.printer.max_accel)|int %}
# Minimum Cruise Ratio
{% set min_cruise_ratio = params.MIN_CRUISE_RATIO|default(0)|float %} # Possible location in printer object stack: printer.configfile.settings.printer.minimum_cruise_ratio
# Bounding inset for large pattern (helps prevent slamming the toolhead into the sides after small skips, and helps to account for machines with imperfectly set dimensions)
{% set bound = params.BOUND|default(20)|int %}
# Size for small pattern box
{% set smallpatternsize = SMALLPATTERNSIZE|default(20)|int %}
# Large pattern
# Max positions, inset by BOUND
{% set x_min = printer.toolhead.axis_minimum.x + bound %}
{% set x_max = printer.toolhead.axis_maximum.x - bound %}
{% set y_min = printer.toolhead.axis_minimum.y + bound %}
{% set y_max = printer.toolhead.axis_maximum.y - bound %}
# Small pattern at center
# Find X/Y center point
{% set x_center = (printer.toolhead.axis_minimum.x|float + printer.toolhead.axis_maximum.x|float ) / 2 %}
{% set y_center = (printer.toolhead.axis_minimum.y|float + printer.toolhead.axis_maximum.y|float ) / 2 %}
# Set small pattern box around center point
{% set x_center_min = x_center - (smallpatternsize/2) %}
{% set x_center_max = x_center + (smallpatternsize/2) %}
{% set y_center_min = y_center - (smallpatternsize/2) %}
{% set y_center_max = y_center + (smallpatternsize/2) %}
# Save current gcode state (absolute/relative, etc)
SAVE_GCODE_STATE NAME=TEST_SPEED
# Output parameters to g-code terminal
{ action_respond_info("TEST_SPEED: starting %d iterations at speed %d, accel %d" % (iterations, speed, accel)) }
# Home and get position for comparison later:
M400 # Finish moves - https://github.com/AndrewEllis93/Print-Tuning-Guide/issues/66
G28
# QGL if not already QGLd (only if QGL section exists in config)
{% if printer.configfile.settings.quad_gantry_level %}
{% if printer.quad_gantry_level.applied == False %}
QUAD_GANTRY_LEVEL
G28 Z
{% endif %}
{% endif %}
# Move 50mm away from max position and home again (to help with hall effect endstop accuracy - https://github.com/AndrewEllis93/Print-Tuning-Guide/issues/24)
G90
G1 X{printer.toolhead.axis_maximum.x-50} Y{printer.toolhead.axis_maximum.y-50} F{30*60}
M400 # Finish moves - https://github.com/AndrewEllis93/Print-Tuning-Guide/issues/66
G28 X Y
G0 X{printer.toolhead.axis_maximum.x-1} Y{printer.toolhead.axis_maximum.y-1} F{30*60}
G4 P1000
GET_POSITION
# Go to starting position
G0 X{x_min} Y{y_min} Z{bound + 10} F{speed*60}
# Set new limits
# SET_VELOCITY_LIMIT VELOCITY={speed} ACCEL={accel} ACCEL_TO_DECEL={accel / 2} # DEPRECATED | Due to deprecation of accel_to_decel parameter in Klipper
SET_VELOCITY_LIMIT VELOCITY={speed} ACCEL={accel} MIN_CRUISE_RATIO={minCruiseRatio}
{% for i in range(iterations) %}
# Large pattern diagonals
G0 X{x_min} Y{y_min} F{speed*60}
G0 X{x_max} Y{y_max} F{speed*60}
G0 X{x_min} Y{y_min} F{speed*60}
G0 X{x_max} Y{y_min} F{speed*60}
G0 X{x_min} Y{y_max} F{speed*60}
G0 X{x_max} Y{y_min} F{speed*60}
# Large pattern box
G0 X{x_min} Y{y_min} F{speed*60}
G0 X{x_min} Y{y_max} F{speed*60}
G0 X{x_max} Y{y_max} F{speed*60}
G0 X{x_max} Y{y_min} F{speed*60}
# Small pattern diagonals
G0 X{x_center_min} Y{y_center_min} F{speed*60}
G0 X{x_center_max} Y{y_center_max} F{speed*60}
G0 X{x_center_min} Y{y_center_min} F{speed*60}
G0 X{x_center_max} Y{y_center_min} F{speed*60}
G0 X{x_center_min} Y{y_center_max} F{speed*60}
G0 X{x_center_max} Y{y_center_min} F{speed*60}
# Small patternbox
G0 X{x_center_min} Y{y_center_min} F{speed*60}
G0 X{x_center_min} Y{y_center_max} F{speed*60}
G0 X{x_center_max} Y{y_center_max} F{speed*60}
G0 X{x_center_max} Y{y_center_min} F{speed*60}
{% endfor %}
# Restore max speed/accel/accel_to_decel to their configured values
#SET_VELOCITY_LIMIT VELOCITY={printer.configfile.settings.printer.max_velocity} ACCEL={printer.configfile.settings.printer.max_accel} ACCEL_TO_DECEL={printer.configfile.settings.printer.max_accel_to_decel} # DEPRECATED | Depercating of accel_to_decel param in klipper
SET_VELOCITY_LIMIT VELOCITY={printer.configfile.settings.printer.max_velocity} ACCEL={printer.configfile.settings.printer.max_accel} MIN_CRUISE_RATIO={printer.configfile.settings.printer.minimum_cruise_ratio}
# Re-home and get position again for comparison:
M400 # Finish moves - https://github.com/AndrewEllis93/Print-Tuning-Guide/issues/66
G28 # This is a full G28 to fix an issue with CoreXZ - https://github.com/AndrewEllis93/Print-Tuning-Guide/issues/12
# Go to XY home positions (in case your homing override leaves it elsewhere)
G90
G0 X{printer.toolhead.axis_maximum.x-1} Y{printer.toolhead.axis_maximum.y-1} F{30*60}
G4 P1000
GET_POSITION
# Restore previous gcode state (absolute/relative, etc)
RESTORE_GCODE_STATE NAME=TEST_SPEED
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment