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

Sending multiple topics to a function

asked 2019-09-30 15:35:57 -0500

petitpoulet gravatar image

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?

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
1

answered 2019-09-30 21:24:57 -0500

Jari gravatar image

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

edit flag offensive delete link more

Comments

1

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

message_filters::Subscriber<nav_msgs::occupancygrid> Sub_CostMap(n, "move_base/local_costmap/costmap", 2); message_filters::Subscriber<geometry_msgs::twist> Sub_vel(n, "cmd_vel", 2); message_filters::TimeSynchronizer<nav_msgs::occupancygrid, geometry_msgs::twist=""> 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<geometry_msgs::twist_<std::allocator<void> >

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

Thanks

petitpoulet gravatar image petitpoulet  ( 2019-10-01 10:16:40 -0500 )edit

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

message_filters::TimeSynchronizer<nav_msgs::occupancygrid, geometry_msgs::twist="">

to

message_filters::TimeSynchronizer<nav_msgs::occupancygrid, geometry_msgs::twist="">

also some of the message names might require PascalCase?

Jari gravatar image Jari  ( 2019-10-08 19:06:00 -0500 )edit

Did this work?

Jari gravatar image Jari  ( 2019-10-15 11:09:49 -0500 )edit

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.

petitpoulet gravatar image petitpoulet  ( 2019-10-15 12:14:31 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2019-09-30 15:35:57 -0500

Seen: 348 times

Last updated: Sep 30 '19