Main Content

send

R2026b

Send command to remote MAVLink client

Since R2026b

    Description

    [status,ack] = send(command,mavlinkclient,commandMsg) sends the MAVLink command message commandMsg to the remote MAVLink client mavlinkclient. The function returns the command send status and the acknowledgment message returned by the remote MAVLink client.

    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
    
         

    Input Arguments

    collapse all

    Command microservice, specified as a commandMicroservice object.

    Remote MAVLink client information, specified as a mavlinkclient object.

    MAVLink command message to send, specified as a structure with these fields:

    • MsgID — Message identifier, specified as 75 for COMMAND_INT commands, or 76 for COMMAND_LONG commands.

    • Payload — Structure with fields corresponding to the specific command definition.

    Tip

    Create the command message using the createcmd object function of the MAVLink dialect object.

    Data Types: struct

    Output Arguments

    collapse all

    Command send status, returned as a 1 or 0 of data type logical. The value is 1 (true) when the local MAVLink client receives the acknowledgement message without exceeding the maximum time and number of retries.

    Data Types: logical

    Command acknowledgement message, returned as a structure with these fields:

    • command — Command identifier, returned as an integer corresponding to a value in the MAV_CMD enum.

    • result — Command result, returned as an integer corresponding to a value in the MAV_RESULT enum.

    • progress — Progress percentage when the result is MAV_RESULT_IN_PROGRESS. Values range from 0 to 100, or 255 if the progress is unknown.

    • result_param2 — Additional result information.

    • target_system — Remote MAVLink system ID, returned as an integer in the range [1, 255].

    • target_component — Remote MAVLink component ID, returned as an integer in the range [0, 255].

    Tip

    To view the available MAV_CMD enum values, use the enuminfo function. For example, to find the values of the MAV_CMD enum:

    info = enuminfo(dialect,"MAV_CMD");
    info.Entries{:}
    where dialect is the MAVLink dialect object that you specify when you create the local MAVLink client.

    Version History

    Introduced in R2026b