Skip to content

Commit 22e6ca1

Browse files
committed
add try to lock mutex and print to not to obsruct RT thread
1 parent c2336ae commit 22e6ca1

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

controller_manager/src/controller_manager.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1297,7 +1297,6 @@ controller_interface::return_type ControllerManager::switch_controller(
12971297
}
12981298

12991299
// start the atomic controller switching
1300-
std::unique_lock<std::mutex> switch_params_guard(switch_params_.mutex);
13011300
switch_params_.strictness = strictness;
13021301
switch_params_.activate_asap = activate_asap;
13031302
if (timeout == rclcpp::Duration{0, 0})
@@ -1310,9 +1309,9 @@ controller_interface::return_type ControllerManager::switch_controller(
13101309
switch_params_.timeout = timeout.to_chrono<std::chrono::nanoseconds>();
13111310
}
13121311
switch_params_.do_switch = true;
1313-
13141312
// wait until switch is finished
13151313
RCLCPP_DEBUG(get_logger(), "Requested atomic controller switch from realtime loop");
1314+
std::unique_lock<std::mutex> switch_params_guard(switch_params_.mutex, std::defer_lock);
13161315
if (!switch_params_.cv.wait_for(
13171316
switch_params_guard, switch_params_.timeout, [this] { return !switch_params_.do_switch; }))
13181317
{
@@ -2156,7 +2155,12 @@ void ControllerManager::read(const rclcpp::Time & time, const rclcpp::Duration &
21562155

21572156
void ControllerManager::manage_switch()
21582157
{
2159-
std::lock_guard<std::mutex> guard(switch_params_.mutex);
2158+
std::unique_lock<std::mutex> guard(switch_params_.mutex, std::try_to_lock);
2159+
if (!guard.owns_lock())
2160+
{
2161+
RCLCPP_DEBUG(get_logger(), "Unable to lock switch mutex. Retrying in next cycle.");
2162+
return;
2163+
}
21602164
// Ask hardware interfaces to change mode
21612165
if (!resource_manager_->perform_command_mode_switch(
21622166
activate_command_interface_request_, deactivate_command_interface_request_))

0 commit comments

Comments
 (0)