Friday, September 11, 2015

Logbook 9/11

This week I worked on attempting to graph the incoming data from the TGAM brain chip over Bluetooth. The data comes in as a string in the specified format below with values being from 0 to 131072 (I think)

freqs = [single strength, attn, relax, delta, theta, alphaL, alphaH, betaL, betaH, gammaL, gammaH]

The signal strength should be low to indicate a good connection, 200 means there is brain waves getting picked up. I wrote a python script to capture the data (Bluetooth serial connected on COM8 on my computer). The script breaks down the data into an array and the plots the values.






Attention and relaxation values I believe are the most important. These are computed within the chip itself from the values of the following frequency bands. For a more accurate or fine-tuned approach, we could monitor the frequency bins as well. The values of attn. and relaxation have a smaller range, 256 being the max, therefore the other values are plotted off the visible area.

For some reason, at times when a certain frequency value goes to high, python prints “ERROR: packet too long” rather than the actual data. To get rid of this messing things up, the script looks to see if the letter ‘E’ is in the incoming data string first. If it is, it just ignores it. No good data should ever have the letter E in it since the TGAM chip only sends a string of numbers and commas.





#Python Analyzing Program
import serial
import numpy as np
import time
from matplotlib import pyplot as plt
ser = serial.Serial('COM8', 9600)

plt.ion() # set plot to animated

line = [None] * 10
ydata = [0] * 50
attn = [0] * 50
relax = [0] * 50
delta = [0] * 50
theta = [0] * 50
alphaL = [0] * 50
alphaH = [0] * 50
betaL = [0] * 50
betaH = [0] * 50
gammaL = [0] * 50
gammaH = [0] * 50

freqs = [attn, relax, delta, theta, alphaL, alphaH, betaL, betaH, gammaL, gammaH]
shades = ["#800000", "#000066","#66FFFF", "#66CCFF", "#99CCFF", "#9999FF", "#CC66FF" , "#FF66FF", "#FF3399", "#FF0066"]
ax1=plt.axes()

# make plot
i=0;
for x in freqs:
    line[i], = plt.plot(x)
    plt.ylim([10,40])
    i+=1;

# start data collection
while True:
    data = ser.readline(); # read data from serial
                                   # port and strip line endings

    if(data.find('E')==-1):
        print data
        data = data.split(",");
        if data[0] != 200:
            ymin = 10;
            ymax = 200
            plt.ylim([ymin,ymax])
            j = 0;
            for f in freqs:
                if(j==0 or j==1):
                    line[j].linestyle='-';
                f.append(data[j+1])
                del f[0]
                line[j].c = shades[j]
                line[j].set_xdata(np.arange(len(f)))
                line[j].set_ydata(f)  # update the data
                j+=1;
            plt.draw() # update the plot

            print "receieved"
            time.sleep(.5) 





Friday, September 4, 2015

Logbook 9/4

We have split up into sections; Mustafa and Mario are working on getting the TGAM chip which we harvested out of the MindFlex toy to work. It seems like the chip is very sensitive to voltage differences and bad ground.

http://store.neurosky.com/products/mindflex












We know the chip is good because we previously tested it using the toy. There is a ‘game board’ with a fan which runs at a variable speed based on your brain patterns, when you take the headset off or don’t connect it on your head right, the panel voices outloud “CHECK HEADSET”

Mario and Mustafa are trying to get the game to work using our electrodes. Since we disassembled it, using the original electrodes doesn’t even seem to work anymore. When monitoring the serial output from the chip, it reads  “200, 0, 0”. From the TGAM datasheet we know that the 200 indicates a too weak EEG signal.


Luckily, I ordered two of these Mindflex games so I am using the spare one to work on the code that will analyze the brainwaves. With the second game, I did not disassembl the headset, but just soldered in two wires for Tx and Gnd as to get the serial brainwave data. Originally I connected these to an Arduino and attempted to echo the data to the usb serial interface that comes with all Arduinos. Oddly enough…….. as soon as you connect the Arduino the “CHECK HEADSET” error reappears. I tried powering the Arduino with a 9V battery rather than the usb port, and the headset made the connection to the game board okay. When I connected the usb cable to capture the data…… the connection dropped and the game board once again went into “CHECK HEADSET” mode. I tested plugging and unplugging the Arduino to see the results, and it seemed that the brain connection would be lost everytime I plugged it in, but the connection was re-established when I disconnected the cable. My conclusion was that there must be some uncommon ground error between the TGAM chip and the laptop, perhaps we can fix Mario and Mustafa’s problem by checking their ground lines and powering their Arduino from a battery as well.

Our final product does not need to be connected to a computer, all brainwave processing will be done onboard with the Arduino. However, for testing we need to know what the thresholds of sleep brainwaves are, so I used a Bluetooth module to send the data out over wirelessly. It worked!



/* Echo Serial data to bluetooth */ 
#include <"softwareserial.h"> 

 SoftwareSerial mySerial(6, 7); 
 // RX, TX to TGAM 
//dedicated Tx and Rx to bluetooth 
 void setup() 

 // Open serial communications and wait for port to open: 
 Serial.begin(9600); // set the data rate for the SoftwareSerial port 
 mySerial.begin(9600); 


 void loop() 

// run over and over 
 while(mySerial.available()) 
 { 
 Serial.write(mySerial.read(); 
 Serial.println(); 
 } 
}