-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_logger.py
92 lines (81 loc) · 3.37 KB
/
data_logger.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
import serial
import time
import matplotlib.pyplot as plt
import numpy as np
import sys
# use ggplot style for more sophisticated visuals
plt.style.use('ggplot')
def live_plotter(x_vec,y1_data,y2_data,line1,line2,ylabel="Title",identifier='',pause_time=0.1):
if line1==[]:
# this is the call to matplotlib that allows dynamic plotting
plt.ion()
fig = plt.figure(figsize=(13,6))
ax = fig.add_subplot(111)
# create a variable for the line so we can later update it
line1, = ax.plot(x_vec,y1_data,'-o',alpha=0.4,ms=2,label='voltage')
line2, = ax.plot(x_vec,y2_data,'-o',alpha=0.4,ms=2,label='current')
#update plot label/title
#plt.legend(loc=2, ncol=2)
plt.title('Solar Output: {}'.format(identifier))
ax.legend()
plt.show()
# after the figure, axis, and line are created, we only need to update the y-data
line1.set_ydata(y1_data)
line2.set_ydata(y2_data)
# adjust limits if new data goes beyond bounds
if np.min([y1_data,y2_data])<=line1.axes.get_ylim()[0] or np.max([y1_data,y2_data])>=line1.axes.get_ylim()[1]:
plt.ylim([np.min([y1_data,y2_data])-np.std([y1_data,y2_data]),np.max([y1_data,y2_data])+np.std([y1_data,y2_data])])
# if np.min(y2_data)<=line2.axes.get_ylim()[0] or np.max(y2_data)>=line2.axes.get_ylim()[1]:
# plt.ylim([np.min(y2_data)-np.std(y2_data),np.max(y2_data)+np.std(y2_data)])
# this pauses the data so the figure/axis can catch up - the amount of pause can be altered above
plt.pause(pause_time)
# return line so we can update it again in the next iteration
return [line1,line2]
ser = serial.Serial(
port='COM3',\
baudrate=9600,\
parity=serial.PARITY_NONE,\
stopbits=serial.STOPBITS_ONE,\
bytesize=serial.EIGHTBITS,\
timeout=0)
print("connected to: " + ser.portstr)
# plt.ion() ## Note this correction
# fig=plt.figure()
# plt.axis([0,1000,0,1])
#this will store the line
seq = []
size = 100
x_vec = np.linspace(0,1,size+1)[0:-1]
y_vec = np.ones(len(x_vec))
z_vec = np.ones(len(x_vec))
line1 = []
line2 = []
vv = 5.0/1023*11989.0/1979
aa = 5.0/1023*10
if __name__ == '__main__':
t_end = time.time() + 60 * 5 #run for 5 minutes
try:
while time.time() < t_end:
for c in ser.read():
seq.append(chr(c)) #convert from ANSII
joined_seq = ''.join(str(v) for v in seq) #Make a string from array
if chr(c) == '\n':
#print(joined_seq)
readings = joined_seq.split(' ', 2)
# plt.scatter(i, readings[0])
# i+=1
seq = []
y_vec[-1] = vv*float(readings[0])
z_vec[-1] = aa*(float(readings[1][0:-2])-437)
#print(float(readings[1][0:-2]))
[line1,line2] = live_plotter(x_vec,y_vec,z_vec,line1,line2)
y_vec = np.append(y_vec[1:],0.0)
z_vec = np.append(z_vec[1:],0.0)
break
# plt.show()
# plt.pause(0.0001)
except KeyboardInterrupt:
print('Interrupted')
ser.flushInput()
np.savetxt("data_dump.csv", np.asarray([y_vec, z_vec]), delimiter=",")
sys.exit(0)