-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_emg_rasp.py
executable file
·159 lines (130 loc) · 4.83 KB
/
get_emg_rasp.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
159
#! /usr/bin/env python
import rospy
from std_msgs.msg import Float32MultiArray
import multiprocessing as mp
import struct
from threading import Thread
import matplotlib.animation as animation
import matplotlib.pyplot as plt
import serial
from matplotlib.lines import Line2D
import numpy as np
##############################################################################
#
# Processing Class
# ================
#
# This class plots data it receives from a pipe.
#
exit_flag = False
def terminate():
plt.close('all')
class ProcessPlotter(object):
def __init__(self):
self.x = [0]
self.y = [[0] for i in range(8)]
print(self.y)
def call_back(self):
while self.pipe.poll():
data = self.pipe.recv()
if data is None:
terminate()
return False
else:
if len(data) == 9:
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(2, 1, sharex=True)
self.lines = [Line2D(self.x, self.y[0], color='b', label="A1"),
Line2D(self.x, self.y[1], color='g', label="A2"),
Line2D(self.x, self.y[2], color='r', label="A3"),
Line2D(self.x, self.y[3], color='c', label="A4"),
Line2D(self.x, self.y[4], color='m', label="F1"),
Line2D(self.x, self.y[5], color='y', label="F2"),
Line2D(self.x, self.y[6], color='k', label="F3"),
Line2D(self.x, self.y[7], color='b', label="F4")]
for i in range(2):
self.graphs[i].add_line(self.lines[0 + i * 4])
self.graphs[i].add_line(self.lines[1 + i * 4])
self.graphs[i].add_line(self.lines[2 + i * 4])
self.graphs[i].add_line(self.lines[3 + i * 4])
self.graphs[i].set_ylim(0, 100)
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[0])
# self.graphs[2].add_line(self.lines[1])
# # self.graphs[2].add_line(self.lines[2])
# # self.graphs[2].add_line(self.lines[3])
# self.graphs[2].set_ylim(0, 255)
# 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=1)
timer.add_callback(self.call_back)
timer.start()
ani = animation.FuncAnimation(self.fig, self.animate, interval=1, 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,))
self.plot_process.daemon = True
self.plot_process.start()
def plot(self, data=None):
send = self.plot_pipe.send
if data is not None:
data = [self.count] + list(data)
# print(str(data[3]) + "," + str(data[6]))
send(data)
self.count += 1
def callback(msg):
data_x = [msg.data[0], msg.data[1], msg.data[2], msg.data[3], 0,0,0,0]
# print(data_x)
pl.plot(data_x)
def main():
global pl
pl = NBPlot()
rospy.init_node('plotter_node')
sub = rospy.Subscriber('emg', Float32MultiArray, callback)
rospy.spin()
if __name__ == '__main__':
main()