-
Notifications
You must be signed in to change notification settings - Fork 1
/
python-serial-plot.py
90 lines (76 loc) · 2.82 KB
/
python-serial-plot.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
# University of British Columbia
# Department of Physics & Astronomy
#!/usr/bin/python2.7
import serial # for serial port
import numpy as np # for arrays, numerical processing
from time import sleep,time
import gtk #the gui toolkit we'll use:
# graph plotting library:
from matplotlib.figure import Figure
from matplotlib.backends.backend_gtkagg import FigureCanvasGTKAgg as FigureCanvas
#define the serial port. Pick one:
port = "/dev/ttyACM0" #for Linux
#port = "COM5" #For Windows?
#port = "/dev/tty.uart-XXXX" #For Mac?
outFile = open("distance_vs_time.txt","a")
#function that gets called when a key is pressed:
# press 'm' to make a measurement
def press(event):
print('press', event.key)
if event.key == 'q':
print ('got q!')
quit_app(None)
if event.key == 'm':
outFile.write(str(current_time)+" "+str(current_distance)+"\n") #write to file
return True
def quit_app(event):
outFile.close()
ser.close()
quit()
#start our program proper:
#open the serial port
try:
ser = serial.Serial(port,2400,timeout = 0.050)
ser.baudrate=9600
# with timeout=0, read returns immediately, even if no data
except:
print ("Opening serial port",port,"failed")
print ("Edit program to point to the correct port.")
print ("Hit enter to exit")
raw_input()
quit()
#create a window to put the plot in
win = gtk.Window()
#connect the destroy signal (clicking the x in the corner)
win.connect("destroy", quit_app)
win.set_default_size(800,800)
yvals = np.zeros(50) #array to hold last 50 measurements
times=np.arange(0,50,1.0) # 50 from 0 to 49.
#create a plot:
fig = Figure()
ax = fig.add_subplot(111,xlabel='Time Step',ylabel='Distance [cm]')
ax.set_ylim(2,400) # set limits of y axis.
canvas = FigureCanvas(fig) #put the plot onto a canvas
win.add(canvas) #put the canvas in the window
# define a callback for when a key is pressed
fig.canvas.mpl_connect('key_press_event',press)
#show the window
win.show_all()
win.set_title("ready to receive data");
line, = ax.plot(times,yvals)
start_time = time()
ser.flushInput()
while(1): #loop forever
data = ser.readline() # look for a character from serial port, will wait up to timeout above.
if (len(data) > 0): #was there a byte to read? should always be true.
current_distance = float(data)/10000;
print(current_distance);
yvals = np.roll(yvals,-1) # shift the values in the array
yvals[49] = current_distance # take the value of the byte
current_time = time() - start_time;
line.set_ydata(yvals) # draw the line
fig.canvas.draw() # update the canvas
win.set_title("Distance: "+str(current_distance)+"cm")
while gtk.events_pending(): #makes sure the GUI updates
gtk.main_iteration()
# sleep(.05) # don't eat the cpu. This delay limits the data rate to ~ 200 samples/s