Ask Your Question
0

[ROS2] Is there a way for a node to subscribe to existing topics?

asked 2019-06-03 21:41:00 -0600

wan_da gravatar image

updated 2019-06-03 23:45:47 -0600

jayess gravatar image

It is extremely easy to create a topic, just by publishing a message to a nonexistent topic would cause ROS2 to create that topic for you, as seen in this tutorial

However, is there a way to make an existing node subscribe to an existing topic?

The tutorial above showed a way to create a new node to subscribe to a topic, by simply using the command ros2 topic echo /<TOPIC NAME>. This created a new node, /_ros2cli_25438, which I assume is the command line, subscribing to the topic /barbarians, which is being published to by the existing node /_ros2cli_publisher_std_msgs_String.

However, is there a command to have an existing node subscribe to an existing topic?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2019-06-04 01:22:35 -0600

Geoff gravatar image

updated 2019-06-04 02:24:47 -0600

However, is there a way to make an existing node subscribe to an existing topic?

A node can subscribe to a topic whether it exists or not. If it doesn't, then after the node subscribes it will effectively exist.

However, is there a command to have an existing node subscribe to an existing topic?

If a node's source is not written so that it subscribes to a topic, then no, you can't make it subscribe to a topic via an external command.

If you want to write a node that can subscribe to a topic on demand, then you can do that with the following steps:

  • Have the node subscribe to a topic with a message type that is something like the following: std_msgs/String topic_name
  • When the node receives a message on the topic, it would subscribe to the specified topic.
  • Use rostopic pub /blag std_msgs/String 'topic_to_subscribe_to' to tell the node to subscribe to a new topic.

The above makes some assumptions:

  • The topic will have a type fixed at compile time, or you are using rclpy and have written your callback such that it can handle in some way any message type that arrives.
  • The node's subscriber callback is reentrant, such that multiple topics receiving messages at once using the same callback is not a problem, or you can guarantee that your node will only ever subscribe to or receive messages on one topic at a time.
edit flag offensive delete link more

Comments

@Geoff: your answer is high-level, so it doesn't really matter, but the OP tagged this with ros2 and you're referring to rospy which is the ROS 1 client library. Should that have been rclpy?

gvdhoorn gravatar imagegvdhoorn ( 2019-06-04 02:19:02 -0600 )edit
1

Gah sorry. I need sleep. Yes, it should be rclpy. Although as you say, the answer doesn't change for ROS 1 or ROS 2.

Geoff gravatar imageGeoff ( 2019-06-04 02:24:23 -0600 )edit

Thank you very much.

wan_da gravatar imagewan_da ( 2019-06-04 03:08:38 -0600 )edit

As a general comment on this topic btw: @wan_da: you may want to look into remapping. If you're worried about nodes not subscribing to topics that exist -- but perhaps they are subscribing to topics with the same type but a different name -- and still want to connect nodes, you can remap whatever topic they are subscribing to, to something else. I've tried to clarify how this works in ROS 1 in #q303611. That Q&A also links to a ROS 2 article about the subject.

It does have its uses, but using a topic to instruct a node to subscribe to another topic seems like an anti-pattern to me.

gvdhoorn gravatar imagegvdhoorn ( 2019-06-04 03:45:42 -0600 )edit

I definitely agree it's an anti-pattern and not something I would recommend doing. A node's interface, in terms of the topics, services, actions and parameters it interacts with, needs to be known and well-documented. Creating a node that subscribes to the message type you are interested in and remapping it at launch time is the correct approach.

Geoff gravatar imageGeoff ( 2019-06-04 19:07:30 -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-06-03 21:41:00 -0600

Seen: 224 times

Last updated: Jun 04