Contenido principal

mavlinkmicroservice

R2026b

Create MAVLink Microservice Object

Since R2026b

    Description

    microservice = mavlinkmicroservice(mavlinkio, type) creates a MAVLink microservice object of the type specified by type using the local MAVLink client mavlinkio.

    MAVLink microservices are higher-level protocols that enable interoperability between MAVLink systems such as ground control stations and autopilots. Each microservice handles a specific type of communication, including exchanging parameters, missions, files, and commands, with built-in support for acknowledgment, retransmission, and error handling.

    These are the supported microservice types:

    • "heartbeat" — Create a heartbeatMicroservice object that sends periodic heartbeat messages to advertise system status and establish connectivity.

    • "parameter" — Create a parameterMicroservice object that reads and writes parameters on a remote MAVLink client.

    • "command" — Create a commandMicroservice object that sends commands to a remote MAVLink client.

    • "mission" — Create a missionMicroservice object that uploads and downloads missions on a remote MAVLink client. This microservice requires PX4 version 1.5.1 or later. If you use ArduPilot, it requires Copter 3.4 or later, or Plane 3.5 or later.

    • "ftp" — Create an ftpMicroservice object that exchanges files with a remote MAVLink client.

    example

    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

    Local MAVLink client, specified as a mavlinkio object.

    Microservice type, specified as one of these options. This argument determines the type of the returned MAVLink microservice object.

    Value of typeMAVLink microservice object

    "heartbeat"

    heartbeatMicroservice

    "parameter"

    parameterMicroservice

    "command"

    commandMicroservice

    "mission"

    missionMicroservice

    "ftp"

    ftpMicroservice

    Data Types: string

    Output Arguments

    collapse all

    MAVLink microservice object, returned as a heartbeatMicroservice, parameterMicroservice, commandMicroservice, missionMicroservice, or ftpMicroservice object. The value of the type argument determines the type of MAVLink microservice object that is returned.

    Version History

    Introduced in R2026b

    See Also

    |

    External Websites