Main Content


Create MAVLink command message

Since R2019a



cmdMsg = createcmd(dialect,cmdSetting,cmdType) returns a blank COMMAND_INT or COMMAND_LONG message structure based on the command setting and type. The command definitions are contained in the mavlinkdialect object, dialect.


collapse all

This example shows how to parse a MAVLink XML file and create messages and commands from the definitions.

Parse and store the MAVLink dialect XML. Specify the XML path. The default "common.xml" dialect is provided. This XML file contains all the message and enum definitions.

dialect = mavlinkdialect("common.xml");

Create a MAVLink command from the MAV_CMD enum, which is an enum of MAVLink commands to send to the UAV. Specify the setting as "int" or "long", and the type as an integer or string.

cmdMsg = createcmd(dialect,"long",22)
cmdMsg = struct with fields:
      MsgID: 76
    Payload: [1x1 struct]

Verify the command name using num2enum. Command 22 is a take-off command for the UAV. You can convert back to an ID using enum2num. Your dialect can contain many different enums with different names and IDs.

cmdName = num2enum(dialect,"MAV_CMD",22)
cmdName = 
cmdID = enum2num(dialect,"MAV_CMD",cmdName)
cmdID = 22

Use enuminfo to view the table of the MAV_CMD enum entries.

info = enuminfo(dialect,"MAV_CMD");
ans=148×3 table
                    Name                     Value                                                                                                                                                                                                                  Description                                                                                                                                                                                                              
    _____________________________________    _____    _______________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________

    "MAV_CMD_NAV_WAYPOINT"                    16      "Navigate to waypoint."                                                                                                                                                                                                                                                                                                                                                                                                                
    "MAV_CMD_NAV_LOITER_UNLIM"                17      "Loiter around this waypoint an unlimited amount of time"                                                                                                                                                                                                                                                                                                                                                                              
    "MAV_CMD_NAV_LOITER_TURNS"                18      "Loiter around this waypoint for X turns"                                                                                                                                                                                                                                                                                                                                                                                              
    "MAV_CMD_NAV_LOITER_TIME"                 19      "Loiter at the specified latitude, longitude and altitude for a certain amount of time. Multicopter vehicles stop at the point (within a vehicle-specific acceptance radius). Forward-only moving vehicles (e.g. fixed-wing) circle the point with the specified radius/direction. If the Heading Required parameter (2) is non-zero forward moving aircraft will only leave the loiter circle once heading towards the next waypoint."
    "MAV_CMD_NAV_RETURN_TO_LAUNCH"            20      "Return to launch location"                                                                                                                                                                                                                                                                                                                                                                                                            
    "MAV_CMD_NAV_LAND"                        21      "Land at location."                                                                                                                                                                                                                                                                                                                                                                                                                    
    "MAV_CMD_NAV_TAKEOFF"                     22      "Takeoff from ground / hand. Vehicles that support multiple takeoff modes (e.g. VTOL quadplane) should take off using the currently configured mode."                                                                                                                                                                                                                                                                                  
    "MAV_CMD_NAV_LAND_LOCAL"                  23      "Land at local position (local frame only)"                                                                                                                                                                                                                                                                                                                                                                                            
    "MAV_CMD_NAV_TAKEOFF_LOCAL"               24      "Takeoff from local position (local frame only)"                                                                                                                                                                                                                                                                                                                                                                                       
    "MAV_CMD_NAV_FOLLOW"                      25      "Vehicle following, i.e. this waypoint represents the position of a moving vehicle"                                                                                                                                                                                                                                                                                                                                                    
    "MAV_CMD_NAV_CONTINUE_AND_CHANGE_ALT"     30      "Continue on the current course and climb/descend to specified altitude. When the altitude is reached continue to the next command (i.e., don't proceed to the next command until the desired altitude is reached."                                                                                                                                                                                                                    
    "MAV_CMD_NAV_LOITER_TO_ALT"               31      "Begin loiter at the specified Latitude and Longitude. If Lat=Lon=0, then loiter at the current position. Don't consider the navigation command complete (don't leave loiter) until the altitude has been reached. Additionally, if the Heading Required parameter is non-zero the aircraft will not leave the loiter until heading toward the next waypoint."                                                                         
    "MAV_CMD_DO_FOLLOW"                       32      "Begin following a target"                                                                                                                                                                                                                                                                                                                                                                                                             
    "MAV_CMD_DO_FOLLOW_REPOSITION"            33      "Reposition the MAV after a follow target command has been sent"                                                                                                                                                                                                                                                                                                                                                                       
    "MAV_CMD_DO_ORBIT"                        34      "Start orbiting on the circumference of a circle defined by the parameters. Setting any value NaN results in using defaults."                                                                                                                                                                                                                                                                                                          
    "MAV_CMD_NAV_ROI"                         80      "Sets the region of interest (ROI) for a sensor set or the vehicle itself. This can then be used by the vehicle's control system to control the vehicle attitude and the attitude of various sensors such as cameras."                                                                                                                                                                                                                 

Query the dialect for a specific message ID. Create a blank MAVLink message using the message ID.

info = msginfo(dialect,"HEARTBEAT")
info=1×4 table
    MessageID    MessageName                                                                                                                                                                                             Description                                                                                                                                                                                               Fields   
    _________    ___________    _____________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________    ___________

        0        "HEARTBEAT"    "The heartbeat message shows that a system or component is present and responding. The type and autopilot fields (along with the message component id), allow the receiving system to treat further messages from this system appropriately (e.g. by laying out the user interface based on the autopilot). This microservice is documented at"    {6x6 table}

msg = createmsg(dialect,info.MessageID);

Input Arguments

collapse all

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

Command setting, specified as either "int" or "long" for either a COMMAND_INT or COMMAND_LONG command.

Command type, specified as either a positive integer or string. If specified as an integer, the command definition with the matching ID from the MAV_CMD enum in dialect is returned. If specified as a string, the command with the matching name is returned.

To get the command types for the MAV_CMD enum, use enuminfo:

enumTable = enuminfo(dialect,"MAV_CMD")

Output Arguments

collapse all

MAVLink command message, returned as a structure with the fields:

  • MsgID: Positive integer for message ID.

  • Payload: Structure containing fields for the specific message definition.

Version History

Introduced in R2019a