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

How to deliver arguments to a callback function?

asked 2011-11-22 02:24:08 -0600

Felix Tr gravatar image

updated 2011-11-22 08:50:07 -0600

tfoote gravatar image

Hello All,

I am relatively new user of ROS, and would be grateful if you could answer to my question. I work with Linux and C, and I want to deliver arguments to my callback function. Lets say I want to deliver to it the next struct as a parameter:

typedef struct imageManagmentStruct
{
    IplImage*  pSrcIplImage;
    IplImage*  pDstIplImage;
    GdkPixbuf* pGtkPixbuf;
    GtkWidget* pGtkImgWindow;    
    int        previousImageSize;
    int        left_attach;
    int        right_attach;
    int        top_attach;
    int        bottom_attach;    
} ImageManagmentStruct;

My callback is:

void imageCallback ( const sensor_msgs::ImageConstPtr& msg,
                     ImageManagmentStruct* pToImageManagmentStruct
                   )
{
    // My code.
}

The call to the callback from the main done like this:

ros::Subscriber subLeft = nh.subscribe ("/bb2/left/image_raw", 10, imageCallback,  
                                        pointerToImageManagmentStruct);

And this is not compiling...

Thank you all, Felix.

edit retag flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted
14

answered 2011-11-22 02:52:32 -0600

dornhege gravatar image

updated 2012-01-17 03:36:27 -0600

I'm not sure, if it can work like this. The extra parameter is usually for callbacks that are member functions.

What you want is to use boost::bind.

Your subscriber call should look like this:

ros::Subscriber subLeft = nh.subscribe<sensor_msgs::Image> ("/bb2/left/image_raw", 10,
    boost::bind(imageCallback, _1, pointerToImageManagmentStruct) );

(untested, but the call should be similar to this)

edit flag offensive delete link more

Comments

1
Hi,I wanted to say thank you for your answer. Just for others that will read this posts I want to be a little more precise. The exact line for my case is: ros::Subscriber subLeft = nh.subscribe<sensor_msgs::Image> ("/bb2/left/image_raw", 10, boost::bind(imageCallback, _1, pointerToImageManagmentStruct ); While the call for imageCallback is exactly the same as I have written in the original post.
Felix Tr gravatar image Felix Tr  ( 2011-11-22 21:48:54 -0600 )edit
3

http://answers.ros.org/question/63991... shows the code for a class method callback

lucasw gravatar image lucasw  ( 2016-01-24 07:44:48 -0600 )edit
0

answered 2012-01-17 02:38:32 -0600

Felix Tr gravatar image

Hello all again,

Now I want to deliver more than one parameter to the call-back. I have tried to do it in a straight-forward way:

ros::Subscriber subLeft = nh.subscribe<sensor_msgs::Image> ("/bb2/left/image_raw", 10, boost::bind(imageCallback, _2, pointerToStruct1, pointerToStruct2 );

But this doesn´t work. There is of course a way to "pack" all my parameters into a "big" struct, but this creates to many "pointers to pointers" structures... Is there a clean way to deliver more than one parameter to a call-back function?

Thank you all, Felix.

edit flag offensive delete link more

Comments

1
The second parameter to boost::bind() should continue to be _1; it specifies which parameter from the lambda gets passed into that position in your callback.
ahendrix gravatar image ahendrix  ( 2012-01-17 03:20:58 -0600 )edit
Thanks a lot, it works :)
Felix Tr gravatar image Felix Tr  ( 2012-01-17 04:45:13 -0600 )edit
1

@ahendrix what do you mean by lambda?

surabhi96 gravatar image surabhi96  ( 2018-09-23 21:57:00 -0600 )edit

Question Tools

3 followers

Stats

Asked: 2011-11-22 02:24:08 -0600

Seen: 20,584 times

Last updated: Jan 17 '12