ros2_control - jazzy
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 <functional>
19#include <memory>
20#include <mutex>
21#include <string>
22#include <unordered_map>
23#include <vector>
24
25#include "hardware_interface/actuator.hpp"
26#include "hardware_interface/actuator_interface.hpp"
27#include "hardware_interface/hardware_component_info.hpp"
28#include "hardware_interface/hardware_info.hpp"
29#pragma GCC diagnostic push
30#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
31#include "hardware_interface/loaned_command_interface.hpp"
32#include "hardware_interface/loaned_state_interface.hpp"
33#pragma GCC diagnostic pop
34#include "hardware_interface/sensor.hpp"
35#include "hardware_interface/sensor_interface.hpp"
36#include "hardware_interface/system.hpp"
37#include "hardware_interface/system_interface.hpp"
38#include "hardware_interface/types/hardware_component_params.hpp"
39#include "hardware_interface/types/hardware_interface_return_values.hpp"
40#include "hardware_interface/types/resource_manager_params.hpp"
41#include "rclcpp/clock.hpp"
42#include "rclcpp/duration.hpp"
43#include "rclcpp/executor.hpp"
44#include "rclcpp/logger.hpp"
45#include "rclcpp/node_interfaces/node_clock_interface.hpp"
46#include "rclcpp/node_interfaces/node_logging_interface.hpp"
47#include "rclcpp/time.hpp"
48#include "rclcpp_lifecycle/state.hpp"
49
50namespace hardware_interface
51{
52class ResourceStorage;
53class ControllerManager;
54
56{
57 return_type result;
58 std::vector<std::string> failed_hardware_names;
59};
60
62{
63public:
65 explicit ResourceManager(
66 rclcpp::node_interfaces::NodeClockInterface::SharedPtr clock_interface,
67 rclcpp::node_interfaces::NodeLoggingInterface::SharedPtr logger_interface);
68
70 explicit ResourceManager(rclcpp::Clock::SharedPtr clock, rclcpp::Logger logger);
71
73
87 explicit ResourceManager(
88 const std::string & urdf,
89 rclcpp::node_interfaces::NodeClockInterface::SharedPtr clock_interface,
90 rclcpp::node_interfaces::NodeLoggingInterface::SharedPtr logger_interface,
91 bool activate_all = false, const unsigned int update_rate = 100);
92
94
108 explicit ResourceManager(
109 const std::string & urdf, rclcpp::Clock::SharedPtr clock, rclcpp::Logger logger,
110 bool activate_all = false, const unsigned int update_rate = 100);
111
113
123 explicit ResourceManager(const hardware_interface::ResourceManagerParams & params, bool load);
124
125 ResourceManager(const ResourceManager &) = delete;
126
127 virtual ~ResourceManager();
128
130
134 bool shutdown_components();
135
137
146 [[deprecated(
147 "Use load_and_initialize_components(const ResourceManagerParams & params) "
148 "instead")]] virtual bool
149 load_and_initialize_components(const std::string & urdf, const unsigned int update_rate = 100);
150
152
163
168 void import_joint_limiters(const std::string & urdf);
169
178 bool are_components_initialized() const;
179
181
189 LoanedStateInterface claim_state_interface(const std::string & key);
190
192
196 std::vector<std::string> state_interface_keys() const;
197
199
203 std::vector<std::string> available_state_interfaces() const;
204
206
209 bool state_interface_is_available(const std::string & name) const;
210
212
217 std::string get_state_interface_data_type(const std::string & name) const;
218
220
230 const std::string & controller_name, std::vector<StateInterface::ConstSharedPtr> & interfaces);
231
233
239 std::vector<std::string> get_controller_exported_state_interface_names(
240 const std::string & controller_name);
241
243
251 void make_controller_exported_state_interfaces_available(const std::string & controller_name);
252
254
261 void make_controller_exported_state_interfaces_unavailable(const std::string & controller_name);
262
264
270 void remove_controller_exported_state_interfaces(const std::string & controller_name);
271
273
283 const std::string & controller_name,
284 const std::vector<hardware_interface::CommandInterface::SharedPtr> & interfaces);
285
287
293 std::vector<std::string> get_controller_reference_interface_names(
294 const std::string & controller_name);
295
297
304 void make_controller_reference_interfaces_available(const std::string & controller_name);
305
307
314 void make_controller_reference_interfaces_unavailable(const std::string & controller_name);
315
317
323 void remove_controller_reference_interfaces(const std::string & controller_name);
324
326
334 const std::string & controller_name, const std::vector<std::string> & interfaces);
335
337
344 std::vector<std::string> get_cached_controllers_to_hardware(const std::string & hardware_name);
345
347
355 bool command_interface_is_claimed(const std::string & key) const;
356
358
366 LoanedCommandInterface claim_command_interface(const std::string & key);
367
369
373 std::vector<std::string> command_interface_keys() const;
374
376
380 std::vector<std::string> available_command_interfaces() const;
381
383
387 bool command_interface_is_available(const std::string & interface) const;
388
390
395 std::string get_command_interface_data_type(const std::string & name) const;
396
398
401 size_t actuator_components_size() const;
402
404
407 size_t sensor_components_size() const;
408
410
413 size_t system_components_size() const;
414
416
428 [[deprecated(
429 "Use import_component(std::unique_ptr<ActuatorInterface> actuator, "
430 "const HardwareComponentParams & params) instead")]] void
431 import_component(std::unique_ptr<ActuatorInterface> actuator, const HardwareInfo & hardware_info);
432
434
446 [[deprecated(
447 "Use import_component(std::unique_ptr<SensorInterface> sensor, "
448 "const HardwareComponentParams & params) instead")]] void
449 import_component(std::unique_ptr<SensorInterface> sensor, const HardwareInfo & hardware_info);
450
452
464 [[deprecated(
465 "Use import_component(std::unique_ptr<SystemInterface> system, "
466 "const HardwareComponentParams & params) instead")]] void
467 import_component(std::unique_ptr<SystemInterface> system, const HardwareInfo & hardware_info);
468
470
483 void import_component(
484 std::unique_ptr<ActuatorInterface> actuator, const HardwareComponentParams & params);
485
487
500 void import_component(
501 std::unique_ptr<SensorInterface> sensor, const HardwareComponentParams & params);
502
504
517 void import_component(
518 std::unique_ptr<SystemInterface> system, const HardwareComponentParams & params);
519
521
524 const std::unordered_map<std::string, HardwareComponentInfo> & get_components_status();
525
527
530 const std::unordered_map<std::string, joint_limits::JointLimits> & get_hard_joint_limits() const;
531
533
536 const std::unordered_map<std::string, joint_limits::SoftJointLimits> & get_soft_joint_limits()
537 const;
538
540
557 const std::vector<std::string> & start_interfaces,
558 const std::vector<std::string> & stop_interfaces);
559
561
574 const std::vector<std::string> & start_interfaces,
575 const std::vector<std::string> & stop_interfaces);
576
578
590 return_type set_component_state(
591 const std::string & component_name, rclcpp_lifecycle::State & target_state);
592
599 bool enforce_command_limits(const rclcpp::Duration & period);
600
602
608 HardwareReadWriteStatus read(const rclcpp::Time & time, const rclcpp::Duration & period);
609
611
617 HardwareReadWriteStatus write(const rclcpp::Time & time, const rclcpp::Duration & period);
618
620
624 bool command_interface_exists(const std::string & key) const;
625
627
630 bool state_interface_exists(const std::string & key) const;
631
633
636 void set_on_component_state_switch_callback(std::function<void()> callback);
637
638 const std::string & get_robot_description() const;
639
640protected:
642
645 rclcpp::Logger get_logger() const;
646
648
651 rclcpp::Clock::SharedPtr get_clock() const;
652
653 bool components_are_loaded_and_initialized_ = false;
654 bool allow_controller_activation_with_inactive_hardware_ = false;
655 bool return_failed_hardware_names_on_return_deactivate_write_cycle_ = true;
656
657 mutable std::recursive_mutex resource_interfaces_lock_;
658 mutable std::recursive_mutex claimed_command_interfaces_lock_;
659 mutable std::recursive_mutex resources_lock_;
660 mutable std::recursive_mutex joint_limiters_lock_;
661
662private:
663 bool validate_storage(const std::vector<hardware_interface::HardwareInfo> & hardware_info) const;
664
665 void release_command_interface(const std::string & key);
666
667 // Note this was added in #2323 and is a temporary addition to be backwards compatible with the
668 // original constructors. This is planned to be removed in a future PR along with the
669 // aforementioned constructors.
671 rclcpp::Clock::SharedPtr clock, rclcpp::Logger logger, const std::string & urdf = std::string(),
672 bool activate_all = false, unsigned int update_rate = 100);
673
674 std::unordered_map<std::string, bool> claimed_command_interface_map_;
675
676 std::unique_ptr<ResourceStorage> resource_storage_;
678
679 // Structure to store read and write status so it is not initialized in the real-time loop
680 HardwareReadWriteStatus read_write_status;
681};
682
683} // namespace hardware_interface
684#endif // HARDWARE_INTERFACE__RESOURCE_MANAGER_HPP_
Definition loaned_command_interface.hpp:31
Definition loaned_state_interface.hpp:30
Definition resource_manager.hpp:62
void make_controller_reference_interfaces_available(const std::string &controller_name)
Add controller's reference interface to available list.
Definition resource_manager.cpp:1785
std::string get_state_interface_data_type(const std::string &name) const
Gets the data type of the state interface.
Definition resource_manager.cpp:1686
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:1768
std::vector< std::string > command_interface_keys() const
Returns all registered command interfaces keys.
Definition resource_manager.cpp:1923
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:1526
bool command_interface_exists(const std::string &key) const
Checks whether a command interface is registered under the given key.
Definition resource_manager.cpp:2659
bool command_interface_is_claimed(const std::string &key) const
Checks whether a command interface is already claimed.
Definition resource_manager.cpp:1881
return_type set_component_state(const std::string &component_name, rclcpp_lifecycle::State &target_state)
Sets state of hardware component.
Definition resource_manager.cpp:2328
const std::unordered_map< std::string, joint_limits::JointLimits > & get_hard_joint_limits() const
Return the unordered map of hard joint limits.
Definition resource_manager.cpp:2038
bool are_components_initialized() const
if the resource manager load_and_initialize_components(...) function has been called this returns tru...
Definition resource_manager.cpp:1642
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:2673
bool shutdown_components()
Shutdown all hardware components, irrespective of their state.
Definition resource_manager.cpp:1509
void remove_controller_exported_state_interfaces(const std::string &controller_name)
Remove controllers exported state interfaces from resource manager.
Definition resource_manager.cpp:1750
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:1963
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:1718
size_t sensor_components_size() const
Return the number of loaded sensor components.
Definition resource_manager.cpp:2649
std::vector< std::string > state_interface_keys() const
Returns all registered state interfaces keys.
Definition resource_manager.cpp:1661
size_t system_components_size() const
Return the number of loaded system components.
Definition resource_manager.cpp:2654
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:1778
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:1942
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:1702
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:1728
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:1874
std::string get_command_interface_data_type(const std::string &name) const
Gets the data type of the command interface.
Definition resource_manager.cpp:1948
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:1680
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:1711
bool state_interface_exists(const std::string &key) const
Checks whether a state interface is registered under the given key.
Definition resource_manager.cpp:2666
void make_controller_reference_interfaces_unavailable(const std::string &controller_name)
Remove controller's reference interface to available list.
Definition resource_manager.cpp:1795
size_t actuator_components_size() const
Return the number size_t of loaded actuator components.
Definition resource_manager.cpp:2644
rclcpp::Clock::SharedPtr get_clock() const
Gets the clock for the resource manager.
Definition resource_manager.cpp:2687
HardwareReadWriteStatus read(const rclcpp::Time &time, const rclcpp::Duration &period)
Reads all loaded hardware components.
Definition resource_manager.cpp:2434
std::vector< std::string > available_command_interfaces() const
Returns all available command interfaces keys.
Definition resource_manager.cpp:1935
void remove_controller_reference_interfaces(const std::string &controller_name)
Remove controllers reference interfaces from resource manager.
Definition resource_manager.cpp:1817
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:2050
void import_joint_limiters(const std::string &urdf)
Import joint limiters from the URDF.
Definition resource_manager.cpp:1635
bool enforce_command_limits(const rclcpp::Duration &period)
Definition resource_manager.cpp:2413
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:2207
std::vector< std::string > available_state_interfaces() const
Returns all available state interfaces keys.
Definition resource_manager.cpp:1673
rclcpp::Logger get_logger() const
Gets the logger for the resource manager.
Definition resource_manager.cpp:2685
LoanedCommandInterface claim_command_interface(const std::string &key)
Claim a command interface given its key.
Definition resource_manager.cpp:1893
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:1834
const std::unordered_map< std::string, joint_limits::SoftJointLimits > & get_soft_joint_limits() const
Return the unordered map of soft joint limits.
Definition resource_manager.cpp:2044
LoanedStateInterface claim_state_interface(const std::string &key)
Claim a state interface given its key.
Definition resource_manager.cpp:1648
HardwareReadWriteStatus write(const rclcpp::Time &time, const rclcpp::Duration &period)
Write all loaded hardware components.
Definition resource_manager.cpp:2535
const std::unordered_map< std::string, HardwareComponentInfo > & get_components_status()
Return status for all components.
Definition resource_manager.cpp:2019
Definition mujoco_system_interface.hpp:57
Parameters required for the initialization of a specific hardware component plugin....
Definition hardware_component_params.hpp:33
This structure stores information about hardware defined in a robot's URDF.
Definition hardware_info.hpp:375
Definition resource_manager.hpp:56
Parameters required for the construction and initial setup of a ResourceManager. This struct is typic...
Definition resource_manager_params.hpp:30