How to add CAN support to non-DroneCAN battery

Our team recently acquired an 18S battery for our drone. However, due to manufacturer limitations, the communication protocol utilized by this battery employs raw CAN protocol and a proprietary frame format. I’m seeking guidance on how to establish communication with the battery and transmit the acquired data to MissionPlanner.

In previous discussions, I’ve come across suggestions to use Lua scripts for parsing CAN data packets effortlessly. However, I haven’t found information on how to send back the parsed battery data.

Could you provide some examples or insights on this matter?

Thank you.

Hi @FoxSuzuran,

I don’t have a complete answer but I agree that it should be possible to write a battery monitor driver in Lua that communicates using CAN.

The C++ driver for the scripting battery monitor is here so this is the C++ driver that the Lua driver is built upon. I.e. the lua script bindings will send the voltage etc into the C++ code though this driver. You’ll need to set BATT_MONITOR = 29.

We have a Lua battery applet here which might be a good reference.

We have a few Lua examples that use CAN including this one.

Thank you for your response. I have found these codes in the bindings.desc file.

include AP_BattMonitor/AP_BattMonitor.h

include AP_BattMonitor/AP_BattMonitor_Scripting.h
userdata BattMonitorScript_State depends AP_BATTERY_SCRIPTING_ENABLED
userdata BattMonitorScript_State field healthy boolean write
userdata BattMonitorScript_State field voltage float'skip_check write
userdata BattMonitorScript_State field cell_count uint8_t'skip_check write
userdata BattMonitorScript_State field capacity_remaining_pct uint8_t'skip_check write
userdata BattMonitorScript_State field cell_voltages'array int(ARRAY_SIZE(ud->cell_voltages)) uint16_t'skip_check write
userdata BattMonitorScript_State field cycle_count uint16_t'skip_check write
userdata BattMonitorScript_State field current_amps float'skip_check write
userdata BattMonitorScript_State field consumed_mah float'skip_check write
userdata BattMonitorScript_State field consumed_wh float'skip_check write
userdata BattMonitorScript_State field temperature float'skip_check write

So, I’m wondering if I can directly assign values to them in the Lua script, just as examples in C++ code. However, as I’m not very familiar with Lua, it seems that I can only call specific function methods when using the battery. Do I need to supplement some set methods in C++ and bind them to the desc?Alternatively, is there a way to directly modify the values of these variables?

It seems that I found a solution in the file at BattMon_ANX.lua. I will try to study the code in the file further.

I’m also wondering if there’s a better way to find the files I want, rather than going through them one by one, as there don’t seem to be any documents or discussions mentioning this file.

1 Like

Hi @FoxSuzuran,

Here are some references:

In general we try to keep the wiki up-to-date with developments but some things fall through the cracks. Developers should really be adding documentation as they add features and drivers. You’ll see on some PRs that we have a “Wiki needed” label which adds the PR into a tracked list which is then used to encourage the updating of the wiki.