forked from dzhu/myo-raw
-
Notifications
You must be signed in to change notification settings - Fork 4
/
myo_raw_osc_gui.py
205 lines (153 loc) · 5.04 KB
/
myo_raw_osc_gui.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#adapted from:
#https://github.com/ptone/pyosc/blob/master/examples/knect-rcv.py
#!/usr/bin/env python3
from OSC import OSCServer
import sys
from time import sleep
from pyqtgraph.Qt import QtGui, QtCore
import pyqtgraph as pg
import getopt
import time
import numpy as np
######################################################################
######################################################################
# default settings
ip = "localhost"
port = 7110
try:
opts, args = getopt.getopt(sys.argv[1:],"hi:p:",
["ip=","port="])
except getopt.GetoptError:
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print('\n')
print ('myo_raw_osc_gui.py: plot raw myo data from OSC ')
print('Usage: -i <ip address> -p <ip port> ')
print('-i --ip: server ip address. Default to "localhost"')
print('-p --port: server ip port. Default to 7110')
print('\n')
sys.exit()
elif opt in ("-i", "--ip"):
ip = arg
if ip == "0":
ip="127.0.0.1"
elif opt in ("-p","--port"):
port = int(arg)
server = OSCServer( (ip,port) )
server.timeout = 0
run = True
# this method of reporting timeouts only works by convention
# that before calling handle_request() field .timed_out is
# set to False
def handle_timeout(self):
self.timed_out = True
# funny python's way to add a method to an instance of a class
import types
server.handle_timeout = types.MethodType(handle_timeout, server)
def user_callback_imu(path, tags, args, source):
global orientation, acceleration, newImuData
orientation = args[:3]
acceleration = args[3:]
newImuData = [orientation, acceleration]
update('imu')
def user_callback_emg(path, tags, args, source):
global newEmgData
newEmgData = args
update('emg')
def quit_callback(path, tags, args, source):
# don't do this at home (or it'll quit blender)
global run
run = False
server.addMsgHandler( "/myo/imu", user_callback_imu )
server.addMsgHandler( "/myo/emg", user_callback_emg )
#server.addMsgHandler( "/quit", quit_callback )
# user script that's called by the game engine every frame
def each_frame():
print("each_frame")
# clear timed_out flag
server.timed_out = False
# handle all pending requests then return
while not server.timed_out:
server.handle_request()
##############################################################################
##############################################################################
##############################################################################
app = QtGui.QApplication([])
win = pg.GraphicsWindow()
win.resize(1000,600)
win.setWindowTitle('Myo Raw Osc GUI')
nEmg = 8
orientationPlot = win.addPlot(row=1,col=1,title="ORIENTATION")
accelerationPlot = win.addPlot(row=2,col=1,title="ACCELERATION")
emgPlot = win.addPlot(row=3,col=1,title="EMG")
plots=[]
plots.append(orientationPlot)
plots.append(accelerationPlot)
plots.append(emgPlot)
orientationPlot.setYRange(-np.pi,np.pi, padding=0)
accelerationPlot.setYRange(-2000,2000, padding=0)
emgPlot.setYRange(0,1000, padding=0)
for p in plots:
p.hideButtons()
p.showGrid(x=True,y=True,alpha=0.5)
p.addLegend();
p.setMouseEnabled(x=False,y=False)
orientationCurves = []
accelerationCurves = []
orientationNames = ['azimuth','elevation','roll']
accelerationNames = ['x','y','z']
for i in np.arange(3):
orientationCurves.append(orientationPlot.plot(pen=pg.intColor(i),name=orientationNames[i]))
accelerationCurves.append(accelerationPlot.plot(pen=pg.intColor(i),name=accelerationNames[i]))
emgCurves = []
for i in np.arange(nEmg):
emgCurves.append(emgPlot.plot(pen=pg.intColor(i),name=i))
curves = []
curves.append(orientationCurves)
curves.append(accelerationCurves)
curves.append(emgCurves)
# values read from handler event
orientation = []
acceleration = []
newImuData = []
newEmgData = []
# historic trace of values
maxHist = 200
orientationHist = []
accelerationHist = []
emgHist = []
for i in np.arange(3):
orientationHist.append(np.zeros(maxHist))
accelerationHist.append(np.zeros(maxHist))
for i in np.arange(nEmg):
emgHist.append(np.zeros(maxHist))
hist = [orientationHist, accelerationHist,emgHist]
indx = 0
def update(who):
global hist, newImuData, newEmgData
global curves, emgCurves, emgHist
# imu
if (who == 'imu'):
for ii in np.arange(2):
h = hist[ii]
c = curves[ii]
for i in np.arange(3):
h[i][:-1] = h[i][1:]
h[i][-1] = newImuData[ii][i]
c[i].setData(h[i])
if (who == 'emg'):
h = emgHist
c = emgCurves
for i in np.arange(nEmg):
h[i][:-1] = h[i][1:]
h[i][-1] = newEmgData[i]
c[i].setData(h[i])
app.processEvents()
while run:
server.handle_request()
if __name__ == '__main__':
import sys
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_'):
QtGui.QApplication.instance().exec_()
server.close()