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

Getting list of published topics from within C++ code

asked 2015-04-08 11:22:41 -0500

Jordan9 gravatar image

I'm using the vicon_bridge node, which publishes a topic "vicon/<subject_name>/<segment_name>" for every subject and segment it finds. In this example, consider that it publishes the topics:

vicon/subject1/segment1 vicon/subject2/segment1

Is there a way, from within C++ code, to get a list of all "vicon/*" topics that are being published? Something like 'rostopic list' that can be used from within the code and can search on a topic "subdirectory"?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
5

answered 2015-04-08 11:39:18 -0500

Wolf gravatar image

updated 2015-04-08 11:39:34 -0500

You could

#include <ros/master.h>

and call

  ros::master::V_TopicInfo topic_infos;
  ros::master::getTopics(topic_infos);

This gets you topic infos (topic name and data type as std::strings) of all advertised topics; obviously not just those in vicon/ but you can filter them with boost:regex or e. g. split and check like:

  std::vector<std::string> lv_elems;

  char lc_delim[2];
  lc_delim[0] = '/';
  lc_delim[1] = '\0';

  boost::algorithm::split( lv_elems, topic_infos[0].name, boost::algorithm::is_any_of( lc_delim ) );

  if ( lv_elems[0] == "vicon" )
{
   // topic of topic_infos[0] is really in namespace vicon!!
}
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2015-04-08 11:22:41 -0500

Seen: 3,289 times

Last updated: Apr 08 '15