Mavlink Agreement Understanding

Source: Internet
Author: User
Tags apm mavlink

Source: blog.csdn.net/super_mice/article/details/44836585 saw the Mavlink agreement before, the online information about Mavlink is not much. This paper summarizes the understanding of the Mavlink protocol. The following, if not stated, is the Mavlink v1.0 version.

First of all, attach the mavlink of the various messages https://pixhawk.ethz.ch/mavlink/(here is a lot of content, it is recommended to know about Mavlink after the visit),


Mavlink Protocol Introduction Http://qgroundcontrol.org/mavlink/start


After that will be in my resources to upload a mavlink for beginners (reprint) has been uploaded to the author resources, free download. Mavlink Pure Little White tutorial It helps new people understand what Mavlink,mavlink is capable of and so on, using popular parlance. Data only the first part, the second part of the author did not find (perhaps the original author did not continue the second part of it).


The next Mavlink protocol is briefly introduced. The Mavlink agreement was first released by Lorenz Meier of the computer Vision and Geometry Experiment Group at the ETH Zurich in 2009 and follows the LGPL open source agreement. Mavlink protocol is a high-level Open source communication protocol based on serial communication, which is mainly used in the communication of micro aerial vehicle. Mavlink is the data that is frequently used for small aircraft and ground stations (or other aircraft) to develop a rules for sending and receiving and adding a checksum (checksum) function.


"1" starts with the introduction of the data structure that Mavlink sends. The basic unit of the Mavlink transmission is the message frame .

, each message frame is the above structure, except the gray, the other lattice represents a byte of data.
RedIs the start flag bit (STX), with "FE" as the starting mark in the v1.0 version. This flag bit is useful for message decoding at the receiving end of the Mavlink message frame.

SecondA lattice represents the byte length (len) of the gray section (payload, known as payload, data to be used in the payload), ranging from 0 to 255. The Mavlink message frame receiver can be compared with the length of the payload actually received to verify that the payload length is correct.

ThirdA lattice represents the sequence number (SEQ) of this message frame, each time a message is sent, the content of this byte will be added 1, plus 255 will start again from 0. This sequence number is used to calculate message loss ratios for Mavlink message frame receivers, which is equivalent to signal strength.

FourthA lattice represents the system number (SYS) of the device that sent this message frame, and the default system number when using the Pixhawk brush PX4 firmware is 1, which is used to mavlink the message frame receiver to identify which device sent the message.

FifthA lattice represents the unit number (comp) of the device that sent this message frame, the default unit number is 50 when using the Pixhawk brush PX4 firmware, and is used to mavlink the message frame receiver to identify which unit of the device sent the message (temporarily useless).

SixthA lattice represents the number of messages in the payload (msg), note that it is different from the ordinal, this byte is very important, the Mavlink message frame receiver to determine what message packet is placed in the payload and select the corresponding way to handle the packet in the payload according to the number.

last twoByte is a 16-bit check bit, CKB is eight bits high, and Cka is low eight bits. The check code is obtained by the CRC16 algorithm, the algorithm calculates the entire message (starting from the starting bit to the end of the payload, plus an extra mavlink_crc_extra byte) for the CRC16 calculation, which results in a 16-bit check code. In each payload mentioned previously, the packet (which is indicated by the message packet number) corresponds to a Mavlink_crc_extra, which is generated by an XML file that generates MAVLINK code. Adding this extra thing is that when the aircraft and the ground station use different versions of the Mavlink protocol, the verification code will be different between the two sides, so that the Mavlink protocol between different versions will not work together, avoiding the significant potential problems caused by communication between different versions.
to facilitate the narrative, a message packet is called a package, and the information represented by the package is called a message. The sys in will be called Sysid, andcomp will be called Compid, andmsg will be called MsgId.
An official introduction such as:

"2" The following is a brief introduction to the types of messages in Mavlink and how to understand the official Mavlink message mentioned at the beginning.


(i) has already mentioned in the Mavlink message frame the most important two things, one is msgid; one is payload, the former is the number of the content in the payload, the latter holds the message. There are many types of messages, in the official website of the Web page in the Blue "#" and the number of the number of messages, such as "#0" (this should be used to facilitate the search in the page to find the definition of the corresponding number message). In the website introduction page down, probably pulled to one-second of the position, began to appear "MAVLink Messages" Introduction, look down is the various message data composition description. The following will take a few messages, for example, to explain the mavlink message.


Take #0 message First, this message is called Heartbeat Packet (Heartbeat). It is generally used to indicate that the device that sent the message is active, and that the aircraft and ground stations emit this signal (typically sent in 1Hz), and that the ground station and the aircraft will determine if they have lost contact with the aircraft or ground station based on the timely receipt of the heartbeat packet.

As can be seen from the figure, the heartbeat packet consists of 6 data, the first is a single byte of the aircraft type data (type), this data indicates what the current message is what the aircraft, such as four rotor, fixed wing and so on. The value of type corresponds to the aircraft type, which is found on the official Mavlink Message page, which is located in the data enumeration where the page begins. As shown in the following:

here is only a part of the type, the first is the general aviation vehicle, the corresponding type value is 0, the second is the fixed wing type, the corresponding value is 1; the third one is the four-rotor, The corresponding value is 2. This type of aircraft, in fact, to the ground station of the heartbeat pack may not be meaningful (different flight control to the message processing methods, at least brush the PX4 firmware Pixhawk flight control to the ground station sent to the heartbeat packet This parameter does not care, if no special instructions, after the said Pixhawk flight control is to brush px 4 firmware flight control), for the aircraft end represents the current type of aircraft, the ground station can be based on this parameter to determine the type of aircraft and make other reactions.
The second parameter is the type of autopilot (commonly referred to as flight control), such as Apm,ppz,pixhawk, which defines the same method of finding the aircraft type as before. In the same way, for the aircraft that sends the heartbeat packet, it represents its own flight control, which means little to the heartbeat packet sent by the ground station.

The third parameter is the basic mode, which is the basic mode in which the flight control is now in, which is meaningless for the ground station that sends the heartbeat packet, which is meaningful for the flight control of the heartbeat packet. This parameter depends on the individual flight control of their own definition, Mavlink introduction of the page will not give a specific pattern. In Pixhawk, the basic mode can be divided into the user mode (custom mode) or Basic mode (this is a bit around, in fact, whether to use user mode). The use of user mode will be described in the next parameter, using the basic mode will be divided into automatic mode (auto), Position control mode (POSCTL) and manual mode (manual). In general the user mode will be used, the ordinary user does not care about this parameter. Developers need to be aware of the basic mode settings when using Mavlink to modify the aircraft mode, see PX4 code for details.

The fourth parameter is User mode (custom mode), which probably says Pixhawk user mode. Take the multi-axis example. It is divided into the main mode and the sub mode, the two modes are combined to become the final mode, the main mode of 3 kinds, manual (manual), auxiliary (assist), automatic (Auto). Manual mode is similar to APM's attitude mode. In the auxiliary mode, it is divided into the height control mode (ALTCTL) and the position control mode (POSCTL) Two sub-modes, the height control mode is similar to the APM's fixed high mode, the throttle corresponds to the aircraft height control. Position mode controls the speed of the aircraft relative to the ground, as well as the throttle and height control modes, yaw axis control and manual mode. Automatic mode is divided into 3 sub-mode, task mode (mission), left mode (loiter), home mode (return), the task mode is to perform a set of waypoint tasks, Left mode is the GPS hover mode, return mode is straight back to the home point and automatically landing. In APM This parameter seems to be useless, note that this data accounted for 4 bytes, in Pixhawk, the first two bytes (low) is reserved, no use, the third byte is the main mode, the fourth byte is a sub-mode. Ordinary users please ignore, developers please note: The official website given by the program settings mode code is wrong. , the last line of code is wrong and should be:


The fifth is the system status, check the definition is good, wherein the standby state in the Pixhawk is still unlocked state, the active state is already unlocked, ready to take off the state.
The sixth is the Mavlink version (Mavlink versions), which is now the "3" version.
The rest of the message is a similar structure, and the definitions of each data can be viewed in the Mavlink official web page, which is usually in the front section of the page. Specify that the flight control will prevail, Mavlink only provide a basic definition.

There are several relatively special and easily confusing messages to be described in particular:

#76消息 (Command long), the message is to send a long command, usually the ground station sent to the flight control command. The message consists of the. The target system (the receiver of the command, which is the target system number Sysid), the target unit (the receiving unit of the command, which is the target unit number Compid). The command data is the number of the order used to distinguish different commands. Confirmation data, the author is not very clear, it is probably necessary to receive an order after the confirmation signal meaning. Then there are seven parameters, these parameters are required to execute this command to tell the flight control, many commands are used in less than seven parameters, the extra parameters clear 0 is enough.

There are many types of commands supported by Pixhawk (but not all Mavlink commands are supported). To see what commands Mavlink provide, please refer to Mavlink's official website for Mav_cmd, in the upper part of the page. For example: Order number 176th Mav_cmd_do_set_mode. This command is used to change the flight mode of the aircraft, the first parameter is to set the base_mode of the flight control, and the second is to set the Custom_mode. To correctly set the Pixhawk mode with this command, you need to view the PX4 code, mavlink the description of the parameter is not specific enough.

Now you should know about the layout of the Mavlink official website. In front of the Web page is mainly about the value and meaning of various types of data, such as flight control type (Mav_autopilot), aircraft type (MAV_TYPE), etc., of which mav_cmd is a relatively special and important data. The second part of the Web page mainly about the type of Mavlink message and data composition, here will use a variety of data, the specific data defined can go back to the first half to find. But Mavlink is a general-purpose communication protocol, different flight control support for the Mavlink is not the same way, generally only support a portion of Mavlink messages, but also expand some of the Mavlink protocol does not define the message (Pixhawk and APM are the same), Specifically, the flight control code shall prevail.



About the ground station and the flight control of the communication process, because have not seen the ground station code, so probably wrong, but also look to comment! General flight control in connection with the ground station will be active to the ground station to send heartbeat packets, aircraft attitude, system status, remote control signals and other components of the data stream. Each data will be sent at a certain frequency, such as heartbeat packet is generally 1Hz, posture information will be faster, Pixhawk with a number of connections QGC when the attitude of data transmission frequency around 7-8hz. The general ground station will send commands when the flight control is just connected, request the flight control to return all parameters (QGC is this), flight control according to their own situation to determine whether to accept the ground station request, and according to different commands to perform the corresponding operation (some commands need to fly control back ground station to confirm the signal). After the ground station according to the user's operation will send the corresponding Mavlink message to the flight control, for example sets the waypoint, rewrites the flight control parameter and so on.

Mavlink There are many versions, although are Mavlink v1.0, but there are a lot of different places, various flight control, different time Mavlink files will not be the same, I said Mavlink is here to download https://github.com/ Mavlink/c_library. The Mavlink code is made up of all the headers and can be easily added to your own code.

you can see that there are multiple folders and several header files inside. Pixhawk,ardupilotmega (APM), Matrixpilot This kind of folder is each flight control their own definition of Mavlink message type, The original Mavlink message is placed in the common folder (most of the messages are in the common folder). The code that calculates the check code is stored in the checksum.h. Mavlink_helper.h inside is to add the individual message packets complete (call checksum.h in the function to calculate the checksum and fill the header of the message frame, such as sysid and compid, etc.) into the Mavlink message frame and then send. The most important features are concentrated in both folders. Mavlink_conversions.h is the DCM, Euler angle, four yuan three gestures represent the conversion code between methods.
The following is an example of sending a heartbeat packet (heartbeat), explaining how to use the Mavlink header file to send a heartbeat packet. First open the Mavlink_msg_heartbeat.h header file in the Common folder. This header file can be divided into two parts, one for packaging, sending the heartbeat message, and the other to decode the message when the heartbeat message is received. Heartbeat.h defines the data type that corresponds to the heartbeat message:
  
 
  1. typedef struct __mavlink_heartbeat_t
  2. {
  3. uint32_t custom_mode; ///< A bitfield for use for autopilot-specific flags.
  4. uint8_t type; ///< Type of the MAV (quadrotor, helicopter, etc., up to 15 types, defined in MAV_TYPE ENUM)
  5. uint8_t autopilot; ///< Autopilot type / class. defined in MAV_AUTOPILOT ENUM
  6. uint8_t base_mode; ///< System mode bitfield, see MAV_MODE_FLAG ENUM in mavlink/include/mavlink_types.h
  7. uint8_t system_status; ///< System status flag, see MAV_STATE ENUM
  8. uint8_t mavlink_version; ///< MAVLink version, not writable by user, gets added by protocol because of magic data type: uint8_t_mavlink_version
  9. } mavlink_heartbeat_t;
"1" can be called if Mavlink can be sent using (serial port, function interface is also compatible)
  
 
    1. static inline void Mavlink_msg_heartbeat_send ( mavlink_channel_t Chan uint8_t type uint8_t autopilot uint8_t Base_mode uint32_t Custom_mode uint8_t System_status

The Chan is the channel abbreviation for selecting the serial port or USB port to be sent. Type is the aircraft types, the rest of the parameters can be seen in this blog above article.

The function is to put incoming parameters in the corresponding format into the heartbeat message packet (that is, packaging)

Inside this function is a preprocessing:

 
   
  
  1. #if MAVLINK_CRC_EXTRA

is whether to use additional CRC check characters (by default), see the first blog for a description of the two check bytes.

Function "2" is called in the function

 
   
  
  1. _mav_finalize_message_chan_send(chan, MAVLINK_MSG_ID_HEARTBEAT, buf, MAVLINK_MSG_ID_HEARTBEAT_LEN, MAVLINK_MSG_ID_HEARTBEAT_CRC);
 
   
  
  1. MAVLINK_MSG_ID_HEARTBEAT//这个是心跳包消息对应的编号 这里=0
 
   
  
  1. MAVLINK_MSG_ID_HEARTBEAT_LEN//这个是心跳包的长度 注意这个长度仅仅是payload的长度,不包括帧的头尾。
 
   
  
  1. MAVLINK_MSG_ID_HEARTBEAT_CRC//这个是heartbeat消息对应的额外的crc校验码 这里=50
This function is located in Mavlink_helper.h, which updates the number of the message frame (seq each send a frame plus 1) and the header of the message frame and computes the checksum to make it a complete mavlink message frame. Finally, the serial send function is called to send the message frame.

If you just want to keep the corresponding heartbeat packet parameters in the heartbeat package format, you can call only

  
 
  1. static uint16_t mavlink_msg_heartbeat _pack ( uint8_t system_id uint8_t component_id mavlink_message_t * msg
  2. uint8_t type, uint8_t autopilot, uint8_t base_mode, uint32_t custom_mode, uint8_t system_status)
Package the parameters as heartbeat message frames for later use.
When decoding a message frame, you can call the Mavlink_helper.h in the
 
   
  
  1. MAVLINK_HELPER uint8_t mavlink_parse_char(uint8_t chan, uint8_t c, mavlink_message_t* r_message, mavlink_status_t* r_mavlink_status)
It decodes the received characters, verifies that the received checksum is correct, is less than the maximum length of the payload, and is consistent with the length of the message. If all goes well, you will get the decoded message and put it in the decoded message frame type.
  
 
  1. typedef struct __mavlink_message {
  2. uint16_t checksum; ///< sent at end of packet
  3. uint8_t magic; ///< protocol magic marker
  4. uint8_t len; ///< Length of payload
  5. uint8_t seq; ///< Sequence of packet
  6. uint8_t sysid; ///< ID of message sender system/aircraft
  7. uint8_t compid; ///< ID of the message sender component
  8. uint8_t msgid; ///< ID of message in payload
  9. uint64_t payload64[(MAVLINK_MAX_PAYLOAD_LEN+MAVLINK_NUM_CHECKSUM_BYTES+7)/8];
  10. }
The magic is the starting flag (fe=254) of a frame, which is the value of MAVLINK_STX.

The rest of the Mavlink messages are similar, and some types of message types in the old Mavlink code may not be found, and it is important to note that the acceptance and the Mavlink version used by the sender are compatible. The common.h in the common folder contains the data type to be used and the header files for all messages, which are included directly in the application.



From for notes (Wiz)

Mavlink Agreement Understanding

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.