Files
klipper-macros/macros/calibration/twist_check.cfg
T
mangavalkandClaude Opus 5.5 92d86d6bfd TWIST_CALIBRATE: axis twist calibration from Mainsail
scripts/twist_check.py runs on the Pi and measures or verifies the nozzle vs
Beacon offset; windows explain each step, show progress and give a verdict,
with an HTML report in config/twist_data/. It refuses to start during a print,
stops on unexpected readings and never changes printer.cfg.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-03 22:53:48 +01:00

116 lines
6.6 KiB
INI

# Axis twist calibration from Mainsail: run TWIST_CALIBRATE and follow the
# windows. Starts scripts/twist_check.py (see there) in the background: inside
# a macro it would deadlock, because the macro holds the G-code queue that the
# script needs.
#
# TWIST_CALIBRATE the start: explains, checks, shows progress while running
# TWIST_STOP stop a run: lifts the nozzle, nozzle heater off. It waits
# for the command that is running (homing, QGL: up to a
# minute); use the emergency stop in a real emergency.
#
# While running: progress on the status line (M117) and in TWIST_CALIBRATE,
# logo as for Z calibration, the result opens a window. Reports and the
# values to apply are in config/twist_data/. Nothing here changes printer.cfg.
#
# Started from Klipper, so restarting the Klipper service ends the run.
[gcode_shell_command _twist_start]
command: bash -c 'setsid nohup python3 "$HOME/printer_data/config/klipper-macros-scripts/twist_check.py" "$1" >/dev/null 2>&1 </dev/null &' twist
timeout: 5.0
verbose: False
# [t]: otherwise pkill also matches this bash command itself
[gcode_shell_command _twist_stop]
command: bash -c 'pkill -TERM -f "[t]wist_check.py (measure|verify)" || true'
timeout: 5.0
verbose: False
# Set by the script; a RESTART clears it
[gcode_macro _TWIST_STATE]
variable_running: 0
variable_mode: ''
variable_done: 0
variable_total: 0
gcode:
[gcode_macro _TWIST_CLOSE]
description: Close the twist calibration window
gcode:
RESPOND TYPE=command MSG="action:prompt_end"
[gcode_macro TWIST_CALIBRATE]
description: Axis twist calibration (nozzle vs Beacon): explains the steps, starts them, shows progress
gcode:
{% set st = printer['gcode_macro _TWIST_STATE'] %}
{% if st.running %}
{% set pct = (100 * st.done / st.total)|int if st.total else 0 %}
{% set n = pct // 5 %}
RESPOND TYPE=command MSG="action:prompt_begin Twist {st.mode} is running"
RESPOND TYPE=command MSG="action:prompt_text Point {st.done} of {st.total}"
RESPOND TYPE=command MSG="action:prompt_text [{'=' * n}{'.' * (20 - n)}] {pct} percent"
RESPOND TYPE=command MSG="action:prompt_text You can close this window: the run continues. The status line shows the progress, and a window opens with the result."
RESPOND TYPE=command MSG="action:prompt_footer_button Stop the run|TWIST_STOP|error"
RESPOND TYPE=command MSG="action:prompt_footer_button Close|_TWIST_CLOSE|secondary"
RESPOND TYPE=command MSG="action:prompt_show"
{% elif printer.print_stats.state in ("printing", "paused") %}
RESPOND TYPE=command MSG="action:prompt_begin Not during a print"
RESPOND TYPE=command MSG="action:prompt_text The calibration moves the nozzle onto the bed. Start it when the print has finished."
RESPOND TYPE=command MSG="action:prompt_footer_button Close|_TWIST_CLOSE|secondary"
RESPOND TYPE=command MSG="action:prompt_show"
{% else %}
RESPOND TYPE=command MSG="action:prompt_begin Axis twist calibration"
RESPOND TYPE=command MSG="action:prompt_text Checks that the nozzle and the Beacon agree across the bed. Run it after work on the X rail, carriage, toolhead, belts or Beacon mount, or when first layers differ left vs right."
RESPOND TYPE=command MSG="action:prompt_text Measure (25 min): measures and tells you whether new values are needed. Start here."
RESPOND TYPE=command MSG="action:prompt_text Verify (17 min): checks the active values with bd_pressure. Run it after applying new values."
RESPOND TYPE=command MSG="action:prompt_button Measure|_TWIST_CHECKLIST MODE=measure|primary"
RESPOND TYPE=command MSG="action:prompt_button Verify|_TWIST_CHECKLIST MODE=verify|secondary"
RESPOND TYPE=command MSG="action:prompt_footer_button Close|_TWIST_CLOSE|secondary"
RESPOND TYPE=command MSG="action:prompt_show"
{% endif %}
[gcode_macro _TWIST_CHECKLIST]
description: Twist calibration: what to check before starting
gcode:
{% set mode = params.MODE|default('measure')|lower %}
{% set bed = printer.heater_bed.temperature %}
RESPOND TYPE=command MSG="action:prompt_begin Before you start {mode}"
RESPOND TYPE=command MSG="action:prompt_text 1. Nothing on the bed: no parts, no tools, plate seated."
RESPOND TYPE=command MSG="action:prompt_text 2. Nozzle clean, no blob: it touches the bed at every point."
RESPOND TYPE=command MSG="action:prompt_text 3. Keep hands out of the printer while it runs."
{% if bed > 40 %}
RESPOND TYPE=command MSG="action:prompt_text Note: the bed is at {bed|int} C. The reference runs were on a cold bed, so wait for it to cool unless you want this on purpose."
{% endif %}
RESPOND TYPE=command MSG="action:prompt_text It heats the nozzle to 150 C, homes, levels and takes {'25' if mode == 'measure' else '17'} minutes. The configuration is not changed."
RESPOND TYPE=command MSG="action:prompt_footer_button Back|TWIST_CALIBRATE|secondary"
RESPOND TYPE=command MSG="action:prompt_footer_button All checked, start|_TWIST_START MODE={mode}|primary"
RESPOND TYPE=command MSG="action:prompt_show"
[gcode_macro _TWIST_START]
description: Start the twist script in the background (use TWIST_CALIBRATE)
gcode:
{% set mode = params.MODE|default('')|lower %}
{% if mode not in ("measure", "verify") %}
_ERROR MSG="_TWIST_START: MODE must be measure or verify"
{% elif printer.print_stats.state in ("printing", "paused") %}
_ERROR MSG="Twist calibration: a print is running" DISPLAY="Twist: not during a print"
{% elif printer['gcode_macro _TWIST_STATE'].running %}
_ERROR MSG="Twist calibration is already running. TWIST_CALIBRATE shows it, TWIST_STOP stops it." DISPLAY="Twist: already running"
{% else %}
M117 Twist {mode}: starting
RESPOND TYPE=command MSG="action:prompt_begin Twist {mode} started"
RESPOND TYPE=command MSG="action:prompt_text You can close this window: the run continues. The status line shows the progress, and a window opens with the result."
RESPOND TYPE=command MSG="action:prompt_footer_button Stop the run|TWIST_STOP|error"
RESPOND TYPE=command MSG="action:prompt_footer_button Close|_TWIST_CLOSE|secondary"
RESPOND TYPE=command MSG="action:prompt_show"
RUN_SHELL_COMMAND CMD=_twist_start PARAMS={mode}
{% endif %}
[gcode_macro TWIST_STOP]
description: Stop a running twist calibration: lifts the nozzle, nozzle heater off
gcode:
RESPOND TYPE=command MSG="action:prompt_end"
M117 Twist: stopping
RUN_SHELL_COMMAND CMD=_twist_stop
# Also clears the flag when a run has died without clearing it itself
SET_GCODE_VARIABLE MACRO=_TWIST_STATE VARIABLE=running VALUE=0