Skip to content

fix: harden control loop against write failures and initialisation edge cases#12

Merged
bvweerd merged 1 commit into
devfrom
fix/control-robustness
Jul 4, 2026
Merged

fix: harden control loop against write failures and initialisation edge cases#12
bvweerd merged 1 commit into
devfrom
fix/control-robustness

Conversation

@bvweerd

@bvweerd bvweerd commented Jul 4, 2026

Copy link
Copy Markdown
Owner

Problem / Fix

Four small robustness fixes from the non-happy-flow review:

  1. Switch-array write not contained. Switch-array toggles were the only actuator write not wrapped in try/except: a service exception (e.g. a timeout) escalated to UpdateFailed and aborted the entire control cycle. Failures are now logged and the toggle retried the next cycle; internal state is only updated after a successful write.
  2. Anti-windup direction check inconsistent. The back-calculation anti-windup compared the PID output direction against the raw grid signal (filtered), while the PID acts on the residual (grid minus the pending battery feedforward). While a battery absorbs an export the two can legitimately differ in sign, wrongly resetting a valid integrator. The check now uses the residual.
  3. Discharge into a running load after restart. _discharge_allowed treated loads the engine had not seen yet as off, so right after a restart the battery could discharge to cover an import caused by a load that was actually running and reducible. Unknown loads are now read from their entity state before the last-resort decision.
  4. Silent SoC fail-open. A configured-but-unavailable SoC sensor silently disabled the SoC limits. Fail-open remains the behaviour (the BMS protects the battery), but a warning is now logged once until the sensor recovers — consistent with the schedule-sensor handling.

Tests

tests/test_robustness.py: switch-array write failure keeps the cycle alive and retries, a running-but-uninitialised load blocks discharge, and the SoC-unavailable warning fires exactly once. Full suite: 242 passed.

https://claude.ai/code/session_01RUWpwxbGsgR3PoLHLq4Djz


Generated by Claude Code

…ge cases

Four small robustness fixes found in a non-happy-flow review:

- Switch-array toggles were the only actuator write not wrapped in
  try/except: a service exception (e.g. a timeout) escalated to
  UpdateFailed and aborted the whole control cycle. Failures are now
  logged and the toggle retried next cycle.
- The back-calculation anti-windup compared the PID output direction
  against the raw grid signal, while the PID acts on the residual
  (grid minus the pending battery response). While a battery absorbs
  an export the two can legitimately differ in sign, wrongly resetting
  a valid integrator. The check now uses the residual.
- _discharge_allowed treated loads the engine had not seen yet as off,
  so right after a restart the battery could discharge into a load
  that was actually running and reducible. Unknown loads are now read
  from their entity state.
- A configured-but-unavailable SoC sensor silently disabled the SoC
  limits (fail-open). That is still the behaviour — the BMS protects
  the battery — but a warning is now logged once until the sensor
  recovers.

https://claude.ai/code/session_01RUWpwxbGsgR3PoLHLq4Djz
@bvweerd bvweerd force-pushed the fix/control-robustness branch from 40e3234 to 546d559 Compare July 4, 2026 09:50
@github-actions github-actions Bot added enhancement New feature or request bug Something isn't working labels Jul 4, 2026
@bvweerd bvweerd merged commit 468fb90 into dev Jul 4, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant