Main Content

mavlinkio

Connect with MAVLink clients to exchange messages

Description

The mavlinkio object connects with MAVLink clients through UDP ports to exchange messages with UAVs (unmanned aerial vehicles) using the MAVLink communication protocols.

Creation

Description

mavlink = mavlinkio(msgDefinitions) creates an interface to connect with MAVLink clients using the input mavlinkdialect object, which defines the message definitions. This dialect object is set directly to the Dialect property.

mavlink = mavlinkio(dialectXML) directly specifies the XML file for the message definitions as a file name. A mavlinkdialect is created using this XML file and set to the Dialect property

example

mavlink = mavlinkio(dialectXML,version) additionally specifies the MAVLink protocol version as either 1 or 2.

mavlink = mavlinkio(___,Name=Value) additionally specifies arguments using the following name-value pairs.

Input Arguments

expand all

Message definition, specified as a mavlinkdialect object

Message definition file, specified as a string scalar

Example: mavlink = mavlinkio("common.xml")

Data Types: string

MAVLink protocol version, specified as a numeric value of 2 or 1

Data Types: numeric

Name-Value Arguments

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

MAVLink system ID, specified as a nonnegative integer between 0 and 255. MAVLink protocol only supports up to 255 systems. Usually, each UAV has its own system ID, but multiple UAVs could be considered one system.

Example: 1

Data Types: numeric

MAVLink component ID, specified as a nonnegative integer between 0 and 255.

Example: 2

Data Types: numeric

MAVLink component type, specified as a string. This value is automatically updated to the correct type if a heartbeat message is received by the client with the matching system ID and component ID.

Example: "MAV_TYPE_GCS"

Data Types: string | character vector

Autopilot type for UAV, specified as a string. This value is automatically updated to the correct type if a heartbeat message is received by the client with the matching system ID and component ID.

Example: "MAV_AUTOPILOT_INVALID"

Data Types: string | character vector

Properties

expand all

MAVLink dialect, specified as a mavlinkdialect object. The dialect specifies the message structure for the MAVLink protocol.

This property is read-only.

Local client information, specified as a structure. The local client is setup in MATLAB® to communicate with other MAVLink clients. The structure contains the following fields:

  • SystemID

  • ComponentID

  • ComponentType

  • AutopilotType

To set these values when creating the mavlinkio object, use name-value pairs. For example:

mavlink = mavlinkio("common.xml",SystemID=1,ComponentID=1)

This property is nontunable when you are connected to a MAVLink client. For more information, see mavlinkclient.

Data Types: struct

Object Functions

connectConnect to MAVLink clients through UDP port
disconnectDisconnect from MAVLink clients
sendmsgSend MAVLink message
sendudpmsgSend MAVLink message to UDP port
serializemsgSerialize MAVLink message to binary buffer
listConnectionsList all active MAVLink connections
listClientsList all connected MAVLink clients
listTopicsList all topics received by MAVLink client

Examples

collapse all

Connect to a MAVLink client.

mavlink = mavlinkio("common.xml");
connect(mavlink,"UDP");

Create the object for storing the client information. Specify the system and component ID.

client = mavlinkclient(mavlink,1,1)
client = 
  mavlinkclient with properties:

         SystemID: 1
      ComponentID: 1
    ComponentType: "Unknown"
    AutopilotType: "Unknown"

Disconnect from client.

disconnect(mavlink)

This example shows how to connect to MAVLink clients, inspect the list of topics, connections, and clients, and send messages through UDP ports using the MAVLink communication protocol.

Connect to a MAVLink client using the "common.xml" dialect. This local client communicates with any other clients through a UDP port.

dialect = mavlinkdialect("common.xml");
mavlink = mavlinkio(dialect);
connect(mavlink,"UDP")
ans = 
"Connection1"

You can list all the active clients, connections, and topics for the MAVLink connection. Currently, there is only one client connection and no topics have received messages.

listClients(mavlink)
ans=1×4 table
    SystemID    ComponentID    ComponentType          AutopilotType     
    ________    ___________    ______________    _______________________

      255            1         "MAV_TYPE_GCS"    "MAV_AUTOPILOT_INVALID"

listConnections(mavlink)
ans=1×2 table
    ConnectionName      ConnectionInfo   
    ______________    ___________________

    "Connection1"     "UDP@0.0.0.0:46250"

listTopics(mavlink)
ans =

  0x5 empty table

    MessageID    MessageName    SystemID    ComponentID    MessageFrequency
    _________    ___________    ________    ___________    ________________

Create a subscriber for receiving messages on the client. This subscriber listens for the "HEARTBEAT" message topic with ID equal to 0.

sub = mavlinksub(mavlink,0);

Create a "HEARTBEAT" message using the mavlinkdialect object. Specify payload information and send the message over the MAVLink client.

msg = createmsg(dialect,"HEARTBEAT");
msg.Payload.type(:) = enum2num(dialect,'MAV_TYPE','MAV_TYPE_QUADROTOR');
sendmsg(mavlink,msg)

Disconnect from the client.

disconnect(mavlink)

Version History

Introduced in R2019a