Ask Your Question
0

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

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

moooeeeep gravatar image

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

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

Comments

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 imagegvdhoorn ( 2018-08-24 05:54:15 -0600 )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 imagemoooeeeep ( 2018-08-24 07:24:11 -0600 )edit

I think this is perfect. Thanks.

gvdhoorn gravatar imagegvdhoorn ( 2018-08-24 07:25:20 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted
2

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

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

Comments

1

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 imagemoooeeeep ( 2018-08-29 04:00:50 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

2 followers

Stats

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

Seen: 699 times

Last updated: Aug 24 '18