Send and receive messages from Ivy with python

This tutorial explains how to send and receive messages to and from an Ivy bus. You should be familiar with basic IVY concepts.

The majority of the work will be done through pprzlink.ivy.IvyMessagesInterface class. You can have a look at the documentation here.

In this tutorial, we will build a simple application which receives PING messages and sends back PONG messages.

Creating an ivy bus

The first step is to create a pprzlink.ivy.IvyMessagesInterface object, which is straightforward:

from ivy.std_api import *
import pprzlink.ivy

# Creation of the ivy interface
ivy = pprzlink.ivy.IvyMessagesInterface(
            agent_name="PprzlinkIvyTutorial",   # Ivy agent name
            start_ivy=False,                    # Do not start the ivy bus now
            ivy_bus="127.255.255.255:2010")     # address of the ivy bus

Subscribe to messages

You can then issue subscriptions on the ivy bus with the pprzlink.ivy.IvyMessagesInterface.subscribe() function. You can subscribe both with a regexp in a string or a PprzMessage specifying the type of message you want. Here we are subscribing to all PING messages.

    # Subscribe to PING messages and sets recv_callback as the callback function.
    ivy.subscribe(recv_callback,message.PprzMessage("datalink", "PING"))

The parameter message.PprzMessage("datalink", "PING") creates an empty PING message which serves as a prototype.

The function passed as first argument to pprzlink.ivy.IvyMessagesInterface.subscribe() (here recv_callback()) will be called when a matching message is received. It will be passed two parameters, the id of the sender of the message (ac_id) and the message itself (pprzMsg).

For now, we only print the message and the id of the sender. Note that since PING is a datalink message, the sender ac_id will be 0.

# Function called when a PING message is received
def recv_callback(ac_id, pprzMsg):
    # Print the message and the sender id
    print ("Received message %s from %s" % (pprzMsg,ac_id))

Sending messages

To send a message we use the pprzlink.ivy.IvyMessagesInterface.send(). We will use this to send a PONG message back when we receive a PING message. We identify ourselves as ac_id 2.

# Function called when a PING message is received
def recv_callback(ac_id, pprzMsg):
    # Print the message and the sender id
    print ("Received message %s from %s" % (pprzMsg,ac_id))
    # Send back a PONG message
    ivy.send(message.PprzMessage("telemetry", "PONG"), sender_id= 2, receiver_id= ac_id)

Complete file

The complete file for this tutorial including waiting for keyboard interuption is here.

#!/usr/bin/env python

from ivy.std_api import *
import pprzlink.ivy

import pprzlink.messages_xml_map as messages_xml_map
import pprzlink.message as message
import time

# Function called when a PING message is received
def recv_callback(ac_id, pprzMsg):
    # Print the message and the sender id
    print ("Received message %s from %s" % (pprzMsg,ac_id))
    # Send back a PONG message
    ivy.send(message.PprzMessage("telemetry", "PONG"), sender_id= 2, receiver_id= ac_id)

# Creation of the ivy interface
ivy = pprzlink.ivy.IvyMessagesInterface(
            agent_name="PprzlinkIvyTutorial",   # Ivy agent name
            start_ivy=False,                    # Do not start the ivy bus now
            ivy_bus="127.255.255.255:2010")     # address of the ivy bus

try:
    # starts the ivy interface
    ivy.start()

    # Subscribe to PING messages and sets recv_callback as the callback function.
    ivy.subscribe(recv_callback,message.PprzMessage("datalink", "PING"))

    # Wait untill ^C is pressed
    while True:
        time.sleep(5)
except KeyboardInterrupt:
    ivy.shutdown()