ros2_control - kilted
Loading...
Searching...
No Matches
loaned_command_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_COMMAND_INTERFACE_HPP_
16#define HARDWARE_INTERFACE__LOANED_COMMAND_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
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 LoanedCommandInterface(
35 CommandInterface & command_interface)
36 : LoanedCommandInterface(command_interface, nullptr)
37 {
38 }
39
40 [[deprecated("Replaced by the new version using shared_ptr")]] LoanedCommandInterface(
41 CommandInterface & command_interface, Deleter && deleter)
42 : command_interface_(command_interface), deleter_(std::forward<Deleter>(deleter))
43 {
44 }
45
46 LoanedCommandInterface(CommandInterface::SharedPtr command_interface, Deleter && deleter)
47 : command_interface_(*command_interface), deleter_(std::forward<Deleter>(deleter))
48 {
49 }
50
51 LoanedCommandInterface(const LoanedCommandInterface & other) = delete;
52
54
56 {
57 auto logger = rclcpp::get_logger(command_interface_.get_name());
58 RCLCPP_WARN_EXPRESSION(
59 rclcpp::get_logger(get_name()),
60 (get_value_statistics_.failed_counter > 0 || get_value_statistics_.timeout_counter > 0),
61 "LoanedCommandInterface %s has %u (%.4f %%) timeouts and %u (~ %.4f %%) missed calls out of "
62 "%u get_value calls",
63 get_name().c_str(), get_value_statistics_.timeout_counter,
64 (get_value_statistics_.timeout_counter * 100.0) / get_value_statistics_.total_counter,
65 get_value_statistics_.failed_counter,
66 (get_value_statistics_.failed_counter * 10.0) / get_value_statistics_.total_counter,
67 get_value_statistics_.total_counter);
68 RCLCPP_WARN_EXPRESSION(
69 rclcpp::get_logger(get_name()),
70 (set_value_statistics_.failed_counter > 0 || set_value_statistics_.timeout_counter > 0),
71 "LoanedCommandInterface %s has %u (%.4f %%) timeouts and %u (~ %.4f %%) missed calls out of "
72 "%u set_value calls",
73 get_name().c_str(), set_value_statistics_.timeout_counter,
74 (set_value_statistics_.timeout_counter * 100.0) / set_value_statistics_.total_counter,
75 set_value_statistics_.failed_counter,
76 (set_value_statistics_.failed_counter * 10.0) / set_value_statistics_.total_counter,
77 set_value_statistics_.total_counter);
78 if (deleter_)
79 {
80 deleter_();
81 }
82 }
83
84 const std::string & get_name() const { return command_interface_.get_name(); }
85
86 const std::string & get_interface_name() const { return command_interface_.get_interface_name(); }
87
88 const std::string & get_prefix_name() const { return command_interface_.get_prefix_name(); }
89
105 template <typename T>
106 [[nodiscard]] bool set_value(const T & value, unsigned int max_tries = 10)
107 {
108 unsigned int nr_tries = 0;
109 ++set_value_statistics_.total_counter;
110 while (!command_interface_.set_limited_value(value))
111 {
112 ++set_value_statistics_.failed_counter;
113 ++nr_tries;
114 if (nr_tries == max_tries)
115 {
116 ++set_value_statistics_.timeout_counter;
117 return false;
118 }
119 std::this_thread::yield();
120 }
121 return true;
122 }
123
124 [[deprecated(
125 "Use std::optional<T> get_optional() instead to retrieve the value. This method will be "
126 "removed by the ROS 2 Kilted Kaiju release.")]]
127 double get_value() const
128 {
129 std::optional<double> opt_value = get_optional();
130 if (opt_value.has_value())
131 {
132 return opt_value.value();
133 }
134 else
135 {
136 return std::numeric_limits<double>::quiet_NaN();
137 }
138 }
139
154 template <typename T = double>
155 [[nodiscard]] std::optional<T> get_optional(unsigned int max_tries = 10) const
156 {
157 unsigned int nr_tries = 0;
158 do
159 {
160 ++get_value_statistics_.total_counter;
161 const std::optional<T> data = command_interface_.get_optional<T>();
162 if (data.has_value())
163 {
164 return data;
165 }
166 ++get_value_statistics_.failed_counter;
167 ++nr_tries;
168 std::this_thread::yield();
169 } while (nr_tries < max_tries);
170
171 ++get_value_statistics_.timeout_counter;
172 return std::nullopt;
173 }
174
179 HandleDataType get_data_type() const { return command_interface_.get_data_type(); }
180
185 bool is_castable_to_double() const { return command_interface_.is_castable_to_double(); }
186
187protected:
188 CommandInterface & command_interface_;
189 Deleter deleter_;
190
191private:
192 struct HandleRTStatistics
193 {
194 unsigned int total_counter = 0;
195 unsigned int failed_counter = 0;
196 unsigned int timeout_counter = 0;
197 };
198 mutable HandleRTStatistics get_value_statistics_;
199 HandleRTStatistics set_value_statistics_;
200};
201
202} // namespace hardware_interface
203#endif // HARDWARE_INTERFACE__LOANED_COMMAND_INTERFACE_HPP_
Definition handle.hpp:408
bool set_limited_value(const T &value)
A setter for the value of the command interface that triggers the limiter.
Definition handle.hpp:435
Definition hardware_info.hpp:137
bool is_castable_to_double() const
Returns true if the handle data type can be casted to double.
Definition handle.hpp:324
std::optional< T > get_optional() const
Get the value of the handle.
Definition handle.hpp:200
Definition loaned_command_interface.hpp:30
bool is_castable_to_double() const
Check if the state interface can be casted to double.
Definition loaned_command_interface.hpp:185
bool set_value(const T &value, unsigned int max_tries=10)
Set the value of the command interface.
Definition loaned_command_interface.hpp:106
HandleDataType get_data_type() const
Get the data type of the command interface.
Definition loaned_command_interface.hpp:179
std::optional< T > get_optional(unsigned int max_tries=10) const
Get the value of the command interface.
Definition loaned_command_interface.hpp:155
Definition actuator.hpp:22