mavlinkio
R2026bCreate local MAVLink client
Description
The mavlinkio object represents a local MAVLink client. After you
create a mavlinkio object, you can connect to UDP or serial port, and
exchange messages with other MAVLink clients such as UAV and ground control stations using the
object functions.
Creation
Syntax
Description
creates a local MAVLink client using a MAVLink message definition specified as a mavlink = mavlinkio(msgDefinitions)mavlinkdialect
object.
creates a local MAVLink client using a MAVLink message definition specified as an XML
file.mavlink = mavlinkio(dialectXML)
specifies the MAVLink protocol version of the XML file.mavlink = mavlinkio(dialectXML,version)
specifies options using one or more name-value arguments in addition to any combination
of input arguments from previous syntaxes. For example, mavlink = mavlinkio(___,Name=Value)SystemID=2
sets the MAVLink system ID to 2.
Input Arguments
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
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.
Tip
To see the components available in the MAV_TYPE enum:
If you create the
mavlinkioobject using MAVLink message definition specified as amavlinkdialectobject namedMavlinkDialectObject, use theenuminfofunction by running this command:enuminfo(MavlinkDialectObject,"MAV_TYPE").Entries{1,1}If you create the
mavlinkioobject using MAVLink message definition specified as an XML file nameddialectXML.xml, first create amavlinkdialectobject by running this command:MavlinkDialectObject = mavlinkdialect("dialectXML.xml")Then, use the
enuminfofunction by running this command: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.
Tip
To see the autopilots available in the MAV_AUTOPILOT
enum:
If you create the
mavlinkioobject using MAVLink message definition specified as amavlinkdialectobject namedMavlinkDialectObject, use theenuminfofunction by running this command:enuminfo(MavlinkDialectObject,"MAV_AUTOPILOT").Entries{1,1}If you create the
mavlinkioobject using MAVLink message definition specified as an XML file nameddialectXML.xml, first create amavlinkdialectby running this command:MavlinkDialectObject = mavlinkdialect("dialectXML.xml")Then, use the
enuminfofunction by running this command:enuminfo(MavlinkDialectObject,"MAV_AUTOPILOT").Entries{1,1}
Data Types: string | char
Properties
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
connect | Connect MAVLink client to UDP or serial port |
disconnect | Disconnect local MAVLink client from UDP or serial ports |
sendmsg | Send MAVLink message to other connected MAVLink clients |
listConnections | List all active MAVLink connections |
listClients | List all connected MAVLink clients |
listTopics | List all topics received by MAVLink client |
sendudpmsg | Send MAVLink message to UDP port |
sendserialmsg | Send MAVLink message to serial port |
serializemsg | Serialize MAVLink message to binary buffer |
Examples
Tune the maximum yaw rate parameter of a Pixhawk® 4 board running PX4® firmware by using the parameter microservice.
Create a mavlinkdialect
object using the common.xml file.
dialect = mavlinkdialect("common.xml");Create a local MAVLink client by using the mavlinkio object.
gcs = mavlinkio(dialect);
Store the remote MAVLink client information by using the mavlinkclient object. This Pixhawk board has a system ID of 1 and a component ID of 1.
uav = mavlinkclient(gcs,1,1);
Connect the local MAVLink client to the same serial port as the Pixhawk board by using the connect
function. This Pixhawk board uses the COM5 port.
connect(gcs,"Serial",SerialPort="COM5");
Create a heartbeat microservice object.
heartbeat = mavlinkmicroservice(gcs,"heartbeat");Start sending the HEARTBEAT messages to the Pixhawk board by using the start function. If the remote MAVLink system replies with HEARTBEAT messages, a connection is established.
start(heartbeat,uav,"Serial",SerialPort="COM5")
If the Pixhawk board replies with HEARTBEAT messages, a connection is established. Verify that the connection has been established by using the listClients function.
listClients(gcs)
ans = 2×4 table
SystemID ComponentID ComponentType AutopilotType
________ ___________ ____________________ _______________________
255 1 "MAV_TYPE_GCS" "MAV_AUTOPILOT_INVALID"
1 1 "MAV_TYPE_QUADROTOR" "MAV_AUTOPILOT_PX4"Create a parameter microservice object.
parameter = mavlinkmicroservice(gcs,"parameter");Obtain the current value of all parameters by using the get
function.
[status,param] = get(parameter,uav)
status = true
param = 10×5 table
param_value param_count param_index param_id param_type
___________ ___________ ___________ ______________ ____________________
0 942 12 SYS_AUTOSTART 6
1 942 22 COM_ARM_WO_GPS 6
300 942 50 COM_RC_LOSS_T 9
15.5 942 88 BAT_LOW_THR 9
12.0 942 105 MPC_VEL_MAX_UP 9
5.0 942 106 MPC_VEL_MAX_DN 9
2.5 942 315 NAV_ACC_RAD 9
200 942 412 MC_YAWRATE_MAX 9
0.5 942 413 MC_PITCHRATE_P 9
0.1 942 418 MC_ROLLRATE_I 9
Obtain the current value of the MC_YAWRATE_MAX parameter. The
output shows that the current maximum yaw rate of the Pixhawk board is 200 degrees per second.
[status,param] = get(parameter,uav,"MC_YAWRATE_MAX")status = true
param = 1×5 table
param_value param_count param_index param_id param_type
___________ ___________ ___________ ______________ ____________________
200 942 412 MC_YAWRATE_MAX 9 Specify a new maximum yaw rate parameter value of 150 degrees per second by using the set function. The function returns the updated parameter value to verify the success of the parameter set operation.
[status,param] = set(parameter,uav,"MC_YAWRATE_MAX",150)status = true
param = 1×5 table
param_value param_count param_index param_id param_type
___________ ___________ ___________ ______________ ____________________
150 942 412 MC_YAWRATE_MAX 9 Send a waypoint command to a Pixhawk 4 board running PX4 firmware by using the command microservice.
Create a mavlinkdialect object using the common.xml file.
dialect = mavlinkdialect("common.xml");Create a local MAVLink client by using the mavlinkio object.
gcs = mavlinkio(dialect);
Store the remote MAVLink client information by using the mavlinkclient object. This Pixhawk board has a system ID of 1 and a component ID of 1.
uav = mavlinkclient(gcs,1,1);
Connect the local MAVLink client to the same serial port as the Pixhawk board by using the connect function. This Pixhawk board uses the COM5 port.
connect(gcs,"Serial",SerialPort="COM5");
Create a heartbeat microservice object.
heartbeat = mavlinkmicroservice(gcs,"heartbeat");Start sending the HEARTBEAT messages to the Pixhawk board by using the start function.
start(heartbeat,uav,"Serial",SerialPort="COM5")
If the Pixhawk board replies with HEARTBEAT messages, a connection is established.
Verify that the connection has been established by using the listClients function.
listClients(gcs)
ans = 2×4 table
SystemID ComponentID ComponentType AutopilotType
________ ___________ ____________________ _______________________
255 1 "MAV_TYPE_GCS" "MAV_AUTOPILOT_INVALID"
1 1 "MAV_TYPE_QUADROTOR" "MAV_AUTOPILOT_PX4"Create the waypoint command message structure by using the createcmd function.
cmdMsg = createcmd(dialect,"INT","MAV_CMD_NAV_WAYPOINT")
cmdMsg =
struct with fields:
MsgID: 75
Payload: [1×1 struct]
Specify these parameters of the waypoint command message:
Acceptance radius — 2 meters
Latitude — 42.2831 degrees
Longitude — –71.3468 degrees
Altitude — 50 meters
For more information on the waypoint command parameters, see the MAV_CMD_NAV_WAYPOINT section of the MAVLink documentation.
cmdMsg.Payload.param2(:) = 2.0; cmdMsg.Payload.param5(:) = 42.2831; cmdMsg.Payload.param6(:) = -71.3468; cmdMsg.Payload.param7(:) = 50.0;
Create a command microservice object.
command = mavlinkmicroservice(gcs,"command");Send the command message to the Pixhawk board by using the send function. The function returns the status and the acknowledgement message sent by the Pixhawk board.
[status,ack] = send(command,uav,cmdMsg)
status = true
ack =
struct with fields:
command: 16
result: 0
progress: 255
result_param2: 0
target_system: 1
target_component: 1
Upload a mission to a Pixhawk 4 board running PX4 firmware by using the mission microservice.
Create a mavlinkdialect object using the common.xml file.
dialect = mavlinkdialect("common.xml");Create a local MAVLink client by using the mavlinkio object.
gcs = mavlinkio(dialect);
Store the remote MAVLink client information by using the mavlinkclient object. This Pixhawk board has a system ID of 1 and a component ID of 1.
uav = mavlinkclient(gcs,1,1);
Connect the local MAVLink client to the same serial port as the Pixhawk board by using the connect function. This Pixhawk board uses the COM5 port.
connect(gcs,"Serial",SerialPort="COM5");
Create a heartbeat microservice object.
heartbeat = mavlinkmicroservice(gcs,"heartbeat");Start sending the HEARTBEAT messages to the Pixhawk board by using the start
function.
start(heartbeat,uav,"Serial",SerialPort="COM5")
If the Pixhawk board replies with HEARTBEAT messages, a connection is established.
Verify that the connection has been established by using the listClients
function.
listClients(gcs)
ans = 2×4 table
SystemID ComponentID ComponentType AutopilotType
________ ___________ ____________________ _______________________
255 1 "MAV_TYPE_GCS" "MAV_AUTOPILOT_INVALID"
1 1 "MAV_TYPE_QUADROTOR" "MAV_AUTOPILOT_PX4"Create a mission microservice object.
mission = mavlinkmicroservice(gcs,"mission");Upload the mission SampleMission.plan to the Pixhawk board by using the upload function. The function returns the upload status and acknowledgement message.
[status,ack] = upload(mission,uav,"SampleMission.plan")status = true
ack =
struct with fields:
target_system : 255
target_component : 1
type : 0
mission_type : 0
Download and delete flight logs from a Pixhawk 4 board running PX4 firmware by using the FTP microservice.
Download the latest version of common.xml file from the MAVLINK Common Message Set
(common.xml) section of the MAVLink documentation, then create a mavlinkdialect
object using the file.
dialect = mavlinkdialect("common.xml");Create a local MAVLink client by using the mavlinkio object.
gcs = mavlinkio(dialect);
Store the remote MAVLink client information by using the mavlinkclient object. This Pixhawk board has a system ID of 1 and a component ID of 1.
uav = mavlinkclient(gcs,1,1);
Connect the local MAVLink client to the same serial port as the Pixhawk board by using the connect function. This Pixhawk board uses the COM5 port.
connect(gcs,"Serial",SerialPort="COM5");
Create a heartbeat microservice object.
heartbeat = mavlinkmicroservice(gcs,"heartbeat");Start sending the HEARTBEAT messages to the Pixhawk board by using the start function.
start(heartbeat,uav,"Serial",SerialPort="COM5")
If the Pixhawk board replies with HEARTBEAT messages, a connection is established.
Verify that the connection has been established by using the listClients function.
listClients(gcs)
ans = 2×4 table
SystemID ComponentID ComponentType AutopilotType
________ ___________ ____________________ _______________________
255 1 "MAV_TYPE_GCS" "MAV_AUTOPILOT_INVALID"
1 1 "MAV_TYPE_QUADROTOR" "MAV_AUTOPILOT_PX4"Create an FTP microservice object.
ftp = mavlinkmicroservice(gcs,"ftp");List the available flight logs stored in the /fs/microsd/log directory of the Pixhawk board by using the list function.
[status,directory] = list(ftp,uav,"/fs/microsd/log")status = true
directory = 4×3 table
Type Name Size
________ _______________ _________
File 2026-03-01.ulg 7340032
File 2026-03-02.ulg 7540432
File 2026-03-03.ulg 6320031
File 2026-03-04.ulg 8221332
Download the 2026-03-02.ulg flight log into the
Documents/logs directory by using the read
function.
[status,~] = read(ftp,uav,"/fs/microsd/log/2026-03-02.ulg", ... LocalPath="Document/logs/2026-03-02.ulg")
status = true
After you have downloaded the 2026-03-02.ulg flight log, delete the
flight log from the Pixhawk board to save memory space by using the remove
function.
status = remove(ftp,uav,"/fs/microsd/log/2026-03-02.ulg")status = true
Verify that the file has been deleted by using the list function
again.
[status,directory] = list(ftp,uav,"/fs/microsd/log")status = true
directory = 3×3 table
Type Name Size
________ _______________ _________
File 2026-03-01.ulg 7340032
File 2026-03-03.ulg 6320031
File 2026-03-04.ulg 8221332
Version History
Introduced in R2019aThe mavlinkio object now displays a warning if you create the
object using an XML file that does not contain a MAVLink message definition. Previously, the
object did not display a warning if you created the object using an invalid XML file.
See Also
heartbeatMicroservice | parameterMicroservice | commandMicroservice | missionMicroservice | ftpMicroservice | mavlinkdialect | mavlinkclient
External Websites
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)