Connecting to a Vehicle

The connection to the vehicle (or multiple vehicles) is set up within the DroneKit script. Scripts import and call the connect() method. After connecting this returns a Vehicle object from which you can get/set parameters and attributes, and control vehicle movement.

The most common way to call connect() is shown below:

from dronekit import connect

# Connect to the Vehicle (in this case a UDP endpoint)
vehicle = connect('127.0.0.1:14550', wait_ready=True)

The first parameter specifies the target address (in this case the loopback address for UDP port 14550). See Connection string options for the strings to use for other common vehicles.

The second parameter (wait_ready) is used to determine whether connect() returns immediately on connection or if it waits until some vehicle parameters and attributes are populated. In most cases you should use wait_ready=True to wait on the default set of parameters.

Connecting over a serial device will look something like this:

from dronekit import connect

# Connect to the Vehicle (in this case a UDP endpoint)
vehicle = connect('/dev/ttyAMA0', wait_ready=True, baud=57600)

Tip

If the baud rate is not set correctly, connect may fail with a timeout error. It is best to set the baud rate explicitly.

connect() also has arguments for setting the baud rate, the length of the connection timeout, and/or to use a custom vehicle class.

There is more documentation on all of the parameters in the API Reference.

Connection string options

The table below shows connection strings you can use for some of the more common connection types:

Connection type Connection string
Linux computer connected to the vehicle via USB /dev/ttyUSB0
Linux computer connected to the vehicle via Serial port (RaspberryPi example) /dev/ttyAMA0 (also set baud=57600)
SITL connected to the vehicle via UDP 127.0.0.1:14550
SITL connected to the vehicle via TCP tcp:127.0.0.1:5760
OSX computer connected to the vehicle via USB dev/cu.usbmodem1
Windows computer connected to the vehicle via USB (in this case on COM14) com14
Windows computer connected to the vehicle using a 3DR Telemetry Radio on COM14 com14 (also set baud=57600)

Tip

The strings above are the same as are used when connecting the MAVProxy GCS. For other options see the MAVProxy documentation.

Note

The default baud rate may not be appropriate for all connection types (this may be the cause if you can connect via a GCS but not DroneKit).

Connecting to multiple vehicles

You can control multiple vehicles from within a single script by calling connect() for each vehicle with the appropriate connection strings.

The returned Vehicle objects are independent of each other and can be separately used to control their respective vehicle.