ros2_control - jazzy
Loading...
Searching...
No Matches
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 <optional>
21#include <string>
22#include <thread>
23#include <utility>
24
25#include "hardware_interface/handle.hpp"
26#include "rclcpp/logging.hpp"
27namespace hardware_interface
28{
30{
31public:
32 using Deleter = std::function<void(void)>;
33
34 [[deprecated("Replaced by the new version using shared_ptr")]] explicit LoanedStateInterface(
35 const StateInterface & state_interface)
36 : LoanedStateInterface(state_interface, nullptr)
37 {
38 }
39
40 [[deprecated("Replaced by the new version using shared_ptr")]] LoanedStateInterface(
41 const StateInterface & state_interface, Deleter && deleter)
42 : state_interface_(state_interface),
43 deleter_(std::forward<Deleter>(deleter)),
44 interface_name_(state_interface.get_name())
45 {
46 }
47
48 explicit LoanedStateInterface(StateInterface::ConstSharedPtr state_interface)
49 : LoanedStateInterface(state_interface, nullptr)
50 {
51 }
52
53 explicit LoanedStateInterface(StateInterface::ConstSharedPtr state_interface, Deleter && deleter)
54 : state_interface_(*state_interface),
55 interface_name_(state_interface->get_name()),
56 deleter_(std::forward<Deleter>(deleter))
57 {
58 }
59
60 LoanedStateInterface(const LoanedStateInterface & other) = delete;
61
63
64 virtual ~LoanedStateInterface()
65 {
66 auto logger = rclcpp::get_logger(interface_name_);
67 RCLCPP_WARN_EXPRESSION(
68 logger,
69 (get_value_statistics_.failed_counter > 0 || get_value_statistics_.timeout_counter > 0),
70 "LoanedStateInterface %s has %u (%.4f %%) timeouts and %u (%.4f %%) missed calls out of %u "
71 "get_value calls",
72 interface_name_.c_str(), get_value_statistics_.timeout_counter,
73 (get_value_statistics_.timeout_counter * 100.0) / get_value_statistics_.total_counter,
74 get_value_statistics_.failed_counter,
75 (get_value_statistics_.failed_counter * 100.0) / get_value_statistics_.total_counter,
76 get_value_statistics_.total_counter);
77 if (deleter_)
78 {
79 deleter_();
80 }
81 }
82
83 const std::string & get_name() const { return state_interface_.get_name(); }
84
85 const std::string & get_interface_name() const { return state_interface_.get_interface_name(); }
86
87 [[deprecated(
88 "Replaced by get_name method, which is semantically more correct")]] const std::string
89 get_full_name() const
90 {
91 return state_interface_.get_name();
92 }
93
94 const std::string & get_prefix_name() const { return state_interface_.get_prefix_name(); }
95
96 [[deprecated(
97 "Use std::optional<T> get_optional() instead to retrieve the value. This method will be "
98 "removed by the ROS 2 Kilted Kaiju release.")]]
99 double get_value() const
100 {
101 std::optional<double> opt_value = get_optional();
102 if (opt_value.has_value())
103 {
104 return opt_value.value();
105 }
106 else
107 {
108 return std::numeric_limits<double>::quiet_NaN();
109 }
110 }
111
126 template <typename T = double>
127 [[nodiscard]] std::optional<T> get_optional(unsigned int max_tries = 10) const
128 {
129 unsigned int nr_tries = 0;
130 do
131 {
132 ++get_value_statistics_.total_counter;
133 const std::optional<T> data = state_interface_.get_optional<T>();
134 if (data.has_value())
135 {
136 return data;
137 }
138 ++get_value_statistics_.failed_counter;
139 ++nr_tries;
140 std::this_thread::yield();
141 } while (nr_tries < max_tries);
142
143 ++get_value_statistics_.timeout_counter;
144 return std::nullopt;
145 }
146
162 template <typename T>
163 [[deprecated(
164 "Use std::optional<T> get_optional() instead to retrieve the value. This method will be "
165 "removed by the ROS 2 Kilted Kaiju release.")]] [[nodiscard]] bool
166 get_value(T & value, unsigned int max_tries = 10) const
167 {
168 const auto opt_value = get_optional<T>(max_tries);
169 if (opt_value.has_value())
170 {
171 value = opt_value.value();
172 return true;
173 }
174 return false;
175 }
176
181 HandleDataType get_data_type() const { return state_interface_.get_data_type(); }
182
183protected:
184 const StateInterface & state_interface_;
185 Deleter deleter_;
186 std::string interface_name_;
187
188private:
189 struct HandleRTStatistics
190 {
191 unsigned int total_counter = 0;
192 unsigned int failed_counter = 0;
193 unsigned int timeout_counter = 0;
194 };
195 mutable HandleRTStatistics get_value_statistics_;
196};
197
198} // namespace hardware_interface
199#endif // HARDWARE_INTERFACE__LOANED_STATE_INTERFACE_HPP_
Definition hardware_info.hpp:158
std::optional< T > get_optional() const
Get the value of the handle.
Definition handle.hpp:337
Definition loaned_state_interface.hpp:30
std::optional< T > get_optional(unsigned int max_tries=10) const
Get the value of the state interface.
Definition loaned_state_interface.hpp:127
bool get_value(T &value, unsigned int max_tries=10) const
Get the value of the state interface.
Definition loaned_state_interface.hpp:166
HandleDataType get_data_type() const
Get the data type of the state interface.
Definition loaned_state_interface.hpp:181
Definition handle.hpp:686
Definition mujoco_system_interface.hpp:57