TopicManager 类

这个类有时也有用,但是不熟悉就容易用错。它具备单例模式 static const TopicManagerPtr& instance(),使用方便。

  • size_t getNumPublishers (const std::string &_topic)    Return the number of publishers connected to this node on a particular topic.

  • size_t getNumSubscribers (const std::string &_topic)    Return the number of subscribers a node has for a particular topic

  • void getAdvertisedTopics (V_string &topics)    Get the list of topics advertised by this node

  • void getSubscribedTopics (V_string &topics)    Get the list of topics subscribed to by this node

根据说明,对以上几个函数,不能直接传一个话题名做参数,以获知订阅者和发布者的数量,结果往往不是你想要的。

1
2
3
4
std::vector<string> topics;
ros::TopicManager::instance()->getSubscribedTopics(topics);
for(int i=0; i< topics.size(); i++)
std::cout << topics.at(i) << " ";

这里获得的话题只有clock,而不是所有被订阅的节点。

  • PublicationPtr lookupPublication(const std::string &topic)    Lookup an advertised topic.