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

Subscribing to topic periodically?

asked 2014-07-17 13:54:20 -0500

2ROS0 gravatar image


I'm a bit new to ROS. I want to subscribe to a topic that is being published continuously. But I only want to read the message once in a while. And I want to read the most recent message at that time.

Is it best to use a pub-sub and get the subscriber node to only obtain the messages at a certain time? Or is the better alternative to define a rosservice?

Thank you.

edit retag flag offensive close merge delete



2 Answers

Sort by ยป oldest newest most voted

answered 2014-07-17 14:45:32 -0500

Airuno2L gravatar image

rosservice is the recommended way of doing this. That link will help understand when to use each.

That being said, sometimes if I'm just trying to hack something together quick and am not worried about overhead, and if a node that I want to get info from is already setup as a topic publisher, I'll just subscribe to the topic. An easy way to do this and only poll it periodically, is to have the subscriber's callback funciton simply copy the data to a member variable if you're using a class (or a global varible, which is messy). That way, elsewhere in your application, you can just poll your member variable when ever you need the info, and it will always have the most recent value.

Hope that helps.

edit flag offensive delete link more


That helps, thank you! I'm implementing your second option for now but long term I'll switch to a service.

2ROS0 gravatar image 2ROS0  ( 2014-07-24 07:56:46 -0500 )edit

If that answers your question, please select this as the answer :)

Airuno2L gravatar image Airuno2L  ( 2014-08-05 09:00:13 -0500 )edit

answered 2014-07-17 19:54:50 -0500

DevonW gravatar image

In your code, add the logic that subscribes to the node when you want to and deletes the subscriber object when you don't want it to. Rinse and repeat for best tasting results

edit flag offensive delete link more



service calls or subscribeOnce are better choices for this.

ahendrix gravatar image ahendrix  ( 2014-07-17 20:06:33 -0500 )edit

Question Tools

1 follower


Asked: 2014-07-17 13:54:20 -0500

Seen: 1,730 times

Last updated: Jul 17 '14