-
Notifications
You must be signed in to change notification settings - Fork 1
/
session_start.py
236 lines (213 loc) · 9.52 KB
/
session_start.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import os, time, datetime, sys, re, pty, serial
import sqlite3, json
from contextlib import closing
import pyOpenBCI
# config
working_dir = '/path/to/openbci-session'
working_dir = '/Volumes/Data/Storage/Dev/openbci-psg'
# for simplicity channels should be attached from
# sequentially, starting from the first and without skipping
# so for 3 channels setup always use channels 1, 2, 3
# otherwise you have to modify script, its not designed for free channels selection
sleep_channels = {
'F8-T5':0, 'F7-T5':1, 'O2-T5':2, 'O1-T5':3,
'T8-T5':4, 'T7-T5':5, 'AFz-T5':6, 'T6-T5':7}
sleep_ext_channels = {
'F8-T5':0, 'F7-T5':1, 'O2-T5':2, 'O1-T5':3,
'T8-T5':4, 'T7-T5':5, 'AFz-T5':6, 'T6-T5':7,
'Cz': 8}
cyton_cap_channels = {
'F4-M1':0, 'F3-M1':1, 'O2-M1':2, 'O1-M1':3,
'T8-M1':4, 'T7-M1':5, 'Cz-M1':6, 'M2-M1':7}
daisy_cap_channels = {
'O2-M1':0, 'O1-M1':1, 'P4-M1':2, 'P3-M1':3,
'C4-M1':4, 'C3-M1':5, 'Cz-M1':6, 'Pz-M1':7,
'F4-M1':8, 'F3-M1':9, 'F8-M1':10, 'F7-M1':11,
'T8-M1':12, 'T7-M1':13, 'Fz-M1':14, 'M2-M1':15}
electrode_choice = {'1':'Gold Cup OpenBCI, Ten20',
'2': 'Premium Ag/AgCl FRI, Sigma Gel',
'3':'Premium Ag/AgCl FRI',
'4':'Ag/AgCl FRI disposable, Sigma Gel',
'5':'Gold Cup Grass, Ten20',
'6':'Gold Cup OpenBCI, Sigma Gel',
'7': 'Ambu Neuroline Cup, Sigma Gel'}
activity_choice = {
'0': {'type': 'sleep', 'dur': '12H', 'sf': 500, 'g': 'Fp2', 'gain': 24,
'e': electrode_choice['1'], 'ch': sleep_ext_channels, 'dev': 'daisy'},
'1': {'type': 'sleep', 'dur': '12H', 'sf': 500, 'g': 'Fp2', 'gain': 24,
'e': electrode_choice['1'], 'ch': sleep_channels, 'dev': 'daisy'},
'2': {'type': 'nsdr', 'dur': '1H', 'sf': 1000, 'g': 'AFz', 'gain': 24,
'e': electrode_choice['4'], 'ch': daisy_cap_channels, 'dev': 'daisy'},
'3': {'type': 'swaroopa isha', 'dur': '1H', 'sf': 1000, 'g': 'AFz', 'gain': 24,
'e': electrode_choice['1'], 'ch': daisy_cap_channels, 'dev': 'daisy'},
'4': {'type': 'meditation', 'dur': '1H', 'sf': 1000, 'g': 'AFz', 'gain': 24,
'e': electrode_choice['6'], 'ch': daisy_cap_channels, 'dev': 'daisy'},
'5': {'type': 'rest', 'dur': '1H', 'sf': 1000, 'g': 'AFz', 'gain': 24,
'e': electrode_choice['1'], 'ch': daisy_cap_channels, 'dev': 'daisy'},
'6': {'type': 'rest-eyeopen', 'dur': '1H', 'sf': 1000, 'g': 'AFz', 'gain': 24,
'e': electrode_choice['1'], 'ch': daisy_cap_channels, 'dev': 'daisy'},
'7': {'type': 'dantian breath', 'dur': '1H', 'sf': 1000, 'g': 'AFz', 'gain': 24,
'e': electrode_choice['1'], 'ch': daisy_cap_channels, 'dev': 'daisy'},
'8': {'type': 'meditation', 'dur': '1H', 'sf': 1000, 'g': 'Fp2', 'gain': 24,
'e': electrode_choice['1'], 'ch': sleep_channels, 'dev': 'cyton'},
'9': {'type': 'unknown', 'dur': '1H', 'sf': 500, 'g': 'Fp2', 'gain': 24,
'e': electrode_choice['1'], 'ch': sleep_ext_channels, 'dev': 'daisy'},
'10': {'type': 'dantian breath', 'dur': '1H', 'sf': 1000, 'g': 'AFz', 'gain': 24,
'e': electrode_choice['1'], 'ch': daisy_cap_channels, 'dev': 'daisy'},
}
note = '5m rest before and after, seated'
note = ''
activity_chosen = '1';
activity = activity_choice[activity_chosen]['type'];
device = activity_choice[activity_chosen]['dev']
ch_n = 8 if device == 'cyton' else 16
channels = activity_choice[activity_chosen]['ch'];
electrode_type = activity_choice[activity_chosen]['e'];
duration = activity_choice[activity_chosen]['dur'];
ground = activity_choice[activity_chosen]['g'];
sampling_rate = activity_choice[activity_chosen]['sf'];
gain = activity_choice[activity_chosen]['gain'];
# emg_channels = {'E2-Fpz':5, 'E1-Fpz':6}
# emg_channels = {'EOG-RL':5, 'ECG-RA-V2':6}
# emg_channels = {'EOG-RL':5}
# emg_channels = {'ECG-AS':5}
emg_channels = {}
print(f'{device}: {activity}, {electrode_type}, g{gain}, {sampling_rate}Hz, {duration}')
print(f'{channels}, ground: {ground}, emg: {emg_channels}')
# format sd card on mac with terminal command:
# sudo diskutil eraseDisk FAT32 OBCI MBRFormat /dev/disk6
t_sleep = 1
dbg = False
board = pyOpenBCI.OpenBCICyton(port='/dev/cu.usbserial-DP04WFVJ', daisy=False)
time.sleep(t_sleep)
res = board.ser.read_all().decode()
if dbg:
board.write_command('?')
time.sleep(t_sleep * 3)
registers = board.ser.read_all().decode()
registers2 = board.ser.read_all().decode()
def print_raw(sample):
print(sample.channels_data)
# command list is here https://docs.openbci.com/Cyton/CytonSDK/
# check if board in default mode
board.write_command('//')
time.sleep(t_sleep)
res = board.ser.read_all().decode()
if dbg: print(res)
if res == 'Success: default$$$':
print(f'mode is default')
else:
sys.exit(f'mode is not default')
# remove daisy if it unused
# if (device == 'daisy') and (len(channels) < 9):
# board.write_command('c')
# time.sleep(t_sleep)
# res = board.ser.read_all().decode()
# if dbg: print(res)
# if res == 'daisy removed$$$':
# print(f'daisy is removed')
# ch_n = 8
# device = 'daisy-off'
# print(f'Device changed to {device}: n_channels to {ch_n}')
# else:
# sys.exit(f'error, daisy should be removed, but it wasnt')
# set sampling rate
sampling_rates = {16000:0,8000:1,4000:2,2000:3,1000:4,500:5,250:6}
board.write_command('~' + str(sampling_rates[sampling_rate]))
time.sleep(t_sleep)
res = board.ser.read_all().decode()
if dbg: print(res)
if len(re.findall('Sample rate is ' + str(sampling_rate) + 'Hz', res)) > 0:
print(f'sampling rate set to {sampling_rate}')
else:
sys.exit(f'sampling rate not set')
# channel configuration
gains = {1:0,2:1,4:2,6:3,8:4,12:5,24:6}
on = '0' + str(gains[gain]) + '0110X'; off = '160000X'
emg = '0' + str(gains[gain]) + '0000X'
chs = [on] * len(channels) + [emg] * len(emg_channels) + [off] * (ch_n -len(channels)-len(emg_channels))
ch_cmd = ('x1' + chs[0] + 'x2' + chs[1] +
'x3' + chs[2] + 'x4' + chs[3] +
'x5' + chs[4]+ 'x6' + chs[5] +
'x7' + chs[6] + 'x8' + chs[7])
if len(chs) == 16:
ch_cmd = ch_cmd + ('xQ' + chs[8] + 'xW' + chs[9] +
'xE' + chs[10] + 'xR' + chs[11] +
'xT' + chs[12]+ 'xY' + chs[13] +
'xU' + chs[14] + 'xI' + chs[15])
print(ch_cmd)
board.write_command(ch_cmd)
time.sleep(t_sleep*5)
res = board.ser.read_all().decode()
if dbg: print(res)
if (len(re.findall('Success', res)) > 0):
channels.update(emg_channels)
print(f'channels set: {channels}')
else:
sys.exit(f'channels not set')
if dbg:
board.write_command('?')
time.sleep(t_sleep*3)
new_registers = board.ser.read_all().decode()
new_registers2 = board.ser.read_all().decode()
# print(new_registers + registers2)
# enable sdcard writing
durations = {'5M':'A','15M':'S','30M':'F','1H':'G','2H':'H','4H':'J','12H':'K','24H':'L'}
board.write_command(durations[duration])
time.sleep(t_sleep * 10)
res = board.ser.read_all().decode()
print(res)
time.sleep(t_sleep * 5)
res2 = board.ser.read_all().decode()
print(res2)
res = res + res2
match = re.search(r' \d+ ', res)
if match is not None:
matched = match.group(0)
if(len(matched) > 0):
blocks = int(re.sub(" ","", matched))
BLOCK_5MIN = 16890
sd_duration = (blocks * 250 / sampling_rate) / (BLOCK_5MIN / 5)
print(f'SD blocks: {blocks} and max duration: {round(sd_duration)} minutes', )
if duration in ['1H','2H','4H','12H','24H']:
if f'{round(sd_duration / 60)}H' != duration:
sys.exit(f'board init wrong duration for sd file: {duration} requested {round(sd_duration/60)}H returned')
elif f'{round(sd_duration)}M' != duration:
sys.exit(f'board init wrong duration for sd file: {duration} requested {round(sd_duration)}M returned')
if dbg: print(res)
if len(re.findall('correct', res)) > 0:
re_file = re.findall(r'I\_.*\.T', res)
if len(re_file) > 0:
re_file = 'OBC' + re_file[0] + 'XT'
print(f'SD file init success {re_file}')
sd_file = re_file
# board.start_stream(print_raw)
board.write_command('~~')
time.sleep(t_sleep)
res = board.ser.read_all().decode()
print(res)
board.write_command('b')
dts = datetime.datetime.now()
print(f'Session started at {dts}')
time.sleep(t_sleep)
with closing(sqlite3.connect(os.path.join(working_dir, 'data','sessions.db'), timeout=10)) as con:
with con:
with closing(con.cursor()) as cur:
sql = 'CREATE TABLE IF NOT EXISTS Sessions(dts datetime NOT NULL PRIMARY KEY, file VARCHAR(256), settings TEXT NOT NULL)'
cur.execute(sql)
settings = {
'gain':gain, 'channels':channels, 'sf': sampling_rate,
'ground': ground, 'electrode': electrode_type, 'emg_ch': emg_channels,
'ch_n': ch_n, 'activity': activity, 'device': device, 'note': note}
json_settings = json.dumps(settings)
sql = 'REPLACE INTO Sessions (dts,file,settings) VALUES (\'' + dts.strftime('%Y-%m-%d %H:%M:%S') + '\', \'' + sd_file + '\', \'' + json_settings + '\')'
cur.execute(sql)
# sql = 'SELECT * FROM Sessions'
# cur.execute(sql)
# cur.fetchall()
else:
print(res)
sys.exit(f'SD file not found. Please restart board, dongle & check sd card')
else:
print(res)
sys.exit(f'SD init failed. Please restart board, dongle & check sd card')