ros2_control - rolling
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 <optional>
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 explicit LoanedStateInterface(StateInterface::ConstSharedPtr state_interface)
34 : LoanedStateInterface(state_interface, nullptr)
35 {
36 }
37
38 explicit LoanedStateInterface(StateInterface::ConstSharedPtr state_interface, Deleter && deleter)
39 : state_interface_(state_interface),
40 interface_name_(state_interface->get_name()),
41 deleter_(std::forward<Deleter>(deleter))
42 {
43 }
44
45 LoanedStateInterface(const LoanedStateInterface & other) = delete;
46
48
49 virtual ~LoanedStateInterface()
50 {
51 if (state_interface_)
52 {
53 auto logger = rclcpp::get_logger(interface_name_);
54 RCLCPP_WARN_EXPRESSION(
55 logger,
56 (get_value_statistics_.failed_counter > 0 || get_value_statistics_.timeout_counter > 0),
57 "LoanedStateInterface %s has %u (%.4f %%) timeouts and %u (%.4f %%) missed calls out of %u "
58 "get_value calls",
59 interface_name_.c_str(), get_value_statistics_.timeout_counter,
60 (get_value_statistics_.timeout_counter * 100.0) / get_value_statistics_.total_counter,
61 get_value_statistics_.failed_counter,
62 (get_value_statistics_.failed_counter * 100.0) / get_value_statistics_.total_counter,
63 get_value_statistics_.total_counter);
64 }
65 if (deleter_)
66 {
67 deleter_();
68 }
69 }
70
71 const std::string & get_name() const { return state_interface_->get_name(); }
72
73 const std::string & get_interface_name() const { return state_interface_->get_interface_name(); }
74
75 const std::string & get_prefix_name() const { return state_interface_->get_prefix_name(); }
76
91 template <typename T = double>
92 [[nodiscard]] std::optional<T> get_optional(unsigned int max_tries = 10) const
93 {
94 unsigned int nr_tries = 0;
95 do
96 {
97 ++get_value_statistics_.total_counter;
98 const std::optional<T> data = state_interface_->get_optional<T>();
99 if (data.has_value())
100 {
101 return data;
102 }
103 ++get_value_statistics_.failed_counter;
104 ++nr_tries;
105 std::this_thread::yield();
106 } while (nr_tries < max_tries);
107
108 ++get_value_statistics_.timeout_counter;
109 return std::nullopt;
110 }
111
116 HandleDataType get_data_type() const { return state_interface_->get_data_type(); }
117
122 bool is_castable_to_double() const { return state_interface_->is_castable_to_double(); }
123
124protected:
125 StateInterface::ConstSharedPtr state_interface_;
126 std::string interface_name_;
127 Deleter deleter_;
128
129private:
130 struct HandleRTStatistics
131 {
132 unsigned int total_counter = 0;
133 unsigned int failed_counter = 0;
134 unsigned int timeout_counter = 0;
135 };
136 mutable HandleRTStatistics get_value_statistics_;
137};
138
139} // namespace hardware_interface
140#endif // HARDWARE_INTERFACE__LOANED_STATE_INTERFACE_HPP_
Definition hardware_info.hpp:158
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:92
HandleDataType get_data_type() const
Get the data type of the state interface.
Definition loaned_state_interface.hpp:116
bool is_castable_to_double() const
Check if the state interface can be casted to double.
Definition loaned_state_interface.hpp:122
Definition mujoco_system_interface.hpp:57