25 #include "rclcpp/create_generic_client.hpp"
26 #include "rclcpp/detail/qos_parameters.hpp"
27 #include "rclcpp/exceptions.hpp"
28 #include "rclcpp/graph_listener.hpp"
29 #include "rclcpp/node.hpp"
30 #include "rclcpp/node_interfaces/node_base.hpp"
31 #include "rclcpp/node_interfaces/node_clock.hpp"
32 #include "rclcpp/node_interfaces/node_graph.hpp"
33 #include "rclcpp/node_interfaces/node_logging.hpp"
34 #include "rclcpp/node_interfaces/node_parameters.hpp"
35 #include "rclcpp/node_interfaces/node_services.hpp"
36 #include "rclcpp/node_interfaces/node_time_source.hpp"
37 #include "rclcpp/node_interfaces/node_timers.hpp"
38 #include "rclcpp/node_interfaces/node_topics.hpp"
39 #include "rclcpp/node_interfaces/node_type_descriptions.hpp"
40 #include "rclcpp/node_interfaces/node_waitables.hpp"
41 #include "rclcpp/qos_overriding_options.hpp"
43 #include "rmw/validate_namespace.h"
45 #include "./detail/resolve_parameter_overrides.hpp"
49 using rclcpp::exceptions::throw_from_rcl_error;
56 extend_sub_namespace(
const std::string & existing_sub_namespace,
const std::string & extension)
61 if (extension.empty()) {
65 "sub-nodes should not extend nodes by an empty sub-namespace",
67 }
else if (extension.front() ==
'/') {
72 "a sub-namespace should not have a leading /",
76 std::string new_sub_namespace;
77 if (existing_sub_namespace.empty()) {
78 new_sub_namespace = extension;
80 new_sub_namespace = existing_sub_namespace +
"/" + extension;
84 if (new_sub_namespace.back() ==
'/') {
85 new_sub_namespace = new_sub_namespace.substr(0, new_sub_namespace.size() - 1);
88 return new_sub_namespace;
93 create_effective_namespace(
const std::string & node_namespace,
const std::string & sub_namespace)
101 if (sub_namespace.empty()) {
102 return node_namespace;
103 }
else if (node_namespace.back() ==
'/') {
105 return node_namespace + sub_namespace;
107 return node_namespace +
"/" + sub_namespace;
130 const std::string & node_name,
132 :
Node(node_name,
"", options)
138 get_parameter_events_qos(
145 if (rcl_options->use_global_arguments) {
146 auto context_ptr = node_base.
get_context()->get_rcl_context();
147 global_args = &(context_ptr->global_arguments);
150 auto parameter_overrides = rclcpp::detail::resolve_parameter_overrides(
153 &rcl_options->arguments,
157 auto prefix =
"qos_overrides." + final_topic_name +
".";
158 std::array<rclcpp::QosPolicyKind, 4> policies = {
159 rclcpp::QosPolicyKind::Depth,
160 rclcpp::QosPolicyKind::Durability,
161 rclcpp::QosPolicyKind::History,
162 rclcpp::QosPolicyKind::Reliability,
164 for (
const auto & policy : policies) {
165 auto param_name = prefix + rclcpp::qos_policy_kind_to_cstr(policy);
166 auto it = parameter_overrides.find(param_name);
167 auto value = it != parameter_overrides.end() ?
170 rclcpp::detail::apply_qos_override(policy, value, final_qos);
176 const std::string & node_name,
177 const std::string & namespace_,
179 : node_base_(new
rclcpp::node_interfaces::NodeBase(
183 *(options.get_rcl_node_options()),
184 options.use_intra_process_comms())),
185 node_graph_(new
rclcpp::node_interfaces::NodeGraph(node_base_.get())),
186 node_logging_(new
rclcpp::node_interfaces::NodeLogging(node_base_)),
187 node_timers_(new
rclcpp::node_interfaces::NodeTimers(node_base_.get())),
188 node_topics_(new
rclcpp::node_interfaces::NodeTopics(node_base_.get(), node_timers_.get())),
189 node_services_(new
rclcpp::node_interfaces::NodeServices(node_base_.get())),
190 node_clock_(new
rclcpp::node_interfaces::NodeClock(
198 node_parameters_(new
rclcpp::node_interfaces::NodeParameters(
204 options.parameter_overrides(),
205 options.start_parameter_services(),
206 options.start_parameter_event_publisher(),
209 get_parameter_events_qos(*node_base_, options),
210 options.parameter_event_publisher_options(),
211 options.allow_undeclared_parameters(),
212 options.automatically_declare_parameters_from_overrides()
214 node_time_source_(new
rclcpp::node_interfaces::NodeTimeSource(
223 options.use_clock_thread()
225 node_type_descriptions_(new
rclcpp::node_interfaces::NodeTypeDescriptions(
231 node_waitables_(new
rclcpp::node_interfaces::NodeWaitables(node_base_.get())),
232 node_options_(options),
234 effective_namespace_(create_effective_namespace(this->get_namespace(), sub_namespace_))
238 rclcpp::detail::declare_qos_parameters(
241 QosPolicyKind::Depth,
242 QosPolicyKind::Durability,
243 QosPolicyKind::History,
244 QosPolicyKind::Reliability,
247 node_topics_->resolve_topic_name(
"/parameter_events"),
252 node_logging_->get_logger().set_level(options.
log_level());
255 node_logging_->create_logger_services(node_services_);
261 const std::string & sub_namespace)
262 : node_base_(other.node_base_),
263 node_graph_(other.node_graph_),
264 node_logging_(other.node_logging_),
265 node_timers_(other.node_timers_),
266 node_topics_(other.node_topics_),
267 node_services_(other.node_services_),
268 node_clock_(other.node_clock_),
269 node_parameters_(other.node_parameters_),
270 node_time_source_(other.node_time_source_),
271 node_waitables_(other.node_waitables_),
272 node_options_(other.node_options_),
273 sub_namespace_(extend_sub_namespace(other.get_sub_namespace(), sub_namespace)),
274 effective_namespace_(create_effective_namespace(other.get_namespace(), sub_namespace_)),
275 hidden_impl_(other.hidden_impl_)
278 int validation_result;
279 size_t invalid_index;
281 rmw_validate_namespace(effective_namespace_.c_str(), &validation_result, &invalid_index);
283 if (rmw_ret != RMW_RET_OK) {
284 if (rmw_ret == RMW_RET_INVALID_ARGUMENT) {
287 throw_from_rcl_error(
RCL_RET_ERROR,
"failed to validate subnode namespace");
290 if (validation_result != RMW_NAMESPACE_VALID) {
292 effective_namespace_.c_str(),
293 rmw_namespace_validation_result_string(validation_result),
301 node_waitables_.reset();
302 node_time_source_.reset();
303 node_parameters_.reset();
305 node_services_.reset();
306 node_topics_.reset();
307 node_timers_.reset();
308 node_logging_.reset();
315 return node_base_->get_name();
321 return node_base_->get_namespace();
327 return node_base_->get_fully_qualified_name();
333 return node_logging_->get_logger();
336 rclcpp::CallbackGroup::SharedPtr
338 rclcpp::CallbackGroupType group_type,
339 bool automatically_add_to_executor_with_node)
341 return node_base_->create_callback_group(group_type, automatically_add_to_executor_with_node);
346 const std::string & name,
348 const rcl_interfaces::msg::ParameterDescriptor & parameter_descriptor,
349 bool ignore_override)
351 return this->node_parameters_->declare_parameter(
354 parameter_descriptor,
360 const std::string & name,
361 rclcpp::ParameterType type,
362 const rcl_interfaces::msg::ParameterDescriptor & parameter_descriptor,
363 bool ignore_override)
365 return this->node_parameters_->declare_parameter(
368 parameter_descriptor,
375 this->node_parameters_->undeclare_parameter(name);
381 return this->node_parameters_->has_parameter(name);
384 rcl_interfaces::msg::SetParametersResult
387 return node_parameters_->set_parameters_atomically({parameter});
390 std::vector<rcl_interfaces::msg::SetParametersResult>
393 return node_parameters_->set_parameters(parameters);
396 rcl_interfaces::msg::SetParametersResult
399 return node_parameters_->set_parameters_atomically(parameters);
405 return node_parameters_->get_parameter(name);
411 return node_parameters_->get_parameter(name, parameter);
414 std::vector<rclcpp::Parameter>
416 const std::vector<std::string> & names)
const
418 return node_parameters_->get_parameters(names);
421 rcl_interfaces::msg::ParameterDescriptor
424 auto result = node_parameters_->describe_parameters({name});
425 if (0 == result.size()) {
428 if (result.size() > 1) {
429 throw std::runtime_error(
"number of described parameters unexpectedly more than one");
431 return result.front();
434 std::vector<rcl_interfaces::msg::ParameterDescriptor>
437 return node_parameters_->describe_parameters(names);
443 return node_parameters_->get_parameter_types(names);
446 rcl_interfaces::msg::ListParametersResult
449 return node_parameters_->list_parameters(prefixes, depth);
452 rclcpp::Node::PreSetParametersCallbackHandle::SharedPtr
455 return node_parameters_->add_pre_set_parameters_callback(callback);
458 rclcpp::Node::OnSetParametersCallbackHandle::SharedPtr
461 return node_parameters_->add_on_set_parameters_callback(callback);
464 rclcpp::Node::PostSetParametersCallbackHandle::SharedPtr
467 return node_parameters_->add_post_set_parameters_callback(callback);
473 node_parameters_->remove_pre_set_parameters_callback(handler);
479 node_parameters_->remove_on_set_parameters_callback(handler);
485 node_parameters_->remove_post_set_parameters_callback(handler);
488 std::vector<std::string>
494 std::map<std::string, std::vector<std::string>>
500 std::map<std::string, std::vector<std::string>>
506 std::map<std::string, std::vector<std::string>>
508 const std::string & node_name,
509 const std::string & namespace_)
const
512 node_name, namespace_);
539 std::vector<rclcpp::TopicEndpointInfo>
545 std::vector<rclcpp::TopicEndpointInfo>
551 std::vector<rclcpp::ServiceEndpointInfo>
557 std::vector<rclcpp::ServiceEndpointInfo>
565 const node_interfaces::NodeBaseInterface::CallbackGroupFunction & func)
567 node_base_->for_each_callback_group(func);
570 rclcpp::Event::SharedPtr
578 const rclcpp::Event::SharedPtr & event,
579 std::chrono::nanoseconds timeout)
584 rclcpp::Clock::SharedPtr
587 return node_clock_->get_clock();
590 rclcpp::Clock::ConstSharedPtr
593 return node_clock_->get_clock();
599 return node_clock_->get_clock()->now();
602 rclcpp::node_interfaces::NodeBaseInterface::SharedPtr
608 rclcpp::node_interfaces::NodeClockInterface::SharedPtr
614 rclcpp::node_interfaces::NodeGraphInterface::SharedPtr
620 rclcpp::node_interfaces::NodeLoggingInterface::SharedPtr
623 return node_logging_;
626 rclcpp::node_interfaces::NodeTimeSourceInterface::SharedPtr
629 return node_time_source_;
632 rclcpp::node_interfaces::NodeTimersInterface::SharedPtr
638 rclcpp::node_interfaces::NodeTopicsInterface::SharedPtr
644 rclcpp::node_interfaces::NodeTypeDescriptionsInterface::SharedPtr
647 return node_type_descriptions_;
650 rclcpp::node_interfaces::NodeServicesInterface::SharedPtr
653 return node_services_;
656 rclcpp::node_interfaces::NodeParametersInterface::SharedPtr
659 return node_parameters_;
662 rclcpp::node_interfaces::NodeWaitablesInterface::SharedPtr
665 return node_waitables_;
671 return this->sub_namespace_;
677 return this->effective_namespace_;
685 return std::shared_ptr<Node>(
new Node(*
this, sub_namespace));
691 return this->node_options_;
694 rclcpp::GenericClient::SharedPtr
696 const std::string & service_name,
697 const std::string & service_type,
699 const rclcpp::CallbackGroup::SharedPtr & group)
Internal implementation to provide hidden and API/ABI stable changes to the Node.
@ Unset
The unset log level.
Encapsulation of options for node initialization.
RCLCPP_PUBLIC rclcpp::Logger::Level log_level() const
Return the log_level option.
RCLCPP_PUBLIC const rclcpp::QoS & parameter_event_qos() const
Return a reference to the parameter_event_qos QoS.
RCLCPP_PUBLIC bool enable_logger_service() const
Return the enable_logger_service flag.
RCLCPP_PUBLIC std::vector< rclcpp::Parameter > & parameter_overrides()
Return a reference to the list of parameter overrides.
RCLCPP_PUBLIC const rcl_node_options_t * get_rcl_node_options() const
Return the rcl_node_options used by the node.
Node is the single point of entry for creating publishers and subscribers.
RCLCPP_PUBLIC rclcpp::Logger get_logger() const
Get the logger of the node.
RCLCPP_PUBLIC rclcpp::node_interfaces::NodeWaitablesInterface::SharedPtr get_node_waitables_interface()
Return the Node's internal NodeWaitablesInterface implementation.
RCLCPP_PUBLIC rclcpp::node_interfaces::NodeTopicsInterface::SharedPtr get_node_topics_interface()
Return the Node's internal NodeTopicsInterface implementation.
RCLCPP_PUBLIC std::vector< rclcpp::ServiceEndpointInfo > get_clients_info_by_service(const std::string &service_name, bool no_mangle=false) const
Return the service endpoint information about clients on a given service.
RCLCPP_PUBLIC std::vector< rclcpp::Parameter > get_parameters(const std::vector< std::string > &names) const
Return the parameters by the given parameter names.
RCLCPP_PUBLIC RCUTILS_WARN_UNUSED OnSetParametersCallbackHandle::SharedPtr add_on_set_parameters_callback(OnSetParametersCallbackType callback)
Add a callback to validate parameters before they are set.
RCLCPP_PUBLIC bool has_parameter(const std::string &name) const
Return true if a given parameter is declared.
RCLCPP_PUBLIC rclcpp::Node::SharedPtr create_sub_node(const std::string &sub_namespace)
Create a sub-node, which will extend the namespace of all entities created with it.
RCLCPP_PUBLIC rcl_interfaces::msg::SetParametersResult set_parameter(const rclcpp::Parameter ¶meter)
Set a single parameter.
RCLCPP_PUBLIC rclcpp::Clock::SharedPtr get_clock()
Get a clock as a non-const shared pointer which is managed by the node.
RCLCPP_PUBLIC rclcpp::node_interfaces::NodeTimersInterface::SharedPtr get_node_timers_interface()
Return the Node's internal NodeTimersInterface implementation.
RCLCPP_PUBLIC rclcpp::node_interfaces::NodeParametersInterface::SharedPtr get_node_parameters_interface()
Return the Node's internal NodeParametersInterface implementation.
RCLCPP_PUBLIC std::vector< rclcpp::TopicEndpointInfo > get_publishers_info_by_topic(const std::string &topic_name, bool no_mangle=false) const
Return the topic endpoint information about publishers on a given topic.
RCLCPP_PUBLIC const rclcpp::ParameterValue & declare_parameter(const std::string &name, const rclcpp::ParameterValue &default_value, const rcl_interfaces::msg::ParameterDescriptor ¶meter_descriptor=rcl_interfaces::msg::ParameterDescriptor(), bool ignore_override=false)
Declare and initialize a parameter, return the effective value.
RCLCPP_PUBLIC rclcpp::node_interfaces::NodeLoggingInterface::SharedPtr get_node_logging_interface()
Return the Node's internal NodeLoggingInterface implementation.
RCLCPP_PUBLIC const std::string & get_effective_namespace() const
Return the effective namespace that is used when creating entities.
RCLCPP_PUBLIC size_t count_publishers(const std::string &topic_name) const
Return the number of publishers created for a given topic.
RCLCPP_PUBLIC std::map< std::string, std::vector< std::string > > get_service_names_and_types() const
Return a map of existing service names to list of service types.
RCLCPP_PUBLIC size_t count_subscribers(const std::string &topic_name) const
Return the number of subscribers created for a given topic.
RCLCPP_PUBLIC std::vector< rcl_interfaces::msg::ParameterDescriptor > describe_parameters(const std::vector< std::string > &names) const
Return a vector of parameter descriptors, one for each of the given names.
RCLCPP_PUBLIC Time now() const
Returns current time from the node clock.
RCLCPP_PUBLIC rclcpp::Event::SharedPtr get_graph_event()
Return a graph event, which will be set anytime a graph change occurs.
RCLCPP_PUBLIC void for_each_callback_group(const node_interfaces::NodeBaseInterface::CallbackGroupFunction &func)
Iterate over the callback groups in the node, calling the given function on each valid one.
RCLCPP_PUBLIC rcl_interfaces::msg::ParameterDescriptor describe_parameter(const std::string &name) const
Return the parameter descriptor for the given parameter name.
RCLCPP_PUBLIC std::vector< rclcpp::ServiceEndpointInfo > get_servers_info_by_service(const std::string &service_name, bool no_mangle=false) const
Return the service endpoint information about servers on a given service.
RCLCPP_PUBLIC void remove_on_set_parameters_callback(const OnSetParametersCallbackHandle *const handler)
Remove a callback registered with add_on_set_parameters_callback.
RCLCPP_PUBLIC std::map< std::string, std::vector< std::string > > get_topic_names_and_types() const
Return a map of existing topic names to list of topic types.
RCLCPP_PUBLIC rclcpp::Parameter get_parameter(const std::string &name) const
Return the parameter by the given name.
RCLCPP_PUBLIC Node(const std::string &node_name, const NodeOptions &options=NodeOptions())
Create a new node with the specified name.
RCLCPP_PUBLIC size_t count_services(const std::string &service_name) const
Return the number of services created for a given service.
RCLCPP_PUBLIC const rclcpp::NodeOptions & get_node_options() const
Return the NodeOptions used when creating this node.
RCLCPP_PUBLIC rclcpp::node_interfaces::NodeBaseInterface::SharedPtr get_node_base_interface()
Return the Node's internal NodeBaseInterface implementation.
RCLCPP_PUBLIC rcl_interfaces::msg::SetParametersResult set_parameters_atomically(const std::vector< rclcpp::Parameter > ¶meters)
Set one or more parameters, all at once.
RCLCPP_PUBLIC rclcpp::node_interfaces::NodeClockInterface::SharedPtr get_node_clock_interface()
Return the Node's internal NodeClockInterface implementation.
RCLCPP_PUBLIC void remove_pre_set_parameters_callback(const PreSetParametersCallbackHandle *const handler)
Remove a callback registered with add_pre_set_parameters_callback.
RCLCPP_PUBLIC const char * get_name() const
Get the name of the node.
RCLCPP_PUBLIC std::vector< rcl_interfaces::msg::SetParametersResult > set_parameters(const std::vector< rclcpp::Parameter > ¶meters)
Set one or more parameters, one at a time.
RCLCPP_PUBLIC rclcpp::node_interfaces::NodeTypeDescriptionsInterface::SharedPtr get_node_type_descriptions_interface()
Return the Node's internal NodeTypeDescriptionsInterface implementation.
RCLCPP_PUBLIC rclcpp::node_interfaces::NodeServicesInterface::SharedPtr get_node_services_interface()
Return the Node's internal NodeServicesInterface implementation.
RCLCPP_PUBLIC std::map< std::string, std::vector< std::string > > get_service_names_and_types_by_node(const std::string &node_name, const std::string &namespace_) const
Return a map of existing service names to list of service types for a specific node.
RCLCPP_PUBLIC void undeclare_parameter(const std::string &name)
Undeclare a previously declared parameter.
RCLCPP_PUBLIC std::vector< uint8_t > get_parameter_types(const std::vector< std::string > &names) const
Return a vector of parameter types, one for each of the given names.
RCLCPP_PUBLIC std::vector< rclcpp::TopicEndpointInfo > get_subscriptions_info_by_topic(const std::string &topic_name, bool no_mangle=false) const
Return the topic endpoint information about subscriptions on a given topic.
RCLCPP_PUBLIC rclcpp::CallbackGroup::SharedPtr create_callback_group(rclcpp::CallbackGroupType group_type, bool automatically_add_to_executor_with_node=true)
Create and return a callback group.
RCLCPP_PUBLIC rclcpp::node_interfaces::NodeTimeSourceInterface::SharedPtr get_node_time_source_interface()
Return the Node's internal NodeTimeSourceInterface implementation.
RCLCPP_PUBLIC RCUTILS_WARN_UNUSED PreSetParametersCallbackHandle::SharedPtr add_pre_set_parameters_callback(PreSetParametersCallbackType callback)
Add a callback that gets triggered before parameters are validated.
RCLCPP_PUBLIC rcl_interfaces::msg::ListParametersResult list_parameters(const std::vector< std::string > &prefixes, uint64_t depth) const
Return a list of parameters with any of the given prefixes, up to the given depth.
RCLCPP_PUBLIC size_t count_clients(const std::string &service_name) const
Return the number of clients created for a given service.
RCLCPP_PUBLIC const char * get_fully_qualified_name() const
Get the fully-qualified name of the node.
RCLCPP_PUBLIC const std::string & get_sub_namespace() const
Return the sub-namespace, if this is a sub-node, otherwise an empty string.
RCLCPP_PUBLIC std::vector< std::string > get_node_names() const
Get the fully-qualified names of all available nodes.
RCLCPP_PUBLIC void wait_for_graph_change(const rclcpp::Event::SharedPtr &event, std::chrono::nanoseconds timeout)
Wait for a graph event to occur by waiting on an Event to become set.
RCLCPP_PUBLIC const char * get_namespace() const
Get the namespace of the node.
RCLCPP_PUBLIC RCUTILS_WARN_UNUSED PostSetParametersCallbackHandle::SharedPtr add_post_set_parameters_callback(PostSetParametersCallbackType callback)
Add a callback that gets triggered after parameters are set successfully.
RCLCPP_PUBLIC rclcpp::node_interfaces::NodeGraphInterface::SharedPtr get_node_graph_interface()
Return the Node's internal NodeGraphInterface implementation.
RCLCPP_PUBLIC void remove_post_set_parameters_callback(const PostSetParametersCallbackHandle *const handler)
Remove a callback registered with add_post_set_parameters_callback.
RCLCPP_PUBLIC rclcpp::GenericClient::SharedPtr create_generic_client(const std::string &service_name, const std::string &service_type, const rclcpp::QoS &qos=rclcpp::ServicesQoS(), const rclcpp::CallbackGroup::SharedPtr &group=nullptr)
Create and return a GenericClient.
Store the type and value of a parameter.
Structure to store an arbitrary parameter with templated get/set methods.
Encapsulation of Quality of Service settings.
Options that are passed in subscription/publisher constructor to specify QoSConfigurability.
Thrown when a node namespace is invalid.
Thrown when a any kind of name (node, namespace, topic, etc.) is invalid.
Thrown if parameter is not declared, e.g. either set or get was called without first declaring.
Pure virtual interface class for the NodeBase part of the Node API.
virtual RCLCPP_PUBLIC std::string resolve_topic_or_service_name(const std::string &name, bool is_service, bool only_expand=false) const =0
Expand and remap a given topic or service name.
virtual RCLCPP_PUBLIC const char * get_fully_qualified_name() const =0
Return the fully qualified name of the node.
virtual RCLCPP_PUBLIC rclcpp::Context::SharedPtr get_context()=0
Return the context of the node.
Versions of rosidl_typesupport_cpp::get_message_type_support_handle that handle adapted types.
RCLCPP_PUBLIC rclcpp::GenericClient::SharedPtr create_generic_client(const std::shared_ptr< node_interfaces::NodeBaseInterface > &node_base, const std::shared_ptr< node_interfaces::NodeGraphInterface > &node_graph, const std::shared_ptr< node_interfaces::NodeServicesInterface > &node_services, const std::string &service_name, const std::string &service_type, const rclcpp::QoS &qos=rclcpp::ServicesQoS(), const rclcpp::CallbackGroup::SharedPtr &group=rclcpp::CallbackGroup::SharedPtr())
Create a generic service client with a name of given type.
Hold output of parsing command line arguments.
#define RCL_RET_INVALID_ARGUMENT
Invalid argument return code.
#define RCL_RET_ERROR
Unspecified error return code.