forked from mamo91/Dongleless-myo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dongleless-emg.py
56 lines (48 loc) · 1.43 KB
/
dongleless-emg.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
import datetime
import dongleless
import functools
import sys
from bluepy import btle
mac = sys.argv[1]
out_file = sys.argv[2]
def write_emg(f, myo, emg):
f.write('E ')
f.write(datetime.datetime.now().time().isoformat())
f.write(' ')
f.write(' '.join(map(str,emg)))
f.write('\n')
def write_imu(f, myo, quat, accel, gyro):
f.write('I ')
f.write(datetime.datetime.now().time().isoformat())
f.write(' ')
f.write(' '.join(map(str,quat)))
f.write(' ')
f.write(' '.join(map(str,accel)))
f.write(' ')
f.write(' '.join(map(str,gyro)))
f.write('\n')
out_fd = open(out_file, 'w')
function_dict = {
"emg_data": functools.partial(write_emg, out_fd),
"imu_data": functools.partial(write_imu, out_fd),
}
while True:
while True:
try:
p = dongleless.Connection(mac)
except btle.BTLEException:
pass
else:
if p:
break
try:
p.setMode(1,1,0) # Enable EMG and IMU data, disable pose classifier
p.setSleep(1) # Disable sleep
p.subscribe() # Subscribe to notifications
p.setDelegate(dongleless.MyoDelegate(function_dict, p)) # Assign callbacks
p.setLeds(0x00,0x00,0x00,0x00,0x00,0x00) # Dim LEDs
while True:
p.vibrate(0) # Doing this in a loop seems to somehow inhibit automatic Myo vibration
p.waitForNotifications(3)
except btle.BTLEException:
pass