ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
4

Passing multiple arguments to a callback. (C++)

asked 2016-04-18 15:53:32 -0600

Hello,

I am currently in the process of writing a ROS node in C++ that subscribes to an image node, then does some processing from some arguments at the launch of the node that points to a few proprietary libraries.

If I were to write a callback in this format:

void processImagecallback(const sensor_msgs::ImageConstPtr& msg, int argc, char* argv[])

would that work? Or can callbacks only process the topic it is being subscribed to?

I would be calling it from an int main(int argc, char* argv[]) function`.

That should pass it through, right?

I'm rather new to ROS and callbacks.

Thank in advance!

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
10

answered 2016-04-19 01:53:06 -0600

croesmann gravatar image

updated 2016-04-19 02:57:23 -0600

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 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.

edit flag offensive delete link more

Comments

Is there something wrong with the OP's approach?, like not recommended or not standard?

IronFist16 gravatar image IronFist16  ( 2020-06-01 18:40:31 -0600 )edit
2

answered 2016-04-18 18:57:20 -0600

Thomas D gravatar image

I have not seen that done before. You might be better served by looking at roslaunch args and ROS parameters that will let you specify values that you can use to setup your node at runtime or save them in member variables and use them later. However, if what you are asking is to subscribe to multiple topics and go to a single callback function when you have all of the desired topics then you can look at message filters, specifically the ExactTime and ApproximateTime policies.

edit flag offensive delete link more

Question Tools

4 followers

Stats

Asked: 2016-04-18 15:53:32 -0600

Seen: 10,178 times

Last updated: Apr 19 '16