FP16 to float conversion

Firmware/software/electronics/mechanics
Post Reply
Naidala
Beginner
Posts: 14
Joined: Fri Dec 11, 2020 9:10 am

FP16 to float conversion

Post by Naidala »

Hi, I'd like to know how to convert FP16 to its float value.
I'm logging values as in https://github.com/bitcraze/crazyflie-l ... py#L75-L84, but I'd like to print (and store) the floating value of 'pm.vbat' (or pm.batteryLevel), in order to use them inside an IF to make the CF land under a certain threshold.

By the way, what threshold would you advise to set?

I have already tried as in viewtopic.php?t=1835 with

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]
and also with

Code: Select all

struct.Struct('e').unpack_from(data)
as in https://github.com/bitcraze/crazyflie-l ... issues/227, but nothing is working.

I would really appreciate your help, Thanks!
jonasdn
Expert
Posts: 132
Joined: Mon Mar 01, 2021 3:13 pm

Re: FP16 to float conversion

Post by jonasdn »

Hi Naidala!

Have you tried using the code in cflib/utils/fp16.py: https://github.com/bitcraze/crazyflie-l ... ls/fp16.py?

Regards
Jonas
Post Reply