From 44d880083e73756955f0bb6cd0441c603eca35cc Mon Sep 17 00:00:00 2001 From: Hamish Willee Date: Wed, 23 Dec 2015 16:12:10 +1100 Subject: [PATCH 1/4] Move topics into new folders (breaks index, will repair later) --- docs/{guide => about}/migrating.rst | 0 docs/{guide => develop}/companion-computers.rst | 0 docs/{guide => develop}/sitl_setup.rst | 0 3 files changed, 0 insertions(+), 0 deletions(-) rename docs/{guide => about}/migrating.rst (100%) rename docs/{guide => develop}/companion-computers.rst (100%) rename docs/{guide => develop}/sitl_setup.rst (100%) diff --git a/docs/guide/migrating.rst b/docs/about/migrating.rst similarity index 100% rename from docs/guide/migrating.rst rename to docs/about/migrating.rst diff --git a/docs/guide/companion-computers.rst b/docs/develop/companion-computers.rst similarity index 100% rename from docs/guide/companion-computers.rst rename to docs/develop/companion-computers.rst diff --git a/docs/guide/sitl_setup.rst b/docs/develop/sitl_setup.rst similarity index 100% rename from docs/guide/sitl_setup.rst rename to docs/develop/sitl_setup.rst From e1db3934c3c9af4a8016bb9a4e60cc752616fa4a Mon Sep 17 00:00:00 2001 From: Hamish Willee Date: Wed, 23 Dec 2015 18:33:06 +1100 Subject: [PATCH 2/4] Migrate to new structure and fix up links. Omits best-practise guide --- docs/about/index.rst | 1 + docs/about/migrating.rst | 2 +- docs/contributing/developer_setup_linux.rst | 3 +- docs/contributing/developer_setup_windows.rst | 2 +- docs/develop/coding_standards.rst | 14 ++ docs/develop/index.rst | 23 +++ docs/develop/installation.rst | 35 ++++ docs/examples/channel_overrides.rst | 2 +- docs/examples/create_attribute.rst | 2 +- docs/examples/drone_delivery.rst | 2 +- docs/examples/flight_replay.rst | 2 +- docs/examples/follow_me.rst | 2 +- docs/examples/guided-set-speed-yaw-demo.rst | 4 +- docs/examples/mission_basic.rst | 2 +- docs/examples/mission_import_export.rst | 2 +- docs/examples/simple_goto.rst | 2 +- docs/examples/vehicle_state.rst | 2 +- docs/guide/connecting_vehicle.rst | 63 ++++++ docs/guide/getting_started.rst | 183 ------------------ docs/guide/index.rst | 9 +- docs/guide/quick_start.rst | 119 ++++++++++++ docs/guide/taking_off.rst | 19 +- docs/index.rst | 10 +- examples/simple_goto/simple_goto.py | 3 +- 24 files changed, 289 insertions(+), 219 deletions(-) create mode 100644 docs/develop/coding_standards.rst create mode 100644 docs/develop/index.rst create mode 100644 docs/develop/installation.rst create mode 100644 docs/guide/connecting_vehicle.rst delete mode 100644 docs/guide/getting_started.rst create mode 100644 docs/guide/quick_start.rst diff --git a/docs/about/index.rst b/docs/about/index.rst index cee9c6eb6..1d5866a3e 100644 --- a/docs/about/index.rst +++ b/docs/about/index.rst @@ -12,6 +12,7 @@ After reading, you will understand what the kit offers and the opportunities it overview release_notes + migrating license diff --git a/docs/about/migrating.rst b/docs/about/migrating.rst index 5cbfb367e..9654f9ce1 100644 --- a/docs/about/migrating.rst +++ b/docs/about/migrating.rst @@ -19,7 +19,7 @@ The sections below outline the main migration areas. Installation ============ -DKPY 2.0 is now installed from `pip` on all platforms - see :ref:`get-started` for more information. +DKPY 2.0 is now installed from `pip` on all platforms - see :ref:`installing_dronekit` for more information. Installation is generally simpler than on DK 1.x because there are far fewer dependencies (both MAVProxy and numpy are no longer needed). diff --git a/docs/contributing/developer_setup_linux.rst b/docs/contributing/developer_setup_linux.rst index 00ffeffce..6bf8a965c 100644 --- a/docs/contributing/developer_setup_linux.rst +++ b/docs/contributing/developer_setup_linux.rst @@ -5,7 +5,8 @@ Building DroneKit-Python on Linux =================================== The setup for *developing* DroneKit-Python on Linux is almost the same as for *using* -DroneKit-Python. We therefore recommend that you start by following the instructions in :ref:`Getting Started `. +DroneKit-Python. We therefore recommend that you start by following the instructions in +:ref:`installing_dronekit`. When you've got DroneKit and a vehicle (simulated or real) communicating, you can then build and install your own fork of DroneKit, as discussed below. diff --git a/docs/contributing/developer_setup_windows.rst b/docs/contributing/developer_setup_windows.rst index 494775d0c..b9d65ef25 100644 --- a/docs/contributing/developer_setup_windows.rst +++ b/docs/contributing/developer_setup_windows.rst @@ -11,7 +11,7 @@ Install DroneKit using WinPython command line ============================================= -First set up a command line DroneKit-Python installation using *WinPython*. This process is the same as described in :ref:`Getting Started `. +First set up a command line DroneKit-Python installation. We recommend *WinPython* or *ActivePython*, as discussed in :ref:`installing_dronekit`. diff --git a/docs/develop/coding_standards.rst b/docs/develop/coding_standards.rst new file mode 100644 index 000000000..36828a5c5 --- /dev/null +++ b/docs/develop/coding_standards.rst @@ -0,0 +1,14 @@ +.. _coding_standards: + +========================== +Coding Standards +========================== + +DroneKit-Python does not impose (or recommend) a particular set of coding standards +for third party code. + +Internally we run the `YAPF formatter `_ +on major releases and we expect contributors to copy the patterns used in similar +code within the existing code base. + + \ No newline at end of file diff --git a/docs/develop/index.rst b/docs/develop/index.rst new file mode 100644 index 000000000..159cb707c --- /dev/null +++ b/docs/develop/index.rst @@ -0,0 +1,23 @@ +======================== +Developing with DroneKit +======================== + +DroneKit-Python is primarily intended for use on Linux-based :doc:`companion-computers` that travel +on a vehicle and communicate with the autopilot via a serial port. It can also be used +on ground-based computers running Linux, Windows or Mac OSX (communicating using WiFi or a telemetry radio). + +During development you'll generally run it on a development computer, communicating with a +:doc:`simulated vehicle` running on the same machine (via a UDP connection). + +This section contains topics explaining how to develop with DroneKit-Python, +covering subjects like installation, setting up the target vehicle or simulator, best practices +and coding standards. + + +.. toctree:: + :maxdepth: 1 + + installation + companion-computers + Simulated Vehicle + coding_standards \ No newline at end of file diff --git a/docs/develop/installation.rst b/docs/develop/installation.rst new file mode 100644 index 000000000..d0e0810cb --- /dev/null +++ b/docs/develop/installation.rst @@ -0,0 +1,35 @@ +.. _installing_dronekit: + +=================== +Installing DroneKit +=================== + +DroneKit-Python can be installed on a Linux, Mac OSX, or Windows computer that +has *Python 2.7* and can install Python packages from the Internet. + +It is installed from **pip** on all platforms: + +.. code-block:: bash + + pip install dronekit + + +**Installation notes:** + +* Mac and Linux require you prefix the command with ``sudo``. +* On Linux you may need to first install **pip** and **python-dev**: + + .. code-block:: bash + + sudo apt-get install python-pip python-dev + +* :doc:`companion-computers` are likely to run on stripped down versions of Linux. Ensure + you use a variant that supports Python 2.7 and can install Python packages from the Internet. +* Windows does not come with Python by default, but there are + `many distributions available `_. + We have tested against: + + * `WinPython 2.7 64bit `_ (see + `these instructions for installation and registration `_). This is the most tested version. + * `ActiveState ActivePython 2.7 `_. +* Python 3 is not supported. diff --git a/docs/examples/channel_overrides.rst b/docs/examples/channel_overrides.rst index fda53187f..2b043225a 100644 --- a/docs/examples/channel_overrides.rst +++ b/docs/examples/channel_overrides.rst @@ -24,7 +24,7 @@ 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`). +and DroneKit have been set up as described in :ref:`installing_dronekit`). In summary, after cloning the repository: diff --git a/docs/examples/create_attribute.rst b/docs/examples/create_attribute.rst index b44480180..0fac3e234 100644 --- a/docs/examples/create_attribute.rst +++ b/docs/examples/create_attribute.rst @@ -31,7 +31,7 @@ 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`). +and DroneKit have been set up as described in :ref:`installing_dronekit`). In summary, after cloning the repository: diff --git a/docs/examples/drone_delivery.rst b/docs/examples/drone_delivery.rst index 2500f8375..0c4b01eeb 100644 --- a/docs/examples/drone_delivery.rst +++ b/docs/examples/drone_delivery.rst @@ -21,7 +21,7 @@ Running the example =================== The example can be run much 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`). The main exception is that you need to +and DroneKit have been set up as described in :ref:`installing_dronekit`). The main exception is that you need to install the CherryPy dependencies and view the behaviour in a web browser. If you're using a simulated vehicle remember to :ref:`disable arming checks ` so diff --git a/docs/examples/flight_replay.rst b/docs/examples/flight_replay.rst index 5ce47375c..48fbd5e87 100644 --- a/docs/examples/flight_replay.rst +++ b/docs/examples/flight_replay.rst @@ -24,7 +24,7 @@ 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`). +and DroneKit have been set up as described in :ref:`installing_dronekit`). If you're using a simulated vehicle, remember to :ref:`disable arming checks ` so that the example can run. diff --git a/docs/examples/follow_me.rst b/docs/examples/follow_me.rst index 4f7e44d81..f51319a43 100644 --- a/docs/examples/follow_me.rst +++ b/docs/examples/follow_me.rst @@ -18,7 +18,7 @@ python language features and libraries (OpenCV, classes, lots of packages etc... Running the example =================== -DroneKit (for Linux) and the vehicle should be set up as described in :ref:`get-started`. +DroneKit (for Linux) and the vehicle should be set up as described in :ref:`installing_dronekit`. Once you've done that: diff --git a/docs/examples/guided-set-speed-yaw-demo.rst b/docs/examples/guided-set-speed-yaw-demo.rst index fd6db8a31..86fbc65f8 100644 --- a/docs/examples/guided-set-speed-yaw-demo.rst +++ b/docs/examples/guided-set-speed-yaw-demo.rst @@ -33,7 +33,7 @@ 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`). +and DroneKit have been set up as described in :ref:`installing_dronekit`). If you're using a simulated vehicle, remember to :ref:`disable arming checks ` so that the example can run. @@ -382,7 +382,7 @@ At time of writing, acceleration and yaw bits are ignored. Testbed settings ================ -This example has been tested on Windows against SITL running both natively and in a virtual machine (as described in :ref:`get-started`). +This example has been tested on Windows against SITL running both natively and in a virtual machine (as described in :ref:`installing_dronekit`). * DroneKit version: 2.0.0rc12 * ArduPilot version: 3.4.0. diff --git a/docs/examples/mission_basic.rst b/docs/examples/mission_basic.rst index 746c97f50..c7af9ba73 100644 --- a/docs/examples/mission_basic.rst +++ b/docs/examples/mission_basic.rst @@ -23,7 +23,7 @@ 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`). +and DroneKit have been set up as described in :ref:`installing_dronekit`). If you're using a simulated vehicle, remember to :ref:`disable arming checks ` so that the example can run. diff --git a/docs/examples/mission_import_export.rst b/docs/examples/mission_import_export.rst index cd00b68c7..20fe1919b 100644 --- a/docs/examples/mission_import_export.rst +++ b/docs/examples/mission_import_export.rst @@ -23,7 +23,7 @@ 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`). +and DroneKit have been set up as described in :ref:`installing_dronekit`). In summary, after cloning the repository: diff --git a/docs/examples/simple_goto.rst b/docs/examples/simple_goto.rst index 576f78c0b..89c2d221c 100644 --- a/docs/examples/simple_goto.rst +++ b/docs/examples/simple_goto.rst @@ -31,7 +31,7 @@ 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`). +and DroneKit have been set up as described in :ref:`installing_dronekit`). If you're using a simulated vehicle, remember to :ref:`disable arming checks ` so that the example can run. diff --git a/docs/examples/vehicle_state.rst b/docs/examples/vehicle_state.rst index b83ccc630..7448ac9ad 100644 --- a/docs/examples/vehicle_state.rst +++ b/docs/examples/vehicle_state.rst @@ -15,7 +15,7 @@ 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`). +the vehicle and DroneKit have been set up as described in :ref:`installing_dronekit`). If you're using a simulated vehicle remember to :ref:`disable arming checks ` so that the example can run. You can also: diff --git a/docs/guide/connecting_vehicle.rst b/docs/guide/connecting_vehicle.rst new file mode 100644 index 000000000..9f2b588e1 --- /dev/null +++ b/docs/guide/connecting_vehicle.rst @@ -0,0 +1,63 @@ +.. _connecting_vehicle: +.. _get_started_connecting: + +======================= +Connecting to a Vehicle +======================= + +The connection to the vehicle (or multiple vehicles) is set up within the +DroneKit script. Scripts import and call the :py:func:`connect() ` +method. After connecting this returns a :py:class:`Vehicle ` +object from which you can get/set parameters and attributes, and control vehicle movement: + +.. code:: python + + 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 above specifies the target address (in this case the loopback +address for UDP port 14550). See :ref:`get_started_connect_string` for the strings to use for +other common vehicles. + +:py:func:`connect() ` also has arguments for setting the baud rate, whether ``connect()`` returns immediately or waits until vehicle parameters and attributes are populated (:py:func:`wait_ready=True `), +the length of the connection timeout, and/or to use a :ref:`custom vehicle class `. + + +.. _get_started_connect_string: + +Connection string options +========================= + +The table below shows *connection strings* you can use for some of the more common connection types: + +.. list-table:: + :widths: 10 10 + :header-rows: 1 + + * - 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`` + * - 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). + diff --git a/docs/guide/getting_started.rst b/docs/guide/getting_started.rst deleted file mode 100644 index 8fc4bb45e..000000000 --- a/docs/guide/getting_started.rst +++ /dev/null @@ -1,183 +0,0 @@ -.. _get-started: - -=============== -Getting Started -=============== - -DroneKit-Python apps are typically run on Linux-based *companion computers* that travel -on the vehicle and communicate with the autopilot via a serial port. However, during development it is usually easier to -prototype apps on a standard Mac, Windows, or Linux computer using a *simulated* autopilot. - - -This topic explains how to set up and run DroneKit-Python on the different host operating systems -and then create and run a basic DroneKit app. - - - -Setting up the vehicle/autopilot -================================ - -For information on how to set up a vehicle (real and simulated) see: - -* :ref:`supported-companion-computers` for links to tested hardware/software configurations for a number of onboard Linux computers. -* :ref:`sitl_setup` for links explaining how to set up a simulated vehicle for Copter, Plane, or Rover. - - - -Installing DroneKit -=================== - -*DroneKit* can be installed on Linux, Windows and Mac OSX. - - -.. _getting_started_installing_dronekit_linux: - -Installing DroneKit on Linux ----------------------------- - -If you are using Ubuntu or Debian Linux you can get most of the *DroneKit* dependencies by running: - -.. code:: bash - - sudo apt-get install python-pip python-dev - - -The remaining dependencies are installed when you get DroneKit-Python from the public PyPi repository: - -.. code:: bash - - sudo pip install dronekit - - - -.. tip:: - - If you are planning to run *DroneKit* on a :ref:`companion computer `, make sure that the - computer runs a variant of Linux that support Python and can install Python packages from the internet. - - -Installing DroneKit on Mac OSX ------------------------------- - -Install DroneKit-Python and its dependencies from the public PyPi repository: - -.. code:: bash - - sudo pip install dronekit - - - -.. _get_started_install_dk_windows: - -Installing DroneKit on Windows ------------------------------- - -Set up a command line DroneKit-Python installation using *WinPython* (this Python distribution already includes most of the needed dependencies). - - -#. Download and run the correct `WinPython installer `_ (**v2.7**) for your platform (win32 vs win64). - - * Run the installer as an administrator (**Right-click** on file, select **Run as Administrator**). - * When prompted for the destination location, specify **C:\\Program Files (x86)** - (the default location is under the **Downloads** folder). - -#. Register the Python that came from *WinPython* as the preferred interpreter for your machine: - - Open the folder where you installed WinPython, run *WinPython Control Panel* and choose **Advanced/Register Distribution**. - - .. image:: http://dev.ardupilot.com/wp-content/uploads/sites/6/2014/03/Screenshot-from-2014-09-03-083816.png - -#. Install DroneKit-Python and its remaining dependencies from the public PyPi repository: - - Open the *WinPython Command Prompt* and run the following command: - - .. code:: bash - - pip install dronekit - - - -.. _get_started_connect_string: - -.. _get_started_connecting: - -Connecting to a Vehicle -======================= - -The connection to the vehicle is set up within the DroneKit script. Scripts import and call the :py:func:`connect()` method. -After connecting this returns a :py:class:`Vehicle ` object from which you can get/set parameters -and attributes, and control vehicle movement. - -.. code:: python - - from dronekit import connect - - # Connect to UDP endpoint. - vehicle = connect('127.0.0.1:14550', wait_ready=True) - -.. note:: - - Calling ``connect()`` with ``wait_ready=True`` (as shown above) ensures that the method will not return until - :py:attr:`Vehicle.parameters ` is fully populated with values from the vehicle. - Vehicle *attributes* are populated in parallel but are not guaranteed to have values when ``connect()`` completes - (an attribute will have value ``None`` if a corresponding MAVLink message has not been received - for example, - if the attribute is not supported by the vehicle). - -The example above connects to the udp address ``127.0.0.1:14550``. The table below shows addresses to use some of -the more common connection types: - - - -.. list-table:: Connection string options - :widths: 10 10 - :header-rows: 1 - - * - 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`` - * - 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``) - -.. note:: - The default baud rate may not be appropriate for all connection types (check this - if you can connect via a GCS but not DroneKit). - You can specify a different rate using the :py:func:`baud ` - parameter when connecting. For example: ``vehicle=connect('com14', baud=57600)``. - -.. tip:: - - The strings above are the same as you would use if connecting with MAVProxy. For other options see the - `MAVProxy documentation `_. - - -You can start this simple script in the same way you would start any other standalone Python script. - -.. code-block:: bash - - python your_dronekit_script.py - - - -.. todo:: Connect method here needs to link to the function, but it isn't exported yet. Fix that once the API tidied. - - - - -.. _getting-started-running_examples: - -Running an app/example -====================== - -This SDK has :ref:`numerous examples `. We recommend you start with :ref:`example-vehicle-state`, -which reads and writes :ref:`vehicle state and parameter ` information. - -For general information on running the examples (and other apps) see :ref:`running_examples_top`. diff --git a/docs/guide/index.rst b/docs/guide/index.rst index d63671773..c6d10c7b7 100644 --- a/docs/guide/index.rst +++ b/docs/guide/index.rst @@ -2,16 +2,13 @@ Guide ===== -The guide contains how-to documentation for using the DroneKit-Python API. These show how to :ref:`set up DroneKit` and use the API. Additional guide material can be found in the :ref:`example-toc`. - +The guide contains how-to documentation for using the DroneKit-Python API. +Additional guide material can be found in the :ref:`example-toc`. .. toctree:: :maxdepth: 1 - migrating - getting_started - companion-computers - Simulated Vehicle + connecting_vehicle vehicle_state_and_parameters taking_off copter/guided_mode diff --git a/docs/guide/quick_start.rst b/docs/guide/quick_start.rst new file mode 100644 index 000000000..2e44fbfd8 --- /dev/null +++ b/docs/guide/quick_start.rst @@ -0,0 +1,119 @@ +.. _quick_start_top: + +=========== +Quick Start +=========== + +This topic shows how to quickly install a DroneKit-Python +*development environment* and run a simple example to get +vehicle attributes from a *simulated* Copter. + + +Installation +============ + +DroneKit-Python and the *dronekit-sitl simulator* are installed +from **pip** on all platforms: + +.. code-block:: bash + + pip install dronekit + pip install dronekit-sitl + +Mac and Linux require you prefix the command with ``sudo``. +On Linux you will first need to install **pip** and **python-dev**: + +.. code-block:: bash + + sudo apt-get install python-pip python-dev + + +See :doc:`installation` and `dronekit-sitl `_ +for more detailed installation instructions. + + +Basic "Hello Drone" +=================== + +The script below first launches the simulator. It then +imports and calls the :py:func:`connect() ` method, +specifying the simulator's connection string (``tcp:127.0.0.1:5760``). +The method returns a :py:class:`Vehicle ` object that +we then use to query the attributes. + +.. code:: python + + print "Start simulator (SITL)" + from dronekit_sitl import SITL + sitl = SITL() + sitl.download('copter', '3.3', verbose=True) + sitl_args = ['-I0', '--model', 'quad', '--home=-35.363261,149.165230,584,353'] + sitl.launch(sitl_args, await_ready=True, restart=True) + + # Import DroneKit-Python + from dronekit import connect, VehicleMode + import time + + # Connect to the Vehicle. + print "Connecting to vehicle on: 'tcp:127.0.0.1:5760'" + vehicle = connect('tcp:127.0.0.1:5760', wait_ready=True) + + # Get some vehicle attributes (state) + print "Get some vehicle attribute values:" + print " GPS: %s" % vehicle.gps_0 + print " Battery: %s" % vehicle.battery + print " Last Heartbeat: %s" % vehicle.last_heartbeat + print " Is Armable?: %s" % vehicle.is_armable + print " System status: %s" % vehicle.system_status.state + print " Mode: %s" % vehicle.mode.name # settable + + # Close vehicle object before exiting script + vehicle.close() + + # Shut down simulator + sitl.stop() + print("Completed") + + +Copy the text above into a new text file (**hello.py**) and run it in the same way +as you would any other standalone Python script. + +.. code-block:: bash + + python hello.py + +You should see the following output from the simulated vehicle: + +.. code-block:: bash + + Start simulator (SITL) + Downloading SITL from http://dronekit-assets.s3.amazonaws.com/sitl/copter/sitl-win-copter-3.3.tar.gz + Extracted. + Connecting to vehicle on: 'tcp:127.0.0.1:5760' + >>> APM:Copter V3.3 (d6053245) + >>> Frame: QUAD + >>> Calibrating barometer + >>> Initialising APM... + >>> barometer calibration complete + >>> GROUND START + Get some vehicle attribute values: + GPS: GPSInfo:fix=3,num_sat=10 + Battery: Battery:voltage=12.587,current=0.0,level=100 + Last Heartbeat: 0.713999986649 + Is Armable?: False + System status: STANDBY + Mode: STABILIZE + Completed + +That's it- you've run your first DroneKit-Python script. + +Next Steps +========== + +* Learn a more about :doc:`../develop/index`. + This covers development best practices and coding standards, + and has more information about installation, working with a simulator + and setting up a companion computer. +* Read through our step by step :doc:`index` to learn how to connect to your + vehicle, takeoff, fly, and much more. +* Check out our :doc:`../examples/index`. diff --git a/docs/guide/taking_off.rst b/docs/guide/taking_off.rst index 8763706b7..0d9fc873f 100644 --- a/docs/guide/taking_off.rst +++ b/docs/guide/taking_off.rst @@ -4,9 +4,10 @@ Taking Off ========== -This article explains how to get your *Copter* to take off. At high level, the steps are: check that the vehicle -is able to arm (can pass pre-arm checks), set the mode to ``GUIDED``, arm the vehicle, -and then call :py:func:`Vehicle.simple_takeoff() `. +This article explains how to get your *Copter* to take off. + +At high level, the steps are: check that the vehicle is *able* to arm, set the mode to ``GUIDED``, +command the vehicle to arm, takeoff and block until we reach the desired altitude. .. todo:: @@ -26,8 +27,8 @@ and then call :py:func:`Vehicle.simple_takeoff() `_ message. - By contrast, Plane apps take off using the ``MAV_CMD_NAV_TAKEOFF`` command in a mission. Plane should first arm and then change to - ``AUTO`` mode to start the mission. + By contrast, Plane apps take off using the ``MAV_CMD_NAV_TAKEOFF`` command in a mission. + Plane should first arm and then change to ``AUTO`` mode to start the mission. The code below shows a function to arm a Copter, take off, and fly to a specified altitude. This is taken from :ref:`example_simple_goto`. @@ -42,7 +43,7 @@ The code below shows a function to arm a Copter, take off, and fly to a specifie """ print "Basic pre-arm checks" - # Don't let the user try to arm until autopilot is ready + # Don't try to arm until autopilot is ready while not vehicle.is_armable: print " Waiting for vehicle to initialise..." time.sleep(1) @@ -52,6 +53,7 @@ The code below shows a function to arm a Copter, take off, and fly to a specifie vehicle.mode = VehicleMode("GUIDED") vehicle.armed = True + # Confirm vehicle armed before attempting to take off while not vehicle.armed: print " Waiting for arming..." time.sleep(1) @@ -78,9 +80,8 @@ The function first performs some pre-arm checks. Arming turns on the vehicle's motors in preparation for flight. The flight controller will not arm until the vehicle has passed a series of pre-arm checks to ensure that it is safe to fly. -DroneKit-Python can't check every possible symptom that might prevent arming, but we can confirm that the -vehicle has booted, EKF is ready, and it has a GPS lock. These checks are encapsulated in the -:py:func:`Vehicle.is_armable ` attribute: +These checks are encapsulated by the :py:func:`Vehicle.is_armable ` +attribute, which is ``true`` when the vehicle has booted, EKF is ready, and the vehicle has GPS lock. .. code-block:: python diff --git a/docs/index.rst b/docs/index.rst index b86c08ed1..fe60ddf22 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -1,15 +1,11 @@ -.. DroneApi documentation master file, created by - sphinx-quickstart on Wed Mar 19 15:28:50 2014. - You can adapt this file completely to your liking, but it should at least - contain the root `toctree` directive. - =========================================== Welcome to DroneKit-Python's documentation! =========================================== DroneKit-Python 2.x helps you create powerful apps for UAVs. These apps run on a UAV's :ref:`Companion Computer `, and augment the autopilot by performing tasks that are both computationally intensive and require a low-latency link (e.g. computer vision). -This documentation provides everything you need to :doc:`get started ` with DroneKit-Python, including an :doc:`overview ` of the API, guide material, a number of demos and examples, and :doc:`API Reference `. +This documentation provides everything you need to get started with DroneKit-Python, including an :doc:`overview ` of the API, quick start, guide material, a number of demos and examples, +and :doc:`API Reference `. .. tip:: @@ -25,6 +21,8 @@ Contents: :maxdepth: 2 Introduction + guide/quick_start + Developing guide/index examples/index contributing/index diff --git a/examples/simple_goto/simple_goto.py b/examples/simple_goto/simple_goto.py index 4f67f5373..231616606 100644 --- a/examples/simple_goto/simple_goto.py +++ b/examples/simple_goto/simple_goto.py @@ -29,7 +29,7 @@ def arm_and_takeoff(aTargetAltitude): """ print "Basic pre-arm checks" - # Don't let the user try to arm until autopilot is ready + # Don't try to arm until autopilot is ready while not vehicle.is_armable: print " Waiting for vehicle to initialise..." time.sleep(1) @@ -40,6 +40,7 @@ def arm_and_takeoff(aTargetAltitude): vehicle.mode = VehicleMode("GUIDED") vehicle.armed = True + # Confirm vehicle armed before attempting to take off while not vehicle.armed: print " Waiting for arming..." time.sleep(1) From c7b53083eb19b3e7740b0ae0b8adcb7f61d548a2 Mon Sep 17 00:00:00 2001 From: Hamish Willee Date: Tue, 29 Dec 2015 18:14:44 +1100 Subject: [PATCH 3/4] Add Best Practice Section --- .../MissionPlanner_ConnectPort.png | Bin docs/develop/best_practice.rst | 246 ++++++++++++++++++ docs/develop/index.rst | 1 + docs/develop/installation.rst | 7 +- 4 files changed, 253 insertions(+), 1 deletion(-) rename docs/{guide => develop}/MissionPlanner_ConnectPort.png (100%) create mode 100644 docs/develop/best_practice.rst diff --git a/docs/guide/MissionPlanner_ConnectPort.png b/docs/develop/MissionPlanner_ConnectPort.png similarity index 100% rename from docs/guide/MissionPlanner_ConnectPort.png rename to docs/develop/MissionPlanner_ConnectPort.png diff --git a/docs/develop/best_practice.rst b/docs/develop/best_practice.rst new file mode 100644 index 000000000..f2bcb750b --- /dev/null +++ b/docs/develop/best_practice.rst @@ -0,0 +1,246 @@ +.. _best_practices: + +============== +Best Practices +============== + +This guide provides a broad overview of how to use the API, its main programming idioms +and best practices. More detail information is linked from each section. + + +General considerations +====================== + +DroneKit-Python communicates with vehicle autopilots using the MAVLink protocol, +which defines how commands, telemetry and vehicle settings/parameters +are sent between vehicles, companion computers, ground stations and other systems on +a MAVLink network. + +Some general considerations from using this protocol are: + +* Messages and message acknowledgments are not guaranteed to arrive (the protocol is not "lossless"). +* Commands may be silently ignored by the Autopilot if it is not in a state where it can + safely act on them. +* Command acknowledgment and completion messages are not sent in most cases + (and if sent, may not arrive). +* Commands may be interrupted before completion. +* Autopilots may choose to interpret the protocol in slightly different ways. +* Commands can arrive at the autopilot from multiple sources. + +Developers should code defensively. Where possible: + +* Check that a vehicle is in a state to obey a command (for example, + poll on :py:func:`Vehicle.is_armable ` + before trying to arm the vehicle). +* Don't assume that a command has succeeded until the changed behaviour is observed. + In particular we recommend a launch sequence where you check that the mode and arming + have succeeded before attempting to take off. +* Monitor for state changes and react accordingly. + For example, if the user changes the mode from ``GUIDED`` your script should + stop sending commands. +* Verify that your script can run inside the normal latency limits for message passing + from the vehicle and tune any monitoring appropriately. + + +Connecting +========== + +In most cases you'll use the normal way to :ref:`connect to a vehicle `, +setting ``wait_ready=True`` to ensure that the vehicle is already populated with attributes +when the :py:func:`connect() ` returns: + +.. code:: python + + from dronekit import connect + + # Connect to the Vehicle (in this case a UDP endpoint) + vehicle = connect('REPLACE_connection_string_for_your_vehicle', wait_ready=True) + +The ``connect()`` call will sometimes fail with an exception. +Additional information about an exception can be obtained by +running the connect within a ``try-catch`` block as shown: + +.. code-block:: python + + import dronekit + import socket + import exceptions + + + try: + dronekit.connect('REPLACE_connection_string_for_your_vehicle', heartbeat_timeout=15) + + # Bad TCP connection + except socket.error: + print 'No server exists!' + except dronekit.APIException: + print 'Timeout!' + + # Bad TTY connection + except exceptions.OSError as e: + print 'No serial exists!' + except dronekit.APIException: + print 'Timeout!' + + # Other error + except: + print 'Some other error!' + +.. tip:: + + The default ``heartbeat_timeout`` on connection is 30 sections. Usually a connection will + succeed quite quickly, so you may wish to reduce this in the ``connect()`` method as shown in the + code snippet above. + +If a connection succeeds from a ground station, but not from DroneKit-Python it may be that your baud +rate is incorrect for your hardware. This rate can also be set in the ``connect()`` method. + + +Launch sequence +=============== + +Generally you should use the standard launch sequence described in :doc:`../guide/taking_off`: + +* Poll on :py:func:`Vehicle.is_armable ` + until the vehicle is ready to arm. +* Set the :py:attr:`Vehicle.mode ` to ``GUIDED`` +* Set :py:attr:`Vehicle.armed ` to ``True`` and + poll on the same attribute until the vehicle is armed. +* Call :py:func:`Vehicle.simple_takeoff ` + with a target altitude. +* Poll on the altitude and allow the code to continue only when it is reached. + +The approach ensures that commands are only sent to the vehicle when it is able +to act on them (e.g. we know :py:func:`Vehicle.is_armable ` +is ``True`` before trying to arm, we know +:py:attr:`Vehicle.armed ` is ``True`` before we take off). +It also makes debugging takeoff problems a lot easier. + + +Movement commands +================= + +DroneKit-Python provides :py:func:`Vehicle.simple_goto ` for moving to a specific position (at a defined speed). It is also possible to control movement by sending commands to specify the vehicle's :ref:`velocity components `. + +.. note:: + + As with :py:func:`Vehicle.simple_takeoff `, movement + commands are asynchronous, and will be interrupted if another command arrives + before the vehicle reaches its target. Calling code should block and wait (or + check that the operation is complete) before preceding to the next command. + +For more information see: :ref:`guided_mode_copter`. + + +Vehicle information +=================== + +Vehicle state information is exposed through vehicle *attributes* which can be read and observed (and in some cases written) +and vehicle settings which can be read, written, iterated and observed using *parameters* (a special attribute). All the attributes are documented in :doc:`../guide/vehicle_state_and_parameters`. + +Attributes are populated by MAVLink messages from the vehicle. +Information read from an attribute may not precisely reflect the actual value on the vehicle. Commands sent +to the vehicle may not arrive, or may be ignored by the autopilot. + +If low-latency is critical, we recommend you verify that the update rate is achievable and +perhaps modify script behaviour if :py:attr:`Vehicle.last_heartbeat ` falls outside +a useful range. + +When setting attributes, poll their values to confirm that they have changed. This applies, in particular, +to :py:attr:`Vehicle.armed ` and :py:attr:`Vehicle.mode `. + + + +Missions and waypoints +====================== + +DroneKit-Python can also :ref:`create and modify autonomous missions `. + +While it is possible to construct DroneKit-Python apps by dynamically constructing missions "on the fly", we recommend you use guided mode for Copter apps. This generally results in a better experience. + +.. tip:: + + If a mission command is not available in guided mode, + it can be useful to switch to a mission and call it, then change + back to normal guided mode operation. + + +Monitor and react to state changes +================================== + +Almost all attributes can be observed - see :ref:`vehicle_state_observe_attributes` for more information. + +Exactly what state information you observe, and how you react to it, depends on your particular script: + +* Most standalone apps should monitor the :py:func:`Vehicle.mode ` and + stop sending commands if the mode changes unexpectedly (this usually indicates + that the user has taken control of the vehicle). +* Apps might monitor :py:func:`Vehicle.last_heartbeat ` + and could attempt to reconnect if the value gets too high. +* Apps could monitor :py:func:`Vehicle.system_status ` + for ``CRITICAL`` or ``EMERGENCY`` in order to implement specific emergency handling. + + +Sleep the script when not needed +================================ + +Sleeping your script appropriately can reduce the memory overhead. + +For example, at low speeds you might only need to check whether you've reached a target every few seconds. +Using ``time.sleep(2)`` between checks will be more efficient than checking more often. + + +Exiting a script +================ + +Scripts should call :py:func:`Vehicle.close() ` +before exiting to ensure that all messages have flushed before the script completes: + +.. code:: python + + # About to exit script + vehicle.close() + + +Subclass Vehicle +===================================== + +If you need to use functionality that is specific to particular hardware, we +recommend you subclass :py:class:`Vehicle ` and pass this new class into +:py:func:`connect() `. + +:doc:`../examples/create_attribute` shows how you can do this. + + + + +Debugging +========= + +DroneKit-Python apps are ordinary standalone Python scripts, and can be :doc:`debugged using standard Python methods <../guide/debugging>` (including the debugger/IDE of your choice). + + +Launching scripts +================= + +Scripts are run from an ordinary Python command prompt. For example: + +.. code:: bash + + python some_python_script.py [arguments] + +Command line arguments are passed into the script as ``sys.argv`` variables (the normal) +and you can use these directly or via an argument parser (e.g. +`argparse `_). + + +Current script directory +======================== + +You can use normal Python methods for getting file system information: + +.. code-block:: python + + import os.path + full_directory_path_of_current_script = os.path.dirname(os.path.abspath(__file__)) + diff --git a/docs/develop/index.rst b/docs/develop/index.rst index 159cb707c..250992942 100644 --- a/docs/develop/index.rst +++ b/docs/develop/index.rst @@ -20,4 +20,5 @@ and coding standards. installation companion-computers Simulated Vehicle + best_practice coding_standards \ No newline at end of file diff --git a/docs/develop/installation.rst b/docs/develop/installation.rst index d0e0810cb..6fb6caba9 100644 --- a/docs/develop/installation.rst +++ b/docs/develop/installation.rst @@ -16,7 +16,12 @@ It is installed from **pip** on all platforms: **Installation notes:** -* Mac and Linux require you prefix the command with ``sudo``. +* Mac and Linux require you prefix the command with ``sudo``. + + .. code-block:: bash + + sudo pip install dronekit + * On Linux you may need to first install **pip** and **python-dev**: .. code-block:: bash From a2fa00961a2c610f4df9fce0275b416f0c1045d5 Mon Sep 17 00:00:00 2001 From: Hamish Willee Date: Tue, 29 Dec 2015 18:26:57 +1100 Subject: [PATCH 4/4] Improve the Connecting topic --- docs/guide/connecting_vehicle.rst | 34 +++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/docs/guide/connecting_vehicle.rst b/docs/guide/connecting_vehicle.rst index 9f2b588e1..7d9363fb1 100644 --- a/docs/guide/connecting_vehicle.rst +++ b/docs/guide/connecting_vehicle.rst @@ -8,23 +8,33 @@ Connecting to a Vehicle The connection to the vehicle (or multiple vehicles) is set up within the DroneKit script. Scripts import and call the :py:func:`connect() ` method. After connecting this returns a :py:class:`Vehicle ` -object from which you can get/set parameters and attributes, and control vehicle movement: +object from which you can get/set parameters and attributes, and control vehicle movement. -.. code:: python +The most common way to call :py:func:`connect() ` is shown below: + +.. code-block:: python 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 above specifies the target address (in this case the loopback +The first parameter specifies the target address (in this case the loopback address for UDP port 14550). See :ref:`get_started_connect_string` for the strings to use for -other common vehicles. +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. + +:py:func:`connect() ` also has arguments for setting the baud rate, +the length of the connection timeout, and/or to use +a :ref:`custom vehicle class `. -:py:func:`connect() ` also has arguments for setting the baud rate, whether ``connect()`` returns immediately or waits until vehicle parameters and attributes are populated (:py:func:`wait_ready=True `), -the length of the connection timeout, and/or to use a :ref:`custom vehicle class `. +There is more documentation on all of the parameters in the :py:func:`API Reference `. +.. _connection_string_options: .. _get_started_connect_string: Connection string options @@ -61,3 +71,15 @@ The table below shows *connection strings* you can use for some of the more comm 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 +:py:func:`connect() ` for each vehicle +with the appropriate :ref:`connection strings `. + +The returned :py:class:`Vehicle ` objects are independent of +each other and can be separately used to control their respective +vehicle. +