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 
45 {
46 public:
48 
51  const rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_base_interface,
52  const rclcpp::node_interfaces::NodeTopicsInterface::SharedPtr node_topics_interface,
53  const rclcpp::node_interfaces::NodeGraphInterface::SharedPtr node_graph_interface,
54  const rclcpp::node_interfaces::NodeServicesInterface::SharedPtr node_services_interface,
55  const std::string & remote_node_name = "",
56  const rmw_qos_profile_t & qos_profile = rmw_qos_profile_parameters);
57 
60  const rclcpp::Node::SharedPtr node,
61  const std::string & remote_node_name = "",
62  const rmw_qos_profile_t & qos_profile = rmw_qos_profile_parameters);
63 
66  rclcpp::Node * node,
67  const std::string & remote_node_name = "",
68  const rmw_qos_profile_t & qos_profile = rmw_qos_profile_parameters);
69 
73  const std::vector<std::string> & names,
76  > callback = nullptr);
77 
81  const std::vector<std::string> & names,
84  > callback = nullptr);
85 
89  const std::vector<rclcpp::Parameter> & parameters,
92  > callback = nullptr);
93 
97  const std::vector<rclcpp::Parameter> & parameters,
100  > callback = nullptr);
101 
105  const std::vector<std::string> & prefixes,
106  uint64_t depth,
109  > callback = nullptr);
110 
111  template<
112  typename CallbackT,
113  typename Alloc = std::allocator<void>,
114  typename SubscriptionT =
117  on_parameter_event(CallbackT && callback)
118  {
120  auto msg_mem_strat =
121  MessageMemoryStrategy<rcl_interfaces::msg::ParameterEvent, Alloc>::create_default();
122 
123  using rcl_interfaces::msg::ParameterEvent;
125  ParameterEvent, CallbackT, Alloc, ParameterEvent, SubscriptionT>(
126  this->node_topics_interface_.get(),
127  "parameter_events",
128  std::forward<CallbackT>(callback),
129  rmw_qos_profile_default,
130  nullptr, // group,
131  false, // ignore_local_publications,
132  false, // use_intra_process_comms_,
133  msg_mem_strat,
134  std::make_shared<Alloc>());
135  }
136 
138  bool
139  service_is_ready() const;
140 
141  template<typename RatioT = std::milli>
142  bool
145  {
147  std::chrono::duration_cast<std::chrono::nanoseconds>(timeout)
148  );
149  }
150 
151 protected:
153  bool
155 
156 private:
157  const rclcpp::node_interfaces::NodeTopicsInterface::SharedPtr node_topics_interface_;
160  get_parameter_types_client_;
163  set_parameters_atomically_client_;
166  describe_parameters_client_;
167  std::string remote_node_name_;
168 };
169 
171 {
172 public:
174 
176  explicit SyncParametersClient(
177  rclcpp::Node::SharedPtr node,
178  const std::string & remote_node_name = "",
179  const rmw_qos_profile_t & qos_profile = rmw_qos_profile_parameters);
180 
182  SyncParametersClient(
183  rclcpp::executor::Executor::SharedPtr executor,
184  rclcpp::Node::SharedPtr node,
185  const std::string & remote_node_name = "",
186  const rmw_qos_profile_t & qos_profile = rmw_qos_profile_parameters);
187 
190  get_parameters(const std::vector<std::string> & parameter_names);
191 
193  bool
194  has_parameter(const std::string & parameter_name);
195 
196  template<typename T>
197  T
199  const std::string & parameter_name, std::function<T()> parameter_not_found_handler)
200  {
202  names.push_back(parameter_name);
203  auto vars = get_parameters(names);
204  if ((vars.size() != 1) || (vars[0].get_type() == rclcpp::ParameterType::PARAMETER_NOT_SET)) {
205  return parameter_not_found_handler();
206  } else {
207  return static_cast<T>(vars[0].get_value<T>());
208  }
209  }
210 
211  template<typename T>
212  T
213  get_parameter(const std::string & parameter_name, const T & default_value)
214  {
215  return get_parameter_impl(
216  parameter_name,
217  std::function<T()>([&default_value]() -> T {return default_value;}));
218  }
219 
220  template<typename T>
221  T
222  get_parameter(const std::string & parameter_name)
223  {
224  return get_parameter_impl(
225  parameter_name,
226  std::function<T()>([]() -> T {throw std::runtime_error("Parameter not set");}));
227  }
228 
231  get_parameter_types(const std::vector<std::string> & parameter_names);
232 
236 
238  rcl_interfaces::msg::SetParametersResult
240 
242  rcl_interfaces::msg::ListParametersResult
244  const std::vector<std::string> & parameter_prefixes,
245  uint64_t depth);
246 
247  template<typename CallbackT>
249  on_parameter_event(CallbackT && callback)
250  {
251  return async_parameters_client_->on_parameter_event(std::forward<CallbackT>(callback));
252  }
253 
255  bool
257  {
258  return async_parameters_client_->service_is_ready();
259  }
260 
261  template<typename RatioT = std::milli>
262  bool
265  {
266  return async_parameters_client_->wait_for_service(timeout);
267  }
268 
269 private:
270  rclcpp::executor::Executor::SharedPtr executor_;
271  rclcpp::Node::SharedPtr node_;
272  AsyncParametersClient::SharedPtr async_parameters_client_;
273 };
274 
275 } // namespace rclcpp
276 
277 #endif // RCLCPP__PARAMETER_CLIENT_HPP_
std::shared_ptr< SubscriptionT > 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< CallbackMessageT, AllocatorT >::SharedPtr msg_mem_strat, typename std::shared_ptr< AllocatorT > allocator)
Definition: create_subscription.hpp:36
Default allocation strategy for messages received by subscriptions.
Definition: message_memory_strategy.hpp:40
std::shared_future< std::vector< rclcpp::Parameter > > get_parameters(const std::vector< std::string > &names, std::function< void(std::shared_future< std::vector< rclcpp::Parameter >>) > callback=nullptr)
rclcpp::Subscription< rcl_interfaces::msg::ParameterEvent >::SharedPtr on_parameter_event(CallbackT &&callback)
Definition: parameter_client.hpp:249
std::shared_future< std::vector< rcl_interfaces::msg::SetParametersResult > > set_parameters(const std::vector< rclcpp::Parameter > &parameters, std::function< void(std::shared_future< std::vector< rcl_interfaces::msg::SetParametersResult >>) > callback=nullptr)
Definition: allocator_common.hpp:24
Definition: parameter_client.hpp:44
bool wait_for_service(std::chrono::duration< int64_t, RatioT > timeout=std::chrono::duration< int64_t, RatioT >(-1))
Definition: parameter_client.hpp:263
bool service_is_ready() const
Definition: parameter_client.hpp:256
Definition: client.hpp:119
Subscription implementation, templated on the type of message this subscription receives.
Definition: subscription.hpp:148
T push_back(T... args)
bool wait_for_service(std::chrono::duration< int64_t, RatioT > timeout=std::chrono::duration< int64_t, RatioT >(-1))
Definition: parameter_client.hpp:143
#define RCLCPP_SMART_PTR_DEFINITIONS(...)
Definition: macros.hpp:36
T get_parameter(const std::string &parameter_name)
Definition: parameter_client.hpp:222
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)
rclcpp::Subscription< rcl_interfaces::msg::ParameterEvent >::SharedPtr on_parameter_event(CallbackT &&callback)
Definition: parameter_client.hpp:117
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)
std::shared_future< rcl_interfaces::msg::SetParametersResult > set_parameters_atomically(const std::vector< rclcpp::Parameter > &parameters, std::function< void(std::shared_future< rcl_interfaces::msg::SetParametersResult >) > callback=nullptr)
bool wait_for_service_nanoseconds(std::chrono::nanoseconds timeout)
#define RCLCPP_PUBLIC
Definition: visibility_control.hpp:50
Definition: parameter_client.hpp:170
Definition: parameter_value.hpp:33
std::shared_future< std::vector< rclcpp::ParameterType > > get_parameter_types(const std::vector< std::string > &names, std::function< void(std::shared_future< std::vector< rclcpp::ParameterType >>) > callback=nullptr)
T get_parameter(const std::string &parameter_name, const T &default_value)
Definition: parameter_client.hpp:213
Node is the single point of entry for creating publishers and subscribers.
Definition: node.hpp:67
T get_parameter_impl(const std::string &parameter_name, std::function< T()> parameter_not_found_handler)
Definition: parameter_client.hpp:198