Skip to content
Snippets Groups Projects
Commit 13731198 authored by Torben Petersen's avatar Torben Petersen
Browse files

Adds new class to emulate temperature flow

parent bc024b60
No related branches found
No related tags found
No related merge requests found
...@@ -445,7 +445,7 @@ class ChamberController(Module): ...@@ -445,7 +445,7 @@ class ChamberController(Module):
logger.debug( "target: {} ambient: {} chamber: {}".format( self.target_temp, self.ambient_temp, self.chamber_temp ) ) logger.debug( "target: {} ambient: {} chamber: {}".format( self.target_temp, self.ambient_temp, self.chamber_temp ) )
start = time.time() start = time.time()
# When the controller sets the the temp to none, this might throw an exception # When the controller sets the the temp to none, this might throw an exception
self.control_tick_pid(protocol) self.control_tick_pid(protocol) #temp is set here
diff = time.time() - start diff = time.time() - start
if diff < 1.0: if diff < 1.0:
time.sleep(1.0 - diff) time.sleep(1.0 - diff)
......
File added
File added
#!/usr/bin/env python3
import paho.mqtt.client as paho
import paho.mqtt.publish as publish
import time
import socket
import struct
from threading import Event
class temp_flow_controller:
CHAMBER_NAME = "lctc-c01"
TARGET_TEMPERATURE = 10.0
DELTA = 0.5
TIMEOUT = 120.0
CHAMBER_TEMPERATURE_TOPIC = "sht21/"+CHAMBER_NAME+"/temp"
temperature_reached = Event() #set event flag to false
client = paho.Client(client_id="chamber_controller_" + socket.gethostname(), clean_session=True)
def on_message(self, client, userdata, msg):
if msg.topic == self.CHAMBER_TEMPERATURE_TOPIC:
temp, = struct.unpack("d", msg.payload)
if abs( temp - self.TARGET_TEMPERATURE ) < self.DELTA:
self.temperature_reached.set() #set the event flag to true
else:
print("[on_message] {} {}".format(msg.payload, msg.topic) )
def on_disconnect(self, client, userdata, rc):
print("[on_disconnect] {}".format( rc ))
def on_connect(self, client, userdata, flags, rc):
print("connected sucessfull")
client.subscribe(self.CHAMBER_TEMPERATURE_TOPIC, qos=2)
def set_temp(self,temp):
self.client.publish("chamber/"+self.CHAMBER_NAME+"/temp", struct.pack("d", temp), qos=2)
self.temperature_reached.clear() #Set Event flag to false and Block until set is called
def wait_for_temp_reached(self, timeout = None):
if(timeout != None):
temperature_reached.wait(timeout)
else:
temperature_reached.wait()
def set_temp_flow(self, temps, sleepTimes=[0], loop=False):
if temps == None:
raise ValueError('The temps array cant be none!')
if loop == None:
loop = false
if len(sleepTimes) < len(temps):
while len(sleepTimes) < len(temps):
sleepTimes.append(0)
while loop:
self.TARGET_TEMPERATURE = temps[0]
try:
self.set_temp( self.TARGET_TEMPERATURE )
# Wait until temperature has been reached
if not self.temperature_reached.wait(): #wait until flag is set or TIMEOUT
print("TIMEOUT")
else:
print("Temp reached: {}".format(self.TARGET_TEMPERATURE))
except KeyboardInterrupt:
set_temp( None )
print("sleep for {} seconds".format(sleepTimes[0]))
time.sleep(sleepTimes[0])
if len(temps) > 1:
for i in range(1,len(temps)):
print("i = {}".format(i))
time.sleep(10)
self.TARGET_TEMPERATURE = temps[i]
try:
self.set_temp( self.TARGET_TEMPERATURE )
# Wait until temperature has been reached
if not self.temperature_reached.wait(): #wait until flag is set or TIMEOUT
print("TIMEOUT")
else:
print("Temp reached: {}".format( self.TARGET_TEMPERATURE))
except KeyboardInterrupt:
set_temp( None )
print("sleep for {} seconds".format(sleepTimes[i]))
time.sleep(sleepTimes[i])
def start_connection(self, host = "lctc-c01.dyn.ibr.cs.tu-bs.de", port=1883, keepalive=5, chamber_name = "lctc-c01"):
self.CHAMBER_NAME = chamber_name
self.CHAMBER_TEMPERATURE_TOPIC = "sht21/"+self.CHAMBER_NAME+"/temp"
self.client.connect( host, port=port, keepalive=keepalive )
self.client.on_connect = self.on_connect
self.client.on_message = self.on_message
self.client.on_disconnect = self.on_disconnect
self.client.loop_start()
if __name__ == "__main__":
flow = temp_flow_controller()
flow.start_connection()
temps = [11.0, 12.0, 13.0] #temp Points that should be reached
sleepTimes = [2, 8, 25] #sleepTimes for each targetTemp
loop = True #repeat again
flow.set_temp_flow(temps, sleepTimes, loop)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment