15 #ifndef RCLCPP__WAIT_FOR_MESSAGE_HPP_
16 #define RCLCPP__WAIT_FOR_MESSAGE_HPP_
23 #include "rcpputils/scope_exit.hpp"
25 #include "rclcpp/create_subscription.hpp"
26 #include "rclcpp/node.hpp"
27 #include "rclcpp/node_interfaces/node_parameters_interface.hpp"
28 #include "rclcpp/node_interfaces/node_topics_interface.hpp"
29 #include "rclcpp/qos.hpp"
30 #include "rclcpp/visibility_control.hpp"
31 #include "rclcpp/wait_set.hpp"
48 template<
class MsgT,
class Rep =
int64_t,
class Period = std::milli>
52 std::shared_ptr<rclcpp::Context> context,
53 std::chrono::duration<Rep, Period> time_to_wait = std::chrono::duration<Rep, Period>(-1))
55 auto gc = std::make_shared<rclcpp::GuardCondition>(context);
56 auto shutdown_callback_handle = context->add_on_shutdown_callback(
57 [weak_gc = std::weak_ptr<rclcpp::GuardCondition>{gc}]() {
58 auto strong_gc = weak_gc.lock();
66 RCPPUTILS_SCOPE_EXIT(wait_set.remove_subscription(subscription); );
67 wait_set.add_guard_condition(gc);
68 auto ret = wait_set.wait(time_to_wait);
69 if (ret.kind() != rclcpp::WaitResultKind::Ready) {
73 if (wait_set.get_rcl_wait_set().guard_conditions[0]) {
78 if (!subscription->take(out, info)) {
102 template<
class MsgT,
class Rep =
int64_t,
class Period = std::milli>
105 rclcpp::node_interfaces::NodeParametersInterface::SharedPtr node_parameters,
106 rclcpp::node_interfaces::NodeTopicsInterface::SharedPtr node_topics,
107 const std::string & topic,
108 std::chrono::duration<Rep, Period> time_to_wait = std::chrono::duration<Rep, Period>(-1),
111 auto sub = rclcpp::create_subscription<MsgT>(
116 [](
const std::shared_ptr<const MsgT>) {});
117 return wait_for_message<MsgT, Rep, Period>(
118 out, sub, node_topics->get_node_base_interface()->get_context(), time_to_wait);
133 template<
class MsgT,
class Rep =
int64_t,
class Period = std::milli>
136 rclcpp::Node::SharedPtr node,
137 const std::string & topic,
138 std::chrono::duration<Rep, Period> time_to_wait = std::chrono::duration<Rep, Period>(-1),
141 return wait_for_message<MsgT, Rep, Period>(
143 node->get_node_parameters_interface(),
144 node->get_node_topics_interface(),
Additional meta data about messages taken from subscriptions.
Encapsulation of Quality of Service settings.
Subscription implementation, templated on the type of message this subscription receives.
Encapsulates sets of waitable items which can be waited on as a group.
void add_subscription(std::shared_ptr< rclcpp::SubscriptionBase > subscription, rclcpp::SubscriptionWaitSetMask mask={})
Add a subscription to this wait set.
Versions of rosidl_typesupport_cpp::get_message_type_support_handle that handle adapted types.
bool wait_for_message(MsgT &out, std::shared_ptr< rclcpp::Subscription< MsgT >> subscription, std::shared_ptr< rclcpp::Context > context, std::chrono::duration< Rep, Period > time_to_wait=std::chrono::duration< Rep, Period >(-1))
Wait for the next incoming message.