-
Notifications
You must be signed in to change notification settings - Fork 640
Add logic to Ros2ControlManager to match ros2_control (backport #3332) #3342
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ff3427b
Add logic to Ros2ControlManager to match ros2_control (#3332)
pac48 84dfdc2
Update moveit_plugins/moveit_ros_control_interface/CMakeLists.txt
sea-bass ad1f30a
Update moveit_plugins/moveit_ros_control_interface/src/controller_man…
sea-bass 6b51285
format
sea-bass 026549b
Merge branch 'humble' into mergify/bp/humble/pr-3332
sea-bass 3ceb0b8
Update test_controller_manager_plugin.cpp
sea-bass fb15464
Just run the tests
sea-bass File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
moveit_plugins/moveit_ros_control_interface/test/CMakeLists.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
find_package(ament_cmake_gtest REQUIRED) | ||
find_package(pluginlib REQUIRED) | ||
ament_add_gtest(test_controller_manager_plugin | ||
test_controller_manager_plugin.cpp TIMEOUT 20) | ||
target_link_libraries(test_controller_manager_plugin | ||
moveit_ros_control_interface_plugin) | ||
target_include_directories(test_controller_manager_plugin | ||
PRIVATE ${CMAKE_SOURCE_DIR}/include) |
179 changes: 179 additions & 0 deletions
179
moveit_plugins/moveit_ros_control_interface/test/test_controller_manager_plugin.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
/********************************************************************* | ||
* Software License Agreement (BSD License) | ||
* | ||
* Copyright (c) 2013, Willow Garage, Inc. | ||
* All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions | ||
* are met: | ||
* | ||
* * Redistributions of source code must retain the above copyright | ||
* notice, this list of conditions and the following disclaimer. | ||
* * Redistributions in binary form must reproduce the above | ||
* copyright notice, this list of conditions and the following | ||
* disclaimer in the documentation and/or other materials provided | ||
* with the distribution. | ||
* * Neither the name of Willow Garage nor the names of its | ||
* contributors may be used to endorse or promote products derived | ||
* from this software without specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | ||
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | ||
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | ||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | ||
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN | ||
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
* POSSIBILITY OF SUCH DAMAGE. | ||
*********************************************************************/ | ||
|
||
#include <vector> | ||
|
||
#include <gtest/gtest.h> | ||
|
||
#include <controller_manager_msgs/srv/detail/list_controllers__struct.hpp> | ||
#include <controller_manager_msgs/srv/detail/switch_controller__struct.hpp> | ||
#include <eigen3/Eigen/Eigen> | ||
#include <moveit/controller_manager/controller_manager.hpp> | ||
#include <pluginlib/class_loader.hpp> | ||
|
||
class MockControllersManagerService final : public rclcpp::Node | ||
{ | ||
public: | ||
MockControllersManagerService() : Node("list_controllers_service") | ||
{ | ||
list_controller_service_ = create_service<controller_manager_msgs::srv::ListControllers>( | ||
"controller_manager/list_controllers", | ||
[this](const std::shared_ptr<controller_manager_msgs::srv::ListControllers::Request>& request, | ||
const std::shared_ptr<controller_manager_msgs::srv::ListControllers::Response>& response) { | ||
handleListControllersService(request, response); | ||
}); | ||
switch_controller_service_ = create_service<controller_manager_msgs::srv::SwitchController>( | ||
"controller_manager/switch_controller", | ||
[this](const std::shared_ptr<controller_manager_msgs::srv::SwitchController::Request>& request, | ||
const std::shared_ptr<controller_manager_msgs::srv::SwitchController::Response>& response) { | ||
handleSwitchControllerService(request, response); | ||
}); | ||
} | ||
|
||
controller_manager_msgs::srv::SwitchController::Request last_request; | ||
|
||
private: | ||
void handleListControllersService( | ||
const std::shared_ptr<controller_manager_msgs::srv::ListControllers::Request>& /*request*/, | ||
const std::shared_ptr<controller_manager_msgs::srv::ListControllers::Response>& response) | ||
{ | ||
controller_manager_msgs::msg::ChainConnection chain_connection_a; | ||
chain_connection_a.name = "B"; | ||
chain_connection_a.reference_interfaces = { "ref_1", "ref_2", "ref_3" }; | ||
controller_manager_msgs::msg::ControllerState controller_a; | ||
controller_a.chain_connections.push_back(chain_connection_a); | ||
controller_a.name = "A"; | ||
controller_a.is_chained = true; | ||
controller_a.required_command_interfaces = { "jnt_1", "jnt_2", "jnt_3" }; | ||
controller_a.type = "joint_trajectory_controller/JointTrajectoryController"; | ||
controller_a.state = activate_set_.find(controller_a.name) != activate_set_.end() ? "active" : "inactive"; | ||
|
||
controller_manager_msgs::msg::ControllerState controller_b; | ||
controller_b.name = "B"; | ||
controller_b.required_command_interfaces = { "jnt_4", "jnt_5" }; | ||
controller_b.reference_interfaces = { "ref_1", "ref_2", "ref_3" }; | ||
controller_b.type = "joint_trajectory_controller/JointTrajectoryController"; | ||
controller_b.state = activate_set_.find(controller_b.name) != activate_set_.end() ? "active" : "inactive"; | ||
|
||
response->controller = { controller_a, controller_b }; | ||
} | ||
|
||
void handleSwitchControllerService( | ||
const std::shared_ptr<controller_manager_msgs::srv::SwitchController::Request>& request, | ||
const std::shared_ptr<controller_manager_msgs::srv::SwitchController::Response>& response) | ||
{ | ||
last_request = *request; | ||
for (const auto& deactivate : request->deactivate_controllers) | ||
{ | ||
activate_set_.erase(deactivate); | ||
} | ||
for (const auto& activate : request->activate_controllers) | ||
{ | ||
activate_set_.insert(activate); | ||
} | ||
response->ok = true; | ||
} | ||
|
||
std::unordered_set<std::string> activate_set_; | ||
rclcpp::Service<controller_manager_msgs::srv::ListControllers>::SharedPtr list_controller_service_; | ||
rclcpp::Service<controller_manager_msgs::srv::SwitchController>::SharedPtr switch_controller_service_; | ||
}; | ||
|
||
TEST(ControllerManagerPlugin, SwitchControllers) | ||
{ | ||
// GIVEN a ClassLoader for MoveItControllerManager | ||
pluginlib::ClassLoader<moveit_controller_manager::MoveItControllerManager> loader( | ||
"moveit_core", "moveit_controller_manager::MoveItControllerManager"); | ||
|
||
// WHEN we load the custom plugin defined in this package | ||
// THEN loading succeeds | ||
auto instance = loader.createUniqueInstance("moveit_ros_control_interface/Ros2ControlManager"); | ||
|
||
const auto mock_service = std::make_shared<MockControllersManagerService>(); | ||
rclcpp::executors::SingleThreadedExecutor executor; | ||
std::thread ros_thread([&executor, &mock_service]() { | ||
executor.add_node(mock_service); | ||
executor.spin(); | ||
}); | ||
instance->initialize(mock_service); | ||
|
||
// A and B should start | ||
instance->switchControllers({ "/A" }, {}); | ||
EXPECT_EQ(mock_service->last_request.activate_controllers, std::vector<std::string>({ "A", "B" })); | ||
EXPECT_EQ(mock_service->last_request.deactivate_controllers, std::vector<std::string>({})); | ||
// A and B should stop | ||
instance->switchControllers({}, { "/B" }); | ||
EXPECT_EQ(mock_service->last_request.activate_controllers, std::vector<std::string>({})); | ||
EXPECT_EQ(mock_service->last_request.deactivate_controllers, std::vector<std::string>({ "A", "B" })); | ||
// Only B should start | ||
instance->switchControllers({ "/B" }, {}); | ||
EXPECT_EQ(mock_service->last_request.activate_controllers, std::vector<std::string>({ "B" })); | ||
EXPECT_EQ(mock_service->last_request.deactivate_controllers, std::vector<std::string>({})); | ||
// Only B should stop | ||
instance->switchControllers({}, { "/B" }); | ||
EXPECT_EQ(mock_service->last_request.activate_controllers, std::vector<std::string>({})); | ||
EXPECT_EQ(mock_service->last_request.deactivate_controllers, std::vector<std::string>({ "B" })); | ||
// Multiple activations results in only 1 | ||
instance->switchControllers({ "/B", "/B", "/B", "/B" }, {}); | ||
EXPECT_EQ(mock_service->last_request.activate_controllers, std::vector<std::string>({ "B" })); | ||
EXPECT_EQ(mock_service->last_request.deactivate_controllers, std::vector<std::string>({})); | ||
instance->switchControllers({}, { "/B" }); | ||
// Multiple activation and deactivate of same controller results in empty list | ||
instance->switchControllers({ "/B", "/A" }, { "/A", "/A", "/A" }); | ||
EXPECT_EQ(mock_service->last_request.activate_controllers, std::vector<std::string>({ "B" })); | ||
EXPECT_EQ(mock_service->last_request.deactivate_controllers, std::vector<std::string>({})); | ||
|
||
executor.cancel(); | ||
ros_thread.join(); | ||
} | ||
|
||
TEST(ControllerManagerPlugin, LoadMoveItControllerManagerPlugin) | ||
{ | ||
// GIVEN a ClassLoader for MoveItControllerManager | ||
pluginlib::ClassLoader<moveit_controller_manager::MoveItControllerManager> loader( | ||
"moveit_core", "moveit_controller_manager::MoveItControllerManager"); | ||
|
||
// WHEN we load the custom plugin defined in this package | ||
// THEN loading succeeds | ||
EXPECT_NO_THROW(loader.createUniqueInstance("moveit_ros_control_interface/Ros2ControlManager")); | ||
} | ||
|
||
int main(int argc, char** argv) | ||
{ | ||
::testing::InitGoogleTest(&argc, argv); | ||
rclcpp::init(argc, argv); | ||
const int result = RUN_ALL_TESTS(); | ||
rclcpp::shutdown(); | ||
return result; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.