-
Notifications
You must be signed in to change notification settings - Fork 0
/
testInput.py
67 lines (52 loc) · 1.61 KB
/
testInput.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
from __future__ import print_function
from matplotlib import pyplot as plt
from myo.utils import TimeInterval
import collections
import myo
import numpy as np
import threading
import time
import os
import keyboard
class Listener(myo.DeviceListener):
def __init__(self, queue_size=10):
self.lock = threading.Lock()
self.emg_data_queue = collections.deque(maxlen=queue_size)
def on_connected(self, event):
event.device.stream_emg(True)
def on_emg(self, event):
with self.lock:
self.emg_data_queue.append((event.timestamp, event.emg))
def on_orientation(self, event):
with self.lock:
self.orientation = event.orientation
self.acceleration = event.acceleration
#self.emg_data_queue.append((event.timestamp, event.gyroscope))
def get_emg_data(self):
with self.lock:
return list(self.emg_data_queue)
def main():
queue_size = 10
# Initialize Myo and create a Hub and our listener.
myo.init()
hub = myo.Hub()
listener = Listener(queue_size)
def update():
emgs = np.array([x[1] for x in listener.get_emg_data()]).T
for x in emgs:
data = x
if len(data) < queue_size:
data = np.concatenate([np.zeros(queue_size - len(data)), data])
word = 'Mean = %d , vector = ' %(data.mean())
print(word + str(data))
try:
threading.Thread(target=lambda: hub.run_forever(listener.on_event)).start()
time = TimeInterval(None, 0.1)
while True:
if time.check_and_reset():
os.system('cls')
update()
finally:
hub.stop() # Will also stop the thread
if __name__ == '__main__':
main()