ros2_control - rolling
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/system_interface.hpp"
31#include "hardware_interface/types/hardware_interface_return_values.hpp"
32#include "rclcpp/duration.hpp"
33#include "rclcpp/node_interfaces/node_logging_interface.hpp"
34#include "rclcpp/time.hpp"
35
36namespace hardware_interface
37{
38class ResourceStorage;
39class ControllerManager;
40
42{
43 bool ok;
44 std::vector<std::string> failed_hardware_names;
45};
46
48{
49public:
51 explicit ResourceManager(
52 rclcpp::node_interfaces::NodeClockInterface::SharedPtr clock_interface,
53 rclcpp::node_interfaces::NodeLoggingInterface::SharedPtr logger_interface);
54
56
69 explicit ResourceManager(
70 const std::string & urdf,
71 rclcpp::node_interfaces::NodeClockInterface::SharedPtr clock_interface,
72 rclcpp::node_interfaces::NodeLoggingInterface::SharedPtr logger_interface,
73 bool activate_all = false, const unsigned int update_rate = 100);
74
75 ResourceManager(const ResourceManager &) = delete;
76
77 virtual ~ResourceManager();
78
80
85
87
97 const std::string & urdf, const unsigned int update_rate = 100);
98
107 bool are_components_initialized() const;
108
110
118 LoanedStateInterface claim_state_interface(const std::string & key);
119
121
125 std::vector<std::string> state_interface_keys() const;
126
128
132 std::vector<std::string> available_state_interfaces() const;
133
135
138 bool state_interface_is_available(const std::string & name) const;
139
141
151 const std::string & controller_name, std::vector<StateInterface::ConstSharedPtr> & interfaces);
152
154
160 std::vector<std::string> get_controller_exported_state_interface_names(
161 const std::string & controller_name);
162
164
172 void make_controller_exported_state_interfaces_available(const std::string & controller_name);
173
175
182 void make_controller_exported_state_interfaces_unavailable(const std::string & controller_name);
183
185
191 void remove_controller_exported_state_interfaces(const std::string & controller_name);
192
194
204 const std::string & controller_name,
205 const std::vector<hardware_interface::CommandInterface::SharedPtr> & interfaces);
206
208
214 std::vector<std::string> get_controller_reference_interface_names(
215 const std::string & controller_name);
216
218
225 void make_controller_reference_interfaces_available(const std::string & controller_name);
226
228
235 void make_controller_reference_interfaces_unavailable(const std::string & controller_name);
236
238
244 void remove_controller_reference_interfaces(const std::string & controller_name);
245
247
255 const std::string & controller_name, const std::vector<std::string> & interfaces);
256
258
265 std::vector<std::string> get_cached_controllers_to_hardware(const std::string & hardware_name);
266
268
276 bool command_interface_is_claimed(const std::string & key) const;
277
279
287 LoanedCommandInterface claim_command_interface(const std::string & key);
288
290
294 std::vector<std::string> command_interface_keys() const;
295
297
301 std::vector<std::string> available_command_interfaces() const;
302
304
308 bool command_interface_is_available(const std::string & interface) const;
309
311
314 size_t actuator_components_size() const;
315
317
320 size_t sensor_components_size() const;
321
323
326 size_t system_components_size() const;
327
329
341 void import_component(
342 std::unique_ptr<ActuatorInterface> actuator, const HardwareInfo & hardware_info);
343
345
357 void import_component(
358 std::unique_ptr<SensorInterface> sensor, const HardwareInfo & hardware_info);
359
361
373 void import_component(
374 std::unique_ptr<SystemInterface> system, const HardwareInfo & hardware_info);
375
377
380 std::unordered_map<std::string, HardwareComponentInfo> get_components_status();
381
383
400 const std::vector<std::string> & start_interfaces,
401 const std::vector<std::string> & stop_interfaces);
402
404
417 const std::vector<std::string> & start_interfaces,
418 const std::vector<std::string> & stop_interfaces);
419
421
433 return_type set_component_state(
434 const std::string & component_name, rclcpp_lifecycle::State & target_state);
435
437
443 HardwareReadWriteStatus read(const rclcpp::Time & time, const rclcpp::Duration & period);
444
446
452 HardwareReadWriteStatus write(const rclcpp::Time & time, const rclcpp::Duration & period);
453
455
459 bool command_interface_exists(const std::string & key) const;
460
462
465 bool state_interface_exists(const std::string & key) const;
466
467protected:
469
472 rclcpp::Logger get_logger() const;
473
475
478 rclcpp::Clock::SharedPtr get_clock() const;
479
480 bool components_are_loaded_and_initialized_ = false;
481
482 mutable std::recursive_mutex resource_interfaces_lock_;
483 mutable std::recursive_mutex claimed_command_interfaces_lock_;
484 mutable std::recursive_mutex resources_lock_;
485
486private:
487 bool validate_storage(const std::vector<hardware_interface::HardwareInfo> & hardware_info) const;
488
489 void release_command_interface(const std::string & key);
490
491 std::unordered_map<std::string, bool> claimed_command_interface_map_;
492
493 std::unique_ptr<ResourceStorage> resource_storage_;
494
495 // Structure to store read and write status so it is not initialized in the real-time loop
496 HardwareReadWriteStatus read_write_status;
497};
498
499} // namespace hardware_interface
500#endif // HARDWARE_INTERFACE__RESOURCE_MANAGER_HPP_
Definition loaned_command_interface.hpp:30
Definition loaned_state_interface.hpp:29
Definition resource_manager.hpp:48
void make_controller_reference_interfaces_available(const std::string &controller_name)
Add controller's reference interface to available list.
Definition resource_manager.cpp:1272
void import_controller_reference_interfaces(const std::string &controller_name, const std::vector< hardware_interface::CommandInterface::SharedPtr > &interfaces)
Add controllers' reference interfaces to resource manager.
Definition resource_manager.cpp:1255
std::vector< std::string > command_interface_keys() const
Returns all registered command interfaces keys.
Definition resource_manager.cpp:1405
virtual bool load_and_initialize_components(const std::string &urdf, const unsigned int update_rate=100)
Load resources from on a given URDF.
Definition resource_manager.cpp:1064
bool command_interface_exists(const std::string &key) const
Checks whether a command interface is registered under the given key.
Definition resource_manager.cpp:1929
bool command_interface_is_claimed(const std::string &key) const
Checks whether a command interface is already claimed.
Definition resource_manager.cpp:1364
return_type set_component_state(const std::string &component_name, rclcpp_lifecycle::State &target_state)
Sets state of hardware component.
Definition resource_manager.cpp:1666
bool are_components_initialized() const
if the resource manager load_and_initialize_components(...) function has been called this returns tru...
Definition resource_manager.cpp:1146
bool shutdown_components()
Shutdown all hardware components, irrespective of their state.
Definition resource_manager.cpp:1047
void remove_controller_exported_state_interfaces(const std::string &controller_name)
Remove controllers exported state interfaces from resource manager.
Definition resource_manager.cpp:1243
std::unordered_map< std::string, HardwareComponentInfo > get_components_status()
Return status for all components.
Definition resource_manager.cpp:1464
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:1433
void make_controller_exported_state_interfaces_available(const std::string &controller_name)
Add controller's exported state interfaces to available list.
Definition resource_manager.cpp:1209
size_t sensor_components_size() const
Return the number of loaded sensor components.
Definition resource_manager.cpp:1919
std::vector< std::string > state_interface_keys() const
Returns all registered state interfaces keys.
Definition resource_manager.cpp:1164
size_t system_components_size() const
Return the number of loaded system components.
Definition resource_manager.cpp:1924
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:1265
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:1424
void import_controller_exported_state_interfaces(const std::string &controller_name, std::vector< StateInterface::ConstSharedPtr > &interfaces)
Add controllers' exported state interfaces to resource manager.
Definition resource_manager.cpp:1193
void make_controller_exported_state_interfaces_unavailable(const std::string &controller_name)
Remove controller's exported state interface to available list.
Definition resource_manager.cpp:1221
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:1357
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:1183
std::vector< std::string > get_controller_exported_state_interface_names(const std::string &controller_name)
Get list of exported tate interface of a controller.
Definition resource_manager.cpp:1202
bool state_interface_exists(const std::string &key) const
Checks whether a state interface is registered under the given key.
Definition resource_manager.cpp:1936
void make_controller_reference_interfaces_unavailable(const std::string &controller_name)
Remove controller's reference interface to available list.
Definition resource_manager.cpp:1284
size_t actuator_components_size() const
Return the number size_t of loaded actuator components.
Definition resource_manager.cpp:1914
rclcpp::Clock::SharedPtr get_clock() const
Gets the clock for the resource manager.
Definition resource_manager.cpp:1947
HardwareReadWriteStatus read(const rclcpp::Time &time, const rclcpp::Duration &period)
Reads all loaded hardware components.
Definition resource_manager.cpp:1750
std::vector< std::string > available_command_interfaces() const
Returns all available command interfaces keys.
Definition resource_manager.cpp:1417
void remove_controller_reference_interfaces(const std::string &controller_name)
Remove controllers reference interfaces from resource manager.
Definition resource_manager.cpp:1306
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:1483
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:1601
std::vector< std::string > available_state_interfaces() const
Returns all available state interfaces keys.
Definition resource_manager.cpp:1176
rclcpp::Logger get_logger() const
Gets the logger for the resource manager.
Definition resource_manager.cpp:1945
LoanedCommandInterface claim_command_interface(const std::string &key)
Claim a command interface given its key.
Definition resource_manager.cpp:1376
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:1317
LoanedStateInterface claim_state_interface(const std::string &key)
Claim a state interface given its key.
Definition resource_manager.cpp:1152
HardwareReadWriteStatus write(const rclcpp::Time &time, const rclcpp::Duration &period)
Write all loaded hardware components.
Definition resource_manager.cpp:1836
Definition actuator.hpp:33
This structure stores information about hardware defined in a robot's URDF.
Definition hardware_info.hpp:170
Definition resource_manager.hpp:42