主要内容

start

R2026b

Start sending HEARTBEAT messages

Since R2026b

    Description

    start(heartbeat,mavlinkclient,connectionType) starts sending HEARTBEAT messages to the remote MAVLink client mavlinkclient using the specified connection type. The component type and autopilot type fields in the HEARTBEAT messages are identical to the corresponding fields of the mavlinkio object used to create the heartbeat microservice.

    start(___,Name=Value) specifies options using one or more name-value arguments in addition to the input arguments from the previous syntax. For example, RemotePort=14550 specifies to send HEARTBEAT messages to UDP port 14550.

    example

    Examples

    collapse all

    Connect a local MAVLink client to a serial port and establish communication with a Pixhawk® 4 board running PX4® firmware by exchanging heartbeat messages.

    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 code stores information for a Pixhawk board with a system ID of 1 and a component ID of 1.

    uav = mavlinkclient(gcs,1,1)
    uav = 
      mavlinkclient with properties:
    
             SystemID: 1
          ComponentID: 1
        ComponentType: "Unknown"
        AutopilotType: "Unknown"

    Connect the local MAVLink client to the same serial port as the Pixhawk board by using the connect function. In this code, the Pixhawk board uses the "COM5" port.

    connect(gcs,"Serial",SerialPort="COM5");

    Create a heartbeat microservice object.

    heartbeat = mavlinkmicroservice(gcs,"heartbeat");

    Start sending 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, the local MAVLink client establishes a connection to it.

    Verify that a connection has been established by using the listClients function. Note that the heartbeat messages include the ComponentType and AutopilotType information of the Pixhawk board.

    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"

    Verify that the ComponentType and AutopilotType properties of the mavlinkclient object have been automatically updated to match the heartbeat message values.

    uav
    uav = 
      mavlinkclient with properties:
    
             SystemID: 1
          ComponentID: 1
        ComponentType: "MAV_TYPE_QUADROTOR"
        AutopilotType: "MAV_AUTOPILOT_PX4"

    To stop the connection, first stop the heartbeat message stream.

    stop(heartbeat,uav)

    Then, disconnect the local MAVLink client from the serial port.

    disconnect(gcs)

    Input Arguments

    collapse all

    Heartbeat microservice, specified as a heartbeatMicroservice object.

    Remote MAVLink client information, specified as a mavlinkclient object. The argument specifies the client that receives the HEARTBEAT messages.

    The object stores the system and component identifiers of the remote client, and its ComponentType and AutopilotType properties update automatically when the local MAVLink client used by heartbeat receives a reply HEARTBEAT message whose identifiers match those stored in this mavlinkclient object.

    Connection type, specified as one of these options.

    • "UDP" — Send HEARTBEAT messages through an existing UDP connection. You must specify the remote host IP address and port using the RemoteHost and RemotePort arguments, respectively.

    • "Serial" — Send HEARTBEAT messages through an existing serial connection. You must specify the serial port using the SerialPort argument.

    Data Types: string

    Name-Value Arguments

    collapse all

    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.

    Example: RemotePort=14550 sends HEARTBEAT messages to remote UDP port 14550.

    Remote host IP address, specified as a string scalar.

    Example: "192.168.4.1"

    Data Types: string

    Remote host port number, specified as an integer in the range [0, 65535].

    Example: 14550

    Data Types: double

    Serial port name, specified as a string scalar. To send and receive HEARTBEAT messages over a serial connection, you must first connect the local MAVLink client used by heartbeat to this port using the connect function.

    Example: "COM2" or "/dev/ttyUSB0"

    Data Types: string

    System status field of the outgoing HEARTBEAT message, specified as a string scalar. This value must be a name contained in the MAV_STATE enum.

    Tip

    To view available MAV_STATE names, use the enuminfo function:

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

    Example: "MAV_STATE_BOOT"

    Data Types: string

    Base mode field of the outgoing HEARTBEAT message, specified as a string scalar. This value must be a name contained in the MAV_MODE_FLAG enum.

    Tip

    To view available MAV_MODE_FLAG names, use the enuminfo function:

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

    Example: "MAV_MODE_FLAG_CUSTOM_MODE_ENABLED"

    Data Types: string

    Custom mode field of the outgoing HEARTBEAT message, specified as a nonnegative integer.

    Example: 9

    Version History

    Introduced in R2026b