[ros-users] question about callback functions
Josh Faust
jfaust at willowgarage.com
Thu Jun 3 23:31:37 UTC 2010
On Thu, Jun 3, 2010 at 4:04 PM, Patrick Bouffard <bouffard at eecs.berkeley.edu
> wrote:
> @Josh: The OP hasn't responded, but I would like to hear what you'd
> have to say if he said "C++".
>
You can use boost::bind (or std::bind if your compiler supports that part of
C++11 yet):
void foo(const std_msgs::StringConstPtr& msg, uint32_t custom_value)
{
}
ros::Subscriber sub = nh.subscribe<std_msgs::String>("my_topic", 1,
boost::bind(foo, _1, 5));
Or if it were a class method:
MyClass c;
ros::Subscriber sub = nh.subscribe<std_msgs::String>("my_topic", 1,
boost::bind(&MyClass::foo, &c, _1, 5));
Note that you need to explicitly specify the template parameter here because
it cannot be deduced from the value returned by bind().
> I'm also interested in how to properly handle the possibility of
> multiple subscriber callbacks within the same C++ node. Are these
> handled in separate threads? Do I need then to implement locks and
> such?
>
> Or, as http://www.ros.org/wiki/roscpp/Overview/Callbacks%20and%20Spinning
> seems to suggest, if I am just using ros::spin(), is each subscriber's
> callback called sequentially, based on the order the messages were
> received?
>
The threading model is entirely up to you. In the default case (just
calling ros::spin()), this means single-threaded sequential callbacks. If
you want threaded callbacks you use something like the AsyncSpinner class.
The "callback queue" construct (also explained on that page) means you can
assign different callback queues to different callbacks if you want, so you
can essentially do whatever makes the most sense for your situation.
Note that on a single subscription you're guaranteed to get all callbacks
sequentially -- there's an option to turn this off that will be in C-turtle.
Josh
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ros.org/pipermail/ros-users/attachments/20100603/24cbe7a5/attachment-0003.html>
More information about the ros-users
mailing list