0
$\begingroup$

Rosanswers logo

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 :-)


Originally posted by Kilin on ROS Answers with karma: 56 on 2012-12-10

Post score: 0

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

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.


Originally posted by Lorenz with karma: 22731 on 2012-12-10

This answer was ACCEPTED on the original site

Post score: 1

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.