0
$\begingroup$

Rosanswers logo

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)

Originally posted by moooeeeep on ROS Answers with karma: 194 on 2018-08-24

Post score: 1


Original comments

Comment by gvdhoorn on 2018-08-24:
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.

Comment by moooeeeep on 2018-08-24:
@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!

Comment by gvdhoorn on 2018-08-24:
I think this is perfect. Thanks.

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

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.


Originally posted by PeteBlackerThe3rd with karma: 9529 on 2018-08-24

This answer was ACCEPTED on the original site

Post score: 3


Original comments

Comment by moooeeeep on 2018-08-29:
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!

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.