FP16 in Log

Firmware/software/electronics/mechanics
Post Reply
3zuli
Beginner
Posts: 11
Joined: Fri Oct 09, 2015 1:51 pm

FP16 in Log

Post by 3zuli »

Hi, I'm trying to use the FP16 data type for logging. Here's the log config setup I'm trying to use:

Code: Select all

self.imu_log = LogConfig(name="IMU", period_in_ms=30)
imu_type="FP16"
imu_log.add_variable("acc.x", imu_type)
imu_log.add_variable("acc.y", imu_type)
imu_log.add_variable("acc.z", imu_type)
imu_log.add_variable("gyro.x", imu_type)
imu_log.add_variable("gyro.y", imu_type)
imu_log.add_variable("gyro.z", imu_type)
#imu_log.add_variable("mag.x", imu_type)
#imu_log.add_variable("mag.y", imu_type)
#imu_log.add_variable("mag.z", imu_type)
However the data I get is just total gibberish, it makes no sense even if the crazyflie is sitting flat on a table.

Code: Select all

[27544][IMU]: {'gyro.y': -21552, 'gyro.x': 0, 'gyro.z': 0, 'acc.x': -24192, 'acc.y': 6400, 'acc.z': 15329}
[27574][IMU]: {'gyro.y': 0, 'gyro.x': 12764, 'gyro.z': 11216, 'acc.x': -24224, 'acc.y': 6272, 'acc.z': 15327}
[27604][IMU]: {'gyro.y': 11216, 'gyro.x': 11216, 'gyro.z': -21552, 'acc.x': -24256, 'acc.y': 6528, 'acc.z': 15329}
If I set the type to FLOAT, it works fine, but I need to include more data in the packet (the commented-out lines) so I need to use the FP16. Variables acc.x,y,z, gyro.x,y,z and mag.x,y,z are left unchanged in stabilizer.c. If I understand correctly, in this case the log framework should be automatically translating the internal data type to FP16 before transmitting from the CF, and then converting back to float upon receiving by the python library. But there seems to be a problem with that.
Also, I'm using the multilink branch from here viewtopic.php?f=9&t=624&p=7919&hilit=multilink#p7919 since in the future I'll want to communicate with multiple crazyflies. As that fork hasn't been updated in quite a while, is it possible that this has been fixed in the official branch?
Edit: I just tried it with the newest official master branch and the results are the same.
Can anybody help me?
jjan3640
Beginner
Posts: 8
Joined: Tue Mar 30, 2021 4:18 am

Re: FP16 in Log

Post by jjan3640 »

Hey, you probably don't need this anymore because it's been a while but I'll just post this here for anyone else who is confused about the weird values being recorded using FP16 to log.

Using this stackoverflow post (https://stackoverflow.com/questions/563 ... l-float-an) I was able to convert the FP16 hex values to FP32 which made them more realistic, matching the values shown on the plotter of the cfclient.

Code: Select all

import struct
import numpy as np

hexVal = hex(fp16Value)
y = struct.pack("H", int(hexVal, 16))
floatVal = np.frombuffer(y, dtype=np.float16)[0]
arnaud
Bitcraze
Posts: 2538
Joined: Tue Feb 06, 2007 12:36 pm

Re: FP16 in Log

Post by arnaud »

Hi,

The latest master of the lib does support FP16 out of the box, this example demonstrate the functionality: https://github.com/bitcraze/crazyflie-l ... log.py#L84. Since python 3.6, Python actually supports fp16 out of the box in struct.pack/unpack so numpy is not needed anymore :).

As for the multilink branch, this is a very old branch that should not be used anymore. For a long wile the Crazyflie lib now supports multiple Crazyflies using one or many radio. The connection uri "radio://0/80/2M/E7E7E7E7E7" is "radio://<radio_id>/<channel>/<datarate>/<address>", each Crazyflie should have a different address, and each crazyradio should be on a different channel and preferably connect all Crazyflie on the same channel. radio_id can be either a number between 0 and (amount_of_radio - 1) of a Crazyradio serial number if you want to allocate some Crazyflie to a specific radio.
Post Reply