/
Ardupilot CAN interfacing over Lua scripts

Ardupilot CAN interfacing over Lua scripts

The drone must output data (ie whether it is accelerating or braking) over CAN in order for the lighting board to receive it. This is done by writing a custom script in Lua that runs on the drone

Lua Script Flashing

@Billy Karantzoulis got this working IIUC!

Ardupilot → Dronecan

There are two different for sending custom packets over Dronecan from Ardupilot.

  1. Using the existing Ardupilot Lua CAN interface and write a scuffed implementation of Dronecan in Lua (what we want to do right now for time’s sake)

  2. Writing a low-level library in C that we somehow call from the Lua side (a good future goal that would probably take longer to make robust)

Lua-side CAN with in-house Lua Dronecan Encoding

Ardupilot has a small library for Lua programs that includes a barebones CAN interface. This approach to broadcasting Dronecan packets from Ardupilot would have 2 steps:

Dronecan encoding

Write custom encoding functions to take in the desired dronecan packet that we want to send and outputs the raw bytes that we should broadcast over CAN. The simplest way to do this would be to only support sending the specific packets that we need to broadcast (we can probably just get away with 1 packet shape for the LED lighting board) and handwrite an encoding function that takes in that packet definition and outputs the correct bytestring. For a simple struct-shaped dronecan packet, this is relatively straightforward.

For example, a packet definition that encodes information about the drone’s position and speed might look like:

int64 speed; int64 altitude;

And it would be encoded as:

packet type

speed

altitude

packet type

speed

altitude

An integer identifier representing the type of packet we are sending. If we just want to send one packet type this can be hardcoded

A 64 bit integer.

A 64 bit integer.

I believe the integer fields are encoded as big endian, but that is TBD.

Lua-side packet transmission

Use the Ardupilot Lua Can interface to transmit the encoded data over CAN.

FFI into LibCanard

Libcanard implements Dronecan encoding and packet transmission. To transmit messages we can possible use the Lua-C API? This is confounded by the fact that to call C functions from Lua in the traditional way one has to control the startup of the Lua runtime, where here that control entirely belongs to Ardupilot. This approach would definitely be less scuffed than doing it all on the Lua-side, but also more difficult as it would likely require an Ardupilot-specific FFI API.

Related content