-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTest.py
More file actions
124 lines (109 loc) · 4.26 KB
/
Test.py
File metadata and controls
124 lines (109 loc) · 4.26 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
#import pyfirmata
import serial
import numpy as np
import matplotlib.pyplot as plt
from drawnow import *
from serial import Serial
from scipy.interpolate import interp1d
import pandas as pd
import datetime
class MaxIntensity:
intensity = 0.0
wavelength = 0.0
time = 0
def __init__(self,intensity, wavelength, time):
self.intensity = intensity
self.wavelength = wavelength
self.time = time
spectreData = serial.Serial("COM23", 115200)
loop=True
allReadings = []
spectreReadings = [0.0,0.0,0.0,0.0,0.0,0.0]
x = np.linspace(450.00, 650.00, num=6, endpoint=True)
defaultYLimit = 1024
defaultWavelengthLimit = 750.00
xnew = np.linspace(450.00, 650.00, num=6, endpoint=True)
fig = plt.figure()
plt.ion()
def getMaximumIntensity(currentReadings):
maxIntensityIndex = -1
for num, value in enumerate(currentReadings,start=1):
if (maxIntensityIndex == -1 or value > currentReadings[maxIntensityIndex]):
maxIntensityIndex = num
return maxIntensityIndex
def handle_close(evt):
spectreData.reset_input_buffer()
plt.ioff()
plt.close()
global loop
loop=False
def plot1():
fig.canvas.mpl_connect('close_event', handle_close)
ax1 = fig.add_subplot(311, )
ax1.set_ylim(0,defaultYLimit)
ax1.set_title('Spectral Response')
ax1.grid(True)
ax1.set_ylabel('Intensity Count')
ax1.set_xlabel('Wavelength (in nm)')
ax1.legend(loc='upper left')
ax1.plot(x, spectreReadings, 'o', xnew, f(xnew), '-')
intensities = list(map(lambda x: x.intensity, maxIntensities))
times =list(map(lambda x: x.time, maxIntensities))
wavelengths = list(map(lambda x: x.wavelength, maxIntensities))
ax2 = fig.add_subplot(312, autoscaley_on = True)
ax2.set_title('Max Intensity vs Time')
ax2.set_ylim(0,defaultYLimit)
ax2.grid(True)
ax2.set_ylabel('Maximum Intensity')
ax2.set_xlabel('Time (in seconds)')
ax2.plot(times, intensities, 'g')
plt.tight_layout()
ax3 = fig.add_subplot(313)
ax3.set_title('Max Wavelength vs Time')
ax3.set_ylim(0,defaultWavelengthLimit)
ax3.grid(True)
ax3.set_ylabel('Maximum Wavelength (in nm)')
ax3.set_xlabel('Time (in seconds)')
ax3.plot(times, wavelengths, 'b')
plt.tight_layout()
times = []
maxIntensities = []
startTime = datetime.datetime.now()
while (loop):
while (spectreData.inWaiting()== 0):
pass
spectreString = spectreData.readline().decode('utf8')
spectreList = spectreString.split(",")
if(len(spectreList)==6):
readingArrivalTime = datetime.datetime.now()
for num, value in enumerate(spectreList,start=0):
spectreReadings[num]=float(value)
maxIntensityIndex = getMaximumIntensity(spectreReadings)
if(maxIntensityIndex != -1 and maxIntensityIndex < len(spectreReadings)):
print("Maximum intensity is " + str(spectreReadings[maxIntensityIndex]))
allReadings.append(spectreReadings[:])
time = (readingArrivalTime - startTime).total_seconds()
intensity = MaxIntensity(spectreReadings[maxIntensityIndex], x[maxIntensityIndex], time)
maxIntensities.append(intensity)
times.append(time)
else:
print("Maximum intensity anamoly detected ")
defaultYLimit=max(spectreReadings)*1.3
f=interp1d(x,spectreReadings,kind='cubic')
drawnow(plot1)
wavelength450 = list(map(lambda x: x[0], allReadings))
wavelength490 = list(map(lambda x: x[1], allReadings))
wavelength530 = list(map(lambda x: x[2], allReadings))
wavelength570 = list(map(lambda x: x[3], allReadings))
wavelength610 = list(map(lambda x: x[4], allReadings))
wavelength650 = list(map(lambda x: x[5], allReadings))
data = {'450':wavelength450,
'490':wavelength490,
'530':wavelength530,
'570':wavelength570,
'610':wavelength610,
'650':wavelength650,
'Time':times}
df = pd.DataFrame(data, columns= ['450', '490', '530', '570', '610', '650', 'Time'])
export_csv = df.to_csv (r'C:\Users\debacle\Documents\Plot\DAQ\export_dataframe.csv', index = True, header=True)
print(".")