Skip to content

Commit 62a5da0

Browse files
authored
Fix exclusive hardware control mode switching on controller failed activation (backport #1522) (#2579)
1 parent 4603de8 commit 62a5da0

11 files changed

Lines changed: 518 additions & 1 deletion

File tree

controller_manager/CMakeLists.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,13 +162,28 @@ if(BUILD_TESTING)
162162
DESTINATION lib
163163
)
164164

165+
add_library(test_controller_failed_activate SHARED
166+
test/test_controller_failed_activate/test_controller_failed_activate.cpp
167+
)
168+
target_link_libraries(test_controller_failed_activate PUBLIC
169+
controller_manager
170+
)
171+
target_compile_definitions(test_controller_failed_activate PRIVATE "CONTROLLER_MANAGER_BUILDING_DLL")
172+
pluginlib_export_plugin_description_file(
173+
controller_interface test/test_controller_failed_activate/test_controller_failed_activate.xml)
174+
install(
175+
TARGETS test_controller_failed_activate
176+
DESTINATION lib
177+
)
178+
165179
ament_add_gmock(test_release_interfaces
166180
test/test_release_interfaces.cpp
167181
APPEND_ENV AMENT_PREFIX_PATH=${ament_index_build_path}_$<CONFIG>
168182
)
169183
target_link_libraries(test_release_interfaces
170184
controller_manager
171185
test_controller_with_interfaces
186+
test_controller_failed_activate
172187
ros2_control_test_assets::ros2_control_test_assets
173188
)
174189

controller_manager/doc/userdoc.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,3 +359,9 @@ Restarting hardware
359359
If hardware gets restarted then you should go through its lifecycle again.
360360
This can be simply achieved by returning ``ERROR`` from ``write`` and ``read`` methods of interface implementation.
361361
**NOT IMPLEMENTED YET - PLEASE STOP/RESTART ALL CONTROLLERS MANUALLY FOR NOW** The controller manager detects that and stops all the controllers that are commanding that hardware and restarts broadcasters that are listening to its states.
362+
363+
Factors that affect Determinism
364+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
365+
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:
366+
367+
* 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.

controller_manager/src/controller_manager.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1578,6 +1578,7 @@ void ControllerManager::activate_controllers()
15781578
{
15791579
std::vector<ControllerSpec> & rt_controller_list =
15801580
rt_controllers_wrapper_.update_and_get_used_by_rt_list();
1581+
std::vector<std::string> failed_controllers_command_interfaces;
15811582
for (const auto & controller_name : activate_request_)
15821583
{
15831584
auto found_it = std::find_if(
@@ -1682,10 +1683,16 @@ void ControllerManager::activate_controllers()
16821683
{
16831684
RCLCPP_ERROR(
16841685
get_logger(),
1685-
"After activation, controller '%s' is in state '%s' (%d), expected '%s' (%d).",
1686+
"After activation, controller '%s' is in state '%s' (%d), expected '%s' (%d). Releasing "
1687+
"interfaces!",
16861688
controller->get_node()->get_name(), new_state.label().c_str(), new_state.id(),
16871689
hardware_interface::lifecycle_state_names::ACTIVE,
16881690
lifecycle_msgs::msg::State::PRIMARY_STATE_ACTIVE);
1691+
controller->release_interfaces();
1692+
failed_controllers_command_interfaces.insert(
1693+
failed_controllers_command_interfaces.end(), command_interface_names.begin(),
1694+
command_interface_names.end());
1695+
continue;
16891696
}
16901697

16911698
// if it is a chainable controller, make the reference interfaces available on activation
@@ -1694,6 +1701,18 @@ void ControllerManager::activate_controllers()
16941701
resource_manager_->make_controller_reference_interfaces_available(controller_name);
16951702
}
16961703
}
1704+
// Now prepare and perform the stop interface switching as this is needed for exclusive
1705+
// interfaces
1706+
if (
1707+
!failed_controllers_command_interfaces.empty() &&
1708+
(!resource_manager_->prepare_command_mode_switch({}, failed_controllers_command_interfaces) ||
1709+
!resource_manager_->perform_command_mode_switch({}, failed_controllers_command_interfaces)))
1710+
{
1711+
RCLCPP_ERROR(
1712+
get_logger(),
1713+
"Error switching back the interfaces in the hardware when the controller activation "
1714+
"failed.");
1715+
}
16971716
// All controllers activated, switching done
16981717
switch_params_.do_switch = false;
16991718
}
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: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#include "controller_manager/controller_manager.hpp"
2323
#include "controller_manager_test_common.hpp"
2424
#include "lifecycle_msgs/msg/state.hpp"
25+
#include "test_controller/test_controller.hpp"
26+
#include "test_controller_failed_activate/test_controller_failed_activate.hpp"
2527
#include "test_controller_with_interfaces/test_controller_with_interfaces.hpp"
2628

2729
using ::testing::_;
@@ -199,3 +201,85 @@ TEST_F(TestReleaseInterfaces, switch_controllers_same_interface)
199201
abstract_test_controller2.c->get_state().id());
200202
}
201203
}
204+
205+
class TestReleaseExclusiveInterfaces
206+
: public ControllerManagerFixture<controller_manager::ControllerManager>
207+
{
208+
public:
209+
TestReleaseExclusiveInterfaces()
210+
: ControllerManagerFixture<controller_manager::ControllerManager>(
211+
std::string(ros2_control_test_assets::urdf_head) +
212+
std::string(ros2_control_test_assets::hardware_resources_with_exclusive_interface) +
213+
std::string(ros2_control_test_assets::urdf_tail))
214+
{
215+
}
216+
};
217+
218+
TEST_F(TestReleaseExclusiveInterfaces, test_exclusive_interface_switching_failure)
219+
{
220+
std::string controller_type =
221+
test_controller_failed_activate::TEST_CONTROLLER_WITH_INTERFACES_CLASS_NAME;
222+
223+
// Load two controllers of different names
224+
std::string controller_name1 = "test_controller1";
225+
std::string controller_name2 = "test_controller2";
226+
ASSERT_NO_THROW(cm_->load_controller(controller_name1, controller_type));
227+
ASSERT_NO_THROW(cm_->load_controller(
228+
controller_name2, test_controller_with_interfaces::TEST_CONTROLLER_WITH_INTERFACES_CLASS_NAME));
229+
ASSERT_EQ(2u, cm_->get_loaded_controllers().size());
230+
controller_manager::ControllerSpec abstract_test_controller1 = cm_->get_loaded_controllers()[0];
231+
controller_manager::ControllerSpec abstract_test_controller2 = cm_->get_loaded_controllers()[1];
232+
233+
// Configure controllers
234+
ASSERT_EQ(controller_interface::return_type::OK, cm_->configure_controller(controller_name1));
235+
ASSERT_EQ(controller_interface::return_type::OK, cm_->configure_controller(controller_name2));
236+
237+
ASSERT_EQ(
238+
lifecycle_msgs::msg::State::PRIMARY_STATE_INACTIVE,
239+
abstract_test_controller1.c->get_state().id());
240+
ASSERT_EQ(
241+
lifecycle_msgs::msg::State::PRIMARY_STATE_INACTIVE,
242+
abstract_test_controller2.c->get_state().id());
243+
244+
{
245+
// Test starting the first controller
246+
// test_controller1 activation always fails
247+
RCLCPP_INFO(cm_->get_logger(), "Starting controller #1");
248+
std::vector<std::string> start_controllers = {controller_name1};
249+
std::vector<std::string> stop_controllers = {};
250+
auto switch_future = std::async(
251+
std::launch::async, &controller_manager::ControllerManager::switch_controller, cm_,
252+
start_controllers, stop_controllers, STRICT, true, rclcpp::Duration(0, 0));
253+
ASSERT_EQ(std::future_status::timeout, switch_future.wait_for(std::chrono::milliseconds(100)))
254+
<< "switch_controller should be blocking until next update cycle";
255+
ControllerManagerRunner cm_runner(this);
256+
EXPECT_EQ(controller_interface::return_type::ERROR, switch_future.get());
257+
ASSERT_EQ(
258+
lifecycle_msgs::msg::State::PRIMARY_STATE_INACTIVE,
259+
abstract_test_controller1.c->get_state().id());
260+
ASSERT_EQ(
261+
lifecycle_msgs::msg::State::PRIMARY_STATE_INACTIVE,
262+
abstract_test_controller2.c->get_state().id());
263+
}
264+
265+
{
266+
// Test starting the second controller, interfaces should have been released
267+
// test_controller2 always successfully activates
268+
RCLCPP_INFO(cm_->get_logger(), "Starting controller #2");
269+
std::vector<std::string> start_controllers = {controller_name2};
270+
std::vector<std::string> stop_controllers = {};
271+
auto switch_future = std::async(
272+
std::launch::async, &controller_manager::ControllerManager::switch_controller, cm_,
273+
start_controllers, stop_controllers, STRICT, true, rclcpp::Duration(0, 0));
274+
ASSERT_EQ(std::future_status::timeout, switch_future.wait_for(std::chrono::milliseconds(100)))
275+
<< "switch_controller should be blocking until next update cycle";
276+
ControllerManagerRunner cm_runner(this);
277+
EXPECT_EQ(controller_interface::return_type::OK, switch_future.get());
278+
ASSERT_EQ(
279+
lifecycle_msgs::msg::State::PRIMARY_STATE_INACTIVE,
280+
abstract_test_controller1.c->get_state().id());
281+
ASSERT_EQ(
282+
lifecycle_msgs::msg::State::PRIMARY_STATE_ACTIVE,
283+
abstract_test_controller2.c->get_state().id());
284+
}
285+
}

hardware_interface_testing/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ add_library(test_components SHARED
2323
test/test_components/test_actuator.cpp
2424
test/test_components/test_sensor.cpp
2525
test/test_components/test_system.cpp
26+
test/test_components/test_actuator_exclusive_interfaces.cpp
2627
)
2728
ament_target_dependencies(test_components hardware_interface pluginlib ros2_control_test_assets)
2829
install(TARGETS test_components

0 commit comments

Comments
 (0)