|
| 1 | +// Copyright (c) 2018 Intel Corporation |
| 2 | +// Copyright (c) 2020 Sarthak Mittal |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | + |
| 16 | +#include <gtest/gtest.h> |
| 17 | +#include <chrono> |
| 18 | +#include <memory> |
| 19 | +#include <set> |
| 20 | + |
| 21 | +#include "../../test_behavior_tree_fixture.hpp" |
| 22 | +#include "nav2_behavior_tree/plugins/decorator/goal_updated_controller.hpp" |
| 23 | + |
| 24 | +using namespace std::chrono; // NOLINT |
| 25 | +using namespace std::chrono_literals; // NOLINT |
| 26 | + |
| 27 | +class GoalUpdatedControllerTestFixture : public nav2_behavior_tree::BehaviorTreeTestFixture |
| 28 | +{ |
| 29 | +public: |
| 30 | + void SetUp() |
| 31 | + { |
| 32 | + // Setting fake goals on blackboard |
| 33 | + geometry_msgs::msg::PoseStamped goal1; |
| 34 | + goal1.header.stamp = node_->now(); |
| 35 | + std::vector<geometry_msgs::msg::PoseStamped> poses1; |
| 36 | + poses1.push_back(goal1); |
| 37 | + config_->blackboard->set("goal", goal1); |
| 38 | + config_->blackboard->set<std::vector<geometry_msgs::msg::PoseStamped>>("goals", poses1); |
| 39 | + |
| 40 | + bt_node_ = std::make_shared<nav2_behavior_tree::GoalUpdatedController>( |
| 41 | + "goal_updated_controller", *config_); |
| 42 | + dummy_node_ = std::make_shared<nav2_behavior_tree::DummyNode>(); |
| 43 | + bt_node_->setChild(dummy_node_.get()); |
| 44 | + } |
| 45 | + |
| 46 | + void TearDown() |
| 47 | + { |
| 48 | + dummy_node_.reset(); |
| 49 | + bt_node_.reset(); |
| 50 | + } |
| 51 | + |
| 52 | +protected: |
| 53 | + static std::shared_ptr<nav2_behavior_tree::GoalUpdatedController> bt_node_; |
| 54 | + static std::shared_ptr<nav2_behavior_tree::DummyNode> dummy_node_; |
| 55 | +}; |
| 56 | + |
| 57 | +std::shared_ptr<nav2_behavior_tree::GoalUpdatedController> |
| 58 | +GoalUpdatedControllerTestFixture::bt_node_ = nullptr; |
| 59 | +std::shared_ptr<nav2_behavior_tree::DummyNode> |
| 60 | +GoalUpdatedControllerTestFixture::dummy_node_ = nullptr; |
| 61 | + |
| 62 | +TEST_F(GoalUpdatedControllerTestFixture, test_behavior) |
| 63 | +{ |
| 64 | + // Creating updated fake-goals |
| 65 | + geometry_msgs::msg::PoseStamped goal2; |
| 66 | + goal2.header.stamp = node_->now(); |
| 67 | + std::vector<geometry_msgs::msg::PoseStamped> poses2; |
| 68 | + poses2.push_back(goal2); |
| 69 | + |
| 70 | + // starting in idle |
| 71 | + EXPECT_EQ(bt_node_->status(), BT::NodeStatus::IDLE); |
| 72 | + |
| 73 | + // tick for the first time, dummy node should be ticked |
| 74 | + dummy_node_->changeStatus(BT::NodeStatus::SUCCESS); |
| 75 | + EXPECT_EQ(bt_node_->executeTick(), BT::NodeStatus::SUCCESS); |
| 76 | + EXPECT_EQ(dummy_node_->status(), BT::NodeStatus::IDLE); |
| 77 | + |
| 78 | + // tick again with updated goal, dummy node should be ticked |
| 79 | + config_->blackboard->set("goal", goal2); |
| 80 | + dummy_node_->changeStatus(BT::NodeStatus::SUCCESS); |
| 81 | + EXPECT_EQ(bt_node_->executeTick(), BT::NodeStatus::SUCCESS); |
| 82 | + EXPECT_EQ(dummy_node_->status(), BT::NodeStatus::IDLE); |
| 83 | + |
| 84 | + // tick again without update, dummy node should not be ticked |
| 85 | + EXPECT_EQ(bt_node_->executeTick(), BT::NodeStatus::RUNNING); |
| 86 | + EXPECT_EQ(dummy_node_->status(), BT::NodeStatus::IDLE); |
| 87 | + |
| 88 | + // tick again with updated goals, dummy node should be ticked |
| 89 | + config_->blackboard->set<std::vector<geometry_msgs::msg::PoseStamped>>("goals", poses2); |
| 90 | + dummy_node_->changeStatus(BT::NodeStatus::SUCCESS); |
| 91 | + EXPECT_EQ(bt_node_->executeTick(), BT::NodeStatus::SUCCESS); |
| 92 | + EXPECT_EQ(dummy_node_->status(), BT::NodeStatus::IDLE); |
| 93 | +} |
| 94 | + |
| 95 | +int main(int argc, char ** argv) |
| 96 | +{ |
| 97 | + ::testing::InitGoogleTest(&argc, argv); |
| 98 | + |
| 99 | + // initialize ROS |
| 100 | + rclcpp::init(argc, argv); |
| 101 | + |
| 102 | + bool all_successful = RUN_ALL_TESTS(); |
| 103 | + |
| 104 | + // shutdown ROS |
| 105 | + rclcpp::shutdown(); |
| 106 | + |
| 107 | + return all_successful; |
| 108 | +} |
0 commit comments