From 425567f3dbeed77187aef0bc678c37a0578b460f Mon Sep 17 00:00:00 2001 From: jens Date: Tue, 12 Oct 2021 09:11:07 +0200 Subject: [PATCH] =?UTF-8?q?-=20refactored=20-=20increased=20Hold.Pid.kp=20?= =?UTF-8?q?-=20decrease=20state=20change=20HEAT=20TO=20HOLD=20windows=20to?= =?UTF-8?q?=201=C2=B0C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- brewpi.py | 3 +- components/pid/__init__.py | 2 + components/pid/tc_constants.py | 49 +++++++++++++++++++ components/pid/temp_controller.py | 44 ++--------------- components/pid/temp_controller_smith.py | 63 ++++--------------------- 5 files changed, 65 insertions(+), 96 deletions(-) create mode 100644 components/pid/tc_constants.py diff --git a/brewpi.py b/brewpi.py index 44814e2..455f712 100755 --- a/brewpi.py +++ b/brewpi.py @@ -36,8 +36,7 @@ if __name__ == '__main__': taskmgr.add(heater_task) # Temperature Controller - tc_params = config['TempCtrl'] - tc = PidFactory.create(config['Controller']['pid_type'], DT, tc_params) + tc = PidFactory.create(config['Controller']['pid_type'], DT, config['TempCtrl'], config['Model']) tc_task = TcTask(tc, DT_TASK, dispatcher.msgio_get("TempCtrl")) taskmgr.add(tc_task) diff --git a/components/pid/__init__.py b/components/pid/__init__.py index fb14e21..d2e4291 100644 --- a/components/pid/__init__.py +++ b/components/pid/__init__.py @@ -1,3 +1,5 @@ from components.pid.pid import * from components.pid.kalman import * +from components.pid.tc_constants import * + from components.pid.pid_factory import PidFactory diff --git a/components/pid/tc_constants.py b/components/pid/tc_constants.py new file mode 100644 index 0000000..16139b8 --- /dev/null +++ b/components/pid/tc_constants.py @@ -0,0 +1,49 @@ + +THRESH_HOLD_IDLE = 0.1 +THRESH_HOLD_HEAT = 1.0 +THRESH_IDLE_HEAT = 1.0 +THRESH_IDLE_HOLD = 0.1 +THRESH_HEAT_HOLD = 1.0 +THRESH_HEAT_IDLE = 1.0 + + +class Test: + tc_ctrl_params = { + "Kalman": { + "var_P": 1.0, + "var_Q": 0.0001, + "var_R": 1.0 + }, + "Hold": { + "kp": 0.4, + "ki": 0.0, + "kd": 0.0, + "kt": 0.0 + }, + "Heat": { + "kp": 0.1, + "ki": 0.01, + "kd": 0.0, + "kt": 1.5 + } + } + + tc_model_params = { + "theta" : 20, + "C" : 4190, + "M" : 20, + "L" : 0.05, + "Td" : 15, + "kn" : 0.2, + "gain" : 0.8 + } + + tc_pot_params = { + "theta" : 20, + "C" : 4190, + "M" : 20, + "L" : 0.05, + "Td" : 15, + "kn" : 0.2, + "gain" : 0.8 + } diff --git a/components/pid/temp_controller.py b/components/pid/temp_controller.py index 343e1fd..0543871 100644 --- a/components/pid/temp_controller.py +++ b/components/pid/temp_controller.py @@ -4,6 +4,7 @@ from components.pid import Pid, Kalman from components import APid import numpy as np from enum import Enum +from components.pid.tc_constants import * class States(Enum): @@ -90,13 +91,6 @@ class TempController(APid): self.process_pid(theta_err, heatrate_err) def process_fsm(self, diff): - THRESH_HOLD_IDLE = 0.1 - THRESH_HOLD_HEAT = 2.0 - THRESH_IDLE_HEAT = 1.0 - THRESH_IDLE_HOLD = 0.1 - THRESH_HEAT_HOLD = 2.0 - THRESH_HEAT_IDLE = 2.0 - # Process state state_next = self.state if self.state == States.IDLE: @@ -139,40 +133,10 @@ class TempController(APid): if __name__ == '__main__': dt = 1.0 - tc_params = { - "Kalman": { - "var_P" : 1.0, - "var_Q" : 0.0001, - "var_R" : 1.0 - }, - "Hold": { - "kp": 0.2, - "ki": 0.0, - "kd": 0.0, - "kt": 0.0 - }, - "Heat": { - "kp": 0.1, - "ki": 0.01, - "kd": 0.0, - "kt": 1.5 - } - } - - pot_params = { - "theta" : 20, - "C" : 4190, - "M" : 20, - "L" : 0.05, - "Td" : 80, - "kn" : 0.2, - "gain" : 0.8 - } - temp_ist = 0 temp_soll = 20 - ctrl = TempController(dt, tc_params) - plant = Pot(dt, pot_params) + ctrl = TempController(dt, Test.tc_ctrl_params) + plant = Pot(dt, Test.tc_pot_params) _temp_ist = np.empty(0) _temp_soll = np.empty(0) _y = np.empty(0) @@ -198,10 +162,10 @@ if __name__ == '__main__': if hold_counter == 0: break hold_counter -= 1 - ctrl.process() plant.process() temp_ist = plant.get_temperature() + 0.0 * np.random.randn() ctrl.set_theta_ist(temp_ist) + ctrl.process() y = 3500*ctrl.get_power() power = max(0, y) diff --git a/components/pid/temp_controller_smith.py b/components/pid/temp_controller_smith.py index 629ec37..0fbddad 100755 --- a/components/pid/temp_controller_smith.py +++ b/components/pid/temp_controller_smith.py @@ -4,6 +4,7 @@ from components.pid import Pid, Kalman from components import APid import numpy as np from enum import Enum +from components.pid.tc_constants import * class States(Enum): @@ -13,7 +14,7 @@ class States(Enum): class TempController(APid): - def __init__(self, dt, params): + def __init__(self, dt, params, model_params): APid.__init__(self) self.pid_hold = Pid(dt) self.pid_rate = Pid(dt) @@ -25,6 +26,7 @@ class TempController(APid): self.theta_ist = 0 self.heatrate_ist = 0 self.params = params + self.model_params = model_params self.kalman_model = Kalman(dt, params['Kalman']) self.kalman_plant = Kalman(dt, params['Kalman']) self.y = -1 @@ -32,9 +34,9 @@ class TempController(APid): self.use_kalman = True self.pid_hold.set_params(params['Hold']) self.pid_rate.set_params(params['Heat']) - self.model = Pot(dt, params['Model']) - self.theta_ist_delay_model = Delay(dt, params['Model']['Td'], 0) - self.dtheta_ist_delay_model = Delay(dt, params['Model']['Td'], 0) + self.model = Pot(dt, model_params) + self.theta_ist_delay_model = Delay(dt, model_params['Td'], 0) + self.dtheta_ist_delay_model = Delay(dt, model_params['Td'], 0) self.is_startup = True def set_theta_ist(self, value): @@ -112,13 +114,6 @@ class TempController(APid): self.process_pid(theta_err, heatrate_err) def process_fsm(self, diff): - THRESH_HOLD_IDLE = 0.1 - THRESH_HOLD_HEAT = 2.0 - THRESH_IDLE_HEAT = 1.0 - THRESH_IDLE_HOLD = 0.1 - THRESH_HEAT_HOLD = 2.0 - THRESH_HEAT_IDLE = 2.0 - # Process state state_next = self.state if self.state == States.IDLE: @@ -167,50 +162,10 @@ class TempController(APid): if __name__ == '__main__': dt = 1.0 - tc_params = { - "Model": { - "theta" : 20, - "C" : 4190, - "M" : 20, - "L" : 0.05, - "Td" : 80, - "kn" : 0.2, - "gain" : 0.8 - }, - "Kalman": { - "var_P" : 1.0, - "var_Q" : 0.0001, - "var_R" : 1.0 - }, - "Hold": { - "kp": 0.2, - "ki": 0.0, - "kd": 0.0, - "kt": 0.0 - }, - "Heat": { - "kp": 0.1, - "ki": 0.01, - "kd": 0.0, - "kt": 1.5 - } - } - - pot_params = { - "dt" : 1.0, - "theta" : 20, - "C" : 4190, - "M" : 20 + 4, - "L" : 0.07, - "Td" : 80 + 5, - "kn" : 0.2, - "gain" : 0.8 - } - temp_ist = 0 temp_soll = 20 - ctrl = TempController(dt, tc_params) - plant = Pot(dt, pot_params) + ctrl = TempController(dt, Test.tc_ctrl_params, Test.tc_model_params) + plant = Pot(dt, Test.tc_pot_params) _temp_ist = np.empty(0) _temp_soll = np.empty(0) _y = np.empty(0) @@ -237,10 +192,10 @@ if __name__ == '__main__': if hold_counter == 0: break hold_counter -= 1 - ctrl.process() plant.process() temp_ist = plant.get_temperature() ctrl.set_theta_ist(temp_ist) + ctrl.process() y = 3500*ctrl.get_power() power = max(0, y)