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/parameter.hpp"
40 #include "rclcpp/qos.hpp"
41 #include "rclcpp/timer.hpp"
42 #include "rclcpp/type_support_decl.hpp"
43 #include "rclcpp/visibility_control.hpp"
45 #ifndef RCLCPP__NODE_HPP_
55 extend_name_with_sub_namespace(
const std::string & name,
const std::string & sub_namespace)
57 std::string name_with_sub_namespace(name);
58 if (sub_namespace !=
"" && name.front() !=
'/' && name.front() !=
'~') {
59 name_with_sub_namespace = sub_namespace +
"/" + name;
61 return name_with_sub_namespace;
64 template<
typename MessageT,
typename AllocatorT,
typename PublisherT>
65 std::shared_ptr<PublisherT>
67 const std::string & topic_name,
71 return rclcpp::create_publisher<MessageT, AllocatorT, PublisherT>(
82 typename SubscriptionT,
83 typename MessageMemoryStrategyT>
84 std::shared_ptr<SubscriptionT>
86 const std::string & topic_name,
88 CallbackT && callback,
90 typename MessageMemoryStrategyT::SharedPtr msg_mem_strat)
92 return rclcpp::create_subscription<MessageT>(
96 std::forward<CallbackT>(callback),
101 template<
typename DurationRepT,
typename DurationT,
typename CallbackT>
104 std::chrono::duration<DurationRepT, DurationT> period,
106 const rclcpp::CallbackGroup::SharedPtr & group,
113 this->node_base_.get(),
114 this->node_timers_.get(),
118 template<
typename DurationRepT,
typename DurationT,
typename CallbackT>
121 std::chrono::duration<DurationRepT, DurationT> period,
123 const rclcpp::CallbackGroup::SharedPtr & group)
130 this->node_base_.get(),
131 this->node_timers_.get());
134 template<
typename ServiceT>
137 const std::string & service_name,
139 const rclcpp::CallbackGroup::SharedPtr & group)
141 return rclcpp::create_client<ServiceT>(
150 template<
typename ServiceT,
typename CallbackT>
153 const std::string & service_name,
154 CallbackT && callback,
156 const rclcpp::CallbackGroup::SharedPtr & group)
158 return rclcpp::create_service<ServiceT, CallbackT>(
162 std::forward<CallbackT>(callback),
167 template<
typename CallbackT>
168 typename rclcpp::GenericService::SharedPtr
170 const std::string & service_name,
171 const std::string & service_type,
172 CallbackT && callback,
174 const rclcpp::CallbackGroup::SharedPtr & group)
176 return rclcpp::create_generic_service<CallbackT>(
181 std::forward<CallbackT>(callback),
186 template<
typename AllocatorT>
187 std::shared_ptr<rclcpp::GenericPublisher>
189 const std::string & topic_name,
190 const std::string & topic_type,
203 template<
typename CallbackT,
typename AllocatorT>
204 std::shared_ptr<rclcpp::GenericSubscription>
206 const std::string & topic_name,
207 const std::string & topic_type,
209 CallbackT && callback,
217 std::forward<CallbackT>(callback),
223 template<
typename ParameterT>
226 const std::string & name,
227 const ParameterT & default_value,
228 const rcl_interfaces::msg::ParameterDescriptor & parameter_descriptor,
229 bool ignore_override)
235 parameter_descriptor,
243 template<
typename ParameterT>
246 const std::string & name,
247 const rcl_interfaces::msg::ParameterDescriptor & parameter_descriptor,
248 bool ignore_override)
257 parameter_descriptor,
265 template<
typename ParameterT>
266 std::vector<ParameterT>
268 const std::string & namespace_,
269 const std::map<std::string, ParameterT> & parameters,
270 bool ignore_overrides)
272 std::vector<ParameterT> result;
273 std::string normalized_namespace = namespace_.empty() ?
"" : (namespace_ +
".");
275 parameters.begin(), parameters.end(), std::back_inserter(result),
276 [
this, &normalized_namespace, ignore_overrides](
auto element) {
277 return this->declare_parameter(
278 normalized_namespace + element.first,
280 rcl_interfaces::msg::ParameterDescriptor(),
287 template<
typename ParameterT>
288 std::vector<ParameterT>
290 const std::string & namespace_,
293 std::pair<ParameterT, rcl_interfaces::msg::ParameterDescriptor>
295 bool ignore_overrides)
297 std::vector<ParameterT> result;
298 std::string normalized_namespace = namespace_.empty() ?
"" : (namespace_ +
".");
300 parameters.begin(), parameters.end(), std::back_inserter(result),
301 [
this, &normalized_namespace, ignore_overrides](
auto element) {
302 return static_cast<ParameterT>(
303 this->declare_parameter(
304 normalized_namespace + element.first,
305 element.second.first,
306 element.second.second,
314 template<
typename ParameterT>
322 parameter =
static_cast<ParameterT
>(parameter_variant.
get_value<ParameterT>());
328 template<
typename ParameterT>
331 const std::string & name,
332 ParameterT & parameter,
333 const ParameterT & alternative_value)
const
336 if (!got_parameter) {
337 parameter = alternative_value;
339 return got_parameter;
342 template<
typename ParameterT>
345 const std::string & name,
346 const ParameterT & alternative_value)
const
348 ParameterT parameter;
356 template<
typename ParameterT>
359 const std::string & prefix,
360 std::map<std::string, ParameterT> & values)
const
362 std::map<std::string, rclcpp::Parameter> params;
363 bool result = node_parameters_->get_parameters_by_prefix(prefix, params);
365 for (
const auto & param : params) {
366 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.