Skip to content

Commit 45e6b9c

Browse files
authored
Further Struct based Method changes to support propagation in gz_ros2_control (backport #2340) (#2407)
1 parent 6f67786 commit 45e6b9c

File tree

3 files changed

+106
-24
lines changed

3 files changed

+106
-24
lines changed

controller_manager/src/controller_manager.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -621,7 +621,13 @@ void ControllerManager::init_resource_manager(const std::string & robot_descript
621621
{
622622
resource_manager_->import_joint_limiters(robot_description_);
623623
}
624-
if (!resource_manager_->load_and_initialize_components(robot_description, update_rate_))
624+
hardware_interface::ResourceManagerParams params;
625+
params.robot_description = robot_description;
626+
params.clock = trigger_clock_;
627+
params.logger = this->get_logger();
628+
params.executor = executor_;
629+
params.update_rate = static_cast<unsigned int>(params_->update_rate);
630+
if (!resource_manager_->load_and_initialize_components(params))
625631
{
626632
RCLCPP_WARN(
627633
get_logger(),

hardware_interface/include/hardware_interface/resource_manager.hpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,57 @@ class ResourceManager
446446
* \param[in] system pointer to the system interface.
447447
* \param[in] hardware_info hardware info
448448
*/
449+
void import_component(
450+
std::unique_ptr<SystemInterface> system, const HardwareComponentParams & params);
451+
452+
/// Import a hardware component which is not listed in the URDF
453+
/**
454+
* Components which are initialized outside a URDF can be added post initialization.
455+
* Nevertheless, there should still be `HardwareInfo` available for this component,
456+
* either parsed from a URDF string (easiest) or filled manually.
457+
*
458+
* \note this might invalidate existing state and command interfaces and should thus
459+
* not be called when a controller is running.
460+
* \note given that no hardware_info is available, the component has to be configured
461+
* externally and prior to the call to import.
462+
* \param[in] actuator pointer to the actuator interface.
463+
* \param[in] params Struct of type HardwareComponentParams containing the hardware info
464+
* and other parameters for the component.
465+
*/
466+
void import_component(
467+
std::unique_ptr<ActuatorInterface> actuator, const HardwareComponentParams & params);
468+
469+
/// Import a hardware component which is not listed in the URDF
470+
/**
471+
* Components which are initialized outside a URDF can be added post initialization.
472+
* Nevertheless, there should still be `HardwareInfo` available for this component,
473+
* either parsed from a URDF string (easiest) or filled manually.
474+
*
475+
* \note this might invalidate existing state and command interfaces and should thus
476+
* not be called when a controller is running.
477+
* \note given that no hardware_info is available, the component has to be configured
478+
* externally and prior to the call to import.
479+
* \param[in] sensor pointer to the sensor interface.
480+
* \param[in] params Struct of type HardwareComponentParams containing the hardware info
481+
* and other parameters for the component.
482+
*/
483+
void import_component(
484+
std::unique_ptr<SensorInterface> sensor, const HardwareComponentParams & params);
485+
486+
/// Import a hardware component which is not listed in the URDF
487+
/**
488+
* Components which are initialized outside a URDF can be added post initialization.
489+
* Nevertheless, there should still be `HardwareInfo` available for this component,
490+
* either parsed from a URDF string (easiest) or filled manually.
491+
*
492+
* \note this might invalidate existing state and command interfaces and should thus
493+
* not be called when a controller is running.
494+
* \note given that no hardware_info is available, the component has to be configured
495+
* externally and prior to the call to import.
496+
* \param[in] system pointer to the system interface.
497+
* \param[in] params Struct of type HardwareComponentParams containing the hardware info
498+
* and other parameters for the component.
499+
*/
449500
void import_component(
450501
std::unique_ptr<SystemInterface> system, const HardwareInfo & hardware_info);
451502

hardware_interface/src/resource_manager.cpp

Lines changed: 48 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1347,6 +1347,7 @@ class ResourceStorage
13471347
// Logger and Clock interfaces
13481348
rclcpp::Clock::SharedPtr rm_clock_;
13491349
rclcpp::Logger rm_logger_;
1350+
rclcpp::Executor::WeakPtr executor_;
13501351

13511352
std::vector<Actuator> actuators_;
13521353
std::vector<Sensor> sensors_;
@@ -1469,28 +1470,17 @@ bool ResourceManager::shutdown_components()
14691470
// CM API: Called in "callback/slow"-thread
14701471
bool ResourceManager::load_and_initialize_components(
14711472
const std::string & urdf, const unsigned int update_rate)
1472-
{
1473-
hardware_interface::ResourceManagerParams params;
1474-
params.robot_description = urdf;
1475-
params.update_rate = update_rate;
1476-
return load_and_initialize_components(params);
1477-
}
1478-
1479-
bool ResourceManager::load_and_initialize_components(
1480-
const hardware_interface::ResourceManagerParams & params)
14811473
{
14821474
components_are_loaded_and_initialized_ = true;
14831475

1484-
resource_storage_->robot_description_ = params.robot_description;
1485-
resource_storage_->cm_update_rate_ = params.update_rate;
1476+
resource_storage_->robot_description_ = urdf;
1477+
resource_storage_->cm_update_rate_ = update_rate;
14861478

1487-
auto hardware_info =
1488-
hardware_interface::parse_control_resources_from_urdf(params.robot_description);
1479+
auto hardware_info = hardware_interface::parse_control_resources_from_urdf(urdf);
14891480
// Set the update rate for all hardware components
14901481
for (auto & hw : hardware_info)
14911482
{
1492-
hw.rw_rate =
1493-
(hw.rw_rate == 0 || hw.rw_rate > params.update_rate) ? params.update_rate : hw.rw_rate;
1483+
hw.rw_rate = (hw.rw_rate == 0 || hw.rw_rate > update_rate) ? update_rate : hw.rw_rate;
14941484
}
14951485

14961486
const std::string system_type = "system";
@@ -1515,9 +1505,9 @@ bool ResourceManager::load_and_initialize_components(
15151505
}
15161506
hardware_interface::HardwareComponentParams interface_params;
15171507
interface_params.hardware_info = individual_hardware_info;
1518-
interface_params.executor = params.executor;
1519-
interface_params.clock = params.clock;
1520-
interface_params.logger = params.logger;
1508+
interface_params.executor = resource_storage_->executor_;
1509+
interface_params.clock = resource_storage_->rm_clock_;
1510+
interface_params.logger = resource_storage_->rm_logger_;
15211511

15221512
if (individual_hardware_info.type == actuator_type)
15231513
{
@@ -1566,6 +1556,17 @@ bool ResourceManager::load_and_initialize_components(
15661556
return components_are_loaded_and_initialized_;
15671557
}
15681558

1559+
bool ResourceManager::load_and_initialize_components(
1560+
const hardware_interface::ResourceManagerParams & params)
1561+
{
1562+
resource_storage_->rm_clock_ = params.clock;
1563+
resource_storage_->rm_logger_ = params.logger;
1564+
resource_storage_->robot_description_ = params.robot_description;
1565+
resource_storage_->cm_update_rate_ = params.update_rate;
1566+
resource_storage_->executor_ = params.executor;
1567+
return load_and_initialize_components(params.robot_description, params.update_rate);
1568+
}
1569+
15691570
void ResourceManager::import_joint_limiters(const std::string & urdf)
15701571
{
15711572
std::lock_guard<std::recursive_mutex> guard(joint_limiters_lock_);
@@ -1894,29 +1895,53 @@ std::string ResourceManager::get_command_interface_data_type(const std::string &
18941895

18951896
void ResourceManager::import_component(
18961897
std::unique_ptr<ActuatorInterface> actuator, const HardwareInfo & hardware_info)
1898+
{
1899+
HardwareComponentParams params;
1900+
params.hardware_info = hardware_info;
1901+
import_component(std::move(actuator), params);
1902+
}
1903+
1904+
void ResourceManager::import_component(
1905+
std::unique_ptr<SensorInterface> sensor, const HardwareInfo & hardware_info)
1906+
{
1907+
HardwareComponentParams params;
1908+
params.hardware_info = hardware_info;
1909+
import_component(std::move(sensor), params);
1910+
}
1911+
1912+
void ResourceManager::import_component(
1913+
std::unique_ptr<SystemInterface> system, const HardwareInfo & hardware_info)
1914+
{
1915+
HardwareComponentParams params;
1916+
params.hardware_info = hardware_info;
1917+
import_component(std::move(system), params);
1918+
}
1919+
1920+
void ResourceManager::import_component(
1921+
std::unique_ptr<ActuatorInterface> actuator, const HardwareComponentParams & params)
18971922
{
18981923
std::lock_guard<std::recursive_mutex> guard(resources_lock_);
1899-
resource_storage_->initialize_actuator(std::move(actuator), hardware_info);
1924+
resource_storage_->initialize_actuator(std::move(actuator), params);
19001925
read_write_status.failed_hardware_names.reserve(
19011926
resource_storage_->actuators_.size() + resource_storage_->sensors_.size() +
19021927
resource_storage_->systems_.size());
19031928
}
19041929

19051930
void ResourceManager::import_component(
1906-
std::unique_ptr<SensorInterface> sensor, const HardwareInfo & hardware_info)
1931+
std::unique_ptr<SensorInterface> sensor, const HardwareComponentParams & params)
19071932
{
19081933
std::lock_guard<std::recursive_mutex> guard(resources_lock_);
1909-
resource_storage_->initialize_sensor(std::move(sensor), hardware_info);
1934+
resource_storage_->initialize_sensor(std::move(sensor), params);
19101935
read_write_status.failed_hardware_names.reserve(
19111936
resource_storage_->actuators_.size() + resource_storage_->sensors_.size() +
19121937
resource_storage_->systems_.size());
19131938
}
19141939

19151940
void ResourceManager::import_component(
1916-
std::unique_ptr<SystemInterface> system, const HardwareInfo & hardware_info)
1941+
std::unique_ptr<SystemInterface> system, const HardwareComponentParams & params)
19171942
{
19181943
std::lock_guard<std::recursive_mutex> guard(resources_lock_);
1919-
resource_storage_->initialize_system(std::move(system), hardware_info);
1944+
resource_storage_->initialize_system(std::move(system), params);
19201945
read_write_status.failed_hardware_names.reserve(
19211946
resource_storage_->actuators_.size() + resource_storage_->sensors_.size() +
19221947
resource_storage_->systems_.size());

0 commit comments

Comments
 (0)