ROS2 How to check if a publisher is connected?
I noticed that creating a publicher does not guarantee an instant connection to the ros2-network. Therefore, sending instant messages immediately after the creation of the node leads to the loss. Does the ROS2 API have c++ tools to check a publisher's current network connectivity?
OS: Ubuntu 20.04
Distro: ROS2 Foxy
Here is the simple publisher I use to test this issue
#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/bool.hpp"
class Pub : public rclcpp::Node {
public:
Pub() : Node("pub") {
publisher_ = this->create_publisher<std_msgs::msg::Bool>("test", 10);
}
~Pub() = default;
void Execute() {
auto msg = std_msgs::msg::Bool();
msg.set__data(true);
publisher_->publish(msg);
RCLCPP_INFO(this->get_logger(), "Set bool");
}
private:
rclcpp::Publisher<std_msgs::msg::Bool>::SharedPtr publisher_;
};
int main(int argc, char* argv[]) {
rclcpp::init(argc, argv);
Pub node;
node.Execute();
rclcpp::shutdown();
return 0;
}