15 #ifndef RCLCPP__CREATE_SUBSCRIPTION_HPP_
16 #define RCLCPP__CREATE_SUBSCRIPTION_HPP_
25 #include "rclcpp/detail/resolve_enable_topic_statistics.hpp"
27 #include "rclcpp/node_interfaces/get_node_timers_interface.hpp"
28 #include "rclcpp/node_interfaces/get_node_topics_interface.hpp"
29 #include "rclcpp/node_interfaces/node_timers_interface.hpp"
30 #include "rclcpp/node_interfaces/node_topics_interface.hpp"
32 #include "rclcpp/create_publisher.hpp"
33 #include "rclcpp/create_timer.hpp"
34 #include "rclcpp/qos.hpp"
35 #include "rclcpp/subscription_factory.hpp"
36 #include "rclcpp/subscription_options.hpp"
37 #include "rclcpp/timer.hpp"
38 #include "rclcpp/topic_statistics/subscription_topic_statistics.hpp"
39 #include "rmw/qos_profiles.h"
50 typename SubscriptionT,
51 typename MessageMemoryStrategyT,
52 typename NodeParametersT,
54 typename ROSMessageType =
typename SubscriptionT::ROSMessageType>
55 typename std::shared_ptr<SubscriptionT>
57 NodeParametersT & node_parameters,
58 NodeTopicsT & node_topics,
59 const std::string & topic_name,
61 CallbackT && callback,
65 typename MessageMemoryStrategyT::SharedPtr msg_mem_strat = (
66 MessageMemoryStrategyT::create_default()
70 using rclcpp::node_interfaces::get_node_topics_interface;
71 auto node_topics_interface = get_node_topics_interface(node_topics);
73 std::shared_ptr<rclcpp::topic_statistics::SubscriptionTopicStatistics<ROSMessageType>>
74 subscription_topic_stats =
nullptr;
76 if (rclcpp::detail::resolve_enable_topic_statistics(
78 *node_topics_interface->get_node_base_interface()))
80 if (options.topic_stats_options.publish_period <= std::chrono::milliseconds(0)) {
81 throw std::invalid_argument(
82 "topic_stats_options.publish_period must be greater than 0, specified value of " +
83 std::to_string(options.topic_stats_options.publish_period.count()) +
87 std::shared_ptr<Publisher<statistics_msgs::msg::MetricsMessage>>
88 publisher = rclcpp::detail::create_publisher<statistics_msgs::msg::MetricsMessage>(
90 node_topics_interface,
91 options.topic_stats_options.publish_topic,
94 subscription_topic_stats = std::make_shared<
96 >(node_topics_interface->get_node_base_interface()->get_name(), publisher);
100 > weak_subscription_topic_stats(subscription_topic_stats);
101 auto sub_call_back = [weak_subscription_topic_stats]() {
102 auto subscription_topic_stats = weak_subscription_topic_stats.lock();
103 if (subscription_topic_stats) {
104 subscription_topic_stats->publish_message_and_reset_measurements();
108 auto node_timer_interface = node_topics_interface->get_node_timers_interface();
111 std::chrono::duration_cast<std::chrono::nanoseconds>(
112 options.topic_stats_options.publish_period),
114 options.callback_group,
115 node_topics_interface->get_node_base_interface(),
119 subscription_topic_stats->set_publisher_timer(timer);
122 auto factory = rclcpp::create_subscription_factory<MessageT>(
123 std::forward<CallbackT>(callback),
126 subscription_topic_stats
129 const rclcpp::QoS & actual_qos = options.qos_overriding_options.get_policy_kinds().size() ?
130 rclcpp::detail::declare_qos_parameters(
131 options.qos_overriding_options, node_parameters,
132 node_topics_interface->resolve_topic_name(topic_name),
136 auto sub = node_topics_interface->create_subscription(topic_name, factory, actual_qos);
137 node_topics_interface->add_subscription(sub, options.callback_group);
139 return std::dynamic_pointer_cast<SubscriptionT>(sub);
172 typename AllocatorT = std::allocator<void>,
174 typename MessageMemoryStrategyT =
typename SubscriptionT::MessageMemoryStrategyType,
176 typename std::shared_ptr<SubscriptionT>
179 const std::string & topic_name,
181 CallbackT && callback,
185 typename MessageMemoryStrategyT::SharedPtr msg_mem_strat = (
186 MessageMemoryStrategyT::create_default()
190 return rclcpp::detail::create_subscription<
191 MessageT, CallbackT, AllocatorT, SubscriptionT, MessageMemoryStrategyT>(
192 node, node, topic_name, qos, std::forward<CallbackT>(callback), options, msg_mem_strat);
202 typename AllocatorT = std::allocator<void>,
204 typename MessageMemoryStrategyT =
typename SubscriptionT::MessageMemoryStrategyType>
205 typename std::shared_ptr<SubscriptionT>
207 rclcpp::node_interfaces::NodeParametersInterface::SharedPtr & node_parameters,
208 rclcpp::node_interfaces::NodeTopicsInterface::SharedPtr & node_topics,
209 const std::string & topic_name,
211 CallbackT && callback,
215 typename MessageMemoryStrategyT::SharedPtr msg_mem_strat = (
216 MessageMemoryStrategyT::create_default()
220 return rclcpp::detail::create_subscription<
221 MessageT, CallbackT, AllocatorT, SubscriptionT, MessageMemoryStrategyT>(
222 node_parameters, node_topics, topic_name, qos,
223 std::forward<CallbackT>(callback), options, msg_mem_strat);
Encapsulation of Quality of Service settings.
Subscription implementation, templated on the type of message this subscription receives.
Versions of rosidl_typesupport_cpp::get_message_type_support_handle that handle adapted types.
std::shared_ptr< SubscriptionT > create_subscription(NodeT &node, const std::string &topic_name, const rclcpp::QoS &qos, CallbackT &&callback, const rclcpp::SubscriptionOptionsWithAllocator< AllocatorT > &options=(rclcpp::SubscriptionOptionsWithAllocator< AllocatorT >()), typename MessageMemoryStrategyT::SharedPtr msg_mem_strat=(MessageMemoryStrategyT::create_default()))
Create and return a subscription of the given MessageT type.
rclcpp::WallTimer< CallbackT >::SharedPtr create_wall_timer(std::chrono::duration< DurationRepT, DurationT > period, CallbackT callback, rclcpp::CallbackGroup::SharedPtr group, node_interfaces::NodeBaseInterface *node_base, node_interfaces::NodeTimersInterface *node_timers)
Convenience method to create a timer with node resources.
Structure containing optional configuration for Subscriptions.