|
| 1 | +// Copyright (c) 2024 Jatin Patil |
| 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 | + |
| 17 | +#include <memory> |
| 18 | +#include <chrono> |
| 19 | + |
| 20 | +#include <rclcpp/rclcpp.hpp> |
| 21 | +#include "nav2_msgs/srv/get_cost.hpp" |
| 22 | +#include "nav2_costmap_2d/costmap_2d_ros.hpp" |
| 23 | + |
| 24 | +class RclCppFixture |
| 25 | +{ |
| 26 | +public: |
| 27 | + RclCppFixture() {rclcpp::init(0, nullptr);} |
| 28 | + ~RclCppFixture() {rclcpp::shutdown();} |
| 29 | +}; |
| 30 | +RclCppFixture g_rclcppfixture; |
| 31 | + |
| 32 | +using namespace std::chrono_literals; |
| 33 | + |
| 34 | +class GetCostServiceTest : public ::testing::Test |
| 35 | +{ |
| 36 | +protected: |
| 37 | + void SetUp() override |
| 38 | + { |
| 39 | + costmap_ = std::make_shared<nav2_costmap_2d::Costmap2DROS>("costmap"); |
| 40 | + client_ = costmap_->create_client<nav2_msgs::srv::GetCost>( |
| 41 | + "/costmap/get_cost_costmap"); |
| 42 | + costmap_->on_configure(rclcpp_lifecycle::State()); |
| 43 | + ASSERT_TRUE(client_->wait_for_service(10s)); |
| 44 | + } |
| 45 | + |
| 46 | + std::shared_ptr<nav2_costmap_2d::Costmap2DROS> costmap_; |
| 47 | + rclcpp::Client<nav2_msgs::srv::GetCost>::SharedPtr client_; |
| 48 | +}; |
| 49 | + |
| 50 | +TEST_F(GetCostServiceTest, TestWithoutFootprint) |
| 51 | +{ |
| 52 | + auto request = std::make_shared<nav2_msgs::srv::GetCost::Request>(); |
| 53 | + request->x = 0.5; |
| 54 | + request->y = 1.0; |
| 55 | + request->use_footprint = false; |
| 56 | + |
| 57 | + auto result_future = client_->async_send_request(request); |
| 58 | + if (rclcpp::spin_until_future_complete( |
| 59 | + costmap_, |
| 60 | + result_future) == rclcpp::FutureReturnCode::SUCCESS) |
| 61 | + { |
| 62 | + auto response = result_future.get(); |
| 63 | + EXPECT_GE(response->cost, 0.0) << "Cost is less than 0"; |
| 64 | + EXPECT_LE(response->cost, 255.0) << "Cost is greater than 255"; |
| 65 | + } else { |
| 66 | + FAIL() << "Failed to call service"; |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +TEST_F(GetCostServiceTest, TestWithFootprint) |
| 71 | +{ |
| 72 | + auto request = std::make_shared<nav2_msgs::srv::GetCost::Request>(); |
| 73 | + request->x = 1.0; |
| 74 | + request->y = 1.0; |
| 75 | + request->theta = 0.5; |
| 76 | + request->use_footprint = true; |
| 77 | + |
| 78 | + auto result_future = client_->async_send_request(request); |
| 79 | + if (rclcpp::spin_until_future_complete( |
| 80 | + costmap_, |
| 81 | + result_future) == rclcpp::FutureReturnCode::SUCCESS) |
| 82 | + { |
| 83 | + auto response = result_future.get(); |
| 84 | + EXPECT_GE(response->cost, 0.0) << "Cost is less than 0"; |
| 85 | + EXPECT_LE(response->cost, 255.0) << "Cost is greater than 255"; |
| 86 | + } else { |
| 87 | + FAIL() << "Failed to call service"; |
| 88 | + } |
| 89 | +} |
0 commit comments