ros2_control - rolling
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 <optional>
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 explicit LoanedCommandInterface(CommandInterface::SharedPtr command_interface)
35 : LoanedCommandInterface(command_interface, nullptr)
36 {
37 }
38
39 explicit LoanedCommandInterface(CommandInterface::SharedPtr command_interface, Deleter && deleter)
40 : command_interface_(command_interface),
41 interface_name_(command_interface->get_name()),
42 deleter_(std::forward<Deleter>(deleter))
43 {
44 }
45
46 LoanedCommandInterface(const LoanedCommandInterface & other) = delete;
47
49
51 {
52 if (command_interface_)
53 {
54 auto logger = rclcpp::get_logger(interface_name_);
55 RCLCPP_WARN_EXPRESSION(
56 logger,
57 (get_value_statistics_.failed_counter > 0 || get_value_statistics_.timeout_counter > 0),
58 "LoanedCommandInterface %s has %u (%.4f %%) timeouts and %u (~ %.4f %%) missed calls out "
59 "of "
60 "%u get_value calls",
61 interface_name_.c_str(), get_value_statistics_.timeout_counter,
62 (get_value_statistics_.timeout_counter * 100.0) / get_value_statistics_.total_counter,
63 get_value_statistics_.failed_counter,
64 (get_value_statistics_.failed_counter * 100.0) / get_value_statistics_.total_counter,
65 get_value_statistics_.total_counter);
66 RCLCPP_WARN_EXPRESSION(
67 logger,
68 (set_value_statistics_.failed_counter > 0 || set_value_statistics_.timeout_counter > 0),
69 "LoanedCommandInterface %s has %u (%.4f %%) timeouts and %u (~ %.4f %%) missed calls out "
70 "of "
71 "%u set_value calls",
72 interface_name_.c_str(), set_value_statistics_.timeout_counter,
73 (set_value_statistics_.timeout_counter * 100.0) / set_value_statistics_.total_counter,
74 set_value_statistics_.failed_counter,
75 (set_value_statistics_.failed_counter * 100.0) / set_value_statistics_.total_counter,
76 set_value_statistics_.total_counter);
77 }
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
87 {
88 return command_interface_->get_interface_name();
89 }
90
91 const std::string & get_prefix_name() const { return command_interface_->get_prefix_name(); }
92
108 template <typename T>
109 [[nodiscard]] bool set_value(const T & value, unsigned int max_tries = 10)
110 {
111 unsigned int nr_tries = 0;
112 ++set_value_statistics_.total_counter;
113 while (!command_interface_->set_limited_value(value))
114 {
115 ++set_value_statistics_.failed_counter;
116 ++nr_tries;
117 if (nr_tries == max_tries)
118 {
119 ++set_value_statistics_.timeout_counter;
120 return false;
121 }
122 std::this_thread::yield();
123 }
124 return true;
125 }
126
141 template <typename T = double>
142 [[nodiscard]] std::optional<T> get_optional(unsigned int max_tries = 10) const
143 {
144 unsigned int nr_tries = 0;
145 do
146 {
147 ++get_value_statistics_.total_counter;
148 const std::optional<T> data = command_interface_->get_optional<T>();
149 if (data.has_value())
150 {
151 return data;
152 }
153 ++get_value_statistics_.failed_counter;
154 ++nr_tries;
155 std::this_thread::yield();
156 } while (nr_tries < max_tries);
157
158 ++get_value_statistics_.timeout_counter;
159 return std::nullopt;
160 }
161
166 HandleDataType get_data_type() const { return command_interface_->get_data_type(); }
167
172 bool is_castable_to_double() const { return command_interface_->is_castable_to_double(); }
173
174protected:
175 CommandInterface::SharedPtr command_interface_;
176 std::string interface_name_;
177 Deleter deleter_;
178
179private:
180 struct HandleRTStatistics
181 {
182 unsigned int total_counter = 0;
183 unsigned int failed_counter = 0;
184 unsigned int timeout_counter = 0;
185 };
186 mutable HandleRTStatistics get_value_statistics_;
187 HandleRTStatistics set_value_statistics_;
188};
189
190} // namespace hardware_interface
191#endif // HARDWARE_INTERFACE__LOANED_COMMAND_INTERFACE_HPP_
Definition hardware_info.hpp:158
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:172
bool set_value(const T &value, unsigned int max_tries=10)
Set the value of the command interface.
Definition loaned_command_interface.hpp:109
HandleDataType get_data_type() const
Get the data type of the command interface.
Definition loaned_command_interface.hpp:166
std::optional< T > get_optional(unsigned int max_tries=10) const
Get the value of the command interface.
Definition loaned_command_interface.hpp:142
Definition mujoco_system_interface.hpp:57