YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
INavigation2DTest.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
6#ifndef INAVIGATION2DTEST_H
7#define INAVIGATION2DTEST_H
8
9#include <yarp/dev/IMap2D.h>
11#include <catch2/catch_amalgamated.hpp>
12
13using namespace yarp::dev;
14using namespace yarp::dev::Nav2D;
15using namespace yarp::sig;
16using namespace yarp::os;
17
18namespace yarp::dev::tests
19{
20 inline void test_similar_angles(INavigation2D* inav, double angle1, double angle2)
21 {
22 bool b0, b1;
23 b0 = inav->setInitialPose(Map2DLocation("map_1", 10.2, 20.1, angle1)); CHECK(b0);
25 INFO("Testing angle" + std::to_string(angle1) + " is similar to:" + std::to_string(angle2));
26 bool is_near;
27 b1 = inav->checkNearToLocation(Map2DLocation("map_1", 10.2, 20.1, angle2), is_near, 0.1, 10.0); CHECK(b1); CHECK(is_near);
28 //yInfo() << "Testing angle" << angle1 << " is similar to:" << angle2 + 5.0;
29 b1 = inav->checkNearToLocation(Map2DLocation("map_1", 10.2, 20.1, angle2 + 5.0), is_near,0.1, 10.0); CHECK(b1); CHECK(is_near);
30 //yInfo() << "Testing angle" << angle1 << " is different from:" << angle2 + 20.0;
31 b1 = inav->checkNearToLocation(Map2DLocation("map_1", 10.2, 20.1, angle2 + 20.0), is_near,0.1, 10.0); CHECK(b1); CHECK(!is_near);
32 //yInfo() << "Testing angle" << angle1 << " is similar to:" << angle2 - 5.0;
33 b1 = inav->checkNearToLocation(Map2DLocation("map_1", 10.2, 20.1, angle2 - 5.0), is_near,0.1, 10.0); CHECK(b1); CHECK(is_near);
34 //yInfo() << "Testing angle" << angle1 << " is different from:" << angle2 - 20.0;
35 b1 = inav->checkNearToLocation(Map2DLocation("map_1", 10.2, 20.1, angle2 - 20.0), is_near,0.1, 10.0); CHECK(b1); CHECK(!is_near);
36 }
37
39 {
41 Map2DLocation loc_test = Map2DLocation("map_1", 10.0, 20.0, 15);
42 Map2DLocation my_current_loc = Map2DLocation("map_1", 10.2, 20.1, 15.5);
44 Map2DArea area_test("map_1", std::vector<Map2DLocation> {Map2DLocation("map_1", -10, -10, 0),
45 Map2DLocation("map_1", -10, +10, 0),
46 Map2DLocation("map_1", +10, +10, 0),
47 Map2DLocation("map_1", +10, -10, 0)}, "this is a test area");
48 bool b0, b1;
49 b0 = imap->storeArea("area_test", area_test); CHECK(b0);
50 b0 = imap->storeLocation("loc_test", loc_test); CHECK(b0);
51
52 {
53 bool is_inside;
54 b0 = inav->setInitialPose(my_current_loc); CHECK(b0);
57 b1 = inav->checkInsideArea("area_test",is_inside); CHECK(b1); CHECK(!is_inside);
58 b1 = inav->checkInsideArea(area_test,is_inside); CHECK(b1); CHECK(!is_inside);
59 b0 = inav->setInitialPose(Map2DLocation("map_1", 0, 0, 0)); CHECK(b0);
61 b0 = inav->getCurrentPosition(loc_to_be_tested); CHECK(loc_to_be_tested == Map2DLocation("map_1", 0, 0, 0));
62 b1 = inav->checkInsideArea("area_test",is_inside); CHECK(b1); CHECK(is_inside);
63 b1 = inav->checkInsideArea(area_test,is_inside); CHECK(b1); CHECK(is_inside);
64 }
65
66 {
67 double lin_tol = 1.0; //m
68 double ang_tol = 1.0; //deg
69 b0 = inav->setInitialPose(my_current_loc); CHECK(b0);
71 bool is_near;
72 b1 = inav->checkNearToLocation("loc_test", is_near, lin_tol, ang_tol); CHECK(b1); CHECK(is_near);
73 b1 = inav->checkNearToLocation(loc_test, is_near, lin_tol, ang_tol); CHECK(b1); CHECK(is_near);
74 }
75 {
76 double lin_tol = 0.0001; //m
77 double ang_tol = 0.0001; //deg
78 b0 = inav->setInitialPose(my_current_loc); CHECK(b0);
80 bool is_near;
81 b1 = inav->checkNearToLocation("loc_test", is_near, lin_tol, ang_tol); CHECK(b1); CHECK(!is_near);
82 b1 = inav->checkNearToLocation(loc_test, is_near, lin_tol, ang_tol); CHECK(b1); CHECK(!is_near);
83 }
84 {
85 double lin_tol = 1.0; //m
87 b0 = inav->setInitialPose(my_current_loc2); CHECK(b0);
89 bool is_near;
90 b1 = inav->checkNearToLocation("loc_test", is_near, lin_tol); CHECK(b1); CHECK(is_near);
91 b1 = inav->checkNearToLocation(loc_test, is_near, lin_tol); CHECK(b1); CHECK(is_near);
92 }
93 {
94 double lin_tol = 0.1; //m
95 double ang_tol = 0.1; //deg
96 bool is_near;
97 b0 = inav->setInitialPose(my_current_loc); CHECK(b0); yarp::os::Time::delay(0.1);
98 b1 = inav->checkNearToLocation(Map2DLocation("map_1", 10.2, 20.1, 15.5), is_near, lin_tol, ang_tol); CHECK(b1);
99 b1 = inav->checkNearToLocation(Map2DLocation("map_1", 10.2, 20.1, 15.5 + 180), is_near, lin_tol, ang_tol); CHECK(b1); CHECK(!is_near);
100 b1 = inav->checkNearToLocation(Map2DLocation("map_1", 10.2, 20.1, 15.5 + 360), is_near, lin_tol, ang_tol); CHECK(b1); CHECK(is_near);
101 b1 = inav->checkNearToLocation(Map2DLocation("map_1", 10.2, 20.1, 15.5 + 720), is_near, lin_tol, ang_tol); CHECK(b1); CHECK(is_near);
102 b1 = inav->checkNearToLocation(Map2DLocation("map_1", 10.2, 20.1, 15.5 - 180), is_near, lin_tol, ang_tol); CHECK(b1); CHECK(!is_near);
103 b1 = inav->checkNearToLocation(Map2DLocation("map_1", 10.2, 20.1, 15.5 - 360), is_near, lin_tol, ang_tol); CHECK(b1); CHECK(is_near);
104 b1 = inav->checkNearToLocation(Map2DLocation("map_1", 10.2, 20.1, 15.5 - 720), is_near, lin_tol, ang_tol); CHECK(b1); CHECK(is_near);
105
106 //in the following tests, the tolerance is set to 10.0 deg
107 test_similar_angles(inav, +2.0, +2.0);
108 test_similar_angles(inav, -2.0, +2.0);
109 test_similar_angles(inav, +2.0, -2.0);
110 test_similar_angles(inav, -2.0, -2.0);
111
112 test_similar_angles(inav, +182.0, +182.0);
113 test_similar_angles(inav, -182.0, +182.0);
114 test_similar_angles(inav, +182.0, -182.0);
115 test_similar_angles(inav, -182.0, -182.0);
116
117 test_similar_angles(inav, 2.0, 358.0);
118 test_similar_angles(inav, -2.0, 358.0);
119 test_similar_angles(inav, 2.0, -358.0);
120 test_similar_angles(inav, -2.0, -358.0);
121
122 test_similar_angles(inav, +2.0, 718.0);
123 test_similar_angles(inav, -2.0, 718.0);
124 test_similar_angles(inav, +2.0, -718.0);
125 test_similar_angles(inav, -2.0, -718.0);
126 }
127 {
128 b0 = inav->setInitialPose(my_current_loc); CHECK(b0); yarp::os::Time::delay(0.1);
130 b1 = inav->getEstimatedOdometry(my_current_odom); CHECK(b1);
131 INFO("Current position is:" + my_current_loc.toString());
132 INFO("Estimated Odometry is:" + my_current_odom.toString());
133 bool bodom = fabs(my_current_loc.x - my_current_odom.odom_x) < 0.0000001 &&
134 fabs(my_current_loc.y - my_current_odom.odom_y) < 0.0000001 &&
135 fabs(my_current_loc.theta - my_current_odom.odom_theta) < 0.0000001;
136 CHECK(bodom);
137 }
138 }
139
141 {
143 bool b0, b1, b2;
144 Map2DLocation tloc("test", 1, 2, 3);
147 std::string tname("testLoc");
148 std::string gname1;
150
151 b0 = inav->storeLocation(tname, tloc); CHECK(b0);
152
153 //going to a location by absolute value
154 b1 = inav->stopNavigation(); CHECK(b1);
155 b1 = inav->getNavigationStatus(gstat1); CHECK(b1); CHECK(gstat1 == NavigationStatusEnum::navigation_status_idle);
156
157 b1 = inav->getAbsoluteLocationOfCurrentTarget(gloc1); CHECK(b1); CHECK(gloc1 == gloc_empty);
158 b1 = inav->getNameOfCurrentTarget(gname1); CHECK(b1); CHECK(gname1 == "");
159 b1 = inav->gotoTargetByAbsoluteLocation(tloc); CHECK(b1);
160 b1 = inav->getAbsoluteLocationOfCurrentTarget(gloc1); CHECK(b1); CHECK(gloc1 == tloc);
161 b1 = inav->getNameOfCurrentTarget(gname1); CHECK(b1); CHECK(gname1 == "");
162
163 //going to an existing location by name
164 b1 = inav->stopNavigation(); CHECK(b1);
165 b1 = inav->getNavigationStatus(gstat1); CHECK(b1); CHECK(gstat1 == NavigationStatusEnum::navigation_status_idle);
166
167 b1 = inav->getAbsoluteLocationOfCurrentTarget(gloc1); CHECK(b1); CHECK(gloc1 == gloc_empty);
168 b1 = inav->getNameOfCurrentTarget(gname1); CHECK(b1); CHECK(gname1 == "");
169
170 b1 = inav->gotoTargetByLocationName(tname); CHECK(b1);
171 b1 = inav->getAbsoluteLocationOfCurrentTarget(gloc1); CHECK(b1); CHECK(gloc1 == tloc);
172 b1 = inav->getNameOfCurrentTarget(gname1); CHECK(b1); CHECK(gname1 == tname);
173
174 //stop must clear navigation target name
175 b1 = inav->stopNavigation(); CHECK(b1);
176 b1 = inav->getNavigationStatus(gstat1); CHECK(b1); CHECK(gstat1 == NavigationStatusEnum::navigation_status_idle);
177 b1 = inav->getNameOfCurrentTarget(gname1); CHECK(b1); CHECK(gname1 == "");
178
179 //trying to goto to a non-existing location by name, target name must be not set
180 b1 = inav->gotoTargetByLocationName("non-existing-loc"); CHECK(b1 == false);
181 b1 = inav->getAbsoluteLocationOfCurrentTarget(gloc1); CHECK(b1); CHECK(gloc1 == gloc_empty);
182 b1 = inav->getNameOfCurrentTarget(gname1); CHECK(b1); CHECK(gname1 == "");
183
184 //mix of last two tests. A non existing location must clear a previously set target name
185 b1 = inav->gotoTargetByLocationName(tname); CHECK(b1);
186 b1 = inav->getAbsoluteLocationOfCurrentTarget(gloc1); CHECK(b1); CHECK(gloc1 == tloc);
187 b1 = inav->gotoTargetByLocationName("non-existing-loc"); CHECK(b1 == false);
188 b1 = inav->getAbsoluteLocationOfCurrentTarget(gloc1); CHECK(b1); CHECK(gloc1 == gloc_empty);
189 }
190
192 {
194 bool b;
195 Map2DLocation loc("test", 1, 2, 3);
197
198 b = inav_ctl->getNavigationStatus(status); CHECK(b);
200
201 b = inav_trgt->gotoTargetByAbsoluteLocation(loc); CHECK(b);
202 b = inav_ctl->getNavigationStatus(status); CHECK(b);
204
205 size_t count=0;
206 do
207 {
208 b = inav_ctl->getNavigationStatus(status); CHECK(b);
210 {
211 break;
212 }
214 count++;
215 if (count>200) {CHECK(0); break; }
216 }
217 while(1);
218
219 count = 0;
220 do
221 {
222 b = inav_ctl->getNavigationStatus(status); CHECK(b);
224 {
225 break;
226 }
228 count++;
229 if (count > 200) { CHECK(0); break; }
230 } while (1);
231
232 //test complete
233 }
234}
235
236#endif
IMap2D Interface.
Definition IMap2D.h:30
An interface to control the navigation of a mobile robot in a 2D environment.
A mini-server for performing network communication in the background.
void exec_iNav2D_test_1(INavigation2D *inav, IMap2D *imap)
void exec_iNav2D_test_3(INavigation2DTargetActions *inav_trgt, INavigation2DControlActions *inav_ctl)
void exec_iNav2D_test_2(INavigation2D *inav, IMap2D *imap)
void test_similar_angles(INavigation2D *inav, double angle1, double angle2)
For streams capable of holding different kinds of content, check what they actually have.
void delay(double seconds)
Wait for a certain number of seconds.
Definition Time.cpp:111
An interface to the operating system, including Port based communication.
std::string INFO
Definition ymanager.cpp:51