- fixed water model

This commit is contained in:
jens
2020-11-29 19:12:17 +01:00
parent 4a87f361dd
commit 5102ad935c
6 changed files with 30 additions and 34 deletions
+8 -8
View File
@@ -30,10 +30,10 @@ class TempSensorTask(ATask):
def on_temp_changed(self, value):
print("Sensor temp {}".format(value))
asyncio.create_task(self.send({'Temp': value}))
def on_temp_changed_kalman(self, value):
print("Kalman temp {}".format(value))
asyncio.create_task(self.send({'Temp': value}))
async def recv(self, data):
print(data)
@@ -87,7 +87,7 @@ class HeaterTask(ATask):
power = Value()
while True:
self.power_soll = 200 + self.heater.get_power_max()*self.y
self.power_soll = max(0, 200 + self.heater.get_power_max()*self.y)
self.heater.setPower(self.power_soll)
self.heater.process()
power.set(round(self.heater.getPower()))
@@ -151,7 +151,7 @@ class TcTask(ATask):
async def on_process(self):
print("{}: Started with interval {} s".format(self.msg_handler.get_key(), self.interval))
self.tc.set_theta_soll(20)
self.tc.set_theta_soll(30)
while True:
self.tc.process()
await asyncio.sleep(self.interval)
@@ -203,14 +203,14 @@ if __name__ == '__main__':
# Plant
pot_params = {
"dt" : 1.0,
"theta_amb" : 15,
"theta" : 20,
"C" : 4190,
"M" : 20,
"M" : 10,
"L" : 0.1,
"Td" : 12,
"kn" : 0.2
}
pot = Pot(pot_params)
pot = Pot(pot_params, 20)
pot.set_on_changed("temp", ValueChanged(sensor.set_fake_temp, prec=1).set)
taskmgr.add(PotTask(pot, 1.0, dispatcher.msgio_get("Pot")))
@@ -225,9 +225,9 @@ if __name__ == '__main__':
"dt": 1.0,
"Hold": {
"Pid": {
"kp": 0.5,
"kp": 0.4,
"ki": 0.0,
"kd": 0.5,
"kd": 0.0,
"rho": 1.0
}
},
-4
View File
@@ -7,10 +7,6 @@ class ATemperatureSensor(AttributeChange):
def __init__(self):
AttributeChange.__init__(self)
def log(self, s):
d = {'user': "TemperatureSensor" + "::" + self.name()}
logging.info("{}".format(s), extra=d)
@abc.abstractmethod
def name(self):
return ""
+11 -3
View File
@@ -74,11 +74,13 @@ if __name__ == '__main__':
ctrl = TempController(params)
_temp_ist = np.empty(0)
_temp_soll = np.empty(0)
_y = np.empty(0)
_fb = np.empty(0)
_t = np.empty(0)
a = 0.5
fb = 0
rho = 0.001
temps = [{'Temp': 20, 'Duration': 100}, {'Temp': 40, 'Duration': 100}, {'Temp': 30, 'Duration': 100}]
rho = 0.02
temps = [{'Temp': 20, 'Duration': 100}, {'Temp': 40, 'Duration': 100}, {'Temp': 35, 'Duration': 100}]
t = 0
for temp in temps:
@@ -93,7 +95,7 @@ if __name__ == '__main__':
break
hold_counter -= 1
ctrl.process()
y = ctrl.get_power_hold()
y = max(0, ctrl.get_power_hold())
fb = (1-a)*fb + a*round(y, 2)
temp_ist += fb
ctrl.set_theta_ist(temp_ist)
@@ -103,12 +105,18 @@ if __name__ == '__main__':
temp_ist -= rho
_temp_ist = np.append(_temp_ist, temp_ist)
_temp_soll = np.append(_temp_soll, temp_soll)
_y = np.append(_y, y)
_fb = np.append(_fb, fb)
_t = np.append(_t, t)
t += 1
figure(1)
subplot(2, 1, 1)
plot(_t, _temp_ist, _t, _temp_soll, 'r-', linewidth=1)
grid(True)
subplot(2, 1, 2)
plot(_t, _y, 'bx', _t, _fb, '-r', linewidth=1)
grid(True)
show()
print("End of program")
+7 -11
View File
@@ -3,7 +3,7 @@ from components.aplant import APlant
class Pot(APlant):
def __init__(self, params):
def __init__(self, params, theta_amb=20):
APlant.__init__(self)
self.dt = params['dt']
self.alpha = 1.0
@@ -17,10 +17,9 @@ class Pot(APlant):
self.L = params['L']
self.Td = params['Td']
self.kn = params['kn']
self.theta_amb = params['theta_amb']
self.temp = params['theta']
self.theta = 0
self.temp = self.theta_amb
self.theta_amb = theta_amb
self.alpha = self.dt / 1
self.alpha_delay = self.dt/self.Td
@@ -34,12 +33,8 @@ class Pot(APlant):
# Delay
self.power_actual = (1-self.alpha_delay) * self.power_actual + self.alpha_delay * self.power_set
self.e = self.e * (1 - ((self.L * self.theta) * self.dt) / (self.M * self.C))
self.x = (1 - self.alpha) * self.x + self.gain * self.alpha * self.power_actual
self.e += self.x
self.theta = self.e / (self.M * self.C)
self.temp = self.getTemperature()
leak = 1/self.M * self.L * (self.theta_amb - self.temp)/self.theta_amb
self.temp += (self.power_actual / (self.M * self.C) + leak) * self.dt
def setPower(self, power):
self.power_set = power
@@ -48,4 +43,5 @@ class Pot(APlant):
return round(self.power_actual, 1)
def getTemperature(self):
return round(self.theta + self.theta_amb + self.kn * np.random.normal(0, 1) / np.sqrt(12.0), 1)
#return round(self.theta + self.theta_amb + self.kn * np.random.normal(0, 1) / np.sqrt(12.0), 1)
return self.temp
+3 -6
View File
@@ -1,5 +1,5 @@
from components.atemperatureSensor import ATemperatureSensor
import math
import numpy as np
class TempSensorSim(ATemperatureSensor):
@@ -8,18 +8,15 @@ class TempSensorSim(ATemperatureSensor):
def __init__(self):
ATemperatureSensor.__init__(self)
self.count = 0
self.freq = 0.2
self.log("Created")
self.temp = 22.37
self.temp_set = self.temp
self.k_noise = 0.01
def set_fake_temp(self, temp):
self.temp_set = temp
def temperature(self):
self.count += 1
return self.temp_set + 0.1*math.sin(2*math.pi*self.count*self.freq)
return self.temp_set + self.k_noise * np.random.normal(0, 1) / np.sqrt(12.0)
def process(self):
temp = self.temperature()
+1 -2
View File
@@ -45,9 +45,8 @@ class AttributeChange(object):
def __setattr__(self, key, value):
try:
old = super().__getattribute__(key)
print("Change: {} from {} to {}".format(key, old, value))
except:
print("Create: {} and initialize with {}".format(key, value))
pass
super().__setattr__(key, value)
if key in self.callbacks: