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

Package for Generically Listening to Topics

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

orion gravatar image

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

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
3

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

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)
            ui.topicsList->addItem(it->name.c_str());
    }
}
edit flag offensive delete link more

Comments

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 -0600 )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 -0600 )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 -0600 )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 -0600 )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 -0600 )edit
0

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

Markus gravatar image

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

You can also try this code :)

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 << " -> " << info.datatype <<       std::endl;
}

See also here: https://answers.ros.org/question/1081...

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

edit flag offensive delete link more

Question Tools

1 follower

Stats

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

Seen: 625 times

Last updated: May 16 '18