This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Extracting Int values from Ascii strings received for motion data[rawdata][RPi][Python3]

I am able to receive motion data [rawdata] from thingy52 on my RPi 4 using bluepy library in Python3.

I want to extract the raw values of Gyro/Accl/Mag from sensor.

The data is received as byte-strings:

Notification: Rotation matrix: b'\xb4\xd9\xe8+}\x1a\xcf.n+\xae\xfb\x0f\xeb\xca\x10\xe6\xc5'

Based on the documentation, the length of byte-string matches the data size(18 bytes), but where can I find the representation of this data?

How can I recover values in a format suitable for plotting?

Thanks

Parents
  • There's some information missing here, like what exact register you're reading or what documentation you've found the '18 bytes' information in. However scanning the documentation of the mpu9250, which is the chip in the Thingy:52, it says 

    "Measurement data is stored in two’s complement and Little Endian format. Measurement range of each axis is from -32760 ~ 32760 decimal in 16-bit output."

    So that's 2 bytes per measurement, or 6 bytes for a complete XYZ sequence, so I'm guessing your 18 bytes are XYZ for gyro/accel/mag in which case you just need python unpack to unpack them

    import struct
    a = b'\xb4\xd9\xe8+}\x1a\xcf.n+\xae\xfb\x0f\xeb\xca\x10\xe6\xc5'
    struct.unpack("<hhhhhhhhh",a)
    
    --> (-9804, 11240, 6781, 11983, 11118, -1106, -5361, 4298, -14874)
    

Reply
  • There's some information missing here, like what exact register you're reading or what documentation you've found the '18 bytes' information in. However scanning the documentation of the mpu9250, which is the chip in the Thingy:52, it says 

    "Measurement data is stored in two’s complement and Little Endian format. Measurement range of each axis is from -32760 ~ 32760 decimal in 16-bit output."

    So that's 2 bytes per measurement, or 6 bytes for a complete XYZ sequence, so I'm guessing your 18 bytes are XYZ for gyro/accel/mag in which case you just need python unpack to unpack them

    import struct
    a = b'\xb4\xd9\xe8+}\x1a\xcf.n+\xae\xfb\x0f\xeb\xca\x10\xe6\xc5'
    struct.unpack("<hhhhhhhhh",a)
    
    --> (-9804, 11240, 6781, 11983, 11118, -1106, -5361, 4298, -14874)
    

Children
  • Thanks for the pointer, I did figured it out from the nodejs sample for data-type format.

    Your method is correct and I just used a different method, Python 3 has built-in method with int.


    a = b'\xb4\xd9\xe8+}\x1a\xcf.n+\xae\xfb\x0f\xeb\xca\x10\xe6\xc5'

    int_data = [int.from_bytes(a[i:i+2],'little',signed = True) for i in range(0,len(a),2)]

    Again Thanks for explaination!!

Related