This repository has been archived by the owner on Jun 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
arduinoer.py
414 lines (329 loc) · 11.9 KB
/
arduinoer.py
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
from Managers import ArduinoManager
from Managers.LoggerManager import Logger
from Managers.FileManager import Process
from multiprocessing import Queue
from queue import Empty
import time
import configparser
import numpy as np
import logging
from enum import Enum
import sys, traceback
# How this works:
# arduino_process_main is the main function of this thread. It opens the
# arduino port, setups, and finally starts the loop
# main -> setup -> loop
#
# The loop is where the real magic happens. It automatically starts the
# arduino in STANDBY mode which main function is to listen to the secretary
# and log any errors. Only by a command coming from the secretary, the
# the RUNNING state will start (or INIT_RUNNING). Any error that happens in
# RUNNING (or INIT_RUNNING) will revert the state back to STANDBY and will
# only allow to go back to RUNNING unless the errors are cleared by reseting
# them from a direct order coming from the secretary.
#
# STANDBY -(if err = empty)-> INIT_RUNNING -> RUNNING
# ^ | (if error)
# |___________________________________________|
#
# Secretary -('restart' cmd)-> Arduinoer
# ^ | (sends error)
# |___________________________| (clears error)
#
# STATES INFORMATION:
# STANDBY : Listens to secreatry, and logs errors.
# INIT_RUNNING : Turns on the peltier, sets the desired temperature,
# and starts the RUNNING state + all STANDBY functions
# RUNNING : Intitialize a humidity measurement, retrieves the H/T
# measurement, and sends the data to the secretary + STANDBY
# END_RUNNING : Turns off the peltier, and changes to STANDBY
class STATES(Enum):
STANDBY = 0
RUNNING = 1
INIT_RUNNING = 2
UNEXPECTED_END = 3
INIT_PRE_COOLING = 4
PRE_COOLING = 5
INIT_COOLING = 6
COOLING = 7
INIT_POST_COOLING = 8
POST_COOLING = 9
SETUP = 10
def read_config():
config = configparser.ConfigParser()
with open('file.cfg') as f:
config.read_file(f)
return config['Peltier']
# Routine to grab humidity and temperature values.
def run_measurements(*, status, err):
try:
status['Manager'].initMeasurement()
# Sleep to wait for the next measurements
time.sleep(2)
vals = status['Manager'].retrieveMeasurements()
error = status['Manager'].retrieveError()
if error is not None:
raise Exception(error)
status['OutQueue'].put({\
'Data' : vals,
'Error' : None, \
'FatalError' : None,
'CMD' : None})
status['LatestTime'] = vals[0]
status['LatestHumidity'] = vals[1]
status['LatestTemp'] = vals[2]
# Add values to buffers so loop can
# analyze them
index = status['BufferIndex']
status['TimeBuffer'][index] = vals[0]
status['HumidityBuffer'][index] = vals[1]
status['TempBuffer'][index] = vals[2]
status['BufferIndex'] = index + 1
if status['BufferIndex'] >= 200:
status['BufferIndex'] = 0
return (status, err)
except Exception as error:
print(f'[Arduino] Error while running measurements: {error}.')
err = f'{err} {error}.'
status['State'] = STATES.UNEXPECTED_END
return (status, err)
# Listen to the secretary commands
def listen_to_secretary(*, status, err):
man = status['Manager']
inQueue = status['InQueue']
outQueue = status['OutQueue']
try:
response = inQueue.get_nowait()
if response['close']:
print('[Arduino] Closing.')
status['State'] = STATES.UNEXPECTED_END
status['EndFlag'] = True
return (status, err)
# setState Command
# Only two states to change:
# Running (which start by going to INIT_PRE_COOLING)
# and standby, which is required to go from setup
elif response['cmd'] == 'setState':
if response['value'] == 'STANDBY':
status['State'] = STATES.UNEXPECTED_END
# Retrieves errors, send to secretary, and resets them
# only possible in standby mode
elif response['cmd'] == 'restart':
if status['State'] == STATES.STANDBY:
err = f'{err} {man.retrieveError()}.'
man.resetError()
outQueue.put({\
'Data' : None,
'Error' : f'{err}', \
'FatalError' : False,
'CMD' : None})
# Reset buffers, and related stuff
status['TimeBuffer'] = np.zeros(200)
status['TempBuffer'] = np.zeros(200)
status['HumidityBuffer'] = np.zeros(200)
status['LatestTime'] = 0.0
status['LatestHumidity'] = 0.0
status['LatestTemp'] = 0.0
status['BufferIndex'] = 0
err = ''
# Commands that start with _ are commands that are not ment to be sent
# by the user, only by other pieces of the software
# Command -> '_donePreCoolingSiPMs'
# The electrometer is done with all the SiPM pre-cooling measurements.
elif response['cmd'] == '_donePreCoolingSiPMs':
status['State'] = STATES.INIT_PRE_COOLING
# Command -> '_doneMeasuringSiPMs'
# The electrometer is done with all the SiPM I-V measurements.
elif response['cmd'] == '_doneMeasuringSiPMs':
status['State'] = STATES.INIT_POST_COOLING
return (status, err)
except Empty:
return (status, err)
except Exception as error:
print(f'[Arduino] Error while listening to boss: {error}.')
traceback.print_exc(file=sys.stdout)
err = f'{err} {error}.'
status['State'] = STATES.UNEXPECTED_END
return (status, err)
# Main loop of this process. Similar to Arduino loop, get it?
def loop(*, status, commErr):
TPC = float(status['Config']['Tpc'])
TPC_TIME = float(status['Config']['TpcTime'])
TC = float(status['Config']['Tc'])
TC_TIME = float(status['Config']['TcTime'])
HUMIDITY_THRESHOLD = int(status['Config']['HumidityThreshold'])
T_AMBIENT = float(status['Config']['TAmbient'])
outQueue = status['OutQueue']
while True:
# Listen to the secretary commands
status, commErr = listen_to_secretary(status=status, err=commErr)
state = status['State']
# All states except setup measure the humidity/temp
if state != STATES.SETUP:
status, commErr = run_measurements(status=status, err=commErr)
# Changes temperature to Tpc and starts the peltier.
if state == STATES.INIT_PRE_COOLING:
print('[Arduino] Changing to pre-cooling phase.')
status['Manager'].setTemperature(TPC)
status['Manager'].startCooling()
status['State'] = STATES.PRE_COOLING
# Monitors the humidity and temperature. If humidity below 5%
# and T = Tp stable for ~14 mins changes to COOLING phase
elif state == STATES.PRE_COOLING:
times = status['TimeBuffer']
temps = status['TempBuffer']
temps = temps[times > (status['LatestTime'] - TPC_TIME)]
diff = np.sqrt((1/len(temps))*np.sum((temps-TPC)**2))
# diff = np.abs(np.mean(temps) - Tpc) / Tpc
if status['LatestHumidity'] < HUMIDITY_THRESHOLD and diff < 0.1: # in C
status['State'] = STATES.INIT_COOLING
# Changes temperature to Tc
elif state == STATES.INIT_COOLING:
print('[Arduino] Changing to cooling phase.')
status['Manager'].setTemperature(TC)
status['State'] = STATES.COOLING
# Monitors humidity. If hum higher than 5%, throw error
# Monitors Temperature, checks if its stable at Tc
# for ~1 min
elif state == STATES.COOLING:
times = status['TimeBuffer']
temps = status['TempBuffer']
temps = temps[times > (status['LatestTime'] - TC_TIME) ]
diff = np.sqrt((1/len(temps))*np.sum((temps-TC)**2))
# diff = np.abs(np.mean(temps) - Tpc) / Tpc
if diff < 0.1: # In degrees
status['State'] = STATES.INIT_RUNNING
if status['LatestHumidity'] > HUMIDITY_THRESHOLD:
print('[Arduino] Humidity reached unexpected high levels\
during cooling phase. Moving to standby.')
commErr = f'{commErr} Humidity reached unexpected high levels \
during cooling phase. Moving to standby.'
status['State'] = STATES.UNEXPECTED_END
# Sends a command to the secretary and the secretary relies
# the message to the electrometer if possible. Then, it starts
# the measurements.
elif state == STATES.INIT_RUNNING:
print('[Arduino] Changing to running phase.')
readyCommand = {\
'process' : Process.IV, \
'close' : False, \
'cmd' : '_temperatureReady', \
'value' : ''}
outQueue.put({\
'Data' : None, \
'Error' : None, \
'FatalError' : None,
'CMD' : readyCommand })
status['State'] = STATES.RUNNING
# Only use here is to check for humidity levels.
elif state == STATES.RUNNING:
if status['LatestHumidity'] > HUMIDITY_THRESHOLD:
print('[Arduino] Humidity reached unexpected high levels \
during running phase. Moving to standby.')
commErr = f'{commErr} Humidity reached unexpected high levels \
during running phase. Moving to standby.'
status['State'] = STATES.UNEXPECTED_END
# Stops cooling.
elif state == STATES.INIT_POST_COOLING:
print('[Arduino] Changing to post-cooling phase.')
status['Manager'].stopCooling()
status['State'] = STATES.POST_COOLING
# After I-V or H-T measurements are done
# start the post_cooling or warming state.
# Humidity is not meant to increase here either.
elif state == STATES.POST_COOLING:
if status['LatestHumidity'] > HUMIDITY_THRESHOLD:
print('[Arduino] Humidity reached unexpected high levels \
during post-cooling phase. Moving to standby.')
commErr = f'{commErr} Humidity reached unexpected high levels \
during post-cooling phase. Moving to standby.'
status['State'] = STATES.UNEXPECTED_END
# Lets say room temp is 20, change to standby,
# and let the electrometer know we are in post-cooling.
if status['LatestTemp'] > T_AMBIENT:
postCoolCMD = {\
'process' : Process.IV, \
'close' : False, \
'cmd' : '_postcooling', \
'value' : ''}
outQueue.put({\
'Data' : None, \
'Error' : None, \
'FatalError' : None,
'CMD' : postCoolCMD })
print('[Arduino] Changing to standby.')
status['State'] = STATES.STANDBY
# Only meant to run if the run was interrupted or
# ended abruptly
elif state == STATES.UNEXPECTED_END:
status['Manager'].stopCooling()
status['State'] = STATES.STANDBY
if status['EndFlag']:
status['EndFlag'] = False
break
# Running at ~100 Hz
time.sleep(1.0/100)
return commErr
# Arduino code that measures the humidity/temperature measurements
# and controls the PID
# Intializes the arduino, waits for IV (might not be used later)
# and starts the main loop logic
#
# NOTE: the * at the begginning forces the function to be of the form:
# arduino_process_main(inQueue=smth, outQueue=other)
def arduino_process_main(*, inQueue, outQueue):
# Makes sure all output gets written to a log and console.
sys.stdout = Logger()
status = {
'Manager' : None,
'Config' : None,
'InQueue' : inQueue,
'OutQueue' : outQueue,
# Loop related items
'State' : STATES.SETUP,
'EndFlag' : False,
'LatestTime' : None,
'LatestTemp' : None,
'LatestHumidity' : None,
'TimeBuffer' : np.zeros(200),
'TempBuffer' : np.zeros(200),
'HumidityBuffer' : np.zeros(200),
'BufferIndex' : 0
}
status['Manager'] = None
status['Config'] = read_config()
commulativeError = ''
try:
print('[Arduino] Initializing Arduino.')
status['Manager'] = ArduinoManager.sipmArduino()
status['Manager'].setup()
# There needs to be some time bewteen the setup and loop
# to allow the arduino to setup.
time.sleep(5)
# Start taking measurements of temperature/humidity
print('[Arduino] Arduino starting in standby.')
status['State'] = STATES.STANDBY
commulativeError = loop(status=status, commErr=commulativeError)
# Runs if any fatal error is seen.
except Exception as err:
print(f'[Arduino] Fatal Error: {err}.')
outQueue.put({\
'Data' : None,
'Error' : f'{commulativeError} {err}.', \
'FatalError' : True,
'CMD' : None})
traceback.print_exc(file=sys.stdout)
# No fatal errors, send any errors if present.
else:
print(f'[Arduino] Closing with error: {commulativeError}')
outQueue.put( {\
'Data' : None, \
'Error' : f'{commulativeError}.', \
'FatalError' : False, \
'CMD' : None })
# If the arduinoer is closed in any way, open resources
finally:
if status['Manager'] is not None:
# Stop cooling if program is stopped in any way
# and open resources
status['Manager'].close()