Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ROS - get current available topic in code (not command)

Tags:

c++

ros

I want to get current available topics in my code so that I can set up publisher and subscribber accordingly. I know command 'rostopic list' would show this, but I want to get the information when my program is running.

Is there any API could do this?

like image 667
linzhang.robot Avatar asked Sep 02 '25 13:09

linzhang.robot


1 Answers

Post edited after Gabor Meszaros's answer.

You find ROS C++ API reference (roscpp) here and - as in Python - you'll find getTopics method in ros::master subsection.

Here is a sample code of how to use it:

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

for (ros::master::V_TopicInfo::iterator it = master_topics.begin() ; it != master_topics.end(); it++) {
  const ros::master::TopicInfo& info = *it;
  std::cout << "topic_" << it - master_topics.begin() << ": " << info.name << std::endl;
}
like image 50
alextoind Avatar answered Sep 05 '25 16:09

alextoind