diff --git a/docs/examples/flight_replay.rst b/docs/examples/flight_replay.rst index 4f8fac2be..e5f1747a1 100644 --- a/docs/examples/flight_replay.rst +++ b/docs/examples/flight_replay.rst @@ -2,57 +2,95 @@ Example: Flight Replay ========================= -This is an interesting demo that uses our web API to query raw flight data from a particular flight. +This example requests a past flight from Droneshare, and then "replays" the mission +by sending waypoints to a vehicle. For safety reasons, the vehicle is replayed at a height of 30 meters. +.. warning:: -Starting the demo -================= + At time of writing this example fails with an exception in DKYP2: `#355 DKPY2 Can't clear waypoints `_. -In this case, we pick some public flight from `Droneshare `_: +.. todo:: Check if `#355 DKPY2 Can't clear waypoints `_ is fixed and re-review example. -.. image:: flight_replay_example.png +You can specify which mission to replay using a parameter when starting the script (for example, to replay your own local flights). +By default the code gets (and replays) the public `Droneshare mission with ID 101 `_. -You'll notice that the mission number for this flight is 101. +.. figure:: flight_replay_example.png -Now we'll launch **flight_replay.py** (/examples/flight_replay/flight_replay.py) and ask it to try and 'replay' mission 101. It will ask the web server for representative points from the flight, parse the JSON response and use that data to generate 100 waypoints we would like our vehicle to hit. For safety rather than using the altitude from the original flight we instead ask our vehicle to fly at a height of 30 meters. + Droneshare Mission #101 -One possible use of some variant of this tool to replay your old flights at your regular test field. -:: - STABILIZE> api start flight_replay.py 101 - STABILIZE> JSON downloaded... - Genrating 95 waypoints from replay... - APIThread-1 exiting... - Got MAVLink msg: MISSION_ACK {target_system : 255, target_component : 0, type : 0} - Sent waypoint 0 : MISSION_ITEM {target_system : 1, target_component : 1, seq : 0, frame : 3, command : 16, current : 0, autocontinue : 0, param1 : 0, param2 : 0, param3 : 0, param4 : 0, x : 45.7379052, y : 126.6273574, z : 30.0} - Sent waypoint 1 : MISSION_ITEM {target_system : 1, target_component : 1, seq : 1, frame : 3, command : 16, current : 0, autocontinue : 0, param1 : 0, param2 : 0, param3 : 0, param4 : 0, x : 45.7378905, y : 126.6273609, z : 30.0} - Sent waypoint 2 : MISSION_ITEM {target_system : 1, target_component : 1, seq : 2, frame : 3, command : 16, current : 0, autocontinue : 0, param1 : 0, param2 : 0, param3 : 0, - ... - Sent waypoint 92 : MISSION_ITEM {target_system : 1, target_component : 1, seq : 92, frame : 3, command : 16, current : 0, autocontinue : 0, param1 : 0, param2 : 0, param3 : 0, param4 : 0, x : 45.737971, y : 126.6274908, z : 30.0} - Sent waypoint 93 : MISSION_ITEM {target_system : 1, target_component : 1, seq : 93, frame : 3, command : 16, current : 0, autocontinue : 0, param1 : 0, param2 : 0, param3 : 0, param4 : 0, x : 45.738018, y : 126.6275664, z : 30.0} - Sent waypoint 94 : MISSION_ITEM {target_system : 1, target_component : 1, seq : 94, frame : 3, command : 16, current : 0, autocontinue : 0, param1 : 0, param2 : 0, param3 : 0, param4 : 0, x : 45.7380429, y : 126.6275067, z : 30.0} - Sent all 95 waypoints - Got MAVLink msg: MISSION_ACK {target_system : 255, target_component : 0, type : 0} - APM: flight plan received +Running the example +=================== + +The example can be run as described in :doc:`running_examples` (which in turn assumes that the vehicle +and DroneKit have been set up as described in :ref:`get-started`). + +If you're using a simulated vehicle, remember to :ref:`disable arming checks ` so +that the example can run. + +In summary, after cloning the repository: + +#. Navigate to the example folder as shown: + + .. code-block:: bash + + cd dronekit-python/examples/flight_replay/ + + +#. Start the example, passing the :ref:`connection string ` + you wish to use in the ``--connect`` parameter and specifying the mission to replay. + + .. code-block:: bash + + python flight_replay.py --connect 127.0.0.1:14550 --mission_id 101 + + .. note:: + + The command parameters above are the defaults, and may be omitted. These + connect to SITL on udp port 127.0.0.1:14550 and replay the mission with id 101. + + +.. tip:: + + It is more interesting to watch the example above on a map than the console. The topic :ref:`viewing_uav_on_map` + explains how to set up *Mission Planner* to view a vehicle running on the simulator (SITL). + +On the command prompt you should see (something like): + +.. code-block:: bash + + + \dronekit-python\examples\flight_replay>flight_replay.py + Connecting to vehicle on: 127.0.0.1:14550 + >>> APM:Copter V3.4-dev (e0810c2e) + >>> Frame: QUAD + JSON downloaded... + Generating 95 waypoints from replay... + ... How it works ============ +The example requests the web server for representative points from the flight, parses the JSON response +and uses that data to generate 100 waypoints. These are then sent to the vehicle. + + + Getting the points ------------------ The following simple function asks for the droneshare flight data: -:: +.. code:: python - def download_messages(mission_id, max_freq = 1.0): - """Download a public mission from droneshare (as JSON)""" - f = urllib.urlopen("%s/api/v1/mission/%s/messages.json?max_freq=%s&api_key=%s" % (api_server, mission_id, max_freq, api_key)) - j = json.load(f, object_hook=_decode_dict) - f.close() - return j + def download_messages(mission_id, max_freq = 1.0): + """Download a public mission from droneshare (as JSON)""" + f = urllib.urlopen("%s/api/v1/mission/%s/messages.json?max_freq=%s&api_key=%s" % (api_server, mission_id, max_freq, api_key)) + j = json.load(f, object_hook=_decode_dict) + f.close() + return j Some comments: @@ -61,11 +99,14 @@ Some comments: Setting the new waypoints -========================= +------------------------- + +If necessary, the example then reduces the number of messages retrieved into a set that can fit on the vehicle (in this case 100 waypoints). -We generate up to 100 waypoints for the vehicle with the following code: +The following code shows how the vehicle writes the received messages as commands (this part of the code is very similar to that +shown in :ref:`example_mission_basic`): -:: +.. code:: python print "Generating %s waypoints from replay..." % len(messages) cmds = v.commands @@ -89,8 +130,18 @@ We generate up to 100 waypoints for the vehicle with the following code: v.flush() -Next we'll work with existing Linux services (gpsd) to add a new drone based feature called :ref:`Follow Me `. +Known issues +============ + +At time of writing this example fails with an exception. See `#355 DKPY2 Can't clear waypoints `_ for more information. + +.. todo:: + + When example is fixed replace above text with + "This example works around the :ref:`known issues in the API `. + Provided that the vehicle is connected and able to arm, it should run through to completion." + Source code =========== @@ -100,4 +151,3 @@ The full source code at documentation build-time is listed below (`current versi .. literalinclude:: ../../examples/flight_replay/flight_replay.py :language: python - \ No newline at end of file diff --git a/examples/flight_replay/flight_replay.py b/examples/flight_replay/flight_replay.py index f496ddb71..c48c73607 100644 --- a/examples/flight_replay/flight_replay.py +++ b/examples/flight_replay/flight_replay.py @@ -1,28 +1,35 @@ """ flight_replay.py: -An example of talking to Droneshare to receive a past flight, and then 'replaying' -that flight by sending waypoints to a vehicle. - -Start this example as shown below, specifying the -missionid (a numeric mission number from a public droneshare flight): - - api start flight_replay.py +This example requests a past flight from Droneshare, and then 'replays' +the flight by sending waypoints to a vehicle. Full documentation is provided at http://python.dronekit.io/examples/flight_replay.html """ +from dronekit import connect from dronekit.lib import Command from pymavlink import mavutil import json, urllib, math -api_server = "https://api.3drobotics.com" -api_key = "a8948c11.9e44351f6c0aa7e3e2ff6d00b14a71c5" +#Set up option parsing to get connection string +import argparse +parser = argparse.ArgumentParser(description='Get a flight from droneshare and send as waypoints to a vehicle. Connects to SITL on local PC by default.') +parser.add_argument('--connect', default='127.0.0.1:14550', + help="vehicle connection target. Default '127.0.0.1:14550'") +parser.add_argument('--mission_id', default='101', + help="DroneShare Mission ID to replay. Default is '101'") +parser.add_argument('--api_server', default='https://api.3drobotics.com', + help="API Server. Default is Droneshare API (https://api.3drobotics.com)") +parser.add_argument('--api_key', default='89b511b1.d884d1cb57306e63925fcc07d032f2af', + help="API Server Key (default should be fine!)") +args = parser.parse_args() + + +# Connect to the Vehicle +print 'Connecting to vehicle on: %s' % args.connect +vehicle = connect(args.connect, await_params=True) -# First get an instance of the API endpoint -api = local_connect() -# Get our vehicle - when running with MAVProxy it only knows about one vehicle (for now) -v = api.get_vehicles()[0] def _decode_list(data): @@ -55,7 +62,8 @@ def _decode_dict(data): def download_messages(mission_id, max_freq = 1.0): """Download a public mission from droneshare (as JSON)""" - f = urllib.urlopen("%s/api/v1/mission/%s/messages.json?max_freq=%s&api_key=%s" % (api_server, mission_id, max_freq, api_key)) + message_url="%s/api/v1/mission/%s/messages.json?max_freq=%s&api_key=%s" % (args.api_server, mission_id, max_freq, args.api_key) + f = urllib.urlopen(message_url) j = json.load(f, object_hook=_decode_dict) f.close() return j @@ -63,7 +71,11 @@ def download_messages(mission_id, max_freq = 1.0): def replay_mission(payload): """Given mission JSON, set a series of wpts approximating the previous flight""" # Pull out just the global position msgs - messages = payload['messages'] + try: + messages = payload['messages'] + except: + print "Exception: payload from site is: %s" % payload + sys.exit() messages = filter(lambda obj: obj['typ'] == 'MAVLINK_MSG_ID_GLOBAL_POSITION_INT', messages) messages = map(lambda obj: obj['fld'], messages) @@ -76,9 +88,9 @@ def replay_mission(payload): messages = shorter print "Generating %s waypoints from replay..." % len(messages) - cmds = v.commands + cmds = vehicle.commands cmds.clear() - v.flush() + vehicle.flush() for i in xrange(0, len(messages)): pt = messages[i] lat = pt['lat'] @@ -94,17 +106,20 @@ def replay_mission(payload): 0, 0, 0, 0, 0, 0, lat, lon, altitude) cmds.add(cmd) - v.flush() - -if len(local_arguments) != 1: - print 'Error: usage "api start flight_replay.py "' -else: - # Now download the vehicle waypoints - cmds = v.commands - cmds.wait_valid() - - mission_id = int(local_arguments[0]) - max_freq = 0.1 - json = download_messages(mission_id, max_freq) - print "JSON downloaded..." - replay_mission(json) + vehicle.flush() + + +# Now download the vehicle waypoints +cmds = vehicle.commands +cmds.wait_valid() +mission_id = int(args.mission_id) +max_freq = 0.1 +json = download_messages(mission_id, max_freq) +print "JSON downloaded..." +replay_mission(json) + + +#Close vehicle object before exiting script +print "Close vehicle object" +vehicle.close() +print("Completed...")