|
| 1 | +// Copyright (c) 2018 Intel Corporation |
| 2 | +// Copyright (c) 2021 Samsung Research America |
| 3 | +// Copyright (c) 2024 Marc Morcos |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +// you may not use this file except in compliance with the License. |
| 7 | +// You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, software |
| 12 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +// See the License for the specific language governing permissions and |
| 15 | +// limitations under the License. |
| 16 | + |
| 17 | +#include <gtest/gtest.h> |
| 18 | +#include <memory> |
| 19 | +#include <set> |
| 20 | +#include <string> |
| 21 | +#include <vector> |
| 22 | + |
| 23 | +#include "nav_msgs/msg/path.hpp" |
| 24 | +#include "geometry_msgs/msg/pose_stamped.hpp" |
| 25 | + |
| 26 | +#include "behaviortree_cpp/bt_factory.h" |
| 27 | + |
| 28 | +#include "utils/test_action_server.hpp" |
| 29 | +#include "nav2_behavior_tree/plugins/action/get_pose_from_path_action.hpp" |
| 30 | +#include "utils/test_behavior_tree_fixture.hpp" |
| 31 | + |
| 32 | +class GetPoseFromPathTestFixture : public ::testing::Test |
| 33 | +{ |
| 34 | +public: |
| 35 | + static void SetUpTestCase() |
| 36 | + { |
| 37 | + node_ = std::make_shared<rclcpp::Node>("get_pose_from_path_action_test_fixture"); |
| 38 | + factory_ = std::make_shared<BT::BehaviorTreeFactory>(); |
| 39 | + |
| 40 | + config_ = new BT::NodeConfiguration(); |
| 41 | + |
| 42 | + // Create the blackboard that will be shared by all of the nodes in the tree |
| 43 | + config_->blackboard = BT::Blackboard::create(); |
| 44 | + // Put items on the blackboard |
| 45 | + config_->blackboard->set<rclcpp::Node::SharedPtr>( |
| 46 | + "node", |
| 47 | + node_); |
| 48 | + |
| 49 | + BT::NodeBuilder builder = |
| 50 | + [](const std::string & name, const BT::NodeConfiguration & config) |
| 51 | + { |
| 52 | + return std::make_unique<nav2_behavior_tree::GetPoseFromPath>( |
| 53 | + name, config); |
| 54 | + }; |
| 55 | + |
| 56 | + factory_->registerBuilder<nav2_behavior_tree::GetPoseFromPath>( |
| 57 | + "GetPoseFromPath", builder); |
| 58 | + } |
| 59 | + |
| 60 | + static void TearDownTestCase() |
| 61 | + { |
| 62 | + delete config_; |
| 63 | + config_ = nullptr; |
| 64 | + node_.reset(); |
| 65 | + factory_.reset(); |
| 66 | + } |
| 67 | + |
| 68 | + void TearDown() override |
| 69 | + { |
| 70 | + tree_.reset(); |
| 71 | + } |
| 72 | + |
| 73 | +protected: |
| 74 | + static rclcpp::Node::SharedPtr node_; |
| 75 | + static BT::NodeConfiguration * config_; |
| 76 | + static std::shared_ptr<BT::BehaviorTreeFactory> factory_; |
| 77 | + static std::shared_ptr<BT::Tree> tree_; |
| 78 | +}; |
| 79 | + |
| 80 | +rclcpp::Node::SharedPtr GetPoseFromPathTestFixture::node_ = nullptr; |
| 81 | +BT::NodeConfiguration * GetPoseFromPathTestFixture::config_ = nullptr; |
| 82 | +std::shared_ptr<BT::BehaviorTreeFactory> GetPoseFromPathTestFixture::factory_ = nullptr; |
| 83 | +std::shared_ptr<BT::Tree> GetPoseFromPathTestFixture::tree_ = nullptr; |
| 84 | + |
| 85 | +TEST_F(GetPoseFromPathTestFixture, test_tick) |
| 86 | +{ |
| 87 | + // create tree |
| 88 | + std::string xml_txt = |
| 89 | + R"( |
| 90 | + <root BTCPP_format="4"> |
| 91 | + <BehaviorTree ID="MainTree"> |
| 92 | + <GetPoseFromPath path="{path}" pose="{pose}" index="{index}" /> |
| 93 | + </BehaviorTree> |
| 94 | + </root>)"; |
| 95 | + |
| 96 | + tree_ = std::make_shared<BT::Tree>(factory_->createTreeFromText(xml_txt, config_->blackboard)); |
| 97 | + |
| 98 | + // create new path and set it on blackboard |
| 99 | + nav_msgs::msg::Path path; |
| 100 | + std::vector<geometry_msgs::msg::PoseStamped> goals; |
| 101 | + goals.resize(2); |
| 102 | + goals[0].pose.position.x = 1.0; |
| 103 | + goals[1].pose.position.x = 2.0; |
| 104 | + path.poses = goals; |
| 105 | + path.header.frame_id = "test_frame_1"; |
| 106 | + config_->blackboard->set("path", path); |
| 107 | + |
| 108 | + config_->blackboard->set("index", 0); |
| 109 | + |
| 110 | + // tick until node succeeds |
| 111 | + while (tree_->rootNode()->status() != BT::NodeStatus::SUCCESS) { |
| 112 | + tree_->rootNode()->executeTick(); |
| 113 | + } |
| 114 | + |
| 115 | + // the goal should have reached our server |
| 116 | + EXPECT_EQ(tree_->rootNode()->status(), BT::NodeStatus::SUCCESS); |
| 117 | + |
| 118 | + // check if returned pose is correct |
| 119 | + geometry_msgs::msg::PoseStamped pose; |
| 120 | + EXPECT_TRUE(config_->blackboard->get<geometry_msgs::msg::PoseStamped>("pose", pose)); |
| 121 | + EXPECT_EQ(pose.header.frame_id, "test_frame_1"); |
| 122 | + EXPECT_EQ(pose.pose.position.x, 1.0); |
| 123 | + |
| 124 | + // halt node so another goal can be sent |
| 125 | + tree_->haltTree(); |
| 126 | + EXPECT_EQ(tree_->rootNode()->status(), BT::NodeStatus::IDLE); |
| 127 | + |
| 128 | + // try last element |
| 129 | + config_->blackboard->set("index", -1); |
| 130 | + |
| 131 | + while (tree_->rootNode()->status() != BT::NodeStatus::SUCCESS) { |
| 132 | + tree_->rootNode()->executeTick(); |
| 133 | + } |
| 134 | + |
| 135 | + EXPECT_EQ(tree_->rootNode()->status(), BT::NodeStatus::SUCCESS); |
| 136 | + |
| 137 | + // check if returned pose is correct |
| 138 | + EXPECT_TRUE(config_->blackboard->get<geometry_msgs::msg::PoseStamped>("pose", pose)); |
| 139 | + EXPECT_EQ(pose.header.frame_id, "test_frame_1"); |
| 140 | + EXPECT_EQ(pose.pose.position.x, 2.0); |
| 141 | +} |
| 142 | + |
| 143 | +int main(int argc, char ** argv) |
| 144 | +{ |
| 145 | + ::testing::InitGoogleTest(&argc, argv); |
| 146 | + |
| 147 | + // initialize ROS |
| 148 | + rclcpp::init(argc, argv); |
| 149 | + |
| 150 | + int all_successful = RUN_ALL_TESTS(); |
| 151 | + |
| 152 | + // shutdown ROS |
| 153 | + rclcpp::shutdown(); |
| 154 | + |
| 155 | + return all_successful; |
| 156 | +} |
0 commit comments