Ask Your Question
1

Sending multiple topics to a function

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

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 -0600

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 imagepetitpoulet ( 2019-10-01 10:16:40 -0600 )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 imageJari ( 2019-10-08 19:06:00 -0600 )edit

Did this work?

Jari gravatar imageJari ( 2019-10-15 11:09:49 -0600 )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 imagepetitpoulet ( 2019-10-15 12:14:31 -0600 )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

Stats

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

Seen: 34 times

Last updated: Sep 30