Multiple MetaMotionC connection Error

Good day
Ive managed to connect to one sensor and steam euler angles. I'm currently trying to connect the second sensor...I get an error
"Segmentation fault" when i run the "read()" in my code.

please see code for reference and help out where possible.

usage: python scan_connect.py

from future import print_function
from mbientlab.metawear import MetaWear
from mbientlab.metawear.cbindings import *
from mbientlab.warble import *
from mbientlab.metawear import *

from datetime import *
import json, csv
import sys,platform,six
from time import sleep

try:
from StringIO import StringIO

except ImportError:

from io import StringIO

I hard coded the address in, just not displaying it here

device = MetaWear("XX:E2:XX:C5:60:XX")
device.connect()
sensor_1 = device.board

output1= StringIO()

'''
device = MetaWear("C6:XX:XX:27:XX:04")
device.connect()
sensor_2 = device.board
output2 = StringIO()
'''

buf = [100]

_______________________________________________________________________

def disconnectS(b):

libmetawear.mbl_mw_sensor_fusion_stop(sensor_1)
libmetawear.mbl_mw_sensor_fusion_clear_enabled_mask(sensor_1)
libmetawear.mbl_mw_datasignal_unsubscribe(euler_signal)
device.disconnect()
print ('disconnected')
sleep(1)

_______________________________________________________________________

def csvLog(but):

fin = open("log.csv","rt")
updateData = fin.read() 
oldData = updateData
updateData = updateData.replace(updateData,buf[0:89])
fin.close()

updateData = updateData.replace(" ","")
updateData = updateData.replace("{","")
updateData = updateData.replace("}"," ")
updateData = updateData.replace(" ","")
updateData = updateData.replace("epoch:","")
updateData = updateData.replace("heading:","")
updateData = updateData.replace("pitch","")
updateData = updateData.replace("roll:","")
updateData = updateData.replace("yaw:","")
updateData = updateData.replace(":","")

fin = open("log.csv","wt")
fin.write(updateData)
fin.close()

fin = open("log.csv","rt")
updateData = fin.read()
fin.close()

_______________________________________________________________________

def swtichOnOff(b):

 libmetawear.mbl_mw_sensor_fusion_start(b)
 sleep(0.3)
 libmetawear.mbl_mw_sensor_fusion_stop(b)

def read(b):

global euler_signal

euler_signal = libmetawear.mbl_mw_sensor_fusion_get_data_signal(b, SensorFusionData.EULER_ANGLE)    
euler_callback = FnVoid_VoidP_DataP(lambda context, data: output1.write("epoch:%s,%s\n" % (data.contents.epoch, parse_value(data))))

libmetawear.mbl_mw_datasignal_subscribe(euler_signal, None, euler_callback)
libmetawear.mbl_mw_sensor_fusion_enable_data(b, SensorFusionData.EULER_ANGLE)
libmetawear.mbl_mw_sensor_fusion_set_mode(b, SensorFusionMode.NDOF)
libmetawear.mbl_mw_sensor_fusion_write_config(b)

_______________________________________________________________________

try:
#ERRRRRRRRRRRROR

read(sensor_1)

while True:

    swtichOnOff(sensor_1)               
    buf = str(output1.getvalue())
    output1 = StringIO()

    csvLog(buf)

    with open('log.csv') as csvfile:

        readcsv = csv.reader(csvfile,delimiter=',')
        for row in readcsv:
            timeStamp = row[0]
            pitch = row[2]
            roll = row[3]
            yaw = row[4]
            break

    print (pitch,roll,yaw)
disconnectS(sensor_1)

except:

disconnectS(sensor_1)

_______________________________________________________________________

Comments

  • edited February 2020

    format your code. we cannot read this.

  • usage: python scan_connect.py

    from future import print_function
    from mbientlab.metawear import MetaWear
    from mbientlab.metawear.cbindings import *
    from mbientlab.warble import *
    from mbientlab.metawear import *
    from datetime import *
    import json, csv
    import sys,platform,six
    from time import sleep
    try:
    from StringIO import StringIO
    except ImportError:
    from io import StringIO

    I hard coded the address in, just not displaying it here

    device = MetaWear("XX:E2:XX:C5:60:AA")
    device.connect()
    device1 = MetaWear("XX:81:2C:27:XX:04")
    device1.connect()
    sensor_1 = device.board
    sensor_2 = device2.board
    output = StringIO()
    buf = [100]

    _______________________________________________________________________

    def disconnectS(b):
    libmetawear.mbl_mw_sensor_fusion_stop(sensor_1)
    libmetawear.mbl_mw_sensor_fusion_clear_enabled_mask(sensor_1)
    libmetawear.mbl_mw_datasignal_unsubscribe(sensor_1_signal)
    device.disconnect()
    print ('disconnected')
    sleep(1)

    _______________________________________________________________________

    def csvLog():
    fin = open("log.csv","rt")
    updateData = fin.read()
    oldData = updateData
    updateData = updateData.replace(updateData,buf[0:89])
    fin.close()
    updateData = updateData.replace(" ","")
    updateData = updateData.replace("{","")
    updateData = updateData.replace("}"," ")
    updateData = updateData.replace(" ","")
    updateData = updateData.replace("epoch:","")
    updateData = updateData.replace("heading:","")
    updateData = updateData.replace("pitch","")
    updateData = updateData.replace("roll:","")
    updateData = updateData.replace("yaw:","")
    updateData = updateData.replace(":","")
    fin = open("log.csv","wt")
    fin.write(updateData)
    fin.close()
    fin = open("log.csv","rt")
    updateData = fin.read()
    fin.close()

    _______________________________________________________________________

    def swtichOnOff(b):
    libmetawear.mbl_mw_sensor_fusion_start(b)
    sleep(0.3)
    libmetawear.mbl_mw_sensor_fusion_stop(b)

    _______________________________________________________________________

    try:
    sensor_1_signal = libmetawear.mbl_mw_sensor_fusion_get_data_signal(sensor_1, SensorFusionData.EULER_ANGLE)
    euler_callback = FnVoid_VoidP_DataP(lambda context, data: output.write("epoch:%s,%s\n" % (data.contents.epoch, parse_value(data))))
    libmetawear.mbl_mw_datasignal_subscribe(sensor_1_signal, None, euler_callback)
    libmetawear.mbl_mw_sensor_fusion_enable_data(sensor_1, SensorFusionData.EULER_ANGLE)
    libmetawear.mbl_mw_sensor_fusion_set_mode(sensor_1, SensorFusionMode.NDOF)
    libmetawear.mbl_mw_sensor_fusion_write_config(sensor_1)
    while True:
    swtichOnOff(sensor_1)
    buf = str(output.getvalue())
    output = StringIO()
    csvLog()
    with open('log.csv') as csvfile:
    readcsv = csv.reader(csvfile,delimiter=',')
    for row in readcsv:
    timeStamp = row[0]
    pitch = row[2]
    roll = row[3]
    yaw = row[4]
    break
    print (pitch,roll,yaw)
    disconnectS(sensor_1)
    except:
    disconnectS(sensor_1)

    _______________________________________________________________________

  • @Laura said:
    format your code. we cannot read this.

    Does that help?

  • its worse, maybe include it in a file?

  • The moment i paste it on the forum it "reformat" cant attache a file either

  • You could link to a file...

  • @Laura said:
    You could link to a file...

    see below code:

    When i run the code a get a stream of the euler angles. See below for results.

    line 26. where im capturing the data into the "euler_callback" variable and print the type of variable on line 33 the result comes out as a . while at first i thought its a dictionary.

    I would like to extract the data i get( timestamp,heading,pitch,roll,yaw) into seperate variable that i can use for my application

    EG.

    while True:
    sleep(1)
    x = euler_callback['heading']
    print (x)

  • Please get more comfortable using Python. Clearly this is simply a case of python dev limitations and not a bug with our SDK.
    Also read our tutorial sections on the data formats.

  • @Laura said:
    Please get more comfortable using Python. Clearly this is simply a case of python dev limitations and not a bug with our SDK.
    Also read our tutorial sections on the data formats.

    Im not saying its a but at all i've been trying all possible methods i know of and nothing has worked so far. if you would kindly lead me in the right direction

  • The best method ive tried was to log the angles on a csv file but that reduced the the number of sambles i could get to 300ms which slowed down my system drastically, I've alos tried to use string IOs to print the data into a var, which worked but constantly had to stop and start the sensor connection with a dealy of (0.3sec) in between which reduced the number of samples im reading

  • edited March 2020

    I think you should read the function: "parse_value" and see how it extracts the data from the callback (from the sensor bluetooth stream). data is a pointer to the sensor data and it needs to be parsed correctly.

    Please see some examples here: https://github.com/mbientlab/MetaWear-SDK-Python/search?p=1&q=parse_value&unscoped_q=parse_value

  • @Laura said:
    I think you should read the function: "parse_value" and see how it extracts the data from the callback (from the sensor bluetooth stream). data is a pointer to the sensor data and it needs to be parsed correctly.

    Please see some examples here: https://github.com/mbientlab/MetaWear-SDK-Python/search?p=1&q=parse_value&unscoped_q=parse_value

    Hi, thank you so much !

  • @Aubrey_1 said:
    line 26. where im capturing the data into the "euler_callback" variable and print the type of variable on line 33 the result comes out as a . while at first i thought its a dictionary.

    I would like to extract the data i get( timestamp,heading,pitch,roll,yaw) into seperate variable that i can use for my application

    EG.

    while True:
    sleep(1)
    x = euler_callback['heading']
    print (x)

    Couple of things...

    euler_callback is a callback function, as you have noted; it doesn't return anything. All the post processing needs to be done in the callback function so your while loop is nonsensical.

    As Laura noted, parse_value extracts the actual value from the data pointer. The code in calibrate.py is somewhat similar to what you are trying to do.

    Lastly, the Python code bridges into the C side with the ctypes module. Reading the docs on that module will help better understand the sample code.
    https://docs.python.org/3/library/ctypes.html

  • @Eric said:

    @Aubrey_1 said:
    line 26. where im capturing the data into the "euler_callback" variable and print the type of variable on line 33 the result comes out as a . while at first i thought its a dictionary.

    I would like to extract the data i get( timestamp,heading,pitch,roll,yaw) into seperate variable that i can use for my application

    EG.

    while True:
    sleep(1)
    x = euler_callback['heading']
    print (x)

    Couple of things...

    euler_callback is a callback function, as you have noted; it doesn't return anything. All the post processing needs to be done in the callback function so your while loop is nonsensical.

    As Laura noted, parse_value extracts the actual value from the data pointer. The code in calibrate.py is somewhat similar to what you are trying to do.

    Lastly, the Python code bridges into the C side with the ctypes module. Reading the docs on that module will help better understand the sample code.
    https://docs.python.org/3/library/ctypes.html

    Appreciate the responce Eric currently working through the solution.

  • @Eric & @Laura

    I would like to say thank you for your assistance with regard to the above issue i had. with you assistance i manged to figure out how save the variables as my application requires. Thank you for taking the time to help me.

    :smiley:

  • Hi @Laura & @Eric

    I'm getting the following two errors when i run the peice of code in the attached file (text.txt)
    Error 1:

    error 1586435361.351481: Error on line: 296 (src/blestatemachine.cc): Operation now in progress
    *** buffer overflow detected ***: python3 terminated
    Aborted

    Error 2:

    Failed to write value to characteristic(Error trying to issue command mid state)

    See attached images for further clarity

    These errors occur occasionally as most of the time i do log the data, but somewhere at a curtaion point the logs stop and when i quit the program the Error 2 occurs, while Error 1 occurs when i try to reconnect

    Your assistance is highly appreciated


Sign In or Register to comment.