Ask Your Question

How to change ROS2 node's topic in runtime?

asked 2020-06-04 07:31:55 -0500 gravatar image

How to change ROS2 node's topic in runtime?

edit retag flag offensive close merge delete


I want to develop a publisher which should publish on 'topic1' and after some time the same publisher should start publishing on 'topic2' during runtime (and i want to be able to trigger this transition by some external function call, SetParameter for example). gravatar image  ( 2020-06-05 11:05:59 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2020-06-05 10:30:14 -0500

MrCheesecake gravatar image

Do you want to change it multiple times in runtime or only at startup?
If at startup dynamically without compiling, I would recommend parameters.

If you want to change the topic, just create a new subscription/publisher and assign it to the object of the old subscription/publisher. The old one stops then because it's a shared pointer and is not used anymore and the old callback is not triggered anymore.


rclcpp::Subscription<std_msgs::msg::Bool>::SharedPtr sub_;

sub_ = this->create_subscription<std_msgs::msg::Bool>("/topic1", 1, sub_cb);
// change topic
sub_ = this->create_subscription<std_msgs::msg::Bool>("/topic2", 1, sub_cb);
edit flag offensive delete link more



Really appreciate your help, i actually want to change a node's topic couple of times during runtime (when node is already spinning), as far as i know its not possible as of now but i wanted to confirm.

Regards, Chandrashekhar gravatar image  ( 2020-06-05 10:38:33 -0500 )edit

It's possible as I described. Just create a new subscription/publisher and replace the old one by it.

MrCheesecake gravatar image MrCheesecake  ( 2020-06-05 10:56:59 -0500 )edit

Yeah, actually i meant that i want the node to publish on topic1 for a while and then same node should start publishing on topic2 during runtime. gravatar image  ( 2020-06-05 11:06:25 -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


Asked: 2020-06-04 07:31:55 -0500

Seen: 387 times

Last updated: Jun 05 '20