|
| 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 "rclcpp/rclcpp.hpp" |
| 17 | +#include "nav2_coverage/robot_params.hpp" |
| 18 | +#include "nav2_coverage/route_generator.hpp" |
| 19 | +#include "nav2_coverage/swath_generator.hpp" |
| 20 | +#include "nav2_coverage/path_generator.hpp" |
| 21 | +#include "tf2/utils.h" |
| 22 | +#include "fields2cover/utils/random.h" |
| 23 | + |
| 24 | +// Luckily, F2C has very high test coverage so we only need to test what we touch |
| 25 | + |
| 26 | +class RosLockGuard |
| 27 | +{ |
| 28 | +public: |
| 29 | + RosLockGuard() {rclcpp::init(0, nullptr);} |
| 30 | + ~RosLockGuard() {rclcpp::shutdown();} |
| 31 | +}; |
| 32 | +RosLockGuard g_rclcpp; |
| 33 | + |
| 34 | +namespace nav2_coverage |
| 35 | +{ |
| 36 | + |
| 37 | +class PathShim : public PathGenerator |
| 38 | +{ |
| 39 | +public: |
| 40 | + template<typename NodeT> |
| 41 | + explicit PathShim(const NodeT & node, RobotParams * robot_params) |
| 42 | + : PathGenerator(node, robot_params) |
| 43 | + {} |
| 44 | + |
| 45 | + TurningBasePtr createCurveShim(const PathType & type, const PathContinuityType & c_type) |
| 46 | + { |
| 47 | + return createCurve(type, c_type); |
| 48 | + } |
| 49 | + |
| 50 | + std::string toStringShim(const PathType & type, const PathContinuityType & c_type) |
| 51 | + { |
| 52 | + return toString(type, c_type); |
| 53 | + } |
| 54 | + |
| 55 | + PathType toTypeShim(const std::string & str) |
| 56 | + { |
| 57 | + return toType(str); |
| 58 | + } |
| 59 | + |
| 60 | + PathContinuityType toContinuityTypeShim(const std::string & str) |
| 61 | + { |
| 62 | + return toContinuityType(str); |
| 63 | + } |
| 64 | +}; |
| 65 | + |
| 66 | +TEST(PathTests, TestpathUtils) |
| 67 | +{ |
| 68 | + auto node = std::make_shared<rclcpp::Node>("test_node"); |
| 69 | + RobotParams params(node); |
| 70 | + auto generator = PathShim(node, ¶ms); |
| 71 | + |
| 72 | + EXPECT_EQ(generator.toTypeShim("FAKE"), PathType::UNKNOWN); |
| 73 | + EXPECT_EQ(generator.toTypeShim("REEDS_SHEPP"), PathType::REEDS_SHEPP); |
| 74 | + EXPECT_EQ(generator.toTypeShim("reeds_shepp"), PathType::REEDS_SHEPP); |
| 75 | + EXPECT_EQ(generator.toTypeShim("DUBIN"), PathType::DUBIN); |
| 76 | + EXPECT_EQ(generator.toTypeShim("dubin"), PathType::DUBIN); |
| 77 | + |
| 78 | + EXPECT_EQ(generator.toContinuityTypeShim("FAKE"), PathContinuityType::UNKNOWN); |
| 79 | + EXPECT_EQ(generator.toContinuityTypeShim("CONTINUOUS"), PathContinuityType::CONTINUOUS); |
| 80 | + EXPECT_EQ(generator.toContinuityTypeShim("continuous"), PathContinuityType::CONTINUOUS); |
| 81 | + EXPECT_EQ(generator.toContinuityTypeShim("DISCONTINUOUS"), PathContinuityType::DISCONTINUOUS); |
| 82 | + EXPECT_EQ(generator.toContinuityTypeShim("discontinuous"), PathContinuityType::DISCONTINUOUS); |
| 83 | + |
| 84 | + EXPECT_GT(generator.toStringShim(PathType::UNKNOWN, PathContinuityType::UNKNOWN).size(), 20u); |
| 85 | + EXPECT_GT( |
| 86 | + generator.toStringShim( |
| 87 | + PathType::REEDS_SHEPP, PathContinuityType::CONTINUOUS).size(), 20u); |
| 88 | + EXPECT_GT( |
| 89 | + generator.toStringShim( |
| 90 | + PathType::DUBIN, PathContinuityType::DISCONTINUOUS).size(), 20u); |
| 91 | + |
| 92 | + EXPECT_TRUE(generator.createCurveShim(PathType::REEDS_SHEPP, PathContinuityType::CONTINUOUS)); |
| 93 | + EXPECT_TRUE(generator.createCurveShim(PathType::REEDS_SHEPP, PathContinuityType::DISCONTINUOUS)); |
| 94 | + EXPECT_TRUE(generator.createCurveShim(PathType::DUBIN, PathContinuityType::CONTINUOUS)); |
| 95 | + EXPECT_TRUE(generator.createCurveShim(PathType::DUBIN, PathContinuityType::DISCONTINUOUS)); |
| 96 | + EXPECT_FALSE(generator.createCurveShim(PathType::UNKNOWN, PathContinuityType::UNKNOWN)); |
| 97 | + EXPECT_FALSE(generator.createCurveShim(PathType::UNKNOWN, PathContinuityType::DISCONTINUOUS)); |
| 98 | + EXPECT_FALSE(generator.createCurveShim(PathType::UNKNOWN, PathContinuityType::CONTINUOUS)); |
| 99 | + EXPECT_FALSE(generator.createCurveShim(PathType::REEDS_SHEPP, PathContinuityType::UNKNOWN)); |
| 100 | + EXPECT_FALSE(generator.createCurveShim(PathType::DUBIN, PathContinuityType::UNKNOWN)); |
| 101 | + |
| 102 | + generator.setPathMode("a mode"); |
| 103 | + generator.setPathContinuityMode("another mode"); |
| 104 | + generator.setTurnPointDistance(0.1); |
| 105 | +} |
| 106 | + |
| 107 | +TEST(PathTests, TestpathGeneration) |
| 108 | +{ |
| 109 | + auto node = std::make_shared<rclcpp::Node>("test_node"); |
| 110 | + RobotParams robot_params(node); |
| 111 | + SwathGenerator swath_gen(node, &robot_params); |
| 112 | + RouteGenerator route_gen(node); |
| 113 | + PathShim generator(node, &robot_params); |
| 114 | + |
| 115 | + // Generate some toy route |
| 116 | + f2c::Random rand; |
| 117 | + auto field = rand.generateRandField(5, 1e5); |
| 118 | + nav2_complete_coverage_msgs::msg::SwathMode sw_settings; |
| 119 | + auto swaths = swath_gen.generateSwaths(field.field.getGeometry(0), sw_settings); |
| 120 | + nav2_complete_coverage_msgs::msg::RouteMode rt_settings; |
| 121 | + auto route = route_gen.generateRoute(swaths, rt_settings); |
| 122 | + |
| 123 | + // Shouldn't throw, results in valid output |
| 124 | + nav2_complete_coverage_msgs::msg::PathMode settings; |
| 125 | + auto path1 = generator.generatePath(route, settings); |
| 126 | + settings.mode = "REEDS_SHEPP"; |
| 127 | + settings.continuity_mode = "CONTINUOUS"; |
| 128 | + auto path2 = generator.generatePath(route, settings); |
| 129 | +} |
| 130 | + |
| 131 | +} // namespace nav2_coverage |
0 commit comments