Ask Your Question
0

How to change ROS2 node's topic in runtime?

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

cghale12@gmail.com gravatar image

How to change ROS2 node's topic in runtime?

edit retag flag offensive close merge delete

Comments

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

cghale12@gmail.com gravatar image cghale12@gmail.com  ( 2020-06-05 11:05:59 -0500 )edit

1 Answer

Sort by » oldest newest most voted
0

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.

Example:

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

Comments

Hi

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

cghale12@gmail.com gravatar image cghale12@gmail.com  ( 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.

cghale12@gmail.com gravatar image cghale12@gmail.com  ( 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

Stats

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

Seen: 382 times

Last updated: Jun 05 '20