ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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 rospy 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.

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 rospyrclpy 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.

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 rostopic pub /blag std_msgs/String 'topic_to_subscribe_to' '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.