ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | Q&A
Ask Your Question

Get sporadic messages from periodic publisher

asked 2012-12-10 04:17:43 -0600

Kilin gravatar image

Hi everyone,

I've just started working with ros, this is my first post here :-)

I'd like to ask a question: I have a publisher that publishes a kinect stream at a certain rate.

Until now I've always needed to process every snapshot, so I just subscribed to the topic and process each message in my callback.

I was thinking that sometimes it could be useful for a client to get single messages, sporadically, from a periodic publisher. Is there an easy way to do that?

I could skip messages in my callback until I want to grab one, but I'd like to avoid the overhead of the calls I don't need.

I guess I could play with subscribe/ubsubscribe on demand, but I imagine it would be very slow.

The better way that I was able to think is to modify the publisher, adding a service that, when called, returns just the last message. This way it could be possible to have a periodic publisher that could also be used as a "sporadic" publisher. Is this the best solution, or there's something simpler, that doesn't need to modify the publisher?

To simplify, I want to use the same publisher as sporadic for some clients, as periodic for some others.

Thank you :-)

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2012-12-10 04:54:13 -0600

Lorenz gravatar image

updated 2012-12-10 04:55:15 -0600

I think the easiest solution here really is to let the callback always execute and only use the value if required. Callbacks are not too expensive and I would consider trying to avoid them without facing actual issues premature optimization.

roscpp provides the function ros::waitForMessage to just get a single message from a topic. But this method might first subscribe, then wait, and finally unsubscribe from the topic which is much more expensive than executing a normal callback multiple times given the size of the received message is moderate.

Your proposed solution with a service sounds ok, too. If you can modify your publisher and if you really run into performance problems, I think it should be your preferred solution.

edit flag offensive delete link more

answered 2012-12-12 06:44:16 -0600

Kilin gravatar image

Thank you very much Lorenz! I'll start with the "skipping callback" option, and maybe try with the service for a more elegant solution, if needed :-)

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2012-12-10 04:17:43 -0600

Seen: 360 times

Last updated: Dec 12 '12