Files
brewpi/components/pid/temp_controller_smith.py
jensandClaude Sonnet 4.6 b3923fb1c0 fix: smooth HOLD→HEAT power transition (bumpless transfer + cascade timing)
Two root causes for the visible power dip at every ramp-to-ramp
step boundary:

1. One-tick cascade delay: heatrate_soll was computed from
   pid_hold.get_y() before pid_hold.process() ran in that tick,
   so the first tick of a new ramp used the stale hold-phase
   output (≈0) as the rate setpoint, driving pid_heat to near
   zero.  Fix: move heatrate_soll computation inside process_pid(),
   after pid_hold.process().

2. Cold-start reset: pid_heat.reset() at HOLD→HEAT discarded
   the hold-phase integral, so pid_heat had to ramp up from
   zero on every new step.  Fix: remove the reset (bumpless
   transfer) — the hold-phase integral is a warm starting point
   that carries the baseline hold power into the new ramp.

Together these eliminate the ~1s dip to near-zero and replace
the slow 20–30s integral ramp-up with an immediate start at
roughly hold-level power, climbing monotonically to the target.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-30 20:33:12 +02:00

71 lines
2.6 KiB
Python
Executable File

from components.plant.pot import Pot
from components.pid.temp_controller_base import TempControllerBase, States
class TempController(TempControllerBase):
def __init__(self, dt):
TempControllerBase.__init__(self, dt)
# last_theta_ist / theta_ist_filtered / beta / dt all live in the base -
# see TempControllerBase.__init__ and _compute_heatrate().
# Fast model: same plant model but with zero transport delay, used
# to predict the current temperature without the dead time.
self.model = Pot(dt)
# Delayed model: keeps the plant's assumed transport delay, so it
# can be compared like-for-like against the real (delayed) measurement.
self.model_delay = Pot(dt)
self.theta_ist_plant = 0
self.theta_ist_model = 0
self.theta_ist_model_delay = 0
def set_model_plant_params(self, model_params):
self.model.set_plant_params({**model_params, 'Td': 0})
self.model_delay.set_plant_params(model_params)
def set_model_power(self, power):
self.model.set_power(power)
self.model_delay.set_power(power)
def set_ambient_temperature(self, theta_amb):
self.model.set_ambient_temperature(theta_amb)
self.model_delay.set_ambient_temperature(theta_amb)
def is_configured(self):
return super().is_configured() and self.model.is_configured() and self.model_delay.is_configured()
def on_state_entered(self, state):
if state in (States.HEAT, States.COOL):
self.model.initial(self.theta_ist)
self.model_delay.initial(self.theta_ist)
def post_pid(self):
self.model.process()
self.model_delay.process()
def process(self):
self._require_params()
if not (self.model.is_configured() and self.model_delay.is_configured()):
raise RuntimeError(
"{}.process(): model plant params and/or ambient temperature not set - "
"call set_model_plant_params() and set_ambient_temperature() first".format(type(self).__name__))
self.theta_ist_plant = self.theta_ist_set
self.theta_ist_model = self.model.get_temperature()
self.theta_ist_model_delay = self.model_delay.get_temperature()
# Smith predictor: use the fast model's prediction, corrected by the
# mismatch between the real (delayed) plant and the delayed model,
# so the dead time drops out of the feedback path.
self.theta_ist = self.theta_ist_model + (self.theta_ist_plant - self.theta_ist_model_delay)
self._compute_heatrate(self.theta_ist)
# Compensate for max heat rate to reduce overshoot
hold_scale = 1.0/self.heatrate_soll_set if self.heatrate_soll_set > 0 else 1.0
theta_err = self.theta_soll_set - self.theta_ist
diff = self.theta_soll_set - self.theta_ist
self.process_fsm(diff)
self.process_pid(theta_err, hold_scale)