Files
brewpi/components/pid/temp_controller_smith.py
T
jensandClaude Sonnet 4.6 95eacabe28 Rewrite Smith predictor controller and demo for the new Pot/no-Kalman design
temp_controller_smith.py still relied on Kalman filtering and Pot's old
get_temperature_intermediate(), both removed by the recent Pot/Kalman
simplification. Rebuild it using two Pot model copies (zero-delay and
delayed) and the same backward-difference heat rate as temp_controller.py,
combined into the classic Smith correction:
theta_ist = theta_model_fast + (theta_plant - theta_model_delay).

Also fix set_model_power() never being called, so the internal model
actually receives the controller's power output, and fill in
demo_temp_controller_smith.py to exercise it end-to-end with an extra
plot of plant vs. fast-model vs. delayed-model temperature.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-19 14:42:10 +02:00

62 lines
2.1 KiB
Python
Executable File

from components.plant.pot import Pot
from components.pid.temp_controller_base import TempControllerBase
from components.pid.tc_constants import States
class TempController(TempControllerBase):
def __init__(self, dt, params, model_params):
TempControllerBase.__init__(self, dt, params)
self.dt = dt
self.last_theta_ist = 20
self.heatrate_ist = 0
# Fast model: same plant model but with zero transport delay, used
# to predict the current temperature without the dead time.
self.model = Pot(dt, {**model_params, 'Td': 0})
# 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, model_params)
self.theta_ist_plant = 0
self.theta_ist_model = 0
self.theta_ist_model_delay = 0
def set_model_power(self, power):
self.model.set_power(power)
self.model_delay.set_power(power)
def on_state_entered(self, state):
if state == States.HEAT:
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.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)
heatrate = (self.theta_ist - self.last_theta_ist)/self.dt*60
self.heatrate_ist = heatrate
self.last_theta_ist = self.theta_ist
# Compensate for max heat rate to reduce overshoot
if self.heatrate_soll_set > 0:
self.pid_hold.scale(1.0/self.heatrate_soll_set)
self.heatrate_soll = self.heatrate_soll_set * self.pid_hold.get_y()
theta_err = self.theta_soll_set - self.theta_ist
heatrate_err = self.heatrate_soll - self.heatrate_ist
diff = self.theta_soll_set - self.theta_ist
self.process_fsm(diff)
self.process_pid(theta_err, heatrate_err)