Main Content

mavlinksub

Receive MAVLink messages

Description

The mavlinksub object subscribes to topics from the connected MAVLink clients using a mavlinkio object. Use the mavlinksub object to obtain the most recently received messages and call functions to process newly received messages.

Creation

Description

sub = mavlinksub(mavlink) subscribes to all topics from all clients connected via the mavlinkio object. This syntax sets the Client property to "Any".

sub = mavlinksub(mavlink,topic) subscribes to a specific topic, specified as a string or integer, from all clients connected via the mavlinkio object. The function sets the topic input to the Topic property.

sub = mavlinksub(mavlink,client) subscribes to all topics from the client specified as a mavlinkclient object. The function sets the Client property to this input client.

sub = mavlinksub(mavlink,client,topic) subscribes to a specific topic on a specific client. The function sets the Client and Topic properties.

example

sub = mavlinksub(___,Name=Value) additionally specifies the BuffferSize or NewMessageFcn properties using name-value pairs and the previous syntaxes. The Name input is one of the property names.

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.

Input Arguments

expand all

mavlink connection, specified as a mavlinkio object

Properties

expand all

Client information of the received message, specified as a mavlinkclient object. The default value of "Any" means the subscriber is listening to all clients connected via the mavlinkio object.

Topic name the subscriber listens to, specified as a string. The default value of "Any" means the subscriber is listening to all topics on the client.

Example: "HEARTBEAT"

Data Types: char | string

Length of message buffer, specified as a positive integer. This value is the maximum number of messages that can be stored in this subscriber.

Data Types: double

Callback function for new messages, specified as a function handle. This function is called when a new message is received by the client. The function handle has the following syntax:

callback(sub,msg)
sub is a structure with fields for the Client, Topic, and BufferSize properties of the mavlinksub object. msg is the message received as a structure with the fields:

  • MsgID –– Positive integer for message ID.

  • SystemID –– System ID of MAVLink client that sent message.

  • ComponentID–– Component ID of MAVLink client that sent message.

  • Payload –– Structure containing fields based on the message definition.

  • Seq –– Positive integer for sequence of message.

The Payload is a structure defined by the message definition for the MAVLink dialect.

Data Types: function_handle

Object Functions

latestmsgsReceived messages from MAVLink subscriber

Examples

collapse all

Connect to a MAVLink client.

mavlink = mavlinkio("common.xml")
mavlink = 
  mavlinkio with properties:

        Dialect: [1x1 mavlinkdialect]
    LocalClient: [1x1 struct]

connect(mavlink,"UDP")
ans = 
"Connection1"

Get the client information.

client = mavlinkclient(mavlink,1,1);

Subscribe to the "HEARTBEAT" topic.

heartbeat = mavlinksub(mavlink,client,'HEARTBEAT');

Get the latest message. You must wait for a message to be received. Currently, no heartbeat message has been received on the mavlink object.

latestmsgs(heartbeat,1)
ans = 

  1x0 empty struct array with fields:

    MsgID
    SystemID
    ComponentID
    Payload
    Seq

Disconnect from client.

disconnect(mavlink)

Version History

Introduced in R2019a