Ask Your Question
0

[ROS2] Is it possible to listen and subscribe to newly created topic in C++?

asked 2019-08-08 14:27:21 -0500

ahtsan gravatar image

updated 2019-08-08 14:27:31 -0500

Not sure if it's possible programmatically.

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
2

answered 2019-08-08 16:16:30 -0500

tfoote gravatar image

updated 2019-08-08 16:27:17 -0500

Yes, you can dynamically create new subscriptions in c++ at run time.

Use the create_subscription method on Node

http://docs.ros2.org/latest/api/rclcp...

edit flag offensive delete link more

Comments

How about discovering new topics that are created during run time?

ahtsan gravatar imageahtsan ( 2019-08-08 16:17:14 -0500 )edit

Sure. You can get the list of topics and their types that have been discovered at any time from the API.

See http://docs.ros2.org/latest/api/rclcp... (search for “introspecting the ROS graph”)

tfoote gravatar imagetfoote ( 2019-08-08 16:25:56 -0500 )edit

But if type is not known in advance, how can we create the subscription? As the message type is needed at the time of subscription creation.

ahtsan gravatar imageahtsan ( 2019-08-08 17:42:34 -0500 )edit
1

Use the api linked above in the comment to get the list of topics and their types before you call create subscription. Or know through some other means what type you're expecting. The callback must match the type too so you're going to need to have an idea about the type beforehand anyway.

tfoote gravatar imagetfoote ( 2019-08-08 18:13:06 -0500 )edit

Got it. Thanks!

ahtsan gravatar imageahtsan ( 2019-08-08 18:55:03 -0500 )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

1 follower

Stats

Asked: 2019-08-08 14:27:21 -0500

Seen: 22 times

Last updated: Aug 08