Template for a ROS Publisher in Python

I do not like to memorize definitions, methods, procedures, etc., but I like to find solutions to help me reach my final goal.

In ROS, the structure of a simple node is repeated almost every time. To write the Publisher nodes as easily as possible, I’ve made a template that you can modify as you needed.

The template below represents a ROS Publisher node written in Python. All you have to do is to copy the template into a ‘. py’ file, delete the extra text and replace the text in capital letters.

Template for a ROS Publisher in Python

Template for a ROS Publisher in Python (image source)

#The ROS Node Publisher template

#!usr/bin/env python

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

#remove or add the message type
from std_msgs.msg import Int32, Float32, String
from basics.msg import TimerAction, TimerGoal, TimeResult
from time import sleep

#you can define functions to provide the required functionality
def body(arg):
    def_body_here
    return the_value

if __name__=='__main__':
    #add here the node name. In ROS, nodes are unique named.
    rospy.init_node('THE_NAME_OF_THE_NODE')

    #publish messages to a topic using rospy.Publisher class
    name_pub=rospy.Publisher('THE_NAME_OF_THE_TOPIC', THE_TYPE_OF_THE_MESSAGE, queue_size=1)

    #you can define functions to provide the required functionality
    if var1==var2:
        make_something()
        else:
            make_something()

    #set a publishing rate. Below is a publishing rate of 10 times/second
    rate=rospy.Rate(10)

    while not rospy.is_shutdown():
        #some calculation here
        if a1==a2:
            pub.publish(message1)
            else:
                pub.publish(message2)
rate.sleep()

I used the above template to make a ROS node that generates a random number between 0 and 5000.

#!/usr/bin/env python
import rospy

from std_msgs.msg import Int32
from random import randint

#a function to generate the random number
def generate_random_number():
    rnd= randint(0,5000)
    return rnd

if __name__=='__main__':
    rospy.init_node('random_number')
    pub=rospy.Publisher('rand_no', Int32, queue_size=10)
    rate= rospy.Rate(5)

    while not rospy.is_shutdown():
        rnd_gen=generate_random_number()
       
        if rnd_gen<=2500:
         rospy.loginfo("The number generated is lower than 2500: %s", rnd_gen)
         pub.publish(rnd_gen)
        else:
         rospy.loginfo("The number generated is higher than 2500: %s", rnd_gen)
         pub.publish(rnd_gen)

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 node with the following command:

rosrun your_package your_ros_node_file.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 *.