Skip to content

PonomarevDA/dronecan_application

Repository files navigation

Code Smells Lines of Code build check_crlf

DroneCAN application

This is C library that brings up the libcanard, platform specific drivers and serialization together to build a minimal DroneCAN application.

A minimal application includes the following protocol-features:

type message
1 broadcast uavcan.protocol.NodeStatus
2 RPC-service uavcan.protocol.GetNodeInfo
3 RPC-service uavcan.protocol.param.*
4 RPC-service uavcan.protocol.RestartNode
5 RPC-service uavcan.protocol.GetTransportStats

The following auxilliary features should be provided as well:

  • actuator
  • airspeed
  • baro
  • circuit status
  • fuel tank
  • esc
  • ice
  • indication
  • power
  • rangefinder
  • gnss
  • mag
  • etc

The library should support the following platforms:

  • Ubuntu: socketcan
  • stm32f103: BXCAN based on platform specific
  • stm32g0: FDCAN based on STM32 HAL
  • stm32f103: DroneCAN/Serial based on STM32 HAL

Design

The source code is divided into a few folders:

  • application has the main source code. It brings up all uavcan/protocol features such as NodeStatus, GetNodeInfo, Params, RestartNode, GetTransportStats.
  • examples provides examples with this library on different platforms
  • serialization has a set of headers for data serialization from C-structures to DroneCAN and vice versa
  • Libs - dependencies
  • platform_specific has a platform specific drivers
  • tests - unit tests

Notes:

  • It depends on libparams v0.8.4 library.
  • It is not thread safe.

How to integrate the library into a project

Add the following lines into CMakeLists.txt of your project:

# 1. Specify the CAN_PLATFORM. Options: bxcan, fdcan or socketcan.
set(CAN_PLATFORM socketcan)

# 2. Specify path to libparams and platform. Options: stm32f103, stm32g0b1, ubuntu.
set(LIBPARAMS_PATH        ../../build/libparams)
set(LIBPARAMS_PLATFORM    ubuntu)

# 3. Include the CMakeLists.txt
include(../../CMakeLists.txt)

# 4. Add DroneCAN related source files and headers to you target.
add_executable(${EXECUTABLE}
    ...
    ${DRONECAN_SOURCES}
    ...
)
target_include_directories(${EXECUTABLE} PRIVATE
    ...
    ${DRONECAN_HEADERS}
    ...
)

Minimal application example

The example is avaliable in examples/ubuntu/minimal folder.

// Include dronecan.h header file
#include "dronecan.h"

// Initialize the library somewhere
const uint8_t node_id = 42;
auto init_res = uavcanInitApplication(node_id);
if (init_res < 0) {
    // handle error here
}

// Spin it periodically:
while (true) {
    ...
    uavcanSpinOnce();
    ...
}

A usage example is shown below:

drawing

Publisher example

A CircuitStatus publisher example is avaliable in examples/publisher/circuit_status folder.

A BatteryInfo publisher example is shown below:

// Include necessary header files
#include "dronecan.h"
#include "uavcan/equipment/power/BatteryInfo.h"

// Create a message and trasnfer id
BatteryInfo_t battery_info{};
static uint8_t transfer_id = 0;

// Publish a message and increase the transfer_id:
dronecan_equipment_battery_info_publish(&battery_info, &transfer_id);
transfer_id++;

A CircuitStatus usage example is shown below:

drawing

Subscriber example

There are Ubuntu RawCommand and ArrayCommand subscriber and Ubuntu LightsCommand subscriber examples.

Let's consider a RawCommand subscriber example.

// Include necessary header files
#include "dronecan.h"
#include "uavcan/equipment/esc/RawCommand.h"

// Add a callback handler function
void callback(CanardRxTransfer* transfer) {
    RawCommand_t raw_command;
    int8_t res = dronecan_equipment_esc_raw_command_deserialize(transfer, &raw_command);
    if (res > 0) {
        // Do something very quickly, or save the command for later use
    } else {
        // Handle a real time error
    }
}

// Add the subscription:
auto sub_id = uavcanSubscribe(UAVCAN_EQUIPMENT_ESC_RAWCOMMAND, callback);
if (sub_id < 0) {
    // Handle an initialization error
}

License

The software is distributed under term of MPL v2.0 license.