Unable to connect to MetaMotionR devices using Raspberry Pi

Hello,

My task is to connect 8 MetaMotionR devices to the MetaHub and collect sensor data simultaneously using Metawear Python SDK along with the pygattlib library. I'm using a Bluetooth dongle to connect to the devices.The program that I am developing has two data collection modes:

  1. collect raw data from all devices (accelerometer, gyro-meter, and magnetometer)
  2. collect Euler angles from all devices (sensor fusion)

The task seems simple but the unpredictable behavior of the MetaMotionR devices and the Bluetooth dongle has made it almost impossible for me to connect all 8 devices (not even 4 devices) and collect readings at the same time. My program has 4 stages:

  1. Discover nearby devices
  2. connect to selected devices
  3. collect data from sensors
  4. Disconnect devices

Most of the errors occur at the second stage of the program, specifically when trying to connect to a device using the method device.connect(), where device is an instance of the class MetaWear. When running the program there is always a big probability that an error will occur while connecting to one of the devices. The errors that I discovered are:

  1. Segmentation Fault
  2. *** Error in ‘python’: double free or corruption (fasttop): 0x75c0ad10 *** Aborted
  3. RuntimeError: device not responding
  4. RuntimeError: Error initializing API




Error 3 has a trace back to file _ctypes/callbacks.c (line 315 in ‘calling callback function’) and to file mbientlab/metawear/__init__.py (line 198 in _read_gatt_char).

At stage 3 of the program, a ‘RuntimeError: channel attribute not ready’ error occurred while collecting data.

Regards,

Amr

Comments

  • edited January 2018
    Please post the python script you are currently running.  It sounds like you should split your devices across multiple dongles, perhaps 2-3 max per dongle.  Also, if you have the time, perhaps running a NodeJS version of the script will yield better results.

    Some other things to consider:
    • Does your script work at all when using 3 or less devices?  
    • Does the example accelerometer script work with multiple devices?
    • How fast are your sampling the sensors?
  • Here is a link to the python script:
    The errors occur mainly at line 119.

    I have tried splitting the devices on multiple dongles but that raised more errors. My approach was something along those lines:
    address = devices.items()[selection][0]
    print("Connecting to %s..." % (address))
    if len(connectedDevices) <= 3:
        device = MetaWear(address, device = "hci0")
    else:
        device = MetaWear(address, device = "hci1")
    device.connect()
    connectedDevice.append(device)

    In many of my runs, I successfully connected upto four devices to the same dongle and collected euler angles at 100 Hz from each device. Then I tried collecting raw data from the four sensors (Acc 100Hz, Gyro 100Hz, and Mag 20Hz) but there was too much missing data from the sampling files. This probably happened because I run the script with high sampling frequencies using one bluetooth dongle. It would be very grateful if you can show me a better way to connect multiple dongles.

    The accelerometer example script (multi_device.py) usually runs successfuly when it is not interupted by the errors raised by device.connect() method. My program is very much based on the methods used in both example scripts multi_device.py and scan_connect.py.

    I am currently trying to run a similar program using NodeJS to see if it will connect better.

    Thanks,
    Amr
  • See this section in the documentation for high frequency streaming:

    The code looks fine in terms of using multiple adapters.  Can you at least stream from 2 devices using 2 adapters?
  • Hello Eric,
    I edited the program and added the multiple dongle code that I suggested above in paragraph two then I ran several tests on two sensors. I recognized that only one of the dongles was used by the Raspberry Pi while the other one was completely ignored. Only one of the dongles was flashing its LED bulb everytime I run a test. Three out of ten tests where successful without any errors (runtime, segmentation fault, fasttop, etc).

    I have been working on this project for the past two weeks and I noticed that when I turn on the Raspberry Pi while it is connected to two Bluetooth adapters, the Raspberry Pi gets confused and doesn't recognize both adapters. I usually end up disconnecting one of the adapters to connect to the other. If I then try to connect the second adapter again it never flashes when I run -> device = metawear(address1, device = "hci0") and -> device = metawear(address2, device = "hci1").

    I wonder what is happening.

  • I did not have any issues, other than the usual connect exception, when doing some quick tests with 2 adapters.  I modified the multi_device.py example to toggle between 'hci0' and 'hci1' and the script was working fine.

    use_snd_adapter = False
    for i in range(len(syst.argv) - 1):
        d = Metawear(sys.argv[i + 1], device = 'hci1' if use_snd_adapter else 'hci0')
        states.append(State(d))
        use_snd_adapter = not use_snd_adapter

    Did you by chance power up the Pi with the second adapter already plugged in or did you plug it in after the OS booted?
  • Hello Eric,

    I ran some tests in both cases. First I plugged in the two adapters and then I powered up the Raspberry Pi. There were two errors that I encountered:

    (1) --> service = DiscoveryService("hci0")
          --> RuntimeError: Invalid Device!
    This error means that the rPi doesn't identify any of the adapters as hci0. I then used DiscoveryService("hci1") instead. This solved the error but raised a new error.

    (2) --> device = Metawear(address, device = 'hci1' if use_snd_adapter else 'hci0')
          --> self.gatt - _pyBlueZGatt(address, "hci0" if 'device' not in kwargs else kwargs['device'])
          --> GaTTRequester.__init__(self, address, False, device)
          --> RuntimeError: Invalid Device!
    I think that both those errors imply that the rPi identifies only one of the adapters when powered up while both adapters are connected.

    For the second case, I unplugged one of the adapters then rebooted the rPi. After the OS booted, I plugged the second adapter again and was finally able to use both adapters.

    None of the above two errors occurred but I am still encountering connection errors like "RuntimeError: device not responding!", "GLib-CRITICAL **: Source ID was not found when attempting to remove it", and etc. 

    Is this caused by the devices/sensors or the used Bluetooth library? is there a solution?

    Thanks
    Amr
  • You'll have to check hciconfig to confirm which adapters are running.  In my case, both the integrated and USB adapters were working just fine and assigned 'hci0' and 'hci1' respectively.

    You are probably better off writing your app with NodeJS as, in my experience, noble is a far better BLE library than anything available for Python.
  • Amr,

    The error you are getting is from the low level Bluetooth link code running on the Pi. It is not coming from the sensors.
    Are all your packages and dependencies up to date?
    Can you use hci tools to sniff the BLE link layer to see what is happening?

  • Hello Laura,

    I checked to see if the packages and dependencies are up to date and I got this:

      -> using $ pip show ...
    metawear 0.3.1
    pygatt 3.2.0
            pybluez 0.22
    gattlib 0.20171002

      -> using $ sudo pip show ...
    metawear 0.3.1
    gattlib 0.20171002

    I also checked the latest packages on https://pypi.python.org/ and It seems that all the dependencies/packages are up to date. There are still errors that occur randomly like "GLib-CRITICAL", "Device is not responding!", "Error initializing API", and etc. I tried using a second Raspberry Pi with the newest Raspbian Stretch OS and all the necessary packages/dependencies, but the errors are still occurring.

    I am not that familiar with hcitool operations since my program runs completely on python. If the procedure you mentioned above will help minimize the chances of error, It would be really grateful if you can let me know the necessary steps to implement this procedure.

    As Eric advised me, I tried to install the metawear module that runs on nodeJS but It was not installed successfully due to several errors that occurred during the installation. Here is a document detailing the procedure that I followed:

    The complete log of the installation in step 15:

    Please check step 7 to step 16 and see if there is something that I am missing and how to fix the issue.

    thanks,
    Amr
  • Use node v8.4.0 not v9.
  • I installed Node v8.4.0 using nvm. Then I proceeded to install the metawear module. During the installation of the module the box at the upper-right corner jumps to 100% and the screen freezes all of the sudden.


    when this first occurred the red led on the Pi was flashing and the bolt icon at the upper-right corner kept occurring. This didn't happen again after I used another usb cable to power the Pi, but the screen still freezes whenever I try to install the module.

    Please help.
  • Try increasing the swapfile size and install the module from the terminal, not in a desktop environment.
This discussion has been closed.