Example: Follow Me

This is a significantly more complex example – showing closed-loop control of the vehicle. It will use a USB GPS attached to your laptop to have the vehicle follow you as you walk around a field.

Run this example with caution - be ready to exit follow-me mode by switching the flight mode switch on your RC radio.

In practice, you don’t really want to use this follow-me implementation, rather you can use this example as a starting point to build your own custom application.

Before running this demo you’ll need to make sure your computer has the gpsd service installed.

Ubuntu install

apt-get install gpsd gpsd-clients

You can then plug in a USB GPS and run the “xgps” client to confirm that it is working. If you do not have a USB GPS you can use simulated data by running droneapi-python/examples/run-fake-gps.sh.

Once your GPS is plugged in you can start follow-me by running the following command inside of MAVProxy:

RTL> api start follow_me.py
RTL> Going to: Location:lat=50.616468333,lon=7.131903333,alt=30,is_relative=True
Got MAVLink msg: MISSION_ACK {target_system : 255, target_component : 0, type : 0}
Going to: Location:lat=50.616468333,lon=7.131903333,alt=30,is_relative=True
Got MAVLink msg: MISSION_ACK {target_system : 255, target_component : 0, type : 0}

These debugging messages will appear every two seconds - when a new target position is sent to the vehicle, to stop follow-me either change the vehicle mode switch on your RC transmitter or type “api stop”.

The source code for this example is a good starting point for your own application, from here you can use all python language features and libraries (OpenCV, classes, lots of packages etc...)

Next, take a look at the full DroneKit-Python API Reference for more information.

Source code

The full source code at documentation build-time is listed below (current version on github):

import gps
import socket
import time
from droneapi.lib import VehicleMode, Location

def followme():
    followme - A DroneAPI example

    This is a somewhat more 'meaty' example on how to use the DroneAPI.  It uses the
    python gps package to read positions from the GPS attached to your laptop an
    every two seconds it sends a new goto command to the vehicle.

    To use this example:
    * Run mavproxy.py with the correct options to connect to your vehicle
    * module load api
    * api start <path-to-follow_me.py>

    When you want to stop follow-me, either change vehicle modes from your RC
    transmitter or type "api stop".
        # First get an instance of the API endpoint (the connect via web case will be similar)
        api = local_connect()

        # Now get our vehicle (we assume the user is trying to control the first vehicle attached to the GCS)
        v = api.get_vehicles()[0]

        # Don't let the user try to fly while the board is still booting
        if v.mode.name == "INITIALISING":
            print "Vehicle still booting, try again later"

        cmds = v.commands
        is_guided = False  # Have we sent at least one destination point?

        # Use the python gps package to access the laptop GPS
        gpsd = gps.gps(mode=gps.WATCH_ENABLE)

        while not api.exit:
            # This is necessary to read the GPS state from the laptop

            if is_guided and v.mode.name != "GUIDED":
                print "User has changed flight modes - aborting follow-me"

            # Once we have a valid location (see gpsd documentation) we can start moving our vehicle around
            if (gpsd.valid & gps.LATLON_SET) != 0:
                altitude = 30  # in meters
                dest = Location(gpsd.fix.latitude, gpsd.fix.longitude, altitude, is_relative=True)
                print "Going to: %s" % dest

                # A better implementation would only send new waypoints if the position had changed significantly
                is_guided = True

                # Send a new target every two seconds
                # For a complete implementation of follow me you'd want adjust this delay
    except socket.error:
        print "Error: gpsd service does not seem to be running, plug in USB GPS or run run-fake-gps.sh"