diff --git a/docs/examples/guided-set-speed-yaw-demo.rst b/docs/examples/guided-set-speed-yaw-demo.rst index dff4cd12a..787023136 100644 --- a/docs/examples/guided-set-speed-yaw-demo.rst +++ b/docs/examples/guided-set-speed-yaw-demo.rst @@ -189,7 +189,6 @@ The functions sending immediate commands are: * :ref:`condition_yaw() ` * :ref:`set_roi(location) ` * :ref:`set_speed(speed) ` -* :ref:`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 `. @@ -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 `_. +* 3.3.0. diff --git a/docs/examples/vehicle_state.rst b/docs/examples/vehicle_state.rst index 07a513ee3..cd94ecb1e 100644 --- a/docs/examples/vehicle_state.rst +++ b/docs/examples/vehicle_state.rst @@ -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 ` so that the example can run. You can also `add a virtual rangefinder `_ -(otherwise the :py:attr:`Vehicle.rangefinder ` attribute may return values of ``None`` for the distance -and voltage). +(otherwise the :py:attr:`Vehicle.rangefinder ` attribute may return +values of ``None`` for the distance and voltage). In summary, after cloning the repository: @@ -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 ... diff --git a/docs/guide/copter/guided_mode.rst b/docs/guide/copter/guided_mode.rst index 40695ef09..2fa224162 100644 --- a/docs/guide/copter/guided_mode.rst +++ b/docs/guide/copter/guided_mode.rst @@ -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 `_ 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 @@ -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() ` 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 `, :ref:`MAV_CMD_DO_CHANGE_SPEED `, :ref:`MAV_CMD_DO_SET_HOME `, :ref:`MAV_CMD_DO_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 `. +Some of the MAV_CMD commands that you might want to send include: :ref:`MAV_CMD_CONDITION_YAW `, :ref:`MAV_CMD_DO_CHANGE_SPEED `, :ref:`MAV_CMD_DO_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 `. @@ -321,42 +321,6 @@ Send the `MAV_CMD_DO_SET_ROI `_ -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: diff --git a/docs/guide/vehicle_state_and_parameters.rst b/docs/guide/vehicle_state_and_parameters.rst index 2d7cf2c34..1a9aaf04a 100644 --- a/docs/guide/vehicle_state_and_parameters.rst +++ b/docs/guide/vehicle_state_and_parameters.rst @@ -30,15 +30,18 @@ Vehicle state information is exposed through vehicle *attributes*. DroneKit-Pyth :py:attr:`Vehicle.mount_status `, :py:attr:`Vehicle.battery `, :py:attr:`Vehicle.rangefinder `, -:py:attr:`Vehicle.home_location ` +:py:attr:`Vehicle.home_location `, :py:attr:`Vehicle.armed `, :py:attr:`Vehicle.mode `. All of the attributes can be :ref:`read ` and :ref:`observed `, -but only the :py:attr:`Vehicle.mode ` and :py:attr:`Vehicle.armed ` +but only the :py:attr:`Vehicle.home_location `, +:py:attr:`Vehicle.mode ` and +:py:attr:`Vehicle.armed ` status can be :ref:`written `. - +The behaviour of :py:attr:`Vehicle.home_location ` is different +from the other attributes, and is :ref:`discussed in its own section below `. .. _vehicle_state_read_attributes: @@ -73,8 +76,6 @@ 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 ` is different, -:ref:`as discussed below `. .. tip:: @@ -82,7 +83,6 @@ The behaviour of :py:attr:`Vehicle.home_location `_ and `optical flow sensors `_. - .. todo:: we need to be able to verify mount_status works/setup. @@ -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 ` is slightly different than other attributes: +:py:attr:`Vehicle.home_location ` has the following behaviour: -* In order to get the current value you must first download :py:attr:`Vehicle.commands `, as shown: +* In order to *get* the current value you must first download :py:attr:`Vehicle.commands `, as shown: .. code:: python @@ -200,11 +200,35 @@ The behaviour of :py:attr:`Vehicle.home_location ` 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 `. + (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 ` 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 ` 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: diff --git a/dronekit/lib/__init__.py b/dronekit/lib/__init__.py index 022798422..51580074e 100644 --- a/dronekit/lib/__init__.py +++ b/dronekit/lib/__init__.py @@ -1286,7 +1286,7 @@ class CommandSequence(object): The current commands/mission for a vehicle are accessed using the :py:attr:`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 @@ -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() ` is called. @@ -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') diff --git a/examples/guided_set_speed_yaw/guided_set_speed_yaw.py b/examples/guided_set_speed_yaw/guided_set_speed_yaw.py index ffba95b55..e5458fd64 100644 --- a/examples/guided_set_speed_yaw/guided_set_speed_yaw.py +++ b/examples/guided_set_speed_yaw/guided_set_speed_yaw.py @@ -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: @@ -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) - """ @@ -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() diff --git a/examples/mission_import_export/mission_import_export.py b/examples/mission_import_export/mission_import_export.py index 0a45e1e23..82dd96108 100644 --- a/examples/mission_import_export/mission_import_export.py +++ b/examples/mission_import_export/mission_import_export.py @@ -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 diff --git a/examples/vehicle_state/vehicle_state.py b/examples/vehicle_state/vehicle_state.py index 672b706a2..249b33d02 100644 --- a/examples/vehicle_state/vehicle_state.py +++ b/examples/vehicle_state/vehicle_state.py @@ -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