diff --git a/docs/examples/vehicle_state.rst b/docs/examples/vehicle_state.rst index 5a19e8065..f62c16fbf 100644 --- a/docs/examples/vehicle_state.rst +++ b/docs/examples/vehicle_state.rst @@ -15,8 +15,11 @@ Running the example =================== The vehicle and DroneKit should be 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. + +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). Once MAVProxy is running and the API is loaded, you can start the example by typing: ``api start vehicle_state.py``. @@ -42,6 +45,9 @@ On the *MAVProxy* console you should see (something like): Airspeed: 0.0 Mount status: [None, None, None] Battery: Battery voltage: 12590, current: 0, level: 99 + Rangefinder: Rangefinder: distance=0.189999997616, voltage=0.0190000012517 + Rangefinder distance: 0.189999997616 + Rangefinder voltage: 0.0190000012517 Mode: STABILIZE Armed: False Set Vehicle.mode=GUIDED (currently: STABILIZE) @@ -76,7 +82,7 @@ On the *MAVProxy* console you should see (something like): Channel default values: {'1': 1500, '3': 1000, '2': 1500, '5': 1800, '4': 1500, '7': 1000, '6': 1000, '8': 1800} Cancelling override - Reset vehicle atributes/parameters and exit + Reset vehicle attributes/parameters and exit Got MAVLink msg: COMMAND_ACK {command : 11, result : 0} APM: DISARMING MOTORS Got MAVLink msg: COMMAND_ACK {command : 400, result : 0} diff --git a/docs/guide/vehicle_state_and_parameters.rst b/docs/guide/vehicle_state_and_parameters.rst index f4a4ace79..808fda9d0 100644 --- a/docs/guide/vehicle_state_and_parameters.rst +++ b/docs/guide/vehicle_state_and_parameters.rst @@ -25,19 +25,21 @@ Vehicle state information is exposed through vehicle *attributes*. DroneKit-Pyth :py:attr:`Vehicle.location `, :py:attr:`Vehicle.attitude `, :py:attr:`Vehicle.velocity `, -:py:attr:`Vehicle.mode `, :py:attr:`Vehicle.airspeed `, :py:attr:`Vehicle.groundspeed `, :py:attr:`Vehicle.gps_0 `, -:py:attr:`Vehicle.armed `, -:py:attr:`Vehicle.location `, -:py:attr:`Vehicle.mount_status `. +:py:attr:`Vehicle.mount_status `, +:py:attr:`Vehicle.battery `, +:py:attr:`Vehicle.rangefinder `, +: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 ` status can be :ref:`written `. + .. _vehicle_state_read_attributes: Getting attributes @@ -48,21 +50,35 @@ The code fragment below shows how to read and print all the attributes. The valu .. code:: python - # v is an instance of the Vehicle class - print "Location: %s" % v.location - print "Attitude: %s" % v.attitude - print "Velocity: %s" % v.velocity - print "GPS: %s" % v.gps_0 - print "groundspeed: %s" % v.groundspeed - print "airspeed: %s" % v.airspeed - print "mount_status: %s" % v.mount_status - print "Mode: %s" % v.mode.name # settable - print "Armed: %s" % v.armed # settable - -If one of these attributes cannot be retrieved or is invalid then the returned object will contain + # vehicle is an instance of the Vehicle class + print "Location: %s" % vehicle.location + print "Attitude: %s" % vehicle.attitude + print "Velocity: %s" % vehicle.velocity + print "GPS: %s" % vehicle.gps_0 + print "Groundspeed: %s" % vehicle.groundspeed + print "Airspeed: %s" % vehicle.airspeed + print "Mount status: %s" % vehicle.mount_status + print "Battery: %s" % vehicle.battery + print "Rangefinder: %s" % vehicle.rangefinder + print "Rangefinder distance: %s" % vehicle.rangefinder.distance + print "Rangefinder voltage: %s" % vehicle.rangefinder.voltage + print "Mode: %s" % vehicle.mode.name # settable + print "Armed: %s" % vehicle.armed # settable + + +If an attribute cannot be retrieved then the returned object will contain ``None`` values for its members (for example, if there was no GPS lock then :py:attr:`Vehicle.gps_0 ` would return a :py:class:`GPSInfo ` -with ``None`` values for ``eph``, ``satellites_visible`` etc.) +with ``None`` values for ``eph``, ``satellites_visible`` etc.) +Attributes will also return ``None`` if the associated hardware is not present on the connected device. + +.. tip:: + + If you're using a :ref:`simulated vehicle ` you can add support for optional hardware including + `rangefinders `_ + and `optical flow sensors `_. + + .. todo:: we need to be able to verify mount_status works/setup. @@ -82,8 +98,8 @@ then forces DroneKit to send outstanding messages. .. code:: python #disarm the vehicle - v.armed = False - v.flush() # Flush to ensure changes are sent to autopilot + vehicle.armed = False + vehicle.flush() # Flush to ensure changes are sent to autopilot .. warning:: @@ -99,10 +115,10 @@ to confirm they have changed before proceeding. .. code:: python - v.mode = VehicleMode("GUIDED") - v.armed = True - v.flush() # Flush to ensure changes are sent to autopilot - while not v.mode.name=='GUIDED' and not v.armed and not api.exit: + vehicle.mode = VehicleMode("GUIDED") + vehicle.armed = True + vehicle.flush() # Flush to ensure changes are sent to autopilot + while not vehicle.mode.name=='GUIDED' and not vehicle.armed and not api.exit: print " Getting ready to take off ..." time.sleep(1) @@ -113,13 +129,8 @@ to confirm they have changed before proceeding. Observing attribute changes --------------------------- -You can observe any of the attributes and will receive notification if their values change. This allows you to -monitor changes to velocity and other vehicle state without the need for polling. - -.. warning:: - - There is currently `a defect (#60) `_ that means that after an - observer is triggered, the callback function is run on every heartbeat (whether or not the observed attribute changes). +You can observe any of the attributes and will receive notification every time a value is received from the connected vehicle. +This allows you to monitor changes to velocity and other vehicle state without the need for polling. Observers are added using :py:func:`Vehicle.add_attribute_observer() `, specifying the name of the attribute to observe and a callback function. The same string is passed to the callback @@ -131,18 +142,35 @@ The code snippet below shows how to add (and remove) a callback function to obse # Callback function. The parameter is the name of the observed attribute (a string) def location_callback(attribute): - print " CALLBACK: Location changed to: ", v.location + print " CALLBACK: Location changed to: ", vehicle.location # Add a callback. The first parameter the name of the observed attribute (a string). - v.add_attribute_observer('location', location_callback) + vehicle.add_attribute_observer('location', location_callback) # Wait 2s so callback can be notified before the observer is removed time.sleep(2) # Remove observer - specifying the attribute and previously registered callback function - v.remove_attribute_observer('location', location_callback) + vehicle.remove_attribute_observer('location', location_callback) + + +The callback is triggered `every time a message is received from the vehicle `_ +(whether or not the observed attribute changes). Callback code may therefore choose to cache the result and only report changes. +For example, the following code can be used in the callback to only print output when the value of :py:attr:`Vehicle.rangefinder ` changes. + +.. code:: python + last_rangefinder_distance=0 + + def rangefinder_callback(rangefinder): + global last_rangefinder_distance + if last_rangefinder_distance == round(vehicle.rangefinder.distance, 1): + return + last_rangefinder_distance = round(vehicle.rangefinder.distance, 1) + print " Rangefinder (metres): %s" % last_rangefinder_distance + + vehicle.add_attribute_observer('rangefinder', rangefinder_callback) @@ -223,7 +251,7 @@ Unlike other vehicle state information, the home location is accessed as the 0 i .. code:: python - cmds = v.commands + cmds = vehicle.commands cmds.download() cmds.wait_valid() print " Home WP: %s" % cmds[0] @@ -265,7 +293,7 @@ The code snippet below shows how to set a “demo” callback function as the ca print "Received", message # Set MAVLink callback handler (after getting Vehicle instance) - v.set_mavlink_callback(mavrx_debug_handler) + vehicle.set_mavlink_callback(mavrx_debug_handler) .. warning:: @@ -303,18 +331,18 @@ An example of setting and clearing overrides is given below: .. code:: python # Override the channel for roll and yaw - v.channel_override = { "1" : 900, "4" : 1000 } - v.flush() + vehicle.channel_override = { "1" : 900, "4" : 1000 } + vehicle.flush() #print current override values - print "Current overrides are:", v.channel_override + print "Current overrides are:", vehicle.channel_override # Print channel values (values if overrides removed) - print "Channel default values:", v.channel_readback + print "Channel default values:", vehicle.channel_readback # Cancel override by setting channels to 0 - v.channel_override = { "1" : 0, "4" : 0 } - v.flush() + vehicle.channel_override = { "1" : 0, "4" : 0 } + vehicle.flush() diff --git a/droneapi/lib/__init__.py b/droneapi/lib/__init__.py index c32ea72b4..cbbb867fe 100644 --- a/droneapi/lib/__init__.py +++ b/droneapi/lib/__init__.py @@ -196,8 +196,8 @@ class Rangefinder(object): """ Rangefinder readings. - :param distance: Distance. - :param voltage: Voltage. + :param distance: Distance (metres). ``None`` if the vehicle doesn't have a rangefinder. + :param voltage: Voltage (volts). ``None`` if the vehicle doesn't have a rangefinder. """ def __init__(self, distance, voltage): self.distance = distance @@ -481,6 +481,11 @@ class Vehicle(HasObservers): Current system :py:class:`Battery` status. + .. py:attribute:: rangefinder + + :py:class:`Rangefinder` distance and voltage values. + + .. py:attribute:: channel_override .. warning:: diff --git a/examples/vehicle_state/vehicle_state.py b/examples/vehicle_state/vehicle_state.py index e48487ee0..eac0c08f5 100644 --- a/examples/vehicle_state/vehicle_state.py +++ b/examples/vehicle_state/vehicle_state.py @@ -1,12 +1,11 @@ -# -# This example shows how to use DroneKit-Python to get and set vehicle state, parameter and channel-override information. -# It also demonstrates how to observe vehicle attribute (state) changes. -# -# Usage: -# * mavproxy.py -# * module load api -# * api start vehicle-state.py -# +""" +vehicle_state.py: Get and set vehicle state, parameter and channel-override information. + +It also demonstrates how to observe vehicle attribute (state) changes. + +Full documentation is provided at http://python.dronekit.io/examples/vehicle_state.html +""" + from droneapi.lib import VehicleMode from pymavlink import mavutil import time @@ -14,91 +13,93 @@ # First get an instance of the API endpoint api = local_connect() # Get the connected vehicle (currently only one vehicle can be returned). -v = api.get_vehicles()[0] +vehicle = api.get_vehicles()[0] # Get all vehicle attributes (state) print "\nGet all vehicle attribute values:" -print " Location: %s" % v.location -print " Attitude: %s" % v.attitude -print " Velocity: %s" % v.velocity -print " GPS: %s" % v.gps_0 -print " Groundspeed: %s" % v.groundspeed -print " Airspeed: %s" % v.airspeed -print " Mount status: %s" % v.mount_status -print " Battery: %s" % v.battery -print " Mode: %s" % v.mode.name # settable -print " Armed: %s" % v.armed # settable +print " Location: %s" % vehicle.location +print " Attitude: %s" % vehicle.attitude +print " Velocity: %s" % vehicle.velocity +print " GPS: %s" % vehicle.gps_0 +print " Groundspeed: %s" % vehicle.groundspeed +print " Airspeed: %s" % vehicle.airspeed +print " Mount status: %s" % vehicle.mount_status +print " Battery: %s" % vehicle.battery +print " Rangefinder: %s" % vehicle.rangefinder +print " Rangefinder distance: %s" % vehicle.rangefinder.distance +print " Rangefinder voltage: %s" % vehicle.rangefinder.voltage +print " Mode: %s" % vehicle.mode.name # settable +print " Armed: %s" % vehicle.armed # settable # Set vehicle mode and armed attributes (the only settable attributes) -print "Set Vehicle.mode=GUIDED (currently: %s)" % v.mode.name -v.mode = VehicleMode("GUIDED") -v.flush() # Flush to guarantee that previous writes to the vehicle have taken place -while not v.mode.name=='GUIDED' and not api.exit: #Wait until mode has changed +print "Set Vehicle.mode=GUIDED (currently: %s)" % vehicle.mode.name +vehicle.mode = VehicleMode("GUIDED") +vehicle.flush() # Flush to guarantee that previous writes to the vehicle have taken place +while not vehicle.mode.name=='GUIDED' and not api.exit: #Wait until mode has changed print " Waiting for mode change ..." time.sleep(1) -print "Set Vehicle.armed=True (currently: %s)" % v.armed -v.armed = True -v.flush() -while not v.armed and not api.exit: +print "Set Vehicle.armed=True (currently: %s)" % vehicle.armed +vehicle.armed = True +vehicle.flush() +while not vehicle.armed and not api.exit: print " Waiting for arming..." time.sleep(1) # Show how to add and remove and attribute observer callbacks (using mode as example) def mode_callback(attribute): - print " CALLBACK: Mode changed to: ", v.mode.name + print " CALLBACK: Mode changed to: ", vehicle.mode.name print "\nAdd mode attribute observer for Vehicle.mode" -v.add_attribute_observer('mode', mode_callback) +vehicle.add_attribute_observer('mode', mode_callback) -print " Set mode=STABILIZE (currently: %s)" % v.mode.name -v.mode = VehicleMode("STABILIZE") -v.flush() +print " Set mode=STABILIZE (currently: %s)" % vehicle.mode.name +vehicle.mode = VehicleMode("STABILIZE") +vehicle.flush() print " Wait 2s so callback invoked before observer removed" time.sleep(2) # Remove observer - specifying the attribute and previously registered callback function -v.remove_attribute_observer('mode', mode_callback) +vehicle.remove_attribute_observer('mode', mode_callback) # Get Vehicle Home location ((0 index in Vehicle.commands) print "\nGet home location" -cmds = v.commands +cmds = vehicle.commands cmds.download() cmds.wait_valid() print " Home WP: %s" % cmds[0] # Get/Set Vehicle Parameters -print "\nRead vehicle param 'THR_MIN': %s" % v.parameters['THR_MIN'] +print "\nRead vehicle param 'THR_MIN': %s" % vehicle.parameters['THR_MIN'] print "Write vehicle param 'THR_MIN' : 10" -v.parameters['THR_MIN']=10 -v.flush() -print "Read new value of param 'THR_MIN': %s" % v.parameters['THR_MIN'] +vehicle.parameters['THR_MIN']=10 +vehicle.flush() +print "Read new value of param 'THR_MIN': %s" % vehicle.parameters['THR_MIN'] # Overriding an RC channel # NOTE: CHANNEL OVERRIDES may be useful for simulating user input and when implementing certain types of joystick control. #DO NOT use unless there is no other choice (there almost always is!) print "\nOverriding RC channels for roll and yaw" -v.channel_override = { "1" : 900, "4" : 1000 } -v.flush() -print " Current overrides are:", v.channel_override -print " Channel default values:", v.channel_readback # All channel values before override +vehicle.channel_override = { "1" : 900, "4" : 1000 } +vehicle.flush() +print " Current overrides are:", vehicle.channel_override +print " Channel default values:", vehicle.channel_readback # All channel values before override # Cancel override by setting channels to 0 print " Cancelling override" -v.channel_override = { "1" : 0, "4" : 0 } -v.flush() +vehicle.channel_override = { "1" : 0, "4" : 0 } +vehicle.flush() ## Reset variables to sensible values. -print "\nReset vehicle atributes/parameters and exit" -v.mode = VehicleMode("STABILIZE") -v.armed = False -v.parameters['THR_MIN']=130 -v.flush() - +print "\nReset vehicle attributes/parameters and exit" +vehicle.mode = VehicleMode("STABILIZE") +vehicle.armed = False +vehicle.parameters['THR_MIN']=130 +vehicle.flush()