ROS 2 rclcpp + rcl - rolling  rolling-20536064
ROS 2 C++ Client Library with ROS Client Library
create_subscription.hpp
1 // Copyright 2016 Open Source Robotics Foundation, Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef RCLCPP__CREATE_SUBSCRIPTION_HPP_
16 #define RCLCPP__CREATE_SUBSCRIPTION_HPP_
17 
18 #include <chrono>
19 #include <functional>
20 #include <memory>
21 #include <stdexcept>
22 #include <string>
23 #include <utility>
24 
25 #include "rclcpp/detail/resolve_enable_topic_statistics.hpp"
26 
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"
31 
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"
39 
40 namespace rclcpp
41 {
42 
43 namespace detail
44 {
45 template<
46  typename MessageT,
47  typename CallbackT,
48  typename AllocatorT,
49  typename SubscriptionT,
50  typename MessageMemoryStrategyT,
51  typename NodeParametersT,
52  typename NodeTopicsT
53 >
54 typename std::shared_ptr<SubscriptionT>
56  NodeParametersT & node_parameters,
57  NodeTopicsT & node_topics,
58  const std::string & topic_name,
59  const rclcpp::QoS & qos,
60  CallbackT && callback,
63  ),
64  typename MessageMemoryStrategyT::SharedPtr msg_mem_strat = (
65  MessageMemoryStrategyT::create_default()
66  )
67 )
68 {
69  using rclcpp::node_interfaces::get_node_topics_interface;
70  auto node_topics_interface = get_node_topics_interface(node_topics);
71 
72  std::shared_ptr<rclcpp::topic_statistics::SubscriptionTopicStatistics>
73  subscription_topic_stats = nullptr;
74 
75  if (rclcpp::detail::resolve_enable_topic_statistics(
76  options,
77  *node_topics_interface->get_node_base_interface()))
78  {
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");
83  }
84 
85  std::shared_ptr<Publisher<statistics_msgs::msg::MetricsMessage>>
86  publisher = rclcpp::detail::create_publisher<statistics_msgs::msg::MetricsMessage>(
87  node_parameters,
88  node_topics_interface,
89  options.topic_stats_options.publish_topic,
90  options.topic_stats_options.qos);
91 
92  subscription_topic_stats =
93  std::make_shared<rclcpp::topic_statistics::SubscriptionTopicStatistics>(
94  node_topics_interface->get_node_base_interface()->get_name(), publisher);
95 
96  std::weak_ptr<
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();
103  }
104  };
105 
106  auto node_timer_interface = node_topics_interface->get_node_timers_interface();
107 
108  auto timer = create_wall_timer(
109  std::chrono::duration_cast<std::chrono::nanoseconds>(
110  options.topic_stats_options.publish_period),
111  sub_call_back,
112  options.callback_group,
113  node_topics_interface->get_node_base_interface(),
114  node_timer_interface
115  );
116 
117  subscription_topic_stats->set_publisher_timer(timer);
118  }
119 
120  auto factory = rclcpp::create_subscription_factory<MessageT>(
121  std::forward<CallbackT>(callback),
122  options,
123  msg_mem_strat,
124  subscription_topic_stats
125  );
126 
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),
132  qos;
133 
134  auto sub = node_topics_interface->create_subscription(topic_name, factory, actual_qos);
135  node_topics_interface->add_subscription(sub, options.callback_group);
136 
137  return std::dynamic_pointer_cast<SubscriptionT>(sub);
138 }
139 } // namespace detail
140 
142 
167 template<
168  typename MessageT,
169  typename CallbackT,
170  typename AllocatorT = std::allocator<void>,
171  typename SubscriptionT = rclcpp::Subscription<MessageT, AllocatorT>,
172  typename MessageMemoryStrategyT = typename SubscriptionT::MessageMemoryStrategyType,
173  typename NodeT>
174 typename std::shared_ptr<SubscriptionT>
176  NodeT & node,
177  const std::string & topic_name,
178  const rclcpp::QoS & qos,
179  CallbackT && callback,
182  ),
183  typename MessageMemoryStrategyT::SharedPtr msg_mem_strat = (
184  MessageMemoryStrategyT::create_default()
185  )
186 )
187 {
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);
191 }
192 
194 
197 template<
198  typename MessageT,
199  typename CallbackT,
200  typename AllocatorT = std::allocator<void>,
201  typename SubscriptionT = rclcpp::Subscription<MessageT, AllocatorT>,
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,
208  const rclcpp::QoS & qos,
209  CallbackT && callback,
212  ),
213  typename MessageMemoryStrategyT::SharedPtr msg_mem_strat = (
214  MessageMemoryStrategyT::create_default()
215  )
216 )
217 {
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);
222 }
223 
224 } // namespace rclcpp
225 
226 #endif // RCLCPP__CREATE_SUBSCRIPTION_HPP_
Encapsulation of Quality of Service settings.
Definition: qos.hpp:114
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.