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 <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 [[deprecated(
89 "Replaced by get_name method, which is semantically more correct")]] const std::string
90 get_full_name() const
91 {
92 return command_interface_.get_name();
93 }
94
95 const std::string & get_prefix_name() const { return command_interface_.get_prefix_name(); }
96
97 template <typename T>
98 [[nodiscard]] bool set_value(T value, unsigned int max_tries = 10)
99 {
100 unsigned int nr_tries = 0;
101 ++set_value_statistics_.total_counter;
102 while (!command_interface_.set_value(value))
103 {
104 ++set_value_statistics_.failed_counter;
105 ++nr_tries;
106 if (nr_tries == max_tries)
107 {
108 ++set_value_statistics_.timeout_counter;
109 return false;
110 }
111 std::this_thread::yield();
112 }
113 return true;
114 }
115
116 double get_value() const
117 {
118 double value;
119 if (get_value(value))
120 {
121 return value;
122 }
123 else
124 {
125 return std::numeric_limits<double>::quiet_NaN();
126 }
127 }
128
129 template <typename T>
130 [[nodiscard]] bool get_value(T & value, unsigned int max_tries = 10) const
131 {
132 unsigned int nr_tries = 0;
133 ++get_value_statistics_.total_counter;
134 while (!command_interface_.get_value(value))
135 {
136 ++get_value_statistics_.failed_counter;
137 ++nr_tries;
138 if (nr_tries == max_tries)
139 {
140 ++get_value_statistics_.timeout_counter;
141 return false;
142 }
143 std::this_thread::yield();
144 }
145 return true;
146 }
147
148protected:
149 CommandInterface & command_interface_;
150 Deleter deleter_;
151
152private:
153 struct HandleRTStatistics
154 {
155 unsigned int total_counter = 0;
156 unsigned int failed_counter = 0;
157 unsigned int timeout_counter = 0;
158 };
159 mutable HandleRTStatistics get_value_statistics_;
160 HandleRTStatistics set_value_statistics_;
161};
162
163} // namespace hardware_interface
164#endif // HARDWARE_INTERFACE__LOANED_COMMAND_INTERFACE_HPP_
Definition handle.hpp:221
Definition loaned_command_interface.hpp:30
Definition actuator.hpp:33