Connecting to Systems (Vehicles)

MAVSDK allows you to connect to multiple vehicles attached to the local WiFi network and/or via serial ports.

In order to detect vehicles you must first specify the communication ports that MAVSDK will monitor for new systems. Once monitoring a port, MAVSDK will automatically detect connected vehicles, add them to its collection, and notify registered users of connection and disconnection events.

Monitoring a Port

Specify the port(s) to watch using one of the (synchronous) connection methods: add_any_connection(), add_udp_connection(), add_tcp_connection() or add_serial_connection(). All the methods are used similarly, and return immediately with a ConnectionResult indicating whether they succeeded.

The add_any_connection() method can be used to set up monitoring for any of the supported port types (while the other methods set up specific connection types). The connection details are specified using the string formats shown below:

Connection URL Format
UDP udp://[Bind_host][:Bind_port]
TCP tcp://[Server_host][:Server_port]
Serial serial://[Dev_Node][:Baudrate]

The code snippet below shows how to set up monitoring with add_any_connection():

Mavsdk mavsdk;
ConnectionResult connection_result = mavsdk.add_any_connection("udp://:14540");
ASSERT_EQ(connection_result, ConnectionResult::Success)

The connection string used above (udp://:14540) is to the standard PX4 UDP port for off-board APIs (14540). This is the normal/most common way for offboard APIs to connect to PX4 over WiFi.

The code fragment below shows how you might print the string for the preceding code fragment to the console:

std::cout << "Connection result: " << connection_result << '\n';

Register for System-Detection Notifications

MAVSDK monitors any added communication ports for new systems. Clients can register for notification when new systems are discovered using subscribe_on_new_system().

The code fragment below shows how to register a callback (in this case the callback is a lambda function that just prints if a new vehicle has been discovered.

Mavsdk mavsdk;
... //add ports
mavsdk.register_on_new_system([]() {
    std::cout << "Discovered new system\n";

Iterating all Systems

You can iterate all systems that have been detected by Mavsdk (since it was started, over all communication ports) using the systems() method. This returns a vector of shared pointers to systems.

The following code fragment shows how to iterate through the systems and checking their MAVLink system ID, whether they are connected, and what components they have:

//Iterate through detected systems
for (auto system : {
    std::cout << "Found system with MAVLink system ID: " << static_cast<int>(system->get_system_id())
              << ", connected: " << (system->is_connected() ? "yes" : "no")
              << ", has autopilot: " << (system->has_autopilot() ? "yes" : "no") << '\n';

Accessing Systems

To access a certain system, pick the one from the vector that you require, or use the first one if only one system is assumed:

   Mavsdk mavsdk;
   // Wait for the system to connect via heartbeat
   while (mavsdk.system().size() == 0) {
   // System got discovered.
   std::shared_ptr<System> system =[0];

The System is used by the MAVSDK plugin classes to query and control the vehicle. For more information see Using Plugins (and the other guide topics).

© Dronecode 2017-2020. License: CC BY 4.0            Updated: 2021-04-14 07:04:25

results matching ""

    No results matching ""