rclcpp  master
C++ ROS Client Library API
node.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_HPP_
16 #define RCLCPP__NODE_HPP_
17 
18 #include <atomic>
19 #include <condition_variable>
20 #include <list>
21 #include <map>
22 #include <memory>
23 #include <mutex>
24 #include <string>
25 #include <tuple>
26 #include <vector>
27 
28 #include "rcl/error_handling.h"
29 #include "rcl/node.h"
30 
31 #include "rcl_interfaces/msg/list_parameters_result.hpp"
32 #include "rcl_interfaces/msg/parameter_descriptor.hpp"
33 #include "rcl_interfaces/msg/parameter_event.hpp"
34 #include "rcl_interfaces/msg/set_parameters_result.hpp"
35 
37 #include "rclcpp/client.hpp"
38 #include "rclcpp/clock.hpp"
39 #include "rclcpp/context.hpp"
40 #include "rclcpp/event.hpp"
41 #include "rclcpp/logger.hpp"
42 #include "rclcpp/macros.hpp"
52 #include "rclcpp/parameter.hpp"
53 #include "rclcpp/publisher.hpp"
54 #include "rclcpp/service.hpp"
55 #include "rclcpp/subscription.hpp"
56 #include "rclcpp/time.hpp"
57 #include "rclcpp/timer.hpp"
59 
60 namespace rclcpp
61 {
62 
64 class Node : public std::enable_shared_from_this<Node>
65 {
66 public:
68 
69 
70 
77  explicit Node(
78  const std::string & node_name,
79  const std::string & namespace_ = "",
80  bool use_intra_process_comms = false);
81 
83 
91  Node(
92  const std::string & node_name,
93  const std::string & namespace_,
94  rclcpp::Context::SharedPtr context,
95  bool use_intra_process_comms = false);
96 
98  virtual ~Node();
99 
101 
103  const char *
104  get_name() const;
105 
107 
109  const char *
110  get_namespace() const;
111 
113 
116  get_logger() const;
117 
120  rclcpp::callback_group::CallbackGroup::SharedPtr
122 
126  get_callback_groups() const;
127 
129 
135  template<
136  typename MessageT, typename Alloc = std::allocator<void>,
137  typename PublisherT = ::rclcpp::Publisher<MessageT, Alloc>>
140  const std::string & topic_name, size_t qos_history_depth,
141  std::shared_ptr<Alloc> allocator = nullptr);
142 
144 
150  template<
151  typename MessageT, typename Alloc = std::allocator<void>,
152  typename PublisherT = ::rclcpp::Publisher<MessageT, Alloc>>
155  const std::string & topic_name,
156  const rmw_qos_profile_t & qos_profile = rmw_qos_profile_default,
157  std::shared_ptr<Alloc> allocator = nullptr);
158 
160 
170  /* TODO(jacquelinekay):
171  Windows build breaks when static member function passed as default
172  argument to msg_mem_strat, nullptr is a workaround.
173  */
174  template<
175  typename MessageT,
176  typename CallbackT,
177  typename Alloc = std::allocator<void>,
178  typename SubscriptionT = rclcpp::Subscription<MessageT, Alloc>>
181  const std::string & topic_name,
182  CallbackT && callback,
183  const rmw_qos_profile_t & qos_profile = rmw_qos_profile_default,
184  rclcpp::callback_group::CallbackGroup::SharedPtr group = nullptr,
185  bool ignore_local_publications = false,
187  msg_mem_strat = nullptr,
188  std::shared_ptr<Alloc> allocator = nullptr);
189 
191 
201  /* TODO(jacquelinekay):
202  Windows build breaks when static member function passed as default
203  argument to msg_mem_strat, nullptr is a workaround.
204  */
205  template<
206  typename MessageT,
207  typename CallbackT,
208  typename Alloc = std::allocator<void>,
209  typename SubscriptionT = rclcpp::Subscription<MessageT, Alloc>>
212  const std::string & topic_name,
213  size_t qos_history_depth,
214  CallbackT && callback,
215  rclcpp::callback_group::CallbackGroup::SharedPtr group = nullptr,
216  bool ignore_local_publications = false,
218  msg_mem_strat = nullptr,
219  std::shared_ptr<Alloc> allocator = nullptr);
220 
222 
227  template<typename DurationT = std::milli, typename CallbackT>
231  CallbackT callback,
232  rclcpp::callback_group::CallbackGroup::SharedPtr group = nullptr);
233 
234  /* Create and return a Client. */
235  template<typename ServiceT>
238  const std::string & service_name,
239  const rmw_qos_profile_t & qos_profile = rmw_qos_profile_services_default,
240  rclcpp::callback_group::CallbackGroup::SharedPtr group = nullptr);
241 
242  /* Create and return a Service. */
243  template<typename ServiceT, typename CallbackT>
246  const std::string & service_name,
247  CallbackT && callback,
248  const rmw_qos_profile_t & qos_profile = rmw_qos_profile_services_default,
249  rclcpp::callback_group::CallbackGroup::SharedPtr group = nullptr);
250 
254 
256  rcl_interfaces::msg::SetParametersResult
258 
259  template<typename ParameterT>
260  void
262  const std::string & name,
263  const ParameterT & value);
264 
267  get_parameters(const std::vector<std::string> & names) const;
268 
271  get_parameter(const std::string & name) const;
272 
274  bool
276  const std::string & name,
277  rclcpp::parameter::ParameterVariant & parameter) const;
278 
280 
287  template<typename ParameterT>
288  bool
289  get_parameter(const std::string & name, ParameterT & parameter) const;
290 
292 
302  template<typename ParameterT>
303  bool
305  const std::string & name,
306  ParameterT & value,
307  const ParameterT & alternative_value) const;
308 
311  describe_parameters(const std::vector<std::string> & names) const;
312 
315  get_parameter_types(const std::vector<std::string> & names) const;
316 
318  rcl_interfaces::msg::ListParametersResult
319  list_parameters(const std::vector<std::string> & prefixes, uint64_t depth) const;
320 
322 
327  template<typename CallbackT>
328  void
329  register_param_change_callback(CallbackT && callback);
330 
334 
338 
340  size_t
341  count_publishers(const std::string & topic_name) const;
342 
344  size_t
345  count_subscribers(const std::string & topic_name) const;
346 
348  /* The graph Event object is a loan which must be returned.
349  * The Event object is scoped and therefore to return the load just let it go
350  * out of scope.
351  */
353  rclcpp::Event::SharedPtr
354  get_graph_event();
355 
357 
365  void
367  rclcpp::Event::SharedPtr event,
368  std::chrono::nanoseconds timeout);
369 
371  rclcpp::Clock::SharedPtr
372  get_clock();
373 
375  Time
376  now();
377 
380  rclcpp::node_interfaces::NodeBaseInterface::SharedPtr
382 
385  rclcpp::node_interfaces::NodeClockInterface::SharedPtr
387 
390  rclcpp::node_interfaces::NodeGraphInterface::SharedPtr
392 
395  rclcpp::node_interfaces::NodeTimersInterface::SharedPtr
397 
400  rclcpp::node_interfaces::NodeTopicsInterface::SharedPtr
402 
405  rclcpp::node_interfaces::NodeServicesInterface::SharedPtr
407 
410  rclcpp::node_interfaces::NodeParametersInterface::SharedPtr
412 
413 private:
415 
417  bool
418  group_in_node(callback_group::CallbackGroup::SharedPtr group);
419 
420  rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_base_;
421  rclcpp::node_interfaces::NodeGraphInterface::SharedPtr node_graph_;
422  rclcpp::node_interfaces::NodeLoggingInterface::SharedPtr node_logging_;
423  rclcpp::node_interfaces::NodeTimersInterface::SharedPtr node_timers_;
424  rclcpp::node_interfaces::NodeTopicsInterface::SharedPtr node_topics_;
425  rclcpp::node_interfaces::NodeServicesInterface::SharedPtr node_services_;
426  rclcpp::node_interfaces::NodeParametersInterface::SharedPtr node_parameters_;
427  rclcpp::node_interfaces::NodeClockInterface::SharedPtr node_clock_;
428 
429  bool use_intra_process_comms_;
430 };
431 
432 } // namespace rclcpp
433 
434 #ifndef RCLCPP__NODE_IMPL_HPP_
435 // Template implementations
436 #include "node_impl.hpp"
437 #endif
438 
439 #endif // RCLCPP__NODE_HPP_
rclcpp::node_interfaces::NodeGraphInterface::SharedPtr get_node_graph_interface()
Return the Node&#39;s internal NodeGraphInterface implementation.
rclcpp::node_interfaces::NodeServicesInterface::SharedPtr get_node_services_interface()
Return the Node&#39;s internal NodeServicesInterface implementation.
Subscription implementation, templated on the type of message this subscription receives.
Definition: subscription.hpp:123
const char * get_name() const
Get the name of the node.
A publisher publishes messages of any type to a topic.
Definition: publisher.hpp:155
Generic timer templated on the clock type. Periodically executes a user-specified callback...
Definition: timer.hpp:105
rclcpp::Event::SharedPtr get_graph_event()
Return a graph event, which will be set anytime a graph change occurs.
#define RCLCPP_DISABLE_COPY(...)
Definition: macros.hpp:26
rcl_interfaces::msg::SetParametersResult set_parameters_atomically(const std::vector< rclcpp::parameter::ParameterVariant > &parameters)
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
rclcpp::node_interfaces::NodeTimersInterface::SharedPtr get_node_timers_interface()
Return the Node&#39;s internal NodeTimersInterface implementation.
const char * get_namespace() const
Get the namespace of the node.
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
rclcpp::node_interfaces::NodeTopicsInterface::SharedPtr get_node_topics_interface()
Return the Node&#39;s internal NodeTopicsInterface implementation.
const std::vector< rclcpp::callback_group::CallbackGroup::WeakPtr > & get_callback_groups() const
Return the list of callback groups in the node.
size_t count_publishers(const std::string &topic_name) const
CallbackGroupType
Definition: callback_group.hpp:43
Definition: client.hpp:118
size_t count_subscribers(const std::string &topic_name) const
rclcpp::Clock::SharedPtr get_clock()
Definition: allocator_common.hpp:24
Definition: logger.hpp:63
std::vector< rcl_interfaces::msg::ParameterDescriptor > describe_parameters(const std::vector< std::string > &names) const
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)
std::vector< uint8_t > get_parameter_types(const std::vector< std::string > &names) const
std::vector< rclcpp::parameter::ParameterVariant > get_parameters(const std::vector< std::string > &names) const
void register_param_change_callback(CallbackT &&callback)
Register the callback for parameter changes.
Definition: node_impl.hpp:203
#define RCLCPP_SMART_PTR_DEFINITIONS(...)
Definition: macros.hpp:36
rclcpp::parameter::ParameterVariant get_parameter(const std::string &name) const
std::map< std::string, std::vector< std::string > > get_topic_names_and_types() const
rclcpp::Logger get_logger() const
Get the logger of the node.
virtual ~Node()
Definition: service.hpp:93
void wait_for_graph_change(rclcpp::Event::SharedPtr event, std::chrono::nanoseconds timeout)
Wait for a graph event to occur by waiting on an Event to become set.
Default allocation strategy for messages received by subscriptions.
Definition: message_memory_strategy.hpp:33
Node is the single point of entry for creating publishers and subscribers.
Definition: node.hpp:64
#define RCLCPP_PUBLIC
Definition: visibility_control.hpp:50
rclcpp::node_interfaces::NodeParametersInterface::SharedPtr get_node_parameters_interface()
Return the Node&#39;s internal NodeParametersInterface implementation.
Node(const std::string &node_name, const std::string &namespace_="", bool use_intra_process_comms=false)
Create a new node with the specified name.
rclcpp::node_interfaces::NodeBaseInterface::SharedPtr get_node_base_interface()
Return the Node&#39;s internal NodeBaseInterface implementation.
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::callback_group::CallbackGroup::SharedPtr create_callback_group(rclcpp::callback_group::CallbackGroupType group_type)
Create and return a callback group.
std::map< std::string, std::vector< std::string > > get_service_names_and_types() const
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_interfaces::msg::ListParametersResult list_parameters(const std::vector< std::string > &prefixes, uint64_t depth) const
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
rclcpp::node_interfaces::NodeClockInterface::SharedPtr get_node_clock_interface()
Return the Node&#39;s internal NodeClockInterface implementation.
Definition: time.hpp:31
Definition: parameter.hpp:45