ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange |
1 | initial version |
You can bind multiple dedicated parameters to a callback. The key is boost::bind
The subscriber interface provides an overloaded initialization function accepting a boost::function object. A boost::function object combines the possibility to provide functions and functors.
boost::bind creates a functor that includes all your extra parameters that should be passed to the callback.
You can initiate your subscriber as follows:
ros::Subscriber sub = n.subscribe<sensor_msgs::Image> ("image_topic", 10,
boost::bind(processImagecallback, _1, argc, argv) );
_1
constitutes a placeholder for the original argument that is passed to the callback (the msg-data (image) here).
If you want to pass a reference to boost::bind use boost::ref(x)
or boost::cref(x)
(refer to the boost manual).
There are also other Questions here regarding callback arguments, e.g. here.
PS: Starting from C++11 std::bind is available in the standard library. Currently, ROS does not provide the corresponding C++11 interface, since it relies on C++03 (see REP003). But boost works fine here, just you cannot mix it).
2 | No.2 Revision |
You can bind multiple dedicated parameters to a callback.
The key is boost::bind .
The subscriber interface provides an overloaded initialization function accepting a boost::function object.
A boost::function object combines the possibility to provide accept both functions and functors.
boost::bind creates a functor that includes all your extra parameters that should be passed to the callback.
You can initiate your subscriber as follows:
ros::Subscriber sub = n.subscribe<sensor_msgs::Image> ("image_topic", 10,
boost::bind(processImagecallback, _1, argc, argv) );
_1
constitutes a placeholder for the original argument that is passed to the callback (the msg-data (image) here).
If you want to pass a reference to boost::bind use boost::ref(x)
or boost::cref(x)
(refer to the boost manual).
There are also other Questions here regarding callback arguments, e.g. here.
PS: Starting from C++11 std::bind is available in the standard library. Currently, ROS does not provide the corresponding C++11 interface, since it relies on C++03 (see REP003). But boost works fine here, just you cannot mix it).
3 | No.3 Revision |
You can bind multiple dedicated parameters to a callback. The key is boost::bind.
The subscriber interface provides an overloaded initialization function accepting a boost::function object. A boost::function object combines the possibility to accept both functions and functors.
boost::bind creates a functor that includes all your extra parameters that should be passed to the callback.
You can initiate your subscriber as follows:
ros::Subscriber sub = n.subscribe<sensor_msgs::Image> ("image_topic", 10,
boost::bind(processImagecallback, _1, argc, argv) );
_1
constitutes a placeholder for the original argument that is passed to the callback (the msg-data (image) here).
If you want to pass a reference to boost::bind use boost::ref(x)
or boost::cref(x)
(refer to the boost manual).
boost::bind works also for class methods (please also refer to the boost::bind manual).
There are also other Questions here regarding callback arguments, e.g. here.
PS: Starting from C++11 std::bind is available in the standard library. Currently, ROS does not provide the corresponding C++11 interface, since it relies on C++03 (see REP003). But boost works fine here, just but you cannot mix it).
4 | No.4 Revision |
You can bind multiple dedicated parameters to a callback. The key is boost::bind.
The subscriber interface provides an overloaded initialization function accepting a boost::function object. A boost::function object combines the possibility to accept both functions and functors.
boost::bind creates a functor that includes all your extra parameters that should be passed to the callback.
You can initiate your subscriber as follows:
ros::Subscriber sub = n.subscribe<sensor_msgs::Image> ("image_topic", 10,
boost::bind(processImagecallback, _1, argc, argv) );
_1
constitutes a placeholder for the original argument that is passed to the callback (the msg-data (image) here).
If you want to pass a reference to boost::bind use boost::ref(x)
or boost::cref(x)
(refer to the boost manual).
boost::bind works also for class methods (please also refer to the boost::bind manual).
There are also other Questions here regarding callback arguments, e.g. here.
PS: Starting from C++11 std::bind is available in the standard library. Currently, ROS does not provide the corresponding C++11 interface, since it relies on C++03 (see REP003). But boost works fine here, but you cannot mix it).
PPS: According to Thomas D.'s answer, I also recommend to use ROS-Parameters and launch file arguments instead of relying on the calling arguments of your binary.
5 | No.5 Revision |
You can bind multiple dedicated parameters to a callback. The key is boost::bind.
The subscriber interface provides an overloaded initialization function accepting a boost::function object. A boost::function object combines the possibility to accept both functions and functors.
boost::bind creates a functor that includes all your extra parameters that should be passed to the callback.
You can initiate your subscriber as follows:
ros::Subscriber sub = n.subscribe<sensor_msgs::Image> ("image_topic", 10,
boost::bind(processImagecallback, _1, argc, argv) );
_1
constitutes a placeholder for the original argument that is passed to the callback (the msg-data (image) here).
If you want to pass a reference to boost::bind use boost::ref(x)
or boost::cref(x)
(refer to the boost manual).
boost::bind works also for class methods (please also refer to the boost::bind manual).
Since you want to subscribe to an image publisher, you might take a look at image_transport. You can directly apply boost:bind according to the above example.
There are also other Questions here regarding callback arguments, e.g. here.
PS: Starting from C++11 std::bind is available in the standard library. Currently, ROS does not provide the corresponding C++11 interface, since it relies on C++03 (see REP003). But However, boost works fine here, but you cannot mix it).
PPS: According to Thomas D.'s answer, I also recommend to use ROS-Parameters and launch file arguments instead of relying on the calling arguments of your binary.
6 | No.6 Revision |
You can bind multiple dedicated parameters to a callback. The key is boost::bind.
The subscriber interface provides an overloaded initialization function accepting a boost::function object. A boost::function object combines the possibility to accept both functions and functors.
boost::bind creates a functor that includes all your extra parameters that should be passed to the callback.
You can initiate your subscriber as follows:
ros::Subscriber sub = n.subscribe<sensor_msgs::Image> ("image_topic", 10,
boost::bind(processImagecallback, _1, argc, argv) );
_1
constitutes a placeholder for the original argument that is passed to the callback (the msg-data (image) here).
If you want to pass a reference to boost::bind use boost::ref(x)
or boost::cref(x)
(refer to the boost manual).
boost::bind works also for class methods (please also refer to the boost::bind manual).
Since you want to subscribe to an image publisher, topic, you might take a look at image_transport. You can directly apply boost:bind according to the above example.
There are also other Questions here regarding callback arguments, e.g. here.
PS: Starting from C++11 std::bind is available in the standard library. Currently, ROS does not provide the corresponding C++11 interface, since it relies on C++03 (see REP003). However, boost works fine here, but you cannot mix it).
PPS: According to Thomas D.'s answer, I also recommend to use ROS-Parameters and launch file arguments instead of relying on the calling arguments of your binary.