15 #ifndef RCLCPP__NODE_IMPL_HPP_
16 #define RCLCPP__NODE_IMPL_HPP_
30 #include "rclcpp/contexts/default_context.hpp"
31 #include "rclcpp/create_client.hpp"
32 #include "rclcpp/create_generic_publisher.hpp"
33 #include "rclcpp/create_generic_subscription.hpp"
34 #include "rclcpp/create_publisher.hpp"
35 #include "rclcpp/create_service.hpp"
36 #include "rclcpp/create_generic_service.hpp"
37 #include "rclcpp/create_subscription.hpp"
38 #include "rclcpp/create_timer.hpp"
39 #include "rclcpp/detail/resolve_enable_topic_statistics.hpp"
40 #include "rclcpp/parameter.hpp"
41 #include "rclcpp/qos.hpp"
42 #include "rclcpp/timer.hpp"
43 #include "rclcpp/type_support_decl.hpp"
44 #include "rclcpp/visibility_control.hpp"
46 #ifndef RCLCPP__NODE_HPP_
56 extend_name_with_sub_namespace(
const std::string & name,
const std::string & sub_namespace)
58 std::string name_with_sub_namespace(name);
59 if (sub_namespace !=
"" && name.front() !=
'/' && name.front() !=
'~') {
60 name_with_sub_namespace = sub_namespace +
"/" + name;
62 return name_with_sub_namespace;
65 template<
typename MessageT,
typename AllocatorT,
typename PublisherT>
66 std::shared_ptr<PublisherT>
68 const std::string & topic_name,
72 return rclcpp::create_publisher<MessageT, AllocatorT, PublisherT>(
83 typename SubscriptionT,
84 typename MessageMemoryStrategyT>
85 std::shared_ptr<SubscriptionT>
87 const std::string & topic_name,
89 CallbackT && callback,
91 typename MessageMemoryStrategyT::SharedPtr msg_mem_strat)
93 return rclcpp::create_subscription<MessageT>(
97 std::forward<CallbackT>(callback),
102 template<
typename DurationRepT,
typename DurationT,
typename CallbackT>
105 std::chrono::duration<DurationRepT, DurationT> period,
107 const rclcpp::CallbackGroup::SharedPtr & group,
114 this->node_base_.get(),
115 this->node_timers_.get(),
119 template<
typename DurationRepT,
typename DurationT,
typename CallbackT>
122 std::chrono::duration<DurationRepT, DurationT> period,
124 const rclcpp::CallbackGroup::SharedPtr & group)
131 this->node_base_.get(),
132 this->node_timers_.get());
135 template<
typename ServiceT>
138 const std::string & service_name,
140 const rclcpp::CallbackGroup::SharedPtr & group)
142 return rclcpp::create_client<ServiceT>(
151 template<
typename ServiceT,
typename CallbackT>
154 const std::string & service_name,
155 CallbackT && callback,
157 const rclcpp::CallbackGroup::SharedPtr & group)
159 return rclcpp::create_service<ServiceT, CallbackT>(
163 std::forward<CallbackT>(callback),
168 template<
typename CallbackT>
169 typename rclcpp::GenericService::SharedPtr
171 const std::string & service_name,
172 const std::string & service_type,
173 CallbackT && callback,
175 const rclcpp::CallbackGroup::SharedPtr & group)
177 return rclcpp::create_generic_service<CallbackT>(
182 std::forward<CallbackT>(callback),
187 template<
typename AllocatorT>
188 std::shared_ptr<rclcpp::GenericPublisher>
190 const std::string & topic_name,
191 const std::string & topic_type,
204 template<
typename CallbackT,
typename AllocatorT>
205 std::shared_ptr<rclcpp::GenericSubscription>
207 const std::string & topic_name,
208 const std::string & topic_type,
210 CallbackT && callback,
218 std::forward<CallbackT>(callback),
224 template<
typename ParameterT>
227 const std::string & name,
228 const ParameterT & default_value,
229 const rcl_interfaces::msg::ParameterDescriptor & parameter_descriptor,
230 bool ignore_override)
236 parameter_descriptor,
244 template<
typename ParameterT>
247 const std::string & name,
248 const rcl_interfaces::msg::ParameterDescriptor & parameter_descriptor,
249 bool ignore_override)
258 parameter_descriptor,
266 template<
typename ParameterT>
267 std::vector<ParameterT>
269 const std::string & namespace_,
270 const std::map<std::string, ParameterT> & parameters,
271 bool ignore_overrides)
273 std::vector<ParameterT> result;
274 std::string normalized_namespace = namespace_.empty() ?
"" : (namespace_ +
".");
276 parameters.begin(), parameters.end(), std::back_inserter(result),
277 [
this, &normalized_namespace, ignore_overrides](
auto element) {
278 return this->declare_parameter(
279 normalized_namespace + element.first,
281 rcl_interfaces::msg::ParameterDescriptor(),
288 template<
typename ParameterT>
289 std::vector<ParameterT>
291 const std::string & namespace_,
294 std::pair<ParameterT, rcl_interfaces::msg::ParameterDescriptor>
296 bool ignore_overrides)
298 std::vector<ParameterT> result;
299 std::string normalized_namespace = namespace_.empty() ?
"" : (namespace_ +
".");
301 parameters.begin(), parameters.end(), std::back_inserter(result),
302 [
this, &normalized_namespace, ignore_overrides](
auto element) {
303 return static_cast<ParameterT>(
304 this->declare_parameter(
305 normalized_namespace + element.first,
306 element.second.first,
307 element.second.second,
315 template<
typename ParameterT>
323 parameter =
static_cast<ParameterT
>(parameter_variant.
get_value<ParameterT>());
329 template<
typename ParameterT>
332 const std::string & name,
333 ParameterT & parameter,
334 const ParameterT & alternative_value)
const
337 if (!got_parameter) {
338 parameter = alternative_value;
340 return got_parameter;
343 template<
typename ParameterT>
346 const std::string & name,
347 const ParameterT & alternative_value)
const
349 ParameterT parameter;
357 template<
typename ParameterT>
360 const std::string & prefix,
361 std::map<std::string, ParameterT> & values)
const
363 std::map<std::string, rclcpp::Parameter> params;
364 bool result = node_parameters_->get_parameters_by_prefix(prefix, params);
366 for (
const auto & param : params) {
367 values[param.first] =
static_cast<ParameterT
>(param.second.get_value<ParameterT>());
Generic timer. Periodically executes a user-specified callback.
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 rclcpp::Clock::SharedPtr get_clock()
Get a clock as a non-const shared pointer which is managed by the node.
rclcpp::GenericService::SharedPtr create_generic_service(const std::string &service_name, const std::string &service_type, CallbackT &&callback, const rclcpp::QoS &qos=rclcpp::ServicesQoS(), const rclcpp::CallbackGroup::SharedPtr &group=nullptr)
Create and return a GenericService.
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.
bool get_parameter_or(const std::string &name, ParameterT ¶meter, const ParameterT &alternative_value) const
Get the parameter value, or the "alternative_value" if not set, and assign it to "parameter".
std::vector< ParameterT > declare_parameters(const std::string &namespace_, const std::map< std::string, ParameterT > ¶meters, bool ignore_overrides=false)
Declare and initialize several parameters with the same namespace and type.
RCLCPP_PUBLIC rclcpp::Parameter get_parameter(const std::string &name) const
Return the parameter by the given name.
rclcpp::GenericTimer< CallbackT >::SharedPtr create_timer(std::chrono::duration< DurationRepT, DurationT > period, CallbackT callback, const rclcpp::CallbackGroup::SharedPtr &group=nullptr)
Create a timer that uses the node clock to drive the callback.
std::shared_ptr< rclcpp::GenericSubscription > create_generic_subscription(const std::string &topic_name, const std::string &topic_type, const rclcpp::QoS &qos, CallbackT &&callback, const rclcpp::SubscriptionOptionsWithAllocator< AllocatorT > &options=(rclcpp::SubscriptionOptionsWithAllocator< AllocatorT >()))
Create and return a GenericSubscription.
std::shared_ptr< SubscriptionT > create_subscription(const std::string &topic_name, const rclcpp::QoS &qos, CallbackT &&callback, const SubscriptionOptionsWithAllocator< AllocatorT > &options=SubscriptionOptionsWithAllocator< AllocatorT >(), typename MessageMemoryStrategyT::SharedPtr msg_mem_strat=(MessageMemoryStrategyT::create_default()))
Create and return a Subscription.
std::shared_ptr< PublisherT > create_publisher(const std::string &topic_name, const rclcpp::QoS &qos, const PublisherOptionsWithAllocator< AllocatorT > &options=PublisherOptionsWithAllocator< AllocatorT >())
Create and return a Publisher.
rclcpp::WallTimer< CallbackT >::SharedPtr create_wall_timer(std::chrono::duration< DurationRepT, DurationT > period, CallbackT callback, const rclcpp::CallbackGroup::SharedPtr &group=nullptr, bool autostart=true)
Create a wall timer that uses the wall clock to drive the callback.
RCLCPP_PUBLIC const std::string & get_sub_namespace() const
Return the sub-namespace, if this is a sub-node, otherwise an empty string.
rclcpp::Client< ServiceT >::SharedPtr create_client(const std::string &service_name, const rclcpp::QoS &qos=rclcpp::ServicesQoS(), const rclcpp::CallbackGroup::SharedPtr &group=nullptr)
Create and return a Client.
std::shared_ptr< rclcpp::GenericPublisher > create_generic_publisher(const std::string &topic_name, const std::string &topic_type, const rclcpp::QoS &qos, const rclcpp::PublisherOptionsWithAllocator< AllocatorT > &options=(rclcpp::PublisherOptionsWithAllocator< AllocatorT >()))
Create and return a GenericPublisher.
rclcpp::Service< ServiceT >::SharedPtr create_service(const std::string &service_name, CallbackT &&callback, const rclcpp::QoS &qos=rclcpp::ServicesQoS(), const rclcpp::CallbackGroup::SharedPtr &group=nullptr)
Create and return a Service.
Indicate the parameter type does not match the expected type.
Store the type and value of a parameter.
Structure to store an arbitrary parameter with templated get/set methods.
decltype(auto) get_value() const
Get value of parameter using rclcpp::ParameterType as template argument.
Encapsulation of Quality of Service settings.
Thrown if requested parameter type is invalid.
Thrown if user attempts to create an uninitialized statically typed parameter.
Versions of rosidl_typesupport_cpp::get_message_type_support_handle that handle adapted types.
rclcpp::TimerBase::SharedPtr create_timer(std::shared_ptr< node_interfaces::NodeBaseInterface > node_base, std::shared_ptr< node_interfaces::NodeTimersInterface > node_timers, rclcpp::Clock::SharedPtr clock, rclcpp::Duration period, CallbackT &&callback, rclcpp::CallbackGroup::SharedPtr group=nullptr, bool autostart=true)
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< GenericPublisher > create_generic_publisher(rclcpp::node_interfaces::NodeTopicsInterface::SharedPtr topics_interface, const std::string &topic_name, const std::string &topic_type, const rclcpp::QoS &qos, const rclcpp::PublisherOptionsWithAllocator< AllocatorT > &options=(rclcpp::PublisherOptionsWithAllocator< AllocatorT >()))
Create and return a GenericPublisher.
std::shared_ptr< GenericSubscription > create_generic_subscription(rclcpp::node_interfaces::NodeTopicsInterface::SharedPtr topics_interface, const std::string &topic_name, const std::string &topic_type, const rclcpp::QoS &qos, CallbackT &&callback, const rclcpp::SubscriptionOptionsWithAllocator< AllocatorT > &options=(rclcpp::SubscriptionOptionsWithAllocator< AllocatorT >()))
Create and return a GenericSubscription.
Structure containing optional configuration for Publishers.
Structure containing optional configuration for Subscriptions.