0
$\begingroup$

Rosanswers logo

Hi there. I'm trying to add external data in the callback function of subscriber. I have read somethings about that and the option is to use "boost::bind". I have developed this code but I get an error. Somebody can help me?

THIS IS THE CALLBACK FUNCTION:

void chatterCallback(const geometry_msgs::PoseStamped::ConstPtr& msg, int MY_NUMBER)
{    
        ROS_INFO_STREAM("Received pose: " << msg->pose.position.x);
        cout<<"The number is= "<< MY_NUMBER <<endl;
}

AND THIS IS THE CALL:

 int i=5;                                                                                               
  ros::Subscriber sub = node.subscribe("/vrpn_client_node/irobot/pose",  1000, boost::bind(chatterCallback, _1, i)); 

P.S. I know that I can create a global variable of MY_NUMBER and it would works good. The variable: "MY_NUMBER" is just to give a trivial example. Thanks


Originally posted by Hector on ROS Answers with karma: 1 on 2016-12-21

Post score: 0

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

Hi Hector,

It helps if you would also include the actual error message that you got (was it during compilation, runtime? etc).

Anyhow, most of the code looks correct, but it seems your subscriber is not templated to the right type. If you change

ros::Subscriber sub = node.subscribe("/vrpn_client_node/irobot/pose",  1000, boost::bind(chatterCallback, _1, i));

to

ros::Subscriber sub = node.subscribe<geometry_msgs::PoseStamped>("/vrpn_client_node/irobot/pose",  1000, boost::bind(chatterCallback, _1, i));

That should be fixed. For more info you can have a look at these related questions:

how to deliver arguments to a callback function

how to make callback function called by several subscriber


Originally posted by rbbg with karma: 1823 on 2016-12-22

This answer was ACCEPTED on the original site

Post score: 0

$\endgroup$

Your Answer

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