-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAxonCOM.py
More file actions
366 lines (313 loc) · 13.8 KB
/
AxonCOM.py
File metadata and controls
366 lines (313 loc) · 13.8 KB
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
import sys
import time
from enum import Enum
if sys.platform == "win32":
from serial.serialwin32 import Serial as Serial
else:
from serial.serialposix import Serial as Serial
from pylsl import StreamInfo, StreamOutlet
import numpy
import threading
from AxonCommon import AxonCommon, State
# from signal_processing import butter_lowpass_filter, butter_highpass_filter, iir_notch_filter
from axontools import PcktType
class AxonCOM(AxonCommon):
ser = Serial()
serial_lock = threading.Lock()
streaming_lock = threading.Lock()
ser.baudrate = 230400
ser.timeout = 5
num_modules = 0
def halt(self):
self.safe_serial_write(b'c')
time.sleep(1)
self.inputThreadAlive = False
# self.state = State.connected
# print(f'After: {self.state}')
# try:
# self.ser.write(b"c")
# except Exception as e:
# print(f"COuld not kill/write to kill: {str(e)}")
# print('Just wrote "c"')
def connect(self, comTrgt):
self.ser.port = comTrgt
try:
self.ser.open()
self.state = State.connected
return 'success'
except Exception as e:
self.state = State.disconnected
print("error open serial port: " + str(e))
return "error open serial port: " + str(e)
# if(self.state==State.connected):
# self.init()
# def handleInitPack(self, pack):
def removeBias(self):
self.safe_serial_write(b'm100.')
# time.sleep(1)
# self.ser.flush()
# time.sleep(1)
return 1
def safe_serial_write(self, data):
with self.serial_lock:
self.ser.write(data)
self.ser.flush()
def safe_serial_read(self, timeout=2.0, size=0):
try:
with self.serial_lock:
self.ser.timeout = timeout
if(size>0):
data = self.ser.read_until(size=size)
else:
data = self.ser.read_until()
return data if data else b""
except Exception as e:
print("Errror reading: ", str(e))
return b""
def safe_serial_reset_buffers(self):
with self.serial_lock:
self.ser.reset_input_buffer()
self.ser.reset_output_buffer()
def calibrate(self):
incomingSize = 5+24*self.num_modules
while True:
self.safe_serial_write(b"k1")
# Recieves the confirmation that voltage calibration mode is enabled.
res = self.safe_serial_read()
print(res)
if b"Enabled voltage calibration mode." not in res:
print("Module was already in vcal mode, trying again.")
continue
else:
print("Enabled vcal mode")
break
# Prepare the arrays for recording
vcal_samples = 500
vcal_data = numpy.zeros((vcal_samples, self.num_modules*8), dtype='int')
vcal_num_avgd = 100
counter = 0
# Start the voltage calibration process
self.safe_serial_write(b'b')
try:
while True:
# print("printing...")
while True:
if self.ser.read() == b"\r":
break
pckt_type_byte = self.safe_serial_read(size=1).hex()
pckt_type = int(bytearray.fromhex(pckt_type_byte).decode("utf-8"), 16)
# print(f"{pckt_type} + {PcktType(pckt_type).name}")
if pckt_type == PcktType.DATA.value:
incoming = self.ser.read(incomingSize).hex()
line = incoming[2:]
sample_num = int.from_bytes(bytearray.fromhex(incoming[0:2]), byteorder='big')
n=6
chunks = [line[i:i + n] for i in range(0, len(line), n)]
rawSample = numpy.array([self.convertReading(chunks[i]) for i in range(0, 8 * self.num_modules)])
sample = rawSample
# Code to save each sample into the array.
vcal_data[counter] = sample
# Read the ending of the packet and extract the status bytes and battery level
endbytes = incoming[-8:]
# if sample_num == 60:
# print(f"Sample num: {sample_num} | End bytes: {endbytes}")
status1 = int.from_bytes(bytearray.fromhex(incoming[-8:-6]), byteorder='big')
status2 = int.from_bytes(bytearray.fromhex(incoming[-6:-4]), byteorder='big')
battlvl = int.from_bytes(bytearray.fromhex(incoming[-4:-2]), byteorder='big') - 64
# print(f"Status1 {status1}, Status2 {status2}, Battery level {battlvl}")
if counter % 2000 == 0:
print(f"Battery level: {battlvl}")
elif pckt_type == PcktType.INIT.value:
raise Exception("Init packet received again, something is wrong.")
elif pckt_type == PcktType.PADDING.value:
raise Exception("Padding packet received, something is wrong.")
elif pckt_type in {PcktType.DEBUG.value, PcktType.INFO.value, PcktType.WARNING.value, PcktType.ERR.value, PcktType.FATAL.value}:
incoming = self.safe_serial_read().hex()
message = bytearray.fromhex(incoming[2:-2]).decode("utf-8")
print(f"{PcktType(pckt_type).name} pckt: \"{message}\"")
else:
raise Exception(f"Unknown packet type received. Packet type: {pckt_type}")
counter+=1
if counter >= vcal_samples:
break
except KeyboardInterrupt:
print("Cancelled by user")
# except Exception as e:
# print("Error: " + str(e))
finally:
print("Stopping calibration stream")
self.safe_serial_write(b"c")
time.sleep(.4)
self.safe_serial_reset_buffers()
# Wait until the module is back into init mode.
res = self.safe_serial_read()
while True:
self.safe_serial_write(b"k0")
# Recieves the confirmation that voltage calibration mode is disabled.
res = self.safe_serial_read()
# print(res)
if b"Disabled voltage calibration mode." not in res:
# print("Module was already not in vcal, trying again.")
continue
else:
print("Disabled vcal mode")
break
# Start analyzing the voltage calibration data.
print("Analyzing voltage calibration data...")
# Steps:
# 1. Find the zero-crossing points
# 2. Remove points around the zero-crossings
# 3. Separate the positive and negative values
# 4. Calculate the average of the positive and negative values
# 5. The amplitude is the difference between the average positive and average negative values
# Find zero-crossing points
zero_crossings = numpy.where(numpy.diff(numpy.sign(vcal_data[:, 0]), axis=0))[0]
# print(f"Zero-crossing points: {zero_crossings}")
# Remove points around the zero-crossings
margin = 3 # Number of points to remove around zero-crossings
for zero_crossing in zero_crossings:
start = max(0, zero_crossing - margin)
end = min(vcal_data.shape[0], zero_crossing + margin)
vcal_data[start:end, :] = 0
# Separate positive and negative values
positive_row_indices = numpy.where(vcal_data[:, 0] > 0)[0]
negative_row_indices = numpy.where(vcal_data[:, 0] < 0)[0]
positive_values = vcal_data[positive_row_indices, :]
negative_values = vcal_data[negative_row_indices, :]
# Calculate the average of the positive and negative values
davg_p = numpy.round(numpy.mean(positive_values, axis=0))
davg_n = numpy.round(numpy.mean(negative_values, axis=0))
self.c_factor = numpy.round(1875 / (davg_p - davg_n), 6)
# # Write all of the samples into a csv file for testing.
# time_now = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
# output_file = f'./test_outputs/vcal_test_{time_now}.csv'
# with open(output_file, 'w', newline='') as csvfile:
# streamwriter = csv.writer(csvfile, delimiter=',', dialect='excel')
# for i in range(0, np.shape(vcal_data)[0]):
# streamwriter.writerow(vcal_data[i])
print("Voltage calibration complete")
print(f"Calibration factors: {self.c_factor}")
def init(self):
# self.ser.flush()
# self.ser.write(b'c')
# self.ser.flush()
# time.sleep(1)
# iPack = self.ser.read_until()
# if iPack != b'':
# print(iPack)
# self.halt()
# self.ser.write(b'h')
# self.ser.flush()
# time.sleep(1)
# self.ser.write(b'c')
# time.sleep(1)
# # time.sleep(3)
self.safe_serial_write(b'p')
# time.sleep(1)
# [ ] maybe add "are you streaming call to the board??"
iPack = self.safe_serial_read()
# TODO handle init packet
# channels = 8
# self.num_modules = 1
msg = ""
try:
# print(init_pckt[0].to_bytes())
if iPack[0].to_bytes(1, 'big') != b'\r':
msg = "No CR on beginning"
# print(init_pckt[17].to_bytes())
if iPack[18].to_bytes(1, 'big') != b'\n':
msg = "No NL on end"
# print(len(init_pckt))
if len(iPack) != 19:
msg = "init packet wrong length"
if msg != "":
self.state = State.disconnected
raise Exception(msg)
self.handleInitPack(iPack)
self.calibrate()
self.removeBias()
return 1
# print("Init packet formatted correctly")
except Exception as e:
print("Init packet error: " + str(e))
print(iPack)
self.halt()
return -1
def handleInitPack(self, pack):
# cleaned = pack.strip().decode('utf-8')
self.num_modules = pack.count(b'2')
self.channels = self.num_modules*8
print(f'num_modules: {self.num_modules}')
def stopStream(self):
print('changing state')
self.state = State.connected
def stream(self):
if(not self.state.streaming):
return False
# self.safe_serial_reset_buffers()
time.sleep(1)
self.safe_serial_write(b'b')
time.sleep(1)
# set up lsl
name = "AxonEEG"
type = "EEG"
srate = 250
info = StreamInfo(name, type, self.channels, srate, "float32", "myuid34234")
outlet = StreamOutlet(info)
incomingSize = 5+24*self.num_modules
b_order = 6
fs = 250.0 # sample rate, Hz
low_cutoff = 15.0 # desired cutoff frequency of the filter, Hz
high_cutoff = 1.0 # desired cutoff frequency of the filter, Hz
notch_center = 60.0 # desired frequency to remove with notch filter, Hz
counter = 0
# with 1==1:
while self.state==State.streaming:
while True:
if(self.ser.read()==b"\r"):
print("got a new line")
break
# break
# while True:
# if self.safe_serial_read() == b"\r":
# break
pckt_type_byte = self.ser.read(1).hex()
pckt_type = int(bytearray.fromhex(pckt_type_byte).decode("utf-8"), 16)
if pckt_type == PcktType.DATA.value:
incoming = self.ser.read(incomingSize).hex()
line = incoming[2:]
sample_num = int.from_bytes(bytearray.fromhex(incoming[0:2]), byteorder='big')
n = 6
chunks = [line[i:i + n] for i in range(0, len(line), n)]
# if self.voltage_calib:
rawSample = numpy.array([self.convertReadingVCal(chunks[i], self.c_factor[i]) for i in range(0, 8 * self.num_modules)])
# else:
# rawSample = numpy.array([convertReading(chunks[i])/100 for i in range(0, 8 * num_modules)])
sample = rawSample
# for chan in range(0, len(rawSample)):
# data = rawSample[:,chan] - np.mean(rawSample[:,chan])
# y = butter_lowpass_filter(data, low_cutoff, fs, b_order)
# y = butter_highpass_filter(y, high_cutoff, fs, b_order)
# sample = iir_notch_filter(y, notch_center, fs)
# print("sample", file=sys.stdout)
# print(sample, file=sys.stdout)
# print("sample")
# print(sample)
outlet.push_sample(sample.tolist())
print('stopping...')
self.safe_serial_write(b'c')
# print(f'state is {self.state}')
# if(self.state == State.closing):
# sys.exit()
print(self.state)
self.state = State.connected
# Function to convert the hex readings to properly signed integers
def convertReading(self, reading):
reading = int(reading, 16)
if reading >= int(0x800000):
reading -= (int(0xFFFFFF) + 1)
return reading
# Function to convert the hex readings to voltage calibrated values
def convertReadingVCal(self, reading, c_factor):
reading = self.convertReading(reading)
return reading * c_factor