ROS-在代码中获取当前可用主题(不是命令)

lin*_*bot 2 c++ ros

我想在代码中获得当前可用的主题,以便可以相应地设置发布者和订阅者。我知道命令“ rostopic list”会显示此信息,但是我想在程序运行时获取信息。

有没有API可以做到这一点?

ale*_*ind 5

帖子在Gabor Meszaros的回答之后进行了编辑。

您可以在此处找到ROS C ++ API参考(roscpp)并且-与Python中一样-您将getTopicsros :: master小节中找到方法。

这是如何使用它的示例代码:

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;
}
Run Code Online (Sandbox Code Playgroud)