Skip to content

Conversation

MaEtUgR
Copy link
Member

@MaEtUgR MaEtUgR commented Jun 3, 2025

Solved Problem

When tracking battery use across multiple flights and vehicles using the serial number from flight logs I found that:

  1. The serial number, that we currently have in battery status is a uin16_t which is too limiting for my use case and likely also others. I found that this periodic battery DroneCAN message
    and the BATTERY_INFO MAVLink message have settled on a char[32].
    I asked in a thread about this here: Common: rename SMART_BATTERY_INFO to BATTERY_INFO and add SOH mavlink/mavlink#2070 (comment)
  2. The serial number of a battery doesn't change so it should not be logged at high rate. This gets more important with a char[32] type because of the wasted space. We need a low rate battery info message for all such static information.

Solution

  1. Add a battery info uORB message and have the id field match the value of battery_status.id to correctly keep track across topic instances.
  2. Have a char[32] battery_info.serial_number field for the unique battery identifier and adjust the upstream drivers to use it.

Other static fields I plan to sort out and move to the battery_info message separately from this pull request.

Changelog Entry

Introduce battery_info uORB message for static data like the serial number

Test coverage

EDIT: I just successfully tested this latest version with a UAVCAN Tattu battery (I'll make a separate pr for the message parsing of that one):
image

@MaEtUgR MaEtUgR self-assigned this Jun 3, 2025
Copy link

github-actions bot commented Jun 3, 2025

🔎 FLASH Analysis

px4_fmu-v5x [Total VM Diff: 712 byte (0.04 %)]
    FILE SIZE        VM SIZE    
--------------  -------------- 
+0.0%    +720  +0.0%    +720    .text
  +264%    +116  +264%    +116    MavlinkStreamBatteryInfo::new_instance()
   +21%     +68   +21%     +68    UavcanBatteryBridge::battery_sub_cb()
   +40%     +64   +40%     +64    UavcanBatteryBridge::filterData()
  [NEW]     +64  [NEW]     +64    orb_publish_auto.isra.0
  [NEW]     +56  [NEW]     +56    CSWTCH.1911
   +14%     +56   +14%     +56    UavcanBatteryBridge::UavcanBatteryBridge()
   +18%     +52   +18%     +52    MavlinkStreamBatteryInfo::send()
   +27%     +48   +27%     +48    uORB::PublicationMulti<>::publish()
  +8.0%     +44  +8.0%     +44    Batmon::RunImpl()
  [NEW]     +44  [NEW]     +44    CSWTCH.2689
  +6.5%     +40  +6.5%     +40    BATT_SMBUS::RunImpl()
  +1.1%     +36  +1.1%     +36    px4::logger::LoggedTopics::add_default_topics()
   +42%     +32   +42%     +32    SMBUS_SBS_BaseClass<>::RunImpl()
   +11%     +28   +11%     +28    Batmon::Batmon()
   +55%     +24   +55%     +24    MavlinkStreamBatteryInfo::~MavlinkStreamBatteryInfo()
   +18%     +24   +18%     +24    UavcanBatteryBridge::~UavcanBatteryBridge()
  +0.1%     +24  +0.1%     +24    uORB::compressed_fields
 -100.0%     +20 -100.0%     +20    [21 Others]
  -2.6%     -20  -2.6%     -20    ucdr_serialize_battery_status()
  [DEL]     -44  [DEL]     -44    CSWTCH.2685
  [DEL]     -56  [DEL]     -56    CSWTCH.1908
+0.0%    +893  [ = ]       0    .debug_abbrev
+0.0%     +64  [ = ]       0    .debug_aranges
+0.0%    +208  [ = ]       0    .debug_frame
+0.1% +17.1Ki  [ = ]       0    .debug_info
+0.0% +2.32Ki  [ = ]       0    .debug_line
 -75.0%      -3  [ = ]       0    [Unmapped]
  +0.0% +2.33Ki  [ = ]       0    [section .debug_line]
+0.0% +1.09Ki  [ = ]       0    .debug_loclists
+0.0%     +92  [ = ]       0    .debug_rnglists
+0.0% +1.67Ki  [ = ]       0    .debug_str
+0.4%      +1  [ = ]       0    .shstrtab
+0.0%    +143  [ = ]       0    .strtab
  [DEL]     -12  [ = ]       0    CSWTCH.1908
  [NEW]     +12  [ = ]       0    CSWTCH.1911
  [DEL]     -12  [ = ]       0    CSWTCH.2685
  [NEW]     +12  [ = ]       0    CSWTCH.2689
  [DEL]     -24  [ = ]       0    CSWTCH.3397
  [DEL]     -24  [ = ]       0    CSWTCH.3398
  [NEW]     +24  [ = ]       0    CSWTCH.3403
  [NEW]     +24  [ = ]       0    CSWTCH.3404
   +23%     +17  [ = ]       0    SMBUS_SBS_BaseClass<>::populate_smbus_data()
  +0.1%     +17  [ = ]       0    [section .strtab]
 -26.7%     -16  [ = ]       0    ___ZL19param_get_cplusplustPf.isra.0_veneer
   +80%     +16  [ = ]       0    __nxsem_wait_veneer
  [NEW]     +19  [ = ]       0    __orb_battery_info
  [NEW]     +24  [ = ]       0    orb_publish_auto.isra.0
   +18%     +66  [ = ]       0    uORB::PublicationMulti<>::publish()
+0.0%    +144  [ = ]       0    .symtab
  [DEL]     -32  [ = ]       0    CSWTCH.1908
  [NEW]     +32  [ = ]       0    CSWTCH.1911
  [DEL]     -32  [ = ]       0    CSWTCH.2685
  [NEW]     +32  [ = ]       0    CSWTCH.2689
  [DEL]     -48  [ = ]       0    CSWTCH.3397
  [DEL]     -48  [ = ]       0    CSWTCH.3398
  [NEW]     +48  [ = ]       0    CSWTCH.3403
  [NEW]     +48  [ = ]       0    CSWTCH.3404
 -50.0%     -16  [ = ]       0    MavlinkStreamAutopilotStateForGimbalDevice::send()
   +33%     +16  [ = ]       0    MavlinkStreamBatteryInfo::send()
   +17%     +16  [ = ]       0    MavlinkStreamUavionixADSBOutDynamic::get_name()
  +100%     +16  [ = ]       0    MavlinkStreamUavionixADSBOutDynamic::updateParamsImpl()
 -40.0%     +96  [ = ]       0    [2 Others]
 -11.8%     -32  [ = ]       0    ___ZL19param_get_cplusplustPf.isra.0_veneer
 -33.3%     -16  [ = ]       0    __net_lock_veneer
   +67%     +32  [ = ]       0    __nxsem_wait_veneer
  [NEW]     +48  [ = ]       0    __orb_battery_info
   +50%     +16  [ = ]       0    __pthread_mutex_lock_veneer
 -25.0%     -16  [ = ]       0    __pthread_mutex_unlock_veneer
   +33%     +16  [ = ]       0    __up_clean_dcache_veneer
  -7.7%     -32  [ = ]       0    events::send()
-6.9%    -712  [ = ]       0    [Unmapped]
-0.1%      -8  -0.1%      -8    .ramfunc
   +14%      +1   +14%      +1    __pthread_mutex_lock_veneer
 -12.5%      -1 -12.5%      -1    __net_lock_veneer
  -1.2%      -4  -1.2%      -4    Ekf::measurementUpdate()
 -16.7%      -4 -16.7%      -4    get_orb_meta()
+0.1% +23.7Ki  +0.0%    +712    TOTAL

px4_fmu-v6x [Total VM Diff: 512 byte (0.03 %)]
    FILE SIZE        VM SIZE    
--------------  -------------- 
+0.0%    +512  +0.0%    +512    .text
  +264%    +116  +264%    +116    MavlinkStreamBatteryInfo::new_instance()
   +21%     +68   +21%     +68    UavcanBatteryBridge::battery_sub_cb()
   +40%     +64   +40%     +64    UavcanBatteryBridge::filterData()
  [NEW]     +56  [NEW]     +56    CSWTCH.1911
   +14%     +56   +14%     +56    UavcanBatteryBridge::UavcanBatteryBridge()
   +18%     +52   +18%     +52    MavlinkStreamBatteryInfo::send()
   +27%     +48   +27%     +48    uORB::PublicationMulti<>::publish()
  [NEW]     +44  [NEW]     +44    CSWTCH.2689
  +1.1%     +36  +1.1%     +36    px4::logger::LoggedTopics::add_default_topics()
   +55%     +24   +55%     +24    MavlinkStreamBatteryInfo::~MavlinkStreamBatteryInfo()
   +18%     +24   +18%     +24    UavcanBatteryBridge::~UavcanBatteryBridge()
  +0.1%     +24  +0.1%     +24    uORB::compressed_fields
  [NEW]     +16  [NEW]     +16    __orb_battery_info
  [NEW]     +12  [NEW]     +12    CSWTCH.3403
  [NEW]     +12  [NEW]     +12    CSWTCH.3404
 -100.0%      +4 -100.0%      +4    [14 Others]
  [DEL]     -12  [DEL]     -12    CSWTCH.3397
  [DEL]     -12  [DEL]     -12    CSWTCH.3398
  -2.6%     -20  -2.6%     -20    ucdr_serialize_battery_status()
  [DEL]     -44  [DEL]     -44    CSWTCH.2685
  [DEL]     -56  [DEL]     -56    CSWTCH.1908
+0.0%    +857  [ = ]       0    .debug_abbrev
+0.0%     +48  [ = ]       0    .debug_aranges
+0.0%    +136  [ = ]       0    .debug_frame
+0.1% +16.0Ki  [ = ]       0    .debug_info
+0.0% +2.13Ki  [ = ]       0    .debug_line
+0.0%    +679  [ = ]       0    .debug_loclists
+0.0%    +113  [ = ]       0    .debug_rnglists
  [NEW]      +3  [ = ]       0    [Unmapped]
  +0.0%    +110  [ = ]       0    [section .debug_rnglists]
+0.0% +1.62Ki  [ = ]       0    .debug_str
-0.8%      -2  [ = ]       0    .shstrtab
+0.0%    +102  [ = ]       0    .strtab
  [DEL]     -12  [ = ]       0    CSWTCH.1908
  [NEW]     +12  [ = ]       0    CSWTCH.1911
  [DEL]     -12  [ = ]       0    CSWTCH.2685
  [NEW]     +12  [ = ]       0    CSWTCH.2689
  [DEL]     -24  [ = ]       0    CSWTCH.3397
  [DEL]     -24  [ = ]       0    CSWTCH.3398
  [NEW]     +24  [ = ]       0    CSWTCH.3403
  [NEW]     +24  [ = ]       0    CSWTCH.3404
  +0.1%     +17  [ = ]       0    [section .strtab]
  [NEW]     +19  [ = ]       0    __orb_battery_info
   +18%     +66  [ = ]       0    uORB::PublicationMulti<>::publish()
+0.0%     +80  [ = ]       0    .symtab
  [DEL]     -32  [ = ]       0    CSWTCH.1908
  [NEW]     +32  [ = ]       0    CSWTCH.1911
  [DEL]     -32  [ = ]       0    CSWTCH.2685
  [NEW]     +32  [ = ]       0    CSWTCH.2689
  [DEL]     -48  [ = ]       0    CSWTCH.3397
  [DEL]     -48  [ = ]       0    CSWTCH.3398
  [NEW]     +48  [ = ]       0    CSWTCH.3403
  [NEW]     +48  [ = ]       0    CSWTCH.3404
 -50.0%     -16  [ = ]       0    MavlinkStreamAutopilotStateForGimbalDevice::send()
   +33%     +16  [ = ]       0    MavlinkStreamBatteryInfo::send()
   +17%     +16  [ = ]       0    MavlinkStreamUavionixADSBOutDynamic::get_name()
  +100%     +16  [ = ]       0    MavlinkStreamUavionixADSBOutDynamic::updateParamsImpl()
  +0.3%     +32  [ = ]       0    [section .symtab]
  [NEW]     +48  [ = ]       0    __orb_battery_info
 -33.3%     -16  [ = ]       0    atanf
 -25.0%     -16  [ = ]       0    atanlo
  -7.7%     -32  [ = ]       0    events::send()
   +20%     +32  [ = ]       0    uORB::PublicationMulti<>::publish()
-11.1%    -512  [ = ]       0    [Unmapped]
+0.0% +21.7Ki  +0.0%    +512    TOTAL

Updated: 2025-06-16T17:33:18

@mrpollo
Copy link
Contributor

mrpollo commented Jun 3, 2025

This is something great to track down in Flight Review as well!

@MaEtUgR MaEtUgR force-pushed the maetugr/battery-info branch 2 times, most recently from a8622e2 to b9dd85e Compare June 3, 2025 15:55
@PX4 PX4 deleted a comment from github-actions bot Jun 3, 2025
@MaEtUgR MaEtUgR force-pushed the maetugr/battery-info branch from b9dd85e to 61c4478 Compare June 3, 2025 16:09
@PX4 PX4 deleted a comment from github-actions bot Jun 3, 2025
@MaEtUgR MaEtUgR force-pushed the maetugr/battery-info branch 3 times, most recently from 7d8e228 to b20fe12 Compare June 3, 2025 16:21
@PX4 PX4 deleted a comment from github-actions bot Jun 3, 2025
@PX4 PX4 deleted a comment from github-actions bot Jun 3, 2025
@MaEtUgR MaEtUgR force-pushed the maetugr/battery-info branch from b20fe12 to 12f0de0 Compare June 3, 2025 17:08
Comment on lines 1 to 4
uint64 timestamp # time since system start [us]

uint8 id # Must match the id in the battery_status message for the same battery
char[32] serial_number # Serial number of the battery pack in ASCII characters, 0 terminated [@invalid All bytes 0]
Copy link
Contributor

@hamishwillee hamishwillee Jun 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Metadata moved to appearing first as per your original suggestion. It makes parsing easier.
  2. Let's add a description
Suggested change
uint64 timestamp # time since system start [us]
uint8 id # Must match the id in the battery_status message for the same battery
char[32] serial_number # Serial number of the battery pack in ASCII characters, 0 terminated [@invalid All bytes 0]
# Battery information.
#
# Static or near-invariant battery information.
# Should be sent on request or streamed at low rate.
uint64 timestamp # [us] Time since system start.
uint8 id # Must match the id in the battery_status message for the same battery.
char[32] serial_number # [@invalid 0 All bytes] Serial number of the battery pack in ASCII characters, 0 terminated.

Copy link
Member Author

@MaEtUgR MaEtUgR Jun 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Totally! Thanks for the exemplary input, I'll adjust👍

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome. Made minor change above - the format is [@invalid <value> <optional description>]

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I took over a version of your proposal in 9fc28fb481226b22b2a03733484d6934cef33f6b

  • I left away the on request part since I'm not currently aware of any mechanism to do that.
  • I left away the . after all the short descriptions(.)

@PX4 PX4 deleted a comment from github-actions bot Jun 4, 2025
@hamishwillee hamishwillee self-requested a review June 4, 2025 21:49
@MaEtUgR MaEtUgR force-pushed the maetugr/battery-info branch from 12f0de0 to e584afa Compare June 11, 2025 09:11
@MaEtUgR MaEtUgR requested a review from sfuhrer June 11, 2025 09:15
@MaEtUgR MaEtUgR force-pushed the maetugr/battery-info branch from e584afa to d6cf5a4 Compare June 11, 2025 09:23
@PX4 PX4 deleted a comment from github-actions bot Jun 11, 2025
@MaEtUgR
Copy link
Member Author

MaEtUgR commented Jun 11, 2025

It's rebased on latest main and I addressed the documentation review 🙏

@@ -1,4 +1,4 @@
uint32 MESSAGE_VERSION = 0
uint32 MESSAGE_VERSION = 1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This bump requires a translation. If you anyway want to move other fields over to the info message I would consider doing it here all in one PR to not have to do the translation multiple times.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm learning 💡 Let me check.

If you anyway want to move other fields over to the info message I would consider doing it here all in one PR

From a public interface standpoint this would make sense but I'd still like to avoid it since if I start to sort out the entire message in this pr it will:
a) take a while to finish (this one already did)
b) explode in size and be hard to review and test

I'd like to work through in iterations. I can make the next one bigger if desired.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was a big handful of boilerplate. Luckily Gemini helped me fill in the repetitive stuff... 😓
The important portion in the docs is the step by step guide here:
https://docs.px4.io/main/en/ros2/px4_ros2_msg_translation_node.html#updating-a-versioned-message
All changes are in the last two commits.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MaEtUgR FMI did you find those docs understandable? It was hard to make logical and I didn't actually have to test this in anger so hoping we didn't miss anything.

Copy link
Member Author

@MaEtUgR MaEtUgR Jun 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

docs understandable?

@hamishwillee To be honest I was in a hurry and skimmed twice to find just how I can do the translation. I couldn't follow 1:1 because the message did not have a translation before. I checked high-level what I need and took the useful example https://github.com/PX4/PX4-Autopilot/blob/main/msg/translation_node/translations/example_translation_direct_v1.h and compared it against the relatively simple https://github.com/PX4/PX4-Autopilot/blob/6b2b20bd6ecb18eb6d3380449dce1fc75f61ce2d/msg/translation_node/translations/translation_event_v1.h. The quickest for me would have been an example commit to an exclusive such change but the events one is mixed with other changes and I'm not even sure why it was done since the message looks exactly the same to me 🤔 TL;DR really good docs but on the verbous side for my particular case 😇

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TL;DR really good docs but on the verbous side for my particular case 😇

Yeah, I've noticed that I'm leaning towards comprehensive but too long docs recently. Sorry!

If you find a good commit that works as a reference in future, let me know!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And thanks for the merge of the docs!

@MaEtUgR MaEtUgR force-pushed the maetugr/battery-info branch 2 times, most recently from b42e6a7 to 3c3889c Compare June 11, 2025 18:22
hamishwillee
hamishwillee previously approved these changes Jun 11, 2025
Copy link
Contributor

@hamishwillee hamishwillee left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving for my part. Though I hope my docs stuff goes in first in the other PR :-)

@MaEtUgR
Copy link
Member Author

MaEtUgR commented Jun 12, 2025

Though I hope my docs stuff goes in first in the other PR :-)

@hamishwillee Of course we merge your docs pr (#24789) first, I rebased 👍

@MaEtUgR MaEtUgR force-pushed the maetugr/battery-info branch from 32d5e4c to 0398d13 Compare June 12, 2025 15:56
Copy link
Contributor

@sfuhrer sfuhrer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MESSAGE_VERSION in the battery_status needs to be be bumped to 1.

@MaEtUgR MaEtUgR force-pushed the maetugr/battery-info branch from 0398d13 to 046eb10 Compare June 16, 2025 17:26
@MaEtUgR
Copy link
Member Author

MaEtUgR commented Jun 16, 2025

@sfuhrer sfuhrer merged commit eccfb18 into main Jun 17, 2025
68 of 69 checks passed
@sfuhrer sfuhrer deleted the maetugr/battery-info branch June 17, 2025 07:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants