ros2_control - rolling
Loading...
Searching...
No Matches
hardware_info.hpp
1// Copyright 2020 ros2_control Development Team
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__HARDWARE_INFO_HPP_
16#define HARDWARE_INTERFACE__HARDWARE_INFO_HPP_
17
18#include <fmt/compile.h>
19
20#include <cstdint>
21#include <stdexcept>
22#include <string>
23#include <unordered_map>
24#include <variant>
25#include <vector>
26
27#include "joint_limits/joint_limits.hpp"
28
29#ifdef TRUE
30#undef TRUE
31#endif
32
33#ifdef FALSE
34#undef FALSE
35#endif
36
37namespace hardware_interface
38{
44{
49 std::string name;
51 std::string min = "";
53 std::string max = "";
55 std::string initial_value = "";
57 std::string data_type = "double";
59 int size;
63 std::unordered_map<std::string, std::string> parameters;
66};
67
70{
71 std::size_t joint_index;
72 std::size_t mimicked_joint_index;
73 double multiplier = 1.0;
74 double offset = 0.0;
75};
76
79{
80 NOT_SET,
81 TRUE,
82 FALSE
83};
84
90{
92 std::string name;
94 std::string type;
95
97 MimicAttribute is_mimic = MimicAttribute::NOT_SET;
98
100 bool enable_limits = true;
101
106 std::vector<InterfaceInfo> command_interfaces;
111 std::vector<InterfaceInfo> state_interfaces;
113 std::unordered_map<std::string, std::string> parameters;
114};
115
118{
119 std::string name;
120 std::vector<std::string> state_interfaces;
121 std::vector<std::string> command_interfaces;
122 std::string role;
123 double mechanical_reduction = 1.0;
124 double offset = 0.0;
125 bool read_only = false;
126};
127
130{
131 std::string name;
132 std::vector<std::string> state_interfaces;
133 std::vector<std::string> command_interfaces;
134 std::string role;
135 double mechanical_reduction = 1.0;
136 double offset = 0.0;
137 bool read_only = false;
138};
139
142{
143 std::string name;
144 std::string type;
145 std::vector<JointInfo> joints;
146 std::vector<ActuatorInfo> actuators;
148 std::unordered_map<std::string, std::string> parameters;
149};
150
154
155using HANDLE_DATATYPE = std::variant<
156 std::monostate, double, float, bool, uint8_t, int8_t, uint16_t, int16_t, uint32_t, int32_t,
157 uint64_t, int64_t>;
158class HandleDataType
159{
160public:
161 enum Value : int8_t
162 {
163 UNKNOWN = -1,
164 DOUBLE,
165 FLOAT32,
166 BOOL,
167 UINT8,
168 INT8,
169 UINT16,
170 INT16,
171 UINT32,
172 INT32,
173 UINT64,
174 INT64,
175 };
176
177 HandleDataType() = default;
178 constexpr HandleDataType(Value value) : value_(value) {} // NOLINT(runtime/explicit)
179 explicit HandleDataType(const std::string & data_type)
180 {
181 if (data_type == "double")
182 {
183 value_ = DOUBLE;
184 }
185 else if (data_type == "bool")
186 {
187 value_ = BOOL;
188 }
189 else if (data_type == "float32")
190 {
191 value_ = FLOAT32;
192 }
193 else if (data_type == "uint8")
194 {
195 value_ = UINT8;
196 }
197 else if (data_type == "int8")
198 {
199 value_ = INT8;
200 }
201 else if (data_type == "uint16")
202 {
203 value_ = UINT16;
204 }
205 else if (data_type == "int16")
206 {
207 value_ = INT16;
208 }
209 else if (data_type == "uint32")
210 {
211 value_ = UINT32;
212 }
213 else if (data_type == "int32")
214 {
215 value_ = INT32;
216 }
217 else if (data_type == "uint64")
218 {
219 value_ = UINT64;
220 }
221 else if (data_type == "int64")
222 {
223 value_ = INT64;
224 }
225 else
226 {
227 value_ = UNKNOWN;
228 }
229 }
230
231 operator Value() const { return value_; }
232
233 explicit operator bool() const = delete;
234
235 constexpr bool operator==(HandleDataType other) const { return value_ == other.value_; }
236 constexpr bool operator!=(HandleDataType other) const { return value_ != other.value_; }
237
238 constexpr bool operator==(Value other) const { return value_ == other; }
239 constexpr bool operator!=(Value other) const { return value_ != other; }
240
241 std::string to_string() const
242 {
243 switch (value_)
244 {
245 case DOUBLE:
246 return "double";
247 case BOOL:
248 return "bool";
249 case FLOAT32:
250 return "float32";
251 case UINT8:
252 return "uint8";
253 case INT8:
254 return "int8";
255 case UINT16:
256 return "uint16";
257 case INT16:
258 return "int16";
259 case UINT32:
260 return "uint32";
261 case INT32:
262 return "int32";
263 case UINT64:
264 return "uint64";
265 case INT64:
266 return "int64";
267 default:
268 return "unknown";
269 }
270 }
271
279 {
280 switch (value_)
281 {
282 case DOUBLE: // fallthrough
283 case FLOAT32: // fallthrough
284 case BOOL: // fallthrough
285 case UINT8: // fallthrough
286 case INT8: // fallthrough
287 case UINT16: // fallthrough
288 case INT16: // fallthrough
289 case UINT32: // fallthrough
290 case INT32:
291 return true;
292 default:
293 return false; // unknown type cannot be converted
294 }
295 }
296
304 double cast_to_double(const HANDLE_DATATYPE & value) const
305 {
306 switch (value_)
307 {
308 case DOUBLE:
309 return std::get<double>(value);
310 case FLOAT32:
311 return static_cast<double>(std::get<float>(value));
312 case BOOL:
313 return static_cast<double>(std::get<bool>(value));
314 case UINT8:
315 return static_cast<double>(std::get<uint8_t>(value));
316 case INT8:
317 return static_cast<double>(std::get<int8_t>(value));
318 case UINT16:
319 return static_cast<double>(std::get<uint16_t>(value));
320 case INT16:
321 return static_cast<double>(std::get<int16_t>(value));
322 case UINT32:
323 return static_cast<double>(std::get<uint32_t>(value));
324 case INT32:
325 return static_cast<double>(std::get<int32_t>(value));
326 // UINT64 and INT64 are not castable, see is_castable_to_double().
327 default:
328 throw std::runtime_error(
329 fmt::format(FMT_COMPILE("Data type : '{}' cannot be casted to double."), to_string()));
330 }
331 }
332
333 HandleDataType from_string(const std::string & data_type) { return HandleDataType(data_type); }
334
335private:
336 Value value_ = UNKNOWN;
337};
338
343struct InterfaceDescription
344{
345 InterfaceDescription(const std::string & prefix_name_in, const InterfaceInfo & interface_info_in)
346 : prefix_name(prefix_name_in),
347 interface_info(interface_info_in),
349 {
350 }
351
355 std::string prefix_name;
356
361
365 std::string interface_name;
366
367 const std::string & get_prefix_name() const { return prefix_name; }
368
369 const std::string & get_interface_name() const { return interface_info.name; }
370
371 const std::string & get_name() const { return interface_name; }
372
373 const std::string & get_data_type_string() const { return interface_info.data_type; }
374
375 HandleDataType get_data_type() const { return HandleDataType(interface_info.data_type); }
376};
377
379{
383 std::string scheduling_policy = "synchronized";
385 std::vector<int> cpu_affinity_cores = {};
387 bool print_warnings = true;
388};
389
392{
394 std::string name;
396 std::string type;
398 std::string group;
400 unsigned int rw_rate;
408 std::unordered_map<std::string, std::string> hardware_parameters;
413 std::vector<ComponentInfo> joints;
417 std::vector<MimicJoint> mimic_joints;
422 std::vector<ComponentInfo> sensors;
427 std::vector<ComponentInfo> gpios;
432 std::vector<TransmissionInfo> transmissions;
436 std::string original_xml;
440 std::unordered_map<std::string, joint_limits::JointLimits> limits;
441
447 std::unordered_map<std::string, joint_limits::SoftJointLimits> soft_limits;
448};
449
450} // namespace hardware_interface
451#endif // HARDWARE_INTERFACE__HARDWARE_INFO_HPP_
Definition hardware_info.hpp:159
bool is_castable_to_double() const
Check if the HandleDataType can be casted to double.
Definition hardware_info.hpp:278
double cast_to_double(const HANDLE_DATATYPE &value) const
Cast the given value to double.
Definition hardware_info.hpp:304
Definition mujoco_system_interface.hpp:57
MimicAttribute
This enum is used to store the mimic attribute of a joint.
Definition hardware_info.hpp:79
std::variant< std::monostate, double, float, bool, uint8_t, int8_t, uint16_t, int16_t, uint32_t, int32_t, uint64_t, int64_t > HANDLE_DATATYPE
Definition hardware_info.hpp:155
Contains semantic info about a given actuator loaded from URDF for a transmission.
Definition hardware_info.hpp:130
Definition hardware_info.hpp:90
bool enable_limits
Whether limits are enabled for this component (set at joint level with <limits enable="....
Definition hardware_info.hpp:100
std::vector< InterfaceInfo > command_interfaces
Definition hardware_info.hpp:106
std::unordered_map< std::string, std::string > parameters
(Optional) Key-value pairs of component parameters, e.g. min/max values or serial number.
Definition hardware_info.hpp:113
std::vector< InterfaceInfo > state_interfaces
Definition hardware_info.hpp:111
std::string name
Name of the component.
Definition hardware_info.hpp:92
MimicAttribute is_mimic
Hold the value of the mimic attribute if given, NOT_SET otherwise.
Definition hardware_info.hpp:97
std::string type
Type of the component: sensor, joint, or GPIO.
Definition hardware_info.hpp:94
Definition hardware_info.hpp:379
bool print_warnings
Whether to print warnings when the async thread doesn't meet its deadline.
Definition hardware_info.hpp:387
std::string scheduling_policy
Scheduling policy for the async worker thread.
Definition hardware_info.hpp:383
int thread_priority
Thread priority for the async worker thread.
Definition hardware_info.hpp:381
std::vector< int > cpu_affinity_cores
CPU affinity cores for the async worker thread.
Definition hardware_info.hpp:385
This structure stores information about hardware defined in a robot's URDF.
Definition hardware_info.hpp:392
std::string type
Type of the hardware: actuator, sensor or system.
Definition hardware_info.hpp:396
std::vector< MimicJoint > mimic_joints
Definition hardware_info.hpp:417
std::string hardware_plugin_name
Name of the pluginlib plugin of the hardware that will be loaded.
Definition hardware_info.hpp:406
std::unordered_map< std::string, joint_limits::JointLimits > limits
Definition hardware_info.hpp:440
HardwareAsyncParams async_params
Async Parameters.
Definition hardware_info.hpp:404
std::unordered_map< std::string, std::string > hardware_parameters
(Optional) Key-value pairs for hardware parameters.
Definition hardware_info.hpp:408
std::string group
Hardware group to which the hardware belongs.
Definition hardware_info.hpp:398
bool is_async
Component is async.
Definition hardware_info.hpp:402
std::unordered_map< std::string, joint_limits::SoftJointLimits > soft_limits
Definition hardware_info.hpp:447
std::vector< ComponentInfo > gpios
Definition hardware_info.hpp:427
std::vector< ComponentInfo > joints
Definition hardware_info.hpp:413
std::string original_xml
Definition hardware_info.hpp:436
std::string name
Name of the hardware.
Definition hardware_info.hpp:394
std::vector< ComponentInfo > sensors
Definition hardware_info.hpp:422
std::vector< TransmissionInfo > transmissions
Definition hardware_info.hpp:432
unsigned int rw_rate
Component's read and write rates in Hz.
Definition hardware_info.hpp:400
InterfaceInfo interface_info
Definition hardware_info.hpp:360
std::string interface_name
Definition hardware_info.hpp:365
std::string prefix_name
Definition hardware_info.hpp:355
Definition hardware_info.hpp:44
std::string max
(Optional) Maximal allowed values of the interface.
Definition hardware_info.hpp:53
std::string initial_value
(Optional) Initial value of the interface.
Definition hardware_info.hpp:55
std::string name
Definition hardware_info.hpp:49
int size
(Optional) If the handle is an array, the size of the array.
Definition hardware_info.hpp:59
std::string min
(Optional) Minimal allowed values of the interface.
Definition hardware_info.hpp:51
std::string data_type
(Optional) The datatype of the interface, e.g. "bool", "int".
Definition hardware_info.hpp:57
std::unordered_map< std::string, std::string > parameters
Definition hardware_info.hpp:63
bool enable_limits
(Optional) enable or disable the limits for the command interfaces
Definition hardware_info.hpp:65
Contains semantic info about a given joint loaded from URDF for a transmission.
Definition hardware_info.hpp:118
This structure stores information about a joint that is mimicking another joint.
Definition hardware_info.hpp:70
Contains semantic info about a given transmission loaded from URDF.
Definition hardware_info.hpp:142
std::unordered_map< std::string, std::string > parameters
(Optional) Key-value pairs of custom parameters
Definition hardware_info.hpp:148