vedderb / bldc

The VESC motor control firmware
2.14k stars 1.34k forks source link

Incorrect temperature in ESC status in UAVCAN mode #169

Open jxltom opened 4 years ago

jxltom commented 4 years ago

In https://github.com/vedderb/bldc/blob/b460fa0fa3cb507a14aef26082d782f76b30d7db/libcanard/canard_driver.c#L119, the returned temperature is always 298.0000 which looks like precesion is lost.

Even I changed code to status.temperature = 24.9219 + 273.15; in bldc, the parsed temperature in https://github.com/UAVCAN/gui_tool is still 298.0000

Here is the screenshot in gui_tool.

image

Maybe something wrong with canardConvertNativeFloatToFloat16?

@pavel-kirienko do you mind also having a look? Thanks.

jxltom commented 4 years ago

Change to float32 is fine. It looks like using float16 is overflow or something like that for big numbers such as 280.

pavel-kirienko commented 4 years ago

At this exponent, the step is around 0.2:

>>> import numpy
>>> numpy.nextafter(numpy.float16(298), numpy.float16(1000))
298.2

Whereas your value falls closer to 298:

>>> 24.9219 + 273.15
298.07189999999997

The behavior you are observing is correct.

jxltom commented 4 years ago

Thanks for the clarification!

So the precision of float16 for numbers such has 298 is around 0.2 or bigger.

Since uavcan is using Kelvin and motor temperature is certainly larger than 273.15, maybe it makes sense to change the definition of uavcan.equipment.esc.Status.temperature to float32, otherwise the digit 0.05 in 273.15 will definitely be lost. float32 doesn't have this issue.

>>> import numpy
>>> numpy.nextafter(numpy.float16(273.15), numpy.float16(numpy.inf))
273.5
>>> numpy.nextafter(numpy.float32(273.15), numpy.float32(numpy.inf))
273.15002