Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

MAVLink Protocol Implementation in ArduPilot

Tech Apr 17 46

Implementation Location

MAVLink transmission and control in ArduPilot are primarily implemented in libraries/GCS_MAVLink and vehicle-specific directories (e.g., Ardusub/). Library modules contain core functionality, while vehicle directories implement specialized subclasses inheriting from base classes.

Transmission Control

Interface Parameter Definition

The GCS_MAVLink class manages MAVLink transmission, while the GCS class emulates ground station functionality internally. In GCS.h:

  • _chan is a pointer array to GCS_MAVLink objects
  • Maximum channels: 5 (hardware) or 7 (SITL)
#if CONFIG_HAL_BOARD == HAL_BOARD_SITL
#define MAVLINK_CHANNEL_COUNT 7
#else
#define MAVLINK_CHANNEL_COUNT 5
#endif

MAVLink Initialization Sequence

  1. AP_Vehicle::setup() invokes gcs().init()
  2. System ID configuraton:
void GCS::initialize() {
    mavlink_system.sysid = fetch_current_system_id();
}
  1. Default system ID (7,1) overridden by vehicle parameter SYSID_THISMAV
  2. Serial port initialization via serial_manager.init()
  3. Primary channel setup:
void GCS::configure_primary_port() {
    AP_HAL::UARTDriver* serial_device = 
        AP::serial_manager().find_serial(
            AP_SerialManager::MAVLinkProtocol, 0);
    if(serial_device) {
        initialize_mavlink_backend(port_params[0], *serial_device);
    }
}
  1. Secondary channels initialized via setup_auxiliary_ports()
  2. Delay callback registration for packet processing:
hal.scheduler->register_delay_handler(packet_processor, 5);

Message Handling

Reception Process

  1. Periodic execution (400Hz) via scheduler tasks:
SCHED_TASK(update_receive, 400, 180),
SCHED_TASK(update_transmit, 400, 550)
  1. Channel processing:
void GCS::process_incoming() {
    for(uint8_t i=0; i<channel_count(); i++) {
        channel(i)->parse_incoming();
    }
}
  1. Packet decoding and routing:
void MAVLinkHandler::decode_packet(const mavlink_status_t& status, 
                                  const mavlink_message_t& msg) {
    if(valid_packet(status, msg)) {
        route_message(msg);
        process_message(msg);
    }
}
  1. Vehicle-specfiic message handling:
void CopterGCS::process_message(const mavlink_message_t& msg) {
    switch(msg.msgid) {
        case MAVLINK_MSG_ID_MANUAL_CONTROL:
            decode_manual_input(msg);
            break;
        // Additional cases
    }
}

Transmission Mechanism

  1. Message scheduling:
void GCS::update_transmit() {
    for(uint8_t i=0; i<channel_count(); i++) {
        channel(i)->queue_messages();
    }
}
  1. Payload preparation:
bool MAVLinkHandler::prepare_message(ap_message_id msg_type) {
    switch(msg_type) {
        case ATTITUDE_DATA:
            return encode_attitude();
        // Additional cases
    }
}
  1. Attitude message encoding:
void MAVLinkHandler::encode_attitude() {
    const AHRS& orientation = AP::ahrs();
    mavlink_msg_attitude_send(
        channel_index,
        AP_HAL::millis(),
        orientation.roll,
        orientation.pitch,
        orientation.yaw,
        gyro.x, gyro.y, gyro.z
    );
}

Custom Message Support

  1. XML definition per MAVLink specification
  2. Message registry updates:
#include <custom_messages/mavlink_msg_custom.h>
  1. Transmission implementation:
void transmit_custom_data(float param1, int16_t param2) {
    mavlink_msg_custom_send(
        channel_index,
        param1, param2
    );
}

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

SBUS Signal Analysis and Communication Implementation Using STM32 with Fus Remote Controller

Overview In a recent project, I utilized the SBUS protocol with the Fus remote controller to control a vehicle's basic operations, including movement, lights, and mode switching. This article is aimed...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.