Skip to content

Commit 9e9ab6a

Browse files
saikishormergify[bot]
authored andcommitted
Add handle_exceptions parameter to controller manager (#2807)
(cherry picked from commit b5fd514) # Conflicts: # doc/release_notes.rst # hardware_interface/src/resource_manager.cpp
1 parent f220551 commit 9e9ab6a

14 files changed

+399
-19
lines changed

controller_manager/src/controller_manager.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -977,6 +977,7 @@ controller_interface::ControllerInterfaceBaseSharedPtr ControllerManager::load_c
977977
get_logger(),
978978
"Caught exception of type : %s while loading the controller '%s' of plugin type '%s':\n%s",
979979
typeid(e).name(), controller_name.c_str(), controller_type.c_str(), e.what());
980+
params_->handle_exceptions ? void() : throw;
980981
return nullptr;
981982
}
982983
catch (...)
@@ -1187,6 +1188,7 @@ controller_interface::return_type ControllerManager::cleanup_controller(
11871188
RCLCPP_ERROR(
11881189
get_logger(), "Caught exception while cleaning-up the controller '%s'",
11891190
controller.info.name.c_str());
1191+
params_->handle_exceptions ? void() : throw;
11901192
return controller_interface::return_type::ERROR;
11911193
}
11921194
return controller_interface::return_type::OK;
@@ -1211,12 +1213,14 @@ void ControllerManager::shutdown_controller(
12111213
get_logger(),
12121214
"Caught exception of type : %s while shutdown the controller '%s' before unloading: %s",
12131215
typeid(e).name(), controller.info.name.c_str(), e.what());
1216+
params_->handle_exceptions ? void() : throw;
12141217
}
12151218
catch (...)
12161219
{
12171220
RCLCPP_ERROR(
12181221
get_logger(), "Failed to shutdown the controller '%s' before unloading",
12191222
controller.info.name.c_str());
1223+
params_->handle_exceptions ? void() : throw;
12201224
}
12211225
}
12221226

@@ -1287,13 +1291,15 @@ controller_interface::return_type ControllerManager::configure_controller(
12871291
RCLCPP_ERROR(
12881292
get_logger(), "Caught exception of type : %s while configuring controller '%s': %s",
12891293
typeid(e).name(), controller_name.c_str(), e.what());
1294+
params_->handle_exceptions ? void() : throw;
12901295
return controller_interface::return_type::ERROR;
12911296
}
12921297
catch (...)
12931298
{
12941299
RCLCPP_ERROR(
12951300
get_logger(), "Caught unknown exception while configuring controller '%s'",
12961301
controller_name.c_str());
1302+
params_->handle_exceptions ? void() : throw;
12971303
return controller_interface::return_type::ERROR;
12981304
}
12991305

@@ -1348,6 +1354,7 @@ controller_interface::return_type ControllerManager::configure_controller(
13481354
RCLCPP_FATAL(
13491355
get_logger(), "Export of the state or reference interfaces failed with following error: %s",
13501356
e.what());
1357+
params_->handle_exceptions ? void() : throw;
13511358
return controller_interface::return_type::ERROR;
13521359
}
13531360
resource_manager_->import_controller_reference_interfaces(controller_name, ref_interfaces);
@@ -2133,6 +2140,7 @@ controller_interface::ControllerInterfaceBaseSharedPtr ControllerManager::add_co
21332140
RCLCPP_ERROR(
21342141
get_logger(), "Caught exception of type : %s while initializing controller '%s': %s",
21352142
typeid(e).name(), controller.info.name.c_str(), e.what());
2143+
params_->handle_exceptions ? void() : throw;
21362144
return nullptr;
21372145
}
21382146
catch (...)
@@ -2141,6 +2149,7 @@ controller_interface::ControllerInterfaceBaseSharedPtr ControllerManager::add_co
21412149
RCLCPP_ERROR(
21422150
get_logger(), "Caught unknown exception while initializing controller '%s'",
21432151
controller.info.name.c_str());
2152+
params_->handle_exceptions ? void() : throw;
21442153
return nullptr;
21452154
}
21462155

@@ -2210,13 +2219,15 @@ void ControllerManager::deactivate_controllers(
22102219
RCLCPP_ERROR(
22112220
get_logger(), "Caught exception of type : %s while deactivating the controller '%s': %s",
22122221
typeid(e).name(), controller_name.c_str(), e.what());
2222+
params_->handle_exceptions ? void() : throw;
22132223
continue;
22142224
}
22152225
catch (...)
22162226
{
22172227
RCLCPP_ERROR(
22182228
get_logger(), "Caught unknown exception while deactivating the controller '%s'",
22192229
controller_name.c_str());
2230+
params_->handle_exceptions ? void() : throw;
22202231
continue;
22212232
}
22222233
}
@@ -2333,6 +2344,7 @@ void ControllerManager::activate_controllers(
23332344
"Caught exception of type : %s while claiming the command interfaces. Can't activate "
23342345
"controller '%s': %s",
23352346
typeid(e).name(), controller_name.c_str(), e.what());
2347+
params_->handle_exceptions ? void() : throw;
23362348
command_loans.clear();
23372349
assignment_successful = false;
23382350
break;
@@ -2374,6 +2386,7 @@ void ControllerManager::activate_controllers(
23742386
"controller '%s': %s",
23752387
typeid(e).name(), controller_name.c_str(), e.what());
23762388
assignment_successful = false;
2389+
params_->handle_exceptions ? void() : throw;
23772390
break;
23782391
}
23792392
}
@@ -2397,12 +2410,14 @@ void ControllerManager::activate_controllers(
23972410
RCLCPP_ERROR(
23982411
get_logger(), "Caught exception of type : %s while activating the controller '%s': %s",
23992412
typeid(e).name(), controller_name.c_str(), e.what());
2413+
params_->handle_exceptions ? void() : throw;
24002414
}
24012415
catch (...)
24022416
{
24032417
RCLCPP_ERROR(
24042418
get_logger(), "Caught unknown exception while activating the controller '%s'",
24052419
controller_name.c_str());
2420+
params_->handle_exceptions ? void() : throw;
24062421
}
24072422
if (new_state.id() != lifecycle_msgs::msg::State::PRIMARY_STATE_ACTIVE)
24082423
{
@@ -3127,13 +3142,15 @@ controller_interface::return_type ControllerManager::update(
31273142
RCLCPP_ERROR(
31283143
get_logger(), "Caught exception of type : %s while updating controller '%s': %s",
31293144
typeid(e).name(), loaded_controller.info.name.c_str(), e.what());
3145+
params_->handle_exceptions ? void() : throw;
31303146
controller_ret = controller_interface::return_type::ERROR;
31313147
}
31323148
catch (...)
31333149
{
31343150
RCLCPP_ERROR(
31353151
get_logger(), "Caught unknown exception while updating controller '%s'",
31363152
loaded_controller.info.name.c_str());
3153+
params_->handle_exceptions ? void() : throw;
31373154
controller_ret = controller_interface::return_type::ERROR;
31383155
}
31393156

controller_manager/src/controller_manager_parameters.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ controller_manager:
1313
description: "If true, the controller manager will enforce command limits defined in the robot description. If false, no limits will be enforced. If true, when the command is outside the limits, the command is clamped to be within the limits depending on the type of configured joint limits defined in the robot description. If the command is within the limits, the command is passed through without any changes.",
1414
}
1515

16+
handle_exceptions: {
17+
type: bool,
18+
default_value: true,
19+
read_only: true,
20+
description: "If true, the controller manager will catch exceptions thrown during the different operations of controllers and hardware components. If false, exceptions will propagate up and will cause the controller manager to crash.",
21+
}
22+
1623
hardware_components_initial_state:
1724
unconfigured: {
1825
type: string_array,

controller_manager/test/controller_manager_test_common.hpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,19 @@ class ControllerManagerFixture : public ::testing::Test
5757
public:
5858
explicit ControllerManagerFixture(
5959
const std::string & robot_description = ros2_control_test_assets::minimal_robot_urdf,
60-
const std::string & cm_namespace = "")
60+
const std::string & cm_namespace = "", std::vector<rclcpp::Parameter> cm_parameters = {})
6161
: robot_description_(robot_description)
6262
{
6363
executor_ = std::make_shared<rclcpp::executors::SingleThreadedExecutor>();
64+
rclcpp::NodeOptions cm_node_options = controller_manager::get_cm_node_options();
65+
if (!cm_parameters.empty())
66+
{
67+
cm_node_options.parameter_overrides(cm_parameters);
68+
}
6469
cm_ = std::make_shared<CtrlMgr>(
6570
std::make_unique<hardware_interface::ResourceManager>(
6671
rm_node_->get_node_clock_interface(), rm_node_->get_node_logging_interface()),
67-
executor_, TEST_CM_NAME, cm_namespace);
72+
executor_, TEST_CM_NAME, cm_namespace, cm_node_options);
6873
// We want to be able to not pass robot description immediately
6974
if (!robot_description_.empty())
7075
{

controller_manager/test/test_controller/test_controller.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ controller_interface::InterfaceConfiguration TestController::state_interface_con
6161
controller_interface::return_type TestController::update(
6262
const rclcpp::Time & time, const rclcpp::Duration & period)
6363
{
64+
if (throw_on_update)
65+
{
66+
throw std::runtime_error("Exception from TestController::update() as requested.");
67+
}
6468
if (time.get_clock_type() != RCL_ROS_TIME)
6569
{
6670
throw std::runtime_error("ROS Time is required for the controller to operate.");
@@ -101,7 +105,14 @@ controller_interface::return_type TestController::update(
101105
return controller_interface::return_type::OK;
102106
}
103107

104-
CallbackReturn TestController::on_init() { return CallbackReturn::SUCCESS; }
108+
CallbackReturn TestController::on_init()
109+
{
110+
if (throw_on_initialize)
111+
{
112+
throw std::runtime_error("Exception from TestController::on_init() as requested.");
113+
}
114+
return CallbackReturn::SUCCESS;
115+
}
105116

106117
CallbackReturn TestController::on_configure(const rclcpp_lifecycle::State & /*previous_state*/)
107118
{

controller_manager/test/test_controller/test_controller.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ class TestController : public controller_interface::ControllerInterface
8383
// errors
8484
double set_first_command_interface_value_to;
8585
rclcpp::Duration update_period_ = rclcpp::Duration::from_seconds(0.);
86+
87+
bool throw_on_initialize = false;
88+
bool throw_on_update = false;
8689
};
8790

8891
} // namespace test_controller

0 commit comments

Comments
 (0)