Advertisement

Template for a Simple ROS Subscriber and Publisher in Python

A few days ago I started writing a series of tutorials that ease the work of beginners in ROS. The first tutorial was about a template for a publisher node, and the second tutorial was about a template for a subscriber node in ROS.

Today, I continue the series of tutorials with a template for a publisher and a subscriber node in Python. In addition, I’ll test it by writing and running a publisher and subscriber node.

Template for a Simple ROS Subscriber and Publisher in Python

Template for a Simple ROS Subscriber and Publisher in Python (image source)

Copy the template into a ‘. py’ file, delete the information that you don’t need and replace the text in capital letters.

#ROS Node Subscriber & Publisher template

#!/usr/bin/env python

#remove or add the library/libraries for ROS
import rospy, time, math, cv2, sys

#remove or add the message type
from std_msgs.msg import String, Float32, Image, LaserScan, Int32

varS=None

#define function/functions to provide the required functionality
def fnc_callback(msg):
    global varS
    varS==do_something(msg.data)

if __name__=='__main__':
    #Add here the name of the ROS. In ROS, names are unique named.
    rospy.init_node('NODE_NAME')
    #subscribe to a topic using rospy.Subscriber class
    sub=rospy.Subscriber('TOPIC_NAME', THE_TYPE_OF_THE_MESSAGE, fnc_callback)
    #publish messages to a topic using rospy.Publisher class
    pub=rospy.Publisher('TOPIC_NAME', THE_TYPE_OF_THE_MESSAGE, queue_size=1)
    rate=rospy.Rate(10)

    while not rospy.is_shutdown():
        if varS<= var2:
            varP=something()
        else:
            varP=something()

        pub.publish(varP)
        rate.sleep()
   

I used the above template to write a ROS node that will display the random numbers received from another node, then I add some conditional statements, and finally I publish the results.

#!/usr/bin/env python

import rospy

from std_msgs.msg import Int32

varS=None

def fnc_callback(msg):
    global varS
    varS=msg.data
   
if __name__=='__main__':
    rospy.init_node('NODE_SUB_AND_PUB')
   
    sub=rospy.Subscriber('rand_no', Int32, fnc_callback)
    pub=rospy.Publisher('sub_pub', Int32, queue_size=1)
    rate=rospy.Rate(10)

    while not rospy.is_shutdown():
        if varS<= 2500:
            varP=0
        else:
             varP=1
   
        pub.publish(varP)
        rate.sleep()

To run the above node, navigate to the .py file and make it executable. The command is:

chmod u+x my_python_file.py

After the file is executable, you can run the node.

Step 1: open a new Terminal and run the command:

roscore

Step 2: open a new Terminal and run the publisher node with the following command:

rosrun your_package your_ros_node_that_generates_random_number.py

Step 3: open a new Terminal and run the publisher and subscriber node with the following command:

rosrun your_package your_ros_node_for_publisher_and_subscriber.py

Commerce Content is independent of articles and advertising, and if you buy something through our posts, I may get a small share of the sale.

Leave a Reply

Required fields are marked *.