Smart Battery Modification to Ardupilot Firmware

Hello,

I am working on a custom ArduPilot firmware and would like to integrate a smart battery that communicates via I2C. My goal is to have the ArduPilot directly receive battery data (voltage, current, capacity, and status) through I2C, bypassing the need for an external power module.

Could anyone provide guidance on the following:

  1. Which part of the ArduPilot codebase should I modify to add support for my I2C smart battery?
  2. Are there any examples or documentation available for integrating custom battery monitors into ArduPilot?
  3. What is the process to test and validate such modifications?

Any resources, tips, or pointers would be greatly appreciated.

1 Like

See:

I did something similar a few years back: Smart Battery via Arduino

But according to the latest post part of the implemention is broken. Or at least the user didn’t confirm if it worked with my suggestion…

Can i add my Smart Battery Library into this? or do i need to use the base BattMonitor lib for creating my own version of it?

Here’s what i want to do.
I have BMS attached to my battery which gives data on I2C. I’ve tested with the Arduino boards.
So, how to do the same in ardupilot?

That depends on the used protocol. If it follows the SBS specification it could already work.
The Maxell one (the protocol I copied for my implantation) uses SBS with some special commands for the single cell voltages.

So if your battery uses SBS the SMBus implementation in ArduPilot (with it’s various adaptions) is probably a good starting point: ardupilot/libraries/AP_BattMonitor/AP_BattMonitor_SMBus.h at master · ArduPilot/ardupilot · GitHub

2 Likes

You can do that using Lua scripts. Set the battery monitor parameter as script and then write your script to read I2C data and update battery values.

The logic will be similar to this script

Edit: With this method you don´t need to modify the Ardupilot Codebase

but what if i want to deploy it on multiple drones?

Then you deploy the script to multiple drones.

1 Like

This. Or you can create a firmware binary with the script already embedded and install it it all your drones.

1 Like

Is there a way to create a custom library under the Battery Monitor library for a specific module and then deploy this firmware on all drones using this battery? I read the library, but it seems to involve a lot of hard work. However, I have the time, so can anyone explain how to write a library and which files I need to check out for this?

Yes, it is possible you will have to write a new battery monitor (backend) driver.

so, is there any ide or tool that i can use to test the lib with ardupilot integration?

In my experience VSCode works quite well with Ardupilot codebase when using WSL (AFAIK recommend on Windows). AFAIK if your smart battery uses I2C or SMBUS, you will have to implement a simulation for it as a part of SITL.

1 Like

I have successfully build the library, also added the enum for my bms module but it is not showing in sitl batt_monitor params

1 Like

Mission Planner fetches the documentation from the main repository, try setting the param to the value you assigned to your driver
.