Ask Your Question
0

Subscribe to two topics and performing some computation

asked 2016-11-23 09:51:44 -0600

anadgopi1994 gravatar image

updated 2016-11-24 00:53:05 -0600

gvdhoorn gravatar image

How can i subsribe to two different topics that conatin different message type using a single node.can you give a example

edit retag flag offensive close merge delete

Comments

Please don't use caps. Please edit your title and question and change things to use appropriate capitalisation.

gvdhoorn gravatar imagegvdhoorn ( 2016-11-23 11:43:33 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2016-11-23 19:58:37 -0600

petern3 gravatar image

updated 2016-11-24 03:05:53 -0600

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2016-11-23 09:51:44 -0600

Seen: 327 times

Last updated: Nov 24 '16