alexandrebarachant / muse-lsl

Python script to stream EEG data from the muse 2016 headset
BSD 3-Clause "New" or "Revised" License
616 stars 182 forks source link

bleak backend, cross-platform bluetooth, no root required #148

Closed xloem closed 2 years ago

xloem commented 3 years ago

fixes #146

EDIT: The PR that enables this when looking for devices is #149, so if you want to use bleak for everything you'll need both PRs.

xloem commented 3 years ago

I've merged this PR along with most of the others into https://github.com/xloem/muse-lsl/tree/dev , and resolved bugs on my system, but it still needs somebody else to test it. What would help others have more capacity for pull request maintenance, merging, testing?

jdpigeon commented 3 years ago

This does sound very promising. However, I wasn't able to get a device successfully connected off of this branch with some cursory hacking this morning on my Ubuntu machine.

I look forward to doing some more testing and getting this stuff merged in when I have more time. Thanks again for all this work @xloem!

xloem commented 3 years ago

pip3 install bleak and -b bleak. EDIT: Oh, you're on Ubuntu! I'll plan to test on Ubuntu somehow and get it to work. I'm on RHEL7. Could you paste your failing output next time to make it easier for me to keep working off where you left off?

xloem commented 3 years ago

I ran the live cd for Ubuntu 20.04.1 LTS (Focal Fossa) in a VM, forwarded my internal USB bluetooth device, turned on my Muse S (maybe it only works on the Muse S =S...), and did this:

sudo add-apt-repository universe
sudo apt-get install python3-pip git
git clone -b bleak https://github.com/xloem/muse-lsl
cd muse-lsl
sudo pip3 install .
sudo python3 -m muselsl stream -b bleak

I still use sudo because this PR doesn't include the other one that passes the backend to the scanner, so it still scans with pygatt. My output from the last command (sorry if there is a typo, hand-copying from VM livecd terminal):

Searching for Muses, this may take up to 10 seconds...
Found device MuseS-1C52, MAC Address 00:55:DA:BB:1C:52
Connecting to MuseS-1C52: 00:55:DA:BB:1C:52...
Connected.
Streaming EEG...

I'll rebase this onto master soon. I also see on shutdown there's a "Set changed size during iteration" error I should fix, on backends.py:28 .

ErikBjare commented 3 years ago

Just tested this and it works great (Arch Linux).

Although it still asks for password when looking for devices (as @xloem mentioned). I side-stepped the issue by simply forcing the code in list_muses to use the bluetoothctl approach (which should probably be the default if bluetoothctl is available on the system?).

jdpigeon commented 3 years ago

This PR looks very promising: could this potentially replace pygatt or even BlueMuse?

Glad to hear it's working on Linux. When I have more time I will give this a test on windows and Mac.

xloem commented 3 years ago

I believe it could replace all the other backends if desired. I have only tested on linux myself (and not with the new state of the tree after all your great merges), so there might be quirks that need to be addressed to make that real.

ErikBjare commented 3 years ago

Me and @mrksbrg just tested it on Windows, and it seems to work there too! (which is great, since we were having issues with BlueMuse)

Would appreciate a speedy merge @jdpigeon :)

ErikBjare commented 3 years ago

One small issue I found: It doesn't seem to disconnect the Muse from Bluetooth when the streaming/program stops, leaving it connected indefinitely which makes it impossible to reconnect with muselsl.stream until the Muse is restarted by hand.

Edit: Nevermind, seems it was my wrapper around muse-lsl which prevented a graceful shutdown.

xloem commented 3 years ago

When I made this I tried to make the changes kind of conservative, to not alter the codebase much. I might be able to eventually find time some day to refactor muse-lsl to integrate bleak better, if that's desired. Please ask me if you want that done.

Personally I have been working with the brainflow project myself a little more, which unifies many drivers under one hood. We just got android client functionality for the openbci cyton dongle and other ftdi-based boards implemented.

danielemarinazzo commented 3 years ago

thanks! @xloem , are you able to see the AUX channel with a Muse 2, or do you confirm as @kowalej wrote in the Bluemuse readme that is not possible to plug an extra micro usb electrode into the charger as aux channel?

xloem commented 3 years ago

(edit: removed reply to outdated comment)

As mentioned in other links, I get an aux channel on my Muse S which is a Muse 2, in the bluetooth protocol transmissions, but I do not have an aux probe to test actual data.

danielemarinazzo commented 3 years ago

Great, thanks!

I am now trying it on Ubuntu but I get the following error

ImportError: this version of pandas is incompatible with numpy < 1.15.4
your numpy version is 1.13.3.

while in fact the numpy version is 1.20

xloem commented 3 years ago

could maybe auxiliary requests for assistance tag me in an issue to not pollute the PR too much? it's pretty clear the maintainers are strapped for capacity merging this.

the error you report doesn't seem related to this PR: it had to find that numpy version number from somewhere, so there is likely a rogue numpy install somewhere in your system. you might be in or not in a virtualenv, thinking the opposite. otherwise the path would be to diagnose whatever is importing pandas and see what generates that number.

xloem commented 2 years ago

@alexandrebarachant @jdpigeon How does this PR stand with you guys? Are you still available to engage in maintenance on this repo?

Personally, it can be very hard for me to engage this work, so it's good to know what the best changes to make are. Would this be more mergeable if I changed more of the codebase to rely on bleak as a single backend?

I ask because my fork at https://github.com/xloem/muse-lsl has been getting a little dev activity from other people, but not enough to really consider it a replacement or anything. It would be great if our codebases could merge somehow. [edit: looks like buyuk-dev got their changes into here, which is great]

jdpigeon commented 2 years ago

Hey @xloem, I agree completely that merging our two codebases would be best. I let this languish because I was putting off getting my old dusty muse out of storage and trying it out first hand. However, at this point I think it'd be fair to give it a trust stamp and bring it into muse-lsl.

We should probably make it the default backend, eh? IIUC, It's the safest option cross-platform? With a version number bump and the update to making it default I'd be happy to merge and push to pypi.

xloem commented 2 years ago

@jdpigeon This sounds great.

I've changed helpers.py to always default to bleak. I'm not sure whether anybody has tested on macos, although it may be an upstream issue with bleak if a platform doesn't work.

It looks like organisation-wise helpers.py should maybe merge with my backends.py for now, but maybe it's more important to merge this very old PR, if it's good.

ErikBjare commented 2 years ago

Awesome, really happy to see this merged!

Thank you @xloem and @jdpigeon!