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 "hardware_interface/types/resource_manager_params.hpp"
33#include "rclcpp/duration.hpp"
34#include "rclcpp/executor.hpp"
35#include "rclcpp/node_interfaces/node_logging_interface.hpp"
36#include "rclcpp/time.hpp"
37
38namespace hardware_interface
39{
40class ResourceStorage;
41class ControllerManager;
42
44{
45 return_type result;
46 std::vector<std::string> failed_hardware_names;
47};
48
50{
51public:
53 explicit ResourceManager(
54 rclcpp::node_interfaces::NodeClockInterface::SharedPtr clock_interface,
55 rclcpp::node_interfaces::NodeLoggingInterface::SharedPtr logger_interface);
56
58 explicit ResourceManager(rclcpp::Clock::SharedPtr clock, rclcpp::Logger logger);
59
61
75 explicit ResourceManager(
76 const std::string & urdf,
77 rclcpp::node_interfaces::NodeClockInterface::SharedPtr clock_interface,
78 rclcpp::node_interfaces::NodeLoggingInterface::SharedPtr logger_interface,
79 bool activate_all = false, const unsigned int update_rate = 100);
80
82
96 explicit ResourceManager(
97 const std::string & urdf, rclcpp::Clock::SharedPtr clock, rclcpp::Logger logger,
98 bool activate_all = false, const unsigned int update_rate = 100);
99
101
111 explicit ResourceManager(const hardware_interface::ResourceManagerParams & params, bool load);
112
113 ResourceManager(const ResourceManager &) = delete;
114
115 virtual ~ResourceManager();
116
118
122 bool shutdown_components();
123
125
136
141 void import_joint_limiters(const std::string & urdf);
142
151 bool are_components_initialized() const;
152
154
162 LoanedStateInterface claim_state_interface(const std::string & key);
163
165
169 std::vector<std::string> state_interface_keys() const;
170
172
176 std::vector<std::string> available_state_interfaces() const;
177
179
182 bool state_interface_is_available(const std::string & name) const;
183
185
190 std::string get_state_interface_data_type(const std::string & name) const;
191
193
203 const std::string & controller_name, std::vector<StateInterface::ConstSharedPtr> & interfaces);
204
206
212 std::vector<std::string> get_controller_exported_state_interface_names(
213 const std::string & controller_name);
214
216
224 void make_controller_exported_state_interfaces_available(const std::string & controller_name);
225
227
234 void make_controller_exported_state_interfaces_unavailable(const std::string & controller_name);
235
237
243 void remove_controller_exported_state_interfaces(const std::string & controller_name);
244
246
256 const std::string & controller_name,
257 const std::vector<hardware_interface::CommandInterface::SharedPtr> & interfaces);
258
260
266 std::vector<std::string> get_controller_reference_interface_names(
267 const std::string & controller_name);
268
270
277 void make_controller_reference_interfaces_available(const std::string & controller_name);
278
280
287 void make_controller_reference_interfaces_unavailable(const std::string & controller_name);
288
290
296 void remove_controller_reference_interfaces(const std::string & controller_name);
297
299
307 const std::string & controller_name, const std::vector<std::string> & interfaces);
308
310
317 std::vector<std::string> get_cached_controllers_to_hardware(const std::string & hardware_name);
318
320
328 bool command_interface_is_claimed(const std::string & key) const;
329
331
339 LoanedCommandInterface claim_command_interface(const std::string & key);
340
342
346 std::vector<std::string> command_interface_keys() const;
347
349
353 std::vector<std::string> available_command_interfaces() const;
354
356
360 bool command_interface_is_available(const std::string & interface) const;
361
363
368 std::string get_command_interface_data_type(const std::string & name) const;
369
371
374 size_t actuator_components_size() const;
375
377
380 size_t sensor_components_size() const;
381
383
386 size_t system_components_size() const;
387
389
401 void import_component(
402 std::unique_ptr<SystemInterface> system, const HardwareComponentParams & params);
403
405
418 void import_component(
419 std::unique_ptr<ActuatorInterface> actuator, const HardwareComponentParams & params);
420
422
435 void import_component(
436 std::unique_ptr<SensorInterface> sensor, const HardwareComponentParams & params);
437
439
442 const std::unordered_map<std::string, HardwareComponentInfo> & get_components_status();
443
445
448 const std::unordered_map<std::string, joint_limits::JointLimits> & get_hard_joint_limits() const;
449
451
454 const std::unordered_map<std::string, joint_limits::SoftJointLimits> & get_soft_joint_limits()
455 const;
456
458
475 const std::vector<std::string> & start_interfaces,
476 const std::vector<std::string> & stop_interfaces);
477
479
492 const std::vector<std::string> & start_interfaces,
493 const std::vector<std::string> & stop_interfaces);
494
496
508 return_type set_component_state(
509 const std::string & component_name, rclcpp_lifecycle::State & target_state);
510
517 bool enforce_command_limits(const rclcpp::Duration & period);
518
520
526 HardwareReadWriteStatus read(const rclcpp::Time & time, const rclcpp::Duration & period);
527
529
535 HardwareReadWriteStatus write(const rclcpp::Time & time, const rclcpp::Duration & period);
536
538
542 bool command_interface_exists(const std::string & key) const;
543
545
548 bool state_interface_exists(const std::string & key) const;
549
551
554 void set_on_component_state_switch_callback(std::function<void()> callback);
555
556 const std::string & get_robot_description() const;
557
558protected:
560
563 rclcpp::Logger get_logger() const;
564
566
569 rclcpp::Clock::SharedPtr get_clock() const;
570
571 bool components_are_loaded_and_initialized_ = false;
572 bool allow_controller_activation_with_inactive_hardware_ = false;
573 bool return_failed_hardware_names_on_return_deactivate_write_cycle_ = true;
574
575 mutable std::recursive_mutex resource_interfaces_lock_;
576 mutable std::recursive_mutex claimed_command_interfaces_lock_;
577 mutable std::recursive_mutex resources_lock_;
578 mutable std::recursive_mutex joint_limiters_lock_;
579
580private:
581 bool validate_storage(const std::vector<hardware_interface::HardwareInfo> & hardware_info) const;
582
583 void release_command_interface(const std::string & key);
584
585 // Note this was added in #2323 and is a temporary addition to be backwards compatible with the
586 // original constructors. This is planned to be removed in a future PR along with the
587 // aforementioned constructors.
589 rclcpp::Clock::SharedPtr clock, rclcpp::Logger logger, const std::string & urdf = std::string(),
590 bool activate_all = false, unsigned int update_rate = 100);
591
592 std::unordered_map<std::string, bool> claimed_command_interface_map_;
593
594 std::unique_ptr<ResourceStorage> resource_storage_;
596
597 // Structure to store read and write status so it is not initialized in the real-time loop
598 HardwareReadWriteStatus read_write_status;
599};
600
601} // namespace hardware_interface
602#endif // HARDWARE_INTERFACE__RESOURCE_MANAGER_HPP_
Definition loaned_command_interface.hpp:30
Definition loaned_state_interface.hpp:29
Definition resource_manager.hpp:50
void make_controller_reference_interfaces_available(const std::string &controller_name)
Add controller's reference interface to available list.
Definition resource_manager.cpp:1753
std::string get_state_interface_data_type(const std::string &name) const
Gets the data type of the state interface.
Definition resource_manager.cpp:1654
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:1736
std::vector< std::string > command_interface_keys() const
Returns all registered command interfaces keys.
Definition resource_manager.cpp:1891
bool command_interface_exists(const std::string &key) const
Checks whether a command interface is registered under the given key.
Definition resource_manager.cpp:2603
bool command_interface_is_claimed(const std::string &key) const
Checks whether a command interface is already claimed.
Definition resource_manager.cpp:1849
return_type set_component_state(const std::string &component_name, rclcpp_lifecycle::State &target_state)
Sets state of hardware component.
Definition resource_manager.cpp:2272
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:1982
bool are_components_initialized() const
if the resource manager load_and_initialize_components(...) function has been called this returns tru...
Definition resource_manager.cpp:1610
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:2617
bool shutdown_components()
Shutdown all hardware components, irrespective of their state.
Definition resource_manager.cpp:1491
void remove_controller_exported_state_interfaces(const std::string &controller_name)
Remove controllers exported state interfaces from resource manager.
Definition resource_manager.cpp:1718
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:1686
size_t sensor_components_size() const
Return the number of loaded sensor components.
Definition resource_manager.cpp:2593
std::vector< std::string > state_interface_keys() const
Returns all registered state interfaces keys.
Definition resource_manager.cpp:1629
size_t system_components_size() const
Return the number of loaded system components.
Definition resource_manager.cpp:2598
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:1746
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:1910
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:1670
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:1696
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:1842
std::string get_command_interface_data_type(const std::string &name) const
Gets the data type of the command interface.
Definition resource_manager.cpp:1916
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:1648
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:1679
void import_component(std::unique_ptr< SystemInterface > system, const HardwareComponentParams &params)
Import a hardware component which is not listed in the URDF.
Definition resource_manager.cpp:1951
bool state_interface_exists(const std::string &key) const
Checks whether a state interface is registered under the given key.
Definition resource_manager.cpp:2610
void make_controller_reference_interfaces_unavailable(const std::string &controller_name)
Remove controller's reference interface to available list.
Definition resource_manager.cpp:1763
size_t actuator_components_size() const
Return the number size_t of loaded actuator components.
Definition resource_manager.cpp:2588
rclcpp::Clock::SharedPtr get_clock() const
Gets the clock for the resource manager.
Definition resource_manager.cpp:2631
HardwareReadWriteStatus read(const rclcpp::Time &time, const rclcpp::Duration &period)
Reads all loaded hardware components.
Definition resource_manager.cpp:2378
std::vector< std::string > available_command_interfaces() const
Returns all available command interfaces keys.
Definition resource_manager.cpp:1903
void remove_controller_reference_interfaces(const std::string &controller_name)
Remove controllers reference interfaces from resource manager.
Definition resource_manager.cpp:1785
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:1994
void import_joint_limiters(const std::string &urdf)
Import joint limiters from the URDF.
Definition resource_manager.cpp:1603
virtual bool load_and_initialize_components(const hardware_interface::ResourceManagerParams &params)
Load resources from on a given URDF.
Definition resource_manager.cpp:1508
bool enforce_command_limits(const rclcpp::Duration &period)
Definition resource_manager.cpp:2357
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:2151
std::vector< std::string > available_state_interfaces() const
Returns all available state interfaces keys.
Definition resource_manager.cpp:1641
rclcpp::Logger get_logger() const
Gets the logger for the resource manager.
Definition resource_manager.cpp:2629
LoanedCommandInterface claim_command_interface(const std::string &key)
Claim a command interface given its key.
Definition resource_manager.cpp:1861
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:1802
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:1988
LoanedStateInterface claim_state_interface(const std::string &key)
Claim a state interface given its key.
Definition resource_manager.cpp:1616
HardwareReadWriteStatus write(const rclcpp::Time &time, const rclcpp::Duration &period)
Write all loaded hardware components.
Definition resource_manager.cpp:2479
const std::unordered_map< std::string, HardwareComponentInfo > & get_components_status()
Return status for all components.
Definition resource_manager.cpp:1963
Definition mujoco_system_interface.hpp:69
Parameters required for the initialization of a specific hardware component plugin....
Definition hardware_component_params.hpp:33
Definition resource_manager.hpp:44
Parameters required for the construction and initial setup of a ResourceManager. This struct is typic...
Definition resource_manager_params.hpp:30