メインコンテンツ

commandMicroservice

R2026b

MAVLink Command microservice object

Since R2026b

    Description

    The commandMicroservice represents the MAVLink Command microservice. This object uses MAVLink command protocol messages to send commands to a remote MAVLink client. Use the object functions to send commands or to configure timeout and retry behavior for command operations.

    Creation

    To create a commandMicroservice object, use the mavlinkmicroservice function and specify the microservice type as "command":

    command = mavlinkmicroservice(mavlinkio,"command").

    Properties

    expand all

    Maximum time, in seconds, that the commandMicroservice object waits for acknowledgment message before retrying to send a command, specified as a positive scalar. To change this value, use the setTimeout function.

    Number of retry attempts for sending a command that do not complete within the timeout period, specified as a nonnegative integer. To change this value, use the setRetry function.

    Object Functions

    sendSend command to remote MAVLink client
    setTimeoutSet microservice timeout value
    setRetrySet microservice retry count

    Examples

    collapse all

    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
    
         

    Version History

    Introduced in R2026b