2017년 7월 8일 토요일

NeuroSky MindWave (bluetooth serial stream) Issue


I have succeeded in building a fully functional App Inventor interface for NeuroSky Mindwave Mobile  but have ran into a problem that is likely a limitation of App Inventor. 

The MindWave Mobile streams bluetooth data at 9600 baud rate. However, the particular packet of data that my app utilizes (EEG power spectrum) is output only once per second.

I have the timer set to fire as fast as it can (1000 times per second) but still only manage to capture an EEG power packet once every 4 to 10 seconds. 

Once a packet with a defined length is captured, it is written into a buffer (a global list). A number of other procedures check the validity of the packets in the buffer, parse their contents, convert them from binary to decimal and look for patterns between speech and EEG input. 

I can post my blocks if anyone would like to see them, but considering there are over 560 of them, I figured it might be better to just describe the problem.

Question: Is there any way to synchronize the "receive byte" rate with the one second Mindwave output OR manipulate App Inventor to get bytes faster than 1000 times a second (perhaps with timer manipulation)?

--
what happens, if you use a more realistic timer interval of let's say 400 milliseconds?

--
Tried increasing timer interval to 400 milliseconds -- resulted in total packet loss. 

I basically am reinventing the wheel here -- NeuroSky provides Android developers with a nice little .jar to add to Android projects that handles all the stream reading and parsing, but unfortunately not compatible with App Inventor as far as I can tell. Other than that, my app is working great -- it uses the voice recognizer to associate speech and EEG patterns which are then referenced in the future to output speech (I look at a computer and it says "computer" etc.)

The MindWave also outputs a smaller packet for raw EEG data 512 times per second -- I am able to receive and parse a greater percentage of those. However, the ThinkGear chip in Mindwave automatically removes artifacts and performs Fourier transformation on raw EEG to produce the 1 second power spectrum packets, which is why I really would like to decrease my packet loss!

Questions:

1. Even though I have a buffer, is it possible that other procedures may be interfering with the execution of "receive byte"? 

2. If I got rid of the timer altogether and looped "receive byte," could that allow my app to sample at a rate faster than the timer allows? (would require rewriting a lot of code, but would definitely be worth it if it has a chance of success).

3. If neither of the above solve the problem, would it be possible to implement something like this with Activity Starter: (Java ignoramus here)

public void run() {
        byte[] buffer = new byte[1024];
        int bytes;
 while (true) {
            try {
                bytes = mmInStream.read(buffer);
                mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer)
                        .sendToTarget();
            } catch (IOException e) {
                break;
            }
        }
    }




--

댓글 1개: