-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmqtt_temp_sweep.py
More file actions
154 lines (117 loc) · 4.42 KB
/
mqtt_temp_sweep.py
File metadata and controls
154 lines (117 loc) · 4.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import miniconf
from gmqtt import Client as MqttClient
import json
import asyncio
import time
import csv
import numpy as np
from matplotlib import pyplot as plt
from pydwf import DwfLibrary, PyDwfError
from pydwf.utilities import openDwfDevice
# thermostat settings
PREFIX = "dt/sinara/thermostat-mqtt/80-1f-12-63-84-1a"
BROKER = "10.42.0.1"
# controller settings for LUCENT D2525P37
P_GAIN = 10
I_GAIN = 2
MAX_I_NEG = 0.3
MAX_I_POS = 1.0
MAXWAIT = 1000 # Maximum waiting time for temp to settle in periods
EM = 0.001 # Error margin for temp settle in °C
# Number of periods where temp has to be in margin to be considered settled
P_SETTLE = 2
# sweep settings
TEMP_START = 20 # starting temp in °C
TEMP_STOP = 60 # stop temp
STEP = 0.5 # temperature steps
async def setup_thermostat():
interface = await miniconf.Miniconf.create(PREFIX, BROKER)
await interface.command("pidsettings/0/max_i_pos", MAX_I_POS)
await interface.command("pidsettings/0/max_i_neg", MAX_I_NEG)
await interface.command("pidsettings/0/pid/0", P_GAIN)
await interface.command("pidsettings/0/pid/1", I_GAIN)
await interface.command("engage_iir/0", True)
class TelemetryReader:
""" Helper utility to read telemetry. """
@classmethod
async def create(cls, prefix, broker, queue):
"""Create a connection to the broker and an MQTT device using it."""
client = MqttClient(client_id='')
await client.connect(broker)
return cls(client, prefix, queue)
def __init__(self, client, prefix, queue):
""" Constructor. """
self.client = client
self._telemetry = []
self.client.on_message = self.handle_telemetry
self._telemetry_topic = f'{prefix}/telemetry'
self.client.subscribe(self._telemetry_topic)
self.queue = queue
def handle_telemetry(self, _client, topic, payload, _qos, _properties):
""" Handle incoming telemetry messages over MQTT. """
assert topic == self._telemetry_topic
self.queue.put_nowait(json.loads(payload))
async def get_tele(telemetry_queue):
latest_values = await telemetry_queue.get()
return [latest_values['adcs'][0], latest_values['dacs'][0], latest_values['adcs'][1]]
async def get_tele(telemetry_queue):
latest_values = await telemetry_queue.get()
return [latest_values['adcs'][0], latest_values['dacs'][0], latest_values['adcs'][1]]
def set_laser_temp(temp):
telemetry_queue = asyncio.LifoQueue()
async def telemetry():
await TelemetryReader.create(PREFIX, BROKER, telemetry_queue)
try:
while True:
await asyncio.sleep(1)
except asyncio.CancelledError:
pass
telemetry_task = asyncio.Task(telemetry())
async def set_and_wait_settle():
interface = await miniconf.Miniconf.create(PREFIX, BROKER)
await interface.command('pidsettings/0/target', temp, retain=False)
data = []
for i in range(MAXWAIT):
data.append(await get_tele(telemetry_queue))
print(f'temp: {data[i][0]}')
if P_SETTLE < len([x[0] for x in data[-20:] if (x[0] > (temp-EM)) and (x[0] < (temp+EM))]):
break
telemetry_task.cancel()
loop = asyncio.get_event_loop()
return loop.run_until_complete(set_and_wait_settle())
def main():
dwf = DwfLibrary()
with openDwfDevice(dwf) as device:
inp = device.analogIn
inp.reset()
print("setup thermostat")
loop = asyncio.get_event_loop()
temp = loop.run_until_complete(setup_thermostat())
print("initializing temp to ", TEMP_START)
set_laser_temp(TEMP_START)
print("sweep start")
temp_range = np.arange(TEMP_START, TEMP_STOP, STEP)
fig, ax = plt.subplots()
ax.set_title("")
plt.ion()
plt.show()
f = open('data.csv', 'w')
writer = csv.writer(f)
v = []
temps = []
for temp in temp_range:
set_laser_temp(temp)
inp.status(False)
print("analog input: {}", inp.statusSample(0))
v.append(inp.statusSample(0))
temps.append(temp)
writer.writerow([inp.statusSample(0)])
fig.canvas.draw()
ax.plot(temps, v)
plt.pause(0.0001)
fig.canvas.flush_events()
f.close()
print("done")
input()
if __name__ == "__main__":
main()