feat: autotune pipeline — structured bring-up sweep for gain, follower bias, and comparator threshold #3
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Summary
The EasyPiezi firmware already contains the core tuning primitives (
adjustFollow,adjustComp,adjustGain,readVcc,readVin) but relies on the operator to arrive at correct values through manual trial-and-error. This issue tracks adding a structured autotune sequence that sweeps those same controls in a defined order to converge on reliable trigger behavior automatically.Motivation
The bring-up process today requires the user to:
This is a barrier for non-technical users and introduces inconsistency across machines. An autotune sequence — modeled on Marlin's
M303heater PID autotune — would reduce bring-up to a single command.Algorithm (proposed)
Step 1 — Baseline capture
A0(follower sense) andA1(comparator sense) at 100Hz for 500msvFollow_idleandvComp_idle(mean + stddev)Step 2 — Trigger floor detection (binary search on GAIN)
GAINregister from 0 to max until the comparator output (INT0/ D7) firesGAIN_min= lowest gain that produces a clean triggerGAIN_min + 1step as headroomStep 3 — Follower bias (VFOL) margin
VFOL(OCR2B) downward from mid-railVFOLvalue where the follower output still tracks the idle piezo signal without false-triggeringVFOL= that value + one step marginStep 4 — Comparator threshold (VCOMP) margin
VCOMP(OC1A) upward from the follower idle outputVCOMPvalue that suppresses all false triggers during a quiet 500ms windowVCOMP= that value +HYSTmargin (default: 10 ADC counts or configurable)Step 5 — Validation pass
GAIN,VFOL,VCOMP,HYSTover serialIntegration options
Option A — Standalone firmware autotune mode
Add an
AUTOTUNEbuild flag or serial command (AT\n) that runs the sequence on boot or on demand, writes results to EEPROM, and resumes normal operation.Option B — Marlin G-code integration
Expose as a custom G-code (e.g.
M710orG38.9) via Marlin's Z-probe plugin interface. Marlin sends the command, EasyPiezi runs the sweep and responds withok GAIN=n VFOL=n VCOMP=n HYST=n. Marlin stores these in EEPROM viaM500.Option C — Host-side tuning script
Python script (or Klipper macro) that communicates with EasyPiezi over serial, drives the sweep externally, and writes final values back via serial commands. Lowest firmware complexity, most portable.
Option A should ship first; B and C can follow.
Technical notes
readVcc()uses the internal 1.1V bandgap reference trick — this is accurate to ~1-2% and is sufficient for autotune convergence. No external reference needed.vComp_idlestddev > threshold (indicates electrical noise problem that tuning cannot solve — check grounding and cable routing).Deliverables
autotune.cpp/autotune.h— sweep logic, decoupled from main loopATcommand)M710response conventionRelated