MavLink Messaging
What is Mavlink?
The MAVLink protocol (Micro Air Vehicle Link) is a lightweight, open-source communication protocol designed specifically for unmanned systems and robotics. It is widely used in the field of autonomous vehicles, particularly in the drone industry. MAVLink enables communication between different onboard systems, such as autopilots, ground control stations, companion computers, and various other components of a robotic system.
Here are some key features and characteristics of MAVLink:
Lightweight: MAVLink is designed to be a minimalistic and efficient protocol, using a small footprint in terms of bandwidth and processing power. It is well-suited for resource-constrained systems like small drones.
Message-based: Communication in MAVLink occurs through messages, which are structured units of data. Each message has a predefined format that includes a header and payload. The header contains information such as the message ID, source system ID, and target system ID.
Extensible: MAVLink allows for the definition of custom messages to accommodate specific system requirements. This flexibility enables developers to extend the protocol to support new types of data and commands.
Transport-agnostic: MAVLink is designed to be transport-agnostic, meaning it can work over various communication channels, such as serial connections (UART), radio links, Ethernet, or UDP/IP networks. The choice of the underlying transport layer depends on the specific implementation.
Platform-independent: MAVLink is platform-independent and supports different programming languages, making it widely accessible for developers working on diverse hardware and software platforms.
High-level and low-level commands: MAVLink supports both high-level commands, such as waypoint navigation and mission planning, as well as low-level commands for direct control of vehicle movements, sensor readings, and parameter adjustments.
Open-source: MAVLink is an open-source project with an active community of developers. This openness promotes collaboration, easy integration with existing systems, and continuous improvement.
Overall, the MAVLink protocol serves as a standardized and interoperable means of communication between different components of unmanned systems, enabling seamless integration and coordination of autonomous vehicles and robotic platforms.
Current Strategy (Message Decoder Class)
The code strategy involves a systematic approach to decoding incoming MAVLink messages. This strategy provides a flexible and efficient way to decode different types of MAVLink messages by utilizing macros and lambdas in C++.
At the heart of the strategy is the REGISTER_DECODER
macro, which allows a user to specify the unique message id, the base name of the message, and a post processing function (lambda) to handle the specific type of MAVLink message.
After registration, when a message arrives, the parseBytesToMavlinkMsgs
function checks for a valid MAVLink message and subsequently invokes the decodeMsg
function. This function uses the unique message id to lookup the appropriate decoding function in a map. If a function is found, it is invoked and the generic MAVLink message is decoded to its specific type and passed to the post processing function. The post processing function, defined in the registration process, can then handle the specific message in any way required.
Example
Consider the MAVLink ATTITUDE
message. This message contains roll, pitch, and yaw data from a UAV. A developer might want to print these values whenever an ATTITUDE
message is received. Using the code strategy described above, the developer could accomplish this as follows:
REGISTER_DECODER(MAVLINK_MSG_ID_ATTITUDE, attitude,
[](mavlink_attitude_t &message) {
std::cout << "ATTITUDE" << std::endl;
std::cout << "roll: " << message.roll << std::endl;
std::cout << "pitch: " << message.pitch << std::endl;
std::cout << "yaw: " << message.yaw << std::endl;
std::cout << "rollspeed: " << message.rollspeed << std::endl;
std::cout << "pitchspeed: " << message.pitchspeed << std::endl;
});
In this case, the REGISTER_DECODER
macro is used to specify that the MAVLink ATTITUDE
message (unique id MAVLINK_MSG_ID_ATTITUDE
) should be handled. The post processing function is defined as a lambda function that takes a mavlink_attitude_t
object (decoded from the generic MAVLink message) and prints its fields.
As a result of this registration, whenever an ATTITUDE
message is received, it is automatically decoded and the roll, pitch, and yaw values are printed to the console. This strategy allows for efficient, flexible, and compact handling of MAVLink messages.
Quick note: Much of this document was created with the help of Chat-GPT