- refactored

- increased Hold.Pid.kp
- decrease state change HEAT TO HOLD windows to 1°C
This commit is contained in:
jens
2021-10-12 09:11:07 +02:00
parent ff90c6d90c
commit 425567f3db
5 changed files with 65 additions and 96 deletions
+1 -2
View File
@@ -36,8 +36,7 @@ if __name__ == '__main__':
taskmgr.add(heater_task) taskmgr.add(heater_task)
# Temperature Controller # Temperature Controller
tc_params = config['TempCtrl'] tc = PidFactory.create(config['Controller']['pid_type'], DT, config['TempCtrl'], config['Model'])
tc = PidFactory.create(config['Controller']['pid_type'], DT, tc_params)
tc_task = TcTask(tc, DT_TASK, dispatcher.msgio_get("TempCtrl")) tc_task = TcTask(tc, DT_TASK, dispatcher.msgio_get("TempCtrl"))
taskmgr.add(tc_task) taskmgr.add(tc_task)
+2
View File
@@ -1,3 +1,5 @@
from components.pid.pid import * from components.pid.pid import *
from components.pid.kalman import * from components.pid.kalman import *
from components.pid.tc_constants import *
from components.pid.pid_factory import PidFactory from components.pid.pid_factory import PidFactory
+49
View File
@@ -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
}
+4 -40
View File
@@ -4,6 +4,7 @@ from components.pid import Pid, Kalman
from components import APid from components import APid
import numpy as np import numpy as np
from enum import Enum from enum import Enum
from components.pid.tc_constants import *
class States(Enum): class States(Enum):
@@ -90,13 +91,6 @@ class TempController(APid):
self.process_pid(theta_err, heatrate_err) self.process_pid(theta_err, heatrate_err)
def process_fsm(self, diff): 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 # Process state
state_next = self.state state_next = self.state
if self.state == States.IDLE: if self.state == States.IDLE:
@@ -139,40 +133,10 @@ class TempController(APid):
if __name__ == '__main__': if __name__ == '__main__':
dt = 1.0 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_ist = 0
temp_soll = 20 temp_soll = 20
ctrl = TempController(dt, tc_params) ctrl = TempController(dt, Test.tc_ctrl_params)
plant = Pot(dt, pot_params) plant = Pot(dt, Test.tc_pot_params)
_temp_ist = np.empty(0) _temp_ist = np.empty(0)
_temp_soll = np.empty(0) _temp_soll = np.empty(0)
_y = np.empty(0) _y = np.empty(0)
@@ -198,10 +162,10 @@ if __name__ == '__main__':
if hold_counter == 0: if hold_counter == 0:
break break
hold_counter -= 1 hold_counter -= 1
ctrl.process()
plant.process() plant.process()
temp_ist = plant.get_temperature() + 0.0 * np.random.randn() temp_ist = plant.get_temperature() + 0.0 * np.random.randn()
ctrl.set_theta_ist(temp_ist) ctrl.set_theta_ist(temp_ist)
ctrl.process()
y = 3500*ctrl.get_power() y = 3500*ctrl.get_power()
power = max(0, y) power = max(0, y)
+9 -54
View File
@@ -4,6 +4,7 @@ from components.pid import Pid, Kalman
from components import APid from components import APid
import numpy as np import numpy as np
from enum import Enum from enum import Enum
from components.pid.tc_constants import *
class States(Enum): class States(Enum):
@@ -13,7 +14,7 @@ class States(Enum):
class TempController(APid): class TempController(APid):
def __init__(self, dt, params): def __init__(self, dt, params, model_params):
APid.__init__(self) APid.__init__(self)
self.pid_hold = Pid(dt) self.pid_hold = Pid(dt)
self.pid_rate = Pid(dt) self.pid_rate = Pid(dt)
@@ -25,6 +26,7 @@ class TempController(APid):
self.theta_ist = 0 self.theta_ist = 0
self.heatrate_ist = 0 self.heatrate_ist = 0
self.params = params self.params = params
self.model_params = model_params
self.kalman_model = Kalman(dt, params['Kalman']) self.kalman_model = Kalman(dt, params['Kalman'])
self.kalman_plant = Kalman(dt, params['Kalman']) self.kalman_plant = Kalman(dt, params['Kalman'])
self.y = -1 self.y = -1
@@ -32,9 +34,9 @@ class TempController(APid):
self.use_kalman = True self.use_kalman = True
self.pid_hold.set_params(params['Hold']) self.pid_hold.set_params(params['Hold'])
self.pid_rate.set_params(params['Heat']) self.pid_rate.set_params(params['Heat'])
self.model = Pot(dt, params['Model']) self.model = Pot(dt, model_params)
self.theta_ist_delay_model = Delay(dt, params['Model']['Td'], 0) self.theta_ist_delay_model = Delay(dt, model_params['Td'], 0)
self.dtheta_ist_delay_model = Delay(dt, params['Model']['Td'], 0) self.dtheta_ist_delay_model = Delay(dt, model_params['Td'], 0)
self.is_startup = True self.is_startup = True
def set_theta_ist(self, value): def set_theta_ist(self, value):
@@ -112,13 +114,6 @@ class TempController(APid):
self.process_pid(theta_err, heatrate_err) self.process_pid(theta_err, heatrate_err)
def process_fsm(self, diff): 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 # Process state
state_next = self.state state_next = self.state
if self.state == States.IDLE: if self.state == States.IDLE:
@@ -167,50 +162,10 @@ class TempController(APid):
if __name__ == '__main__': if __name__ == '__main__':
dt = 1.0 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_ist = 0
temp_soll = 20 temp_soll = 20
ctrl = TempController(dt, tc_params) ctrl = TempController(dt, Test.tc_ctrl_params, Test.tc_model_params)
plant = Pot(dt, pot_params) plant = Pot(dt, Test.tc_pot_params)
_temp_ist = np.empty(0) _temp_ist = np.empty(0)
_temp_soll = np.empty(0) _temp_soll = np.empty(0)
_y = np.empty(0) _y = np.empty(0)
@@ -237,10 +192,10 @@ if __name__ == '__main__':
if hold_counter == 0: if hold_counter == 0:
break break
hold_counter -= 1 hold_counter -= 1
ctrl.process()
plant.process() plant.process()
temp_ist = plant.get_temperature() temp_ist = plant.get_temperature()
ctrl.set_theta_ist(temp_ist) ctrl.set_theta_ist(temp_ist)
ctrl.process()
y = 3500*ctrl.get_power() y = 3500*ctrl.get_power()
power = max(0, y) power = max(0, y)