Arduino DroneCAN
A library to send CAN messages compatible with Ardupilot and PX4 UAV autopilots.
About
We made a library to make DroneCAN development as simple as possible. the Arduino DroneCAN repository allows you to get started with Ardupilot/PX4 compatible CAN messages and functionality straight out of the box using Beyond Robotix CAN node hardware.
This repository is still in development, some core features such as parameters are unimplemented, however the features to read/write messages as well as some core CAN node functionality are ready.
[LINK TO REPOSITORY]
The Arduino DroneCAN project reduces using DroneCAN down to creating an object, initialising the object, and one method to call in your loop function. Any messages on the bus can be accessed in "onTransferReceived" and messages can be sent in the loop() function.
onTransferRecieved and shouldAcceptTransfer are required to be in this format to be compatible with Canard, the underlying DroneCAN infrastructure. Even if you don't end up using them. They could be defined in another file or the declaration could be avioded if they were placed before setup().
Installation
There are a few ways of working with Arduino Code, we recommend the following steps for seamless integration with the project.
Install Visual Studio Code
Install the PlatformIO extension
Press upload!
Detailed Example
DroneCAN code can be daunting, even with the assistance of Arduino DroneCAN. We've provided an example in /src/main.cpp which reads sensor data, sends this data over CAN and also reads in some data on the CAN bus. We'll break down the loop() function below:
The following code sets us up to run our if statement at 10Hz, checking if 100ms has passed since our last call. This is because we want to only send our CAN message at 10Hz and we want to call our "dronecan.cycle()" function to be called as much as possible to ensure CAN messages are send and received in a timely manner. We want to avoid delay() as much as possible!
Next, we want to read in our sensor value. This could be from anything, a current monitor, position sensor.. in this example, we read in the temperature of our STM32 processor.
Next, we initialise our DroneCAN battery message packet and we assign one of its attributes a value from when we read in our sensor. We've used a battery message, since Ardupilot supports 8 of these by default, Mission planner can display information from any of these 8 and Ardupilot logs all battery instances. There may be messages suitable for your application, but be aware, Ardupilot does not support many DroneCAN messages.
Finally, we perform the boilerplate required to pack the message and hand it to Canard. As you can see, this process is specific to a Battery Info packet and would need changing to the appropriate equivalent for other message types.
At the end of the loop() we call our dronecan.cycle() method, which is required, and we also reset our watchdog timer.
Last updated