Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AP_BattMonitor: Remove double cast in mah calculation #19540

Merged
merged 2 commits into from
Dec 21, 2021

Conversation

arBalasquide
Copy link
Contributor

@arBalasquide arBalasquide commented Dec 16, 2021

This PR addresses issue #19452.

Also adds regression tests and accuracy tests to validate there isn't any loss of accuracy from removing the double casts.

@@ -51,6 +51,8 @@ class AP_BattMonitor_UAVCAN : public AP_BattMonitor_Backend
static void handle_battery_info_aux_trampoline(AP_UAVCAN* ap_uavcan, uint8_t node_id, const BattInfoAuxCb &cb);
static void handle_mppt_stream_trampoline(AP_UAVCAN* ap_uavcan, uint8_t node_id, const MpptStreamCb &cb);

static float calculate_mah(float amps, float dt) { return (float) (amps * dt * 0.0000002778f); }
Copy link
Contributor

Choose a reason for hiding this comment

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

Needs a comment to say where the magic constant came from, and arguably should actually be a helper that is in the general battery backend (or a seperate static function that any of the backends could include as needed), since the analog monitor has the same code path and we should be trying to hold these to do identical calculations.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Alright, I made it a static function in the backend header. Thought about virtual but it would make it harder to run the test class, although its currently causing errors for some reason it might just be best to scrap it.

I also made Analog use the new function.

@rmackay9
Copy link
Contributor

Really nicely done.

@peterbarker should probably decide if he's happy with the test code.

Copy link
Member

@hendjoshsr71 hendjoshsr71 left a comment

Choose a reason for hiding this comment

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

Love the fact that you included unit tests!! :) 🥇

libraries/AP_BattMonitor/AP_BattMonitor_Backend.h Outdated Show resolved Hide resolved
libraries/AP_BattMonitor/AP_BattMonitor_Analog.cpp Outdated Show resolved Hide resolved
libraries/AP_BattMonitor/AP_BattMonitor_UAVCAN.cpp Outdated Show resolved Hide resolved
@hendjoshsr71
Copy link
Member

Looking good!

Two things:

  • calculated_mah() can go negative via a negative A-s input so you could a add a test case. But I think it is good enough, how you have it.
  • We need to squash up the commits. We try to keep commits split up by library and to logically what you are doing. Something like the following
    • AP_Math: Add AUS_TO_MAH definition
    • AP_BattMonitor: use calculated_mah, add unit tests (but if you want to split off the unit tests parts as separate commit that is good too)

If you need help with the squashing let me know?

@arBalasquide
Copy link
Contributor Author

@hendjoshsr71 Thank you for the suggestions. I've squashed the commits now let me know if its OK.

Copy link
Member

@hendjoshsr71 hendjoshsr71 left a comment

Choose a reason for hiding this comment

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

LGTM

@tridge
Copy link
Contributor

tridge commented Dec 20, 2021

just needs intel copyright removed

Copy link
Member

@hendjoshsr71 hendjoshsr71 left a comment

Choose a reason for hiding this comment

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

LGTM

@tridge tridge merged commit fd3c83c into ArduPilot:master Dec 21, 2021
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.

None yet

5 participants