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

Revision history [back]

click to hide/show revision 1
initial version

In the publish/subscribe example in the ROS tutorials, it shows about the simplest a subscriber can get (example).

There are two things required for a subscription, that is the callback function, and the subscriber handle. You can simply duplicate these as many times as you need. A modified example could be:

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


void fooCallback(const std_msgs::String::ConstPtr& msg)
{
  // process the string
}

void barCallback(const std_msgs::Int16 msg)
{
  // process the int
}

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

  ros::Subscriber sub1 = n.subscribe("foo", 1000, fooCallback);
  ros::Subscriber sub2 = n.subscribe("bar", 1000, barCallback);

  ros::spin();

  return 0;
}

I haven't tested this, so you probably can't just copy and run it.

In the publish/subscribe example in the ROS tutorials, it shows about the simplest a subscriber can get (example).

There are two things required for a subscription, that is the callback function, and the subscriber handle. You can simply duplicate these as many times as you need. A modified example could be:

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


void fooCallback(const std_msgs::String::ConstPtr& msg)
{
  // process the string
}

void barCallback(const std_msgs::Int16 msg)
{
  // process the int
}

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

  ros::Subscriber sub1 = n.subscribe("foo", 1000, fooCallback);
  ros::Subscriber sub2 = n.subscribe("bar", 1000, barCallback);

  ros::spin();

  return 0;
}

I haven't tested this, so you probably can't just copy and run it.

In the publish/subscribe example in the ROS tutorials, it shows about the simplest a subscriber can get (example).

There are two things required for a subscription, that is the callback function, and the subscriber handle. You can simply duplicate these as many times as you need. A modified example could be:

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


void fooCallback(const std_msgs::String::ConstPtr& msg)
{
  // process the string
}

void barCallback(const std_msgs::Int16 msg)
{
  // process the int
}

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

  ros::Subscriber sub1 = n.subscribe("foo", 1000, fooCallback);
  ros::Subscriber sub2 = n.subscribe("bar", 1000, barCallback);

  ros::spin();

  return 0;
}

I haven't tested this, so you probably can't just copy and run it.