简体   繁体   English

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

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

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. 我知道命令“ rostopic list”会显示此信息,但是我想在程序运行时获取信息。

Is there any API could do this? 有没有API可以做到这一点?

Post edited after Gabor Meszaros's answer. 帖子在Gabor Meszaros的回答之后进行了编辑。

You find ROS C++ API reference (roscpp) here and - as in Python - you'll find getTopics method in ros::master subsection. 您可以在此处找到ROS C ++ API参考(roscpp) 并且-如在Python中一样-您将在ros :: master小节中找到getTopics方法。

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;
}

I am not familiar with ROS API, is getTopics that you are looking for? 我不熟悉ROS API,您正在寻找getTopics吗?

Alternatively you can check the implementation of the rostopic list (it is python, but maybe there are overlap with the C++ API). 或者,您可以检查rostopic list的实现(它是python,但可能与C ++ API有重叠)。 You can find it here . 你可以在这里找到它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM