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

How do I create a ROS topic publisher/subscriber?

asked 2012-07-06 02:16:20 -0500

Nishant gravatar image

Hi all!

I have a sensor from which I read data via a serial communication (port is of type ttyUSB). Now I am thinking of writing two packages, "publisher_node" and "client". What I want is, publisher_node should read data from the sensor continuously, and publish it as a topic (basically just scream out the values). On the other end, in the client package, I have a specific task, during which I want to poll the device for readings.

I think that using a topic rather than a service is much more logical here. Data flow is a unidirectional stream. Its sort of like filling a cup(client) with water from a river(publisher_node). Am I thinking in the right direction?

My System: Ubuntu 11.10 64-bit

ROS version: ROS electric

Here are some of the key questions I have:

1) How do I create a topic for publisher_node? Is there any tutorial online for that? Maybe any step by step procedure..?

2) Once I know how to create a topic, I will do that in publisher_node. Then I will 'rosmake' and 'rosrun publisher_node publisher_node'. After that, I will make sure I have the topic up, by doing 'rostopic list'. All is OK. Now how do I write code/make the client "subscribe" to the topic? Any guidelines here?

3) Also, since this is serial communication, I want a special functionality with the topic. I want the user to have an option of setting the frequency of publishing (i.e how fast the data is screamed out). Is this possible with ROS?

Note: Yes, I am using a serial device, but I have not used rosserial in any way. I am implementing my own serial port by using the termios API in Linux.

Any help is greatly appreciated. Thanks in advance!

edit retag flag offensive close merge delete


Thanks cagatay and Lorenz. The simple publisher/subscriber in the ROS tutorial is what I wanted. I have another question though. It says that spin() keeps on calling all client callback functions till Ctrl+C are pressed or the node is shut down. How can I subscribe at will (and not continuously)?

Nishant gravatar image Nishant  ( 2012-07-06 03:05:06 -0500 )edit

Best if you open a new question for this. I think, it has been asked recently though:

Lorenz gravatar image Lorenz  ( 2012-07-06 03:17:33 -0500 )edit

Yep, I will open a new question for this and include what I understood from the tip you just gave me above. Thanks Lorenz :)

Nishant gravatar image Nishant  ( 2012-07-06 03:29:07 -0500 )edit

Did you find the tutorials hard to locate?

joq gravatar image joq  ( 2012-07-06 03:39:44 -0500 )edit

Hi joq, No, in fact, to be totally true, I had skimmed over them about a month ago. I just thought that creating a topic, and listening to the messages on that topic would be a much more complex task than what was given in the tutorial. Turns out it isn't! :)

Nishant gravatar image Nishant  ( 2012-07-06 04:35:21 -0500 )edit

2 Answers

Sort by » oldest newest most voted

answered 2012-07-06 02:27:46 -0500

Lorenz gravatar image

The ros tutorials are always a good starting point. In particular, there are tutorials on how to create a publisher and a subscriber.

For setting the publish frequency, you have a few choices. If it is sufficient to set the rate once, when your node is started up, you can use a ros parameter that is best set using a launch file.

If you need to change the publish rate at run time, you can either use a service or use dynamic_reconfigure. While the former one should be easier to implement, I think the latter one is more flexible. For instance, you can use a nice GUI to change the parameters which is really handy when you are trying to find a good value.

edit flag offensive delete link more

answered 2012-07-06 02:26:40 -0500

cagatay gravatar image

updated 2012-07-06 02:28:58 -0500


start from here

this is going to help you

here is a serialport implementation in ros & linux , this might help too

edit flag offensive delete link more

Question Tools


Asked: 2012-07-06 02:16:20 -0500

Seen: 8,418 times

Last updated: Jul 06 '12