#!/usr/bin/env python3 """Nozzle vs Beacon offset across the bed (axis twist compensation). Beacon sits 20 mm behind the nozzle (y_offset 20). If the toolhead rolls as it moves (X rail twist), Beacon's proximity reading and the nozzle disagree by an amount that depends on X (and a little on Y). Beacon meshes and QGL inherit that error; Klipper's [axis_twist_compensation] corrects it. Normally started from Mainsail with the macro TWIST_CALIBRATE, which explains each step (macros/calibration/twist_check.cfg). From a shell on the Pi: python3 ~/klipper-macros/scripts/twist_check.py measure python3 ~/klipper-macros/scripts/twist_check.py verify python3 ~/klipper-macros/scripts/twist_check.py analyse Modes: measure ~25 min. Proximity (nozzle at x, y-20, so Beacon sees bed x,y), then Beacon contact with the nozzle moved onto x,y. Rows y 55/175/295 and a Y line at x=175. Works with or without compensation active: Beacon adds the same correction to proximity and contact, so contact - proximity is the raw error. New values replace the old ones. Result: apply, leave it, or re-run. It never changes printer.cfg; a ready block is saved in twist_suggested.cfg. verify ~17 min. Proximity (compensated), then BDP_TOUCH at x,y (bd_pressure, Z from the stepper, not compensated), then Beacon contact. Rows y 175/295. With good compensation, bdp - proximity is flat along X. Both: BED_MESH_CLEAR (the active mesh only; saved profiles stay), nozzle 150C, HOME, QGL, G28 Z, nozzle wipe (BLOBIFIER_CLEAN), contact Z, 2 passes (second in reverse order to cancel slow drift), then Z10 and nozzle off. The bed heater is not touched. Safety: refuses to start when Klipper is not ready, a print runs or another run is active. Travel moves run under the nozzle guard. Every reading is checked before the nozzle goes low, and anything unexpected stops the run. A stop (TWIST_STOP, Ctrl-C, an error) lifts the nozzle and switches its heater off, except when a print has started: then it leaves the printer alone. Writes twist__.jsonl and .html (report with charts) in ~/printer_data/config/twist_data/, plus twist_run.log with all messages. """ import argparse import datetime import html import json import os import re import signal import statistics as st import sys import time import urllib.error import urllib.parse import urllib.request XS = [25, 75, 125, 175, 225, 275, 325] YS = [55, 95, 135, 175, 215, 255, 295] PASSES = 2 TRIGGER = 2.0 # [beacon] trigger_distance: proximity result - 2 = bed BDP_ABOVE = 0.15 # BDP_TOUCH start height above the expected bed # Result checks (mm) # Set so the good 2026-11-02 runs pass: rows 0.012, residual 0.012, one point # with pass spread 0.031 (the next 0.012, median 0.004), verify flat 0.022. ROWS_AGREE = 0.015 # each X row vs the mean: else X and Y are not separable RESIDUAL = 0.015 # worst error left by the X+Y model PASS_SPREAD = 0.02 # same point in the two passes: dirty nozzle or bed when PASS_OUTLIERS = 1 # more points than this are above it, PASS_WORST = 0.04 # or any point above this MIN_CHANGE = 0.01 # smaller differences to the active values: leave it MAX_COMP = 0.3 # larger: something is mechanically wrong, do not correct FLAT_GOOD = 0.025 # verify: bdp - proximity range along one row FLAT_MAX = 0.035 # Reading checks during the run (mm); outside these the run stops PROX_LIMIT = 0.5 # bed height from Beacon vs Z 0: something on the bed? CONTACT_LIMIT = 0.25 # nozzle contact vs Beacon: blob on the nozzle? BED_WARN = 40 # C: all reference runs were on a cold bed DATA_DIR = os.path.expanduser("~/printer_data/config/twist_data") LOG = None class Problem(Exception): """Something the user has to look at; the run stops.""" class Stopped(Exception): """TWIST_STOP or Ctrl-C.""" def log(text): line = "%s %s" % (time.strftime("%H:%M:%S"), text) try: print(line, flush=True) except OSError: pass # SSH window closed: the run continues, the log file stays if LOG: LOG.write(line + "\n") LOG.flush() def clean(text): # Double quotes end a G-code parameter and ; starts a comment return str(text).replace('"', "'").replace(";", ",") class Printer: def __init__(self, host): self.host = host def get(self, path, timeout=10): try: with urllib.request.urlopen(self.host + path, timeout=timeout) as r: return json.load(r)["result"] except (OSError, ValueError, KeyError) as e: raise Problem("No answer from Moonraker at %s (%s)" % (self.host, e)) def query(self, *objects): path = "/printer/objects/query?" + "&".join( urllib.parse.quote(o) for o in objects) return self.get(path)["status"] def gcode(self, script, timeout=900): t0 = time.time() url = (self.host + "/printer/gcode/script?script=" + urllib.parse.quote(script)) req = urllib.request.Request(url, method="POST") try: urllib.request.urlopen(req, timeout=timeout).read() except urllib.error.HTTPError as e: raise Problem(self.error_text(e, script)) except OSError as e: raise Problem("Lost the connection during %s (%s)" % (script, e)) store = self.get("/server/gcode_store?count=40")["gcode_store"] return [e["message"] for e in store if e["time"] >= t0 - 1 and e["type"] == "response"] def error_text(self, e, script): try: text = json.load(e)["error"]["message"] except (ValueError, KeyError, TypeError): text = "HTTP %d" % e.code return "%s failed: %s" % (script.split()[0], text) def say(self, text): log(text) self.gcode('RESPOND MSG="Twist: %s"' % clean(text), timeout=60) def dialog(self, title, lines, buttons=()): # Mainsail shows this as a window (needs [respond]) out = ["action:prompt_begin " + title] out += ["action:prompt_text " + line for line in lines] out += ["action:prompt_button %s|%s|%s" % b for b in buttons] out += ["action:prompt_footer_button Close|_TWIST_CLOSE|secondary", "action:prompt_show"] self.gcode("\n".join('RESPOND TYPE=command MSG="%s"' % clean(m) for m in out), timeout=60) def state(self, running, mode=None, done=None, total=None): cmd = "SET_GCODE_VARIABLE MACRO=_TWIST_STATE VARIABLE=%s VALUE=%s" out = [cmd % ("running", running)] if mode is not None: out.append(cmd % ("mode", "\"'%s'\"" % mode)) if done is not None: out.append(cmd % ("done", done)) if total is not None: out.append(cmd % ("total", total)) return "\n".join(out) def logo(self, status): # The Stealthburner logo: visible from across the room objects = self.get("/printer/objects/list")["objects"] if status == "error": if "gcode_macro _STATUS_FLASH" in objects: return "_STATUS_FLASH STATE=error" elif "gcode_macro _STATUS" in objects: return "_STATUS STATE=" + status return "" def num(lines, pattern, what): for line in reversed(lines): m = re.search(pattern, line) if m: return float(m.group(1)) raise Problem("%s gave no reading (expected %s in the answer)" % (what, pattern)) def print_active(p): s = p.query("print_stats", "pause_resume", "virtual_sdcard") return (s["print_stats"]["state"] in ("printing", "paused") or s["pause_resume"]["is_paused"] or s["virtual_sdcard"]["is_active"]) def check_ready(p): info = p.get("/printer/info") if info["state"] != "ready": raise Problem("Klipper is not ready (%s): %s" % (info["state"], info.get("state_message", "").strip())) def preflight(p, mode): check_ready(p) if print_active(p): raise Problem("A print is running or paused. Try again afterwards.") objects = p.get("/printer/objects/list")["objects"] settings = p.query("configfile")["configfile"]["settings"] missing = [m for m in ("HOME", "_NOZZLE_CONTACT_TEMP", "_TWIST_STATE", "_GUARD_ON", "BLOBIFIER_CLEAN") if "gcode_macro " + m not in objects] missing += [s for s in ["beacon"] + (["bdp_touch"] * (mode == "verify")) if s not in settings] if missing: raise Problem("Not found in Klipper: %s. Is klipper-macros up to " "date and Klipper restarted?" % ", ".join(missing)) if p.query("gcode_macro _TWIST_STATE")[ "gcode_macro _TWIST_STATE"]["running"]: raise Problem("Another twist run is active. TWIST_STOP stops it (and " "clears this flag if that run has died).") bed = p.query("heater_bed")["heater_bed"]["temperature"] if bed > BED_WARN: p.say("Note: bed at %d C. The reference runs were on a cold bed." % bed) def bar(done, total, width=20): n = width * done // total if total else 0 return "[%s%s]" % ("=" * n, "." * (width - n)) def eta(start, done, total): if not done: return "" left = (time.time() - start) / done * (total - done) return ", ~%d min left" % (left // 60 + 1) def travel(p, x, y): # Nothing may touch the nozzle here: an object on the bed stops Klipper p.gcode("_GUARD_ON PHASE=twist_travel\nG1 X%d Y%d Z5 F9000\n_GUARD_OFF" % (x, y)) def measure_point(p, mode, x, y): r = {"x": x, "y": y, "t": time.time()} travel(p, x, y - 20) r["prox"] = num(p.gcode("PROBE PROBE_METHOD=proximity"), r"z=(-?[\d.]+)", "Beacon proximity") bed = r["prox"] - TRIGGER if abs(bed) > PROX_LIMIT: raise Problem("Beacon sees the bed at %+.3f mm at x=%d y=%d (limit " "%.1f). Is something on the bed, or the plate not " "seated?" % (bed, x, y, PROX_LIMIT)) p.gcode("G1 Z5 F600") travel(p, x, y) if mode == "verify": p.gcode("G1 Z%.3f F300" % (bed + BDP_ABOVE)) r["bdp"] = num(p.gcode("BDP_TOUCH FLOOR=0.7"), r"fit z (-?[\d.]+)", "BDP_TOUCH") p.gcode("G1 Z5 F600") r["contact"] = num(p.gcode("PROBE PROBE_METHOD=contact SAMPLES=1"), r"z=(-?[\d.]+)", "Beacon contact") p.gcode("G1 Z5 F600") if abs(r["contact"] - bed) > CONTACT_LIMIT: raise Problem("Nozzle and Beacon differ by %.3f mm at x=%d y=%d (limit" " %.2f). Clean the nozzle; if it stays, check the " "toolhead and Beacon mount." % (abs(r["contact"] - bed), x, y, CONTACT_LIMIT)) return r def run_points(p, mode, path): if mode == "measure": pts = [(x, y) for y in (55, 175, 295) for x in XS] pts += [(175, y) for y in YS if y not in (55, 175, 295)] else: pts = [(x, y) for y in (175, 295) for x in XS] total = len(pts) * PASSES p.gcode(p.state(1, mode, 0, total)) p.gcode("M117 Twist %s: heating, homing, QGL" % mode) p.say("%s started: heating the nozzle to 150C, homing, levelling" % mode) p.gcode("BED_MESH_CLEAR") p.gcode("_NOZZLE_CONTACT_TEMP WAIT=1") p.gcode("HOME") p.gcode("QUAD_GANTRY_LEVEL") # As in PRINT_START. Plastic left on the nozzle sets the contact Z too high # and squeezes away during the run: the touches then sink by ~0.004 mm per # point (2026-11-04). QGL moved Z, so home again before the brush takes the # nozzle below Z0. The bristles press on the nozzle: guard in log mode. p.gcode("M117 Twist %s: cleaning the nozzle" % mode) p.gcode("G28 Z") p.gcode("_GUARD_ON PHASE=brush MODE=log\nBLOBIFIER_CLEAN\n_GUARD_OFF") p.gcode("G28 Z METHOD=CONTACT CALIBRATE=1") p.gcode("G90") p.say("%d points, %d passes. The nozzle will touch the bed at each one." % (len(pts), PASSES)) done = 0 start = time.time() with open(path, "w") as out: for rep in range(PASSES): for (x, y) in (pts if rep == 0 else pts[::-1]): check_ready(p) if print_active(p): raise Problem("A print started during the run.") r = measure_point(p, mode, x, y) r["rep"] = rep out.write(json.dumps(r) + "\n") out.flush() done += 1 text = "%s %d%% %s%s" % (mode, 100 * done // total, bar(done, total, 10), eta(start, done, total)) p.gcode(p.state(1, done=done) + "\nM117 Twist " + clean(text)) log("%d/%d %s" % (done, total, r)) if done % 10 == 0: p.say(text) def load(path): groups = {} with open(path) as f: for line in f: r = json.loads(line) groups.setdefault((r["x"], r["y"]), []).append(r) return groups def mean(groups, key, fn): out = {} for k, rs in groups.items(): v = [fn(r) for r in rs if r.get(key) is not None and r.get("prox") is not None] if v: out[k] = (st.mean(v), max(v) - min(v)) return out def noisy_passes(spreads): return (max(spreads) > PASS_WORST or sum(v > PASS_SPREAD for v in spreads) > PASS_OUTLIERS) def analyse_verify(groups): rows = sorted({y for x, y in groups}, reverse=True) prox = mean(groups, "bdp", lambda r: r["bdp"] - (r["prox"] - TRIGGER)) cont = mean(groups, "bdp", lambda r: r["bdp"] - r["contact"]) spreads = [v[1] for v in prox.values()] + [v[1] for v in cont.values()] spread = max(spreads) flat = {y: max(prox[(x, y)][0] for x in XS) - min(prox[(x, y)][0] for x in XS) for y in rows} worst = max(flat.values()) res = {"mode": "verify", "rows": rows, "series": {y: [prox[(x, y)][0] for x in XS] for y in rows}, "contact": {y: [cont[(x, y)][0] for x in XS] for y in rows}, "block": None} facts = ["Flat along X: worst row varies %.3f mm (good: up to %.3f)." % (worst, FLAT_GOOD), "Same point between passes: up to %.3f mm." % spread] if noisy_passes(spreads): res.update(level="bad", title="Verify: run it again", lines=facts + [ "The two passes disagree: clean the nozzle and bed, then run " "Verify again."], retry="verify") elif worst <= FLAT_GOOD: res.update(level="good", title="Verify: compensation is good", lines=facts + ["Nothing to do. PRINT_START makes a new " "mesh by itself."]) elif worst <= FLAT_MAX: res.update(level="warn", title="Verify: acceptable", lines=facts + [ "Slightly more than ideal but usable. Check a first layer."]) else: res.update(level="bad", title="Verify: compensation is off", lines=facts + ["Run Measure again and apply the new " "values. If it stays, check the X rail, " "belts and Beacon mount."], retry="measure") return res def analyse_measure(groups, current): full = mean(groups, "contact", lambda r: r["contact"] - (r["prox"] - TRIGGER)) d = {k: v[0] for k, v in full.items()} spreads = [v[1] for v in full.values()] spread = max(spreads) xrows = [y for y in (55, 175, 295) if all((x, y) in d for x in XS)] if not xrows or not all((175, y) in d for y in YS): raise Problem("Not all points were measured; nothing to conclude.") rel = {y: [d[(x, y)] - d[(175, y)] for x in XS] for y in xrows} xprof = [st.mean(rel[y][i] for y in xrows) for i in range(len(XS))] yprof = [d[(175, y)] - d[(175, 175)] for y in YS] agree = max(abs(rel[y][i] - xprof[i]) for y in xrows for i in range(len(XS))) worst = 0. for y in xrows: for i, x in enumerate(XS): left = d[(x, y)] - d[(175, 175)] - xprof[i] - yprof[YS.index(y)] worst = max(worst, abs(left)) biggest = max(abs(v) for v in xprof + yprof) change = None if current and (current.get("z_compensations") or [])[:1] != []: old_x = current.get("z_compensations") or [] old_y = current.get("zy_compensations") or [] if len(old_x) == len(XS) and len(old_y) == len(YS): change = max(abs(a - b) for a, b in zip(xprof + yprof, old_x + old_y)) fmt = lambda vals: ", ".join("%.4f" % (v + 0.) for v in vals) block = """[axis_twist_compensation] # Measured %s with scripts/twist_check.py measure: # nozzle contact vs Beacon proximity, 0 at 175,175 calibrate_start_x: %d calibrate_end_x: %d calibrate_y: 175 z_compensations: %s compensation_start_x: %d compensation_end_x: %d calibrate_start_y: %d calibrate_end_y: %d calibrate_x: 175 zy_compensations: %s compensation_start_y: %d compensation_end_y: %d """ % (datetime.date.today().isoformat(), XS[0], XS[-1], fmt(xprof), XS[0], XS[-1], YS[0], YS[-1], fmt(yprof), YS[0], YS[-1]) res = {"mode": "measure", "xprof": xprof, "yprof": yprof, "rel": rel, "block": None, "suggested": block} facts = [ "Largest error: %.3f mm. The rows agree within %.3f (limit %.3f)." % (biggest, agree, ROWS_AGREE), "Left after correction: %.3f (limit %.3f). Passes: up to %.3f." % (worst, RESIDUAL, spread)] if noisy_passes(spreads): res.update(level="bad", title="Measure: run it again", lines=facts + [ "The two passes disagree: clean the nozzle and bed, then run " "Measure again. Nothing changed."], retry="measure") elif biggest > MAX_COMP: res.update(level="bad", title="Measure: do not apply", lines=facts + [ "This error is too large to correct safely. Check the X rail, " "belts, toolhead and Beacon mount. Nothing changed."]) elif agree > ROWS_AGREE or worst > RESIDUAL: res.update(level="bad", title="Measure: do not apply", lines=facts + [ "The error does not split into an X and a Y part, so this " "compensation cannot correct it. Check the mechanics first. " "Nothing changed."]) elif change is not None and change < MIN_CHANGE: res.update(level="good", title="Measure: no change needed", lines=facts + ["The active values are still right (largest" " difference %.3f mm). Nothing to do." % change]) else: how = ("Largest difference to the active values: %.3f mm." % change if change is not None else "No compensation is active yet.") res.update(level="warn", title="Measure: new values recommended", block=block, lines=facts + [ how, "1. Open twist_data/twist_suggested.cfg in Mainsail " "(Machine, config files) and copy it.", "2. In printer.cfg replace the [axis_twist_compensation" "] block with it (a backup copy of printer.cfg first).", "3. Save and Restart, then run Verify here."]) return res def analyse(path, current=None): groups = load(path) if any("bdp" in r for rs in groups.values() for r in rs): res = analyse_verify(groups) else: res = analyse_measure(groups, current) res["file"] = os.path.basename(path) return res def report_console(res): print() if res["mode"] == "verify": print("bdp fit - proximity (flat = compensation good):") for y in res["rows"]: print("y=%3d " % y + " ".join("%+.3f" % v for v in res["series"][y])) print("bdp fit - Beacon contact (shows Beacon's contact correction):") for y in res["rows"]: print("y=%3d " % y + " ".join("%+.3f" % v for v in res["contact"][y])) else: print("contact - proximity, each row minus its own x=175:") for y, vals in res["rel"].items(): print("y=%3d " % y + " ".join("%+.3f" % v for v in vals)) print("X profile:", " ".join("%+.4f" % v for v in res["xprof"])) print("Y profile:", " ".join("%+.4f" % v for v in res["yprof"])) print("\n" + res["title"].upper()) for line in res["lines"]: print(" " + line) if res["block"]: print("\n" + res["block"]) COLORS = ["#2a78c6", "#d95f02", "#1b9e77", "#7570b3"] def chart(title, xs, series, xlabel): # series: list of (name, values); a plain SVG line chart in mm w, h, ml, mr, mt, mb = 640, 300, 62, 16, 54, 44 vals = [v for _, vs in series for v in vs] lo, hi = min(vals + [-0.02]), max(vals + [0.02]) pad = (hi - lo) * 0.1 lo, hi = lo - pad, hi + pad px = lambda x: ml + (x - xs[0]) * (w - ml - mr) / (xs[-1] - xs[0]) py = lambda v: mt + (hi - v) * (h - mt - mb) / (hi - lo) out = ['' % (w, h, html.escape(title)), '%s' % (ml, html.escape(title))] step = 0.01 if hi - lo < 0.08 else (0.02 if hi - lo < 0.16 else 0.05) v = int(lo / step) * step while v <= hi: if v >= lo: out.append('' % (ml, w - mr, py(v), py(v), "z" if abs(v) < step / 2 else "g")) out.append('' '%+.2f' % (ml - 6, py(v) + 4, v + 0.)) v += step for x in xs: out.append('%d' '' % (px(x), h - mb + 16, x)) out.append('%s (mm)' '' % ((ml + w - mr) // 2, h - 8, xlabel)) for i, (name, vs) in enumerate(series): c = COLORS[i % len(COLORS)] pts = " ".join("%.1f,%.1f" % (px(x), py(v)) for x, v in zip(xs, vs)) out.append('' % (pts, c)) out += ['%s, %d: ' '%+.3f mm' % (px(x), py(v), c, html.escape(name), x, v) for x, v in zip(xs, vs)] out.append('' '%s' % (ml + i * 70, 40, c, c, html.escape(name))) out.append("") return "\n".join(out) def report_html(res, path): if res["mode"] == "verify": charts = [ chart("bdp fit - proximity: should be a flat line", XS, [("y=%d" % y, res["series"][y]) for y in res["rows"]], "X"), chart("bdp fit - Beacon contact: minus the correction", XS, [("y=%d" % y, res["contact"][y]) for y in res["rows"]], "X")] else: charts = [ chart("Error along X, per row: lines should overlap", XS, [("y=%d" % y, v) for y, v in res["rel"].items()] + [("mean", res["xprof"])], "X"), chart("Error along Y at x=175", YS, [("Y", res["yprof"])], "Y")] block = res["block"] or res.get("suggested") or "" page = """ Twist %s

%s

%s
%s %s %s, written %s """ % (res["mode"], res["level"], html.escape(res["title"]), "".join("

%s

" % html.escape(s) for s in res["lines"]), "\n".join(charts), ("
%s
" % html.escape(block)) if block else "", html.escape(res["file"]), datetime.datetime.now().strftime("%Y-%m-%d %H:%M")) with open(path, "w", encoding="utf-8") as f: f.write(page) def cleanup(p): # Never fight a print that has started, and do not raise from here try: check_ready(p) if print_active(p): log("A print is active: leaving the printer alone.") return True homed = p.query("toolhead")["toolhead"]["homed_axes"] p.gcode("_GUARD_OFF", timeout=120) if "z" in homed: p.gcode("G90\nG1 Z10 F600", timeout=120) p.gcode("M104 S0", timeout=120) return True except Exception as e: log("Could not finish cleanly: %s" % e) log("Check the printer: the nozzle heater may still be on until " "Klipper restarts.") return False def on_signal(signum, frame): raise Stopped() def run(mode, host): global LOG os.makedirs(DATA_DIR, exist_ok=True) LOG = open(os.path.join(DATA_DIR, "twist_run.log"), "a") p = Printer(host) stamp = datetime.datetime.now().strftime("%Y%m%d_%H%M") path = os.path.join(DATA_DIR, "twist_%s_%s.jsonl" % (mode, stamp)) report = path[:-len(".jsonl")] + ".html" log("=== %s, writing %s" % (mode, path)) failure = None res = None started = False signal.signal(signal.SIGTERM, on_signal) signal.signal(signal.SIGINT, on_signal) if hasattr(signal, "SIGHUP"): # A closed SSH window must not end the run halfway signal.signal(signal.SIGHUP, signal.SIG_IGN) try: preflight(p, mode) started = True p.gcode(p.logo("calibrating_z") or "M400") run_points(p, mode, path) res = analyse(path, p.query("configfile")["configfile"]["settings"] .get("axis_twist_compensation")) except Stopped: failure = ("Stopped", ["Stopped on request. The result is not used " "and nothing changed."]) except Problem as e: failure = ("Twist %s stopped" % mode, [ str(e), "Nothing changed in the configuration. " "Fix this, then start again with TWIST_CALIBRATE."]) except Exception as e: # Whatever went wrong, the nozzle must still be lifted and switched off failure = ("Twist %s stopped" % mode, [ "Unexpected error: %r" % e, "Nothing changed in the configuration." " See twist_data/twist_run.log."]) signal.signal(signal.SIGTERM, signal.SIG_IGN) signal.signal(signal.SIGINT, signal.SIG_IGN) if started: cleanup(p) if res: try: if res["block"]: with open(os.path.join(DATA_DIR, "twist_suggested.cfg"), "w") as f: f.write(res["block"]) report_html(res, report) report_console(res) except Exception as e: log("Could not write the report: %r" % e) log(res["title"]) return finish(p, res, failure, os.path.basename(report), started) def finish(p, res, failure, report_name, started): # Klipper may be down here; the log and the shell keep the message try: if res: buttons = [] if res.get("retry"): buttons.append(("Run %s again" % res["retry"], "_TWIST_CHECKLIST MODE=%s" % res["retry"], "primary")) elif res["level"] == "warn" and res["mode"] == "measure": buttons.append(("Run Verify (after restart)", "_TWIST_CHECKLIST MODE=verify", "primary")) p.gcode(p.state(0)) logo = p.logo("error" if res["level"] == "bad" else "ready") p.gcode(logo or "M400") p.gcode("M117 %s" % clean(res["title"])) for line in res["lines"]: p.say(line) p.dialog(res["title"], res["lines"] + [ "Report with charts: twist_data/" + report_name], buttons) else: title, lines = failure # Refused at the start: an active run or print keeps its flag, # status line and logo if started: p.gcode(p.state(0)) logo = p.logo("error" if title != "Stopped" else "ready") p.gcode(logo or "M400") p.gcode("M117 %s" % clean(title)) p.dialog(title, lines) except Exception as e: log("Could not show the result in Mainsail: %s" % e) if failure: log(failure[0] + ": " + " ".join(failure[1])) return 1 return 0 if res["level"] != "bad" else 2 def main(): ap = argparse.ArgumentParser( description="Measure and verify axis twist compensation (nozzle vs " "Beacon). Normally started with the macro TWIST_CALIBRATE.") ap.add_argument("mode", choices=("measure", "verify", "analyse")) ap.add_argument("file", nargs="?", help="analyse: a saved .jsonl") ap.add_argument("--host", default="http://localhost:7125", help="Moonraker (its own port: nginx ends requests after " "60 s)") a = ap.parse_args() if a.mode != "analyse": return run(a.mode, a.host.rstrip("/")) if not a.file: ap.error("analyse needs a file") current = None try: current = Printer(a.host.rstrip("/")).query("configfile")[ "configfile"]["settings"].get("axis_twist_compensation") except Problem: print("Printer not reachable: not comparing with the active values") res = analyse(a.file, current) report_console(res) report = os.path.splitext(a.file)[0] + ".html" report_html(res, report) print("\nReport:", report) return 0 if __name__ == "__main__": sys.exit(main())