rclcpp  master
C++ ROS Client Library API
parameter_client.hpp
Go to the documentation of this file.
1 // Copyright 2015 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__PARAMETER_CLIENT_HPP_
16 #define RCLCPP__PARAMETER_CLIENT_HPP_
17 
18 #include <memory>
19 #include <string>
20 #include <utility>
21 #include <vector>
22 
23 #include "rcl_interfaces/msg/parameter.hpp"
24 #include "rcl_interfaces/msg/parameter_event.hpp"
25 #include "rcl_interfaces/msg/parameter_value.hpp"
26 #include "rcl_interfaces/srv/describe_parameters.hpp"
27 #include "rcl_interfaces/srv/get_parameter_types.hpp"
28 #include "rcl_interfaces/srv/get_parameters.hpp"
29 #include "rcl_interfaces/srv/list_parameters.hpp"
30 #include "rcl_interfaces/srv/set_parameters.hpp"
31 #include "rcl_interfaces/srv/set_parameters_atomically.hpp"
32 #include "rclcpp/executors.hpp"
34 #include "rclcpp/macros.hpp"
35 #include "rclcpp/node.hpp"
36 #include "rclcpp/parameter.hpp"
39 #include "rmw/rmw.h"
40 
41 namespace rclcpp
42 {
43 namespace parameter_client
44 {
45 
47 {
48 public:
50 
53  const rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_base_interface,
54  const rclcpp::node_interfaces::NodeTopicsInterface::SharedPtr node_topics_interface,
55  const rclcpp::node_interfaces::NodeGraphInterface::SharedPtr node_graph_interface,
56  const rclcpp::node_interfaces::NodeServicesInterface::SharedPtr node_services_interface,
57  const std::string & remote_node_name = "",
58  const rmw_qos_profile_t & qos_profile = rmw_qos_profile_parameters);
59 
62  const rclcpp::node::Node::SharedPtr node,
63  const std::string & remote_node_name = "",
64  const rmw_qos_profile_t & qos_profile = rmw_qos_profile_parameters);
65 
69  const std::vector<std::string> & names,
72  > callback = nullptr);
73 
77  const std::vector<std::string> & names,
80  > callback = nullptr);
81 
88  > callback = nullptr);
89 
96  > callback = nullptr);
97 
101  const std::vector<std::string> & prefixes,
102  uint64_t depth,
105  > callback = nullptr);
106 
107  template<
108  typename CallbackT,
109  typename Alloc = std::allocator<void>,
110  typename SubscriptionT =
113  on_parameter_event(CallbackT && callback)
114  {
116  auto msg_mem_strat =
117  MessageMemoryStrategy<rcl_interfaces::msg::ParameterEvent, Alloc>::create_default();
118 
120  rcl_interfaces::msg::ParameterEvent, CallbackT, Alloc, SubscriptionT>(
121  this->node_topics_interface_.get(),
122  "parameter_events",
123  std::forward<CallbackT>(callback),
124  rmw_qos_profile_default,
125  nullptr, // group,
126  false, // ignore_local_publications,
127  false, // use_intra_process_comms_,
128  msg_mem_strat,
129  std::make_shared<Alloc>());
130  }
131 
133  bool
134  service_is_ready() const;
135 
136  template<typename RatioT = std::milli>
137  bool
140  {
142  std::chrono::duration_cast<std::chrono::nanoseconds>(timeout)
143  );
144  }
145 
146 protected:
148  bool
150 
151 private:
152  const rclcpp::node_interfaces::NodeTopicsInterface::SharedPtr node_topics_interface_;
155  get_parameter_types_client_;
158  set_parameters_atomically_client_;
161  describe_parameters_client_;
162  std::string remote_node_name_;
163 };
164 
166 {
167 public:
169 
171  explicit SyncParametersClient(
172  rclcpp::node::Node::SharedPtr node,
173  const rmw_qos_profile_t & qos_profile = rmw_qos_profile_parameters);
174 
176  SyncParametersClient(
177  rclcpp::executor::Executor::SharedPtr executor,
178  rclcpp::node::Node::SharedPtr node,
179  const rmw_qos_profile_t & qos_profile = rmw_qos_profile_parameters);
180 
183  get_parameters(const std::vector<std::string> & parameter_names);
184 
186  bool
187  has_parameter(const std::string & parameter_name);
188 
189  template<typename T>
190  T
192  const std::string & parameter_name, std::function<T()> parameter_not_found_handler)
193  {
195  names.push_back(parameter_name);
196  auto vars = get_parameters(names);
197  if ((vars.size() != 1) || (vars[0].get_type() == rclcpp::parameter::PARAMETER_NOT_SET)) {
198  return parameter_not_found_handler();
199  } else {
200  return static_cast<T>(vars[0].get_value<T>());
201  }
202  }
203 
204  template<typename T>
205  T
206  get_parameter(const std::string & parameter_name, const T & default_value)
207  {
208  // *INDENT-OFF*
209  return get_parameter_impl(parameter_name,
210  std::function<T()>([&default_value]() -> T {return default_value; }));
211  // *INDENT-ON*
212  }
213 
214  template<typename T>
215  T
216  get_parameter(const std::string & parameter_name)
217  {
218  // *INDENT-OFF*
219  return get_parameter_impl(parameter_name,
220  std::function<T()>([]() -> T {throw std::runtime_error("Parameter not set"); }));
221  // *INDENT-ON*
222  }
223 
226  get_parameter_types(const std::vector<std::string> & parameter_names);
227 
231 
233  rcl_interfaces::msg::SetParametersResult
235 
237  rcl_interfaces::msg::ListParametersResult
239  const std::vector<std::string> & parameter_prefixes,
240  uint64_t depth);
241 
242  template<typename CallbackT>
244  on_parameter_event(CallbackT && callback)
245  {
246  return async_parameters_client_->on_parameter_event(std::forward<CallbackT>(callback));
247  }
248 
250  bool
252  {
253  return async_parameters_client_->service_is_ready();
254  }
255 
256  template<typename RatioT = std::milli>
257  bool
260  {
261  return async_parameters_client_->wait_for_service(timeout);
262  }
263 
264 private:
265  rclcpp::executor::Executor::SharedPtr executor_;
266  rclcpp::node::Node::SharedPtr node_;
267  AsyncParametersClient::SharedPtr async_parameters_client_;
268 };
269 
270 } // namespace parameter_client
271 } // namespace rclcpp
272 
273 #endif // RCLCPP__PARAMETER_CLIENT_HPP_
Definition: parameter_client.hpp:46
std::shared_future< std::vector< rclcpp::parameter::ParameterType > > get_parameter_types(const std::vector< std::string > &names, std::function< void(std::shared_future< std::vector< rclcpp::parameter::ParameterType >>) > callback=nullptr)
Default allocation strategy for messages received by subscriptions.
Definition: message_memory_strategy.hpp:33
std::shared_future< rcl_interfaces::msg::ListParametersResult > list_parameters(const std::vector< std::string > &prefixes, uint64_t depth, std::function< void(std::shared_future< rcl_interfaces::msg::ListParametersResult >) > callback=nullptr)
Subscription implementation, templated on the type of message this subscription receives.
Definition: subscription.hpp:128
T get_parameter_impl(const std::string &parameter_name, std::function< T()> parameter_not_found_handler)
Definition: parameter_client.hpp:191
Definition: allocator_common.hpp:24
T push_back(T... args)
T get_parameter(const std::string &parameter_name)
Definition: parameter_client.hpp:216
bool wait_for_service(std::chrono::duration< int64_t, RatioT > timeout=std::chrono::duration< int64_t, RatioT >(-1))
Definition: parameter_client.hpp:258
#define RCLCPP_SMART_PTR_DEFINITIONS(...)
Definition: macros.hpp:36
bool service_is_ready() const
Definition: parameter_client.hpp:251
rclcpp::subscription::Subscription< rcl_interfaces::msg::ParameterEvent >::SharedPtr on_parameter_event(CallbackT &&callback)
Definition: parameter_client.hpp:113
AsyncParametersClient(const rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_base_interface, const rclcpp::node_interfaces::NodeTopicsInterface::SharedPtr node_topics_interface, const rclcpp::node_interfaces::NodeGraphInterface::SharedPtr node_graph_interface, const rclcpp::node_interfaces::NodeServicesInterface::SharedPtr node_services_interface, const std::string &remote_node_name="", const rmw_qos_profile_t &qos_profile=rmw_qos_profile_parameters)
std::shared_future< rcl_interfaces::msg::SetParametersResult > set_parameters_atomically(const std::vector< rclcpp::parameter::ParameterVariant > &parameters, std::function< void(std::shared_future< rcl_interfaces::msg::SetParametersResult >) > callback=nullptr)
rclcpp::subscription::Subscription< rcl_interfaces::msg::ParameterEvent >::SharedPtr on_parameter_event(CallbackT &&callback)
Definition: parameter_client.hpp:244
rclcpp::subscription::Subscription< MessageT, AllocatorT >::SharedPtr create_subscription(rclcpp::node_interfaces::NodeTopicsInterface *node_topics, const std::string &topic_name, CallbackT &&callback, const rmw_qos_profile_t &qos_profile, rclcpp::callback_group::CallbackGroup::SharedPtr group, bool ignore_local_publications, bool use_intra_process_comms, typename rclcpp::message_memory_strategy::MessageMemoryStrategy< MessageT, AllocatorT >::SharedPtr msg_mem_strat, typename std::shared_ptr< AllocatorT > allocator)
Definition: create_subscription.hpp:31
Definition: parameter_client.hpp:165
#define RCLCPP_PUBLIC
Definition: visibility_control.hpp:50
std::shared_future< std::vector< rcl_interfaces::msg::SetParametersResult > > set_parameters(const std::vector< rclcpp::parameter::ParameterVariant > &parameters, std::function< void(std::shared_future< std::vector< rcl_interfaces::msg::SetParametersResult >>) > callback=nullptr)
std::shared_future< std::vector< rclcpp::parameter::ParameterVariant > > get_parameters(const std::vector< std::string > &names, std::function< void(std::shared_future< std::vector< rclcpp::parameter::ParameterVariant >>) > callback=nullptr)
Definition: client.hpp:121
bool wait_for_service_nanoseconds(std::chrono::nanoseconds timeout)
Definition: parameter.hpp:36
T get_parameter(const std::string &parameter_name, const T &default_value)
Definition: parameter_client.hpp:206
bool wait_for_service(std::chrono::duration< int64_t, RatioT > timeout=std::chrono::duration< int64_t, RatioT >(-1))
Definition: parameter_client.hpp:138