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

Revision history [back]

If both topics publish the same message type, you can definitely use the same callback for both subscriptions, although you won't be able to tell which message came from which topic.

Otherwise, you could always delegate the work to a third function, like this:

void processInt (int a)
{
  // Lots of computation here
}

void callback1 (MyMessageConstPtr msg)
{
  processInt (msg->int_value);
}

void callback2 (MyOtherMessageConstPtr msg)
{
  processInt (msg->other_int_value);
}

int main (int argc, char** argv)
{
  ros::init (argc, argv, "my_node");
  ros::NodeHandle node;
  ros::Subscriber s1 = node.subscribe<MyMessage>("topic1", 1, callback1);
  ros::Subscriber s2 = node.subscribe<MyOtherMessage>("topic2", 1, callback2);
  ros::spin();
  return 0;
}