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

Dynamically resolve message type and subscribe to topic at runtime in roscpp ?

asked 2018-08-24 05:34:03 -0500

moooeeeep gravatar image

updated 2018-08-24 07:31:07 -0500

Is it possible to subscribe to a topic configured at runtime in a C++ node?

For example, is there an API to do the following in roscpp (adapted the code snippet seen here)?

type_name = rostopic.get_topic_type(topic_name, blocking=False)[0]
if type_name:
    type_class = roslib.message.get_message_class(type_name)
    subscriber = rospy.Subscriber(topic_name, type_class, callback)
edit retag flag offensive close merge delete


May I suggest a topic change? "subscribe dynamically at runtime" is typically understood to mean: "create subscribers (for a specific msg type) at runtime". You seem to be asking how to create subscribers without knowing the msg type. That would be something else.

gvdhoorn gravatar image gvdhoorn  ( 2018-08-24 05:54:15 -0500 )edit

@gvdhoorn I updated the title to hopefully better express the intent of the question. Feel free to improve if you can come up with an even better title!

moooeeeep gravatar image moooeeeep  ( 2018-08-24 07:24:11 -0500 )edit

I think this is perfect. Thanks.

gvdhoorn gravatar image gvdhoorn  ( 2018-08-24 07:25:20 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2018-08-24 06:19:59 -0500

Yes it is possible to do exactly what you want. The package you'll be using is ros_type_introspection this allows you to dynamically access the meta information of a message and extract the values from any message type.

There is tutorial that shows you how to do exactly what you're asking about here.

The only limitation of this technique is that you can't automatically create the original ros message type objects, you'd have to code for each one individually.

Hope this helps.

edit flag offensive delete link more



Thanks for this link! I ended up to only need the topic_tools::ShapeShifter for my application. But especially the examples for ros_type_introspection here helped me a lot!

moooeeeep gravatar image moooeeeep  ( 2018-08-29 04:00:50 -0500 )edit

Question Tools



Asked: 2018-08-24 05:34:03 -0500

Seen: 2,456 times

Last updated: Aug 24 '18