[ROS In 5 Minutes] 003 – How to create a ROS Publisher

[ROS-In-5-Minutes]-003-How-to-create-a-ROS-Publisher

Written by Ruben Alves

05/05/2018

Hi all!

Welcome to this “ROS In 5 Minutes” videos series.

In today’s video we are going to see how to create a ROS Publisher.

But before we start, if you want to Learn ROS Fast, we recommend you the following courses:

Remember that if you prefer to watch a video, the following video covers the same content of this post.

Step 1. Create an account in Robot Ignite Academy (RIA)

The steps explained in this post can be executed in your local computer without any problem, but if you want an environment that has everything already set, we highly recommend you creating an account in RIA. The platform helps you to Learn ROS Fast in the easiest way possible, without you having to setup the ROS environment locally. The only thing you need is a web browser! If you want to try the platform, you can easily create an account here and start to browse the trial course for free now! We’ll use the ROS Basic course as an example today.

Step 2. Create a package

In order to create our publisher we first need a package. Let’s create one called tutorial under the ~/catkin_ws/src directory with the following command

cd ~/catkin_ws/src
catkin_create_pkg tutorial rospy

Then we create a file called publisher.py  under the ~/catkin_ws/src/tutorial/src  folder and insert the following content on that file:

#! /usr/bin/env python

import rospy
from std_msgs.msg import String

rospy.init_node('tutorial')

publisher = rospy.Publisher('/say_hello', String, queue_size=1)
rate = rospy.Rate(3) # 3 Hz

while not rospy.is_shutdown():
    publisher.publish('Hey!')
    rate.sleep()

Before we execute our file let’s analyze what that code does, starting with the first 3 lines:

#! /usr/bin/env python 

import rospy 
from std_msgs.msg import String

In the first line we basically tell Linux that we use Python code. In the subsequent two lines we import the required packages. The rospy package contains classes and functions that allow us to easily instantiate ROS Nodes (among other functionalities) using python. The type of message we will publish is a String , that is why we imported it. Let’s analyze the remaining code.

rospy.init_node('tutorial') 

publisher = rospy.Publisher('/say_hello', String, queue_size=1) 
rate = rospy.Rate(3) # 3 Hz

With rospy.init_node(‘tutorial’)  we basically initialize our ROS Node and call it tutorial. A ROS Node can have the name you want, as far it doesn’t contain spaces. If you choose a name of a node that already exists, then ROS will automatically kill the existing one when you launch yours. Be aware of that.

After initializing our node we create our publisher. In the first parameter of rospy.Publisher we provide name of the topic in which we want to publish. If you don’t know yet what is a Topic, please check our other posts or take our ROS In 5 Days (Python) course that explains that and also shows you how to create publishers, subscribers, services, actions, how to use debugging tools, etc. Now, back torospy.Publisher , in the second parameter we specify the type of message we want to publish in that topic, which in this case is String. The third parameter is the queue_size, which specify the amount of messages that will be queued before the messages are dropped if not consumed.

The rate variable we created is because we want to publish 3 messages per second. Let’s now check the last part of our code:

while not rospy.is_shutdown(): 
    publisher.publish('Hey!')
    rate.sleep()

The line while not rospy.is_shutdown(): is to keep our node publishing messages while roscore is running. If somehow roscore is killed, our ROS Node just finishes. This way, whoever is running our code won’t have to manually kill our node, which is really useful in case you have hundreds of nodes, really common on real robots.

Inside the while block we publish our message. In this example we publish the message Hey!. You can publish any message, like Congratulations YOU_NAME, you have your first ROS Publisher working.

To make sure we publish 3 messages a second we use rate.sleep().

With everything in place, it’s time to run our node. You have to make sure your file is executable. Let’s do that with the following commands:

cd ~/catkin_ws/src/tutorial/src
chmod +x publisher.py

rosrun tutorial publisher.py

In Robot Ignite Academy you already have a roscore running and if you are using the platform, your code should have been executed without any problem. If you are running it on your own computer, you must run roscore in a separated shell (or run it in background with roscore & before actually running your node.

Assuming your node is running, you shouldn’t see any message because we are not printing any, but you can check that your node is running by executing the command below in a separated shell:

rosnode list

That command shows the list of nodes running in your ROS environment. You should see a node called /tutorial, which is the name we gave to our node.

To make sure your publisher is publishing on the correct topic, you can use the command below to list the topics.

rostopic list

You should see a topic called /say_hello because it is what we defined when creating our publisher.

Now, in order to see the messages that are being publishing we just run the command below:

rostopic echo /say_hello

You should see the messages being printed.

That is all for today guys. I hope you have liked the post and the video, if so, please share this post or the video with your friends.

Whether you like the video or not, please leave a comment on the comments section of the video on YouTube.

Keep pushing your ROS Learning.

 

Edit by: Tony Huang and Ruben Alves

Masterclass 2023 batch2 blog banner

Check Out These Related Posts

0 Comments

Submit a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Pin It on Pinterest

Share This