Skip to content

Commit b5fd514

Browse files
authored
Add handle_exceptions parameter to controller manager (#2807)
1 parent cde86ae commit b5fd514

14 files changed

+387
-20
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);
@@ -2134,6 +2141,7 @@ controller_interface::ControllerInterfaceBaseSharedPtr ControllerManager::add_co
21342141
RCLCPP_ERROR(
21352142
get_logger(), "Caught exception of type : %s while initializing controller '%s': %s",
21362143
typeid(e).name(), controller.info.name.c_str(), e.what());
2144+
params_->handle_exceptions ? void() : throw;
21372145
return nullptr;
21382146
}
21392147
catch (...)
@@ -2142,6 +2150,7 @@ controller_interface::ControllerInterfaceBaseSharedPtr ControllerManager::add_co
21422150
RCLCPP_ERROR(
21432151
get_logger(), "Caught unknown exception while initializing controller '%s'",
21442152
controller.info.name.c_str());
2153+
params_->handle_exceptions ? void() : throw;
21452154
return nullptr;
21462155
}
21472156

@@ -2211,13 +2220,15 @@ void ControllerManager::deactivate_controllers(
22112220
RCLCPP_ERROR(
22122221
get_logger(), "Caught exception of type : %s while deactivating the controller '%s': %s",
22132222
typeid(e).name(), controller_name.c_str(), e.what());
2223+
params_->handle_exceptions ? void() : throw;
22142224
continue;
22152225
}
22162226
catch (...)
22172227
{
22182228
RCLCPP_ERROR(
22192229
get_logger(), "Caught unknown exception while deactivating the controller '%s'",
22202230
controller_name.c_str());
2231+
params_->handle_exceptions ? void() : throw;
22212232
continue;
22222233
}
22232234
}
@@ -2334,6 +2345,7 @@ void ControllerManager::activate_controllers(
23342345
"Caught exception of type : %s while claiming the command interfaces. Can't activate "
23352346
"controller '%s': %s",
23362347
typeid(e).name(), controller_name.c_str(), e.what());
2348+
params_->handle_exceptions ? void() : throw;
23372349
command_loans.clear();
23382350
assignment_successful = false;
23392351
break;
@@ -2375,6 +2387,7 @@ void ControllerManager::activate_controllers(
23752387
"controller '%s': %s",
23762388
typeid(e).name(), controller_name.c_str(), e.what());
23772389
assignment_successful = false;
2390+
params_->handle_exceptions ? void() : throw;
23782391
break;
23792392
}
23802393
}
@@ -2398,12 +2411,14 @@ void ControllerManager::activate_controllers(
23982411
RCLCPP_ERROR(
23992412
get_logger(), "Caught exception of type : %s while activating the controller '%s': %s",
24002413
typeid(e).name(), controller_name.c_str(), e.what());
2414+
params_->handle_exceptions ? void() : throw;
24012415
}
24022416
catch (...)
24032417
{
24042418
RCLCPP_ERROR(
24052419
get_logger(), "Caught unknown exception while activating the controller '%s'",
24062420
controller_name.c_str());
2421+
params_->handle_exceptions ? void() : throw;
24072422
}
24082423
if (new_state.id() != lifecycle_msgs::msg::State::PRIMARY_STATE_ACTIVE)
24092424
{
@@ -3128,13 +3143,15 @@ controller_interface::return_type ControllerManager::update(
31283143
RCLCPP_ERROR(
31293144
get_logger(), "Caught exception of type : %s while updating controller '%s': %s",
31303145
typeid(e).name(), loaded_controller.info.name.c_str(), e.what());
3146+
params_->handle_exceptions ? void() : throw;
31313147
controller_ret = controller_interface::return_type::ERROR;
31323148
}
31333149
catch (...)
31343150
{
31353151
RCLCPP_ERROR(
31363152
get_logger(), "Caught unknown exception while updating controller '%s'",
31373153
loaded_controller.info.name.c_str());
3154+
params_->handle_exceptions ? void() : throw;
31383155
controller_ret = controller_interface::return_type::ERROR;
31393156
}
31403157

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)