Pot: require plant params and ambient temperature via setters, not the constructor

Pot.__init__ now only takes dt; set_plant_params() and set_ambient_temperature()
must be called before process(), which now raises RuntimeError if either is
missing instead of silently computing on whatever the constructor happened to
default to. set_ambient_temperature() only seeds the plant's starting
temperature the first time it's called, so changing ambient mid-run still
can't clobber an in-progress simulated temperature. Updates all callers
(server/brewpi.py, TempController(Smith)'s two internal models,
SudForecastEstimator, and the plant/pid/sud demo scripts) to call the setters
right after construction.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DkkuG48uHFCGKe6dPSERFk
This commit is contained in:
2026-06-22 18:15:08 +02:00
co-authored by Claude Sonnet 4.6
parent e0bd3acc26
commit 296d3a333d
8 changed files with 67 additions and 31 deletions
+6 -2
View File
@@ -11,10 +11,14 @@ class TempController(TempControllerBase):
# Fast model: same plant model but with zero transport delay, used # Fast model: same plant model but with zero transport delay, used
# to predict the current temperature without the dead time. # to predict the current temperature without the dead time.
self.model = Pot(dt, {**model_params, 'Td': 0}, theta_amb) self.model = Pot(dt)
self.model.set_plant_params({**model_params, 'Td': 0})
self.model.set_ambient_temperature(theta_amb)
# Delayed model: keeps the plant's assumed transport delay, so it # Delayed model: keeps the plant's assumed transport delay, so it
# can be compared like-for-like against the real (delayed) measurement. # can be compared like-for-like against the real (delayed) measurement.
self.model_delay = Pot(dt, model_params, theta_amb) self.model_delay = Pot(dt)
self.model_delay.set_plant_params(model_params)
self.model_delay.set_ambient_temperature(theta_amb)
self.theta_ist_plant = 0 self.theta_ist_plant = 0
self.theta_ist_model = 0 self.theta_ist_model = 0
+43 -23
View File
@@ -2,7 +2,7 @@ from components.aplant import APlant
from components.plant import delay from components.plant import delay
class Pot(APlant): class Pot(APlant):
def __init__(self, dt, params, theta_amb=20): def __init__(self, dt):
APlant.__init__(self) APlant.__init__(self)
self.dt = dt self.dt = dt
@@ -10,31 +10,49 @@ class Pot(APlant):
self.x = 0 self.x = 0
self.p_pot = 0 self.p_pot = 0
# Plant specific thermal capacity [W*s/(kg*K)] # Plant specific thermal capacity [W*s/(kg*K)], mass [kg], energy
self.C = params['C'] # loss coefficient [W/(kg*K)] and transport propagation delay [s]
# - all None until set_plant_params() is called; process() checks
# for that rather than silently computing on bogus values.
self.C = None
self.M = None
self.L = None
self.Td = None
self.delay = None
# Plant mass [kg] # Plant temperature [°C] - seeded from theta_amb the first time
self.M = params['M'] # set_ambient_temperature() is called (see there), or overridden
# explicitly via initial(). None until either happens;
# process() checks for that too.
self.temp = None
# Plant energy loss coefficient [W/(kg*K)] # Ambient temperature [°C] - None until set_ambient_temperature()
# Negative input power as a function of plant mass and temperature difference T_plant and T_ambient # is called; process() checks for that as well.
# P_loss = L * Mass * (T_plant - T_ambient) self.theta_amb = None
self.L = params['L']
# Energy transport propagation delay
self.Td = params['Td']
# Plant temperature [°C]
self.temp = theta_amb
# Ambient temperature [°C]
self.theta_amb = theta_amb
# Set power [W] # Set power [W]
self.p_in = 0 self.p_in = 0
# Plant delay [s] def set_plant_params(self, params):
self.delay = delay.Delay(dt, self.Td, 0) self.C = params['C']
self.M = params['M']
# Negative input power as a function of plant mass and
# temperature difference T_plant and T_ambient:
# P_loss = L * Mass * (T_plant - T_ambient)
self.L = params['L']
self.Td = params['Td']
self.delay = delay.Delay(self.dt, self.Td, 0)
def set_ambient_temperature(self, theta_amb):
self.theta_amb = theta_amb
# Only seeds the plant's own starting temperature the first time
# this is called (i.e. there's no real temperature yet) - once a
# run is in progress, changing the ambient setting must not
# clobber whatever temperature has actually been simulated since.
if self.temp is None:
self.temp = theta_amb
def initial(self, temp): def initial(self, temp):
self.temp = temp self.temp = temp
@@ -43,6 +61,11 @@ class Pot(APlant):
pass pass
def process(self): def process(self):
if self.delay is None:
raise RuntimeError("Pot.process(): plant params not set - call set_plant_params() first")
if self.theta_amb is None:
raise RuntimeError("Pot.process(): ambient temperature not set - call set_ambient_temperature() first")
self.delay.put(self.p_in) self.delay.put(self.p_in)
p_loss = self.L * self.M * (self.temp - self.theta_amb) p_loss = self.L * self.M * (self.temp - self.theta_amb)
@@ -53,9 +76,6 @@ class Pot(APlant):
def is_activated(self): def is_activated(self):
return True return True
def set_ambient_temperature(self, theta_amb):
self.theta_amb = theta_amb
def set_thermal_params(self, M, C): def set_thermal_params(self, M, C):
self.M = M self.M = M
self.C = C self.C = C
+3 -1
View File
@@ -61,7 +61,9 @@ class SudForecastEstimator:
if not sud.load(doc) or not sud.schedule: if not sud.load(doc) or not sud.schedule:
return [0.0], [start_theta], SudState.DONE, [] return [0.0], [start_theta], SudState.DONE, []
pot = Pot(self.dt, self.plant_params, self.theta_amb) pot = Pot(self.dt)
pot.set_plant_params(self.plant_params)
pot.set_ambient_temperature(self.theta_amb)
pot.initial(start_theta) pot.initial(start_theta)
tc = PidFactory.create(self.pid_type, self.dt, self.tempctrl_params, self.plant_params, theta_amb=self.theta_amb) tc = PidFactory.create(self.pid_type, self.dt, self.tempctrl_params, self.plant_params, theta_amb=self.theta_amb)
tc.set_enabled(True) tc.set_enabled(True)
+3 -1
View File
@@ -45,7 +45,9 @@ if __name__ == '__main__':
heatrate_soll = 1.25 heatrate_soll = 1.25
ctrl = TempController(dt, ctrl_params) ctrl = TempController(dt, ctrl_params)
ctrl.set_enabled(True) ctrl.set_enabled(True)
plant = Pot(dt, plant_params, theta_amb) plant = Pot(dt)
plant.set_plant_params(plant_params)
plant.set_ambient_temperature(theta_amb)
_y = np.empty(0) _y = np.empty(0)
_fb = np.empty(0) _fb = np.empty(0)
_t = np.empty(0) _t = np.empty(0)
@@ -45,7 +45,9 @@ if __name__ == '__main__':
heatrate_soll = 1.25 heatrate_soll = 1.25
ctrl = TempController(dt, ctrl_params, plant_params, theta_amb) ctrl = TempController(dt, ctrl_params, plant_params, theta_amb)
ctrl.set_enabled(True) ctrl.set_enabled(True)
plant = Pot(dt, plant_params, theta_amb) plant = Pot(dt)
plant.set_plant_params(plant_params)
plant.set_ambient_temperature(theta_amb)
_y = np.empty(0) _y = np.empty(0)
_fb = np.empty(0) _fb = np.empty(0)
_t = np.empty(0) _t = np.empty(0)
+3 -1
View File
@@ -19,7 +19,9 @@ if __name__ == '__main__':
"gain" : 1.0 "gain" : 1.0
} }
pot = Pot(dt, pot_params, theta_amb) pot = Pot(dt)
pot.set_plant_params(pot_params)
pot.set_ambient_temperature(theta_amb)
_t = np.empty(0) _t = np.empty(0)
_temp = np.empty(0) _temp = np.empty(0)
+3 -1
View File
@@ -49,7 +49,9 @@ if __name__ == '__main__':
} }
ctrl = TempController(dt, ctrl_params, plant_params, theta_amb) ctrl = TempController(dt, ctrl_params, plant_params, theta_amb)
plant = Pot(dt, plant_params, theta_amb) plant = Pot(dt)
plant.set_plant_params(plant_params)
plant.set_ambient_temperature(theta_amb)
stirrer = StirrerSim(dt) stirrer = StirrerSim(dt)
heater = HeaterSim({}) heater = HeaterSim({})
+3 -1
View File
@@ -76,7 +76,9 @@ if __name__ == '__main__':
taskmgr.add(sensor_task) taskmgr.add(sensor_task)
# Plant # Plant
pot = Pot(DT, DEFAULT_PLANT_PARAMS, theta_amb) pot = Pot(DT)
pot.set_plant_params(DEFAULT_PLANT_PARAMS)
pot.set_ambient_temperature(theta_amb)
taskmgr.add(PotTask(pot, DT_TASK, dispatcher.msgio_get("Pot"))) taskmgr.add(PotTask(pot, DT_TASK, dispatcher.msgio_get("Pot")))
# Heater # Heater