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 -0600

ahtsan gravatar image

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

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 -0600

tfoote gravatar image

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

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 image ahtsan  ( 2019-08-08 16:17:14 -0600 )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 image tfoote  ( 2019-08-08 16:25:56 -0600 )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 image ahtsan  ( 2019-08-08 17:42:34 -0600 )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 image tfoote  ( 2019-08-08 18:13:06 -0600 )edit

Got it. Thanks!

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

1 follower

Stats

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

Seen: 143 times

Last updated: Aug 08 '19