我想在我的代码中获取当前可用的主题,以便我可以相应地设置发布者和订阅者。我知道命令'rostopic list'会显示这一点,但我想在程序运行时获取这些信息。ROS - 获取代码中的当前可用主题(不是命令)
有没有什么API可以做到这一点?
谢谢
我想在我的代码中获取当前可用的主题,以便我可以相应地设置发布者和订阅者。我知道命令'rostopic list'会显示这一点,但我想在程序运行时获取这些信息。ROS - 获取代码中的当前可用主题(不是命令)
有没有什么API可以做到这一点?
谢谢
Gabor Meszaros的回答后编辑。
您会发现ROS C++ API参考(roscpp)here和 - 在Python中 - 您会在ros::master小节中找到getTopics
方法。
下面是如何使用它的一个示例代码:
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;
}
感谢您的回答!我更喜欢Alex给出的一个片段来介绍这个用法。 – 2014-11-07 11:34:50
@ linzhang.robot如你所愿,我只是想帮忙。 :) – 2014-11-07 11:54:42