Robotics StackExchange | Archived questions

How to get ros topic message type by coding

I'm trying to write a node that read a config file, which includes topic names, then read the messages published to that topics. Currently, I can retrieve the message type like this:

ros::master::V_TopicInfo topic_list;
ros::master::getTopics(topic_list);
for (auto i : topic_list) {
    if (i.name == "/battery_info") {
        std::cout << "Data type is: " << i.datatype << std::endl;
    }
}

But the datatype is a string, how can I convert this string to the equivalent type so that I can create a topic callback function, or is there another way, maybe written with Python code?

Asked by Kevin1719 on 2022-09-28 21:34:18 UTC

Comments

What is the overall goal of this task? Do you want to create a generic topic subscriber?

Asked by ravijoshi on 2022-09-29 04:46:25 UTC

Thanks a lot, this is exactly what I'm looking for.

Asked by Kevin1719 on 2022-09-30 03:22:06 UTC

Glad you found it useful!

Asked by ravijoshi on 2022-09-30 03:26:48 UTC

Answers