From 0a6d3e6462baceeaaf894e0d9199a6eebafb13e7 Mon Sep 17 00:00:00 2001 From: Jens Ahrensfeld Date: Mon, 22 Jun 2026 18:21:20 +0200 Subject: [PATCH] TempControllerBase: require PID gains via set_params(), not the constructor Mirrors components/pid/pid.py's own Pid.set_params() pattern: params/ thresholds start out None, and set_params() configures pid_hold/pid_heat/ pid_cool from them. process()/process_fsm() fail naturally (TypeError on None) if called before set_params() - same as Pid.process() already does - rather than a new bespoke check. Updates all callers (server/brewpi.py, SudForecastEstimator, and the pid/sud demo scripts) to call set_params() right after construction. Co-Authored-By: Claude Sonnet 4.6 Claude-Session: https://claude.ai/code/session_01DkkuG48uHFCGKe6dPSERFk --- components/pid/temp_controller.py | 4 ++-- components/pid/temp_controller_base.py | 19 +++++++++++++------ components/pid/temp_controller_smith.py | 4 ++-- components/sud_forecast.py | 3 ++- scripts/demos/pid/demo_temp_controller.py | 3 ++- .../demos/pid/demo_temp_controller_smith.py | 3 ++- scripts/demos/sud/demo_sud.py | 3 ++- server/brewpi.py | 3 ++- 8 files changed, 27 insertions(+), 15 deletions(-) diff --git a/components/pid/temp_controller.py b/components/pid/temp_controller.py index c7dba24..c9d3574 100644 --- a/components/pid/temp_controller.py +++ b/components/pid/temp_controller.py @@ -2,11 +2,11 @@ from components.pid.temp_controller_base import TempControllerBase class TempController(TempControllerBase): - def __init__(self, dt, params, model_params=None, theta_amb=20): + def __init__(self, dt, model_params=None, theta_amb=20): # model_params/theta_amb are accepted (but unused) for constructor # compatibility with TempControllerSmith - PidFactory.create() calls # either with the same arguments; "Normal" has no internal model. - TempControllerBase.__init__(self, dt, params) + TempControllerBase.__init__(self, dt) self.dt = dt self.last_theta_ist = 20 self.heatrate_ist = 0 diff --git a/components/pid/temp_controller_base.py b/components/pid/temp_controller_base.py index bedcbe7..be64529 100644 --- a/components/pid/temp_controller_base.py +++ b/components/pid/temp_controller_base.py @@ -27,7 +27,7 @@ class States(enum.Enum): class TempControllerBase(APid): - def __init__(self, dt, params): + def __init__(self, dt): APid.__init__(self) self.pid_hold = Pid(dt) self.pid_heat = Pid(dt) @@ -44,13 +44,13 @@ class TempControllerBase(APid): self.heatrate_soll = 1.0 self.theta_ist = 0 self.heatrate_ist = 0 - self.params = params - self.thresholds = {**DEFAULT_THRESHOLDS, **params.get('Thresholds', {})} + # None until set_params() is called - mirrors Pid's own + # params/set_params() (components/pid/pid.py), whose process() + # already relies on the same "None means not configured yet". + self.params = None + self.thresholds = None self.y = -1 self.state = States.INIT - self.pid_hold.set_params(params['Hold']) - self.pid_heat.set_params(params['Heat']) - self.pid_cool.set_params(params['Cool']) self.is_startup = True # Master on/off switch: while disabled, the FSM is held in IDLE and # the controller tries not to drive the heater at all (output @@ -58,6 +58,13 @@ class TempControllerBase(APid): # (manual mode) or by SudTask for the duration of a run. self.enabled = False + def set_params(self, params): + self.params = params + self.thresholds = {**DEFAULT_THRESHOLDS, **params.get('Thresholds', {})} + self.pid_hold.set_params(params['Hold']) + self.pid_heat.set_params(params['Heat']) + self.pid_cool.set_params(params['Cool']) + def on_state_entered(self, state): pass diff --git a/components/pid/temp_controller_smith.py b/components/pid/temp_controller_smith.py index 1db57be..5091945 100755 --- a/components/pid/temp_controller_smith.py +++ b/components/pid/temp_controller_smith.py @@ -3,8 +3,8 @@ from components.pid.temp_controller_base import TempControllerBase, States class TempController(TempControllerBase): - def __init__(self, dt, params, model_params, theta_amb=20): - TempControllerBase.__init__(self, dt, params) + def __init__(self, dt, model_params, theta_amb=20): + TempControllerBase.__init__(self, dt) self.dt = dt self.last_theta_ist = 20 self.heatrate_ist = 0 diff --git a/components/sud_forecast.py b/components/sud_forecast.py index fbb74bf..b3dc2c8 100644 --- a/components/sud_forecast.py +++ b/components/sud_forecast.py @@ -65,7 +65,8 @@ class SudForecastEstimator: pot.set_plant_params(self.plant_params) pot.set_ambient_temperature(self.theta_amb) 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.plant_params, theta_amb=self.theta_amb) + tc.set_params(self.tempctrl_params) tc.set_enabled(True) tc.set_theta_ist(pot.get_temperature()) # Seed the target at start_theta - this tc is a fresh, throwaway diff --git a/scripts/demos/pid/demo_temp_controller.py b/scripts/demos/pid/demo_temp_controller.py index 7ebacfa..c3af445 100644 --- a/scripts/demos/pid/demo_temp_controller.py +++ b/scripts/demos/pid/demo_temp_controller.py @@ -43,7 +43,8 @@ if __name__ == '__main__': temp_ist = 0 temp_soll = 20 heatrate_soll = 1.25 - ctrl = TempController(dt, ctrl_params) + ctrl = TempController(dt) + ctrl.set_params(ctrl_params) ctrl.set_enabled(True) plant = Pot(dt) plant.set_plant_params(plant_params) diff --git a/scripts/demos/pid/demo_temp_controller_smith.py b/scripts/demos/pid/demo_temp_controller_smith.py index 7534b5a..8bf602d 100644 --- a/scripts/demos/pid/demo_temp_controller_smith.py +++ b/scripts/demos/pid/demo_temp_controller_smith.py @@ -43,7 +43,8 @@ if __name__ == '__main__': temp_ist = 0 temp_soll = 20 heatrate_soll = 1.25 - ctrl = TempController(dt, ctrl_params, plant_params, theta_amb) + ctrl = TempController(dt, plant_params, theta_amb) + ctrl.set_params(ctrl_params) ctrl.set_enabled(True) plant = Pot(dt) plant.set_plant_params(plant_params) diff --git a/scripts/demos/sud/demo_sud.py b/scripts/demos/sud/demo_sud.py index aaa1e87..aef4127 100644 --- a/scripts/demos/sud/demo_sud.py +++ b/scripts/demos/sud/demo_sud.py @@ -48,7 +48,8 @@ if __name__ == '__main__': "Td" : 60 } - ctrl = TempController(dt, ctrl_params, plant_params, theta_amb) + ctrl = TempController(dt, plant_params, theta_amb) + ctrl.set_params(ctrl_params) plant = Pot(dt) plant.set_plant_params(plant_params) plant.set_ambient_temperature(theta_amb) diff --git a/server/brewpi.py b/server/brewpi.py index 2ea7283..fcc3131 100755 --- a/server/brewpi.py +++ b/server/brewpi.py @@ -88,7 +88,8 @@ if __name__ == '__main__': taskmgr.add(heater_task) # Temperature Controller - tc = PidFactory.create(config['Controller']['pid_type'], DT, config['TempCtrl'], DEFAULT_PLANT_PARAMS, theta_amb=theta_amb) + tc = PidFactory.create(config['Controller']['pid_type'], DT, DEFAULT_PLANT_PARAMS, theta_amb=theta_amb) + tc.set_params(config['TempCtrl']) tc_task = TcTask(tc, DT_TASK, dispatcher.msgio_get("TempCtrl")) taskmgr.add(tc_task)