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 "hardware_interface/types/resource_manager_params.hpp"
36#include "rclcpp/duration.hpp"
37#include "rclcpp/executor.hpp"
38#include "rclcpp/node_interfaces/node_logging_interface.hpp"
39#include "rclcpp/time.hpp"
40
41namespace hardware_interface
42{
43class ResourceStorage;
44class ControllerManager;
45
47{
48 return_type result;
49 std::vector<std::string> failed_hardware_names;
50};
51
53{
54public:
56 explicit ResourceManager(
57 rclcpp::node_interfaces::NodeClockInterface::SharedPtr clock_interface,
58 rclcpp::node_interfaces::NodeLoggingInterface::SharedPtr logger_interface);
59
61 explicit ResourceManager(rclcpp::Clock::SharedPtr clock, rclcpp::Logger logger);
62
64
78 explicit ResourceManager(
79 const std::string & urdf,
80 rclcpp::node_interfaces::NodeClockInterface::SharedPtr clock_interface,
81 rclcpp::node_interfaces::NodeLoggingInterface::SharedPtr logger_interface,
82 bool activate_all = false, const unsigned int update_rate = 100);
83
85
99 explicit ResourceManager(
100 const std::string & urdf, rclcpp::Clock::SharedPtr clock, rclcpp::Logger logger,
101 bool activate_all = false, const unsigned int update_rate = 100);
102
104
114 explicit ResourceManager(const hardware_interface::ResourceManagerParams & params, bool load);
115
116 ResourceManager(const ResourceManager &) = delete;
117
118 virtual ~ResourceManager();
119
121
125 bool shutdown_components();
126
128
139
144 void import_joint_limiters(const std::string & urdf);
145
154 bool are_components_initialized() const;
155
157
165 LoanedStateInterface claim_state_interface(const std::string & key);
166
168
172 std::vector<std::string> state_interface_keys() const;
173
175
179 std::vector<std::string> available_state_interfaces() const;
180
182
185 bool state_interface_is_available(const std::string & name) const;
186
188
193 std::string get_state_interface_data_type(const std::string & name) const;
194
196
206 const std::string & controller_name, std::vector<StateInterface::ConstSharedPtr> & interfaces);
207
209
215 std::vector<std::string> get_controller_exported_state_interface_names(
216 const std::string & controller_name);
217
219
227 void make_controller_exported_state_interfaces_available(const std::string & controller_name);
228
230
237 void make_controller_exported_state_interfaces_unavailable(const std::string & controller_name);
238
240
246 void remove_controller_exported_state_interfaces(const std::string & controller_name);
247
249
259 const std::string & controller_name,
260 const std::vector<hardware_interface::CommandInterface::SharedPtr> & interfaces);
261
263
269 std::vector<std::string> get_controller_reference_interface_names(
270 const std::string & controller_name);
271
273
280 void make_controller_reference_interfaces_available(const std::string & controller_name);
281
283
290 void make_controller_reference_interfaces_unavailable(const std::string & controller_name);
291
293
299 void remove_controller_reference_interfaces(const std::string & controller_name);
300
302
310 const std::string & controller_name, const std::vector<std::string> & interfaces);
311
313
320 std::vector<std::string> get_cached_controllers_to_hardware(const std::string & hardware_name);
321
323
331 bool command_interface_is_claimed(const std::string & key) const;
332
334
342 LoanedCommandInterface claim_command_interface(const std::string & key);
343
345
349 std::vector<std::string> command_interface_keys() const;
350
352
356 std::vector<std::string> available_command_interfaces() const;
357
359
363 bool command_interface_is_available(const std::string & interface) const;
364
366
371 std::string get_command_interface_data_type(const std::string & name) const;
372
374
377 size_t actuator_components_size() const;
378
380
383 size_t sensor_components_size() const;
384
386
389 size_t system_components_size() const;
390
392
404 void import_component(
405 std::unique_ptr<SystemInterface> system, const HardwareComponentParams & params);
406
408
421 void import_component(
422 std::unique_ptr<ActuatorInterface> actuator, const HardwareComponentParams & params);
423
425
438 void import_component(
439 std::unique_ptr<SensorInterface> sensor, const HardwareComponentParams & params);
440
442
445 const std::unordered_map<std::string, HardwareComponentInfo> & get_components_status();
446
448
451 const std::unordered_map<std::string, joint_limits::JointLimits> & get_hard_joint_limits() const;
452
454
457 const std::unordered_map<std::string, joint_limits::SoftJointLimits> & get_soft_joint_limits()
458 const;
459
461
478 const std::vector<std::string> & start_interfaces,
479 const std::vector<std::string> & stop_interfaces);
480
482
495 const std::vector<std::string> & start_interfaces,
496 const std::vector<std::string> & stop_interfaces);
497
499
511 return_type set_component_state(
512 const std::string & component_name, rclcpp_lifecycle::State & target_state);
513
520 bool enforce_command_limits(const rclcpp::Duration & period);
521
523
529 HardwareReadWriteStatus read(const rclcpp::Time & time, const rclcpp::Duration & period);
530
532
538 HardwareReadWriteStatus write(const rclcpp::Time & time, const rclcpp::Duration & period);
539
541
545 bool command_interface_exists(const std::string & key) const;
546
548
551 bool state_interface_exists(const std::string & key) const;
552
554
557 void set_on_component_state_switch_callback(std::function<void()> callback);
558
559 const std::string & get_robot_description() const;
560
561protected:
563
566 rclcpp::Logger get_logger() const;
567
569
572 rclcpp::Clock::SharedPtr get_clock() const;
573
574 bool components_are_loaded_and_initialized_ = false;
575 bool allow_controller_activation_with_inactive_hardware_ = false;
576 bool return_failed_hardware_names_on_return_deactivate_write_cycle_ = true;
577
578 mutable std::recursive_mutex resource_interfaces_lock_;
579 mutable std::recursive_mutex claimed_command_interfaces_lock_;
580 mutable std::recursive_mutex resources_lock_;
581 mutable std::recursive_mutex joint_limiters_lock_;
582
583private:
584 bool validate_storage(const std::vector<hardware_interface::HardwareInfo> & hardware_info) const;
585
586 void release_command_interface(const std::string & key);
587
588 // Note this was added in #2323 and is a temporary addition to be backwards compatible with the
589 // original constructors. This is planned to be removed in a future PR along with the
590 // aforementioned constructors.
592 rclcpp::Clock::SharedPtr clock, rclcpp::Logger logger, const std::string & urdf = std::string(),
593 bool activate_all = false, unsigned int update_rate = 100);
594
595 std::unordered_map<std::string, bool> claimed_command_interface_map_;
596
597 std::unique_ptr<ResourceStorage> resource_storage_;
598
599 // Structure to store read and write status so it is not initialized in the real-time loop
600 HardwareReadWriteStatus read_write_status;
601};
602
603} // namespace hardware_interface
604#endif // HARDWARE_INTERFACE__RESOURCE_MANAGER_HPP_
Definition loaned_command_interface.hpp:30
Definition loaned_state_interface.hpp:29
Definition resource_manager.hpp:53
void make_controller_reference_interfaces_available(const std::string &controller_name)
Add controller's reference interface to available list.
Definition resource_manager.cpp:1702
std::string get_state_interface_data_type(const std::string &name) const
Gets the data type of the state interface.
Definition resource_manager.cpp:1601
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:1685
std::vector< std::string > command_interface_keys() const
Returns all registered command interfaces keys.
Definition resource_manager.cpp:1842
bool command_interface_exists(const std::string &key) const
Checks whether a command interface is registered under the given key.
Definition resource_manager.cpp:2551
bool command_interface_is_claimed(const std::string &key) const
Checks whether a command interface is already claimed.
Definition resource_manager.cpp:1800
return_type set_component_state(const std::string &component_name, rclcpp_lifecycle::State &target_state)
Sets state of hardware component.
Definition resource_manager.cpp:2224
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:1936
bool are_components_initialized() const
if the resource manager load_and_initialize_components(...) function has been called this returns tru...
Definition resource_manager.cpp:1554
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:2565
bool shutdown_components()
Shutdown all hardware components, irrespective of their state.
Definition resource_manager.cpp:1438
void remove_controller_exported_state_interfaces(const std::string &controller_name)
Remove controllers exported state interfaces from resource manager.
Definition resource_manager.cpp:1667
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:1633
size_t sensor_components_size() const
Return the number of loaded sensor components.
Definition resource_manager.cpp:2541
std::vector< std::string > state_interface_keys() const
Returns all registered state interfaces keys.
Definition resource_manager.cpp:1573
size_t system_components_size() const
Return the number of loaded system components.
Definition resource_manager.cpp:2546
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:1695
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:1861
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:1617
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:1645
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:1793
std::string get_command_interface_data_type(const std::string &name) const
Gets the data type of the command interface.
Definition resource_manager.cpp:1870
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:1592
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:1626
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:1905
bool state_interface_exists(const std::string &key) const
Checks whether a state interface is registered under the given key.
Definition resource_manager.cpp:2558
void make_controller_reference_interfaces_unavailable(const std::string &controller_name)
Remove controller's reference interface to available list.
Definition resource_manager.cpp:1714
size_t actuator_components_size() const
Return the number size_t of loaded actuator components.
Definition resource_manager.cpp:2536
rclcpp::Clock::SharedPtr get_clock() const
Gets the clock for the resource manager.
Definition resource_manager.cpp:2579
HardwareReadWriteStatus read(const rclcpp::Time &time, const rclcpp::Duration &period)
Reads all loaded hardware components.
Definition resource_manager.cpp:2330
std::vector< std::string > available_command_interfaces() const
Returns all available command interfaces keys.
Definition resource_manager.cpp:1854
void remove_controller_reference_interfaces(const std::string &controller_name)
Remove controllers reference interfaces from resource manager.
Definition resource_manager.cpp:1736
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:1948
void import_joint_limiters(const std::string &urdf)
Import joint limiters from the URDF.
Definition resource_manager.cpp:1547
virtual bool load_and_initialize_components(const hardware_interface::ResourceManagerParams &params)
Load resources from on a given URDF.
Definition resource_manager.cpp:1455
bool enforce_command_limits(const rclcpp::Duration &period)
Definition resource_manager.cpp:2309
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:2104
std::vector< std::string > available_state_interfaces() const
Returns all available state interfaces keys.
Definition resource_manager.cpp:1585
rclcpp::Logger get_logger() const
Gets the logger for the resource manager.
Definition resource_manager.cpp:2577
LoanedCommandInterface claim_command_interface(const std::string &key)
Claim a command interface given its key.
Definition resource_manager.cpp:1812
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:1753
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:1942
LoanedStateInterface claim_state_interface(const std::string &key)
Claim a state interface given its key.
Definition resource_manager.cpp:1560
HardwareReadWriteStatus write(const rclcpp::Time &time, const rclcpp::Duration &period)
Write all loaded hardware components.
Definition resource_manager.cpp:2429
const std::unordered_map< std::string, HardwareComponentInfo > & get_components_status()
Return status for all components.
Definition resource_manager.cpp:1917
Definition actuator.hpp:22
Parameters required for the initialization of a specific hardware component plugin....
Definition hardware_component_params.hpp:33
Definition resource_manager.hpp:47
Parameters required for the construction and initial setup of a ResourceManager. This struct is typic...
Definition resource_manager_params.hpp:30