ros2_control - rolling
loaned_state_interface.hpp
1 // Copyright 2020 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 HARDWARE_INTERFACE__LOANED_STATE_INTERFACE_HPP_
16 #define HARDWARE_INTERFACE__LOANED_STATE_INTERFACE_HPP_
17 
18 #include <functional>
19 #include <limits>
20 #include <string>
21 #include <thread>
22 #include <utility>
23 
24 #include "hardware_interface/handle.hpp"
25 #include "rclcpp/logging.hpp"
26 namespace hardware_interface
27 {
29 {
30 public:
31  using Deleter = std::function<void(void)>;
32 
33  [[deprecated("Replaced by the new version using shared_ptr")]] explicit LoanedStateInterface(
34  const StateInterface & state_interface)
35  : LoanedStateInterface(state_interface, nullptr)
36  {
37  }
38 
39  [[deprecated("Replaced by the new version using shared_ptr")]] LoanedStateInterface(
40  const StateInterface & state_interface, Deleter && deleter)
41  : state_interface_(state_interface), deleter_(std::forward<Deleter>(deleter))
42  {
43  }
44 
45  explicit LoanedStateInterface(StateInterface::ConstSharedPtr state_interface)
46  : LoanedStateInterface(state_interface, nullptr)
47  {
48  }
49 
50  LoanedStateInterface(StateInterface::ConstSharedPtr state_interface, Deleter && deleter)
51  : state_interface_(*state_interface), deleter_(std::forward<Deleter>(deleter))
52  {
53  }
54 
55  LoanedStateInterface(const LoanedStateInterface & other) = delete;
56 
57  LoanedStateInterface(LoanedStateInterface && other) = default;
58 
59  virtual ~LoanedStateInterface()
60  {
61  auto logger = rclcpp::get_logger(state_interface_.get_name());
62  RCLCPP_WARN_EXPRESSION(
63  logger,
64  (get_value_statistics_.failed_counter > 0 || get_value_statistics_.timeout_counter > 0),
65  "LoanedStateInterface %s has %u (%.4f %%) timeouts and %u (%.4f %%) missed calls out of %u "
66  "get_value calls",
67  state_interface_.get_name().c_str(), get_value_statistics_.timeout_counter,
68  (get_value_statistics_.timeout_counter * 100.0) / get_value_statistics_.total_counter,
69  get_value_statistics_.failed_counter,
70  (get_value_statistics_.failed_counter * 10.0) / get_value_statistics_.total_counter,
71  get_value_statistics_.total_counter);
72  if (deleter_)
73  {
74  deleter_();
75  }
76  }
77 
78  const std::string & get_name() const { return state_interface_.get_name(); }
79 
80  const std::string & get_interface_name() const { return state_interface_.get_interface_name(); }
81 
82  [[deprecated(
83  "Replaced by get_name method, which is semantically more correct")]] const std::string
84  get_full_name() const
85  {
86  return state_interface_.get_name();
87  }
88 
89  const std::string & get_prefix_name() const { return state_interface_.get_prefix_name(); }
90 
91  double get_value() const
92  {
93  double value;
94  if (get_value(value))
95  {
96  return value;
97  }
98  else
99  {
100  return std::numeric_limits<double>::quiet_NaN();
101  }
102  }
103 
104  template <typename T>
105  [[nodiscard]] bool get_value(T & value, unsigned int max_tries = 10) const
106  {
107  unsigned int nr_tries = 0;
108  ++get_value_statistics_.total_counter;
109  while (!state_interface_.get_value(value))
110  {
111  ++get_value_statistics_.failed_counter;
112  ++nr_tries;
113  if (nr_tries == max_tries)
114  {
115  ++get_value_statistics_.timeout_counter;
116  return false;
117  }
118  std::this_thread::yield();
119  }
120  return true;
121  }
122 
123 protected:
124  const StateInterface & state_interface_;
125  Deleter deleter_;
126 
127 private:
128  struct HandleRTStatistics
129  {
130  unsigned int total_counter = 0;
131  unsigned int failed_counter = 0;
132  unsigned int timeout_counter = 0;
133  };
134  mutable HandleRTStatistics get_value_statistics_;
135 };
136 
137 } // namespace hardware_interface
138 #endif // HARDWARE_INTERFACE__LOANED_STATE_INTERFACE_HPP_
Definition: loaned_state_interface.hpp:29
Definition: handle.hpp:203
Definition: actuator.hpp:33