-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_exp.py
264 lines (232 loc) · 8.96 KB
/
run_exp.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
# this file runs the experiments for the APPJ testbed in the following paper:
#
#
# Requirements:
# * Python 3
# * several 3rd party packages including CasADi, NumPy, Scikit-Optimize for
# the implemented algorithms and Seabreeze, os, serial, etc. for connection to
# the experimental setup.
#
# Copyright (c) 2021 Mesbah Lab. All Rights Reserved.
# Contributor(s): Kimberly Chan
# Affiliation: University of California, Berkeley
#
# This file is under the MIT License. A copy of this license is included in the
# download of the entire code package (within the root folder of the package).
## import 3rd party packages
import sys
sys.dont_write_bytecode = True
import numpy as np
from seabreeze.spectrometers import Spectrometer, list_devices
import time
import os
import serial
import cv2
from datetime import datetime
import asyncio
# pickle import to save class data
try:
import cPickle as pickle
except ModuleNotFoundError:
import pickle
import argparse
## import user functions
import utils.APPJPythonFunctions as appj
from utils.experiments import Experiment
sample_num_default = 1 # sample number for treatment
time_treat_default = 30.0 # time to run experiment in seconds
P_treat_default = 2.0 # power setting for the treatment in Watts
q_treat_default = 2.0 # flow setting for the treatment in SLM
dist_treat_default = 5.0 # jet-to-substrate distance in cm
################################################################################
## Set up argument parser
################################################################################
parser = argparse.ArgumentParser(description='Experiment Settings')
parser.add_argument('-n', '--sample_num', type=int, default=sample_num_default,
help='The sample number for the test treatments.')
parser.add_argument('-t', '--time_treat', type=float, default=time_treat_default,
help='The treatment time desired in seconds.')
parser.add_argument('-p', '--P_treat', type=float, default=P_treat_default,
help='The power setting for the treatment in Watts.')
parser.add_argument('-q', '--q_treat', type=float, default=q_treat_default,
help='The flow rate setting for the treatment in SLM.')
parser.add_argument('-d', '--dist_treat', type=float, default=dist_treat_default,
help='The jet-to-substrate distance in centimeters.')
args = parser.parse_args()
sample_num = args.sample_num
time_treat = args.time_treat
P_treat = args.P_treat
q_treat = args.q_treat
dist_treat = args.dist_treat
print(f"The settings for this treatment are:\n"+
f"Sample Number: {sample_num}\n"+
f"Treatment Time (s): {time_treat}\n"+
f"Power (W): {P_treat}\n"+
f"Flow Rate (SLM): {q_treat}\n"+
f"Separation Distance (cm): {dist_treat}\n")
cfm = input("Confirm these are correct: [Y/n]\n")
if cfm in ['Y', 'y']:
pass
else:
quit()
################################################################################
## Startup/prepare APPJ
################################################################################
## collect time stamp
timeStamp = datetime.now().strftime('%Y_%m_%d_%H'+'h%M''m%S'+'s')
print('Timestamp for save files: ', timeStamp)
# configure run options
runOpts = appj.RunOpts()
runOpts.collectData = True
runOpts.collectEntireSpectra = True
runOpts.collectOscMeas = False
runOpts.collectSpatialTemp = False
runOpts.saveSpectra = True
runOpts.saveOscMeas = False
runOpts.saveSpatialTemp = False
runOpts.saveEntireImage = False
runOpts.tSampling = 1.0
Nsim = int(time_treat/runOpts.tSampling)
ts = runOpts.tSampling
## Set startup values
dutyCycleIn = 100
powerIn = 2.0
flowIn = 3.0
Ts0_des = 37.0 # desired initial surface temperature
coolDownDiff = 3 # degrees to substract from desired surface temperature for cooldown
warmUpDiff = 1 # degrees to subtract from desired surface temperature for initialization of experiment
# set save location
directory = os.getcwd()
# os.makedirs(directory+"/ExperimentalData/"+timeStamp, exist_ok=True)
saveDir = directory+"/ExperimentalData/"+timeStamp+"/"
print('\nData will be saved in the following directory:')
print(saveDir)
## connect to/open connection to devices in setup
# Arduino
arduinoAddress = appj.getArduinoAddress(os="ubuntu")
print("Arduino Address: ", arduinoAddress)
arduinoPI = serial.Serial(arduinoAddress, baudrate=38400, timeout=1)
s = time.time()
# Oscilloscope
oscilloscope = appj.Oscilloscope() # Instantiate object from class
instr = oscilloscope.initialize() # Initialize oscilloscope
# Spectrometer
devices = list_devices()
print(devices)
spec = Spectrometer(devices[0])
spec.integration_time_micros(12000*6)
# Thermal Camera
dev, ctx = appj.openThermalCamera()
print("Devices opened/connected to sucessfully!")
devices = {}
devices['arduinoPI'] = arduinoPI
devices['arduinoAddress'] = arduinoAddress
devices['instr'] = instr
devices['spec'] = spec
# send startup inputs
time.sleep(2)
appj.sendInputsArduino(arduinoPI, powerIn, flowIn, dutyCycleIn, arduinoAddress)
input("Ensure plasma has ignited and press Return to begin.\n")
## Startup asynchronous measurement
if os.name == 'nt':
ioloop = asyncio.ProactorEventLoop() # for subprocess' pipes on Windows
asyncio.set_event_loop(ioloop)
else:
ioloop = asyncio.get_event_loop()
# run once to initialize measurements
prevTime = (time.time()-s)*1e3
tasks, runTime = ioloop.run_until_complete(appj.async_measure(arduinoPI, prevTime, instr, spec, runOpts))
print('measurement devices ready!')
s = time.time()
# let APPJ run for a bit
powerIn = 1.5
flowIn = 1.5
appj.sendInputsArduino(arduinoPI, powerIn, flowIn, dutyCycleIn, arduinoAddress)
time.sleep(0.5)
w8 = input("Wait 5 min? [y,n]\n")
if w8 == 'y':
print("Waiting 5 minutes to ensure roughly steady plasma startup...\n")
time.sleep(60)
print("4 minutes left...")
time.sleep(60)
print("3 minutes left...")
time.sleep(60)
print("2 minutes left...")
time.sleep(60)
print("1 minute left...")
time.sleep(60)
else:
time.sleep(5)
# wait for cooldown
appj.sendInputsArduino(arduinoPI, 0.0, 0.0, dutyCycleIn, arduinoAddress)
arduinoPI.close()
while appj.getSurfaceTemperature() > Ts0_des-coolDownDiff:
time.sleep(runOpts.tSampling)
print('cooling down ...')
arduinoPI = serial.Serial(arduinoAddress, baudrate=38400, timeout=1)
time.sleep(2)
appj.sendInputsArduino(arduinoPI, powerIn, flowIn, dutyCycleIn, arduinoAddress)
# wait for surface to reach desired starting temp
while appj.getSurfaceTemperature() < Ts0_des-warmUpDiff:
time.sleep(runOpts.tSampling)
print('warming up ...')
prevTime = (time.time()-s)*1e3
# get initial measurements
tasks, runTime = ioloop.run_until_complete(appj.async_measure(arduinoPI, prevTime, instr, spec, runOpts))
if runOpts.collectData:
thermalCamOut = tasks[0].result()
Ts0 = thermalCamOut[0]
specOut = tasks[1].result()
I0 = specOut[0]*I_NORMALIZATION
oscOut = tasks[2].result()
arduinoOut = tasks[3].result()
outString = "Measured Outputs: Temperature: %.2f, Intensity: %.2f" % (Ts0, I0)
print(outString)
else:
Ts0 = 37
I0 = 100
s = time.time()
arduinoPI.close()
################################################################################
## Begin Experiment:
################################################################################
exp = Experiment(Nsim, saveDir)
for i in range(Nrep):
# connect to Arduino
arduinoPI = serial.Serial(arduinoAddress, baudrate=38400, timeout=1)
s = time.time()
print('Pausing for cooldown...')
time.sleep(2)
if Nrep>1:
Ts0_des = Ts0_vec[i]
while appj.getSurfaceTemperature() > Ts0_des-coolDownDiff:
time.sleep(runOpts.tSampling)
appj.sendInputsArduino(arduinoPI, powerIn, flowIn, dutyCycleIn, arduinoAddress)
devices['arduinoPI'] = arduinoPI
# create input sequences
pseq = P_treat*np.ones((Nsim,))
qseq = q_treat*np.ones((Nsim,))
print(pseq)
print(qseq)
# additional information to save
opt_dict = {}
opt_dict['sep_dist'] = dist_treat
opt_dict['sample_num'] = sample_num
exp_data = exp.run_open_loop(ioloop,
power_seq=pseq,
flow_seq=qseq,
runOpts=runOpts,
devices=devices,
prevTime=prevTime,
opt_dict=opt_dict)
arduinoPI.close()
# reconnect Arduino
arduinoPI = serial.Serial(arduinoAddress, baudrate=38400, timeout=1)
devices['arduinoPI'] = arduinoPI
# turn off plasma jet (programmatically)
appj.sendInputsArduino(arduinoPI, 0.0, 0.0, dutyCycleIn, arduinoAddress)
arduinoPI.close()
print("Experiments complete!\n"+
"################################################################################################################\n"+
"IF FINISHED WITH EXPERIMENTS, PLEASE FOLLOW THE SHUT-OFF PROCEDURE FOR THE APPJ\n"+
"################################################################################################################\n")