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
1 change: 1 addition & 0 deletions docs/examples/vehicle_state.rst
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ On the command prompt you should see (something like):
Mount status: [None, None, None]
Battery: Battery:voltage=12.587,current=0.0,level=100
EKF OK?: False
Last Heartbeat: 0.769999980927
Rangefinder: Rangefinder: distance=None, voltage=None
Rangefinder distance: None
Rangefinder voltage: None
Expand Down
8 changes: 4 additions & 4 deletions docs/guide/migrating.rst
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ values as were passed to *MAVProxy* when setting up a connection in DKPY 1.x (in
populated with values from the vehicle. Check out :py:func:`Vehicle.wait_ready() <dronekit.lib.Vehicle.wait_ready>` for more
information (this method is used by the ``connect()`` implementation).

:py:func:`connect() <dronekit.lib.connect>` also has arguments for setting the baud rate
and returning your own :ref:`custom vehicle classes <example_create_attribute>`.
:py:func:`connect() <dronekit.lib.connect>` also has arguments for setting the baud rate,
returning your own :ref:`custom vehicle classes <example_create_attribute>` and setting the length of the connection timeout.


After connecting, the returned ``vehicle`` can be used in exactly the same way as in DKPY 1.x.
Expand Down Expand Up @@ -112,7 +112,6 @@ Remove code that checks the ``api.exit`` status (note that the ``api.exit`` call
In fact you should delete all references to ``APIConnection`` class and its methods (``get_vehicles()``, ``exit()`` and ``stop()``).


.. todo:: Find out how to check the connection status is still valid. That would go in separate section.


Script completion checks
Expand Down Expand Up @@ -305,7 +304,8 @@ a few more attributes have been added, including:
:py:func:`Vehicle.heading <dronekit.lib.Vehicle.heading>`,
:py:func:`Vehicle.mount_status <dronekit.lib.Vehicle.mount_status>`,
:py:func:`Vehicle.ekf_ok <dronekit.lib.Vehicle.ekf_ok>`,
:py:func:`Vehicle.is_armable <dronekit.lib.Vehicle.is_armable>`.
:py:func:`Vehicle.is_armable <dronekit.lib.Vehicle.is_armable>`,
:py:func:`Vehicle.last_heartbeat <dronekit.lib.Vehicle.last_heartbeat>`.


Channel Overrides
Expand Down
8 changes: 5 additions & 3 deletions docs/guide/vehicle_state_and_parameters.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Vehicle state information is exposed through vehicle *attributes*. DroneKit-Pyth
:py:attr:`Vehicle.battery <dronekit.lib.Vehicle.battery>`,
:py:attr:`Vehicle.rangefinder <dronekit.lib.Vehicle.rangefinder>`,
:py:attr:`Vehicle.ekf_ok <dronekit.lib.Vehicle.ekf_ok>`,
:py:attr:`Vehicle.last_heartbeat <dronekit.lib.Vehicle.last_heartbeat>`,
:py:attr:`Vehicle.home_location <dronekit.lib.Vehicle.home_location>`,
:py:func:`Vehicle.system_status <dronekit.lib.Vehicle.system_status>`,
:py:func:`Vehicle.heading <dronekit.lib.Vehicle.heading>`,
Expand Down Expand Up @@ -76,6 +77,7 @@ regularly updated from MAVLink messages sent by the vehicle).
print "Mount status: %s" % vehicle.mount_status
print "Battery: %s" % vehicle.battery
print "EKF OK?: %s" % vehicle.ekf_ok
print "Last Heartbeat: %s" % vehicle.last_heartbeat
print "Rangefinder: %s" % vehicle.rangefinder
print "Rangefinder distance: %s" % vehicle.rangefinder.distance
print "Rangefinder voltage: %s" % vehicle.rangefinder.voltage
Expand Down Expand Up @@ -239,7 +241,7 @@ callback that will be called for any/all attribute changes:
def wildcard_callback(self, attr_name, value):
print " CALLBACK: (%s): %s" % (attr_name,value)

print "\nAdd attribute calback detecting any attribute change"
print "\nAdd attribute callback detecting any attribute change"
vehicle.add_attribute_listener('*', wildcard_callback)


Expand Down Expand Up @@ -324,7 +326,7 @@ Parameters
==========

Vehicle parameters provide the information used to configure the autopilot for the vehicle-specific hardware/capabilities.
The available parameters for each platform are documented in the ardupilot wiki here:
The available parameters for each platform are documented in the ArduPilot wiki here:
`Copter Parameters <http://copter.ardupilot.com/wiki/configuration/arducopter-parameters/>`_,
`Plane Parameters <http://plane.ardupilot.com/wiki/arduplane-parameters/>`_,
`Rover Parameters <http://rover.ardupilot.com/wiki/apmrover2-parameters/>`_
Expand Down Expand Up @@ -436,4 +438,4 @@ for "any parameter") using the
Known issues
============

Known issues and improvement suggestions can viewed on `github here <https://github.com/dronekit/dronekit-python/issues>`_.
Known issues and improvement suggestions can viewed on `Github here <https://github.com/dronekit/dronekit-python/issues>`_.
45 changes: 45 additions & 0 deletions dronekit/lib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1149,6 +1149,51 @@ def listener(self, name, msg):
errprinter('>>> ...link restored.')
self._heartbeat_timeout = False

self._last_heartbeat = None

@handler.forward_loop
def listener(_):
if self._heartbeat_lastreceived:
self._last_heartbeat = time.time() - self._heartbeat_lastreceived
self.notify_attribute_listeners('last_heartbeat', self.last_heartbeat)

@property
def last_heartbeat(self):
"""
Time since last MAVLink heartbeat was received (in seconds).

The attribute can be used to monitor link activity and implement script-specific timeout handling.

For example, to pause the script if no heartbeat is received for more than 1 second you might implement
the following observer, and use ``pause_script`` in a program loop to wait until the link is recovered:

.. code-block:: python

pause_script=False

@vehicle.on_attribute('last_heartbeat')
def listener(self, attr_name, value):
global pause_script
if value > 1 and not pause_script:
print "Pausing script due to bad link"
pause_script=True;
if value < 1 and pause_script:
pause_script=False;
print "Un-pausing script"

The observer will be called at the period of the messaging loop (about every 0.01 seconds). Testing
on SITL indicates that ``last_heartbeat`` averages about .5 seconds, but will rarely exceed 1.5 seconds
when connected. Whether heartbeat monitoring can be useful will very much depend on the application.


.. note::

If you just want to change the heartbeat timeout you can modify the ``heartbeat_timeout``
parameter passed to the :py:func:`connect() <dronekit.lib.connect>` function.

"""
return self._last_heartbeat

def on_message(self, name):
"""
Decorator for message listener callback functions.
Expand Down
4 changes: 1 addition & 3 deletions examples/vehicle_state/vehicle_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
print " Mount status: %s" % vehicle.mount_status
print " Battery: %s" % vehicle.battery
print " EKF OK?: %s" % vehicle.ekf_ok
print " Last Heartbeat: %s" % vehicle.last_heartbeat
print " Rangefinder: %s" % vehicle.rangefinder
print " Rangefinder distance: %s" % vehicle.rangefinder.distance
print " Rangefinder voltage: %s" % vehicle.rangefinder.voltage
Expand Down Expand Up @@ -151,9 +152,6 @@ def decorated_mode_callback(self, attr_name, value):

# Demonstrate getting callback on any attribute change
def wildcard_callback(self, attr_name, value):
# `attr_name` - attribute name (useful if callback is used for multiple attributes)
# `self` - associated vehicle object (used if callback behaviour is different for multiple vehicles)
# `value` - updated attribute value.
print " CALLBACK: (%s): %s" % (attr_name,value)

print "\nAdd attribute callback detecting ANY attribute change"
Expand Down