Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Blink value is always zero #3

Open
nagasgura opened this issue Jul 25, 2013 · 17 comments
Open

Blink value is always zero #3

nagasgura opened this issue Jul 25, 2013 · 17 comments

Comments

@nagasgura
Copy link

Meditation and attention work perfectly, but I can't get blink detection to work. It is always at zero.

@alanchrt
Copy link
Contributor

Hmm, not sure. Are you binding a handler to the blink event, or you are you reading headset.blink directly?

Here's how to capture a blink event and read its strength:

def on_blink(headset, blink_strength):
    print "Blink detected. Strength: %s" % blink_strength

headset.blink_handlers.append(on_blink)

Let me know if it fails that way too. Reading the value directly will only briefly flicker to a non-zero value whenever a blink occurs.

@nagasgura
Copy link
Author

Didn't work. Here's my code:

import mindwave, time, sys

headset = mindwave.Headset(2)
time.sleep(2)

headset.connect()
print "Connecting..."

while headset.status != 'connected':
    time.sleep(0.5)
    if headset.status == 'standby':
        headset.connect()
        print "Retrying connect..."
print "Connected."
def on_blink(headset, blink_strength):
    print "Blink detected. Strength: %s" % blink_strength
    sys.exit()



while True:
    print "Attention: %s, Meditation: %s" % (headset.attention, headset.meditation)
    headset.blink_handlers.append(on_blink)

I might be doing something completely wrong. Could you show me a full example code that gets blinks?

@alanchrt
Copy link
Contributor

A couple things jump out a bit at first glance.

One is that appending the handler inside the while loop means that it's getting added over and over and over again, hundreds of times. It only needs to be added once to be "registered," so you can pull it up out of the while loop.

Also, it's a good idea to put a little bit of delay in your while loop so you're not taking 100% of the CPU and possibly blocking the thread from executing the blink handler when a blink is detected. This can be done by just adding a small sleep.

Try modifying the bottom part of the code to something like this:

headset.blink_handlers.append(on_blink)

while True:
    print "Attention: %s, Meditation: %s" % (headset.attention, headset.meditation)
    time.sleep(0.1)

Let me know if that helps! Also, have you tried the manufacturer demos just to double-check that blinks are working well?

@nagasgura
Copy link
Author

Still nothing. The manufacturer demos work fine though. I read somewhere that blink detection is actually done in software, not build directly into the headband like attention and meditation detection. Have you ever gotten blink detection working with this?

@alanchrt
Copy link
Contributor

It did work with the MindWave USB headset for sure, but I'm not sure if anyone has tested it with the Bluetooth one. Are you using the USB or Bluetooth MindWave?

We've since pretty much retired our headsets, so I don't have them on hand to be able to test any more.

@nagasgura
Copy link
Author

I'm using the USB version. I have no idea why it's not working. Do you have a full script that I could try so I could do the exact same thing that you did?

@alanchrt
Copy link
Contributor

Sorry for the frustrations. I can take a peek when I'm back in the office on Monday and see if I can find any old code that had blinking in it. I have a feeling the only projects we hung onto were the ones that used just the attention/meditation values.

I'm going to re-open the issue for now so anyone else with the problem can chime in, or someone can contribute their working blink code if they have a functioning example.

@alanchrt alanchrt reopened this Jul 27, 2013
@alanchrt
Copy link
Contributor

Just thought of another quick test..

Here's the code for reading blinks from the headset: https://github.com/BarkleyUS/mindwave-python/blob/master/mindwave.py#L127

No need to understand that, but if you notice above that line, it's reading/calling event handlers in the same way it calls the attention/meditation handlers.

Would you mind trying to swap out your code to call your handler function as a handler for the meditation or attention events instead? Just to make sure the event handler calling is working at all, since we know attention/meditation are working.

Your code might look something like this:

import mindwave, time, sys

headset = mindwave.Headset(2)
time.sleep(2)

headset.connect()
print "Connecting..."

while headset.status != 'connected':
    time.sleep(0.5)
    if headset.status == 'standby':
        headset.connect()
        print "Retrying connect..."
print "Connected."

def on_attention(headset, attention):
    print "Attention detected. Level: %s" % attention
    sys.exit()

headset.attention_handlers.append(on_attention)
while True:
    time.sleep(0.1)

Just make sure it actually detects and prints out attention values. Then, swapping the exact same code, make sure it fails for blink events.

@nagasgura
Copy link
Author

Thanks for your help. I tried what you said and it works fine for attention and meditation, just not blinks. Any other ideas of what it could be?

@radistmorse
Copy link

Have the same issue. Blinking is never even reported. I even changed a bit the SYNCing logic and enabled checksum (why did you disable it in the first place?) to be extra sure that I'm not losing anything and dumped any unhandled codes. It seems that it simply does not report blining. Period. So I guess there is no problem in your code, just some weird thing with the EEG itself (blinking is working in windows).

I have a USB headset.

@alanchrt
Copy link
Contributor

alanchrt commented Jan 6, 2014

Man, that is so bizarre. I'm really scratching my head, especially since it works with the manufacturer software.

And, on the checksums, I was getting invalid checksums when the packets looked just fine, causing it to drop a lot of values, so I disabled it. But, maybe that was just the headset I had at the time.

@radistmorse
Copy link

I'll tinker with my EEG later trying to get to the root of this. Probably will dump the whole exchange from windows to see if there are some undocumented codes or commands.

BTW, there is an error in your checksum code: you should do it for the whole payload, not for payload[:-1]. Also
val &= 0xff
is redundant
val = ~val & 0xff
is quite enough.

@dynamicmindset
Copy link

Having the same issue, I found a note in the Mindset communication protocol:
Blink Strength
Note: This data value is currently only available via the TGCD and TGC APIs. It is
not directly available as output from any current ThinkGear hardware. For TGCD, see the
TG_DATA_BLINK_STRENGTH data type for use with the TG_GetValueStatus() and TG_GetValue()
functions.
This is strange taking into account that Blink Strength is listed with code 0x16 in the same list as Attention and Meditation.

@jacobstein123
Copy link

I ended up just writing my own blink detection algorithm which worked pretty well. Probably not quite as accurate as the standard one, though.

@MuhamadAbbas
Copy link

@jacobstein123 Can u share your code with us ? Thanks in advance.

@jacobstein123
Copy link

Here's the code: https://github.com/jacobstein123/EEG_Robotic_Arm/blob/master/eeg_control.py. It has a bunch of other stuff in it, but the blink detection is there. I wrote this in high school before I knew anything so it's prob pretty rough, but it did work.

@vsltech
Copy link

vsltech commented Sep 25, 2018

Hi!
I tried this: https://github.com/BarkleyUS/mindwave-python/blob/master/mindwave.py#L127
& all above mentioned eye_blink handlers but no luck on Eye Blinks. So, I decided to write my own code which works well in Windows 7/8/8.1/10 + Ubuntu 16.04 & is tested for different age groups(18-70 years) on different mind states provided the headset is kept IDLE on forehead no loose contact or movement.

Check these projects based on mindwave-python

http://www.vslcreations.com/2018/01/how-to-connect-neurosky-mindwave-with.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants