diff --git a/docs/examples/vehicle_state.rst b/docs/examples/vehicle_state.rst index 5e1c91989..624e3f571 100644 --- a/docs/examples/vehicle_state.rst +++ b/docs/examples/vehicle_state.rst @@ -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 diff --git a/docs/guide/migrating.rst b/docs/guide/migrating.rst index b4f88333f..04d3c0619 100644 --- a/docs/guide/migrating.rst +++ b/docs/guide/migrating.rst @@ -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() ` for more information (this method is used by the ``connect()`` implementation). - :py:func:`connect() ` also has arguments for setting the baud rate - and returning your own :ref:`custom vehicle classes `. + :py:func:`connect() ` also has arguments for setting the baud rate, + returning your own :ref:`custom vehicle classes ` 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. @@ -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 @@ -305,7 +304,8 @@ a few more attributes have been added, including: :py:func:`Vehicle.heading `, :py:func:`Vehicle.mount_status `, :py:func:`Vehicle.ekf_ok `, -:py:func:`Vehicle.is_armable `. +:py:func:`Vehicle.is_armable `, +:py:func:`Vehicle.last_heartbeat `. Channel Overrides diff --git a/docs/guide/vehicle_state_and_parameters.rst b/docs/guide/vehicle_state_and_parameters.rst index ca2ac97a0..9c2713fdb 100644 --- a/docs/guide/vehicle_state_and_parameters.rst +++ b/docs/guide/vehicle_state_and_parameters.rst @@ -33,6 +33,7 @@ Vehicle state information is exposed through vehicle *attributes*. DroneKit-Pyth :py:attr:`Vehicle.battery `, :py:attr:`Vehicle.rangefinder `, :py:attr:`Vehicle.ekf_ok `, +:py:attr:`Vehicle.last_heartbeat `, :py:attr:`Vehicle.home_location `, :py:func:`Vehicle.system_status `, :py:func:`Vehicle.heading `, @@ -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 @@ -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) @@ -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 `_, `Plane Parameters `_, `Rover Parameters `_ @@ -436,4 +438,4 @@ for "any parameter") using the Known issues ============ -Known issues and improvement suggestions can viewed on `github here `_. \ No newline at end of file +Known issues and improvement suggestions can viewed on `Github here `_. \ No newline at end of file diff --git a/dronekit/lib/__init__.py b/dronekit/lib/__init__.py index 7a85a0df1..cd3da80b4 100644 --- a/dronekit/lib/__init__.py +++ b/dronekit/lib/__init__.py @@ -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() ` function. + + """ + return self._last_heartbeat + def on_message(self, name): """ Decorator for message listener callback functions. diff --git a/examples/vehicle_state/vehicle_state.py b/examples/vehicle_state/vehicle_state.py index f4a83b568..71dc28060 100644 --- a/examples/vehicle_state/vehicle_state.py +++ b/examples/vehicle_state/vehicle_state.py @@ -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 @@ -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"