ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Package for Generically Listening to Topics

asked 2014-01-13 04:32:15 -0500

orion gravatar image

updated 2014-01-14 09:45:03 -0500

Is there a way to get a list of topics while running a node. For example, if a new message is published, is there a way to programmatically get the topic name posted (equivalent to running rostopic list in a terminal).

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2014-01-13 05:00:48 -0500

dornhege gravatar image

I used this to add topics of a certain type to a GUI selector:

ros::master::V_TopicInfo ti;
if(ros::master::getTopics(ti)) {
    for(ros::master::V_TopicInfo::iterator it = ti.begin(); it != ti.end(); it++) {
        if(it->datatype == _topicType)
edit flag offensive delete link more


Looks good to me. I will try it and see if it works for my needs. However, I may also need to do this in python. I figured before I start digging I could ask if you knew of a solution for that as well?

orion gravatar image orion  ( 2014-01-14 09:45:53 -0500 )edit

Yes and no: I don't have code, but there are python APIs that do exactly the same thing. If you can't find that easily in the rospy documentation, just see what rostopic list does.

dornhege gravatar image dornhege  ( 2014-01-15 00:47:49 -0500 )edit

So I setup and have the ability to get topics whether it be C++ or python. I started looking further through the code to see if there was a way to listen for a new topic as a callback (similar to subscriber), versus a ROS loop, but did not see one. Curious if I just missed it?

orion gravatar image orion  ( 2014-02-11 06:47:15 -0500 )edit

You want something that tells you if a new topic has come up? I don't think that exists. So you'd probably need to keep polling.

dornhege gravatar image dornhege  ( 2014-02-11 22:17:29 -0500 )edit

That is what I assumed. I just made a polling method to take care of it. Thanks.

orion gravatar image orion  ( 2014-02-15 07:55:37 -0500 )edit

answered 2018-05-16 04:00:04 -0500

Markus gravatar image

updated 2018-05-16 04:18:54 -0500

You can also try this code :)

ros::master::V_TopicInfo 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.datatype <<       std::endl;

See also here:

However if someone knows how to do that with rospy let me know ! :)

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2014-01-13 04:32:15 -0500

Seen: 591 times

Last updated: May 16 '18