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.
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
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
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
.