Skip to content

Understanding Publisher Subscriber Nodes

Chandra Gummaluru edited this page Feb 2, 2020 · 10 revisions

Understanding Publisher/Subscriber Nodes

In this part of the tutorial, you will understand how publisher/subscriber nodes work.

Note: Make sure you already understand what client libraries are.

A publisher is any node that publishes (i.e., writes) messages to a topic. A subscriber is any node that subscribes to (i.e., reads) messages from a topic.

Topics and Messages

You should think of a topic as kind of like a public channel for communication about a literal topic. Each topic is associated with a particular type of message, whose structure is defined in a .msg file, associated with the topic.

Using rostopic

The rostopic tool has several useful commands for working with topics.

We can view the messages going through a particular topic by running the echo command:

rostopic echo <topic>

We can view the message type associated with a topic using the type command

rostopic type <topic>

Using rosmsg

The rosmsg tool has several useful commands for working with messages.

We can view more details about a particular message type using the show command

ros_msg show <message_type>

Writing a Publisher and Subscriber

First, if you haven't already done so, creating a ROS workspace. Next, within your ROS workspace, create a ROS package.

Now, make sure you are within your package directory.

roscd <package_directory>

Within the package directory, create a directory to store our scripts in.

mkdir scripts
cd scripts

Note: We don't necessarily have to call the directory scripts. In fact, you don't even need to store the nodes in a separate directory. However, this is considered good practice.

Creating the Node

We will be using Python (via rospy) to create the node.

First, create a new Python file, and add the following:

#!/usr/bin/env python

Note: The above line is called a shebang and is needed for the executable to be recognised as Python.

Next, import rospy.

import rospy

We can now initialize the node using

rospy.init_node('node_name')

Recall that a node can be a publisher, subscriber, or both. In fact, a node can publish and subscribe to as many topics as needed.

Adding a Publisher Interface

A node publishes to a topic using a publisher interface. In Python, this is done using

pub = rospy.Publisher('topic_name', msg_type)

To publish data, we can use

pub.publish(data)

A publisher (in Python) typically uses the following flow:

#!/usr/bin/env python
import rospy

def publisher():
    rospy.init_node('node_name')
    pub = rospy.Publisher('topic_name', msg_type)
    rate = rospy.Rate(publications_per_second)

    while not rospy.is_shutdown():
        data = ??? #get data/
        pub.publish(data)
        rate.sleep()

if __name__ == '__main__':
    try:
        publisher()
    except rospy.ROSInterruptException:
        pass

Adding a Subscriber Interface

A node subscribes to a topic using a subscriber interface. In Python, this is done using

sub = rospy.Subscriber('topic_name', msg_type, callback_function)

The data received can be processed within the callback function specified.

A subscriber (in Python) typically uses the following flow:

#!/usr/bin/env python
import rospy

def callback_fnc(data):
  ??? #do something with the data

def subscriber():
    rospy.init_node('node_name')
    sub = rospy.Subscriber('topic_name', msg_type, callback_fnc)
    
    rospy.spin()

if __name__ == '__main__':
    try:
        subscriber()
    except rospy.ROSInterruptException:
        pass

Building the Node

Recall that we can build the node by building the entire ROS package in which it is contained.

Running the Node

Recall that we can run a node using rosrun or create a launch file and run the node using roslaunch.

Note: You will need to make your files executable by running

chmod +x <file_name>

Next Steps

We have provided a step-by-step example of creating a publisher and subscriber that communicate with each other. We highly recommend you try this yourself.

If you feel comfortable with publisher/subscriber nodes, you can also try understanding service nodes.

Clone this wiki locally