147 const std::
string & instance_name, const Config & config, Context context, const rclcpp::QoS & qos)
148: RosConditionNode(instance_name, config, context), qos_{qos}
152 if (
const BT::Expected<std::string> expected_name = context_.
getTopicName(
this)) {
158 dynamic_client_instance_ =
true;
165 if (topic_name.empty()) {
166 throw exceptions::RosNodeError(
167 context_.getFullyQualifiedTreeNodeName(
this) +
" - Argument topic_name is empty when trying to create a client.");
171 if (publisher_ && topic_name_ == topic_name)
return true;
173 rclcpp::Node::SharedPtr node = context_.getRosNode();
177 topic_name, [&] {
return node->template create_publisher<MessageT>(topic_name, qos_); });
178 topic_name_ = topic_name;
180 logger_,
"%s - Created publisher for topic '%s'.", context_.getFullyQualifiedTreeNodeName(
this).c_str(),
185 logger_,
"%s - Waiting for at least one subscriber to connect to topic '%s'...",
186 context_.getFullyQualifiedTreeNodeName(
this).c_str(), topic_name_.c_str());
188 const auto start_time = context_.getCurrentTime();
189 while (rclcpp::ok() && publisher_->get_subscription_count() == 0) {
190 if ((context_.getCurrentTime() - start_time) > context_.getRegistrationOptions().wait_timeout) {
192 logger_,
"%s - Timeout waiting for subscriber to connect to topic '%s' after %.2f seconds.",
193 context_.getFullyQualifiedTreeNodeName(
this).c_str(), topic_name_.c_str(),
194 context_.getRegistrationOptions().wait_timeout.count());
197 rclcpp::sleep_for(std::chrono::milliseconds(5));
200 if (publisher_->get_subscription_count() > 0) {
202 logger_,
"%s - At least one subscriber found.", context_.getFullyQualifiedTreeNodeName(
this).c_str(),
203 topic_name_.c_str());
204 }
else if (context_.getRegistrationOptions().allow_unreachable) {
206 logger_,
"%s - No subscriber connected to topic '%s', but continuing as allow_unreachable is true.",
207 context_.getFullyQualifiedTreeNodeName(
this).c_str(), topic_name_.c_str());
209 throw exceptions::RosNodeError(
210 context_.getFullyQualifiedTreeNodeName(
this) +
" - No subscriber connected to topic '" + topic_name_ +
211 "' after waiting for " + std::to_string(context_.getRegistrationOptions().wait_timeout.count()) +
" seconds.");