1
1
import multiprocessing
2
+ import queue
3
+ import numpy as np
2
4
import mpl_toolkits .mplot3d as plt3d
3
5
from mpl_toolkits .mplot3d import Axes3D
4
6
import matplotlib .pyplot as plt
5
7
from matplotlib import animation
6
- import numpy as np
8
+ from matplotlib . cm import get_cmap
7
9
8
10
from myo_serial import MyoRaw
9
11
10
- print ("EPILEPSY WARNING" )
11
- print ("The plot updates fast, press ctrl+c to stop" )
12
+ print ("Press ctrl+pause/break to stop" )
12
13
13
14
# ------------ Myo Setup ---------------
14
15
q = multiprocessing .Queue ()
@@ -34,41 +35,66 @@ def print_battery(bat):
34
35
while True :
35
36
try :
36
37
m .run ()
37
- except KeyboardInterrupt :
38
+ except :
38
39
print ("Worker Stopped" )
39
40
quit ()
40
41
41
-
42
42
# ------------ Plot Setup ---------------
43
- fig = plt .figure ()
44
- axm = fig .add_subplot ()
45
-
46
- myox = [2048 ,128 ,128 ,128 ,128 ,128 ,128 ,128 ]
47
- rects = axm .bar (list (range (1 ,9 )), myox )
43
+ QUEUE_SIZE = 100
44
+ SENSORS = 8
45
+ subplots = []
46
+ lines = []
47
+ # Set the size of the plot
48
+ plt .rcParams ["figure.figsize" ] = (4 ,8 )
49
+ # using the variable axs for multiple Axes
50
+ fig , subplots = plt .subplots (SENSORS , 1 )
51
+ fig .tight_layout ()
52
+ # Set each line to a different color
53
+
54
+ name = "tab10" # Change this if you have sensors > 10
55
+ cmap = get_cmap (name ) # type: matplotlib.colors.ListedColormap
56
+ colors = cmap .colors # type: list
57
+
58
+ for i in range (0 ,SENSORS ):
59
+ ch_line , = subplots [i ].plot (range (QUEUE_SIZE ),[0 ]* (QUEUE_SIZE ), color = colors [i ])
60
+ lines .append (ch_line )
61
+
62
+ emg_queue = queue .Queue (QUEUE_SIZE )
48
63
49
64
def animate (i ):
50
- myox = [128 ,128 ,128 ,128 ,128 ,128 ,128 ,128 ]
51
-
52
65
# Myo Plot
53
66
while not (q .empty ()):
54
67
myox = list (q .get ())
55
- print (myox )
56
-
57
- for rect , yi in zip (rects , myox ):
58
- rect .set_height (yi )
68
+ if (emg_queue .full ()):
69
+ emg_queue .get ()
70
+ emg_queue .put (myox )
59
71
60
- return rects
72
+ channels = np . array ( emg_queue . queue )
61
73
62
- def main ():
63
- anim = animation .FuncAnimation (fig , animate , blit = False , interval = 2 )
64
- try :
65
- plt .show ()
66
- except KeyboardInterrupt :
67
- quit ()
74
+ if (emg_queue .full ()):
75
+ for i in range (0 ,SENSORS ):
76
+ channel = channels [:,i ]
77
+ lines [i ].set_ydata (channel )
78
+ subplots [i ].set_ylim (0 ,max (1024 ,max (channel )))
68
79
69
80
if __name__ == '__main__' :
70
81
# Start Myo Process
71
82
p = multiprocessing .Process (target = worker , args = (q ,))
72
83
p .start ()
73
84
74
- main ()
85
+ while (q .empty ()):
86
+ # Wait until we actually get data
87
+ continue
88
+ anim = animation .FuncAnimation (fig , animate , blit = False , interval = 2 )
89
+ def on_close (event ):
90
+ p .terminate ()
91
+ raise KeyboardInterrupt
92
+ print ("On close has ran" )
93
+ fig .canvas .mpl_connect ('close_event' , on_close )
94
+
95
+ try :
96
+ plt .show ()
97
+ except KeyboardInterrupt :
98
+ plt .close ()
99
+ p .close ()
100
+ quit ()
0 commit comments