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"
54 #include "rclcpp/parameter.hpp"
55 #include "rclcpp/publisher.hpp"
56 #include "rclcpp/service.hpp"
57 #include "rclcpp/subscription.hpp"
59 #include "rclcpp/time.hpp"
60 #include "rclcpp/timer.hpp"
62 
63 namespace rclcpp
64 {
65 
67 class Node : public std::enable_shared_from_this<Node>
68 {
69 public:
71 
72 
73 
80  explicit Node(
81  const std::string & node_name,
82  const std::string & namespace_ = "",
83  bool use_intra_process_comms = false);
84 
86 
100  Node(
101  const std::string & node_name,
102  const std::string & namespace_,
103  rclcpp::Context::SharedPtr context,
104  const std::vector<std::string> & arguments,
105  const std::vector<Parameter> & initial_parameters,
106  bool use_global_arguments = true,
107  bool use_intra_process_comms = false,
108  bool start_parameter_services = true);
109 
111  virtual ~Node();
112 
114 
116  const char *
117  get_name() const;
118 
120 
122  const char *
123  get_namespace() const;
124 
126 
129  get_logger() const;
130 
133  rclcpp::callback_group::CallbackGroup::SharedPtr
135 
139  get_callback_groups() const;
140 
142 
148  template<
149  typename MessageT, typename Alloc = std::allocator<void>,
150  typename PublisherT = ::rclcpp::Publisher<MessageT, Alloc>>
153  const std::string & topic_name, size_t qos_history_depth,
154  std::shared_ptr<Alloc> allocator = nullptr);
155 
157 
163  template<
164  typename MessageT, typename Alloc = std::allocator<void>,
165  typename PublisherT = ::rclcpp::Publisher<MessageT, Alloc>>
168  const std::string & topic_name,
169  const rmw_qos_profile_t & qos_profile = rmw_qos_profile_default,
170  std::shared_ptr<Alloc> allocator = nullptr);
171 
173 
183  /* TODO(jacquelinekay):
184  Windows build breaks when static member function passed as default
185  argument to msg_mem_strat, nullptr is a workaround.
186  */
187  template<
188  typename MessageT,
189  typename CallbackT,
190  typename Alloc = std::allocator<void>,
191  typename SubscriptionT = rclcpp::Subscription<
195  const std::string & topic_name,
196  CallbackT && callback,
197  const rmw_qos_profile_t & qos_profile = rmw_qos_profile_default,
198  rclcpp::callback_group::CallbackGroup::SharedPtr group = nullptr,
199  bool ignore_local_publications = false,
201  typename rclcpp::subscription_traits::has_message_type<CallbackT>::type, Alloc>::SharedPtr
202  msg_mem_strat = nullptr,
203  std::shared_ptr<Alloc> allocator = nullptr);
204 
206 
216  /* TODO(jacquelinekay):
217  Windows build breaks when static member function passed as default
218  argument to msg_mem_strat, nullptr is a workaround.
219  */
220  template<
221  typename MessageT,
222  typename CallbackT,
223  typename Alloc = std::allocator<void>,
224  typename SubscriptionT = rclcpp::Subscription<
225  typename rclcpp::subscription_traits::has_message_type<CallbackT>::type, Alloc>>
228  const std::string & topic_name,
229  CallbackT && callback,
230  size_t qos_history_depth,
231  rclcpp::callback_group::CallbackGroup::SharedPtr group = nullptr,
232  bool ignore_local_publications = false,
234  typename rclcpp::subscription_traits::has_message_type<CallbackT>::type, Alloc>::SharedPtr
235  msg_mem_strat = nullptr,
236  std::shared_ptr<Alloc> allocator = nullptr);
237 
239 
244  template<typename DurationT = std::milli, typename CallbackT>
248  CallbackT callback,
249  rclcpp::callback_group::CallbackGroup::SharedPtr group = nullptr);
250 
251  /* Create and return a Client. */
252  template<typename ServiceT>
255  const std::string & service_name,
256  const rmw_qos_profile_t & qos_profile = rmw_qos_profile_services_default,
257  rclcpp::callback_group::CallbackGroup::SharedPtr group = nullptr);
258 
259  /* Create and return a Service. */
260  template<typename ServiceT, typename CallbackT>
263  const std::string & service_name,
264  CallbackT && callback,
265  const rmw_qos_profile_t & qos_profile = rmw_qos_profile_services_default,
266  rclcpp::callback_group::CallbackGroup::SharedPtr group = nullptr);
267 
271 
273  rcl_interfaces::msg::SetParametersResult
275 
276  template<typename ParameterT>
277  void
279  const std::string & name,
280  const ParameterT & value);
281 
283 
290  template<typename MapValueT>
291  void
293  const std::string & name,
294  const std::map<std::string, MapValueT> & values);
295 
298  get_parameters(const std::vector<std::string> & names) const;
299 
302  get_parameter(const std::string & name) const;
303 
305  bool
307  const std::string & name,
308  rclcpp::Parameter & parameter) const;
309 
311 
318  template<typename ParameterT>
319  bool
320  get_parameter(const std::string & name, ParameterT & parameter) const;
321 
323 
334  template<typename MapValueT>
335  bool
337  const std::string & name,
338  std::map<std::string, MapValueT> & values) const;
339 
341 
351  template<typename ParameterT>
352  bool
354  const std::string & name,
355  ParameterT & value,
356  const ParameterT & alternative_value) const;
357 
359 
369  template<typename ParameterT>
370  void
372  const std::string & name,
373  ParameterT & value,
374  const ParameterT & alternative_value);
375 
378  describe_parameters(const std::vector<std::string> & names) const;
379 
382  get_parameter_types(const std::vector<std::string> & names) const;
383 
385  rcl_interfaces::msg::ListParametersResult
386  list_parameters(const std::vector<std::string> & prefixes, uint64_t depth) const;
387 
389 
394  template<typename CallbackT>
395  void
396  register_param_change_callback(CallbackT && callback);
397 
400  get_node_names() const;
401 
405 
409 
411  size_t
412  count_publishers(const std::string & topic_name) const;
413 
415  size_t
416  count_subscribers(const std::string & topic_name) const;
417 
419  /* The graph Event object is a loan which must be returned.
420  * The Event object is scoped and therefore to return the loan just let it go
421  * out of scope.
422  */
424  rclcpp::Event::SharedPtr
425  get_graph_event();
426 
428 
436  void
438  rclcpp::Event::SharedPtr event,
439  std::chrono::nanoseconds timeout);
440 
442  rclcpp::Clock::SharedPtr
443  get_clock();
444 
446  Time
447  now();
448 
451  rclcpp::node_interfaces::NodeBaseInterface::SharedPtr
453 
456  rclcpp::node_interfaces::NodeClockInterface::SharedPtr
458 
461  rclcpp::node_interfaces::NodeGraphInterface::SharedPtr
463 
466  rclcpp::node_interfaces::NodeLoggingInterface::SharedPtr
468 
471  rclcpp::node_interfaces::NodeTimersInterface::SharedPtr
473 
476  rclcpp::node_interfaces::NodeTopicsInterface::SharedPtr
478 
481  rclcpp::node_interfaces::NodeServicesInterface::SharedPtr
483 
486  rclcpp::node_interfaces::NodeWaitablesInterface::SharedPtr
488 
491  rclcpp::node_interfaces::NodeParametersInterface::SharedPtr
493 
496  rclcpp::node_interfaces::NodeTimeSourceInterface::SharedPtr
498 
499 private:
500  RCLCPP_DISABLE_COPY(Node)
501 
503  bool
504  group_in_node(callback_group::CallbackGroup::SharedPtr group);
505 
506  rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_base_;
507  rclcpp::node_interfaces::NodeGraphInterface::SharedPtr node_graph_;
508  rclcpp::node_interfaces::NodeLoggingInterface::SharedPtr node_logging_;
509  rclcpp::node_interfaces::NodeTimersInterface::SharedPtr node_timers_;
510  rclcpp::node_interfaces::NodeTopicsInterface::SharedPtr node_topics_;
511  rclcpp::node_interfaces::NodeServicesInterface::SharedPtr node_services_;
512  rclcpp::node_interfaces::NodeClockInterface::SharedPtr node_clock_;
513  rclcpp::node_interfaces::NodeParametersInterface::SharedPtr node_parameters_;
514  rclcpp::node_interfaces::NodeTimeSourceInterface::SharedPtr node_time_source_;
515  rclcpp::node_interfaces::NodeWaitablesInterface::SharedPtr node_waitables_;
516 
517  bool use_intra_process_comms_;
518 };
519 
520 } // namespace rclcpp
521 
522 #ifndef RCLCPP__NODE_IMPL_HPP_
523 // Template implementations
524 #include "node_impl.hpp"
525 #endif
526 
527 #endif // RCLCPP__NODE_HPP_
void register_param_change_callback(CallbackT &&callback)
Register the callback for parameter changes.
Definition: node_impl.hpp:208
rclcpp::node_interfaces::NodeServicesInterface::SharedPtr get_node_services_interface()
Return the Node&#39;s internal NodeServicesInterface implementation.
rclcpp::node_interfaces::NodeTimersInterface::SharedPtr get_node_timers_interface()
Return the Node&#39;s internal NodeTimersInterface implementation.
Definition: logger.hpp:77
Default allocation strategy for messages received by subscriptions.
Definition: message_memory_strategy.hpp:40
std::vector< uint8_t > get_parameter_types(const std::vector< std::string > &names) const
rclcpp::callback_group::CallbackGroup::SharedPtr create_callback_group(rclcpp::callback_group::CallbackGroupType group_type)
Create and return a callback group.
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)
#define RCLCPP_DISABLE_COPY(...)
Definition: macros.hpp:26
std::vector< rcl_interfaces::msg::ParameterDescriptor > describe_parameters(const std::vector< std::string > &names) const
rclcpp::node_interfaces::NodeBaseInterface::SharedPtr get_node_base_interface()
Return the Node&#39;s internal NodeBaseInterface implementation.
Structure to store an arbitrary parameter with templated get/set methods.
Definition: parameter.hpp:32
Definition: timer.hpp:192
rclcpp::Clock::SharedPtr get_clock()
std::vector< std::string > get_node_names() const
virtual ~Node()
CallbackGroupType
Definition: callback_group.hpp:45
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:156
size_t count_subscribers(const std::string &topic_name) const
Definition: allocator_common.hpp:24
rclcpp::Event::SharedPtr get_graph_event()
Return a graph event, which will be set anytime a graph change occurs.
Definition: service.hpp:88
Definition: client.hpp:119
void get_parameter_or_set(const std::string &name, ParameterT &value, const ParameterT &alternative_value)
Get the parameter value; if not set, set the "alternative value" and store it in the node...
Definition: node_impl.hpp:300
rclcpp::Logger get_logger() const
Get the logger of the node.
Subscription implementation, templated on the type of message this subscription receives.
Definition: subscription.hpp:148
rclcpp::node_interfaces::NodeLoggingInterface::SharedPtr get_node_logging_interface()
Return the Node&#39;s internal NodeLoggingInterface implementation.
void set_parameters_if_not_set(const std::string &name, const std::map< std::string, MapValueT > &values)
Set a map of parameters with the same prefix.
Definition: node_impl.hpp:232
typename std::remove_cv< rclcpp::function_traits::function_traits< CallbackT >::template argument_type< 0 > >::type type
Definition: subscription_traits.hpp:61
#define RCLCPP_SMART_PTR_DEFINITIONS(...)
Definition: macros.hpp:36
std::vector< rcl_interfaces::msg::SetParametersResult > set_parameters(const std::vector< rclcpp::Parameter > &parameters)
Node(const std::string &node_name, const std::string &namespace_="", bool use_intra_process_comms=false)
Create a new node with the specified name.
A publisher publishes messages of any type to a topic.
Definition: publisher.hpp:155
rclcpp::node_interfaces::NodeTopicsInterface::SharedPtr get_node_topics_interface()
Return the Node&#39;s internal NodeTopicsInterface implementation.
void set_parameter_if_not_set(const std::string &name, const ParameterT &value)
Definition: node_impl.hpp:215
#define RCLCPP_PUBLIC
Definition: visibility_control.hpp:50
size_t count_publishers(const std::string &topic_name) const
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:56
rclcpp::node_interfaces::NodeTimeSourceInterface::SharedPtr get_node_time_source_interface()
Return the Node&#39;s internal NodeParametersInterface implementation.
std::map< std::string, std::vector< std::string > > get_topic_names_and_types() const
rclcpp::node_interfaces::NodeClockInterface::SharedPtr get_node_clock_interface()
Return the Node&#39;s internal NodeClockInterface implementation.
rclcpp::node_interfaces::NodeGraphInterface::SharedPtr get_node_graph_interface()
Return the Node&#39;s internal NodeGraphInterface implementation.
rclcpp::node_interfaces::NodeParametersInterface::SharedPtr get_node_parameters_interface()
Return the Node&#39;s internal NodeParametersInterface implementation.
rclcpp::Parameter get_parameter(const std::string &name) const
Definition: time.hpp:31
rcl_interfaces::msg::SetParametersResult set_parameters_atomically(const std::vector< rclcpp::Parameter > &parameters)
const char * get_name() const
Get the name of the node.
const char * get_namespace() const
Get the namespace of the node.
const std::vector< rclcpp::callback_group::CallbackGroup::WeakPtr > & get_callback_groups() const
Return the list of callback groups in the node.
std::vector< rclcpp::Parameter > get_parameters(const std::vector< std::string > &names) const
std::map< std::string, std::vector< std::string > > get_service_names_and_types() const
Node is the single point of entry for creating publishers and subscribers.
Definition: node.hpp:67
rcl_interfaces::msg::ListParametersResult list_parameters(const std::vector< std::string > &prefixes, uint64_t depth) const
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.
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:195
rclcpp::node_interfaces::NodeWaitablesInterface::SharedPtr get_node_waitables_interface()
Return the Node&#39;s internal NodeWaitablesInterface 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< typename rclcpp::subscription_traits::has_message_type< CallbackT >::type, Alloc >::SharedPtr msg_mem_strat=nullptr, std::shared_ptr< Alloc > allocator=nullptr)
Create and return a Subscription.
Definition: node_impl.hpp:91
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:286