|
1 | | -======================== |
2 | | -Example: Simple Go To |
3 | | -======================== |
| 1 | +.. _example_simple_goto: |
4 | 2 |
|
5 | | -This little demonstration just tells the vehicle to fly to a couple of different locations in the world. You can edit the code to pick a latitude and longitude close to your position. |
| 3 | +============================== |
| 4 | +Example: Simple Go To (Copter) |
| 5 | +============================== |
6 | 6 |
|
7 | | -Running the example |
8 | | -=================== |
| 7 | +This example demonstrates how to arm and launch a Copter in GUIDED mode, travel to a number of waypoints, and then return |
| 8 | +to the home location. It uses :py:func:`Vehicle.commands.takeoff() <droneapi.lib.CommandSequence.takeoff>`, |
| 9 | +:py:func:`Vehicle.commands.goto() <droneapi.lib.CommandSequence.goto>` and :py:attr:`Vehicle.mode <droneapi.lib.Vehicle.mode>`. |
| 10 | + |
| 11 | +The locations used are centred around the home location when the :ref:`Simulated Vehicle <vagrant-sitl-from-full-image>` is booted; you can edit the latitude and longitude |
| 12 | +to use more appropriate positions for your own vehicle. |
| 13 | + |
| 14 | +.. note:: |
9 | 15 |
|
10 | | -Once Mavproxy is running and the API is loaded, you can run this small example by typing: ``api start simple_goto.py`` |
| 16 | + This example will only run on *Copter*: |
11 | 17 |
|
12 | | -It will tell your vehicle to start flying to a particular latitude and longitude stored in the file (though for safety the take-off command is not included - you must manually tell vehicle to fly). On the mavproxy console you should see: |
| 18 | + * *Plane* does not support ``takeoff`` in GUIDED mode. |
| 19 | + * *Rover* will ignore the ``takeoff`` command and will then stick at the altitude check. |
| 20 | + |
13 | 21 |
|
14 | | -:: |
15 | 22 |
|
16 | | - STABILIZE> api start simple_goto.py |
17 | | - STABILIZE> Got MAVLink msg: MISSION_ACK {target_system : 255, target_component : 0, type : 0} |
18 | | - GUIDED> Mode GUIDED |
19 | | - APIThread-0 exiting... |
20 | | - Got MAVLink msg: MISSION_ACK {target_system : 255, target_component : 0, type : 0} |
| 23 | +Running the example |
| 24 | +=================== |
| 25 | + |
| 26 | +The vehicle and DroneKit should be set up as described in the :ref:`quick-start` or :ref:`get-started`. |
| 27 | + |
| 28 | +If you're using a simulated vehicle, remember to :ref:`disable arming checks <disable-arming-checks>` so |
| 29 | +that the example can run. |
| 30 | + |
| 31 | +Once MAVProxy is running and the API is loaded, you can start the example by typing: ``api start simple_goto.py``. |
| 32 | + |
| 33 | +.. note:: |
| 34 | + |
| 35 | + The command above assumes you started the *MAVProxy* prompt in a directory containing the example script. If not, |
| 36 | + you will have to specify the full path to the script (something like): |
| 37 | + ``api start /home/user/git/dronekit-python/examples/simple_goto/simple_goto.py``. |
| 38 | + |
| 39 | +.. tip:: |
| 40 | + |
| 41 | + It is more interesting to watch the example above on a map than the console. The topic :ref:`viewing_uav_on_map` |
| 42 | + explains how to set up *Mission Planner* to view a vehicle running on the simulator (SITL). |
| 43 | + |
| 44 | +On the *MAVProxy* console you should see (something like): |
| 45 | + |
| 46 | +.. code-block:: python |
| 47 | +
|
| 48 | + MAV> api start simple_goto.py |
| 49 | + STABILIZE> Basic pre-arm checks |
| 50 | + Arming motors |
| 51 | + Waiting for arming... |
| 52 | + Waiting for arming... |
| 53 | + APM: ARMING MOTORS |
| 54 | + APM: GROUND START |
| 55 | + Waiting for arming... |
| 56 | + Waiting for arming... |
| 57 | + GUIDED> Mode GUIDED |
| 58 | + APM: Initialising APM... |
| 59 | + Got MAVLink msg: COMMAND_ACK {command : 400, result : 0} |
| 60 | + Waiting for arming... |
| 61 | + ARMED |
| 62 | + Taking off! |
| 63 | + Altitude: 0.0 |
| 64 | + Got MAVLink msg: COMMAND_ACK {command : 22, result : 0} |
| 65 | + Altitude: 0.10000000149 |
| 66 | + Altitude: 0.620000004768 |
| 67 | + ... |
| 68 | + Altitude: 19.25 |
| 69 | + Reached target altitude |
| 70 | + Going to first point... |
| 71 | + Got MAVLink msg: MISSION_ACK {target_system : 255, target_component : 0, type : 0} |
| 72 | + Going to second point... |
| 73 | + Got MAVLink msg: MISSION_ACK {target_system : 255, target_component : 0, type : 0} |
| 74 | + Returning to Launch |
| 75 | + APIThread-0 exiting... |
| 76 | + |
| 77 | +.. tip:: |
| 78 | +
|
| 79 | + If you get stuck in ``Waiting for arming...`` it is very likely that the vehicle did not pass all pre-arm checks. |
| 80 | + On a real device you can view the controller LEDs to determine likely issues. On the Simulator console you |
| 81 | + can disable the checks if needed: |
| 82 | +
|
| 83 | + .. code-block:: bash |
| 84 | +
|
| 85 | + STABILIZE>param load ../Tools/autotest/copter_params.parm |
| 86 | + STABILIZE>param set ARMING_CHECK 0 |
21 | 87 |
|
22 | 88 |
|
23 | 89 | How does it work? |
24 | 90 | ================= |
25 | 91 |
|
26 | | -The key code in this demo is the following: |
| 92 | +The code has three distinct sections: arming and takeoff, flight to a specified location, and return-to-home. |
| 93 | +
|
| 94 | +Takeoff |
| 95 | +------- |
| 96 | +
|
| 97 | +To launch *Copter* you need to set the mode to ``GUIDED``, arm the vehicle, and then call |
| 98 | +:py:func:`Vehicle.commands.takeoff() <droneapi.lib.CommandSequence.takeoff>`. The takeoff code in this example |
| 99 | +is explained in the guide topic :ref:`taking-off`. |
| 100 | +
|
| 101 | + |
| 102 | +Flying to a point - Goto |
| 103 | +------------------------ |
| 104 | +
|
| 105 | +The vehicle is already in ``GUIDED`` mode, so to send it to a certain point we just need to |
| 106 | +call :py:func:`Vehicle.commands.goto() <droneapi.lib.CommandSequence.goto>` with the target location, |
| 107 | +and then :py:func:`flush() <droneapi.lib.Vehicle.flush>` the command: |
| 108 | +
|
| 109 | +.. code-block:: python |
| 110 | +
|
| 111 | + point1 = Location(-35.361354, 149.165218, 20, is_relative=True) |
| 112 | + vehicle.commands.goto(point1) |
| 113 | + vehicle.flush() |
| 114 | +
|
| 115 | + # sleep so we can see the change in map |
| 116 | + time.sleep(30) |
27 | 117 |
|
28 | | -:: |
| 118 | +Without some sort of "wait" the next command would be executed immediately. In this example we just |
| 119 | +sleep for 30 seconds - a good opportunity to observe the vehicle's movement on a map. |
29 | 120 |
|
30 | | - vehicle.mode = VehicleMode("GUIDED") |
31 | | - origin = Location(-34.364114, 149.166022, 30, is_relative=True) |
32 | 121 |
|
33 | | - commands.goto(origin) |
34 | | - vehicle.flush() |
| 122 | +RTL - Return to launch |
| 123 | +------------------------ |
35 | 124 |
|
36 | | -It tells the vehicle to fly to a specified lat/long and hover at that location (30 meters in the air). ``is_relative=True`` is the default and is recommended - it means that the altitude (30 meters) is *relative* to the vehicle home location. If you had set ``is_relative`` to ``false``, it would have told the vehicle to fly to a specified mean-sea-level which is probably not what you want unless you are next to an ocean. |
| 125 | +To return to the home position and land, we set the mode to ``RTL``: |
37 | 126 |
|
| 127 | +.. code-block:: python |
38 | 128 |
|
39 | | -Building on the basic vehicle control you just learned, we now show how to write a small web application that allows you to command a drone to fly to a particular location. |
| 129 | + vehicle.mode = VehicleMode("RTL") |
| 130 | + vehicle.flush() |
40 | 131 |
|
41 | 132 |
|
42 | 133 | Source code |
43 | 134 | =========== |
44 | 135 |
|
45 | | -The full source code at documentation build-time is listed below (`current version on github <https://github.com/diydrones/dronekit-python/blob/master/example/simple_goto/simple_goto.py>`_): |
| 136 | +The full source code at documentation build-time is listed below (`current version on github <https://github.com/diydrones/dronekit-python/blob/master/examples/simple_goto/simple_goto.py>`_): |
46 | 137 |
|
47 | | -.. include:: ../../example/simple_goto/simple_goto.py |
48 | | - :literal: |
| 138 | +.. literalinclude:: ../../examples/simple_goto/simple_goto.py |
| 139 | + :language: python |
0 commit comments