Ask Your Question
0

Understanding registerCallback() from TF MessageFilter tutorial

asked 2018-12-29 03:38:04 -0500

jwin gravatar image

I'm going through the TF MessageFilter tutorial and I understand the code except for line 13:

tf_filter_->registerCallback( boost::bind(&PoseDrawer::msgCallback, this, _1) );

I'm more of a novice when it comes to using the boost libraries, but after reading some documentation, it seems that this line is creating a function object that equates to something of this form: msgCallback(this, _1) where this is the PoseDrawer object and _1 is a placeholder that will be substituted with the first argument provided to the function object (which I'm assuming is done by the TF library under the hood).

What I'm confused about is how you know to provide the this object and that the callback function needs an _1 placeholder. Is the _1 simply because the callback function (shown below) only takes one argument? If my callback function had no parameters, would I just remove the _1 argument when calling the function? Why is the this object necessary?

void msgCallback(const boost::shared_ptr<const geometry_msgs::PointStamped>& point_ptr)
edit retag flag offensive close merge delete

Comments

These are all C++ questions and not directly related to ROS. I would however recommend reading up on C++ Lamba functions to understand exactly what they do. This tutorial is a good start.

PeteBlackerThe3rd gravatar image PeteBlackerThe3rd  ( 2018-12-29 08:03:48 -0500 )edit

Ok sorry about that. Do you think I should post this somewhere else? I posted here because this was code directly given in the ROS TF MessageFilters tutorial and thought it might help someone else going through the tutorials.

jwin gravatar image jwin  ( 2018-12-29 11:26:50 -0500 )edit

Also, I looked at the Lambda functions tutorial you linked. Could you specify how my question is related? I don't think the code above is using Lambda functions. I think the registerCallback line is just calling a function within a function.

jwin gravatar image jwin  ( 2018-12-29 11:28:46 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2019-04-23 05:52:09 -0500

drNoob13 gravatar image

You first need to understand the boost binding function and how use it to bind function object.

tf_filter_->registerCallback( boost::bind(&PoseDrawer::msgCallback, this, _1) );

Simply put, it binds the call back function with the function whose object being created pointed by a this pointer. Do a review on boost::bind to understand the syntax and usage. PoseDrawer is the class that has a member function msgCallback. _1 means the default argument passing when the rospin() is activated up on the scope of the current subscriber. For the tf::MessageFilters tutorial, it is the turtle_point_stamped topic.

In summary, registerCallback is a way to bind the object function msgCallback of the current object of class PoseDrawer to the default handle of topic turtle_point_stamped.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

2 followers

Stats

Asked: 2018-12-29 03:38:04 -0500

Seen: 699 times

Last updated: Apr 23 '19