0
$\begingroup$

Rosanswers logo

I can't figure out how to correctly use boost::bind while sending a class function as a callback Function. I want to callBack function to have on class I made as an argument as well.

Do far my code is like this :

    Scribe(ros::NodeHandle ao_nh, std::string topic, Platform& p) : _verbose(false), _topic_name(topic), _timeStamp(ros::Time::now()), _cmd(), _needChecking(false) {
    //Scribe mode
    _scribe=ao_nh.subscribe<geometry_msgs::Twist>( topic,1000, boost::bind(&Scribe::callBackFunc, _1, &p), this);
    //Timer
    _bomb=ao_nh.createTimer(ros::Duration(2),&Scribe::bombCallBack, this);
};

and the call back function is like this :

void callBackFunc(const geometry_msgs::Twist::ConstPtr& msg, Platform* plat);

Or, I have a error such that no matching function to call and I can't figure out how to correctly declare the subscriber.

Any help is appreciated.


Originally posted by Maya on ROS Answers with karma: 1172 on 2014-03-22

Post score: 0

$\endgroup$

2 Answers 2

0
$\begingroup$

Rosanswers logo

Generally:

boost::bind(&ClassName::classMethod, &class_instance, arg1, arg2, ...);

So in this case, since const geometry_msgs::Twist::ConstPtr& msg is passed in as _1:

boost::bind(&Scribe::callBackFunc, this, _1, &p);


Originally posted by paulbovbel with karma: 4518 on 2014-03-22

This answer was ACCEPTED on the original site

Post score: 4


Original comments

Comment by Maya on 2014-03-22:
Ok thanks, it worked !

$\endgroup$
0
$\begingroup$

Rosanswers logo

subscribe has direct support for class members as callbacks. So I suggest making callBackFunc a method of class Platform and following the instructions in the tutorial: http://wiki.ros.org/roscpp_tutorials/Tutorials/UsingClassMethodsAsCallbacks

Edit: Ooops... didn't read properly, your callback is alreay member of class Scribe. Check out the answer by @paulbovbel and make sure to remove the last this paramter in the subscribe call, because you already supply it in via bind.


Originally posted by demmeln with karma: 4306 on 2014-03-22

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.