Robotics StackExchange | Archived questions

Sending multiple topics to a function

I'm trying to send multiple topics to a function:

 void CostMapCallBack(const nav_msgs::OccupancyGrid &msg, const geometry_msgs::Twist &msg_speed)

To send the values of the topics, I created 2 subscribers:

 Sub_CostMap = n.subscribe("move_base/local_costmap/costmap", 2, &CostMapCallBack);
 Sub_vel = n.subscribe("cmd_vel", 2, &CostMapCallBack);

However, when I compile, I get this error:

 no matching function for call to ‘ros::NodeHandle::subscribe(const char [8], int, void (*)(const OccupancyGrid&, const Twist&))’

Is there a way to send multiple topics to a function?

Asked by petitpoulet on 2019-09-30 15:35:57 UTC

Comments

Answers

I think you're looking for message filters. Basically you create two subscribers and then register them with a time synchronizer and that way you can supply a callback that will expect both messages as arguments

You can read about this here as well as see examples http://wiki.ros.org/message_filters

Asked by Jari on 2019-09-30 21:24:57 UTC

Comments

I read about message_filters like you recommended, and change my structure to:

message_filters::Subscriber Sub_CostMap(n, "move_base/local_costmap/costmap", 2); message_filters::Subscriber Sub_vel(n, "cmd_vel", 2); message_filters::TimeSynchronizer sync(Sub_CostMap, Sub_vel, 2); sync.registerCallback(&CostMapCallBack);

However, I still get an error:

‘value’ is not a member of ‘ros::message_traits::TimeStamp >

I included the libraries, so I don't really know why it doesn't like it. Do you have an idea?

Thanks

Asked by petitpoulet on 2019-10-01 10:16:40 UTC

You seem to have an added ="" in your template arguments. Change

message_filters::TimeSynchronizer

to

message_filters::TimeSynchronizer

also some of the message names might require PascalCase?

Asked by Jari on 2019-10-08 19:06:00 UTC

Did this work?

Asked by Jari on 2019-10-15 11:09:49 UTC

I changed my approach. I created a global variable (which I don't find is a good way...) and it works. I will probably try your suggestion in the future, so I can't tell right now if it works. However, thanks again and I'll give you feedback.

Asked by petitpoulet on 2019-10-15 12:14:31 UTC