Skip to content

Commit

Permalink
ganglion: scaling output
Browse files Browse the repository at this point in the history
  • Loading branch information
jfrey-xx committed Mar 2, 2017
1 parent 0f1be18 commit 86d4bdd
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions open_bci_ganglion.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ def handle_sample(sample):
TODO: support impedance
TODO: better sample ID
TODO: scale output, both for EEG and AUX
TODO: switch for 18bit aux
TODO: indicate incoming message -- end ascii packet or timeout
Expand All @@ -33,7 +32,8 @@ def handle_sample(sample):
from btle import Scanner, DefaultDelegate, Peripheral

SAMPLE_RATE = 200.0 # Hz
scale_fac_uVolts_per_count = 1200 * 8388607.0 * 1.5 * 51.0;
scale_fac_uVolts_per_count = 1200 / (8388607.0 * 1.5 * 51.0)
scale_fac_accel_G_per_count = 0.000032

# service for communication, as per docs
BLE_SERVICE = "fe84"
Expand Down Expand Up @@ -70,6 +70,7 @@ def __init__(self, port=None, baud=0, filter_data=False,
self.streaming = False
self.timeout = timeout
self.max_packets_to_skip = max_packets_to_skip
self.scaling_output = scaled_output

print("Looking for Ganglion board")
if port == None:
Expand All @@ -79,8 +80,7 @@ def __init__(self, port=None, baud=0, filter_data=False,
self.connect()

self.streaming = False
self.scaling_output = scaled_output
# number of EEG channels and (optionally) accelerometer channel
# number of EEG channels and (optionally) accelerometer channel
self.eeg_channels_per_sample = 4
self.aux_channels_per_sample = 3
self.read_state = 0
Expand Down Expand Up @@ -112,7 +112,7 @@ def connect(self):
print ("disconnect, properties: " + str(self.char_discon.propertiesToString()) + ", supports read: " + str(self.char_discon.supportsRead()))

# set delegate to handle incoming data
self.delegate = GanglionDelegate()
self.delegate = GanglionDelegate(self.scaling_output)
self.gang.setDelegate(self.delegate)

print("Turn on notifications")
Expand Down Expand Up @@ -328,7 +328,7 @@ def __init__(self, packet_id, channel_data, aux_data):

class GanglionDelegate(DefaultDelegate):
""" Called by bluepy (handling BLE connection) when new data arrive, parses samples. """
def __init__(self):
def __init__(self, scaling_output = True):
DefaultDelegate.__init__(self)
# holds samples until OpenBCIBoard claims them
self.samples = []
Expand All @@ -339,6 +339,7 @@ def __init__(self):
self.lastChannelData = [0, 0, 0, 0]
# 18bit data got here and then accelerometer with it
self.lastAcceleromoter = [0, 0, 0]
self.scaling_output = scaling_output

def handleNotification(self, cHandle, data):
if len(data) < 1:
Expand Down Expand Up @@ -394,11 +395,19 @@ def parseRaw(self, sample_id, packet):
for i in range(0,12,3):
chan_data.append(conv24bitsToInt(packet[i:i+3]))
# save uncompressed raw channel for future use and append whole sample
sample = OpenBCISample(sample_id, chan_data, [])
self.samples.append(sample)
self.pushSample(sample_id, chan_data, self.lastAcceleromoter)
self.lastChannelData = chan_data
self.updatePacketsCount(sample_id)

def pushSample(self, sample_id, chan_data, aux_data):
""" Add a sample to inner stack, dealing with scaling if necessary """

if self.scaling_output:
chan_data = list(np.array(chan_data) * scale_fac_uVolts_per_count)
aux_data = list(np.array(aux_data) * scale_fac_accel_G_per_count)
sample = OpenBCISample(sample_id, chan_data, aux_data)
self.samples.append(sample)

def parse19bit(self, sample_id, packet):
""" Dealing with "19-bit compression without Accelerometer" """
if len(packet) != 19:
Expand All @@ -412,8 +421,7 @@ def parse19bit(self, sample_id, packet):
# TODO: use more broadly numpy
full_data = list(np.array(self.lastChannelData) - np.array(delta))
# NB: aux data updated only in 18bit mode, send values here only to be consistent
sample = OpenBCISample(sample_id, full_data, self.lastAcceleromoter)
self.samples.append(sample)
self.pushSample(sample_id, full_data, self.lastAcceleromoter)
self.lastChannelData = full_data
self.updatePacketsCount(sample_id)

Expand Down Expand Up @@ -442,8 +450,7 @@ def parse18bit(self, sample_id, packet):
# 19bit packets hold deltas between two samples
# TODO: use more broadly numpy
full_data = list(np.array(self.lastChannelData) - np.array(delta))
sample = OpenBCISample(sample_id, full_data, self.lastAcceleromoter)
self.samples.append(sample)
self.pushSample(sample_id, full_data, self.lastAcceleromoter)
self.lastChannelData = full_data
self.updatePacketsCount(sample_id)

Expand Down

0 comments on commit 86d4bdd

Please sign in to comment.