-
Notifications
You must be signed in to change notification settings - Fork 0
/
PySerialPlot.py
158 lines (131 loc) · 4.66 KB
/
PySerialPlot.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
"""
Serial in: A1 Raw, DC Shift, Power, A2 Raw, DC Shift, Power
Matplotlib in background process
"""
import multiprocessing as mp
import time
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from matplotlib.lines import Line2D
import numpy as np
import random
import datetime
import serial
import struct
###############################################################################
#
# Processing Class
# ================
#
# This class plots data it receives from a pipe.
#
class ProcessPlotter(object):
def __init__(self):
self.x = [0]
self.y = [[0] for i in range(6)]
print(self.y)
def terminate(self):
plt.close('all')
def call_back(self):
while self.pipe.poll():
data = self.pipe.recv()
if data is None:
self.terminate()
return False
else:
if (len(data)==7):
self.x.append(data[0])
self.x = self.x[-1000:]
for i in range(len(self.y)):
self.y[i].append(data[1+i])
self.y[i] = self.y[i][-1000:]
return True
def animate(self, frame):
#self.ax.clear()
#self.ax.plot(self.x,self.y)
#self.fig.canvas.draw()
self.graphs[0].set_xlim(self.x[0],self.x[-1])
self.graphs[0].figure.canvas.draw()
for i in range(len(self.y)):
self.lines[i].set_data(self.x,self.y[i])
#self.bx.set_xlim(self.x[0],self.x[-1])
#self.bx.figure.canvas.draw()
return self.lines
def __call__(self, pipe):
print('starting plotter...')
self.pipe = pipe
self.fig, self.graphs = plt.subplots(3,1,sharex=True)
self.lines = [Line2D(self.x,self.y[0],color='b',label="A1 raw"),
Line2D(self.x,self.y[1],color='g',label="DC Shift"),
Line2D(self.x,self.y[2],color='c',label="A1"),
Line2D(self.x,self.y[3],color='b',label="A2 raw"),
Line2D(self.x,self.y[4],color='g',label="DC Shift"),
Line2D(self.x,self.y[5],color='r',label="A2")]
for i in range(2):
self.graphs[i].add_line(self.lines[0+i*3])
self.graphs[i].add_line(self.lines[1+i*3])
self.graphs[i].set_ylim(0,700)
self.graphs[i].set_title("EMG Value")
self.graphs[i].set_ylabel("Amplitude")
self.graphs[i].set_xlabel("Time")
self.graphs[i].legend()
self.graphs[2].add_line(self.lines[2])
self.graphs[2].add_line(self.lines[5])
self.graphs[2].set_ylim(0,100)
self.graphs[2].legend()
self.graphs[2].set_title("Filtered Output")
self.graphs[2].set_ylabel("Amplitude")
self.graphs[2].set_xlabel("Time")
self.fig.tight_layout()
timer = self.fig.canvas.new_timer(interval=2)
timer.add_callback(self.call_back)
timer.start()
ani = animation.FuncAnimation(self.fig,self.animate, interval=2, blit=True)
print('...done')
plt.show()
###############################################################################
#
# Plotting class
# ==============
#
# This class uses multiprocessing to spawn a process to run code from the
# class above. When initialized, it creates a pipe and an instance of
# ``ProcessPlotter`` which will be run in a separate process.
#
# When run from the command line, the parent process sends data to the spawned
# process which is then plotted via the callback function specified in
# ``ProcessPlotter:__call__``.
#
class NBPlot(object):
def __init__(self):
self.count = 0
self.plot_pipe, plotter_pipe = mp.Pipe()
self.plotter = ProcessPlotter()
self.plot_process = mp.Process(
target=self.plotter, args=(plotter_pipe,), daemon=True)
self.plot_process.start()
def plot(self, data=None):
send = self.plot_pipe.send
if (data!=None):
data=[self.count]+list(data)
print(data)
send(data)
self.count+=1
def main():
#s = serial.Serial(port='/dev/cu.wchusbserial1410',baudrate=115200)
s = serial.Serial(port='/dev/cu.usbmodem14101',baudrate=115200)
pl = NBPlot()
#for ii in range(10):
# data = [ii,random.randint(0,10)]
# pl.plot(data)
# time.sleep(0.2)
while True:
datum = s.read(24)
print(type(datum))
print(datum)
data = struct.unpack("iIfiIf",datum)
pl.plot(data)
if __name__ == '__main__':
if plt.get_backend() == "MacOSX":
mp.set_start_method("forkserver")
main()