Skip to content

Commit e4037a0

Browse files
committed
ganglion: better handling of ID in output samples
1 parent 86d4bdd commit e4037a0

File tree

1 file changed

+35
-28
lines changed

1 file changed

+35
-28
lines changed

open_bci_ganglion.py

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ def handle_sample(sample):
1212
board.start(handle_sample)
1313
1414
TODO: support impedance
15-
TODO: better sample ID
1615
TODO: switch for 18bit aux
1716
TODO: indicate incoming message -- end ascii packet or timeout
1817
@@ -319,7 +318,7 @@ def reconnect(self):
319318
self.init_steaming()
320319

321320
class OpenBCISample(object):
322-
"""Object encapulsating a single sample from the OpenBCI board. Note for Ganglion board: since most of the time two samples are compressed in one BLE packet, two consecutive samples will likely have the same ID."""
321+
"""Object encapulsating a single sample from the OpenBCI board."""
323322
def __init__(self, packet_id, channel_data, aux_data):
324323
self.id = packet_id;
325324
self.channel_data = channel_data;
@@ -384,7 +383,7 @@ def parse(self, packet):
384383
else:
385384
print("Warning: unknown type of packet: " + str(start_byte))
386385

387-
def parseRaw(self, sample_id, packet):
386+
def parseRaw(self, packet_id, packet):
388387
""" Dealing with "Raw uncompressed" """
389388
if len(packet) != 19:
390389
print('Wrong size, for raw data' + str(len(data)) + ' instead of 19 bytes')
@@ -395,77 +394,85 @@ def parseRaw(self, sample_id, packet):
395394
for i in range(0,12,3):
396395
chan_data.append(conv24bitsToInt(packet[i:i+3]))
397396
# save uncompressed raw channel for future use and append whole sample
398-
self.pushSample(sample_id, chan_data, self.lastAcceleromoter)
397+
self.pushSample(packet_id, chan_data, self.lastAcceleromoter)
399398
self.lastChannelData = chan_data
400-
self.updatePacketsCount(sample_id)
399+
self.updatePacketsCount(packet_id)
401400

402-
def pushSample(self, sample_id, chan_data, aux_data):
403-
""" Add a sample to inner stack, dealing with scaling if necessary """
404-
405-
if self.scaling_output:
406-
chan_data = list(np.array(chan_data) * scale_fac_uVolts_per_count)
407-
aux_data = list(np.array(aux_data) * scale_fac_accel_G_per_count)
408-
sample = OpenBCISample(sample_id, chan_data, aux_data)
409-
self.samples.append(sample)
410-
411-
def parse19bit(self, sample_id, packet):
401+
def parse19bit(self, packet_id, packet):
412402
""" Dealing with "19-bit compression without Accelerometer" """
413403
if len(packet) != 19:
414404
print('Wrong size, for 19-bit compression data' + str(len(data)) + ' instead of 19 bytes')
415405
return
416406

417407
# should get 2 by 4 arrays of uncompressed data
418408
deltas = decompressDeltas19Bit(packet)
409+
# the sample_id will be shifted
410+
delta_id = 1
419411
for delta in deltas:
412+
# convert from packet to sample id
413+
sample_id = (packet_id - 1) * 2 + delta_id
420414
# 19bit packets hold deltas between two samples
421415
# TODO: use more broadly numpy
422416
full_data = list(np.array(self.lastChannelData) - np.array(delta))
423417
# NB: aux data updated only in 18bit mode, send values here only to be consistent
424418
self.pushSample(sample_id, full_data, self.lastAcceleromoter)
425419
self.lastChannelData = full_data
426-
self.updatePacketsCount(sample_id)
420+
delta_id += 1
421+
self.updatePacketsCount(packet_id)
427422

428423

429-
def parse18bit(self, sample_id, packet):
424+
def parse18bit(self, packet_id, packet):
430425
""" Dealing with "18-bit compression without Accelerometer" """
431426
if len(packet) != 19:
432427
print('Wrong size, for 18-bit compression data' + str(len(data)) + ' instead of 19 bytes')
433428
return
434429

435430

436431
# accelerometer X
437-
if sample_id % 10 == 1:
432+
if packet_id % 10 == 1:
438433
self.lastAcceleromoter[0] = conv8bitToInt8(packet[18])
439434
# accelerometer Y
440-
elif sample_id % 10 == 2:
435+
elif packet_id % 10 == 2:
441436
self.lastAcceleromoter[1] = conv8bitToInt8(packet[18])
442437
# accelerometer Z
443-
elif sample_id % 10 == 3:
438+
elif packet_id % 10 == 3:
444439
self.lastAcceleromoter[2] = conv8bitToInt8(packet[18])
445440

446-
447441
# deltas: should get 2 by 4 arrays of uncompressed data
448442
deltas = decompressDeltas18Bit(packet[:-1])
443+
# the sample_id will be shifted
444+
delta_id = 1
449445
for delta in deltas:
446+
# convert from packet to sample id
447+
sample_id = (packet_id - 1) * 2 + delta_id
450448
# 19bit packets hold deltas between two samples
451449
# TODO: use more broadly numpy
452450
full_data = list(np.array(self.lastChannelData) - np.array(delta))
453451
self.pushSample(sample_id, full_data, self.lastAcceleromoter)
454452
self.lastChannelData = full_data
455-
self.updatePacketsCount(sample_id)
453+
delta_id += 1
454+
self.updatePacketsCount(packet_id)
455+
456+
def pushSample(self, sample_id, chan_data, aux_data):
457+
""" Add a sample to inner stack, setting ID and dealing with scaling if necessary. """
458+
if self.scaling_output:
459+
chan_data = list(np.array(chan_data) * scale_fac_uVolts_per_count)
460+
aux_data = list(np.array(aux_data) * scale_fac_accel_G_per_count)
461+
sample = OpenBCISample(sample_id, chan_data, aux_data)
462+
self.samples.append(sample)
456463

457-
def updatePacketsCount(self, sample_id):
464+
def updatePacketsCount(self, packet_id):
458465
"""Update last packet ID and dropped packets"""
459466
if self.last_id == -1:
460-
self.last_id = sample_id
467+
self.last_id = packet_id
461468
self.packets_dropped = 0
462469
return
463470
# ID loops every 101 packets
464-
if sample_id > self.last_id:
465-
self.packets_dropped = sample_id - self.last_id - 1
471+
if packet_id > self.last_id:
472+
self.packets_dropped = packet_id - self.last_id - 1
466473
else:
467-
self.packets_dropped = sample_id + 101 - self.last_id - 1
468-
self.last_id = sample_id
474+
self.packets_dropped = packet_id + 101 - self.last_id - 1
475+
self.last_id = packet_id
469476
if self.packets_dropped > 0:
470477
print("Warning: dropped " + str(self.packets_dropped) + " packets.")
471478

0 commit comments

Comments
 (0)