diff --git a/modules/chambercontroller.py b/modules/chambercontroller.py index ebc2cee67a08c24d51637c988a233589125acac8..65a22487ccb9d321654f536732edb7ead190d4eb 100644 --- a/modules/chambercontroller.py +++ b/modules/chambercontroller.py @@ -445,7 +445,7 @@ class ChamberController(Module): logger.debug( "target: {} ambient: {} chamber: {}".format( self.target_temp, self.ambient_temp, self.chamber_temp ) ) start = time.time() # 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 if diff < 1.0: time.sleep(1.0 - diff) diff --git a/util/__pycache__/chamber_flow.cpython-36.pyc b/util/__pycache__/chamber_flow.cpython-36.pyc new file mode 100644 index 0000000000000000000000000000000000000000..c33378854085a1dac1156e0590cb0563f25c534d Binary files /dev/null and b/util/__pycache__/chamber_flow.cpython-36.pyc differ diff --git a/util/__pycache__/temp_flow_controller.cpython-36.pyc b/util/__pycache__/temp_flow_controller.cpython-36.pyc new file mode 100644 index 0000000000000000000000000000000000000000..67055a2928df8f85035fd89920d37c0d93033bfa Binary files /dev/null and b/util/__pycache__/temp_flow_controller.cpython-36.pyc differ diff --git a/util/temp_flow_controller.py b/util/temp_flow_controller.py new file mode 100644 index 0000000000000000000000000000000000000000..7ba85a5afc8cc3d0be14788c2d550ddcea3dcf3b --- /dev/null +++ b/util/temp_flow_controller.py @@ -0,0 +1,105 @@ + + +#!/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) +