-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSteam.py
184 lines (150 loc) · 7.14 KB
/
Steam.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
import numpy as np
import os
from time import time, sleep
from pylsl import StreamInfo, StreamOutlet
from functools import partial
import pygatt
import subprocess
from sys import platform
from . import helper
from .muse import Muse
from .constants import MUSE_SCAN_TIMEOUT, AUTO_DISCONNECT_DELAY, \
MUSE_NB_EEG_CHANNELS, MUSE_SAMPLING_EEG_RATE, LSL_EEG_CHUNK, \
MUSE_NB_PPG_CHANNELS, MUSE_SAMPLING_PPG_RATE, LSL_PPG_CHUNK, \
MUSE_NB_ACC_CHANNELS, MUSE_SAMPLING_ACC_RATE, LSL_ACC_CHUNK, \
MUSE_NB_GYRO_CHANNELS, MUSE_SAMPLING_GYRO_RATE, LSL_GYRO_CHUNK
# Returns a list of available Muse devices.
def list_muses(backend='auto', interface=None):
backend = helper.resolve_backend(backend)
if backend == 'gatt':
interface = interface or 'hci0'
adapter = pygatt.GATTToolBackend(interface)
elif backend == 'bluemuse':
print('Starting BlueMuse, see BlueMuse window for interactive list of devices.')
subprocess.call('start bluemuse:', shell=True)
return
else:
adapter = pygatt.BGAPIBackend(serial_port=interface)
adapter.start()
print('Searching for Muses, this may take up to 10 seconds...')
devices = adapter.scan(timeout=MUSE_SCAN_TIMEOUT)
adapter.stop()
muses = []
for device in devices:
if device['name'] and 'Muse' in device['name']:
muses = muses + [device]
if(muses):
for muse in muses:
print('Found device %s, MAC Address %s' %
(muse['name'], muse['address']))
else:
print('No Muses found.')
return muses
# Returns the address of the Muse with the name provided, otherwise returns address of first available Muse.
def find_muse(name=None):
muses = list_muses()
if name:
for muse in muses:
if muse['name'] == name:
return muse
elif muses:
return muses[0]
# Begins LSL stream(s) from a Muse with a given address with data sources determined by arguments
def stream(address, backend='auto', interface=None, name=None, ppg_enabled=False, acc_enabled=False, gyro_enabled=False, eeg_disabled=False,):
bluemuse = backend == 'bluemuse'
if not bluemuse:
if not address:
found_muse = find_muse(name)
if not found_muse:
return
else:
address = found_muse['address']
name = found_muse['name']
if not eeg_disabled:
eeg_info = StreamInfo('Muse', 'EEG', MUSE_NB_EEG_CHANNELS, MUSE_SAMPLING_EEG_RATE, 'float32',
'Muse%s' % address)
eeg_info.desc().append_child_value("manufacturer", "Muse")
eeg_channels = eeg_info.desc().append_child("channels")
for c in ['TP9', 'AF7', 'AF8', 'TP10', 'Right AUX']:
eeg_channels.append_child("channel") \
.append_child_value("label", c) \
.append_child_value("unit", "microvolts") \
.append_child_value("type", "EEG")
eeg_outlet = StreamOutlet(eeg_info, LSL_EEG_CHUNK)
if ppg_enabled:
ppg_info = StreamInfo('Muse', 'PPG', MUSE_NB_PPG_CHANNELS, MUSE_SAMPLING_PPG_RATE,
'float32', 'Muse%s' % address)
ppg_info.desc().append_child_value("manufacturer", "Muse")
ppg_channels = ppg_info.desc().append_child("channels")
for c in ['PPG1', 'PPG2', 'PPG3']:
ppg_channels.append_child("channel") \
.append_child_value("label", c) \
.append_child_value("unit", "mmHg") \
.append_child_value("type", "PPG")
ppg_outlet = StreamOutlet(ppg_info, LSL_PPG_CHUNK)
if acc_enabled:
acc_info = StreamInfo('Muse', 'ACC', MUSE_NB_ACC_CHANNELS, MUSE_SAMPLING_ACC_RATE,
'float32', 'Muse%s' % address)
acc_info.desc().append_child_value("manufacturer", "Muse")
acc_channels = acc_info.desc().append_child("channels")
for c in ['X', 'Y', 'Z']:
acc_channels.append_child("channel") \
.append_child_value("label", c) \
.append_child_value("unit", "g") \
.append_child_value("type", "accelerometer")
acc_outlet = StreamOutlet(acc_info, LSL_ACC_CHUNK)
if gyro_enabled:
gyro_info = StreamInfo('Muse', 'GYRO', MUSE_NB_GYRO_CHANNELS, MUSE_SAMPLING_GYRO_RATE,
'float32', 'Muse%s' % address)
gyro_info.desc().append_child_value("manufacturer", "Muse")
gyro_channels = gyro_info.desc().append_child("channels")
for c in ['X', 'Y', 'Z']:
gyro_channels.append_child("channel") \
.append_child_value("label", c) \
.append_child_value("unit", "dps") \
.append_child_value("type", "gyroscope")
gyro_outlet = StreamOutlet(gyro_info, LSL_GYRO_CHUNK)
def push(data, timestamps, outlet):
for ii in range(data.shape[1]):
outlet.push_sample(data[:, ii], timestamps[ii])
push_eeg = partial(push, outlet=eeg_outlet) if not eeg_disabled else None
push_ppg = partial(push, outlet=ppg_outlet) if ppg_enabled else None
push_acc = partial(push, outlet=acc_outlet) if acc_enabled else None
push_gyro = partial(push, outlet=gyro_outlet) if gyro_enabled else None
if all(f is None for f in [push_eeg, push_ppg, push_acc, push_gyro]):
print('Stream initiation failed: At least one data source must be enabled.')
return
muse = Muse(address=address, callback_eeg=push_eeg, callback_ppg=push_ppg, callback_acc=push_acc, callback_gyro=push_gyro,
backend=backend, interface=interface, name=name)
if(bluemuse):
muse.connect()
if not address and not name:
print('Targeting first device BlueMuse discovers...')
else:
print('Targeting device: '
+ ':'.join(filter(None, [name, address])) + '...')
print('\n*BlueMuse will auto connect and stream when the device is found. \n*You can also use the BlueMuse interface to manage your stream(s).')
muse.start()
return
didConnect = muse.connect()
# line 165 was added to create an empty file so muselsl knows when to start/stop streaming (if the file "Stop_EEG1.csv" exists, then we keep streaming)
# lines 176-182 constantly checks to see if the above file exists. If it doesn't, streaming stops and the muse is disconnected
if(didConnect):
print('Connected.')
stop_eeg = [0]
np.savetxt("/Users/mathlab/muse_exp/Experiments/Stop_EEG1.csv", (stop_eeg), delimiter=',',fmt="%s")
muse.start()
eeg_string = " EEG" if not eeg_disabled else ""
ppg_string = " PPG" if ppg_enabled else ""
acc_string = " ACC" if acc_enabled else ""
gyro_string = " GYRO" if gyro_enabled else ""
print("Streaming%s%s%s%s..." %
(eeg_string, ppg_string, acc_string, gyro_string))
while os.path.isfile("/Users/mathlab/muse_exp/Experiments/Stop_EEG1.csv") == True:
try:
sleep(1)
except KeyboardInterrupt:
muse.stop()
muse.disconnect()
break
print('Disconnected.')