fix: split inner-loop yi_max clamp to fix HOLD-state windup overshoot

Renames pid_hold/pid_heat/pid_cool to pid_outer/pid_inner/pid_inner_cool
to match what actually runs when, and splits inner-loop config into
Inner.Heat/Inner.Hold/Inner.Cool so the same PID instance gets a tight
yi_max ceiling only while HOLD drives it, without capping legitimate
1.5 K/min ramps. Fixes the overshoot from docs/overshoot_hold_windup.md
where a cold-water disturbance during HOLD wound up pid_heat's integral
term with no anti-windup engagement, taking ~35s+ to unwind naturally.

Breaking config change: Hold/Heat/Cool -> Outer/Inner.{Heat,Hold,Cool}
in config.json, both .tpl templates, the pid/sud demo scripts, and
replay_sim.py's CLI flags. Adds tests/components/pid/ (stdlib unittest)
covering the Pid clamp/recovery behavior and closed-loop disturbance,
ramp, and HOLD<->HEAT transition cases.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DGQhVQ2Y3yXAQTXhrxVd5u
This commit is contained in:
2026-07-05 21:23:55 +02:00
co-authored by Claude Sonnet 5
parent da832b961e
commit f69d63c31b
17 changed files with 411 additions and 113 deletions
+7 -6
View File
@@ -63,7 +63,7 @@ git history for `temp_controller.py`/`temp_controller_smith.py`).
before that file was removed entirely — `Pot` dropped `gain`
entirely, see `components/plant/TODO.md`.
- [ ] **`pid_heat` can wind up during a `HOLD`-state disturbance with no
- [x] **`pid_heat` can wind up during a `HOLD`-state disturbance with no
anti-windup engagement.** A cold-water disturbance while holding drove
`pid_heat`'s integral term up without ever saturating `y` (peaked at
`y≈0.74` of the `1.0` ceiling), so the existing back-calculation
@@ -75,16 +75,17 @@ git history for `temp_controller.py`/`temp_controller_smith.py`).
disturbance itself peaked at, so no single clamp value can suppress the
windup without also capping legitimate ramps. An FSM-gating alternative
(freeze the loop's output in `HOLD` unless engaged) was also superseded.
Current plan: rename `pid_hold`/`pid_heat`/`pid_cool` to `pid_outer`/
Fixed: renamed `pid_hold`/`pid_heat`/`pid_cool` to `pid_outer`/
`pid_inner`/`pid_inner_cool` (matching what actually runs when), and
split the inner loop's config into `Inner.Heat`/`Inner.Hold`/
`Inner.Cool` so the *same* PID instance gets a tight `yi_max` only while
`HOLD` is driving it and stays unclamped for real `HEAT` ramps — no
freeze/thaw, bumpless transfer preserved for free. See
`docs/overshoot_hold_windup.md` for the full writeup and test plan. This
is also a breaking config change (`Hold`/`Heat`/`Cool``Outer`/
`Inner.*`) — every deployed `config.json` needs migrating, not just the
repo templates.
`docs/overshoot_hold_windup.md` for the full writeup. This was also a
breaking config change (`Hold`/`Heat`/`Cool``Outer`/`Inner.*`) —
`config.json`, the templates, and the demo scripts were all migrated.
Test coverage for this (closed-loop disturbance/ramp/transition cases)
is still outstanding — see the "No automated tests" item above.
- [ ] **`kalman.py` is now dead code in production.** Neither
`temp_controller.py` nor `temp_controller_smith.py` uses `Kalman`
+3
View File
@@ -35,6 +35,9 @@ class Pid:
kt = self.params['kt'] * scale
self.yi = self.yi + ki*dt * err + kt*dt * self.awu
yi_max = self.params.get('yi_max')
if yi_max is not None:
self.yi = max(-yi_max, min(yi_max, self.yi))
yd = kd/dt*(d - self.d)
yp = kp * err
+20 -15
View File
@@ -19,6 +19,8 @@ class TempControllerBase(TempControllerFsm, APid):
# params/set_params() (components/pid/pid.py), whose process()
# already relies on the same "None means not configured yet".
self.params = None
self._inner_heat_params = None
self._inner_hold_params = None
self.y = -1
# Heat-rate pre-filter state (option A) - None until first process() tick
self.last_theta_ist = None
@@ -29,9 +31,10 @@ class TempControllerBase(TempControllerFsm, APid):
self.params = params
self.thresholds = {**DEFAULT_THRESHOLDS, **params.get('Thresholds', {})}
self.beta = params.get('beta', 0.05)
self.pid_hold.set_params(params['Hold'])
self.pid_heat.set_params(params['Heat'])
self.pid_cool.set_params(params['Cool'])
self.pid_outer.set_params(params['Outer'])
self._inner_heat_params = params['Inner']['Heat']
self._inner_hold_params = params['Inner']['Hold']
self.pid_inner_cool.set_params(params['Inner']['Cool'])
def _compute_heatrate(self, theta_ist):
"""Pre-filter theta_ist, then differentiate and low-pass to get heatrate_ist.
@@ -131,31 +134,33 @@ class TempControllerBase(TempControllerFsm, APid):
return self.heatrate_soll_set
def process_pid(self, theta_err, hold_scale=1.0):
self.pid_hold.process(theta_err, -self.theta_ist, hold_scale)
# In HOLD state, clamp pid_hold's output to [0, 1]: a small temperature
# overshoot makes pid_hold.y go negative, which would invert heatrate_soll
# and drive pid_heat's power to 0, causing a limit cycle (power on →
self.pid_outer.process(theta_err, -self.theta_ist, hold_scale)
# In HOLD state, clamp pid_outer's output to [0, 1]: a small temperature
# overshoot makes pid_outer.y go negative, which would invert heatrate_soll
# and drive pid_inner's power to 0, causing a limit cycle (power on →
# overshoot → power off → coast down → repeat). Clamping to 0 lets the
# outer loop reduce the inner setpoint to zero but no further.
pid_hold_y = self.pid_hold.get_y()
pid_outer_y = self.pid_outer.get_y()
if self.state == States.HOLD:
pid_hold_y = max(0.0, pid_hold_y)
self.heatrate_soll = self.heatrate_soll_set * pid_hold_y
pid_outer_y = max(0.0, pid_outer_y)
self.heatrate_soll = self.heatrate_soll_set * pid_outer_y
heatrate_err = self.heatrate_soll - self.heatrate_ist
# Only the PID actually driving y is advanced - otherwise the
# inactive one (e.g. pid_heat while COOL has pid_cool driving)
# inactive one (e.g. pid_inner while COOL has pid_inner_cool driving)
# would keep silently integrating against a heatrate_err that
# isn't actually under its control, building a stale windup that
# causes a discontinuity in y the moment it takes back over.
if self.state == States.IDLE:
self.y = 0
elif self.state == States.COOL:
self.pid_cool.process(heatrate_err, -self.heatrate_ist)
self.y = self.pid_cool.get_y()
self.pid_inner_cool.process(heatrate_err, -self.heatrate_ist)
self.y = self.pid_inner_cool.get_y()
else:
self.pid_heat.process(heatrate_err, -self.heatrate_ist)
self.y = self.pid_heat.get_y()
inner_params = self._inner_heat_params if self.state == States.HEAT else self._inner_hold_params
self.pid_inner.set_params(inner_params)
self.pid_inner.process(heatrate_err, -self.heatrate_ist)
self.y = self.pid_inner.get_y()
self.post_pid()
+20 -20
View File
@@ -8,9 +8,9 @@ DEFAULT_THRESHOLDS = {
# when COOL meant nothing more than going idle - it's an active state
# with its own PID now, so a threshold this tight relative to a real
# heater's discrete power steps/sensor noise causes the system to
# chatter in and out of it every tick, resetting both pid_hold's and
# pid_cool's integrators each time and never letting either actually
# converge. Symmetric with the others instead.
# chatter in and out of it every tick, resetting both pid_outer's and
# pid_inner_cool's integrators each time and never letting either
# actually converge. Symmetric with the others instead.
"HoldCool": 1.0,
"HeatHold": 1.0,
"HeatCool": 1.0,
@@ -30,14 +30,14 @@ class States(enum.Enum):
class TempControllerFsm:
def __init__(self, dt):
self.pid_hold = Pid(dt)
self.pid_heat = Pid(dt)
self.pid_outer = Pid(dt)
self.pid_inner = Pid(dt)
# Separate gains for ramping down (negative diff) - the actuator
# (e.g. a heat-only Pot/heater) is responsible for clamping the
# resulting negative power to whatever it's actually capable of;
# the controller itself no longer assumes "can't cool" == "must go
# idle".
self.pid_cool = Pid(dt)
self.pid_inner_cool = Pid(dt)
self.thresholds = None
self.state = States.INIT
self.is_startup = True
@@ -80,13 +80,13 @@ class TempControllerFsm:
# which calls this method directly), so an unconditional HOLD
# here gets mistaken for "ramp already reached" and can finish
# a freshly (re-)started step instantly - see SudTask.
# on_process()'s is_holding() check. pid_heat/pid_cool were
# frozen (see process_pid()) and possibly stale for as long as
# we were disabled - start whichever one matters clean rather
# on_process()'s is_holding() check. pid_inner/pid_inner_cool
# were frozen (see process_pid()) and possibly stale for as long
# as we were disabled - start whichever one matters clean rather
# than resuming wherever it last left off.
self.pid_hold.reset()
self.pid_heat.reset()
self.pid_cool.reset()
self.pid_outer.reset()
self.pid_inner.reset()
self.pid_inner_cool.reset()
if diff >= self.thresholds['HoldHeat']:
state_next = States.HEAT
elif diff <= -self.thresholds['HoldCool']:
@@ -96,31 +96,31 @@ class TempControllerFsm:
elif self.state == States.HOLD:
if diff >= self.thresholds['HoldHeat']:
state_next = States.HEAT
# No pid_heat.reset() here — bumpless transfer: carry the
# No pid_inner.reset() here — bumpless transfer: carry the
# hold-phase integral into the new ramp so power doesn't
# drop to near-zero and crawl back up from scratch.
elif diff <= -self.thresholds['HoldCool']:
state_next = States.COOL
self.pid_cool.reset()
self.pid_inner_cool.reset()
elif self.state == States.HEAT:
if diff <= -self.thresholds['HeatCool']:
state_next = States.COOL
self.pid_cool.reset()
self.pid_inner_cool.reset()
elif diff <= self.thresholds['HeatHold']:
state_next = States.HOLD
self.pid_hold.reset()
self.pid_outer.reset()
elif self.state == States.COOL:
if diff >= self.thresholds['CoolHeat']:
state_next = States.HEAT
self.pid_heat.reset()
self.pid_inner.reset()
elif diff >= -self.thresholds['CoolHold']:
state_next = States.HOLD
self.pid_hold.reset()
# pid_heat was frozen during COOL (see process_pid()) -
self.pid_outer.reset()
# pid_inner was frozen during COOL (see process_pid()) -
# resume it clean rather than from whatever it last held
# before COOL took over, which by now may be a stale fit
# for a completely different part of the curve.
self.pid_heat.reset()
self.pid_inner.reset()
if state_next != self.state:
self.state = state_next