Replace Pid.scale()'s mutable gain factor with a stateless process() arg

Pid.scale(k) set self.k as persistent state, mutated from outside the
class and never reset in reset(), so a stale scale factor could survive
a state-transition reset. Replace it with a plain scale=1.0 argument on
Pid.process(), and have temp_controller.py/temp_controller_smith.py
compute the heat-rate-overshoot compensation factor themselves and pass
it through process_pid() each tick instead of mutating pid_hold's state.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-06-19 17:37:55 +02:00
co-authored by Claude Sonnet 4.6
parent 519bbd485f
commit 1e89a58370
5 changed files with 20 additions and 26 deletions
+9 -8
View File
@@ -22,14 +22,15 @@ git history for `temp_controller.py`/`temp_controller_smith.py`).
`temp_controller_smith.py` was rewritten to drop Kalman filtering `temp_controller_smith.py` was rewritten to drop Kalman filtering
entirely (see below), so there's no shared tuning to split anymore. entirely (see below), so there's no shared tuning to split anymore.
- [ ] **`Pid.scale()` is a gain-scheduling hack, not anti-windup.** - [x] **`Pid.scale()` is a gain-scheduling hack, not anti-windup.**
`pid.py`'s `scale(k)` multiplies every gain by `1/heatrate_soll_set` Fixed: removed `Pid.scale()`/`self.k` entirely. `Pid.process(err, d,
(called from `temp_controller*.py`'s `process()`), but it's framed scale=1.0)` now takes the gain multiplier as a plain per-call
as overshoot compensation, conflated with the real anti-windup logic argument instead of mutable state, so there's nothing to go stale
a few lines below. `self.k` also never resets in `reset()`, so a across a `reset()`. `temp_controller*.py` compute `hold_scale =
stale scale factor can survive a state-transition reset. Consider 1.0/heatrate_soll_set if heatrate_soll_set > 0 else 1.0` themselves
moving this scheduling logic out of the generic `Pid` class and into and pass it through `process_pid(theta_err, heatrate_err,
the temp controller, and resetting `k` in `reset()`. Still present. hold_scale)` — the overshoot-compensation scheduling logic now lives
entirely in the temp controller, not the generic `Pid` block.
- [ ] **No automated tests.** `tests/components/pid/` was added once - [ ] **No automated tests.** `tests/components/pid/` was added once
(stdlib `unittest`, no pytest) covering FSM transitions, anti-windup (stdlib `unittest`, no pytest) covering FSM transitions, anti-windup
+5 -10
View File
@@ -5,8 +5,6 @@ class Pid:
self.dt = dt self.dt = dt
self.params = None self.params = None
self.k = 1.0
# Integrator # Integrator
self.yi = 0 self.yi = 0
@@ -21,9 +19,6 @@ class Pid:
# Output # Output
self.y = 0 self.y = 0
def scale(self, k):
self.k = k
def set_params(self, params): def set_params(self, params):
self.params = params self.params = params
@@ -32,12 +27,12 @@ class Pid:
self.d = 0 self.d = 0
self.awu = 0 self.awu = 0
def process(self, err, d): def process(self, err, d, scale=1.0):
dt = self.dt dt = self.dt
kp = self.params['kp'] * self.k kp = self.params['kp'] * scale
ki = self.params['ki'] * self.k ki = self.params['ki'] * scale
kd = self.params['kd'] * self.k kd = self.params['kd'] * scale
kt = self.params['kt'] * self.k kt = self.params['kt'] * scale
self.yi = self.yi + ki*dt * err + kt*dt * self.awu self.yi = self.yi + ki*dt * err + kt*dt * self.awu
yd = kd/dt*(d - self.d) yd = kd/dt*(d - self.d)
+2 -3
View File
@@ -18,8 +18,7 @@ class TempController(TempControllerBase):
self.last_theta_ist = self.theta_ist self.last_theta_ist = self.theta_ist
# Compensate for max heat rate to reduce overshoot # Compensate for max heat rate to reduce overshoot
if self.heatrate_soll_set > 0: hold_scale = 1.0/self.heatrate_soll_set if self.heatrate_soll_set > 0 else 1.0
self.pid_hold.scale(1.0/self.heatrate_soll_set)
self.heatrate_soll = self.heatrate_soll_set * self.pid_hold.get_y() self.heatrate_soll = self.heatrate_soll_set * self.pid_hold.get_y()
theta_err = self.theta_soll_set - self.theta_ist theta_err = self.theta_soll_set - self.theta_ist
@@ -27,4 +26,4 @@ class TempController(TempControllerBase):
diff = self.theta_soll_set - self.theta_ist diff = self.theta_soll_set - self.theta_ist
self.process_fsm(diff) self.process_fsm(diff)
self.process_pid(theta_err, heatrate_err) self.process_pid(theta_err, heatrate_err, hold_scale)
+2 -2
View File
@@ -106,8 +106,8 @@ class TempControllerBase(APid):
print("New state = {}".format(state_next)) print("New state = {}".format(state_next))
self.on_state_entered(state_next) self.on_state_entered(state_next)
def process_pid(self, theta_err, heatrate_err): def process_pid(self, theta_err, heatrate_err, hold_scale=1.0):
self.pid_hold.process(theta_err, -self.theta_ist) self.pid_hold.process(theta_err, -self.theta_ist, hold_scale)
self.pid_rate.process(heatrate_err, -self.heatrate_ist) self.pid_rate.process(heatrate_err, -self.heatrate_ist)
if self.state == States.IDLE: if self.state == States.IDLE:
+2 -3
View File
@@ -50,8 +50,7 @@ class TempController(TempControllerBase):
self.last_theta_ist = self.theta_ist self.last_theta_ist = self.theta_ist
# Compensate for max heat rate to reduce overshoot # Compensate for max heat rate to reduce overshoot
if self.heatrate_soll_set > 0: hold_scale = 1.0/self.heatrate_soll_set if self.heatrate_soll_set > 0 else 1.0
self.pid_hold.scale(1.0/self.heatrate_soll_set)
self.heatrate_soll = self.heatrate_soll_set * self.pid_hold.get_y() self.heatrate_soll = self.heatrate_soll_set * self.pid_hold.get_y()
theta_err = self.theta_soll_set - self.theta_ist theta_err = self.theta_soll_set - self.theta_ist
@@ -59,4 +58,4 @@ class TempController(TempControllerBase):
diff = self.theta_soll_set - self.theta_ist diff = self.theta_soll_set - self.theta_ist
self.process_fsm(diff) self.process_fsm(diff)
self.process_pid(theta_err, heatrate_err) self.process_pid(theta_err, heatrate_err, hold_scale)