Skip to content

Instantly share code, notes, and snippets.

@m0to
Forked from JeremyKennedy/pa_cal.cfg
Created March 15, 2022 18:28
Show Gist options
  • Save m0to/d395d44fa412d9808e0130857ea74f0b to your computer and use it in GitHub Desktop.
Save m0to/d395d44fa412d9808e0130857ea74f0b to your computer and use it in GitHub Desktop.
Klipper Pressure Advance Line Calibration Macro
[gcode_macro PA_CAL]
# Klipper pressure advance line calibration macro.
# Usage: PA_CAL BED=100 NOZZLE=240 PA_START=0.0 PA_STOP=0.1 NZL=0.4
# Or you can execute with no parameters, which will use values from printer.cfg and saved_variables.cfg.
# First prints a line with current set PA, then prints 21 additional line segments starting with PA_START, and increasing to PA_STOP.
# Based on http://realdeuce.github.io/Voron/PA/pressure_advance.html
# Cleaned up and moved to using saved_variables.cfg by u/jibbsisme
# Assisted by u/imoftendisgruntled, u/scul86, and thanks to u/beansisfat, u/stray_r
description: Tune Pressure Advance
gcode:
# saved_variables.cfg
{% set speed = printer.save_variables.variables.speed %}
{% set nozzle_temp = printer.save_variables.variables.nozzle_temp %}
{% set bed_temp = printer.save_variables.variables.bed_temp %}
# macro parameters
{% set BED = params.BED|default(bed_temp)|float %}
{% set NOZZLE = params.NOZZLE|default(nozzle_temp)|float %}
{% set PA_START = params.PA_START|default(0.0)|float %}
{% set PA_STOP = params.PA_STOP|default(0.1)|float %}
{% set NZL = params.NZL|default(printer.configfile.config["extruder"]["nozzle_diameter"])|float %}
# calculated variables
{% set PA_STEP = (PA_STOP - PA_START) / 20 |float %}
{% set E20 = (0.1147475 * NZL) * 20|float %}
{% set E40 = (0.1147475 * NZL) * 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 %}
START_PRINT BED_TEMP={BED} NOZZLE_TEMP={NOZZLE}
G21 ; millimeter units
G90 ; absolute XYZ
M83 ; relative E
SET_VELOCITY_LIMIT ACCEL=3000 ACCEL_TO_DECEL=1500
G92 E0
M106 S0
G1 X{(X_MID-40)} Y{(Y_MID-65)} F{speed} ; move to start position
G1 Z0.25 F300 ; move to layer height
G1 E0.75 F1800 ; un-retract
G1 X{(X_MID-20)} Y{(Y_MID-65)} E{E20} F300 ; print line part one
G1 X{(X_MID+20)} Y{(Y_MID-65)} E{E40} F3000 ; print line part two
G1 X{(X_MID+40)} Y{(Y_MID-65)} E{E20} F300 ; print line part three
G1 E-0.75 F1800 ; retract
G1 Z1 F300 ; move above layer height
{% for i in range(0, 21) %}
SET_PRESSURE_ADVANCE ADVANCE={PA_START + (i * PA_STEP)} ; set Pressure Advance
M117 PA={PA_START + (i * PA_STEP)}, STEP={PA_STEP}.
G1 X{(X_MID-40)} Y{(Y_MID-35)+(5*i)} F30000 ; move to start position
G1 Z0.25 F300 ; move to layer height
G1 E0.75 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} F3000 ; print line part two
G1 X{(X_MID+40)} Y{(Y_MID-35)+(5*i)} E{E20} F300 ; print line part three
G1 E-0.75 F1800 ; retract
G1 Z1 F300 ; move above layer height
{% endfor %}
END_PRINT
# these may not work, in which case you will need to count and do the math yourself!
M117 New PA = ({PA_START} + (bestLine * {PA_STEP}) )
{action_respond_info ("Find best line and multiply it by (" + PA_START|string + " + (bestLine * " + PA_STEP|string + ") ) to find your PA setting.")}
[Variables]
speed = 9000
nozzle_temp = 215
bed_temp = 60
@kusold
Copy link

kusold commented Apr 25, 2022

{% set E20 = (0.1147475 * NZL) * 20|float %}

What is the number that is being multiplied by the nozzle size? Is it printer specific, or universal?

Thanks for sharing this.

@m0to
Copy link
Author

m0to commented May 4, 2022

I forked this from @JeremyKennedy so we'd have to ask him.

@JeremyKennedy
Copy link

@kusold
Copy link

kusold commented May 4, 2022

I think I found the math for this a few days ago:

https://github.com/th33xitus/klipper_config/blob/cd6052284861b57bfc51dc5c94824f99ec2dd4df/macros/pa_calibrate.cfg#L61-L69

ksanislo has created a pretty advance macro for this that I'm using. It also is calculating this value dynamically.

I believe that as long as you don't modify the line height, and use 1.75mm filament the hardcoded value should be ok.

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