Skip to content

Instantly share code, notes, and snippets.

@jeanfbrito
Created April 6, 2023 13:56
Show Gist options
  • Save jeanfbrito/73e0da1165032eeb7bd4c5b4070e9357 to your computer and use it in GitHub Desktop.
Save jeanfbrito/73e0da1165032eeb7bd4c5b4070e9357 to your computer and use it in GitHub Desktop.
Klipper macros
[gcode_macro BED_MESH_CALIBRATE]
rename_existing: BED_MESH_CALIBRATE_BASE
; gcode parameters
variable_parameter_AREA_START : 0,0
variable_parameter_AREA_END : 0,0
; the clearance between print area and probe area
variable_mesh_area_offset : 5.0
; number of sample per probe point
variable_probe_samples : 2
; minimum probe count
variable_min_probe_count : 3
; scale up the probe count, should be 1.0 ~ < variable_max_probe_count/variable_min_probe_count
variable_probe_count_scale_factor : 1.0
gcode:
{% if params.AREA_START and params.AREA_END %}
{% set bedMeshConfig = printer["configfile"].config["bed_mesh"] %}
{% set safe_min_x = bedMeshConfig.mesh_min.split(",")[0]|float %}
{% set safe_min_y = bedMeshConfig.mesh_min.split(",")[1]|float %}
{% set safe_max_x = bedMeshConfig.mesh_max.split(",")[0]|float %}
{% set safe_max_y = bedMeshConfig.mesh_max.split(",")[1]|float %}
{% set area_min_x = params.AREA_START.split(",")[0]|float %}
{% set area_min_y = params.AREA_START.split(",")[1]|float %}
{% set area_max_x = params.AREA_END.split(",")[0]|float %}
{% set area_max_y = params.AREA_END.split(",")[1]|float %}
{% set meshPointX = bedMeshConfig.probe_count.split(",")[0]|int %}
{% set meshPointY = bedMeshConfig.probe_count.split(",")[1]|int %}
{% set meshMaxPointX = meshPointX %}
{% set meshMaxPointY = meshPointY %}
{% if (area_min_x < area_max_x) and (area_min_y < area_max_y) %}
{% if area_min_x - mesh_area_offset >= safe_min_x %}
{% set area_min_x = area_min_x - mesh_area_offset %}
{% else %}
{% set area_min_x = safe_min_x %}
{% endif %}
{% if area_min_y - mesh_area_offset >= safe_min_y %}
{% set area_min_y = area_min_y - mesh_area_offset %}
{% else %}
{% set area_min_y = safe_min_y %}
{% endif %}
{% if area_max_x + mesh_area_offset <= safe_max_x %}
{% set area_max_x = area_max_x + mesh_area_offset %}
{% else %}
{% set area_max_x = safe_max_x %}
{% endif %}
{% if area_max_y + mesh_area_offset <= safe_max_y %}
{% set area_max_y = area_max_y + mesh_area_offset %}
{% else %}
{% set area_max_y = safe_max_y %}
{% endif %}
{% set meshPointX = (meshPointX * (area_max_x - area_min_x) / (safe_max_x - safe_min_x) * probe_count_scale_factor)|round(0)|int %}
{% if meshPointX < min_probe_count %}
{% set meshPointX = min_probe_count %}
{% endif %}
{% if meshPointX > meshMaxPointX %}
{% set meshPointX = meshMaxPointX %}
{% endif %}
{% set meshPointY = (meshPointY * (area_max_y -area_min_y ) / (safe_max_y - safe_min_y) * probe_count_scale_factor )|round(0)|int %}
{% if meshPointY < min_probe_count %}
{% set meshPointY = min_probe_count %}
{% endif %}
{% if meshPointY > meshMaxPointY %}
{% set meshPointY = meshMaxPointY %}
{% endif %}
BED_MESH_CALIBRATE_BASE mesh_min={area_min_x},{area_min_y} mesh_max={area_max_x},{area_max_y} probe_count={meshPointX},{meshPointY} samples={probe_samples|int}
{% else %}
BED_MESH_CALIBRATE_BASE
{% endif %}
{% else %}
BED_MESH_CALIBRATE_BASE
{% endif %}
######################################################################
# Filament Change
######################################################################
# M600: Filament Change. This macro will pause the printer, move the
# tool to the change position, and retract the filament 50mm. Adjust
# the retraction settings for your own extruder. After filament has
# been changed, the print can be resumed from its previous position
# with the "RESUME" gcode.
# Pressure Advance test macro by drawing 20 simple lines
#
# Adapted from: https://www.reddit.com/r/VORONDesign/comments/sjdiol/pressure_advance_testing_macro_klipper/
# to suit home-made free ABL that requires a pause after homing to manually retract the probe.
#
# To do that, the process is divided into 2 macros:
#
# - PA_INIT to bring bed and extruder up to temperature, then home.
# eg. PA_INIT BED_TEMP=65 EXTRUDER_TEMP=205
# After homing, the ABL can be manually retracted
#
# - PA_CAL to start the Pressure Advance test.
# eg. PA_CAL PA_START=0.01 PA_STEP=0.01 NZL=0.4
# This will draw 20 lines using different PA values
#
# To use, simply include in printer.cfg:
#
# [include pa_macro.cfg]
# [gcode_macro PA_INIT]
# gcode:
# {% set BED_TEMP = params.BED_TEMP|default(60)|float %}
# {% set EXTRUDER_TEMP = params.EXTRUDER_TEMP|default(190)|float %}
# M140 S{BED_TEMP} ;Start heating bed
# M190 S{BED_TEMP} ;Wait for bed to reach temp before proceeding
# M104 S{EXTRUDER_TEMP} ;Start heating extruder
# M109 S{EXTRUDER_TEMP} ;Wait for extruder to reach temp before proceeding
# G28 ;Home all axes and pause with audio alert
[gcode_macro PRESSURE_ADVANCE_CALIBRATION]
gcode:
{% set BED_TEMP = params.BED_TEMP|default(60)|float %}
{% set EXTRUDER_TEMP = params.EXTRUDER_TEMP|default(200)|float %}
{% set RETRACTION_LENGTH = params.RETRACTION_LENGTH|default(0.3)|float %}
{% set LAYER_HEIGHT = params.LAYER_HEIGHT|default(0.2)|float %}
{% set PA_START = params.PA_START|default(0.0)|float %}
{% set PA_STEP = params.PA_STEP|default(0.005)|float %}
{% set NOZZLE_SIZE = params.NOZZLE_SIZE|default(0.4)|float %}
{% set E5 = (0.1147475 * NOZZLE_SIZE) * 5|float %}
{% set E20 = (0.1147475 * NOZZLE_SIZE) * 20|float %}
{% set E40 = (0.1147475 * NOZZLE_SIZE) * 40|float %}
{% set X_MID = printer.configfile.config["stepper_x"]["position_max"]|float / 2.0 %}
{% set Y_MID = printer.configfile.config["stepper_y"]["position_max"]|float / 2.0 %}
M140 S{BED_TEMP} ;Start heating bed
M190 S{BED_TEMP} ;Wait for bed to reach temp before proceeding
M104 S{EXTRUDER_TEMP} ;Start heating extruder
M109 S{EXTRUDER_TEMP} ;Wait for extruder to reach temp before proceeding
G28 ;Home all axes and pause with audio alert
G21 ; millimeter units
G90 ; absolute XYZ
M83 ; relative E
SET_VELOCITY_LIMIT ACCEL=3000 ACCEL_TO_DECEL=1500
G92 E0 ; reset extruder
M106 S0 ; set fan speed to zero
; This was taken from Cura to prime the extruder by tracing a line at the
; left side of the build bed twice
G1 X10.1 Y20 Z0.28 F5000.0 ; move to start position
G1 X10.1 Y200.0 Z0.28 F1500.0 E15 ; draw the first line
G1 X10.4 Y200.0 Z0.28 F5000.0 ; move to side a little
G1 X10.4 Y20 Z0.28 F1500.0 E30 ; draw the second line
G1 E-2 F1800 ; retract
G1 Z5 F300 ; move above layer height
{% for i in range(0, 20) %}
SET_PRESSURE_ADVANCE ADVANCE={PA_START + (i * PA_STEP)} ; set Pressure Advance
M117 Testing Pressure Advance at: {PA_START + (i * PA_STEP)}
G1 X{(X_MID-40)} Y{(Y_MID-35)+(5*i)} F5000 ; move to start position
G1 Z{LAYER_HEIGHT} F300 ; move to layer height
G1 E{RETRACTION_LENGTH} F1800 ; un-retract
G1 X{(X_MID-20)} Y{(Y_MID-35)+(5*i)} E{E20} F300 ; print line part one
G1 X{(X_MID+20)} Y{(Y_MID-35)+(5*i)} E{E40} F9000 ; print line part two
G1 X{(X_MID+40)} Y{(Y_MID-35)+(5*i)} E{E20} F300 ; print line part three
G1 E-{RETRACTION_LENGTH} F1800 ; retract
G1 Z5 F300 ; move above layer height
{% endfor %}
; This was again taken from Cura to raise the extruder and push the build plate forward when test is done
G1 Z20 F300 ; Raise Z more
G1 X{X_MID} Y{(Y_MID-35)+(5*24)} F300 ; Present print by pushing build plate forward a little
M117 Find best line and multiply it by ({PA_START} + (line * {PA_STEP}) ) to find your PA setting.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment