Files
jensandClaude Sonnet 5 f69d63c31b 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
2026-07-05 21:23:55 +02:00

55 lines
1.1 KiB
Python

class Pid:
def __init__(self, dt):
self.dt = dt
self.params = None
# Integrator
self.yi = 0
# Differentiator
self.d = 0
# Anti windup
self.y_min = -1.0
self.y_max = 1.0
self.awu = 0
# Output
self.y = 0
def set_params(self, params):
self.params = params
def reset(self):
self.yi = 0
self.d = 0
self.awu = 0
def process(self, err, d, scale=1.0):
dt = self.dt
kp = self.params['kp'] * scale
ki = self.params['ki'] * scale
kd = self.params['kd'] * scale
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
self.d = d
y = yp + self.yi + yd
self.y = max(self.y_min, min(self.y_max, y))
# Anti windup
self.awu = self.y - y
def get_y(self):
return self.y