Skip to content

Firmware 2.3.0: the limit switches decide the ends; position after a power cut - #27

Draft
daniel-frenkel wants to merge 30 commits into
mainfrom
fw/position-truth
Draft

daniel-frenkel wants to merge 30 commits into
mainfrom
fw/position-truth

Conversation

@daniel-frenkel

@daniel-frenkel daniel-frenkel commented Nov 7, 2026 •

Copy link
Copy Markdown
Member

Draft: merges only after 2.2.0 is tagged from the benched build.

What

Daniel's 2.3.0 decisions: the limit switches are the source of truth for the ends, and the position survives a power cut. Review items (Amendments 1–3 in the predictions file) are included.

The ends. The cover shows 0 % (closed) only while S5 is pressed and 100 % (fully open) only while S4 is pressed, at startup and after every move. Otherwise it stays within 1–99 %.

  • A close that ends without S5 keeps its fault and makes the position unknown, so the next Close runs until S5.
  • An open that ends without S4 shows 99 % with its fault and doesn't seek S4.
  • A pressed switch wins: at startup, a pressed S5 or S4 sets the position even if the last move was cut off. A Close (Open) made while the position is unknown checks the switch's level first; if it is already pressed, the position is settled and no move starts.

Saving (option A). Today the position reaches flash only once a minute (ESPHome's default flash_write_interval), so a power cut or an unplanned restart can bring back an old position.

  • The position is now written at once at every stop (position_commit), always after the motor has been halted: STOP, both switches, stalls, the thermal lockout, calibration and the end of a move.
  • A move-in-progress marker is written before every move starts (move_begin, from drive_open, drive_close and the proportional set-position move) and cleared at the stop.
  • Nothing is written while the motor moves. ESPHome's periodic flush is off (flash_write_interval: never); a 30 s flush runs only while idle, and move_begin and the Speed setting flush only while idle. Settings changed while idle reach flash within 30 s; one changed during a move is saved at its stop. ESPHome's shutdown flush stays.
  • Flash wear: about 3 small writes per move, roughly 12 erase cycles per NVS page per year at 50 moves a day.

Unknown position. It happens in three cases:

  • at startup, the marker is still set and neither switch is pressed;
  • at startup, the position was saved at an end whose switch isn't pressed (checked 500 ms in, once the switch debounce has settled);
  • a close ends without S5.

While the position is unknown:

  • the cover shows 50 % and Fault shows position unknown - close or open fully;
  • the next Close runs until S5, at most a full stroke + 2 mm (seek_margin_mm), with stall detection on;
  • the next Open runs until S4 and doesn't learn;
  • Set position closes to S5 first, then goes to the target (both logged);
  • an S5 or S4 press makes it known again.

Restarts and updates. The motor stops when the unit restarts or an update begins.

After a stall stop (Amendment 5; Daniel's call, he may strike it): the next Close or Open runs until its limit switch, as when the position is unknown, while the cover keeps showing the counted position. On the bench a stall left the count 2.12 mm off, and the next Close stopped short and faulted. Also, the detector switches log "disabled" only when they really stay off.

Logging for the bench. The S5 handler logs the count at its trigger, and an unknown-position close logs the count it starts from, so the recovery close after a power cut shows where the carriage really stopped.

Also:

  • the 200 ms loop's comment is corrected;
  • docs: firmware/README.md (ends rule, "Position after a power cut", fault rows, entity reference), the README feature lines, and a CHANGELOG 2.3.0 section;
  • the bench build gains "BENCH: close switch (S5) dead, kept across restarts", for bench check PC4;
  • tools/bench/pc_run.py runs PC1–PC6 with the power cut by hand, and pc_sim.py is its dry run. It uses the bench interlock and prompts from bench_common.py (Amendment 6). No case, cut or move is sent unless the open guard is connected and watching. Outside a cut, the script sends STOP and ends the case if the guard drops mid-move. A case that ends on the interlock sends STOP first. A guard missing at case start ends only that case. Every pull, plug-in, countdown and alert is shown on screen and spoken. Before a cue, nothing shown or spoken contains an action word: the countdown reads "Power cut: 3, 2, 1", and only the cue says "pull … now" (Amendment 7).

Checks

Registered before each code change in tools/regression-gate/predictions/2026-11-position-truth.md (original and Amendments 1–7).

Check Result Sabotage
C1 build esphome config valid; compile succeeds (flash 71.1 %, 1,305,292 bytes); the bench build resolves an undefined id in position_commit is refused by the compiler
G1 entity gate clean a renamed entity is flagged
G2 behaviour gate exactly as re-registered in Amendment 2. Changed (17): the cover lambda and all four actions, on_boot 500 and 900, on_shutdown 600, drive_open, drive_close, motion_halt, auto_calibrate, the 200 ms completion poll, both limit-switch handlers, the Speed setting, and the Fault lambda. New (3): move_begin, position_commit, the 30 s flush. Dropped: none. Amendment 5 vs the previous head: 8 changed and 1 dropped (the StallGuard switch's off-action, which held only the log line), as re-registered in its note before G2 ran. with the base's drive_open spliced in, drive_open is no longer listed
R1 resolved-config facts 11/11, with the flush fact as re-registered (the only flushes are move_begin, position_commit, the Speed setting and the 30 s flush) the base fails all 11; a head without the marker in drive_open fails that fact; a head with an extra flush fails the flush fact
R2 (Amendment 1) 12/12: switch wins at boot; the level check in both seeks; halt before every commit; the flush guards; the 200 ms poll no longer writes the saved position the base fails all 12; a drive_close without the level check, a motion_halt that commits first, and a 30 s flush without its idle guard each fail exactly their fact
R3 (Amendment 2) 2/2: the S5 trigger count and the unknown-position close's start count are logged the base fails both
R5 (Amendment 5) 8/8: seek_next persisted and set by a stall stop outside calibration; both seeks apply it; the open learns only without it; S5/S4 clear it; the cover doesn't read it; the detector switches log "disabled" only when they stay off the base fails R5; a head whose stall stop doesn't set it, one whose S5 doesn't clear it, and one whose cover shows 50 % for it each fail exactly that fact
D1 docs 66 entities in the config, 66 reference rows; all links resolve a dropped row is reported
Bench script the dry run passes PC1–PC6; each injected fault (the unit never goes off, the old position comes back, a late PC6 cut, another MAC, the wrong calibrated speed) is reported at the right step; with 4 minutes of window left no case starts the injected faults are the sabotages
Bench interlock and prompts (Amendment 6) 26/26 as predicted on one head (d0bd766). No guard: nothing sent. Guard drops mid-move: STOP in 0.2 s. During cuts the guard's lost connection neither stops nor ends a case, and no recovery move goes out on its old connection (also with a guard that notices late). A refused cut sends STOP before the case is recorded. With a guard that never returns, every remaining case reads "guard missing", nothing is sent, and the run exits cleanly. Every pull and plug-in is shown and spoken, with the countdown a second apart. Amendment 4's dry runs are unchanged. gate bypassed; no in-move check; no cut exemption; a plain check after power-up; a pull step that only prints; no case-start wait; no STOP on an interlock end; the case-start wait through the in-move check; a case-start refusal that ends the run. Each fails its check.
Prompt wording (Amendment 7) 28/28 as predicted on bca384b: no action word before any cue, shown or spoken; the full Amendment 6 set unchanged a countdown line that says "pull" is flagged
W1 wording the push gate passes on the net diff, every commit, the commit messages and this text its per-scan self-tests block planted lines

On record: R1's original flush fact missed on the Amendment 1 head (the 30 s flush was added on purpose; the fact was then re-registered). Two checker faults were rerun and disclosed: the R1 reader for the if/else-wrapped scripts, and an R3 reader that matched a quote-escaped dump instead of the lambda text. Amendment 5: a G2 run on a partial head (before the detector change) is disclosed, and the R5 reader first read the switch triggers one level too shallow (fixed, rerun). Amendment 6: the first dry run never returned, because the simulator's sleep stopped short of its end (fixed in the simulator, rerun, disclosed). A sabotage run then showed that a guard exception in the case-start wait could end the whole run. That was fixed as ruled (STOP first; the case-start wait without the in-move check; a missing guard there ends only that case), and the full set was rerun on one head.

Bench (2.3.0)

Registered in the predictions file; they run with Daniel present.

  • PC1–PC6: power cut mid-move at Fast and at Slow, a power cut after a stop, a saved end with its switch released at boot, Set position while unknown, and a cut right after an open from S5 starts (it counts only if S5 is still pressed at boot). Daniel pulls the 24 V adapter from the wall when pc_run.py counts down, and plugs it back in when it says (both shown on screen and spoken), with the open guard running; USB stays unplugged. Each test is judged on its registered outcome, with where the carriage actually stopped taken from the recovery move's logged counts.
  • L3 / L4: the changed expectations for a dead S4 / S5.

🤖 Generated with Claude Code

daniel-frenkel and others added 11 commits November 7, 2026 08:57
…ower cut (before any code)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… stop

The cover shows closed only while S5 is pressed and fully open only while S4 is
pressed. The position is written to flash at every stop, and a move-in-progress
marker before each move (cleared at its stop); nothing is written while moving. A
cut-off move, a saved end whose switch isn't pressed, or a close that ends without
S5 makes the position unknown: 50 %, a Fault line, the next Close runs until S5 and
the next Open until S4, and Set position closes to S5 first.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…r PC4

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ush while moving)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… stop paths)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ancel)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…dle-only flushes, restart halts first, no moves during an update

- A pressed limit switch decides the position at startup even with the marker set;
  a seek toward a switch already pressed settles at once.
- Every stop halts the motor before its position is saved.
- ESPHome's periodic flush is off; a 30 s flush runs only while idle, and move_begin
  and the Speed setting flush only while idle.
- on_shutdown halts the motor before capturing the position.
- An update sets ota_active: Open, Close, Set position and Auto-Calibrate refuse
  until it fails or is aborted (a finished one restarts).
- The S5 handler logs its trigger count; an unknown-position close logs its start.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
daniel-frenkel and others added 3 commits November 10, 2026 22:02
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
daniel-frenkel and others added 4 commits November 11, 2026 12:49
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…oved)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ch; detector log order

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
daniel-frenkel and others added 6 commits November 11, 2026 14:36
…rompts)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ck after a cut)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…t, PC6 detail, dry-run models)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…prompts (Amendment 6)

pc_run.py: no case, cut or move unless the open guard is connected and watching;
STOP and end the case if it drops mid-move outside a cut; from the call until the unit
and the guard are back its lost connection is expected; the guard counts as back once it
has read not connected since the call. Pull, plug-in, countdown and alerts go through
bench_common.Prompter. pc_sim.py: open-guard models (--sim-guard), the bench hears the
prompts, sleep ends exactly on time.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ted result (I6-3a)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…art wait, I6-5 refined, I6-6)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
daniel-frenkel and others added 5 commits November 11, 2026 15:25
… guard at case start ends only that case (ruling 47)

The case-start wait sends nothing and sleeps without the in-move check; a guard exception
there is recorded as "guard missing" and the next case waits again. pc_sim.py: drop-at-cut
guard model and the no-return fault for the dry-run checks.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… predicted)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…e the cue)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant