Files
brewpi/components/pid/temp_controller_base.py
T
jens 3fea29cc58 Add an enable/disable switch for the temperature controller
New TempControllerBase.enabled (default False) forces y=0 in
process_pid() when off, same mechanism as the existing cooling
override - the controller tries not to drive the heater at all.

tasks/tempctrl.py: new 'Enable' recv command and 'Enabled' broadcast
on the TempCtrl channel. tasks/sud.py: SudTask now enables the
controller on any non-IDLE/DONE Sud state and disables it (forcing
power back to 0) on IDLE/DONE, so automatic mode owns it for the
duration of a run and hands back manual control once it stops/finishes.

gui: new "Enabled" checkbox on the Manual tab's Controller group,
synced from the server; the temp/heatrate setpoint controls are
disabled whenever the controller itself is disabled.
2026-06-21 12:54:09 +02:00

138 lines
3.5 KiB
Python

from components import APid
from components.pid.pid import Pid
import enum
DEFAULT_THRESHOLDS = {
"HoldIdle": 0.1,
"HoldHeat": 1.0,
"IdleHeat": 1.0,
"IdleHold": 0.1,
"HeatHold": 1.0,
"HeatIdle": 1.0
}
class States(enum.Enum):
INIT = -1,
IDLE = 0,
HEAT = 1,
HOLD = 2
class TempControllerBase(APid):
def __init__(self, dt, params):
APid.__init__(self)
self.pid_hold = Pid(dt)
self.pid_rate = Pid(dt)
self.theta_ist_set = 0
self.theta_soll_set = 0
self.heatrate_ist_set = 0
self.heatrate_soll_set = 1.0
self.heatrate_soll = 1.0
self.theta_ist = 0
self.heatrate_ist = 0
self.params = params
self.thresholds = {**DEFAULT_THRESHOLDS, **params.get('Thresholds', {})}
self.y = -1
self.state = States.INIT
self.pid_hold.set_params(params['Hold'])
self.pid_rate.set_params(params['Heat'])
self.is_startup = True
# Explicit override for a ramp step whose target is below the
# current temperature: the heater can't actively cool, so rather
# than wait for the FSM's own (slower, hysteresis-based) IDLE
# transition, force y to 0 as soon as the caller (SudTask) knows
# the step needs to passively cool down.
self.cooling = False
# Master on/off switch: while disabled, the controller tries not to
# drive the heater at all (output forced to 0) regardless of the
# FSM/setpoints - off by default, enabled either by the user
# (manual mode) or by SudTask for the duration of a run.
self.enabled = False
def on_state_entered(self, state):
pass
def post_pid(self):
pass
def set_theta_ist(self, value):
self.theta_ist_set = value
if self.is_startup:
self.is_startup = False
def get_theta_ist(self):
return self.theta_ist
def set_heatrate_ist(self, value):
self.heatrate_ist_set = value
def get_heatrate_ist(self):
return self.heatrate_ist
def set_theta_soll(self, value):
self.theta_soll_set = value
def get_theta_soll(self):
return self.theta_soll
def get_theta_soll_set(self):
return self.theta_soll_set
def set_heatrate_soll(self, value):
self.heatrate_soll_set = value
def set_cooling(self, value):
self.cooling = value
def set_enabled(self, value):
self.enabled = value
def get_heatrate_soll(self):
return self.heatrate_soll
def get_heatrate_soll_set(self):
return self.heatrate_soll_set
def process_fsm(self, diff):
state_next = self.state
if self.state == States.INIT:
if not self.is_startup:
state_next = States.IDLE
elif self.state == States.IDLE:
if diff >= self.thresholds['IdleHeat']:
state_next = States.HEAT
self.pid_rate.reset()
elif diff >= -self.thresholds['IdleHold']:
state_next = States.HOLD
self.pid_rate.reset()
elif self.state == States.HOLD:
if diff >= self.thresholds['HoldHeat']:
state_next = States.HEAT
self.pid_rate.reset()
elif diff <= -self.thresholds['HoldIdle']:
state_next = States.IDLE
elif self.state == States.HEAT:
if diff <= -self.thresholds['HeatIdle']:
state_next = States.IDLE
elif diff <= self.thresholds['HeatHold']:
state_next = States.HOLD
self.pid_hold.reset()
if state_next != self.state:
self.state = state_next
self.on_state_entered(state_next)
def process_pid(self, theta_err, heatrate_err, hold_scale=1.0):
self.pid_hold.process(theta_err, -self.theta_ist, hold_scale)
self.pid_rate.process(heatrate_err, -self.heatrate_ist)
if self.state == States.IDLE or self.cooling or not self.enabled:
self.y = 0
else:
self.y = self.pid_rate.get_y()
self.post_pid()
def get_power(self):
return self.y