0
$\begingroup$

Rosanswers logo

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!


Originally posted by atomoclast on ROS Answers with karma: 151 on 2016-04-18

Post score: 4

$\endgroup$

2 Answers 2

0
$\begingroup$

Rosanswers logo

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.


Originally posted by croesmann with karma: 2531 on 2016-04-19

This answer was ACCEPTED on the original site

Post score: 10


Original comments

Comment by IronFist16 on 2020-06-01:
Is there something wrong with the OP's approach?, like not recommended or not standard?

$\endgroup$
0
$\begingroup$

Rosanswers logo

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.


Originally posted by Thomas D with karma: 4347 on 2016-04-18

This answer was NOT ACCEPTED on the original site

Post score: 2

$\endgroup$

Your Answer

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