rclcpp  master
C++ ROS Client Library API
node_impl.hpp
Go to the documentation of this file.
1 // Copyright 2014 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__NODE_IMPL_HPP_
16 #define RCLCPP__NODE_IMPL_HPP_
17 
18 #include <rmw/error_handling.h>
19 #include <rmw/rmw.h>
20 
21 #include <algorithm>
22 #include <cstdlib>
23 #include <iostream>
24 #include <limits>
25 #include <map>
26 #include <memory>
27 #include <sstream>
28 #include <stdexcept>
29 #include <string>
30 #include <utility>
31 #include <vector>
32 
33 #include "rcl/publisher.h"
34 #include "rcl/subscription.h"
35 
36 #include "rcl_interfaces/msg/intra_process_message.hpp"
37 
40 #include "rclcpp/parameter.hpp"
45 
46 #ifndef RCLCPP__NODE_HPP_
47 #include "node.hpp"
48 #endif
49 
50 namespace rclcpp
51 {
52 
53 template<typename MessageT, typename Alloc, typename PublisherT>
56  const std::string & topic_name, size_t qos_history_depth,
57  std::shared_ptr<Alloc> allocator)
58 {
59  if (!allocator) {
60  allocator = std::make_shared<Alloc>();
61  }
62  rmw_qos_profile_t qos = rmw_qos_profile_default;
63  qos.depth = qos_history_depth;
64  return this->create_publisher<MessageT, Alloc, PublisherT>(topic_name, qos, allocator);
65 }
66 
67 template<typename MessageT, typename Alloc, typename PublisherT>
70  const std::string & topic_name, const rmw_qos_profile_t & qos_profile,
71  std::shared_ptr<Alloc> allocator)
72 {
73  if (!allocator) {
74  allocator = std::make_shared<Alloc>();
75  }
76  return rclcpp::create_publisher<MessageT, Alloc, PublisherT>(
77  this->node_topics_.get(),
78  topic_name,
79  qos_profile,
80  use_intra_process_comms_,
81  allocator);
82 }
83 
84 template<typename MessageT, typename CallbackT, typename Alloc, typename SubscriptionT>
87  const std::string & topic_name,
88  CallbackT && callback,
89  const rmw_qos_profile_t & qos_profile,
90  rclcpp::callback_group::CallbackGroup::SharedPtr group,
91  bool ignore_local_publications,
93  msg_mem_strat,
94  std::shared_ptr<Alloc> allocator)
95 {
96  if (!allocator) {
97  allocator = std::make_shared<Alloc>();
98  }
99 
100  if (!msg_mem_strat) {
102  msg_mem_strat = MessageMemoryStrategy<MessageT, Alloc>::create_default();
103  }
104 
105  return rclcpp::create_subscription<MessageT, CallbackT, Alloc, SubscriptionT>(
106  this->node_topics_.get(),
107  topic_name,
108  std::forward<CallbackT>(callback),
109  qos_profile,
110  group,
111  ignore_local_publications,
112  use_intra_process_comms_,
113  msg_mem_strat,
114  allocator);
115 }
116 
117 template<typename MessageT, typename CallbackT, typename Alloc, typename SubscriptionT>
120  const std::string & topic_name,
121  size_t qos_history_depth,
122  CallbackT && callback,
123  rclcpp::callback_group::CallbackGroup::SharedPtr group,
124  bool ignore_local_publications,
126  msg_mem_strat,
127  std::shared_ptr<Alloc> allocator)
128 {
129  rmw_qos_profile_t qos = rmw_qos_profile_default;
130  qos.depth = qos_history_depth;
131  return this->create_subscription<MessageT, CallbackT, Alloc, SubscriptionT>(
132  topic_name,
133  std::forward<CallbackT>(callback),
134  qos,
135  group,
136  ignore_local_publications,
137  msg_mem_strat,
138  allocator);
139 }
140 
141 template<typename DurationT, typename CallbackT>
145  CallbackT callback,
146  rclcpp::callback_group::CallbackGroup::SharedPtr group)
147 {
149  std::chrono::duration_cast<std::chrono::nanoseconds>(period),
150  std::move(callback));
151  node_timers_->add_timer(timer, group);
152  return timer;
153 }
154 
155 template<typename ServiceT>
158  const std::string & service_name,
159  const rmw_qos_profile_t & qos_profile,
160  rclcpp::callback_group::CallbackGroup::SharedPtr group)
161 {
163  options.qos = qos_profile;
164 
165  using rclcpp::Client;
166  using rclcpp::ClientBase;
167 
169  node_base_.get(),
170  node_graph_,
171  service_name,
172  options);
173 
174  auto cli_base_ptr = std::dynamic_pointer_cast<ClientBase>(cli);
175  node_services_->add_client(cli_base_ptr, group);
176  return cli;
177 }
178 
179 template<typename ServiceT, typename CallbackT>
182  const std::string & service_name,
183  CallbackT && callback,
184  const rmw_qos_profile_t & qos_profile,
185  rclcpp::callback_group::CallbackGroup::SharedPtr group)
186 {
187  rclcpp::AnyServiceCallback<ServiceT> any_service_callback;
188  any_service_callback.set(std::forward<CallbackT>(callback));
189 
191  service_options.qos = qos_profile;
192 
193  auto serv = Service<ServiceT>::make_shared(
194  node_base_->get_shared_rcl_node_handle(),
195  service_name, any_service_callback, service_options);
196  auto serv_base_ptr = std::dynamic_pointer_cast<ServiceBase>(serv);
197  node_services_->add_service(serv_base_ptr, group);
198  return serv;
199 }
200 
201 template<typename CallbackT>
202 void
204 {
205  this->node_parameters_->register_param_change_callback(std::forward<CallbackT>(callback));
206 }
207 
208 template<typename ParameterT>
209 void
211  const std::string & name,
212  const ParameterT & value)
213 {
214  rclcpp::parameter::ParameterVariant parameter_variant;
215  if (!this->get_parameter(name, parameter_variant)) {
216  this->set_parameters({
218  });
219  }
220 }
221 
222 template<typename ParameterT>
223 bool
224 Node::get_parameter(const std::string & name, ParameterT & value) const
225 {
226  rclcpp::parameter::ParameterVariant parameter_variant;
227  bool result = get_parameter(name, parameter_variant);
228  if (result) {
229  value = parameter_variant.get_value<ParameterT>();
230  }
231 
232  return result;
233 }
234 
235 template<typename ParameterT>
236 bool
238  const std::string & name,
239  ParameterT & value,
240  const ParameterT & alternative_value) const
241 {
242  bool got_parameter = get_parameter(name, value);
243  if (!got_parameter) {
244  value = alternative_value;
245  }
246  return got_parameter;
247 }
248 
249 } // namespace rclcpp
250 
251 #endif // RCLCPP__NODE_IMPL_HPP_
Generic timer templated on the clock type. Periodically executes a user-specified callback...
Definition: timer.hpp:105
rclcpp::WallTimer< CallbackT >::SharedPtr create_wall_timer(std::chrono::duration< int64_t, DurationT > period, CallbackT callback, rclcpp::callback_group::CallbackGroup::SharedPtr group=nullptr)
Create a timer.
Definition: node_impl.hpp:143
std::shared_ptr< PublisherT > create_publisher(const std::string &topic_name, size_t qos_history_depth, std::shared_ptr< Alloc > allocator=nullptr)
Create and return a Publisher.
Definition: node_impl.hpp:55
Definition: client.hpp:118
Definition: allocator_common.hpp:24
bool get_parameter_or(const std::string &name, ParameterT &value, const ParameterT &alternative_value) const
Get the parameter value, or the "alternative value" if not set, and assign it to "value".
Definition: node_impl.hpp:237
void set_parameter_if_not_set(const std::string &name, const ParameterT &value)
Definition: node_impl.hpp:210
std::vector< rcl_interfaces::msg::SetParametersResult > set_parameters(const std::vector< rclcpp::parameter::ParameterVariant > &parameters)
rmw_qos_profile_t qos
void register_param_change_callback(CallbackT &&callback)
Register the callback for parameter changes.
Definition: node_impl.hpp:203
std::enable_if< type==ParameterType::PARAMETER_INTEGER, int64_t >::type get_value() const
Definition: parameter.hpp:87
rclcpp::parameter::ParameterVariant get_parameter(const std::string &name) const
rcl_client_options_t rcl_client_get_default_options(void)
T dynamic_pointer_cast(T... args)
T move(T... args)
Definition: service.hpp:93
Default allocation strategy for messages received by subscriptions.
Definition: message_memory_strategy.hpp:33
std::shared_ptr< SubscriptionT > create_subscription(const std::string &topic_name, CallbackT &&callback, const rmw_qos_profile_t &qos_profile=rmw_qos_profile_default, rclcpp::callback_group::CallbackGroup::SharedPtr group=nullptr, bool ignore_local_publications=false, typename rclcpp::message_memory_strategy::MessageMemoryStrategy< MessageT, Alloc >::SharedPtr msg_mem_strat=nullptr, std::shared_ptr< Alloc > allocator=nullptr)
Create and return a Subscription.
Definition: node_impl.hpp:86
rclcpp::Client< ServiceT >::SharedPtr create_client(const std::string &service_name, const rmw_qos_profile_t &qos_profile=rmw_qos_profile_services_default, rclcpp::callback_group::CallbackGroup::SharedPtr group=nullptr)
rcl_service_options_t rcl_service_get_default_options(void)
rclcpp::Service< ServiceT >::SharedPtr create_service(const std::string &service_name, CallbackT &&callback, const rmw_qos_profile_t &qos_profile=rmw_qos_profile_services_default, rclcpp::callback_group::CallbackGroup::SharedPtr group=nullptr)
Definition: node_impl.hpp:181
rmw_qos_profile_t qos
Definition: client.hpp:50
Definition: service.hpp:39
Definition: any_service_callback.hpp:31
Definition: parameter.hpp:45
void set(CallbackT callback)
Definition: any_service_callback.hpp:65