Skip to content

fix(control): unwind PI integrator on mode change and on wrong-direction windup - #297

Merged
frahlg merged 1 commit into
masterfrom
fix/pi-integral-windup-on-mode-change
May 25, 2026
Merged

frahlg merged 1 commit into
masterfrom
fix/pi-integral-windup-on-mode-change

Conversation

@frahlg

@frahlg frahlg commented May 25, 2026

Copy link
Copy Markdown
Member

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

yesterday 18:00     operator: planner_self → self_consumption
                    handleSetMode: state.Mode = m; PI.Reset() NOT called
                    PI integral state carries from prior loop's accumulation

yesterday ~18:30    reactive PI on grid_w = +697 (importing)
                    integral runs to negative saturation (-3000) within ~30 cycles

overnight           load drains battery, grid swings between import/export
                    PI integral keeps fighting per-cycle err, oscillates around saturation

today 04:51 UTC     sunrise, grid_w = -1000 (exporting), should command CHARGE
                    PI: output = Kp×err + integral_pinned_negative ≈ -1080  (DISCHARGE)
                    meter-clamp detects wrong-direction → clamps output to 0
                    BUT integral is left at -3000, next cycle is just as wound up

today 04:53 UTC     ~3 minutes of natural Ki·err drain later, integral finally crosses
                    zero, PI flips to commanding charge. SoC at 7 %.

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

  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 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

  • `DecayIntegral(factor)` — multiplies integral by factor ∈ [0, 1]. Clamped defensively.
  • `Integral()` — read-only accessor.

Tests

  • `TestPIDecayIntegralUnwindsSaturation` — geometric 0.5 decay from -3000.
  • `TestPIDecayIntegralClampsFactor` — factor clamped to [0, 1].
  • `TestDispatchWrongDirectionWindupDecaysPIIntegral` — the live regression: pre-wind integral, fire one dispatch cycle, assert integral has unwound; behavioural: after 6 cycles PI must command charge (positive) against -1000 W live export, not stay stuck discharging.
  • `TestHandleSetModeResetsPIIntegral` — POST /api/mode resets a saturated integral.

Full suite green: `go test ./...`

🤖 Generated with Claude Code

…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]>
@frahlg
frahlg merged commit 9288d52 into master May 25, 2026
1 check passed
@frahlg
frahlg deleted the fix/pi-integral-windup-on-mode-change branch May 25, 2026 05:05
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