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 b1 = inav->checkNearToLocation(Map2DLocation("map_1", 10.2, 20.1, angle2), 0.1, 10.0); CHECK(b1);
27 //yInfo() << "Testing angle" << angle1 << " is similar to:" << angle2 + 5.0;
28 b1 = inav->checkNearToLocation(Map2DLocation("map_1", 10.2, 20.1, angle2 + 5.0), 0.1, 10.0); CHECK(b1);
29 //yInfo() << "Testing angle" << angle1 << " is different from:" << angle2 + 20.0;
30 b1 = inav->checkNearToLocation(Map2DLocation("map_1", 10.2, 20.1, angle2 + 20.0), 0.1, 10.0); CHECK(!b1);
31 //yInfo() << "Testing angle" << angle1 << " is similar to:" << angle2 - 5.0;
32 b1 = inav->checkNearToLocation(Map2DLocation("map_1", 10.2, 20.1, angle2 - 5.0), 0.1, 10.0); CHECK(b1);
33 //yInfo() << "Testing angle" << angle1 << " is different from:" << angle2 - 20.0;
34 b1 = inav->checkNearToLocation(Map2DLocation("map_1", 10.2, 20.1, angle2 - 20.0), 0.1, 10.0); CHECK(!b1);
35 }
36
38 {
40 Map2DLocation loc_test = Map2DLocation("map_1", 10.0, 20.0, 15);
41 Map2DLocation my_current_loc = Map2DLocation("map_1", 10.2, 20.1, 15.5);
43 Map2DArea area_test("map_1", std::vector<Map2DLocation> {Map2DLocation("map_1", -10, -10, 0),
44 Map2DLocation("map_1", -10, +10, 0),
45 Map2DLocation("map_1", +10, +10, 0),
46 Map2DLocation("map_1", +10, -10, 0)}, "this is a test area");
47 bool b0, b1;
48 b0 = imap->storeArea("area_test", area_test); CHECK(b0);
49 b0 = imap->storeLocation("loc_test", loc_test); CHECK(b0);
50
51 {
52 b0 = inav->setInitialPose(my_current_loc); CHECK(b0);
55 b1 = inav->checkInsideArea("area_test"); CHECK(b1 == false);
56 b1 = inav->checkInsideArea(area_test); CHECK(b1 == false);
57 b0 = inav->setInitialPose(Map2DLocation("map_1", 0, 0, 0)); CHECK(b0);
59 b0 = inav->getCurrentPosition(loc_to_be_tested); CHECK(loc_to_be_tested == Map2DLocation("map_1", 0, 0, 0));
60 b1 = inav->checkInsideArea("area_test"); CHECK(b1);
61 b1 = inav->checkInsideArea(area_test); CHECK(b1);
62 }
63
64 {
65 double lin_tol = 1.0; //m
66 double ang_tol = 1.0; //deg
67 b0 = inav->setInitialPose(my_current_loc); CHECK(b0);
69 b1 = inav->checkNearToLocation("loc_test", lin_tol, ang_tol); CHECK(b1);
70 b1 = inav->checkNearToLocation(loc_test, lin_tol, ang_tol); CHECK(b1);
71 }
72 {
73 double lin_tol = 0.0001; //m
74 double ang_tol = 0.0001; //deg
75 b0 = inav->setInitialPose(my_current_loc); CHECK(b0);
77 b1 = inav->checkNearToLocation("loc_test", lin_tol, ang_tol); CHECK(b1 == false);
78 b1 = inav->checkNearToLocation(loc_test, lin_tol, ang_tol); CHECK(b1 == false);
79 }
80 {
81 double lin_tol = 1.0; //m
83 b0 = inav->setInitialPose(my_current_loc2); CHECK(b0);
85 b1 = inav->checkNearToLocation("loc_test", lin_tol); CHECK(b1);
86 b1 = inav->checkNearToLocation(loc_test, lin_tol); CHECK(b1);
87 }
88 {
89 double lin_tol = 0.1; //m
90 double ang_tol = 0.1; //deg
91 b0 = inav->setInitialPose(my_current_loc); CHECK(b0); yarp::os::Time::delay(0.1);
92 b1 = inav->checkNearToLocation(Map2DLocation("map_1", 10.2, 20.1, 15.5), lin_tol, ang_tol); CHECK(b1);
93 b1 = inav->checkNearToLocation(Map2DLocation("map_1", 10.2, 20.1, 15.5 + 180), lin_tol, ang_tol); CHECK(b1 == false);
94 b1 = inav->checkNearToLocation(Map2DLocation("map_1", 10.2, 20.1, 15.5 + 360), lin_tol, ang_tol); CHECK(b1);
95 b1 = inav->checkNearToLocation(Map2DLocation("map_1", 10.2, 20.1, 15.5 + 720), lin_tol, ang_tol); CHECK(b1);
96 b1 = inav->checkNearToLocation(Map2DLocation("map_1", 10.2, 20.1, 15.5 - 180), lin_tol, ang_tol); CHECK(b1 == false);
97 b1 = inav->checkNearToLocation(Map2DLocation("map_1", 10.2, 20.1, 15.5 - 360), lin_tol, ang_tol); CHECK(b1);
98 b1 = inav->checkNearToLocation(Map2DLocation("map_1", 10.2, 20.1, 15.5 - 720), lin_tol, ang_tol); CHECK(b1);
99
100 //in the following tests, the tolerance is set to 10.0 deg
101 test_similar_angles(inav, +2.0, +2.0);
102 test_similar_angles(inav, -2.0, +2.0);
103 test_similar_angles(inav, +2.0, -2.0);
104 test_similar_angles(inav, -2.0, -2.0);
105
106 test_similar_angles(inav, +182.0, +182.0);
107 test_similar_angles(inav, -182.0, +182.0);
108 test_similar_angles(inav, +182.0, -182.0);
109 test_similar_angles(inav, -182.0, -182.0);
110
111 test_similar_angles(inav, 2.0, 358.0);
112 test_similar_angles(inav, -2.0, 358.0);
113 test_similar_angles(inav, 2.0, -358.0);
114 test_similar_angles(inav, -2.0, -358.0);
115
116 test_similar_angles(inav, +2.0, 718.0);
117 test_similar_angles(inav, -2.0, 718.0);
118 test_similar_angles(inav, +2.0, -718.0);
119 test_similar_angles(inav, -2.0, -718.0);
120 }
121 {
122 b0 = inav->setInitialPose(my_current_loc); CHECK(b0); yarp::os::Time::delay(0.1);
124 b1 = inav->getEstimatedOdometry(my_current_odom); CHECK(b1);
125 INFO("Current position is:" + my_current_loc.toString());
126 INFO("Estimated Odometry is:" + my_current_odom.toString());
127 bool bodom = fabs(my_current_loc.x - my_current_odom.odom_x) < 0.0000001 &&
128 fabs(my_current_loc.y - my_current_odom.odom_y) < 0.0000001 &&
129 fabs(my_current_loc.theta - my_current_odom.odom_theta) < 0.0000001;
130 CHECK(bodom);
131 }
132 }
133
135 {
137 bool b0, b1, b2;
138 Map2DLocation tloc("test", 1, 2, 3);
141 std::string tname("testLoc");
142 std::string gname1;
144
145 b0 = inav->storeLocation(tname, tloc); CHECK(b0);
146
147 //going to a location by absolute value
148 b1 = inav->stopNavigation(); CHECK(b1);
149 b1 = inav->getNavigationStatus(gstat1); CHECK(b1); CHECK(gstat1 == NavigationStatusEnum::navigation_status_idle);
150
151 b1 = inav->getAbsoluteLocationOfCurrentTarget(gloc1); CHECK(b1); CHECK(gloc1 == gloc_empty);
152 b1 = inav->getNameOfCurrentTarget(gname1); CHECK(b1); CHECK(gname1 == "");
153 b1 = inav->gotoTargetByAbsoluteLocation(tloc); CHECK(b1);
154 b1 = inav->getAbsoluteLocationOfCurrentTarget(gloc1); CHECK(b1); CHECK(gloc1 == tloc);
155 b1 = inav->getNameOfCurrentTarget(gname1); CHECK(b1); CHECK(gname1 == "");
156
157 //going to an existing location by name
158 b1 = inav->stopNavigation(); CHECK(b1);
159 b1 = inav->getNavigationStatus(gstat1); CHECK(b1); CHECK(gstat1 == NavigationStatusEnum::navigation_status_idle);
160
161 b1 = inav->getAbsoluteLocationOfCurrentTarget(gloc1); CHECK(b1); CHECK(gloc1 == gloc_empty);
162 b1 = inav->getNameOfCurrentTarget(gname1); CHECK(b1); CHECK(gname1 == "");
163
164 b1 = inav->gotoTargetByLocationName(tname); CHECK(b1);
165 b1 = inav->getAbsoluteLocationOfCurrentTarget(gloc1); CHECK(b1); CHECK(gloc1 == tloc);
166 b1 = inav->getNameOfCurrentTarget(gname1); CHECK(b1); CHECK(gname1 == tname);
167
168 //stop must clear navigation target name
169 b1 = inav->stopNavigation(); CHECK(b1);
170 b1 = inav->getNavigationStatus(gstat1); CHECK(b1); CHECK(gstat1 == NavigationStatusEnum::navigation_status_idle);
171 b1 = inav->getNameOfCurrentTarget(gname1); CHECK(b1); CHECK(gname1 == "");
172
173 //trying to goto to a non-existing location by name, target name must be not set
174 b1 = inav->gotoTargetByLocationName("non-existing-loc"); CHECK(b1 == false);
175 b1 = inav->getAbsoluteLocationOfCurrentTarget(gloc1); CHECK(b1); CHECK(gloc1 == gloc_empty);
176 b1 = inav->getNameOfCurrentTarget(gname1); CHECK(b1); CHECK(gname1 == "");
177
178 //mix of last two tests. A non existing location must clear a previously set target name
179 b1 = inav->gotoTargetByLocationName(tname); CHECK(b1);
180 b1 = inav->getAbsoluteLocationOfCurrentTarget(gloc1); CHECK(b1); CHECK(gloc1 == tloc);
181 b1 = inav->gotoTargetByLocationName("non-existing-loc"); CHECK(b1 == false);
182 b1 = inav->getAbsoluteLocationOfCurrentTarget(gloc1); CHECK(b1); CHECK(gloc1 == gloc_empty);
183 }
184
186 {
188 bool b;
189 Map2DLocation loc("test", 1, 2, 3);
191
192 b = inav_ctl->getNavigationStatus(status); CHECK(b);
194
195 b = inav_trgt->gotoTargetByAbsoluteLocation(loc); CHECK(b);
196 b = inav_ctl->getNavigationStatus(status); CHECK(b);
198
199 size_t count=0;
200 do
201 {
202 b = inav_ctl->getNavigationStatus(status); CHECK(b);
204 {
205 break;
206 }
208 count++;
209 if (count>200) {CHECK(0); break; }
210 }
211 while(1);
212
213 count = 0;
214 do
215 {
216 b = inav_ctl->getNavigationStatus(status); CHECK(b);
218 {
219 break;
220 }
222 count++;
223 if (count > 200) { CHECK(0); break; }
224 } while (1);
225
226 //test complete
227 }
228}
229
230#endif
IMap2D Interface.
Definition IMap2D.h:29
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.
Definition jointData.cpp:13
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.