Skip to content

Commit 20bb881

Browse files
committed
complete unit tests
1 parent befaccd commit 20bb881

File tree

7 files changed

+377
-15
lines changed

7 files changed

+377
-15
lines changed

nav2_coverage/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ install(DIRECTORY include/
8585
DESTINATION include/
8686
)
8787

88+
install(FILES test/test_field.xml
89+
DESTINATION share/${PROJECT_NAME}/
90+
)
91+
8892
if(BUILD_TESTING)
8993
find_package(ament_lint_auto REQUIRED)
9094
find_package(ament_cmake_gtest REQUIRED)

nav2_coverage/README.md

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,9 @@ Walk through
6464
- RQT
6565

6666
Future
67-
- Unit testing to make sure my visual inspection is correct
68-
- Transform GPS to UTM zone properly + back to GPS on return
67+
- Transform GPS to UTM zone properly + back to GPS on return (https://github.com/Fields2Cover/Fields2Cover/pull/95/files)
68+
- Use coverage exceptions
69+
- transform back into proper frame
6970

7071
- BT nodes to interact with in in the Nav2 autonomy framework
7172
- A couple of utilities for the BT nodes to iterate through the swath-turn combos
@@ -79,12 +80,3 @@ Q
7980
- Other F2C add on features you care about? --> designed to expand or contribute back to F2C
8081
- headland pass!
8182
- inner boundary not to go through (...) can wait, firefly might do.
82-
83-
84-
Leader follower 2-3 months out to kick off
85-
a bunch of other stuff --> projects interest me. open nav hire to fill!
86-
87-
88-
89-
TODO
90-
- Use coverage exception

nav2_coverage/test/CMakeLists.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,25 @@ ament_target_dependencies(test_route
6363
target_link_libraries(test_route
6464
${library_name}
6565
)
66+
67+
# Test path
68+
ament_add_gtest(test_path
69+
test_path.cpp
70+
)
71+
ament_target_dependencies(test_path
72+
${dependencies}
73+
)
74+
target_link_libraries(test_path
75+
${library_name}
76+
)
77+
78+
# Test server
79+
ament_add_gtest(test_server
80+
test_server.cpp
81+
)
82+
ament_target_dependencies(test_server
83+
${dependencies}
84+
)
85+
target_link_libraries(test_server
86+
${library_name}
87+
)

nav2_coverage/test/test_field.xml

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<GAOS_parcel xmlns:gml="http://www.opengis.net/gml" id="14d36f66-eac9-4e2b-b835-f6bddd925ae2">
2+
<Field area="17.2581" date="2018-04-05" id="boundary" lineage="Unknown">
3+
<geometry>
4+
<gml:Polygon srsName="EPSG:4258">
5+
<gml:outerBoundaryIs>
6+
<gml:LinearRing>
7+
<gml:coordinates>4.26199990317851,51.7859704975047 4.26203858931428,51.7860392024232 4.26234728296244,51.7867682098064 4.26283009086539,51.7878798462305 4.26321532471923,51.7887809885397 4.26344944239078,51.7893481817958 4.25601634525234,51.7906387243889 4.25603742148301,51.7905786368844 4.25662083047068,51.7889909449083 4.25732681137421,51.7870536505356 4.25749399420598,51.7866017400346 4.26195105582634,51.7858278333044 4.26199990317851,51.7859704975047</gml:coordinates>
8+
</gml:LinearRing>
9+
</gml:outerBoundaryIs>
10+
</gml:Polygon>
11+
</geometry>
12+
</Field>
13+
<Edge date="2018-04-05" id="1" include="1" maxstripwidth="0.50" minstripwidth="0.25" preference="1" striptype="2">
14+
<geometry>
15+
<gml:LineString srsName="EPSG:4258">
16+
<gml:coordinates>4.26203858931428,51.7860392024232 4.26234728296244,51.7867682098064 4.26283009086539,51.7878798462305 4.26321532471923,51.7887809885397 4.26344944239078,51.7893481817958</gml:coordinates>
17+
</gml:LineString>
18+
</geometry>
19+
</Edge>
20+
<Edge date="2018-04-05" id="2" include="1" maxstripwidth="0.50" minstripwidth="0.25" preference="1" striptype="2">
21+
<geometry>
22+
<gml:LineString srsName="EPSG:4258">
23+
<gml:coordinates>4.26344944239078,51.7893481817958 4.25601634525234,51.7906387243889</gml:coordinates>
24+
</gml:LineString>
25+
</geometry>
26+
</Edge>
27+
<Edge date="2018-04-05" id="3" include="0" maxstripwidth="0.0" minstripwidth="0.0" preference="0" striptype="0">
28+
<geometry>
29+
<gml:LineString srsName="EPSG:4258">
30+
<gml:coordinates>4.25601634525234,51.7906387243889 4.25603742148301,51.7905786368844</gml:coordinates>
31+
</gml:LineString>
32+
</geometry>
33+
</Edge>
34+
<Edge date="2018-04-05" id="4" include="1" maxstripwidth="0.50" minstripwidth="0.25" preference="1" striptype="2">
35+
<geometry>
36+
<gml:LineString srsName="EPSG:4258">
37+
<gml:coordinates>4.25603742148301,51.7905786368844 4.25662083047068,51.7889909449083 4.25732681137421,51.7870536505356 4.25749399420598,51.7866017400346</gml:coordinates>
38+
</gml:LineString>
39+
</geometry>
40+
</Edge>
41+
<Edge date="2018-04-05" id="5" include="1" maxstripwidth="0.50" minstripwidth="0.25" preference="1" striptype="2">
42+
<geometry>
43+
<gml:LineString srsName="EPSG:4258">
44+
<gml:coordinates>4.25749399420598,51.7866017400346 4.26195105582634,51.7858278333044</gml:coordinates>
45+
</gml:LineString>
46+
</geometry>
47+
</Edge>
48+
<Edge date="2018-04-05" id="6" include="1" maxstripwidth="0.50" minstripwidth="0.25" preference="1" striptype="2">
49+
<geometry>
50+
<gml:LineString srsName="EPSG:4258">
51+
<gml:coordinates>4.26195105582634,51.7858278333044 4.26199990317851,51.7859704975047 4.26203858931428,51.7860392024232</gml:coordinates>
52+
</gml:LineString>
53+
</geometry>
54+
</Edge>
55+
</GAOS_parcel>

nav2_coverage/test/test_path.cpp

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
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, &params);
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

nav2_coverage/test/test_route.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,16 +86,16 @@ TEST(RouteTests, TestrouteUtils)
8686

8787
generator.setMode("a mode");
8888
generator.setSpiralN(10);
89-
generator.setCustomOrder(std::vector<long int>{});
89+
generator.setCustomOrder(std::vector<long int>{}); // NOLINT
9090
}
9191

9292
TEST(RouteTests, TestrouteGeneration)
9393
{
9494
auto node = std::make_shared<rclcpp::Node>("test_node");
9595
RobotParams robot_params(node);
96-
auto swath_gen = SwathGenerator(node, &robot_params);
96+
SwathGenerator swath_gen(node, &robot_params);
9797
nav2_complete_coverage_msgs::msg::SwathMode sw_settings;
98-
auto generator = RouteShim(node);
98+
RouteShim generator(node);
9999

100100
// Generate some toy field
101101
f2c::Random rand;
@@ -109,8 +109,10 @@ TEST(RouteTests, TestrouteGeneration)
109109
auto route2 = generator.generateRoute(swaths, settings);
110110
settings.mode = "SPIRAL";
111111
auto route3 = generator.generateRoute(swaths, settings);
112+
113+
// Throws since custom order is set to emptry set
112114
settings.mode = "CUSTOM";
113-
auto route4 = generator.generateRoute(swaths, settings);
115+
EXPECT_THROW(generator.generateRoute(swaths, settings), std::length_error);
114116
}
115117

116118
} // namespace nav2_coverage

0 commit comments

Comments
 (0)