0
$\begingroup$

Rosanswers logo

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.


Originally posted by Felix Tr on ROS Answers with karma: 378 on 2011-11-22

Post score: 7

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

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)


Originally posted by dornhege with karma: 31395 on 2011-11-22

This answer was ACCEPTED on the original site

Post score: 14


Original comments

Comment by Felix Tr on 2011-11-22:
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.

Comment by lucasw on 2016-01-24:
http://answers.ros.org/question/63991/how-to-make-callback-function-called-by-several-subscriber/?answer=63998#post-id-63998 shows the code for a class method callback

$\endgroup$

Your Answer

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