ros2_control - kilted
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 explicit LoanedStateInterface(StateInterface::ConstSharedPtr state_interface)
35 : LoanedStateInterface(state_interface, nullptr)
36 {
37 }
38
39 explicit LoanedStateInterface(StateInterface::ConstSharedPtr state_interface, Deleter && deleter)
40 : state_interface_(state_interface),
41 interface_name_(state_interface->get_name()),
42 deleter_(std::forward<Deleter>(deleter))
43 {
44 }
45
46 LoanedStateInterface(const LoanedStateInterface & other) = delete;
47
49
50 virtual ~LoanedStateInterface()
51 {
52 if (state_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 "LoanedStateInterface %s has %u (%.4f %%) timeouts and %u (%.4f %%) missed calls out of %u "
59 "get_value calls",
60 interface_name_.c_str(), get_value_statistics_.timeout_counter,
61 (get_value_statistics_.timeout_counter * 100.0) / get_value_statistics_.total_counter,
62 get_value_statistics_.failed_counter,
63 (get_value_statistics_.failed_counter * 100.0) / get_value_statistics_.total_counter,
64 get_value_statistics_.total_counter);
65 }
66 if (deleter_)
67 {
68 deleter_();
69 }
70 }
71
72 const std::string & get_name() const { return state_interface_->get_name(); }
73
74 const std::string & get_interface_name() const { return state_interface_->get_interface_name(); }
75
76 const std::string & get_prefix_name() const { return state_interface_->get_prefix_name(); }
77
78 [[deprecated(
79 "Use std::optional<T> get_optional() instead to retrieve the value. This method will be "
80 "removed by the ROS 2 Lyrical Luth release.")]]
81 double get_value() const
82 {
83 std::optional<double> opt_value = get_optional();
84 if (opt_value.has_value())
85 {
86 return opt_value.value();
87 }
88 else
89 {
90 return std::numeric_limits<double>::quiet_NaN();
91 }
92 }
93
108 template <typename T = double>
109 [[nodiscard]] std::optional<T> get_optional(unsigned int max_tries = 10) const
110 {
111 unsigned int nr_tries = 0;
112 do
113 {
114 ++get_value_statistics_.total_counter;
115 const std::optional<T> data = state_interface_->get_optional<T>();
116 if (data.has_value())
117 {
118 return data;
119 }
120 ++get_value_statistics_.failed_counter;
121 ++nr_tries;
122 std::this_thread::yield();
123 } while (nr_tries < max_tries);
124
125 ++get_value_statistics_.timeout_counter;
126 return std::nullopt;
127 }
128
133 HandleDataType get_data_type() const { return state_interface_->get_data_type(); }
134
139 bool is_castable_to_double() const { return state_interface_->is_castable_to_double(); }
140
141protected:
142 StateInterface::ConstSharedPtr state_interface_;
143 std::string interface_name_;
144 Deleter deleter_;
145
146private:
147 struct HandleRTStatistics
148 {
149 unsigned int total_counter = 0;
150 unsigned int failed_counter = 0;
151 unsigned int timeout_counter = 0;
152 };
153 mutable HandleRTStatistics get_value_statistics_;
154};
155
156} // namespace hardware_interface
157#endif // HARDWARE_INTERFACE__LOANED_STATE_INTERFACE_HPP_
Definition hardware_info.hpp:158
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:109
HandleDataType get_data_type() const
Get the data type of the state interface.
Definition loaned_state_interface.hpp:133
bool is_castable_to_double() const
Check if the state interface can be casted to double.
Definition loaned_state_interface.hpp:139
Definition mujoco_system_interface.hpp:57