Skip to content

Commit ff52562

Browse files
authored
Fix exclusive hardware control mode switching on controller failed activation (#1522)
1 parent 56fc052 commit ff52562

File tree

8 files changed

+267
-12
lines changed

8 files changed

+267
-12
lines changed

controller_manager/CMakeLists.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,20 @@ if(BUILD_TESTING)
184184
DESTINATION lib
185185
)
186186

187+
add_library(test_controller_failed_activate SHARED
188+
test/test_controller_failed_activate/test_controller_failed_activate.cpp
189+
)
190+
target_link_libraries(test_controller_failed_activate PUBLIC
191+
controller_manager
192+
)
193+
target_compile_definitions(test_controller_failed_activate PRIVATE "CONTROLLER_MANAGER_BUILDING_DLL")
194+
pluginlib_export_plugin_description_file(
195+
controller_interface test/test_controller_failed_activate/test_controller_failed_activate.xml)
196+
install(
197+
TARGETS test_controller_failed_activate
198+
DESTINATION lib
199+
)
200+
187201
ament_add_gmock(test_release_interfaces
188202
test/test_release_interfaces.cpp
189203
APPEND_ENV AMENT_PREFIX_PATH=${ament_index_build_path}_$<CONFIG>
@@ -192,6 +206,7 @@ if(BUILD_TESTING)
192206
controller_manager
193207
test_controller
194208
test_controller_with_interfaces
209+
test_controller_failed_activate
195210
ros2_control_test_assets::ros2_control_test_assets
196211
)
197212

controller_manager/doc/userdoc.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,12 @@ Hardware and Controller Errors
403403
If the hardware during it's ``read`` or ``write`` method returns ``return_type::ERROR``, the controller manager will stop all controllers that are using the hardware's command and state interfaces.
404404
Likewise, if a controller returns ``return_type::ERROR`` from its ``update`` method, the controller manager will deactivate the respective controller. In future, the controller manager will try to start any fallback controllers if available.
405405

406+
Factors that affect Determinism
407+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
408+
When run under the conditions determined in the above section, the determinism is assured up to the limitations of the hardware and the real-time kernel. However, there are some situations that can affect determinism:
409+
410+
* When a controller fails to activate, the controller_manager will call the methods ``prepare_command_mode_switch`` and ``perform_command_mode_switch`` to stop the started interfaces. These calls can cause jitter in the main control loop.
411+
406412
Support for Asynchronous Updates
407413
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
408414
For some applications, it is desirable to run a controller at a lower frequency than the controller manager's update rate. For instance, if the ``update_rate`` for the controller manager is 100Hz, the sum of the execution times of all controllers' ``update`` calls and hardware components ``read`` and ``write`` calls must be below 10ms. If one controller requires 15ms of execution time, it cannot be executed synchronously without affecting the overall system update rate. Running a controller asynchronously can be beneficial in this scenario.

controller_manager/src/controller_manager.cpp

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2189,6 +2189,7 @@ void ControllerManager::activate_controllers(
21892189
const std::vector<ControllerSpec> & rt_controller_list,
21902190
const std::vector<std::string> controllers_to_activate)
21912191
{
2192+
std::vector<std::string> failed_controllers_command_interfaces;
21922193
for (const auto & controller_name : controllers_to_activate)
21932194
{
21942195
auto found_it = std::find_if(
@@ -2297,35 +2298,38 @@ void ControllerManager::activate_controllers(
22972298
}
22982299
controller->assign_interfaces(std::move(command_loans), std::move(state_loans));
22992300

2301+
auto new_state = controller->get_lifecycle_state();
23002302
try
23012303
{
23022304
found_it->periodicity_statistics->reset();
23032305
found_it->execution_time_statistics->reset();
2304-
const auto new_state = controller->get_node()->activate();
2305-
if (new_state.id() != lifecycle_msgs::msg::State::PRIMARY_STATE_ACTIVE)
2306-
{
2307-
RCLCPP_ERROR(
2308-
get_logger(),
2309-
"After activation, controller '%s' is in state '%s' (%d), expected '%s' (%d).",
2310-
controller->get_node()->get_name(), new_state.label().c_str(), new_state.id(),
2311-
hardware_interface::lifecycle_state_names::ACTIVE,
2312-
lifecycle_msgs::msg::State::PRIMARY_STATE_ACTIVE);
2313-
}
2306+
new_state = controller->get_node()->activate();
23142307
}
23152308
catch (const std::exception & e)
23162309
{
23172310
RCLCPP_ERROR(
23182311
get_logger(), "Caught exception of type : %s while activating the controller '%s': %s",
23192312
typeid(e).name(), controller_name.c_str(), e.what());
2320-
controller->release_interfaces();
2321-
continue;
23222313
}
23232314
catch (...)
23242315
{
23252316
RCLCPP_ERROR(
23262317
get_logger(), "Caught unknown exception while activating the controller '%s'",
23272318
controller_name.c_str());
2319+
}
2320+
if (new_state.id() != lifecycle_msgs::msg::State::PRIMARY_STATE_ACTIVE)
2321+
{
2322+
RCLCPP_ERROR(
2323+
get_logger(),
2324+
"After activation, controller '%s' is in state '%s' (%d), expected '%s' (%d). Releasing "
2325+
"interfaces!",
2326+
controller->get_node()->get_name(), new_state.label().c_str(), new_state.id(),
2327+
hardware_interface::lifecycle_state_names::ACTIVE,
2328+
lifecycle_msgs::msg::State::PRIMARY_STATE_ACTIVE);
23282329
controller->release_interfaces();
2330+
failed_controllers_command_interfaces.insert(
2331+
failed_controllers_command_interfaces.end(), command_interface_names.begin(),
2332+
command_interface_names.end());
23292333
continue;
23302334
}
23312335

@@ -2337,6 +2341,18 @@ void ControllerManager::activate_controllers(
23372341
resource_manager_->make_controller_reference_interfaces_available(controller_name);
23382342
}
23392343
}
2344+
// Now prepare and perform the stop interface switching as this is needed for exclusive
2345+
// interfaces
2346+
if (
2347+
!failed_controllers_command_interfaces.empty() &&
2348+
(!resource_manager_->prepare_command_mode_switch({}, failed_controllers_command_interfaces) ||
2349+
!resource_manager_->perform_command_mode_switch({}, failed_controllers_command_interfaces)))
2350+
{
2351+
RCLCPP_ERROR(
2352+
get_logger(),
2353+
"Error switching back the interfaces in the hardware when the controller activation "
2354+
"failed.");
2355+
}
23402356
}
23412357

23422358
void ControllerManager::activate_controllers_asap(
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// Copyright 2021 Department of Engineering Cybernetics, NTNU.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "test_controller_failed_activate.hpp"
16+
17+
#include <memory>
18+
#include <string>
19+
20+
#include "lifecycle_msgs/msg/transition.hpp"
21+
22+
namespace test_controller_failed_activate
23+
{
24+
TestControllerFailedActivate::TestControllerFailedActivate()
25+
: controller_interface::ControllerInterface()
26+
{
27+
}
28+
29+
rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn
30+
TestControllerFailedActivate::on_init()
31+
{
32+
return rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn::SUCCESS;
33+
}
34+
35+
controller_interface::return_type TestControllerFailedActivate::update(
36+
const rclcpp::Time & /*time*/, const rclcpp::Duration & /*period*/)
37+
{
38+
return controller_interface::return_type::OK;
39+
}
40+
41+
rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn
42+
TestControllerFailedActivate::on_configure(const rclcpp_lifecycle::State & /*previous_state&*/)
43+
{
44+
return rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn::SUCCESS;
45+
}
46+
47+
rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn
48+
TestControllerFailedActivate::on_activate(const rclcpp_lifecycle::State & /*previous_state&*/)
49+
{
50+
// Simply simulate a controller that can not be activated
51+
return rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn::FAILURE;
52+
}
53+
54+
rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn
55+
TestControllerFailedActivate::on_cleanup(const rclcpp_lifecycle::State & /*previous_state*/)
56+
{
57+
return rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn::SUCCESS;
58+
}
59+
60+
} // namespace test_controller_failed_activate
61+
62+
#include "pluginlib/class_list_macros.hpp"
63+
64+
PLUGINLIB_EXPORT_CLASS(
65+
test_controller_failed_activate::TestControllerFailedActivate,
66+
controller_interface::ControllerInterface)
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Copyright 2020 Department of Engineering Cybernetics, NTNU
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef TEST_CONTROLLER_FAILED_ACTIVATE__TEST_CONTROLLER_FAILED_ACTIVATE_HPP_
16+
#define TEST_CONTROLLER_FAILED_ACTIVATE__TEST_CONTROLLER_FAILED_ACTIVATE_HPP_
17+
18+
#include <memory>
19+
#include <string>
20+
21+
#include "controller_manager/controller_manager.hpp"
22+
23+
namespace test_controller_failed_activate
24+
{
25+
// Corresponds to the name listed within the pluginglib xml
26+
constexpr char TEST_CONTROLLER_WITH_INTERFACES_CLASS_NAME[] =
27+
"controller_manager/test_controller_failed_activate";
28+
// Corresponds to the command interface to claim
29+
constexpr char TEST_CONTROLLER_COMMAND_INTERFACE[] = "joint2/velocity";
30+
class TestControllerFailedActivate : public controller_interface::ControllerInterface
31+
{
32+
public:
33+
TestControllerFailedActivate();
34+
35+
virtual ~TestControllerFailedActivate() = default;
36+
37+
controller_interface::InterfaceConfiguration command_interface_configuration() const override
38+
{
39+
return controller_interface::InterfaceConfiguration{
40+
controller_interface::interface_configuration_type::INDIVIDUAL,
41+
{TEST_CONTROLLER_COMMAND_INTERFACE}};
42+
}
43+
44+
controller_interface::InterfaceConfiguration state_interface_configuration() const override
45+
{
46+
return controller_interface::InterfaceConfiguration{
47+
controller_interface::interface_configuration_type::NONE};
48+
}
49+
50+
controller_interface::return_type update(
51+
const rclcpp::Time & time, const rclcpp::Duration & period) override;
52+
53+
rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn on_init() override;
54+
55+
rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn on_configure(
56+
const rclcpp_lifecycle::State & previous_state) override;
57+
58+
rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn on_activate(
59+
const rclcpp_lifecycle::State & previous_state) override;
60+
61+
rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn on_cleanup(
62+
const rclcpp_lifecycle::State & previous_state) override;
63+
};
64+
65+
} // namespace test_controller_failed_activate
66+
67+
#endif // TEST_CONTROLLER_FAILED_ACTIVATE__TEST_CONTROLLER_FAILED_ACTIVATE_HPP_
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<library path="test_controller_failed_activate">
2+
3+
<class name="controller_manager/test_controller_failed_activate" type="test_controller_failed_activate::TestControllerFailedActivate" base_class_type="controller_interface::ControllerInterface">
4+
<description>
5+
Controller used for testing
6+
</description>
7+
</class>
8+
9+
</library>

controller_manager/test/test_release_interfaces.cpp

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "gmock/gmock.h"
2020
#include "lifecycle_msgs/msg/state.hpp"
2121
#include "test_controller/test_controller.hpp"
22+
#include "test_controller_failed_activate/test_controller_failed_activate.hpp"
2223
#include "test_controller_with_interfaces/test_controller_with_interfaces.hpp"
2324

2425
using ::testing::_;
@@ -330,3 +331,72 @@ TEST_F(TestReleaseExclusiveInterfaces, test_exclusive_interface_deactivation_on_
330331
abstract_test_controller2.c->get_lifecycle_state().id());
331332
}
332333
}
334+
335+
TEST_F(TestReleaseExclusiveInterfaces, test_exclusive_interface_switching_failure)
336+
{
337+
std::string controller_type =
338+
test_controller_failed_activate::TEST_CONTROLLER_WITH_INTERFACES_CLASS_NAME;
339+
340+
// Load two controllers of different names
341+
std::string controller_name1 = "test_controller1";
342+
std::string controller_name2 = "test_controller2";
343+
ASSERT_NO_THROW(cm_->load_controller(controller_name1, controller_type));
344+
ASSERT_NO_THROW(cm_->load_controller(
345+
controller_name2, test_controller_with_interfaces::TEST_CONTROLLER_WITH_INTERFACES_CLASS_NAME));
346+
ASSERT_EQ(2u, cm_->get_loaded_controllers().size());
347+
controller_manager::ControllerSpec abstract_test_controller1 = cm_->get_loaded_controllers()[0];
348+
controller_manager::ControllerSpec abstract_test_controller2 = cm_->get_loaded_controllers()[1];
349+
350+
// Configure controllers
351+
ASSERT_EQ(controller_interface::return_type::OK, cm_->configure_controller(controller_name1));
352+
ASSERT_EQ(controller_interface::return_type::OK, cm_->configure_controller(controller_name2));
353+
354+
ASSERT_EQ(
355+
lifecycle_msgs::msg::State::PRIMARY_STATE_INACTIVE,
356+
abstract_test_controller1.c->get_lifecycle_state().id());
357+
ASSERT_EQ(
358+
lifecycle_msgs::msg::State::PRIMARY_STATE_INACTIVE,
359+
abstract_test_controller2.c->get_lifecycle_state().id());
360+
361+
{
362+
// Test starting the first controller
363+
// test_controller1 activation always fails
364+
RCLCPP_INFO(cm_->get_logger(), "Starting controller #1");
365+
std::vector<std::string> start_controllers = {controller_name1};
366+
std::vector<std::string> stop_controllers = {};
367+
auto switch_future = std::async(
368+
std::launch::async, &controller_manager::ControllerManager::switch_controller, cm_,
369+
start_controllers, stop_controllers, STRICT, true, rclcpp::Duration(0, 0));
370+
ASSERT_EQ(std::future_status::timeout, switch_future.wait_for(std::chrono::milliseconds(100)))
371+
<< "switch_controller should be blocking until next update cycle";
372+
ControllerManagerRunner cm_runner(this);
373+
EXPECT_EQ(controller_interface::return_type::ERROR, switch_future.get());
374+
ASSERT_EQ(
375+
lifecycle_msgs::msg::State::PRIMARY_STATE_INACTIVE,
376+
abstract_test_controller1.c->get_lifecycle_state().id());
377+
ASSERT_EQ(
378+
lifecycle_msgs::msg::State::PRIMARY_STATE_INACTIVE,
379+
abstract_test_controller2.c->get_lifecycle_state().id());
380+
}
381+
382+
{
383+
// Test starting the second controller, interfaces should have been released
384+
// test_controller2 always successfully activates
385+
RCLCPP_INFO(cm_->get_logger(), "Starting controller #2");
386+
std::vector<std::string> start_controllers = {controller_name2};
387+
std::vector<std::string> stop_controllers = {};
388+
auto switch_future = std::async(
389+
std::launch::async, &controller_manager::ControllerManager::switch_controller, cm_,
390+
start_controllers, stop_controllers, STRICT, true, rclcpp::Duration(0, 0));
391+
ASSERT_EQ(std::future_status::timeout, switch_future.wait_for(std::chrono::milliseconds(100)))
392+
<< "switch_controller should be blocking until next update cycle";
393+
ControllerManagerRunner cm_runner(this);
394+
EXPECT_EQ(controller_interface::return_type::OK, switch_future.get());
395+
ASSERT_EQ(
396+
lifecycle_msgs::msg::State::PRIMARY_STATE_INACTIVE,
397+
abstract_test_controller1.c->get_lifecycle_state().id());
398+
ASSERT_EQ(
399+
lifecycle_msgs::msg::State::PRIMARY_STATE_ACTIVE,
400+
abstract_test_controller2.c->get_lifecycle_state().id());
401+
}
402+
}

hardware_interface_testing/test/test_components/test_components.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
Test Actuator
66
</description>
77
</class>
8+
<class name="test_actuator_exclusive_interfaces" type="TestActuatorExclusiveInterfaces" base_class_type="hardware_interface::ActuatorInterface">
9+
<description>
10+
Test Actuator
11+
</description>
12+
</class>
13+
814

915
<class name="test_sensor" type="TestSensor" base_class_type="hardware_interface::SensorInterface">
1016
<description>

0 commit comments

Comments
 (0)