Skip to content

Instantly share code, notes, and snippets.

@Igloczek
Created February 23, 2025 13:09
Show Gist options
  • Save Igloczek/f74b027594f0e82d5454ecf580bf99cc to your computer and use it in GitHub Desktop.
Save Igloczek/f74b027594f0e82d5454ecf580bf99cc to your computer and use it in GitHub Desktop.
Klipper macro for semi-automated `rotation_distance` calibration

Klipper Extruder Calibration Macros

Why?

Klipper and Mainsail lack built-in extruder calibration. These macros automate it.

How It Works

  1. Mark the filament at 120mm before running the macro.
  2. Run:
    EXTRUDER_CALIBRATION
    
    • Heats nozzle, moves to center, extrudes 100mm, then cools down.
    • Measure remaining filament.
  3. Enter measured value:
    SET_EXTRUDER_CALIBRATION MEASURED_LENGTH=<your_value>
    
    (Replace <your_value> with the actual measurement.)
  4. Update printer.cfg manually with the new rotation_distance shown.

Example Output

Starting calibration...
Extruding 100mm...
Measure remaining filament and enter:
SET_EXTRUDER_CALIBRATION MEASURED_LENGTH=<your_value>
Old rotation_distance: 7.50
New rotation_distance: 8.62
Update printer.cfg manually!

Notes

  • Macros can’t pause for input, so it’s split into two steps.
  • No auto-saving to printer.cfg, manual update required.
[gcode_macro EXTRUDER_CALIBRATION]
description: "Start extruder calibration"
gcode:
RESPOND MSG="Starting calibration, make sure you already marked the 120mm spot on the filament"
RESPOND MSG="Pre-heating nozle"
SET_HEATER_TEMPERATURE HEATER=extruder TARGET=200
RESPOND MSG="Moving to save position"
G28
G90
{% set bed_x = printer.configfile.settings.stepper_x.position_max|float %}
{% set bed_y = printer.configfile.settings.stepper_y.position_max|float %}
{% set center_x = bed_x / 2 %}
{% set center_y = bed_y / 2 %}
G1 X{center_x} Y{center_y} Z100 F6000
RESPOND MSG="Waiting for nozle to be hot enough"
TEMPERATURE_WAIT SENSOR=extruder MINIMUM=200
RESPOND MSG="Extruding 100mm"
G91
M106 S100
G1 E100 F100
M106 S0
G90
RESPOND MSG="Extursion finished, cooling down"
TURN_OFF_HEATERS
RESPOND MSG="Measure remaining filament and enter: SET_EXTRUDER_CALIBRATION MEASURED_LENGTH=your_value_in_mm"
[gcode_macro SET_EXTRUDER_CALIBRATION]
description: "Calculate new rotation_distance"
gcode:
{% if params.MEASURED_LENGTH|float > 0 %}
{% set requested_extrude_distance = 100 %}
{% set initial_mark_distance = 120 %}
{% set subsequent_mark_distance = params.MEASURED_LENGTH|float %}
{% set actual_extrude_distance = initial_mark_distance - subsequent_mark_distance %}
{% set previous_rotation_distance = printer.configfile.settings.extruder.rotation_distance|float %}
{% set new_rotation_distance = previous_rotation_distance * actual_extrude_distance / requested_extrude_distance %}
RESPOND MSG="Old rotation_distance: {previous_rotation_distance}"
RESPOND MSG="New rotation_distance: {new_rotation_distance}"
RESPOND MSG="Update printer.cfg manually!"
{% else %}
RESPOND MSG="ERROR: No valid measurement provided!"
{% endif %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment