-
Notifications
You must be signed in to change notification settings - Fork 0
/
ACSv3.py
298 lines (187 loc) · 6.83 KB
/
ACSv3.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
#cd /Users/zakwatts/Coding/MImiZakLocal/2ndTerm/040222/TestPALED
#source venv/bin/activate
#This is the python code to be used with the Adruino code to operate an acoustic deformation chamber
#For notes and use please vist - GITHUB "all rights reserved"
#By Zak Watts and Mimi Houlihan
#Import required plugins
#XXXX If you have the package serial installed, pip unistall it, then pip install pyserial to avoid errors
import time
import os
import sys
import serial
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
#Establish serial connection
serialport = serial.Serial('COM4', baudrate=9600, timeout=2)
#start of the program
def main():
print()
print("************Welcome to the Acoustic Chamber Software (ACS)**************")
print()
menu()
def menu():
print("Please select how you would like to operate the device (type letter)")
choice = input("""
A: Manually frequency selection
B: Auto find anti-resonance frequency selection (No DATA EXPORT)
C: The above with CSV + Graph
D: Time repeat of B
Q: Quit
Please enter your choice: """)
if choice == "A" or choice =="a":
manualfreq()
elif choice == "B" or choice =="b":
autofreq()
elif choice == "C" or choice =="c":
autofreqEXPORT()
elif choice == "D" or choice =="d":
repeatautofreq()
elif choice=="Q" or choice=="q":
sys.exit
else:
print("You must only select either A or B")
print("Please try again")
menu()
def manualfreq():
print("You have selected to manually enter a frequency")
manualfreqchoice = input("Input the frequency you would like to drive the piezo at (in Hz): ")
print("You have chosen to drive the peizo at ", manualfreqchoice," Hz")
sendfreq(manualfreqchoice)
def autofreq():
print("You have selected to automatically find the anti-resonance frequency")
array_split = freqscan()
values = dataprocessing(array_split)
FREQ = values[0]
V_DIFFERENCE = values[1]
Phase = values[2]
driving_freq = values[3]
sendfreq(driving_freq)
def autofreqEXPORT():
print("You have selected to automically find the anti-resoance frew and export and csv and graph")
array_split = freqscan()
values = dataprocessing(array_split)
FREQ = values[0]
V_DIFFERENCE = values[1]
Phase = values[2]
driving_freq = values[3]
savedata(array_split)
makegraph(FREQ, V_DIFFERENCE, Phase)
sendfreq(driving_freq)
def repeatautofreq():
print("You have selected to repeatedly find the anti-resonance frequency")
#Code to calcuate the driving frequency once data has been recieved
def dataprocessing(array_split):
FREQ = []
VREF = []
VM = []
VP = []
COM = []
length = len(array_split)
print('Processing data')
for i in range(1, length-1):
FREQ.append(float(array_split[i][0]))
VREF.append(float(array_split[i][1]))
VM.append(float(array_split[i][2]))
VP.append(float(array_split[i][3]))
COM.append(float(array_split[i][4]))
V_DIFFERENCE = []
Phase = []
for i in range(0,length-2):
V_DIFFERENCE.append(np.abs(VM[i]-VREF[i]))
Phase.append(VP[i])
#print(Phase)
#print(FREQ)
#print(V_DIFFERENCE)
phase_peak = max(Phase)
phase_peak_index = Phase.index(phase_peak)
driving_freq = FREQ[phase_peak_index]
print('Values:')
print('Phase peak =', phase_peak)
print('Corresponding driving frequency =', driving_freq, 'Hz')
return FREQ, V_DIFFERENCE, Phase, driving_freq
#The code to find anti-resonant freq
def freqscan():
#send the byte "F" to tell the arduino to scan the freqs and send back the data
serialport.write(b'F')
array = []
array_split = []
open = True
while open:
arduinodata = serialport.readline().decode('ascii').strip()
array = arduinodata.split(',')
array_split.append(array)
if not arduinodata or arduinodata == 'exit':
open = False
return array_split
#The code to send the desired frequency back to the arduino
def sendfreq(driving_freq):
#Establish serial connection
#serialport = serial.Serial('COM4', baudrate=9600, timeout=2)
#Send the byte "S" to tell the arudino to listen to what to what to drive the frequncy at
serialport.write(b'S')
j = str(driving_freq)
serialport.write(j.encode())
#listen to hear for a confirmation that the frequency has been sent
print(serialport.readline().decode('ascii'))
time.sleep(0.5)
#listen to hear for a confirmation that the frequency has been sent
print(serialport.readline().decode('ascii'))
def savedata(array_split):
np.savetxt('pythonpiezodataarray.csv', array_split, delimiter=',', fmt='%s')
#Be sure to close down the graph to allow the code ot continue
def makegraph(FREQ, V_DIFFERENCE, Phase):
print('Creating graph')
plt.rcParams["font.family"] = "Times New Roman"
fig, ax1 = plt.subplots()
ax1.set_xlabel('Frequency (Hz)')
ax1.set_ylabel('Impedance (Ohms)', color='red')
ax1.plot(FREQ, V_DIFFERENCE, color='red')
ax1.tick_params(axis='y')
ax2 = ax1.twinx()
ax2.set_ylabel('Phase (Degrees)', color='blue')
ax2.plot(FREQ, Phase, color='blue')
ax2.tick_params(axis = 'y')
fig.tight_layout()
plt.show()
def programme():
array_split = freqscan()
values = dataprocessing(array_split)
FREQ = values[0]
V_DIFFERENCE = values[1]
Phase = values[2]
driving_freq = values[3]
makegraph(FREQ, V_DIFFERENCE, Phase)
sendfreq(driving_freq)
def opmenu():
print("The code is currently running")
def endmenu():
print("Thanks for using the Acoustic Chamber Software")
endchoice = input("""
A: Go back to the start menu
Q: Quit
Please enter your choice: """)
if endchoice== "A" or endchoice== "a":
menu()
elif endchoice== "Q" or endchoice== 'q':
quit()
#This is where the programme is initiated
main()
"""
savedatachoice = input("Do you wish to save this data (Y/N)")
if savedatachoice =="Y" or savedatachoice=="y":
savedata(array_split)
elif savedatachoice =="N" or savedatachoice=="n":
return
else:
print("You must only select either Y or N")
print("Please try again")
makegraphchoice = input("Do you wish to have an impedance vs frequency graph?")
if makegraphchoice =="Y" or makegraphchoice =="y":
makegraph(FREQ, V_DIFFERENCE, Phase)
elif makegraphchoice =="N" or makegraphchoice=="n":
return
else:
print("You must only select either Y or N")
print("Please try again")
"""