Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions docs/examples/guided-set-speed-yaw-demo.rst
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,6 @@ The functions sending immediate commands are:
* :ref:`condition_yaw() <guided_mode_copter_set_yaw>`
* :ref:`set_roi(location) <guided_mode_copter_set_roi>`
* :ref:`set_speed(speed) <guided_mode_copter_set_speed>`
* :ref:`set_home() <guided_mode_copter_set_home>`

The example uses a number functions to convert global locations co-ordinates (decimal degrees) into local coordinates relative to the vehicle (in metres). These are :ref:`described in the guide <guided_mode_copter_useful_conversion_functions>`.

Expand Down Expand Up @@ -349,14 +348,14 @@ This demo has been tested on Windows against SITL running both natively and in a

DroneKit environment (from PIP):

* droneapi: 1.2.0
* droneapi: 2.0.0rc2
* pymavlink: 1.1.57
* MAVProxy: 1.4.23
* protobuf: 2.6.1

ArduPilot version:

* `3.3.0beta2 <https://github.com/diydrones/ardupilot/commit/c73945686c821cab1034c0d434fc7d3f66a0fd50>`_.
* 3.3.0.



Expand Down
15 changes: 11 additions & 4 deletions docs/examples/vehicle_state.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ 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 <disable-arming-checks>` so
that the example can run. You can also
`add a virtual rangefinder <http://dev.ardupilot.com/wiki/using-sitl-for-ardupilot-testing/#adding_a_virtual_rangefinder>`_
(otherwise the :py:attr:`Vehicle.rangefinder <dronekit.lib.Vehicle.rangefinder>` attribute may return values of ``None`` for the distance
and voltage).
(otherwise the :py:attr:`Vehicle.rangefinder <dronekit.lib.Vehicle.rangefinder>` attribute may return
values of ``None`` for the distance and voltage).

In summary, after cloning the repository:

Expand Down Expand Up @@ -70,10 +70,17 @@ On the command prompt you should see (something like):
Rangefinder voltage: None
Mode: STABILIZE
Armed: False
Home Location: LocationGlobal:lat=0.0,lon=0.0,alt=0.0,is_relative=False

Home Location (before downloading waypoints): None
Home Location (after downloading waypoints): LocationGlobal:lat=-35.3632621765,lon=149.165237427,alt=583.989990234,is_relative=False
Set new home location
Waiting for home location: LocationGlobal:lat=0.0,lon=0.0,alt=0.0,is_relative=False
...
Waiting for home location: LocationGlobal:lat=0.0,lon=0.0,alt=0.0,is_relative=False
Waiting for home location: LocationGlobal:lat=0.0,lon=0.0,alt=0.0,is_relative=False

Autopilot set home location: LocationGlobal:lat=-35.3632621765,lon=149.165237427,alt=583.989990234,is_relative=False
New Home Location (altitude should be 222): LocationGlobal:lat=-35.3632621765,lon=149.165237427,alt=222.0,is_relative=False

Set Vehicle.mode=GUIDED (currently: STABILIZE)
Waiting for mode change ...

Expand Down
40 changes: 2 additions & 38 deletions docs/guide/copter/guided_mode.rst
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ The command can be interrupted by a later movement command. When moving the vehi
You can also control the velocity using the `SET_POSITION_TARGET_GLOBAL_INT <https://pixhawk.ethz.ch/mavlink/#SET_POSITION_TARGET_GLOBAL_INT>`_ MAVLink command in almost exactly the same way (there is no real benefit in sending one command over the other). For more information on this option see :ref:`example_guided_mode_send_global_velocity` in the example code.



.. _guided_mode_copter_accel_force_control:

Acceleration and force control
Expand Down Expand Up @@ -214,7 +214,7 @@ Supported commands

DroneKit-Python provides a friendly Python API that abstracts many of the commands. Where possible you should use the API rather than send messages directly. For example it is better to use :py:func:`Vehicle.commands.takeoff() <dronekit.lib.CommandSequence.takeoff>` than to explicitly send the ``MAV_CMD_NAV_TAKEOFF`` command.

Some of the MAV_CMD commands that you might want to send include: :ref:`MAV_CMD_CONDITION_YAW <guided_mode_copter_set_yaw>`, :ref:`MAV_CMD_DO_CHANGE_SPEED <guided_mode_copter_set_speed>`, :ref:`MAV_CMD_DO_SET_HOME <guided_mode_copter_set_home>`, :ref:`MAV_CMD_DO_SET_ROI <guided_mode_copter_set_roi>`, ``MAV_CMD_DO_SET_SERVO``, ``MAV_CMD_DO_REPEAT_SERVO``, ``MAV_CMD_DO_SET_RELAY``, ``MAV_CMD_DO_REPEAT_RELAY``, ``MAV_CMD_DO_FENCE_ENABLE``, ``MAV_CMD_DO_PARACHUTE``, ``MAV_CMD_DO_GRIPPER``, ``MAV_CMD_MISSION_START``. These would be sent in a ``COMMAND_LONG`` message :ref:`as discussed above <guided_mode_how_to_send_commands_command_long>`.
Some of the MAV_CMD commands that you might want to send include: :ref:`MAV_CMD_CONDITION_YAW <guided_mode_copter_set_yaw>`, :ref:`MAV_CMD_DO_CHANGE_SPEED <guided_mode_copter_set_speed>`, :ref:`MAV_CMD_DO_SET_ROI <guided_mode_copter_set_roi>`, ``MAV_CMD_DO_SET_SERVO``, ``MAV_CMD_DO_REPEAT_SERVO``, ``MAV_CMD_DO_SET_RELAY``, ``MAV_CMD_DO_REPEAT_RELAY``, ``MAV_CMD_DO_FENCE_ENABLE``, ``MAV_CMD_DO_PARACHUTE``, ``MAV_CMD_DO_GRIPPER``, ``MAV_CMD_MISSION_START``. These would be sent in a ``COMMAND_LONG`` message :ref:`as discussed above <guided_mode_how_to_send_commands_command_long>`.



Expand Down Expand Up @@ -321,42 +321,6 @@ Send the `MAV_CMD_DO_SET_ROI <http://copter.ardupilot.com/common-mavlink-mission
The ROI (and yaw) is also reset when the mode, or the command used to control movement, is changed.


.. _guided_mode_copter_set_home:

Setting the home location
-------------------------

Send the `MAV_CMD_DO_SET_HOME <http://copter.ardupilot.com/common-mavlink-mission-command-messages-mav_cmd/#mav_cmd_do_set_home>`_
command to set the *home location* to either the current location or a specified location.

.. code-block:: python

def set_home(aLocation, aCurrent=1):
msg = vehicle.message_factory.command_long_encode(
0, 0, # target system, target component
mavutil.mavlink.MAV_CMD_DO_SET_HOME, #command
0, #confirmation
aCurrent, #param 1: 1 to use current position, 2 to use the entered values.
0, 0, 0, #params 2-4
aLocation.lat,
aLocation.lon,
aLocation.alt
)
# send command to vehicle
vehicle.send_mavlink(msg)


The *home location* is updated immediately in ArduPilot, but the change may not appear in the GCS/*Mission Planner*. You can force an update by reading the mission commands (this works, because the home location is currently implemented as the 0th waypoint command):

.. code-block:: python

# Set new Home location to current LocationGlobal
set_home(vehicle.location.global_frame)
# Reloads the home location in GCSs
cmds = vehicle.commands
cmds.download()
cmds.wait_ready()
print " Home WP: %s" % cmds[0]

.. _guided_mode_copter_responses:

Expand Down
48 changes: 36 additions & 12 deletions docs/guide/vehicle_state_and_parameters.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,18 @@ Vehicle state information is exposed through vehicle *attributes*. DroneKit-Pyth
:py:attr:`Vehicle.mount_status <dronekit.lib.Vehicle.mount_status>`,
:py:attr:`Vehicle.battery <dronekit.lib.Vehicle.battery>`,
:py:attr:`Vehicle.rangefinder <dronekit.lib.Vehicle.rangefinder>`,
:py:attr:`Vehicle.home_location <dronekit.lib.Vehicle.home_location>`
:py:attr:`Vehicle.home_location <dronekit.lib.Vehicle.home_location>`,
:py:attr:`Vehicle.armed <dronekit.lib.Vehicle.armed>`,
:py:attr:`Vehicle.mode <dronekit.lib.Vehicle.mode>`.

All of the attributes can be :ref:`read <vehicle_state_read_attributes>` and :ref:`observed <vehicle_state_observe_attributes>`,
but only the :py:attr:`Vehicle.mode <dronekit.lib.Vehicle.mode>` and :py:attr:`Vehicle.armed <dronekit.lib.Vehicle.armed>`
but only the :py:attr:`Vehicle.home_location <dronekit.lib.Vehicle.home_location>`,
:py:attr:`Vehicle.mode <dronekit.lib.Vehicle.mode>` and
:py:attr:`Vehicle.armed <dronekit.lib.Vehicle.armed>`
status can be :ref:`written <vehicle_state_set_attributes>`.


The behaviour of :py:attr:`Vehicle.home_location <dronekit.lib.Vehicle.home_location>` is different
from the other attributes, and is :ref:`discussed in its own section below <vehicle_state_home_location>`.

.. _vehicle_state_read_attributes:

Expand Down Expand Up @@ -73,16 +76,13 @@ If an attribute cannot be retrieved then the returned object will contain
with ``None`` values for ``eph``, ``satellites_visible`` etc.)
Attributes will also return ``None`` if the associated hardware is not present on the connected device.

The behaviour of :py:attr:`Vehicle.home_location <dronekit.lib.Vehicle.home_location>` is different,
:ref:`as discussed below <vehicle_state_home_location>`.

.. tip::

If you're using a :ref:`simulated vehicle <sitl_setup>` you can add support for optional hardware including
`rangefinders <http://dev.ardupilot.com/using-sitl-for-ardupilot-testing/#adding_a_virtual_rangefinder>`_
and `optical flow sensors <http://dev.ardupilot.com/using-sitl-for-ardupilot-testing/#adding_a_virtual_optical_flow_sensor>`_.



.. todo:: we need to be able to verify mount_status works/setup.

Expand Down Expand Up @@ -184,13 +184,13 @@ For example, the following code can be used in the callback to only print output
Home location
-------------

The *Home location* is set when a vehicle is armed and first gets a good location fix from the GPS. The location is used
The *Home location* is set when a vehicle first gets a good location fix from the GPS. The location is used
as the target when the vehicle does a "return to launch". In Copter missions (and often Plane) missions, the altitude of
waypoints is set relative to this position.

The behaviour of :py:attr:`Vehicle.home_location <dronekit.lib.Vehicle.home_location>` is slightly different than other attributes:
:py:attr:`Vehicle.home_location <dronekit.lib.Vehicle.home_location>` has the following behaviour:

* In order to get the current value you must first download :py:attr:`Vehicle.commands <dronekit.lib.Vehicle.commands>`, as shown:
* In order to *get* the current value you must first download :py:attr:`Vehicle.commands <dronekit.lib.Vehicle.commands>`, as shown:

.. code:: python

Expand All @@ -200,11 +200,35 @@ The behaviour of :py:attr:`Vehicle.home_location <dronekit.lib.Vehicle.home_loca
print " Home Location: %s" % vehicle.home_location

The returned value is a :py:class:`LocationGlobal <dronekit.lib.LocationGlobal>` object
(or ``None`` before you download the commands).
* The attribute is not observable.
* While you cannot directly set the attribute it can be :ref:`set using a message <guided_mode_copter_set_home>`.
(or ``None`` before you download the commands). If the location has not been set, then
the returned value's attributes will all be set to zero.

* The attribute can be *set* to a :py:class:`LocationGlobal <dronekit.lib.LocationGlobal>` object
(the code fragment below sets it to the current location):

.. code:: python

vehicle.home_location=vehicle.location.global_frame

There are some caveats:

* You must download the commands again to read the changed ``home_location``.
* The autopilot must first set the value (on startup) before it can be changed by DroneKit-Python code.
* The new location must be within 50 km of the EKF origin or setting the value will silently fail.

* The attribute is not observable.


.. note::

:py:attr:`Vehicle.home_location <dronekit.lib.Vehicle.home_location>` behaves this way because
ArduPilot implements/stores the home location as a waypoint rather than sending them as messages.
While DroneKit-Python hides this fact from you when working with commands, to access the value
you still need to download the commands.

We hope to improve this attribute in later versions of ArduPilot, where there may be specific
commands to get the home location from the vehicle.


.. _vehicle_state_parameters:

Expand Down
6 changes: 3 additions & 3 deletions dronekit/lib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1286,7 +1286,7 @@ class CommandSequence(object):

The current commands/mission for a vehicle are accessed using the :py:attr:`Vehicle.commands <dronekit.lib.Vehicle.commands>` attribute.
Waypoints are not downloaded from vehicle until :py:func:`download()` is called. The download is asynchronous;
use :py:func:`wait_valid()` to block your thread until the download is complete.
use :py:func:`wait_ready()` to block your thread until the download is complete.
The code to download the commands from a vehicle is shown below:

.. code-block:: python
Expand All @@ -1298,7 +1298,7 @@ class CommandSequence(object):
# Download the vehicle waypoints (commands). Wait until download is complete.
cmds = vehicle.commands
cmds.download()
cmds.wait_valid()
cmds.wait_ready()

The set of commands can be changed and uploaded to the client. The changes are not guaranteed to be complete until
:py:func:`upload() <Vehicle.commands.upload>` is called.
Expand Down Expand Up @@ -1342,7 +1342,7 @@ def __init__(self, vehicle):
def download(self):
'''
Download all waypoints from the vehicle.
The download is asynchronous. Use :py:func:`wait_valid()` to block your thread until the download is complete.
The download is asynchronous. Use :py:func:`wait_ready()` to block your thread until the download is complete.
'''
self.wait_ready()
self._vehicle._ready_attrs.remove('commands')
Expand Down
31 changes: 4 additions & 27 deletions examples/guided_set_speed_yaw/guided_set_speed_yaw.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ def arm_and_takeoff(aTargetAltitude):
* MAV_CMD_CONDITION_YAW - set direction of the front of the Copter (latitude, longitude)
* MAV_CMD_DO_SET_ROI - set direction where the camera gimbal is aimed (latitude, longitude, altitude)
* MAV_CMD_DO_CHANGE_SPEED - set target speed in metres/second.
* MAV_CMD_DO_SET_HOME - set the home location.


The full set of available commands are listed here:
Expand Down Expand Up @@ -163,29 +162,6 @@ def set_speed(speed):
vehicle.send_mavlink(msg)


def set_home(aLocation, aCurrent=1):
"""
Send MAV_CMD_DO_SET_HOME command to set the Home location to either the current LocationGlobal
or a specified location.

For more information see:
http://copter.ardupilot.com/common-mavlink-mission-command-messages-mav_cmd/#mav_cmd_do_set_home
"""
# create the MAV_CMD_DO_SET_HOME command:
# http://copter.ardupilot.com/common-mavlink-mission-command-messages-mav_cmd/#mav_cmd_do_set_home
msg = vehicle.message_factory.command_long_encode(
0, 0, # target system, target component
mavutil.mavlink.MAV_CMD_DO_SET_HOME, #command
0, #confirmation
aCurrent, #param 1: 1 to use current position, 2 to use the entered values.
0, 0, 0, #params 2-4
aLocation.lat,
aLocation.lon,
aLocation.alt
)
# send command to vehicle
vehicle.send_mavlink(msg)



"""
Expand Down Expand Up @@ -606,9 +582,10 @@ def send_global_velocity(velocity_x, velocity_y, velocity_z):
time.sleep(DURATION)
send_global_velocity(0,0,0)

print("Set new Home location to current location")
set_home(vehicle.location.global_frame)
print "Get new home location" #This reloads the home location in GCSs
print("Set new home location to current location")
vehicle.home_location=vehicle.location.global_frame
print "Get new home location"
#This reloads the home location in DroneKit and GCSs
cmds = vehicle.commands
cmds.download()
cmds.wait_ready()
Expand Down
2 changes: 1 addition & 1 deletion examples/mission_import_export/mission_import_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def download_mission():
cmds = vehicle.commands
cmds.download()
cmds.wait_valid()
for cmd in cmds:
for cmd in cmds:
missionlist.append(cmd)
return missionlist

Expand Down
32 changes: 28 additions & 4 deletions examples/vehicle_state/vehicle_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,40 @@
print " Armed: %s" % vehicle.armed # settable


# Get Vehicle Home location
print "\n Home Location (before downloading waypoints): %s" % vehicle.home_location
# Get Vehicle Home location
# Note: home_location attributes will be 0.0 if the autopilot has not yet set the home location.
cmds = vehicle.commands
cmds.download()
cmds.wait_ready()
print " Home Location: %s" % vehicle.home_location


# Set vehicle home_location, mode, and armed attributes (the only settable attributes)
print "\nSet new home location"

# Home location must first have been set by the autopilot before we can set it.
while vehicle.home_location.lat == 0.0:
cmds = vehicle.commands
cmds.download()
cmds.wait_ready()
if vehicle.home_location.lat == 0.0:
print " Waiting for home location: %s" % vehicle.home_location
else:
print "\n Autopilot set home location: %s" % vehicle.home_location

# Home location must be within 50km of EKF home location (or setting will fail silently)
# In this case, just set value to current location with an easily recognisable altitude (222)
my_location_alt=vehicle.location.global_frame
my_location_alt.alt=222
vehicle.home_location=my_location_alt

#Confirm it is written out (note that you must re-download commands)
cmds = vehicle.commands
cmds.download()
cmds.wait_ready()
print " Home Location (after downloading waypoints): %s" % vehicle.home_location
print " New Home Location (altitude should be 222): %s" % vehicle.home_location


# Set vehicle mode and armed attributes (the only settable attributes)
print "\nSet Vehicle.mode=GUIDED (currently: %s)" % vehicle.mode.name
vehicle.mode = VehicleMode("GUIDED")
while not vehicle.mode.name=='GUIDED': #Wait until mode has changed
Expand Down