ros2_control - iron
Loading...
Searching...
No Matches
resource_manager.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__RESOURCE_MANAGER_HPP_
16#define HARDWARE_INTERFACE__RESOURCE_MANAGER_HPP_
17
18#include <memory>
19#include <string>
20#include <unordered_map>
21#include <vector>
22
23#include "hardware_interface/actuator.hpp"
24#include "hardware_interface/hardware_component_info.hpp"
25#include "hardware_interface/hardware_info.hpp"
26#include "hardware_interface/loaned_command_interface.hpp"
27#include "hardware_interface/loaned_state_interface.hpp"
28#include "hardware_interface/sensor.hpp"
29#include "hardware_interface/system.hpp"
30#include "hardware_interface/types/hardware_interface_return_values.hpp"
31#include "hardware_interface/types/lifecycle_state_names.hpp"
32#include "lifecycle_msgs/msg/state.hpp"
33#include "rclcpp/duration.hpp"
34#include "rclcpp/node.hpp"
35#include "rclcpp/time.hpp"
36
37namespace hardware_interface
38{
39class ResourceStorage;
40class ControllerManager;
41
43{
44 bool ok;
45 std::vector<std::string> failed_hardware_names;
46};
47
48class HARDWARE_INTERFACE_PUBLIC ResourceManager
49{
50public:
53 unsigned int update_rate = 100,
54 rclcpp::node_interfaces::NodeClockInterface::SharedPtr clock_interface = nullptr);
55
57
71 explicit ResourceManager(
72 const std::string & urdf, bool validate_interfaces = true, bool activate_all = false,
73 unsigned int update_rate = 100,
74 rclcpp::node_interfaces::NodeClockInterface::SharedPtr clock_interface = nullptr);
75
76 ResourceManager(const ResourceManager &) = delete;
77
79
81
92 void load_urdf(
93 const std::string & urdf, bool validate_interfaces = true,
94 bool load_and_initialize_components = true);
95
104 bool is_urdf_already_loaded() const;
105
107
115 LoanedStateInterface claim_state_interface(const std::string & key);
116
118
122 std::vector<std::string> state_interface_keys() const;
123
125
129 std::vector<std::string> available_state_interfaces() const;
130
132
135 bool state_interface_is_available(const std::string & name) const;
136
138
148 const std::string & controller_name, std::vector<CommandInterface> & interfaces);
149
151
157 std::vector<std::string> get_controller_reference_interface_names(
158 const std::string & controller_name);
159
161
168 void make_controller_reference_interfaces_available(const std::string & controller_name);
169
171
178 void make_controller_reference_interfaces_unavailable(const std::string & controller_name);
179
181
187 void remove_controller_reference_interfaces(const std::string & controller_name);
188
190
198 const std::string & controller_name, const std::vector<std::string> & interfaces);
199
201
208 std::vector<std::string> get_cached_controllers_to_hardware(const std::string & hardware_name);
209
211
219 bool command_interface_is_claimed(const std::string & key) const;
220
222
230 LoanedCommandInterface claim_command_interface(const std::string & key);
231
233
237 std::vector<std::string> command_interface_keys() const;
238
240
244 std::vector<std::string> available_command_interfaces() const;
245
247
251 bool command_interface_is_available(const std::string & interface) const;
252
254
257 size_t actuator_components_size() const;
258
260
263 size_t sensor_components_size() const;
264
266
269 size_t system_components_size() const;
270
272
284 void import_component(
285 std::unique_ptr<ActuatorInterface> actuator, const HardwareInfo & hardware_info);
286
288
300 void import_component(
301 std::unique_ptr<SensorInterface> sensor, const HardwareInfo & hardware_info);
302
304
316 void import_component(
317 std::unique_ptr<SystemInterface> system, const HardwareInfo & hardware_info);
318
320
323 std::unordered_map<std::string, HardwareComponentInfo> get_components_status();
324
326
343 const std::vector<std::string> & start_interfaces,
344 const std::vector<std::string> & stop_interfaces);
345
347
360 const std::vector<std::string> & start_interfaces,
361 const std::vector<std::string> & stop_interfaces);
362
364
376 return_type set_component_state(
377 const std::string & component_name, rclcpp_lifecycle::State & target_state);
378
380
386 HardwareReadWriteStatus read(const rclcpp::Time & time, const rclcpp::Duration & period);
387
389
395 HardwareReadWriteStatus write(const rclcpp::Time & time, const rclcpp::Duration & period);
396
398
402 bool command_interface_exists(const std::string & key) const;
403
405
408 bool state_interface_exists(const std::string & key) const;
409
410private:
411 void validate_storage(const std::vector<hardware_interface::HardwareInfo> & hardware_info) const;
412
413 void release_command_interface(const std::string & key);
414
415 std::unordered_map<std::string, bool> claimed_command_interface_map_;
416
417 mutable std::recursive_mutex resource_interfaces_lock_;
418 mutable std::recursive_mutex claimed_command_interfaces_lock_;
419 mutable std::recursive_mutex resources_lock_;
420
421 std::unique_ptr<ResourceStorage> resource_storage_;
422
423 // Structure to store read and write status so it is not initialized in the real-time loop
424 HardwareReadWriteStatus read_write_status;
425
426 bool is_urdf_loaded__ = false;
427};
428
429} // namespace hardware_interface
430#endif // HARDWARE_INTERFACE__RESOURCE_MANAGER_HPP_
Definition loaned_command_interface.hpp:27
Definition loaned_state_interface.hpp:27
void make_controller_reference_interfaces_available(const std::string &controller_name)
Add controller's reference interface to available list.
Definition resource_manager.cpp:863
std::vector< std::string > command_interface_keys() const
Returns all registered command interfaces keys.
Definition resource_manager.cpp:997
bool command_interface_exists(const std::string &key) const
Checks whether a command interface is registered under the given key.
Definition resource_manager.cpp:1417
bool command_interface_is_claimed(const std::string &key) const
Checks whether a command interface is already claimed.
Definition resource_manager.cpp:956
return_type set_component_state(const std::string &component_name, rclcpp_lifecycle::State &target_state)
Sets state of hardware component.
Definition resource_manager.cpp:1227
std::unordered_map< std::string, HardwareComponentInfo > get_components_status()
Return status for all components.
Definition resource_manager.cpp:1056
void import_component(std::unique_ptr< ActuatorInterface > actuator, const HardwareInfo &hardware_info)
Import a hardware component which is not listed in the URDF.
Definition resource_manager.cpp:1025
size_t sensor_components_size() const
Return the number of loaded sensor components.
Definition resource_manager.cpp:1407
void load_urdf(const std::string &urdf, bool validate_interfaces=true, bool load_and_initialize_components=true)
Load resources from on a given URDF.
Definition resource_manager.cpp:759
std::vector< std::string > state_interface_keys() const
Returns all registered state interfaces keys.
Definition resource_manager.cpp:818
bool is_urdf_already_loaded() const
if the resource manager load_urdf(...) function has been called this returns true....
Definition resource_manager.cpp:803
size_t system_components_size() const
Return the number of loaded system components.
Definition resource_manager.cpp:1412
std::vector< std::string > get_controller_reference_interface_names(const std::string &controller_name)
Get list of reference interface of a controller.
Definition resource_manager.cpp:856
bool command_interface_is_available(const std::string &interface) const
Checks whether a command interface is available under the given name.
Definition resource_manager.cpp:1016
void import_controller_reference_interfaces(const std::string &controller_name, std::vector< CommandInterface > &interfaces)
Add controllers' reference interfaces to resource manager.
Definition resource_manager.cpp:847
std::vector< std::string > get_cached_controllers_to_hardware(const std::string &hardware_name)
Return cached controllers for a specific hardware.
Definition resource_manager.cpp:949
bool state_interface_is_available(const std::string &name) const
Checks whether a state interface is available under the given key.
Definition resource_manager.cpp:837
bool state_interface_exists(const std::string &key) const
Checks whether a state interface is registered under the given key.
Definition resource_manager.cpp:1424
void make_controller_reference_interfaces_unavailable(const std::string &controller_name)
Remove controller's reference interface to available list.
Definition resource_manager.cpp:875
size_t actuator_components_size() const
Return the number size_t of loaded actuator components.
Definition resource_manager.cpp:1402
HardwareReadWriteStatus read(const rclcpp::Time &time, const rclcpp::Duration &period)
Reads all loaded hardware components.
Definition resource_manager.cpp:1330
std::vector< std::string > available_command_interfaces() const
Returns all available command interfaces keys.
Definition resource_manager.cpp:1009
void remove_controller_reference_interfaces(const std::string &controller_name)
Remove controllers reference interfaces from resource manager.
Definition resource_manager.cpp:898
bool prepare_command_mode_switch(const std::vector< std::string > &start_interfaces, const std::vector< std::string > &stop_interfaces)
Prepare the hardware components for a new command interface mode.
Definition resource_manager.cpp:1077
bool perform_command_mode_switch(const std::vector< std::string > &start_interfaces, const std::vector< std::string > &stop_interfaces)
Notify the hardware components that realtime hardware mode switching should occur.
Definition resource_manager.cpp:1187
std::vector< std::string > available_state_interfaces() const
Returns all available state interfaces keys.
Definition resource_manager.cpp:830
LoanedCommandInterface claim_command_interface(const std::string &key)
Claim a command interface given its key.
Definition resource_manager.cpp:968
ResourceManager(unsigned int update_rate=100, rclcpp::node_interfaces::NodeClockInterface::SharedPtr clock_interface=nullptr)
Default constructor for the Resource Manager.
Definition resource_manager.cpp:732
void cache_controller_to_hardware(const std::string &controller_name, const std::vector< std::string > &interfaces)
Cache mapping between hardware and controllers using it.
Definition resource_manager.cpp:909
LoanedStateInterface claim_state_interface(const std::string &key)
Claim a state interface given its key.
Definition resource_manager.cpp:806
HardwareReadWriteStatus write(const rclcpp::Time &time, const rclcpp::Duration &period)
Write all loaded hardware components.
Definition resource_manager.cpp:1370
Definition resource_manager.cpp:71
Definition mujoco_system_interface.hpp:57
This structure stores information about hardware defined in a robot's URDF.
Definition hardware_info.hpp:107
Definition resource_manager.hpp:43