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

Battery: fix MAVLink wrap when battery percentage is above 100% #15858

Closed
durka opened this issue Nov 20, 2020 · 13 comments
Closed

Battery: fix MAVLink wrap when battery percentage is above 100% #15858

durka opened this issue Nov 20, 2020 · 13 comments

Comments

@durka
Copy link

durka commented Nov 20, 2020

Bug report

Issue details

I've got a poor current sensor that usually reports a negative current (~ -2A) at idle. The battery backend integrates current to keep track of consumed mAh, then subtracts this from the known pack capacity to get a percentage. If the current is negative, this consumed mAh value is nonsense.

This produces cascading problems because capacity_remaining_pct caps the returned percentage to 255, but GCS_Common.cpp casts to an int8, which means with my bad sensor the percentage will climb to 127%, then go negative, eventually ending up at -1 which is supposed to be the sentinel value for "I don't have a battery".

Since the sensor isn't producing meaningful data on the ground I don't see a better solution than thresholding it at zero so at least we don't integrate negative values.

Version
Old master (forked 2020-06-15), but the relevant files (AP_BattMonitor_Backend.cpp, AP_BattMonitor_Analog.cpp) doesn't seem to have meaningfully changed

Platform
[x] All
[ ] AntennaTracker
[ ] Copter
[ ] Plane
[ ] Rover
[ ] Submarine

Airframe type
Quad-X

Hardware type
CubeBlack

Logs
Log attached:
log_64_2020-11-18-12-40-20.bin.zip
The battery status starts out at CurrTot=-5772.76, with the pack capacity at 16000, so we are going to calculate 136%, which gets reported over mavlink as -120% I believe.

@IamPete1
Copy link
Member

I don't think we can just ignore negative current, it probably would be fair to constrain the capacity to not go over the set capacity. Ie limit to a max of 100%

@durka
Copy link
Author

durka commented Nov 21, 2020

I think both are nonsensical (percentage over 100% and negative integrated energy). Unless a current sensor really could read negative if the battery were plugged into a charger or something?

@magicrub
Copy link
Contributor

Onboard charging is a thing and measuring it is very important. We have gas powered electric generator libraries for had planes and solar aircraft are getting more popular

@durka
Copy link
Author

durka commented Nov 21, 2020

OK fair enough. Capping it at 100% seems reasonable. I think the algorithm should be enhanced in other ways, like if the voltage is close to BATT_LOW_VOLT it's clearly not 100% but that's another topic.

@IamPete1
Copy link
Member

Thinking about this more, I think percentages over 100 should also be allowed if your generator goes mad and starts overcharging it might happen.

But we should certainly fix the wrap issue it MAVLink. I'm going to update the title to reflect that.

@IamPete1 IamPete1 changed the title Battery estimation confused by negative current reading Battery: fix MAVLink wrap when battery percentage is above 100% Aug 16, 2021
@IamPete1
Copy link
Member

If anyone fancys taking this one the code is here:

battery.capacity_remaining_pct(instance),

We should add a check just like the current, mah and wh ones a few lines above.

@peterbarker
Copy link
Contributor

Pretty sure this one's already being tackled here: #13712

@Allan0820
Copy link

@peterbarker Is this issue still open ? Would like to try solving it.

@IamPete1
Copy link
Member

@Allan0820 Yes, this is still a issue, a fix would be great!

@shiv-tyagi
Copy link
Contributor

Is anyone working on this issue? If not, I would love to give it a try.

@IamPete1
Copy link
Member

@shiv-tyagi I don't think we have a open PR for this, if you could fix that would be great. Thanks for helping us close all these issues!

@shiv-tyagi
Copy link
Contributor

@IamPete1 Pleasure is all mine. I added a check in GCS_Common. I hope this fixes this issue. Please review the PR #18731

@IamPete1
Copy link
Member

Closed by #18731, Thanks @shiv-tyagi!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants