ROS 2 rclcpp + rcl - lyrical  lyrical
ROS 2 C++ Client Library with ROS Client Library
parameter_event_handler.hpp
1 // Copyright 2019 Intel Corporation
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_EVENT_HANDLER_HPP_
16 #define RCLCPP__PARAMETER_EVENT_HANDLER_HPP_
17 
18 #include <list>
19 #include <memory>
20 #include <string>
21 #include <unordered_map>
22 #include <utility>
23 #include <vector>
24 
25 #include "rclcpp/create_subscription.hpp"
26 #include "rclcpp/node_interfaces/get_node_base_interface.hpp"
27 #include "rclcpp/node_interfaces/get_node_topics_interface.hpp"
28 #include "rclcpp/node_interfaces/node_base_interface.hpp"
29 #include "rclcpp/node_interfaces/node_topics_interface.hpp"
30 #include "rclcpp/parameter.hpp"
31 #include "rclcpp/qos.hpp"
32 #include "rclcpp/subscription.hpp"
33 #include "rclcpp/visibility_control.hpp"
34 #include "rcl_interfaces/msg/parameter_event.hpp"
35 
36 namespace rclcpp
37 {
38 
40 {
41  RCLCPP_SMART_PTR_DEFINITIONS(ParameterCallbackHandle)
42 
43  using ParameterCallbackType = std::function<void (const rclcpp::Parameter &)>;
44 
45  std::string parameter_name;
46  std::string node_name;
47  ParameterCallbackType callback;
48 };
49 
51 {
52  RCLCPP_SMART_PTR_DEFINITIONS(ParameterEventCallbackHandle)
53 
54  using ParameterEventCallbackType =
55  std::function<void (const rcl_interfaces::msg::ParameterEvent &)>;
56 
57  ParameterEventCallbackType callback;
58 };
59 
61 
178 {
179 public:
181 
185  template<typename NodeT>
187  const NodeT & node,
188  const rclcpp::QoS & qos =
189  rclcpp::QoS(rclcpp::QoSInitialization::from_rmw(rmw_qos_profile_parameter_events)))
190  : node_base_(rclcpp::node_interfaces::get_node_base_interface(node))
191  {
192  auto node_topics = rclcpp::node_interfaces::get_node_topics_interface(node);
193 
194  callbacks_ = std::make_shared<Callbacks>();
195 
196  event_subscription_ = rclcpp::create_subscription<rcl_interfaces::msg::ParameterEvent>(
197  node_topics, "/parameter_events", qos,
198  [callbacks = callbacks_](const rcl_interfaces::msg::ParameterEvent & event) {
199  callbacks->event_callback(event);
200  });
201  }
202 
203  using ParameterEventCallbackType =
204  ParameterEventCallbackHandle::ParameterEventCallbackType;
205 
207 
216  RCLCPP_PUBLIC
217  RCUTILS_WARN_UNUSED
218  ParameterEventCallbackHandle::SharedPtr
220  const ParameterEventCallbackType & callback);
221 
223 
226  RCLCPP_PUBLIC
227  void
229  const ParameterEventCallbackHandle::SharedPtr & callback_handle);
230 
231  using ParameterCallbackType = ParameterCallbackHandle::ParameterCallbackType;
232 
234 
250  RCLCPP_PUBLIC
251  RCUTILS_WARN_UNUSED
252  ParameterCallbackHandle::SharedPtr
254  const std::string & parameter_name,
255  const ParameterCallbackType & callback,
256  const std::string & node_name = "");
257 
259 
280  RCLCPP_PUBLIC
281  bool configure_nodes_filter(const std::vector<std::string> & node_names);
282 
284 
291  RCLCPP_PUBLIC
292  void
294  const ParameterCallbackHandle::SharedPtr & callback_handle);
295 
297 
307  RCLCPP_PUBLIC
308  static bool
310  const rcl_interfaces::msg::ParameterEvent & event,
311  rclcpp::Parameter & parameter,
312  const std::string & parameter_name,
313  const std::string & node_name = "");
314 
316 
329  RCLCPP_PUBLIC
330  static rclcpp::Parameter
332  const rcl_interfaces::msg::ParameterEvent & event,
333  const std::string & parameter_name,
334  const std::string & node_name = "");
335 
337 
341  RCLCPP_PUBLIC
342  static std::vector<rclcpp::Parameter>
344  const rcl_interfaces::msg::ParameterEvent & event);
345 
346  using CallbacksContainerType = std::list<ParameterCallbackHandle::WeakPtr>;
347 
348 protected:
349  // *INDENT-OFF* Uncrustify doesn't handle indented public/private labels
350  // Hash function for string pair required in std::unordered_map
351  // See: https://stackoverflow.com/questions/35985960/c-why-is-boosthash-combine-the-best-way-to-combine-hash-values
353  {
354  public:
355  template<typename T>
356  inline void hash_combine(std::size_t & seed, const T & v) const
357  {
358  std::hash<T> hasher;
359  seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
360  }
361 
362  inline size_t operator()(const std::pair<std::string, std::string> & s) const
363  {
364  size_t seed = 0;
365  hash_combine(seed, s.first);
366  hash_combine(seed, s.second);
367  return seed;
368  }
369  };
370  // *INDENT-ON*
371 
372  struct Callbacks
373  {
374  std::recursive_mutex mutex_;
375 
376  // Map container for registered parameters
377  std::unordered_map<
378  std::pair<std::string, std::string>,
379  CallbacksContainerType,
381  > parameter_callbacks_;
382 
383  std::list<ParameterEventCallbackHandle::WeakPtr> event_callbacks_;
384 
386  RCLCPP_PUBLIC
387  void
388  event_callback(const rcl_interfaces::msg::ParameterEvent & event);
389  };
390 
391  std::shared_ptr<Callbacks> callbacks_;
392 
393  // Utility function for resolving node path.
394  std::string resolve_path(const std::string & path);
395 
396  // Node interface used for base functionality
397  std::shared_ptr<rclcpp::node_interfaces::NodeBaseInterface> node_base_;
398 
400 };
401 
402 } // namespace rclcpp
403 
404 #endif // RCLCPP__PARAMETER_EVENT_HANDLER_HPP_
A class used to "handle" (monitor and respond to) changes to parameters.
static RCLCPP_PUBLIC bool get_parameter_from_event(const rcl_interfaces::msg::ParameterEvent &event, rclcpp::Parameter &parameter, const std::string &parameter_name, const std::string &node_name="")
Get an rclcpp::Parameter from a parameter event.
static RCLCPP_PUBLIC std::vector< rclcpp::Parameter > get_parameters_from_event(const rcl_interfaces::msg::ParameterEvent &event)
Get all rclcpp::Parameter values from a parameter event.
ParameterEventHandler(const NodeT &node, const rclcpp::QoS &qos=rclcpp::QoS(rclcpp::QoSInitialization::from_rmw(rmw_qos_profile_parameter_events)))
Construct a parameter events monitor.
RCLCPP_PUBLIC RCUTILS_WARN_UNUSED ParameterCallbackHandle::SharedPtr add_parameter_callback(const std::string &parameter_name, const ParameterCallbackType &callback, const std::string &node_name="")
Add a callback for a specified parameter.
RCLCPP_PUBLIC void remove_parameter_callback(const ParameterCallbackHandle::SharedPtr &callback_handle)
Remove a parameter callback registered with add_parameter_callback.
RCLCPP_PUBLIC bool configure_nodes_filter(const std::vector< std::string > &node_names)
Configure which node parameter events will be received.
RCLCPP_PUBLIC void remove_parameter_event_callback(const ParameterEventCallbackHandle::SharedPtr &callback_handle)
Remove parameter event callback registered with add_parameter_event_callback.
RCLCPP_PUBLIC RCUTILS_WARN_UNUSED ParameterEventCallbackHandle::SharedPtr add_parameter_event_callback(const ParameterEventCallbackType &callback)
Set a callback for all parameter events.
Structure to store an arbitrary parameter with templated get/set methods.
Definition: parameter.hpp:53
Encapsulation of Quality of Service settings.
Definition: qos.hpp:116
Subscription implementation, templated on the type of message this subscription receives.
Versions of rosidl_typesupport_cpp::get_message_type_support_handle that handle adapted types.
RCLCPP_PUBLIC void event_callback(const rcl_interfaces::msg::ParameterEvent &event)
Callback for parameter events subscriptions.
static QoSInitialization from_rmw(const rmw_qos_profile_t &rmw_qos)
Create a QoSInitialization from an existing rmw_qos_profile_t, using its history and depth.
Definition: qos.cpp:69