Calling spinOnce inside a service callback that publishes a message
I have a roscpp service callback that, upon request, does a few things and then publishes a message on a topic. Here is an oversimplified example:
void someServiceCb(std_srvs::Trigger::Request& req,
std_srvs::Trigger::Response& resp) {
if (something)
{
std_msgs::Empty msg;
pub.publish(msg);
resp.success = true;
ros::spinOnce(); // Do I need this? Can it cause problems?
doSomeOtherStuff();
}
else
{
resp.success = false;
}
}
The intent is to get the message out as soon as possible.
- Do I need that
ros::spinOnce()
in order to get the message out as quickly as possible? - Can calling
ros::spinOnce()
inside a service callback cause problems?
Asked by spmaniato on 2017-01-14 11:08:24 UTC
Answers
You don't need a call back to publish a message. It will publish at "pub.publish(msg);". As to calling ros::spin from a callabck, I have no idea what happens. I've never used in that fashion. Call backs are typically used from the main() to enter a callback when new data arrives on a subscribed topic.
Asked by billy on 2017-01-14 14:26:11 UTC
Comments
Thanks @billy. I've been doing some tests and comparing against the docs. It looks like on certain occasions calling ros::spinOnce()
might be a problem (as it takes us back to the callback queue). Will try to formulate a full answer later today.
Asked by spmaniato on 2017-01-14 15:14:23 UTC
Yep; calling publish
puts the message in a transmit queue, where it's picked up and sent by a background thread that isn't tied to spinOnce
.
Asked by ahendrix on 2017-01-14 23:01:33 UTC
spinOnce
does process message and service callbacks, so if there's another pending service request it can cause your service callback to be called a second time, from within the spinOnce
in the first service callback.
Asked by ahendrix on 2017-01-14 23:02:14 UTC
Comments
Reading http://wiki.ros.org/roscpp/Overview more carefully, I think the answer is that
ros::spinOnce()
does not help with "getting the message out more quickly". I would still like to know if it can cause problems though :-)Asked by spmaniato on 2017-01-14 14:24:24 UTC