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#pragma GCC diagnostic push
27#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
28#include "hardware_interface/loaned_command_interface.hpp"
29#include "hardware_interface/loaned_state_interface.hpp"
30#pragma GCC diagnostic pop
31#include "hardware_interface/sensor.hpp"
32#include "hardware_interface/system.hpp"
33#include "hardware_interface/system_interface.hpp"
34#include "hardware_interface/types/hardware_interface_return_values.hpp"
35#include "rclcpp/duration.hpp"
36#include "rclcpp/node_interfaces/node_logging_interface.hpp"
37#include "rclcpp/time.hpp"
38
39namespace hardware_interface
40{
41class ResourceStorage;
42class ControllerManager;
43
45{
46 bool ok;
47 std::vector<std::string> failed_hardware_names;
48};
49
51{
52public:
54 explicit ResourceManager(
55 rclcpp::node_interfaces::NodeClockInterface::SharedPtr clock_interface,
56 rclcpp::node_interfaces::NodeLoggingInterface::SharedPtr logger_interface);
57
59 explicit ResourceManager(rclcpp::Clock::SharedPtr clock, rclcpp::Logger logger);
60
62
76 explicit ResourceManager(
77 const std::string & urdf,
78 rclcpp::node_interfaces::NodeClockInterface::SharedPtr clock_interface,
79 rclcpp::node_interfaces::NodeLoggingInterface::SharedPtr logger_interface,
80 bool activate_all = false, const unsigned int update_rate = 100);
81
83
97 explicit ResourceManager(
98 const std::string & urdf, rclcpp::Clock::SharedPtr clock, rclcpp::Logger logger,
99 bool activate_all = false, const unsigned int update_rate = 100);
100
101 ResourceManager(const ResourceManager &) = delete;
102
103 virtual ~ResourceManager();
104
106
110 bool shutdown_components();
111
113
123 const std::string & urdf, const unsigned int update_rate = 100);
124
129 void import_joint_limiters(const std::string & urdf);
130
139 bool are_components_initialized() const;
140
142
150 LoanedStateInterface claim_state_interface(const std::string & key);
151
153
157 std::vector<std::string> state_interface_keys() const;
158
160
164 std::vector<std::string> available_state_interfaces() const;
165
167
170 bool state_interface_is_available(const std::string & name) const;
171
173
178 std::string get_state_interface_data_type(const std::string & name) const;
179
181
191 const std::string & controller_name, std::vector<StateInterface::ConstSharedPtr> & interfaces);
192
194
200 std::vector<std::string> get_controller_exported_state_interface_names(
201 const std::string & controller_name);
202
204
212 void make_controller_exported_state_interfaces_available(const std::string & controller_name);
213
215
222 void make_controller_exported_state_interfaces_unavailable(const std::string & controller_name);
223
225
231 void remove_controller_exported_state_interfaces(const std::string & controller_name);
232
234
244 const std::string & controller_name,
245 const std::vector<hardware_interface::CommandInterface::SharedPtr> & interfaces);
246
248
254 std::vector<std::string> get_controller_reference_interface_names(
255 const std::string & controller_name);
256
258
265 void make_controller_reference_interfaces_available(const std::string & controller_name);
266
268
275 void make_controller_reference_interfaces_unavailable(const std::string & controller_name);
276
278
284 void remove_controller_reference_interfaces(const std::string & controller_name);
285
287
295 const std::string & controller_name, const std::vector<std::string> & interfaces);
296
298
305 std::vector<std::string> get_cached_controllers_to_hardware(const std::string & hardware_name);
306
308
316 bool command_interface_is_claimed(const std::string & key) const;
317
319
327 LoanedCommandInterface claim_command_interface(const std::string & key);
328
330
334 std::vector<std::string> command_interface_keys() const;
335
337
341 std::vector<std::string> available_command_interfaces() const;
342
344
348 bool command_interface_is_available(const std::string & interface) const;
349
351
356 std::string get_command_interface_data_type(const std::string & name) const;
357
359
362 size_t actuator_components_size() const;
363
365
368 size_t sensor_components_size() const;
369
371
374 size_t system_components_size() const;
375
377
389 void import_component(
390 std::unique_ptr<ActuatorInterface> actuator, const HardwareInfo & hardware_info);
391
393
405 void import_component(
406 std::unique_ptr<SensorInterface> sensor, const HardwareInfo & hardware_info);
407
409
421 void import_component(
422 std::unique_ptr<SystemInterface> system, const HardwareInfo & hardware_info);
423
425
428 const std::unordered_map<std::string, HardwareComponentInfo> & get_components_status();
429
431
448 const std::vector<std::string> & start_interfaces,
449 const std::vector<std::string> & stop_interfaces);
450
452
465 const std::vector<std::string> & start_interfaces,
466 const std::vector<std::string> & stop_interfaces);
467
469
481 return_type set_component_state(
482 const std::string & component_name, rclcpp_lifecycle::State & target_state);
483
490 bool enforce_command_limits(const rclcpp::Duration & period);
491
493
499 HardwareReadWriteStatus read(const rclcpp::Time & time, const rclcpp::Duration & period);
500
502
508 HardwareReadWriteStatus write(const rclcpp::Time & time, const rclcpp::Duration & period);
509
511
515 bool command_interface_exists(const std::string & key) const;
516
518
521 bool state_interface_exists(const std::string & key) const;
522
524
527 void set_on_component_state_switch_callback(std::function<void()> callback);
528
529 const std::string & get_robot_description() const;
530
531protected:
533
536 rclcpp::Logger get_logger() const;
537
539
542 rclcpp::Clock::SharedPtr get_clock() const;
543
544 bool components_are_loaded_and_initialized_ = false;
545
546 mutable std::recursive_mutex resource_interfaces_lock_;
547 mutable std::recursive_mutex claimed_command_interfaces_lock_;
548 mutable std::recursive_mutex resources_lock_;
549 mutable std::recursive_mutex joint_limiters_lock_;
550
551private:
552 bool validate_storage(const std::vector<hardware_interface::HardwareInfo> & hardware_info) const;
553
554 void release_command_interface(const std::string & key);
555
556 std::unordered_map<std::string, bool> claimed_command_interface_map_;
557
558 std::unique_ptr<ResourceStorage> resource_storage_;
559
560 // Structure to store read and write status so it is not initialized in the real-time loop
561 HardwareReadWriteStatus read_write_status;
562};
563
564} // namespace hardware_interface
565#endif // HARDWARE_INTERFACE__RESOURCE_MANAGER_HPP_
Definition loaned_command_interface.hpp:30
Definition loaned_state_interface.hpp:29
Definition resource_manager.hpp:51
void make_controller_reference_interfaces_available(const std::string &controller_name)
Add controller's reference interface to available list.
Definition resource_manager.cpp:1651
std::string get_state_interface_data_type(const std::string &name) const
Gets the data type of the state interface.
Definition resource_manager.cpp:1556
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:1634
std::vector< std::string > command_interface_keys() const
Returns all registered command interfaces keys.
Definition resource_manager.cpp:1785
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:1419
bool command_interface_exists(const std::string &key) const
Checks whether a command interface is registered under the given key.
Definition resource_manager.cpp:2458
bool command_interface_is_claimed(const std::string &key) const
Checks whether a command interface is already claimed.
Definition resource_manager.cpp:1743
return_type set_component_state(const std::string &component_name, rclcpp_lifecycle::State &target_state)
Sets state of hardware component.
Definition resource_manager.cpp:2127
bool are_components_initialized() const
if the resource manager load_and_initialize_components(...) function has been called this returns tru...
Definition resource_manager.cpp:1509
void set_on_component_state_switch_callback(std::function< void()> callback)
A method to register a callback to be called when the component state changes.
Definition resource_manager.cpp:2472
bool shutdown_components()
Shutdown all hardware components, irrespective of their state.
Definition resource_manager.cpp:1402
void remove_controller_exported_state_interfaces(const std::string &controller_name)
Remove controllers exported state interfaces from resource manager.
Definition resource_manager.cpp:1622
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:1828
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:1588
size_t sensor_components_size() const
Return the number of loaded sensor components.
Definition resource_manager.cpp:2448
std::vector< std::string > state_interface_keys() const
Returns all registered state interfaces keys.
Definition resource_manager.cpp:1528
size_t system_components_size() const
Return the number of loaded system components.
Definition resource_manager.cpp:2453
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:1644
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:1804
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:1572
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:1600
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:1736
std::string get_command_interface_data_type(const std::string &name) const
Gets the data type of the command interface.
Definition resource_manager.cpp:1813
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:1547
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:1581
bool state_interface_exists(const std::string &key) const
Checks whether a state interface is registered under the given key.
Definition resource_manager.cpp:2465
void make_controller_reference_interfaces_unavailable(const std::string &controller_name)
Remove controller's reference interface to available list.
Definition resource_manager.cpp:1663
size_t actuator_components_size() const
Return the number size_t of loaded actuator components.
Definition resource_manager.cpp:2443
rclcpp::Clock::SharedPtr get_clock() const
Gets the clock for the resource manager.
Definition resource_manager.cpp:2486
HardwareReadWriteStatus read(const rclcpp::Time &time, const rclcpp::Duration &period)
Reads all loaded hardware components.
Definition resource_manager.cpp:2232
std::vector< std::string > available_command_interfaces() const
Returns all available command interfaces keys.
Definition resource_manager.cpp:1797
void remove_controller_reference_interfaces(const std::string &controller_name)
Remove controllers reference interfaces from resource manager.
Definition resource_manager.cpp:1685
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:1879
void import_joint_limiters(const std::string &urdf)
Import joint limiters from the URDF.
Definition resource_manager.cpp:1502
bool enforce_command_limits(const rclcpp::Duration &period)
Definition resource_manager.cpp:2211
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:2021
std::vector< std::string > available_state_interfaces() const
Returns all available state interfaces keys.
Definition resource_manager.cpp:1540
rclcpp::Logger get_logger() const
Gets the logger for the resource manager.
Definition resource_manager.cpp:2484
LoanedCommandInterface claim_command_interface(const std::string &key)
Claim a command interface given its key.
Definition resource_manager.cpp:1755
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:1696
LoanedStateInterface claim_state_interface(const std::string &key)
Claim a state interface given its key.
Definition resource_manager.cpp:1515
HardwareReadWriteStatus write(const rclcpp::Time &time, const rclcpp::Duration &period)
Write all loaded hardware components.
Definition resource_manager.cpp:2341
const std::unordered_map< std::string, HardwareComponentInfo > & get_components_status()
Return status for all components.
Definition resource_manager.cpp:1860
Definition actuator.hpp:34
This structure stores information about hardware defined in a robot's URDF.
Definition hardware_info.hpp:234
Definition resource_manager.hpp:45