MAVLink Messages¶

Some useful MAVLink messages sent by the autopilot are not (yet) directly available to DroneKit-Python scripts through the observable attributes in Vehicle.

This topic shows how you can intercept specific MAVLink messages by defining a listener callback function using the Vehicle.on_message() decorator.

Tip

Example: Create Attribute in App shows how you can extend this approach to create a new Vehicle attribute in your client code.

Creating a message listener¶

The Vehicle.on_message() decorator can be used to set a particular function as the callback handler for a particular message type. You can create listeners for as many messages as you like, or even multiple listeners for the same message.

For example, the code snippet below shows how to set a listener for the RANGEFINDER message:

#Create a message listener using the decorator.
@vehicle.on_message('RANGEFINDER')
def listener(self, name, message):
    print message

Tip

Every single listener can have the same name/prototpye as above (”listener”) because Python does not notice the decorated functions are in the same scope.

Unfortunately this also means that you can’t unregister a callback created using this method.

The messages are classes from the pymavlink library. The output of the code above looks something like:

RANGEFINDER {distance : 0.0899999961257, voltage : 0.00900000054389}
...

You can access the message fields directly. For example, to access the RANGEFINDER message your listener function might look like this:

#Create a message listener using the decorator.
@vehicle.on_message('RANGEFINDER')
def listener(self, name, message):
    print 'distance: %s' % message.distance
    print 'voltage: %s' % message.voltage

Watching all messages¶

You can register a callback for all messages by setting the message name as the wildcard string (’*’):

#Create a message listener for all messages.
@vehicle.on_message('*')
def listener(self, name, message):
    print 'message: %s' % message

Removing an observer¶

Callbacks registered using the Vehicle.on_message() decorator cannot be removed. This is generally not a problem, because in most cases you’re interested in messages for the lifetime of a session.

If you do need to be able to remove messages you can instead add the callback using Vehicle.add_message_listener, and then remove it by calling Vehicle.remove_message_listener.

DroneKit Python

Navigation

  • Introduction
  • Quick Start
  • Developing
  • Guide
    • Connecting to a Vehicle
    • Vehicle State and Settings
    • Taking Off
    • Guiding and Controlling Copter
    • Missions
    • Debugging
    • MAVLink Messages
  • Examples
  • Contributing
  • API Reference

Related Topics

  • Documentation overview
    • Guide
      • Previous: Debugging
      • Next: Examples
©2015-2016, 3D Robotics. | Powered by Sphinx 8.2.3 & Alabaster 1.0.0 | Page source