Last active
November 24, 2022 03:31
-
-
Save bechu/6222399 to your computer and use it in GitHub Desktop.
Get all topic and type from ROS in c++ !
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <ros/ros.h> | |
int main(int argc, char** argv) | |
{ | |
ros::init(argc, argv, "ros_something"); | |
ros::start(); | |
XmlRpc::XmlRpcValue params("ros_topic_list"); | |
XmlRpc::XmlRpcValue results; | |
XmlRpc::XmlRpcValue r; | |
if(ros::master::execute("getTopicTypes", params, results, r, false) == true) | |
{ | |
if(results.getType() == XmlRpc::XmlRpcValue::TypeArray) | |
{ | |
int32_t i = 2; | |
if(results[i].getType() == XmlRpc::XmlRpcValue::TypeArray) | |
{ | |
for (int32_t j = 0; j < results[i].size(); ++j) | |
{ | |
if(results[i][j].getType() == XmlRpc::XmlRpcValue::TypeArray) | |
{ | |
if(results[i][j].size() == 2) | |
{ | |
if(results[i][j][0].getType() == XmlRpc::XmlRpcValue::TypeString | |
&& results[i][j][1].getType() == XmlRpc::XmlRpcValue::TypeString) | |
{ | |
std::string topic = static_cast<std::string>(results[i][j][0]); | |
std::string type = static_cast<std::string>(results[i][j][1]); | |
std::cout<<"Topic : "<<topic<<" -> "<<type<<std::endl; | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
ros::shutdown(); | |
return 0; | |
} |
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; }
achieves same result as above cleaner code. found this at http://stackoverflow.com/questions/26785675/ros-get-current-available-topic-in-code-not-command
Only get published topic
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How do you set the URI? When I use the command line to list topics, I get five or six separate topics, but I have no luck when running the code in C++.