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

问题描述 投票:2回答:2

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

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

c++ ros
2个回答
5
投票

在Gabor Meszaros的回答后编辑。

您可以在here子节中找到ROS C ++ API参考(roscpp)getTopics和 - 在Python中 - 您将找到ros::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;
}

1
投票

我不熟悉ROS API,你正在寻找getTopics吗?

或者你可以检查rostopic list的实现(它是python,但可能与C ++ API重叠)。你可以找到它here

© www.soinside.com 2019 - 2024. All rights reserved.