Skip to content

fix(mavlink): preserve -1 marker in battery fields#27142

Merged
dakejahl merged 1 commit into
PX4:mainfrom
av-remco:fix/only-scale-battery-when-non-negative
May 5, 2026
Merged

fix(mavlink): preserve -1 marker in battery fields#27142
dakejahl merged 1 commit into
PX4:mainfrom
av-remco:fix/only-scale-battery-when-non-negative

Conversation

@av-remco
Copy link
Copy Markdown
Contributor

@av-remco av-remco commented Apr 20, 2026

Solved Problem

Fixes #27141

Solution

Check whether fields are negative before scaling

Changelog Entry

For release notes:

Bugfix: Prevent scaling to preserve -1 marker in battery fields

Alternatives

I guess we could pass -0.01 from SITL and get -1 with scaling, or pass battery_status.connected = false

Test coverage

Ran unit + integration tests, no regression

First PR, so open to any comments

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 20, 2026

🔎 FLASH Analysis

px4_fmu-v5x [Total VM Diff: 152 byte (0.01 %)]
    FILE SIZE        VM SIZE    
 --------------  -------------- 
  +0.0%    +152  +0.0%    +152    .text
     +13%     +76   +13%     +76    MavlinkStreamBatteryStatus::send()
     +10%     +72   +10%     +72    MavlinkStreamSysStatus::send()
    [NEW]     +60  [NEW]     +60    CSWTCH.1991
    [NEW]     +44  [NEW]     +44    CSWTCH.2790
    [NEW]     +14  [NEW]     +14    CSWTCH.3558
    [NEW]     +14  [NEW]     +14    CSWTCH.3559
    +0.6%      +4  +0.6%      +4    MavlinkStreamServoOutputRaw<>::send()
    [DEL]     -14  [DEL]     -14    CSWTCH.3556
    [DEL]     -14  [DEL]     -14    CSWTCH.3557
    [DEL]     -44  [DEL]     -44    CSWTCH.2788
    [DEL]     -60  [DEL]     -60    CSWTCH.1989
  +0.0%     +55  [ = ]       0    .debug_abbrev
  -0.0%      -2  [ = ]       0    .debug_info
  +0.0%     +99  [ = ]       0    .debug_line
   -60.0%      -3  [ = ]       0    [Unmapped]
    +0.0%    +102  [ = ]       0    [section .debug_line]
  -0.0%     -11  [ = ]       0    .debug_loclists
  -0.0%      -1  [ = ]       0    .debug_rnglists
  -1.7%    -152  [ = ]       0    [Unmapped]
  +0.0%    +140  +0.0%    +152    TOTAL

px4_fmu-v6x [Total VM Diff: 160 byte (0.01 %)]
    FILE SIZE        VM SIZE    
 --------------  -------------- 
  +0.0%    +160  +0.0%    +160    .text
     +13%     +76   +13%     +76    MavlinkStreamBatteryStatus::send()
     +10%     +72   +10%     +72    MavlinkStreamSysStatus::send()
    [NEW]     +60  [NEW]     +60    CSWTCH.1991
    [NEW]     +44  [NEW]     +44    CSWTCH.2790
    [NEW]     +14  [NEW]     +14    CSWTCH.3558
    [NEW]     +14  [NEW]     +14    CSWTCH.3559
    +0.0%      +8  +0.0%      +8    [section .text]
    +0.6%      +4  +0.6%      +4    MavlinkStreamServoOutputRaw<>::send()
    [DEL]     -14  [DEL]     -14    CSWTCH.3556
    [DEL]     -14  [DEL]     -14    CSWTCH.3557
    [DEL]     -44  [DEL]     -44    CSWTCH.2788
    [DEL]     -60  [DEL]     -60    CSWTCH.1989
  +0.0%     +55  [ = ]       0    .debug_abbrev
  -0.0%      -2  [ = ]       0    .debug_info
  +0.0%     +99  [ = ]       0    .debug_line
   -75.0%      -3  [ = ]       0    [Unmapped]
    +0.0%    +102  [ = ]       0    [section .debug_line]
  -0.0%     -11  [ = ]       0    .debug_loclists
  -0.0%      -1  [ = ]       0    .debug_rnglists
  -3.2%    -160  [ = ]       0    [Unmapped]
  +0.0%    +140  +0.0%    +160    TOTAL

Updated: 2026-04-30T08:32:35

@dakejahl dakejahl requested review from julianoes and ttechnick April 21, 2026 05:46
@ttechnick
Copy link
Copy Markdown
Member

This essentially prevents negative currents right?
I can think of a few situations, where we might actually have a negative current (think recovering energy while gliding, charging the battery mid air)
So I would really only want the case where the invalid value is sent to be treated

@av-remco av-remco force-pushed the fix/only-scale-battery-when-non-negative branch from 346e990 to ea7fc8c Compare April 30, 2026 07:58
@github-actions github-actions Bot added kind:bug Something is broken or behaving incorrectly. scope:mavlink MAVLink module, streams, commands, or protocol handling. labels Apr 30, 2026
@av-remco
Copy link
Copy Markdown
Contributor Author

I reasoned that the mavlink specification already prevents negative currents by using -1 as the invalid value, but I agree with your point.

Now scaling is only skipped if the -1.0f value is used

dakejahl
dakejahl previously approved these changes Apr 30, 2026
Comment thread src/modules/mavlink/streams/BATTERY_STATUS.hpp Outdated
@av-remco av-remco force-pushed the fix/only-scale-battery-when-non-negative branch from ea7fc8c to 5927127 Compare April 30, 2026 08:24
@dakejahl dakejahl merged commit 9d8af2f into PX4:main May 5, 2026
73 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind:bug Something is broken or behaving incorrectly. scope:mavlink MAVLink module, streams, commands, or protocol handling.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Battery fields contain -100 instead of -1 when field not set

4 participants