ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange |
1 | initial version |
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);