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/qos.hpp"
34 #include "rclcpp/subscription_factory.hpp"
35 #include "rclcpp/subscription_options.hpp"
36 #include "rclcpp/timer.hpp"
37 #include "rclcpp/topic_statistics/subscription_topic_statistics.hpp"
38 #include "rmw/qos_profiles.h"
49 typename SubscriptionT,
50 typename MessageMemoryStrategyT,
51 typename NodeParametersT,
54 typename std::shared_ptr<SubscriptionT>
56 NodeParametersT & node_parameters,
57 NodeTopicsT & node_topics,
58 const std::string & topic_name,
60 CallbackT && callback,
64 typename MessageMemoryStrategyT::SharedPtr msg_mem_strat = (
65 MessageMemoryStrategyT::create_default()
69 using rclcpp::node_interfaces::get_node_topics_interface;
70 auto node_topics_interface = get_node_topics_interface(node_topics);
72 std::shared_ptr<rclcpp::topic_statistics::SubscriptionTopicStatistics>
73 subscription_topic_stats =
nullptr;
75 if (rclcpp::detail::resolve_enable_topic_statistics(
77 *node_topics_interface->get_node_base_interface()))
79 if (options.topic_stats_options.publish_period <= std::chrono::milliseconds(0)) {
80 throw std::invalid_argument(
81 "topic_stats_options.publish_period must be greater than 0, specified value of " +
82 std::to_string(options.topic_stats_options.publish_period.count()) +
" ms");
85 std::shared_ptr<Publisher<statistics_msgs::msg::MetricsMessage>>
86 publisher = rclcpp::detail::create_publisher<statistics_msgs::msg::MetricsMessage>(
88 node_topics_interface,
89 options.topic_stats_options.publish_topic,
90 options.topic_stats_options.qos);
92 subscription_topic_stats =
93 std::make_shared<rclcpp::topic_statistics::SubscriptionTopicStatistics>(
94 node_topics_interface->get_node_base_interface()->get_name(), publisher);
98 > weak_subscription_topic_stats(subscription_topic_stats);
99 auto sub_call_back = [weak_subscription_topic_stats]() {
100 auto subscription_topic_stats = weak_subscription_topic_stats.lock();
101 if (subscription_topic_stats) {
102 subscription_topic_stats->publish_message_and_reset_measurements();
106 auto node_timer_interface = node_topics_interface->get_node_timers_interface();
109 std::chrono::duration_cast<std::chrono::nanoseconds>(
110 options.topic_stats_options.publish_period),
112 options.callback_group,
113 node_topics_interface->get_node_base_interface(),
117 subscription_topic_stats->set_publisher_timer(timer);
120 auto factory = rclcpp::create_subscription_factory<MessageT>(
121 std::forward<CallbackT>(callback),
124 subscription_topic_stats
127 const rclcpp::QoS & actual_qos = options.qos_overriding_options.get_policy_kinds().size() ?
128 rclcpp::detail::declare_qos_parameters(
129 options.qos_overriding_options, node_parameters,
130 node_topics_interface->resolve_topic_name(topic_name),
134 auto sub = node_topics_interface->create_subscription(topic_name, factory, actual_qos);
135 node_topics_interface->add_subscription(sub, options.callback_group);
137 return std::dynamic_pointer_cast<SubscriptionT>(sub);
170 typename AllocatorT = std::allocator<void>,
172 typename MessageMemoryStrategyT =
typename SubscriptionT::MessageMemoryStrategyType,
174 typename std::shared_ptr<SubscriptionT>
177 const std::string & topic_name,
179 CallbackT && callback,
183 typename MessageMemoryStrategyT::SharedPtr msg_mem_strat = (
184 MessageMemoryStrategyT::create_default()
188 return rclcpp::detail::create_subscription<
189 MessageT, CallbackT, AllocatorT, SubscriptionT, MessageMemoryStrategyT>(
190 node, node, topic_name, qos, std::forward<CallbackT>(callback), options, msg_mem_strat);
200 typename AllocatorT = std::allocator<void>,
202 typename MessageMemoryStrategyT =
typename SubscriptionT::MessageMemoryStrategyType>
203 typename std::shared_ptr<SubscriptionT>
205 rclcpp::node_interfaces::NodeParametersInterface::SharedPtr & node_parameters,
206 rclcpp::node_interfaces::NodeTopicsInterface::SharedPtr & node_topics,
207 const std::string & topic_name,
209 CallbackT && callback,
213 typename MessageMemoryStrategyT::SharedPtr msg_mem_strat = (
214 MessageMemoryStrategyT::create_default()
218 return rclcpp::detail::create_subscription<
219 MessageT, CallbackT, AllocatorT, SubscriptionT, MessageMemoryStrategyT>(
220 node_parameters, node_topics, topic_name, qos,
221 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.
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, bool autostart=true)
Convenience method to create a wall timer with node resources.
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.
Structure containing optional configuration for Subscriptions.