Files
brewpi/components/plant/pot.py
T
jensandClaude Sonnet 4.6 296d3a333d 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
2026-06-22 18:15:08 +02:00

95 lines
2.5 KiB
Python

from components.aplant import APlant
from components.plant import delay
class Pot(APlant):
def __init__(self, dt):
APlant.__init__(self)
self.dt = dt
self.e = 0
self.x = 0
self.p_pot = 0
# Plant specific thermal capacity [W*s/(kg*K)], mass [kg], energy
# 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 temperature [°C] - seeded from theta_amb the first time
# set_ambient_temperature() is called (see there), or overridden
# explicitly via initial(). None until either happens;
# process() checks for that too.
self.temp = None
# Ambient temperature [°C] - None until set_ambient_temperature()
# is called; process() checks for that as well.
self.theta_amb = None
# Set power [W]
self.p_in = 0
def set_plant_params(self, params):
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):
self.temp = temp
def activate(self, enable):
pass
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)
p_loss = self.L * self.M * (self.temp - self.theta_amb)
self.p_pot = self.delay.get() - p_loss
self.temp = min(100, self.temp + self.p_pot/(self.M * self.C) * self.dt)
def is_activated(self):
return True
def set_thermal_params(self, M, C):
self.M = M
self.C = C
def set_power(self, power):
self.p_in = power
def get_power(self):
return round(self.p_in, 1)
def get_temperature(self):
return self.temp
def get_p_pot(self):
return self.p_pot