Skip to content

Commit a29fa97

Browse files
authored
Fix the initialization of the parsed resource managers (#3346)
1 parent 2c49deb commit a29fa97

4 files changed

Lines changed: 48 additions & 7 deletions

File tree

controller_manager/include/controller_manager/controller_manager.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ class ControllerManager : public rclcpp::Node
342342
unsigned int update_rate_;
343343
std::vector<std::vector<std::string>> chained_controllers_configuration_;
344344

345-
std::unique_ptr<hardware_interface::ResourceManager> resource_manager_;
345+
std::unique_ptr<hardware_interface::ResourceManager> resource_manager_ = nullptr;
346346

347347
private:
348348
std::vector<std::string> get_controller_names();

controller_manager/src/controller_manager.cpp

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -574,9 +574,14 @@ ControllerManager::ControllerManager(
574574
kControllerInterfaceNamespace, kControllerInterfaceClassName)),
575575
chainable_loader_(
576576
std::make_shared<pluginlib::ClassLoader<controller_interface::ChainableControllerInterface>>(
577-
kControllerInterfaceNamespace, kChainableControllerInterfaceClassName)),
578-
robot_description_(resource_manager_->get_robot_description())
577+
kControllerInterfaceNamespace, kChainableControllerInterfaceClassName))
579578
{
579+
if (resource_manager_ == nullptr)
580+
{
581+
throw std::runtime_error("The parsed resource manager is a nullptr!");
582+
}
583+
584+
robot_description_ = resource_manager_->get_robot_description();
580585
initialize_parameters();
581586
if (is_resource_manager_initialized())
582587
{
@@ -586,8 +591,20 @@ ControllerManager::ControllerManager(
586591
}
587592
else
588593
{
589-
RCLCPP_FATAL(get_logger(), "The resource manager is not properly initialized");
590-
throw std::runtime_error("Resource manager object is not valid. See the FATAL message above.");
594+
if (!robot_description_.empty())
595+
{
596+
RCLCPP_FATAL(get_logger(), "The resource manager is not properly initialized");
597+
throw std::runtime_error(
598+
"Resource manager object is not valid. See the FATAL message above.");
599+
}
600+
else
601+
{
602+
RCLCPP_WARN(
603+
get_logger(),
604+
"The resource manager is not yet initialized, will wait for the robot description to "
605+
"initialize it..");
606+
init_controller_manager();
607+
}
591608
}
592609
}
593610

@@ -803,7 +820,10 @@ void ControllerManager::init_resource_manager(const std::string & robot_descript
803820
params.return_failed_hardware_names_on_return_deactivate_write_cycle_ =
804821
params_->defaults.deactivate_controllers_on_hardware_self_deactivate;
805822
params.handle_exceptions = params_->handle_exceptions;
806-
resource_manager_ = std::make_unique<hardware_interface::ResourceManager>(params, false);
823+
if (resource_manager_ == nullptr)
824+
{
825+
resource_manager_ = std::make_unique<hardware_interface::ResourceManager>(params, false);
826+
}
807827

808828
resource_manager_->set_on_component_state_switch_callback(
809829
std::bind(&ControllerManager::publish_activity, this));

controller_manager/test/test_controller_manager_with_resource_manager.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,31 @@ TEST_F(ControllerManagerTest, robot_description_callback_handles_no_geometry)
116116
EXPECT_TRUE(cm.is_resource_manager_initialized());
117117
}
118118

119-
TEST_F(ControllerManagerTest, cm_constructor_with_invalid_rm_object)
119+
TEST_F(ControllerManagerTest, cm_constructor_with_nullptr_rm_object)
120120
{
121+
test_resource_manager_ = nullptr;
121122
EXPECT_THROW(
122123
{ TestControllerManager cm(std::move(test_resource_manager_), executor_); },
123124
std::runtime_error);
124125
}
125126

127+
TEST_F(ControllerManagerTest, cm_constructor_with_uninitialized_rm_object_with_valid_urdf)
128+
{
129+
hardware_interface::ResourceManagerParams rm_param;
130+
rm_param.logger = node_->get_logger();
131+
rm_param.clock = node_->get_clock();
132+
rm_param.robot_description = ros2_control_test_assets::minimal_robot_urdf;
133+
test_resource_manager_ = std::make_unique<hardware_interface::ResourceManager>(rm_param, false);
134+
EXPECT_THROW(
135+
{ TestControllerManager cm(std::move(test_resource_manager_), executor_); },
136+
std::runtime_error);
137+
}
138+
139+
TEST_F(ControllerManagerTest, cm_constructor_with_uninitialized_rm_object_with_no_urdf)
140+
{
141+
EXPECT_NO_THROW({ TestControllerManager cm(std::move(test_resource_manager_), executor_); });
142+
}
143+
126144
int main(int argc, char ** argv)
127145
{
128146
::testing::InitGoogleTest(&argc, argv);

hardware_interface/src/resource_manager.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,9 @@ class ResourceStorage
170170
: ResourceStorage(rm_param.clock, rm_param.logger)
171171
{
172172
handle_exception_ = rm_param.handle_exceptions;
173+
robot_description_ = rm_param.robot_description;
174+
cm_update_rate_ = rm_param.update_rate;
175+
handle_exception_ = rm_param.handle_exceptions;
173176
}
174177

175178
template <class HardwareT, class HardwareInterfaceT>

0 commit comments

Comments
 (0)