|
| 1 | +/********************************************************************* |
| 2 | + * Software License Agreement (BSD License) |
| 3 | + * |
| 4 | + * Copyright (c) 2013, Willow Garage, Inc. |
| 5 | + * All rights reserved. |
| 6 | + * |
| 7 | + * Redistribution and use in source and binary forms, with or without |
| 8 | + * modification, are permitted provided that the following conditions |
| 9 | + * are met: |
| 10 | + * |
| 11 | + * * Redistributions of source code must retain the above copyright |
| 12 | + * notice, this list of conditions and the following disclaimer. |
| 13 | + * * Redistributions in binary form must reproduce the above |
| 14 | + * copyright notice, this list of conditions and the following |
| 15 | + * disclaimer in the documentation and/or other materials provided |
| 16 | + * with the distribution. |
| 17 | + * * Neither the name of Willow Garage nor the names of its |
| 18 | + * contributors may be used to endorse or promote products derived |
| 19 | + * from this software without specific prior written permission. |
| 20 | + * |
| 21 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 22 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 23 | + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 24 | + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 25 | + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 26 | + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 27 | + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 28 | + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 29 | + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 30 | + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
| 31 | + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 32 | + * POSSIBILITY OF SUCH DAMAGE. |
| 33 | + *********************************************************************/ |
| 34 | + |
| 35 | +#include <vector> |
| 36 | + |
| 37 | +#include <gtest/gtest.h> |
| 38 | + |
| 39 | +#include <controller_manager_msgs/srv/detail/list_controllers__struct.hpp> |
| 40 | +#include <controller_manager_msgs/srv/detail/switch_controller__struct.hpp> |
| 41 | +#include <eigen3/Eigen/Eigen> |
| 42 | +#include <moveit/controller_manager/controller_manager.hpp> |
| 43 | +#include <pluginlib/class_loader.hpp> |
| 44 | + |
| 45 | +class MockControllersManagerService final : public rclcpp::Node |
| 46 | +{ |
| 47 | +public: |
| 48 | + MockControllersManagerService() : Node("list_controllers_service") |
| 49 | + { |
| 50 | + list_controller_service_ = create_service<controller_manager_msgs::srv::ListControllers>( |
| 51 | + "controller_manager/list_controllers", |
| 52 | + [this](const std::shared_ptr<controller_manager_msgs::srv::ListControllers::Request>& request, |
| 53 | + const std::shared_ptr<controller_manager_msgs::srv::ListControllers::Response>& response) { |
| 54 | + handleListControllersService(request, response); |
| 55 | + }); |
| 56 | + switch_controller_service_ = create_service<controller_manager_msgs::srv::SwitchController>( |
| 57 | + "controller_manager/switch_controller", |
| 58 | + [this](const std::shared_ptr<controller_manager_msgs::srv::SwitchController::Request>& request, |
| 59 | + const std::shared_ptr<controller_manager_msgs::srv::SwitchController::Response>& response) { |
| 60 | + handleSwitchControllerService(request, response); |
| 61 | + }); |
| 62 | + } |
| 63 | + |
| 64 | + controller_manager_msgs::srv::SwitchController::Request last_request; |
| 65 | + |
| 66 | +private: |
| 67 | + void handleListControllersService( |
| 68 | + const std::shared_ptr<controller_manager_msgs::srv::ListControllers::Request>& /*request*/, |
| 69 | + const std::shared_ptr<controller_manager_msgs::srv::ListControllers::Response>& response) |
| 70 | + { |
| 71 | + controller_manager_msgs::msg::ChainConnection chain_connection_a; |
| 72 | + chain_connection_a.name = "B"; |
| 73 | + chain_connection_a.reference_interfaces = { "ref_1", "ref_2", "ref_3" }; |
| 74 | + controller_manager_msgs::msg::ControllerState controller_a; |
| 75 | + controller_a.chain_connections.push_back(chain_connection_a); |
| 76 | + controller_a.name = "A"; |
| 77 | + controller_a.is_chained = true; |
| 78 | + controller_a.required_command_interfaces = { "jnt_1", "jnt_2", "jnt_3" }; |
| 79 | + controller_a.type = "joint_trajectory_controller/JointTrajectoryController"; |
| 80 | + controller_a.state = activate_set_.find(controller_a.name) != activate_set_.end() ? "active" : "inactive"; |
| 81 | + |
| 82 | + controller_manager_msgs::msg::ControllerState controller_b; |
| 83 | + controller_b.name = "B"; |
| 84 | + controller_b.required_command_interfaces = { "jnt_4", "jnt_5" }; |
| 85 | + controller_b.reference_interfaces = { "ref_1", "ref_2", "ref_3" }; |
| 86 | + controller_b.type = "joint_trajectory_controller/JointTrajectoryController"; |
| 87 | + controller_b.state = activate_set_.find(controller_b.name) != activate_set_.end() ? "active" : "inactive"; |
| 88 | + |
| 89 | + response->controller = { controller_a, controller_b }; |
| 90 | + } |
| 91 | + |
| 92 | + void handleSwitchControllerService( |
| 93 | + const std::shared_ptr<controller_manager_msgs::srv::SwitchController::Request>& request, |
| 94 | + const std::shared_ptr<controller_manager_msgs::srv::SwitchController::Response>& response) |
| 95 | + { |
| 96 | + last_request = *request; |
| 97 | + for (const auto& deactivate : request->deactivate_controllers) |
| 98 | + { |
| 99 | + activate_set_.erase(deactivate); |
| 100 | + } |
| 101 | + for (const auto& activate : request->activate_controllers) |
| 102 | + { |
| 103 | + activate_set_.insert(activate); |
| 104 | + } |
| 105 | + response->ok = true; |
| 106 | + } |
| 107 | + |
| 108 | + std::unordered_set<std::string> activate_set_; |
| 109 | + rclcpp::Service<controller_manager_msgs::srv::ListControllers>::SharedPtr list_controller_service_; |
| 110 | + rclcpp::Service<controller_manager_msgs::srv::SwitchController>::SharedPtr switch_controller_service_; |
| 111 | +}; |
| 112 | + |
| 113 | +TEST(ControllerManagerPlugin, SwitchControllers) |
| 114 | +{ |
| 115 | + // GIVEN a ClassLoader for MoveItControllerManager |
| 116 | + pluginlib::ClassLoader<moveit_controller_manager::MoveItControllerManager> loader( |
| 117 | + "moveit_core", "moveit_controller_manager::MoveItControllerManager"); |
| 118 | + |
| 119 | + // WHEN we load the custom plugin defined in this package |
| 120 | + // THEN loading succeeds |
| 121 | + auto instance = loader.createUniqueInstance("moveit_ros_control_interface/Ros2ControlManager"); |
| 122 | + |
| 123 | + const auto mock_service = std::make_shared<MockControllersManagerService>(); |
| 124 | + rclcpp::executors::SingleThreadedExecutor executor; |
| 125 | + std::thread ros_thread([&executor, &mock_service]() { |
| 126 | + executor.add_node(mock_service); |
| 127 | + executor.spin(); |
| 128 | + }); |
| 129 | + instance->initialize(mock_service); |
| 130 | + |
| 131 | + // A and B should start |
| 132 | + instance->switchControllers({ "/A" }, {}); |
| 133 | + EXPECT_EQ(mock_service->last_request.activate_controllers, std::vector<std::string>({ "A", "B" })); |
| 134 | + EXPECT_EQ(mock_service->last_request.deactivate_controllers, std::vector<std::string>({})); |
| 135 | + // A and B should stop |
| 136 | + instance->switchControllers({}, { "/B" }); |
| 137 | + EXPECT_EQ(mock_service->last_request.activate_controllers, std::vector<std::string>({})); |
| 138 | + EXPECT_EQ(mock_service->last_request.deactivate_controllers, std::vector<std::string>({ "A", "B" })); |
| 139 | + // Only B should start |
| 140 | + instance->switchControllers({ "/B" }, {}); |
| 141 | + EXPECT_EQ(mock_service->last_request.activate_controllers, std::vector<std::string>({ "B" })); |
| 142 | + EXPECT_EQ(mock_service->last_request.deactivate_controllers, std::vector<std::string>({})); |
| 143 | + // Only B should stop |
| 144 | + instance->switchControllers({}, { "/B" }); |
| 145 | + EXPECT_EQ(mock_service->last_request.activate_controllers, std::vector<std::string>({})); |
| 146 | + EXPECT_EQ(mock_service->last_request.deactivate_controllers, std::vector<std::string>({ "B" })); |
| 147 | + // Multiple activations results in only 1 |
| 148 | + instance->switchControllers({ "/B", "/B", "/B", "/B" }, {}); |
| 149 | + EXPECT_EQ(mock_service->last_request.activate_controllers, std::vector<std::string>({ "B" })); |
| 150 | + EXPECT_EQ(mock_service->last_request.deactivate_controllers, std::vector<std::string>({})); |
| 151 | + instance->switchControllers({}, { "/B" }); |
| 152 | + // Multiple activation and deactivate of same controller results in empty list |
| 153 | + instance->switchControllers({ "/B", "/A" }, { "/A", "/A", "/A" }); |
| 154 | + EXPECT_EQ(mock_service->last_request.activate_controllers, std::vector<std::string>({ "B" })); |
| 155 | + EXPECT_EQ(mock_service->last_request.deactivate_controllers, std::vector<std::string>({})); |
| 156 | + |
| 157 | + executor.cancel(); |
| 158 | + ros_thread.join(); |
| 159 | +} |
| 160 | + |
| 161 | +TEST(ControllerManagerPlugin, LoadMoveItControllerManagerPlugin) |
| 162 | +{ |
| 163 | + // GIVEN a ClassLoader for MoveItControllerManager |
| 164 | + pluginlib::ClassLoader<moveit_controller_manager::MoveItControllerManager> loader( |
| 165 | + "moveit_core", "moveit_controller_manager::MoveItControllerManager"); |
| 166 | + |
| 167 | + // WHEN we load the custom plugin defined in this package |
| 168 | + // THEN loading succeeds |
| 169 | + EXPECT_NO_THROW(loader.createUniqueInstance("moveit_ros_control_interface/Ros2ControlManager")); |
| 170 | +} |
| 171 | + |
| 172 | +int main(int argc, char** argv) |
| 173 | +{ |
| 174 | + ::testing::InitGoogleTest(&argc, argv); |
| 175 | + rclcpp::init(argc, argv); |
| 176 | + const int result = RUN_ALL_TESTS(); |
| 177 | + rclcpp::shutdown(); |
| 178 | + return result; |
| 179 | +} |
0 commit comments