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

Subscribing to multiple topics using one (meta)callback

asked 2019-05-28 07:16:13 -0500

CharlieMAC gravatar image

Hello Guys,

I have 13 different topics of the same class (control_msgs::JointControllerState) that I would like to subscribe to using ROSCPP. If I follow the standard procedure, I would need to define 13 different callbacks, which will be pretty similar to each other. This would make my code to be more extensive and harder to maintain. Is it possible to create one common callback (like a meta callback) for all these topics? I do not need any sort of sync.

I have been thinking of creating an array of subscribers and callbacks. Using typedef I created a array of pointers to functions (the callbacks), but the problem comes when I need to assign a specific topic to each of the callbacks.

Any suggestions? Thanks in advance,

Charlie

edit retag flag offensive close merge delete

Comments

1

You can achieve exactly what you want using lambda functions. I'm a bit rusty on the syntax, but you need to write a normal callback function but with an additional parameter, say string topic_name or int id. Then you use a pass a lambda to each subscribe call which passes the appropriate additional parameter.

That's the gist of it.

PeteBlackerThe3rd gravatar image PeteBlackerThe3rd  ( 2019-05-28 08:49:46 -0500 )edit

This answer here gives you some examples : https://answers.ros.org/question/3083...

PeteBlackerThe3rd gravatar image PeteBlackerThe3rd  ( 2019-05-28 08:52:38 -0500 )edit
1

@PeteBlackerThe3rd: that Q&A only applies to ROS 2.

gvdhoorn gravatar image gvdhoorn  ( 2019-05-28 09:00:00 -0500 )edit

That's true, I'll put together a ROS 1 example quickly

PeteBlackerThe3rd gravatar image PeteBlackerThe3rd  ( 2019-05-28 09:23:22 -0500 )edit

@PeteBlackerThe3rd, is there some thing similar to that for python ROS1?

lxg gravatar image lxg  ( 2020-10-15 07:55:54 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2019-05-28 09:22:55 -0500

I've just modified the chatter C++ example code here so that it subscribes to two different topics using a single callback, as I was trying to describe above.

#include "ros/ros.h"
#include "std_msgs/String.h"

void chatterCallback(const std_msgs::String::ConstPtr& msg, std::string topicName)
{
  ROS_INFO("I heard: [%s] on topic \"%s\"", msg->data.c_str(), topicName.c_str());
}

int main(int argc, char **argv)
{
  ros::init(argc, argv, "listener");

  ros::NodeHandle n;

  ros::Subscriber sub1 = n.subscribe<std_msgs::String>("chatter", 1, boost::bind(&chatterCallback, _1, "chatter"));
  ros::Subscriber sub2 = n.subscribe<std_msgs::String>("other_chatter", 1, boost::bind(&chatterCallback, _1, "other_chatter"));

  ros::spin();

  return 0;
}

Hopefully this will give you a simple ROS 1 example you can adapt to your needs.

edit flag offensive delete link more

Comments

If this is just about the topic name I would suggest to use a MessageEvent.

gvdhoorn gravatar image gvdhoorn  ( 2019-05-28 09:28:54 -0500 )edit

@PeteBlackerThe3rd, how could I do this if my callback is class method?

CharlieMAC gravatar image CharlieMAC  ( 2019-05-29 02:05:13 -0500 )edit

@CharlieMAC: this topic has come up multiple times on ROS Answers. If you search for something like subscriber extra argument site:answers.ros.org (with Google) it should return quite a few posts that answer your question.

gvdhoorn gravatar image gvdhoorn  ( 2019-05-29 02:11:33 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2019-05-28 07:16:13 -0500

Seen: 2,429 times

Last updated: May 28 '19