forked from NiklasRosenstein/myo-python
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhello_myo.py
182 lines (149 loc) · 5.38 KB
/
hello_myo.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
# Copyright (c) 2015 Niklas Rosenstein
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
from __future__ import print_function
import myo as libmyo; libmyo.init()
import time
import sys
class Listener(libmyo.DeviceListener):
"""
Listener implementation. Return False from any function to
stop the Hub.
"""
interval = 0.05 # Output only 0.05 seconds
def __init__(self):
super(Listener, self).__init__()
self.orientation = None
self.pose = libmyo.Pose.rest
self.emg_enabled = False
self.locked = False
self.rssi = None
self.emg = None
self.last_time = 0
def output(self):
ctime = time.time()
if (ctime - self.last_time) < self.interval:
return
self.last_time = ctime
parts = []
if self.orientation:
for comp in self.orientation:
parts.append(str(comp).ljust(15))
parts.append(str(self.pose).ljust(10))
parts.append('E' if self.emg_enabled else ' ')
parts.append('L' if self.locked else ' ')
parts.append(self.rssi or 'NORSSI')
if self.emg:
for comp in self.emg:
parts.append(str(comp).ljust(5))
print('\r' + ''.join('[{0}]'.format(p) for p in parts), end='')
sys.stdout.flush()
def on_connect(self, myo, timestamp, firmware_version):
myo.vibrate('short')
myo.vibrate('short')
myo.request_rssi()
myo.request_battery_level()
def on_rssi(self, myo, timestamp, rssi):
self.rssi = rssi
self.output()
def on_pose(self, myo, timestamp, pose):
if pose == libmyo.Pose.double_tap:
myo.set_stream_emg(libmyo.StreamEmg.enabled)
self.emg_enabled = True
elif pose == libmyo.Pose.fingers_spread:
myo.set_stream_emg(libmyo.StreamEmg.disabled)
self.emg_enabled = False
self.emg = None
self.pose = pose
self.output()
def on_orientation_data(self, myo, timestamp, orientation):
self.orientation = orientation
self.output()
def on_accelerometor_data(self, myo, timestamp, acceleration):
pass
def on_gyroscope_data(self, myo, timestamp, gyroscope):
pass
def on_emg_data(self, myo, timestamp, emg):
self.emg = emg
self.output()
def on_unlock(self, myo, timestamp):
self.locked = False
self.output()
def on_lock(self, myo, timestamp):
self.locked = True
self.output()
def on_event(self, kind, event):
"""
Called before any of the event callbacks.
"""
def on_event_finished(self, kind, event):
"""
Called after the respective event callbacks have been
invoked. This method is *always* triggered, even if one of
the callbacks requested the stop of the Hub.
"""
def on_pair(self, myo, timestamp, firmware_version):
"""
Called when a Myo armband is paired.
"""
def on_unpair(self, myo, timestamp):
"""
Called when a Myo armband is unpaired.
"""
def on_disconnect(self, myo, timestamp):
"""
Called when a Myo is disconnected.
"""
def on_arm_sync(self, myo, timestamp, arm, x_direction, rotation,
warmup_state):
"""
Called when a Myo armband and an arm is synced.
"""
def on_arm_unsync(self, myo, timestamp):
"""
Called when a Myo armband and an arm is unsynced.
"""
def on_battery_level_received(self, myo, timestamp, level):
"""
Called when the requested battery level received.
"""
def on_warmup_completed(self, myo, timestamp, warmup_result):
"""
Called when the warmup completed.
"""
def main():
print("Connecting to Myo ... Use CTRL^C to exit.")
try:
hub = libmyo.Hub()
except MemoryError:
print("Myo Hub could not be created. Make sure Myo Connect is running.")
return
hub.set_locking_policy(libmyo.LockingPolicy.none)
hub.run(1000, Listener())
# Listen to keyboard interrupts and stop the hub in that case.
try:
while hub.running:
time.sleep(0.25)
except KeyboardInterrupt:
print("\nQuitting ...")
finally:
print("Shutting down hub...")
hub.shutdown()
if __name__ == '__main__':
main()