-
Notifications
You must be signed in to change notification settings - Fork 17.2k
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
BattMonitor: add scripting backend #22786
Conversation
This is an example driver, simulating a use-case where someone has 4 separate DC-to-DC converters, and implements 5 battery monitors. 1 aggregate monitor for the operator, and 4 individual monitors for logging/diagnosing problems. I'll need to add an example driver in the PR eventually, but I don't know if it should be this exact one, so I've left it out for now:
|
We have wanted this for ages! Thanks. Take a look at the way the EFI scripting backend works. The script builds a structure and sets the whole thing in one go. You would add the |
Thanks for the reply, Peter, I hope I understand you correctly, I do have a virtual set state method (called The reason I didn't use the existing The thing that is nagging me though is that there is always a binding to |
We could just always enable the scripting backend when scripting is enabled, we can always fix the binding generator in the future. I see your point about |
This implementation allows for optionally setting its own failsafe, mah consumption, wh consumption, and more, while automatically falling back to having the backend provide those if the script doesn't. One of the few things I didn't implement was resting voltage and resistance. Those are handled by the parent backend class currently. It wouldn't be too hard to add those as optionally-handled by script if we want. |
I added the ability to override resistance and/or resting voltage estimates in the script. This should allow @jschall to implement the EKF battery state estimation as a lua backend. We should probably add a way to use a lua script to combine such an estimator with existing backends (probably something for a separate PR), but as a start this would allow you to do it with analog inputs by manually reading the pins in the lua script. |
89f865c
to
dd3748e
Compare
I am the person who asked about a custom battery monitor on Discord, and I can confirm that I was able to use the code from this PR and from the example to create custom battery monitors very easily! Much better than a custom driver in C. I haven't used the aggregated state yet, but I can now read values from DC-DC converters using I2C and fake them into battery monitors. |
needs a rebase |
needs a rebase and an example script |
dd3748e
to
d240657
Compare
I'm happy to write an example, or to have Peter do it. I could add the example I posted as a comment. Alternatively, I could try to implement a backend for a real device, such as the Millswood 250W PMU. |
e51caab
to
f3f4e42
Compare
cdd8502
to
af0931d
Compare
@robertlong13 I've done some updates on this and written a working example for a CAN battery monitor |
4639981
to
3754fac
Compare
note that the ANX driver needs a README and has some debug code in it that will be removed once tested |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be nice to default the "invalid" values correctly. current_amps
, consumed_mah
, consumed_wh
, temperature
, cycle_count
, capacity_remaining_pct
all use none zero values. We could do it with a custom scripting constructor. This would mean the script writer just has to fill in what they have and it should all work.
AP_BattMonitor_Scripting: whitespace consistency
AP_BattMonitor_Scripting: whitespace consistency
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
This PR adds a lua backend for battery monitors, opening up a lot more flexibility for custom PMU configurations. This has been on my to-do list for a while for making custom simulated power systems in SITL, but I finally decided to go for it when someone asked a question about custom battery monitor drivers on Discord.
This is by far my largest-scope PR for ArduPilot so far, so I expect this will take some massaging to get it in-line with best practices.
I mostly tried to follow the same ideas that went into the EFI scripting backend. A couple of notable differences:
depends
flag to remove the binding forget_backend
ifHAL_BATTMON_SCRIPTING_ENABLED
is not set.