|
| 1 | +// Copyright (c) 2023 Open Navigation LLC |
| 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 <gtest/gtest.h> |
| 16 | +#include <memory> |
| 17 | +#include <set> |
| 18 | +#include <string> |
| 19 | + |
| 20 | +#include "behaviortree_cpp_v3/bt_factory.h" |
| 21 | + |
| 22 | +#include "nav2_behavior_tree/utils/test_action_server.hpp" |
| 23 | +#include "nav2_coverage_bt/cancel_complete_coverage_path.hpp" |
| 24 | +#include "lifecycle_msgs/srv/change_state.hpp" |
| 25 | + |
| 26 | +class CancelCoverageServer |
| 27 | + : public TestActionServer<nav2_complete_coverage_msgs::action::ComputeCoveragePath> |
| 28 | +{ |
| 29 | +public: |
| 30 | + CancelCoverageServer() |
| 31 | + : TestActionServer("compute_coverage_path") |
| 32 | + {} |
| 33 | + |
| 34 | +protected: |
| 35 | + void execute( |
| 36 | + const typename std::shared_ptr< |
| 37 | + rclcpp_action::ServerGoalHandle<nav2_complete_coverage_msgs::action::ComputeCoveragePath>> |
| 38 | + goal_handle) |
| 39 | + { |
| 40 | + while (!goal_handle->is_canceling()) { |
| 41 | + // Coverage here until goal cancels |
| 42 | + std::this_thread::sleep_for(std::chrono::milliseconds(15)); |
| 43 | + } |
| 44 | + } |
| 45 | +}; |
| 46 | + |
| 47 | +class CancelCoverageActionTestFixture : public ::testing::Test |
| 48 | +{ |
| 49 | +public: |
| 50 | + static void SetUpTestCase() |
| 51 | + { |
| 52 | + node_ = std::make_shared<rclcpp::Node>("cancel_compute_coverage_path_test_fixture"); |
| 53 | + factory_ = std::make_shared<BT::BehaviorTreeFactory>(); |
| 54 | + |
| 55 | + config_ = new BT::NodeConfiguration(); |
| 56 | + |
| 57 | + // Create the blackboard that will be shared by all of the nodes in the tree |
| 58 | + config_->blackboard = BT::Blackboard::create(); |
| 59 | + // Put items on the blackboard |
| 60 | + config_->blackboard->set<rclcpp::Node::SharedPtr>( |
| 61 | + "node", |
| 62 | + node_); |
| 63 | + config_->blackboard->set<std::chrono::milliseconds>( |
| 64 | + "server_timeout", |
| 65 | + std::chrono::milliseconds(20)); |
| 66 | + config_->blackboard->set<std::chrono::milliseconds>( |
| 67 | + "bt_loop_duration", |
| 68 | + std::chrono::milliseconds(10)); |
| 69 | + client_ = |
| 70 | + rclcpp_action::create_client<nav2_complete_coverage_msgs::action::ComputeCoveragePath>( |
| 71 | + node_, "compute_coverage_path"); |
| 72 | + |
| 73 | + BT::NodeBuilder builder = |
| 74 | + [](const std::string & name, const BT::NodeConfiguration & config) |
| 75 | + { |
| 76 | + return std::make_unique<nav2_coverage_bt::CoverageCancel>( |
| 77 | + name, "compute_coverage_path", config); |
| 78 | + }; |
| 79 | + |
| 80 | + factory_->registerBuilder<nav2_coverage_bt::CoverageCancel>( |
| 81 | + "CancelCoverage", builder); |
| 82 | + } |
| 83 | + |
| 84 | + static void TearDownTestCase() |
| 85 | + { |
| 86 | + delete config_; |
| 87 | + config_ = nullptr; |
| 88 | + node_.reset(); |
| 89 | + action_server_.reset(); |
| 90 | + client_.reset(); |
| 91 | + factory_.reset(); |
| 92 | + } |
| 93 | + |
| 94 | + void TearDown() override |
| 95 | + { |
| 96 | + tree_.reset(); |
| 97 | + } |
| 98 | + |
| 99 | + static std::shared_ptr<CancelCoverageServer> action_server_; |
| 100 | + static std::shared_ptr< |
| 101 | + rclcpp_action::Client<nav2_complete_coverage_msgs::action::ComputeCoveragePath>> client_; |
| 102 | + |
| 103 | +protected: |
| 104 | + static rclcpp::Node::SharedPtr node_; |
| 105 | + static BT::NodeConfiguration * config_; |
| 106 | + static std::shared_ptr<BT::BehaviorTreeFactory> factory_; |
| 107 | + static std::shared_ptr<BT::Tree> tree_; |
| 108 | +}; |
| 109 | + |
| 110 | +rclcpp::Node::SharedPtr CancelCoverageActionTestFixture::node_ = nullptr; |
| 111 | +std::shared_ptr<CancelCoverageServer> |
| 112 | +CancelCoverageActionTestFixture::action_server_ = nullptr; |
| 113 | +std::shared_ptr<rclcpp_action::Client<nav2_complete_coverage_msgs::action::ComputeCoveragePath>> |
| 114 | +CancelCoverageActionTestFixture::client_ = nullptr; |
| 115 | + |
| 116 | +BT::NodeConfiguration * CancelCoverageActionTestFixture::config_ = nullptr; |
| 117 | +std::shared_ptr<BT::BehaviorTreeFactory> |
| 118 | +CancelCoverageActionTestFixture::factory_ = nullptr; |
| 119 | +std::shared_ptr<BT::Tree> CancelCoverageActionTestFixture::tree_ = nullptr; |
| 120 | + |
| 121 | +TEST_F(CancelCoverageActionTestFixture, test_ports) |
| 122 | +{ |
| 123 | + std::string xml_txt = |
| 124 | + R"( |
| 125 | + <root main_tree_to_execute = "MainTree" > |
| 126 | + <BehaviorTree ID="MainTree"> |
| 127 | + <CancelCoverage name="CoverageCancel"/> |
| 128 | + </BehaviorTree> |
| 129 | + </root>)"; |
| 130 | + |
| 131 | + tree_ = std::make_shared<BT::Tree>(factory_->createTreeFromText(xml_txt, config_->blackboard)); |
| 132 | + auto send_goal_options = rclcpp_action::Client< |
| 133 | + nav2_complete_coverage_msgs::action::ComputeCoveragePath>::SendGoalOptions(); |
| 134 | + |
| 135 | + // Creating a dummy goal_msg |
| 136 | + auto goal_msg = nav2_complete_coverage_msgs::action::ComputeCoveragePath::Goal(); |
| 137 | + |
| 138 | + // BackUping for server and sending a goal |
| 139 | + client_->wait_for_action_server(); |
| 140 | + client_->async_send_goal(goal_msg, send_goal_options); |
| 141 | + |
| 142 | + // Adding a sleep so that the goal is indeed older than 10ms as described in our abstract class |
| 143 | + std::this_thread::sleep_for(std::chrono::milliseconds(15)); |
| 144 | + |
| 145 | + // Executing tick |
| 146 | + tree_->rootNode()->executeTick(); |
| 147 | + |
| 148 | + // BT node should return success, once when the goal is cancelled |
| 149 | + EXPECT_EQ(tree_->rootNode()->status(), BT::NodeStatus::SUCCESS); |
| 150 | + |
| 151 | + // Adding another test case to check if the goal is infact cancelling |
| 152 | + EXPECT_EQ(action_server_->isGoalCancelled(), true); |
| 153 | +} |
| 154 | + |
| 155 | +int main(int argc, char ** argv) |
| 156 | +{ |
| 157 | + ::testing::InitGoogleTest(&argc, argv); |
| 158 | + |
| 159 | + // initialize ROS |
| 160 | + rclcpp::init(argc, argv); |
| 161 | + |
| 162 | + // initialize action server and back_up on new thread |
| 163 | + CancelCoverageActionTestFixture::action_server_ = |
| 164 | + std::make_shared<CancelCoverageServer>(); |
| 165 | + std::thread server_thread([]() { |
| 166 | + rclcpp::spin(CancelCoverageActionTestFixture::action_server_); |
| 167 | + }); |
| 168 | + |
| 169 | + int all_successful = RUN_ALL_TESTS(); |
| 170 | + |
| 171 | + // shutdown ROS |
| 172 | + rclcpp::shutdown(); |
| 173 | + server_thread.join(); |
| 174 | + |
| 175 | + return all_successful; |
| 176 | +} |
0 commit comments