Main Content

mavlinkclient

R2026b

Store remote MAVLink client information

Description

The mavlinkclient object stores the information of a remote MAVLink client that the local MAVLink client mavlinkio communicates with.

When the local MAVLink client receives a HEARTBEAT message whose system and component IDs match those stored in this mavlinkclient object, the ComponentType and AutopilotType properties update automatically to the ComponentType and AutopilotType of the remote MAVLink client.

Creation

Description

client = mavlinkclient(mavlink,sysID,compID) creates a mavlinkclient object that represents a remote MAVLink client identified by sysID and compID. The ComponentType and AutopilotType properties of the object update automatically when the local MAVLink client mavlinkio receives a HEARTBEAT message with matching identifiers.

Tip

Use the heartbeatMicroservice object to enable the local MAVLink client mavlinkio to exchange HEARTBEAT messages with a remote MAVLink client.

example

Input Arguments

expand all

Local MAVLink client, specified as a mavlinkio object.

Remote MAVLink system ID, specified as an integer in the range [1, 255]. The MAVLink protocol supports up to 255 systems. Typically, each UAV has its own system ID, but multiple UAVs can share a system ID if they are treated as one system.

Data Types: numeric

Remote MAVLink component ID, specified as an integer in the range [0, 255].

Data Types: numeric

Properties

expand all

Remote MAVLink system ID, specified as an integer in the range [1, 255]. The MAVLink protocol supports up to 255 systems. Typically, each UAV has its own system ID, but multiple UAVs can share a system ID if they are treated as one system.

Data Types: uint8

Remote MAVLink component ID, specified as an integer in the range [0, 255].

Data Types: uint8

Remote MAVLink component type, specified as a string. This value updates automatically when the local MAVLink client mavlink receives a HEARTBEAT message whose system and component IDs match those stored in this mavlinkclient object.

Example: "MAV_TYPE_GCS"

Data Types: string

Remote MAVLink autopilot component type, specified as a string. This value updates automatically when the local MAVLink client mavlink receives a HEARTBEAT message whose system and component IDs match those stored in this mavlinkclient object.

Example: "MAV_AUTOPILOT_INVALID"

Data Types: string

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)

Tune the maximum yaw rate parameter of a Pixhawk 4 board running PX4 firmware by using the parameter 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. If the remote MAVLink system replies with HEARTBEAT messages, a connection is established.

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 a parameter microservice object.

parameter = mavlinkmicroservice(gcs,"parameter");

Obtain the current value of all parameters by using the get function.

[status,param] = get(parameter,uav)
status = true
param = 10×5 table
    param_value   param_count    param_index       param_id           param_type     
    ___________   ___________    ___________    ______________    ____________________
        0             942            12         SYS_AUTOSTART             6            
        1             942            22         COM_ARM_WO_GPS            6            
      300             942            50         COM_RC_LOSS_T             9            
     15.5             942            88         BAT_LOW_THR               9            
     12.0             942           105         MPC_VEL_MAX_UP            9            
      5.0             942           106         MPC_VEL_MAX_DN            9            
      2.5             942           315         NAV_ACC_RAD               9            
      200             942           412         MC_YAWRATE_MAX            9            
      0.5             942           413         MC_PITCHRATE_P            9            
      0.1             942           418         MC_ROLLRATE_I             9            

Obtain the current value of the MC_YAWRATE_MAX parameter. The output shows that the current maximum yaw rate of the Pixhawk board is 200 degrees per second.

[status,param] = get(parameter,uav,"MC_YAWRATE_MAX")
status = true
param = 1×5 table
    param_value   param_count    param_index       param_id           param_type     
    ___________   ___________    ___________    ______________    ____________________
       200            942            412        MC_YAWRATE_MAX            9          

Specify a new maximum yaw rate parameter value of 150 degrees per second by using the set function. The function returns the updated parameter value to verify the success of the parameter set operation.

[status,param] = set(parameter,uav,"MC_YAWRATE_MAX",150)
status = true
param = 1×5 table
    param_value   param_count    param_index       param_id           param_type     
    ___________   ___________    ___________    ______________    ____________________
        150           942            412        MC_YAWRATE_MAX            9          

Version History

Introduced in R2019a