Ask Your Question

callback function with arguments from subscription to three nodes

asked 2017-08-07 16:59:58 -0500

Blupon gravatar image

Hi !

I'm looking for a way to process a


using two angles provided by another node that provides two topics (the angles as std_msgs::Float32). The thing is, when I read other posts regarding sending several parameters to a callback function (boost::bind) or using one callback function for several subscriptions (this case seems to be only explained for cases where you want to apply a similar function to three different topics independently, and not somehow combine the three topics to build something to publish, which is what I want to do), it never really answers all of my questions.

I'm therefore looking for something like:

pub_ = nh_.advertise<sensor_msgs::LaserScan>("/filtered_scan",100);

sub_ = nh_.subscribe<sensor_msgs::LaserScan>("/scan",100, &SubscribeAndPublish::callbackFilter, this);

sub2_ = nh_.subscribe<std_msgs::Float32>("/angle1",100, &SubscribeAndPublish::callbackFilter, this);

sub3_ = nh_.subscribe<std_msgs::Float32>("/angle2",100, &SubscribeAndPublish::callbackFilter, this);

void callbackFilter(const sensor_msgs::LaserScan& inputScan, const std_msgs::Float32& inputAngle1, const std_msgs::Float32& inputAngle2);

Would someone have an accurate idea (or, my dream, an example code :D) of how to implement that ?

Sorry if I missed the right forum post to solve my problem !

edit retag flag offensive close merge delete


Should the three topics be synchronised (ie: use msg from time t from topic 1, 2 and 3)? If so, look at message_filters.

gvdhoorn gravatar image gvdhoorn  ( 2017-08-08 01:56:11 -0500 )edit

Not necessarily, the angles and the LaserScan variables should just be updated when new data comes in. Doing it right away, thanks.

Blupon gravatar image Blupon  ( 2017-08-08 02:32:47 -0500 )edit

Actually, I could just use one callback method per input attribute, to update the input attributes, and at each of these updates, call another callback method that would update the output attribute (a LaserScan) and publish it ?

Blupon gravatar image Blupon  ( 2017-08-08 04:22:06 -0500 )edit

If it's acceptable that none of the three callbacks can guarantee (or: you run the risk of publishing a msg in cb1 that uses old data from cb2 and cb3) that it has the 'latest' data of the other two then yes, that could work.

gvdhoorn gravatar image gvdhoorn  ( 2017-08-08 05:01:03 -0500 )edit

I'm going to try this. That being said, I'm hesitating between closing this post or waiting to see if another solution is found by me or suggested by someone else :|

Blupon gravatar image Blupon  ( 2017-08-08 06:30:00 -0500 )edit

I would say the canonical way to do any sort of semi-synchronised publishing based on multiple message streams is to use message_filters with a suitable filter setup (ie: time based or something else). That is the least 'weird' and follows standard practices. That needs msgs with Headers though.

gvdhoorn gravatar image gvdhoorn  ( 2017-08-08 06:32:22 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2017-08-08 07:28:08 -0500

Airuno2L gravatar image

Assuming I understand the problem fully, I would make a class that had inputAngle1 and inputAngle2 as member variables, then I would have three separate callbacks as member functions.

The first callback would subscribe to /angle1 and simply update inputAngle1 member variable.

The second callback would do the same but for the second angle.

The third callback would subscribe to /scan and do all the processing and grabbing the class's up to date inputAngle1 and inputAngle2 values.

edit flag offensive delete link more



This is a sampling approach on top of an event based architecture. It works, but it's also very easy to work with stale data, as depending on when the last callback for each subscription was processed the member variable may not have been updated for a (very) long time.

gvdhoorn gravatar image gvdhoorn  ( 2017-08-08 10:16:22 -0500 )edit

Your Answer

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

Add Answer

Question Tools

1 follower


Asked: 2017-08-07 16:59:58 -0500

Seen: 476 times

Last updated: Aug 08 '17