メインコンテンツ

mavlinkio

Create local MAVLink client

Description

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

Creation

Description

mavlink = mavlinkio(msgDefinitions) creates a local MAVLink client using a MAVLink message definition specified as a mavlinkdialect object.

mavlink = mavlinkio(dialectXML) creates a local MAVLink client using a MAVLink message definition specified as a an XML file.

example

mavlink = mavlinkio(dialectXML,version) specifies the MAVLink protocol version of the XML file..

mavlink = mavlinkio(___,Name=Value) specifies options using one or more name-value arguments in addition to any combination of input arguments from previous syntaxes.. For example, SystemID=2 sets the MAVLink system ID to 2.

Input Arguments

expand all

MAVLink message definition, specified as a mavlinkdialect object.

MAVLink message definition XML filename, 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.

Example: mavlinkio("common.xml",SystemID=2) sets the MAVLink system ID to 2.

MAVLink system ID, specified as an integer in the range [0, 255].

Data Types: double

MAVLink component ID, specified as an integer in the range [0, 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 components available in the MAV_TYPE enum, use the enuminfo function.

If you create the mavlinkio object using MAVLink message definition specified as a mavlinkdialect object named MavlinkDialectObject:

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

If you create the mavlinkio object using MAVLink message definition specified as an XML file named dialectXML.xml:

MavlinkDialectObject = mavlinkdialect("dialectXML.xml")

enuminfo(MavlinkDialectObject,"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 autopilots available in the MAV_AUTOPILOT enum, use the enuminfo function.

If you create the mavlinkio object using MAVLink message definition specified as a mavlinkdialect object named MavlinkDialectObject:

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

If you create the mavlinkio object using MAVLink message definition specified as an XML file named dialectXML.xml:

MavlinkDialectObject = mavlinkdialect("dialectXML.xml")

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

Data Types: string | char

Properties

expand all

MAVLink message definition, specified as a mavlinkdialect object.

This property is read-only.

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

  • SystemID — MAVLink system ID.

  • ComponentID — MAVLink component ID.

  • ComponentType — MAVLink component type.

  • AutopilotType — MAVLink autopilot type.

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)

Create a MAVLink dialect object using the common.xml file.

dialect = mavlinkdialect("common.xml");

Create a local MAVLink client object that represents a simulated UAV. Specify these options:

  • MAVLink message definition — dialect

  • MAVLink system ID — 1

  • MAVLink component ID — 1

  • MAVLink component type — MAV_TYPE_QUADROTOR

  • MAVLink autopilot type — MAV_AUTOPILOT_GENERIC

uavClient = mavlinkio(dialect,SystemID=1,ComponentID=1,AutopilotType="MAV_AUTOPILOT_GENERIC",ComponentType="MAV_TYPE_QUADROTOR");

Connect the local simulated UAV to a random open UDP port by using the connect object function.

connect(uavClient,"UDP");

List all MAVLink clients that are connected to the simulated UAV by using the listClients object function. The output contains only the simulated UAV, which shows that no other MAVLink client is connected to the simulated UAV.

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

       1             1         "MAV_TYPE_QUADROTOR"    "MAV_AUTOPILOT_GENERIC"

Launch QGroundControl.

To establish a connection with QGroundControl, first create a blank heartbeat message by using the createmsg object function.

heartbeat = createmsg(dialect,"HEARTBEAT");

Assign the component type and autopilot type of the simulated UAV, and the system status, to the heartbeat message fields.

heartbeat.Payload.type(:) = enum2num(dialect,MAV_TYPE=uavClient.LocalClient.ComponentType);
heartbeat.Payload.autopilot(:) = enum2num(dialect,MAV_AUTOPILOT=uavClient.LocalClient.AutopilotType);
heartbeat.Payload.system_status(:) = enum2num(dialect,MAV_STATE="MAV_STATE_STANDBY");

Create a timer callback function. Configure the callback function to send the heartbeat message to QGroundControl through a UDP connection using the sendudpmsg object function.

localHost = "127.0.0.1";
qgcPort = 14550;
heartBeatTimerFunction = @(~,~)sendudpmsg(uavClient,heartbeat,localHost,qgcPort);

Create a timer object with a fixed rate execution mode, period of 1 second, and the timer callback function.

heartbeatTimer = timer(ExecutionMode="fixedRate",Period=1,TimerFcn=heartBeatTimerFunction);

Start the timer object. The local simulated UAV broadcasts the heartbeat message at 1 Hz, and QGroundControl connects to the simulated UAV.

start(heartbeatTimer)

List the MAVLink clients again to verify that QGroundControl is now connected. The output shows that QGroundControl is a connected MAVLink client with a component type of MAV_TYPE_GCS.

listClients(uavClient)

Once you have finished running the example, stop and delete the timer object to end the connection.

stop(heartbeatTimer)
delete(heartbeatTimer)

Version History

Introduced in R2019a

expand all