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