ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

What are publishers and subscribers?

asked 2014-07-07 07:05:18 -0500

manaXmizery gravatar image

Hi , can anyone explain the basic concepts of publishing and subscribing to me in ROS? I am still a beginner hence , I have limited knowledge in it. Thanks in advance

edit retag flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted

answered 2014-07-07 07:41:09 -0500

McMurdo gravatar image

updated 2014-07-07 07:59:03 -0500

There is a simple way to look at it. (People who don't know the basic Operating Systems concepts - like me - tend to find it utterly difficult to understand ROS. Tutorials, I feel, should not assume computer science knowledge).

(1). Whatsapp = ROS.

(2). Your contacts list = ROS env variables in the .bashrc file...

Example: You are registered on whatsapp with your phone number. If you have a friend's contact, and if he happens to be on whatsapp you can see him.

Analogy: The computer itself is 'registered' by using ROS_HOSTNAME. This is usually set to the ip of the computer whose bashrc you are looking at. If you have the ROS_MASTER_URI set, and if the computer with that ip is running a ROS master you can see it.

(3). Whatsapp groups = ROS TOPIC This analogy is crude. But, I hope it helps.

If someone adds you to a group, you start receiving messages from them (whenever a participant posts a message). Each contact in whatsapp is analogous to a node in ROS. So, if a node registers a subscription to a particular topic, it receives data from the topic (whenever a message is posted on a topic). Note here that no-one needs to add that node to the topic. It can register a subscription, if it has the data-type of the data that is being posted on the topic (this is not true in whatsapp. The creator of a group adds participants to it.). Similarly a node can also publish on that topic (simply send a data structure to it).

What happens when you send a message to a ROS topic? We usually do,

ros::Publisher my_pub = nh.advertise<my_pkg::MyDataType> ("/topic_name", 1) // register a publisher.

mypkg::MyDataType garbage_data; // Create the variable. Later you will set the variables.

my_pub.publish(garbage_data); // Send the data to the topic.

refer to the tutorials (writing a simple publisher/subscriber)) for what this means.

IMPORTANT: The publisher dictates the type of a topic.

Then, ROS will serialize the data into bytes and send it to all subscribers (including a 'rostopic echo' - it is after-all a subscriber). But, on the receiving end, it will be assembled back together (de-serialzed). This means on the receiving end we need to specify the data-type properly. This is done in the definition of the callback function (c++).

void callback(const my_pkg::MyDataTypeConstPtr _msg)

What is this ConstPtr? It simply expands to:

boost::shared_ptr <const my_pkg::MyDataType>

Why is it const? We don't want to modify it even accidentally. Assume that _msg is of type MyDataType* (only that it is not a normal pointer: it is a smart pointer) and to de-reference you can simply do _msg-> or *_msg

Another point that starters take time to understand is that one node can subscribe and publish to the same topic (just like whatsapp groups). And one node can subscribe to one topic and publish to another at the same time (One group is muted and the other is not ... (more)

edit flag offensive delete link more



Gosh , thank you so much for the detailed explanation ! I now have a better idea of how a topic works and although i have not covered everything , i'll definitely get a head start from here. Thank you once again and have a good day !

manaXmizery gravatar image manaXmizery  ( 2014-07-07 08:19:24 -0500 )edit

You can accept the answer if it has answered your question by clicking the green tick mark ;-) Have a fine day ahead!

McMurdo gravatar image McMurdo  ( 2014-07-07 09:51:04 -0500 )edit

answered 2014-07-07 07:19:36 -0500

dornhege gravatar image

Please read the Tutorials and the Introduction to ROS for those explanations. Feel free to ask questions about those documents if something specific is unclear.

edit flag offensive delete link more

answered 2014-07-21 11:51:24 -0500

Andromeda gravatar image

updated 2014-07-21 11:53:05 -0500

Hi I m new here too.

So...if I understood (I did all the first Tutorials on the ROS Webpage) one should take ROS as a OS, which masters different nodes. So please let me understand better, the idea is: I create one node for one sensor, one node for the controller PID and one node for my motor DC. lets say I start a topic and sensor's node is going to publish on it. This is going later to be subscribed by the controller which makes some calculation and opens a new topic for the motor. The motor reads the right values (for istance: speed) subscribing to the second topic, which has the values coming form the controller.

I m right? Or I am completely wrong about the way of working of ROS?

I takes time but it is really hard to understand, which is the correlation between ROS (form the tutorials) and a real robot/application


edit flag offensive delete link more


Your understanding is correct. But, ROS is much more than what you have stated. For example, the example you have stated is so simple. All three things can be performed on a single Node in different threads. Keep going. All the best!

McMurdo gravatar image McMurdo  ( 2014-07-21 12:14:34 -0500 )edit

Ahahah, ok but one should start with simple things, or not? Of course can ROS more than what I wrote above, but as a concept should be right. Anyway...I find your statement very interesting. Could you pleas tell me, how (conceptually) do manage all those thing from one node. I mean... let say node Main collect data from the sensor, calculate the PID and rcontrol the motor, why should then publish those information on three different topic? Why topic at all?!?!?

Andromeda gravatar image Andromeda  ( 2014-07-21 12:55:09 -0500 )edit

Suppose I wrote a node for all the stuff mentioned above..... Suppose you want to only invoke my methods when a particular condition is satisfied. Lets say, you are using an intelligent network of sensors and calculating when the user is sad. At that instant you want to simply trigger a robot to ...

McMurdo gravatar image McMurdo  ( 2014-07-21 14:31:31 -0500 )edit

...trigger a robot to go near him. Of course, the sensing, controlling and other code has been written by me and wrapped into a ros package. Then, you have access only to the binary.. you don't have anything more. To trigger the "robot goto x y theta" you will need to make use of topics.

McMurdo gravatar image McMurdo  ( 2014-07-21 14:33:06 -0500 )edit

Another example: I want to process point clouds. But, if I transport the messages and process it on another machine, there is a huge transport lag. But, instead I can write a nodelet (which is an object that will be dynamically loaded into it's own thread) and attach it to the camera_nodelet_manager

McMurdo gravatar image McMurdo  ( 2014-07-21 14:34:52 -0500 )edit

Theoritically, the stuff still happens on the same node - in different threads. And using nodelets and subscribing to the topic /camera/depth/points means that there is a zero copy pointer pass (much like a function with pointer arguments), though I am still using topics.

McMurdo gravatar image McMurdo  ( 2014-07-21 14:36:03 -0500 )edit

Question Tools

1 follower


Asked: 2014-07-07 07:05:18 -0500

Seen: 2,377 times

Last updated: Jul 21 '14