-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathreadAndStorePressureData.py
More file actions
38 lines (32 loc) · 887 Bytes
/
readAndStorePressureData.py
File metadata and controls
38 lines (32 loc) · 887 Bytes
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
import serial
import time
import matplotlib.pyplot as plt
import numpy as np
import csv
plotData = False
# open connection to arduino
ser = serial.Serial('/dev/cu.usbserial-AK061JAI', 9600)
ser.flushInput()
# data visualisation
if(plotData):
plot_window = 20
y_var = np.array(np.zeros([plot_window]))
plt.ion()
fig, ax = plt.subplots()
line, = ax.plot(y_var)
# continuesly read serial input and write it to a csv file
with open("pressure_log.csv","a") as hLogfile:
writer = csv.writer(hLogfile,delimiter=",")
while True:
readVal = float(ser.readline())
writer.writerow([time.time(),readVal])
print(str(time.time()) + ": " + str(float(ser.readline())))
hLogfile.flush()
if(plotData):
y_var = np.append(y_var,readVal)
y_var = y_var[1:plot_window+1]
line.set_ydata(y_var)
ax.relim()
ax.autoscale_view()
fig.canvas.draw()
fig.canvas.flush_events()