lambdaloop

Scattered projects and ideas

Streaming OpenBCI data in Python

I’ve used the OpenBCI with Python for some time now. The experience definitely needs improvement, but at least it works.

The idea is to use open_bci.py to stream the EEG data. Usually, I don’t use this library directly, but through a wrapper for collecting CSV data.

To use this, first put open_bci.py and the wrapper (csv_collector.py) in one folder, and write a script there. Then, the code for some tagged data collection looks something like this:

from csv_collector import CSVCollector
import time

port = '/dev/ttyUSB0'
eeg_fname = 'eeg.csv'
collector = CSVCollector(port=port, fname=eeg_fname)
collector.start() # start the data collection
# default tag is 0
time.sleep(3) # make sure data is collecting and signal is stable

collector.tag("X")  # now tag is "X"
# show stimulus X

collector.tag("Y") # now tag is "Y"
# show stimulus Y

# ... show more stuff ...

collector.stop() # finish data collection

You can see an involved example here, from our memory encoding study.

Problems with our software

So this works, but there are a few (pretty bad, if you ask me) problems with my current streaming method.

First, you have to specify the port of the OpenBCI board manually. On my Linux system, it’s “/dev/ttyUSB0”, so that’s what it is in the code (in encode.py). However, on OS X, it may be something like /dev/tty.usbmodem1532. On Windows, it’s something else as well, so watch out.

Second, the program does not terminate cleanly. I use a thread in the background to tag data, but it doesn’t terminate cleanly when I exit the program, so I usually suspend the python process with Control-z and then kill it using “killall -9 python”.

I’m hoping both of those issues will be fixed in future versions of open_bci.py, but watch out for them now.

Problems with OpenBCI firmware

Another problem you might encounter is that the OpenBCI firmware does not terminate the connection cleanly, so you have to constantly plug out the board and plug it back in, restarting the streaming code until you get streaming data.

For this, I heard that AJ Keller’s new OpenBCI firmware fixes this, though I haven’t tried this myself.