Repository navigation
fix(control): unwind PI integrator on mode change and on wrong-direction windup - #297
Merged
Merged
Conversation
…ion windup
Live regression spanning 2026-05-24 evening → 2026-05-25 morning:
- Yesterday ~18:00 the operator switched mode from planner_self →
self_consumption to recover from a stuck-import situation.
handleSetMode mutated state.Mode but left state.PI.integral
untouched. The new reactive loop inherited zero state for the
first cycle, then immediately encountered a sustained import
error and ran the integral to negative saturation (Ki=0.1,
iLim=3000).
- The dispatch already detected the resulting wrong-direction
output via the meter-clamp anti-windup branch and clamped the
OUTPUT to currentTotal — but it left the SATURATED INTEGRAL in
place. The integral kept dragging the PI's output negative
across subsequent cycles. Without an active drain, recovery
waits for opposite-signed live error to integrate it back
naturally; with the morning sunrise that took ~3 minutes of
"PI commands discharge while grid is exporting" before the
integral unwound enough for the output sign to flip.
- Overnight, while the loop oscillated between "PI says
discharge, clamp blocks it" and "PI says discharge, slew
moves the battery", the fleet drained from ~60 % SoC to 7 %.
Net 3.2 kWh imported at 250+ öre/kWh.
Two narrow fixes:
1. handleSetMode calls PI.Reset() under CtrlMu. Mode change is
a discrete control-regime change; the prior integral has no
meaning under the new regime.
2. The wrong-direction anti-windup branch in dispatch now calls
state.PI.DecayIntegral(0.5) when it fires. Geometric 0.5 per
cycle drains a saturated integral within a handful of
dispatch ticks instead of minutes, without snapping to zero
(which would drop legitimate steady-state offsets).
New API on PIController: DecayIntegral(factor) and Integral()
read-only accessor. Factor is clamped to [0,1] defensively.
Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
4 of 6 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Live regression — root cause
Spanning 2026-05-24 evening → 2026-05-25 morning. Fleet drained from ~60 % SoC to 7 %, importing 3.2 kWh at 250+ öre/kWh overnight.
What happened
The meter-clamp anti-windup logic already detected the wrong-direction case (`errW > 0 && correctionDir > epsilon` etc.) and clamped the OUTPUT to `currentTotal` — but the integral was untouched, so the controller stayed stuck across every subsequent cycle.
Two narrow fixes
`handleSetMode` calls `PI.Reset()` under `CtrlMu`. Mode change is a discrete control-regime change; the prior integral has no meaning under the new regime.
The wrong-direction branch in `dispatch.go` now calls `state.PI.DecayIntegral(0.5)`. Geometric drain unwinds a saturated integral within a handful of dispatch ticks instead of minutes, without snapping to zero (which would drop legitimate steady-state offsets).
New API on PIController
Tests
Full suite green: `go test ./...`
🤖 Generated with Claude Code