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

Revision history [back]

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.