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

Add external data to callback function of subscriber

asked 2016-12-21 19:12:32 -0500

Hector gravatar image

updated 2016-12-21 22:00:32 -0500

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

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2016-12-22 00:09:38 -0500

rbbg gravatar image

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

edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2016-12-21 19:12:32 -0500

Seen: 257 times

Last updated: Dec 22 '16