DroneCAN consumed charge value is calculated incorrectly in PX4

Background:

For context I am working on implementing DroneCAN protocol for a custom battery management device. I have included the libcanard C libraries into our STM32 Cube project GitHub - dronecan/libcanard · GitHub

I have our device connected to the Pixhawk6x (PX4 Pro V1.17.0) over CAN1, using Qgroundcontrol to view the battery status transmitted by PX4 to QGC over MAVLink.

Image 1: PX4 UAVCAN parameters

Image 2: PX4 Battery Calibration Parameters

Test:

Running our custom board (BMS) for 1 hour, with 1.29 A. Transmitting battery status messages to the pixhawk over MAVLink (TELEM1) and DroneCAN (CAN1).

Results:

The PX4 seems to calculate an incorrect value for consumed current.

Image 4: Test results - 1 hour operation

Discussion:

I have found some information on how the consumed current may be calculated for a power module in the docs >en>config>battery.md which indicates that voltage and load compensation are factors in the equation. However it is not clear if or how this works with a DroneCAN battery info message

I was also able to find a line where the discharged_mah is calculated in Line 114 src\drivers\cyphal\subscribers\udralbattery.hpp

However its not clear where the values for energy and nominal voltage are coming from.

Image 5: Calculated dischared mah

Questions:

Does anyone else have a similar issue? and does this seem to be a bug in the PX4 firmware or is it expected behavior and an error in my configuration of UAVCAN and battery calibration parameters? or a misunderstanding of how this is supposed to function?

SOLVED:

I have discovered the source of the error. In the battery.cpp file, the time delta is limited to a max of 2 seconds, and the current is apparently only integrated on new reception. Therefore the current will be integrated incorrectly for a battery info message with a period greater than 2 seconds.

Changing the tx interval to 2 seconds solves the issue.

File: src/lib/battery/battery.cpp

Line: 213