Contenuto principale

mavlinkio

Create local MAVLink client

Description

The mavlinkio function creates a mavlinkio object, which represents a local MAVLink client. After you create a mavlinkio object, you can connect the object and exchange messages with other MAVLink clients such as UAV and ground control stations using the object functions.

Creation

Description

mavlink = mavlinkio(msgDefinitions) creates a local MAVLink client with communication interface using MAVLink message definition that you specify using a mavlinkdialect object.

mavlink = mavlinkio(dialectXML) creates a local MAVLink client with communication interface using MAVLink message definition that you specify using an XML file.

example

mavlink = mavlinkio(dialectXML,version) additionally specifies the MAVLink protocol version.

mavlink = mavlinkio(___,Name=Value) additionally sets properties using one or more name-value arguments. For example, mavlink = mavlinkio("common.xml",SystemID=2) sets the system ID to 2.

Input Arguments

expand all

MAVLink message definition, specified as a mavlinkdialect object

MAVLink message definition XML file name, specified as a string scalar or character vector

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

Data Types: string | char

MAVLink protocol version, specified as 2 or 1

Data Types: double

Name-Value Arguments

expand all

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.

Data Types: double

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

Data Types: double

MAVLink component type, specified as a string scalar or character vector.

You must choose a MAVLink component type that is available in the MAV_TYPE enum. To see the MAV_TYPE enum, use the enuminfo function by running this command:

enuminfo(dialect,"MAV_TYPE").Entries{1,1}

Data Types: string | char

MAVLink autopilot type, specified as a string scalar or character vector.

You must choose a MAVLink autopilot type that is available in the MAV_AUTOPILOT enum. To see the MAV_AUTOPILOT enum, use the enuminfo function by running this command:

enuminfo(dialect,"MAV_AUTOPILOT").Entries{1,1}

Data Types: string | char

Properties

expand all

MAVLink message definition, returned as a mavlinkdialect object.

This property is read-only.

Local MAVLink client information, returned as a structure. The structure contains these fields:

  • SystemID

  • ComponentID

  • ComponentType

  • AutopilotType

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:46583"

listTopics(mavlink)
ans =

  0×5 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