MavLink is easy to use

Posted by claire on Wed, 05 Jan 2022 13:40:14 +0100

Reference connection: https://mavlink.io/zh/

Overall description

This paper does not explore the specific content of mavlink protocol, but only explains how to use it simply. Environment: win10 c/c++

MAVLink warehouse clone

In fact, clone is a tool that can help us convert definition files into code. The sender and receiver use the same code to pack and unpack data.

Use the command line:
git clone https://github.com/mavlink/mavlink.git --recursive

Or little turtle:

The definition file is in: mavlink\message_definitions\v1.0 folder. Open test XML, look at the content, you should know how to write the definition file.

How do I open this tool?

This tool is written in Python, so you need to install python. We can download the latest one from the official website. Official website

During installation, you need to add python to the environment variable (check the red box option in the figure below). It can be installed by default (Install Now)

Install python's future module
Installing with the command line

Double click mavlink \ mavgenerate Py file, the tool opens.

How do I use this tool?

First, you need to select a definition file.
In this example, mavlink\message_definitions\v1.0\test.xml copy and rename it mavlink test xml.


Open it with notepad and change the name of the message to MY_MAV. Other contents remain unchanged. Note that the array length defined in the definition file is 3. You have to be consistent when writing code.

Then use the tool to turn the definition file into code.

The generated file is shown in the following figure. In practical application, put the generated file into the project and include a header file #include "mavlink test / mavlink. H".
From the generated file, we can know that the name of the mavlink test folder is the name of the definition file.

Simple code example

Create a new console application MavlinkTest with vs2019. Copy the generated code into the project. The directory structure is as follows

The main functions are as follows

// MavlinkTest.cpp: this file contains the "main" function. Program execution will begin and end here.
//

#include <iostream>

#include "mavlinktest/mavlink.h"

int main() {
  /*Sender:*/
  // The first step is to package msg
  mavlink_message_t msg;
  // The length of the array is defined in the definition file
  uint8_t uint8_t_array[3] = {0, 1, 2}; 
  uint16_t uint16_t_array[3] = {0, 1, 2};
  uint32_t uint32_t_array[3] = {0, 1, 2};
  uint64_t uint64_t_array[3] = {0, 1, 2};
  int8_t int8_t_array[3] = {0, 1, 2};
  int16_t int16_t_array[3] = {0, 1, 2};
  int32_t int32_t_array[3] = {0, 1, 2};
  int64_t int64_t_array[3] = {0, 1, 2};
  float float_array[3] = {0, 1, 2};
  double double_array[3] = {0, 1, 2};
  // Alternative function mavlink_msg_my_mav_pack or mavlink_msg_my_mav_encode_chan
  mavlink_msg_my_mav_pack_chan(
      0, 0, 0, &msg, 'a', "hello world!", 1, 1, 1, 1,
      1, 1, 1, 1, 1.1, 1.1, uint8_t_array,
      uint16_t_array, uint32_t_array, uint64_t_array,
      int8_t_array, int16_t_array, int32_t_array,
      int64_t_array, float_array, double_array);

  // The second step is to convert msg to uint8_t array. Note that the length of the array must be long enough
  uint8_t buf[512] = {0};
  uint16_t rlen = mavlink_msg_to_send_buffer(buf, &msg);

  // The third step is to send the array through network port, serial port and other methods
  // ...................



  /*Receiver:*/
  //The first step is to convert the received data into uint8 through network port, serial port and other methods_ t[]
  // ...................

  // The second step is character by character parsing
  for (int i = 0; i < rlen; i++) {
    mavlink_status_t rstatus;
    mavlink_message_t rmsg;
    //The status of the resolution. When a problem is resolved, the problem can be tracked according to this status
    // std::cout << rstatus.parse_state;
    if (mavlink_parse_char(0, (uint8_t)(buf[i]), &rmsg, &rstatus) == 1) {
      switch (rmsg.msgid) {
        case MAVLINK_MSG_ID_MY_MAV:
          mavlink_my_mav_t my_mav;
          mavlink_msg_my_mav_decode(&rmsg, &my_mav);
          std::cout << my_mav.c;
          break;
        default:
          break;
      }
    }
  }
}