Debian Codegraph @

test_suite/unit_tests/AI/test_AIFlightPlan.cxx

raw symbols SCIP
1 /*
2 * SPDX-FileName: test_AIFlightPlan.cxx
3 * SPDX-FileCopyrightText: Copyright (C) 2020 James Turner
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7 #include "test_AIFlightPlan.hxx"
8
9 #include <cstring>
10 #include <memory>
11
12 #include "config.h"
13 #include "test_suite/FGTestApi/NavDataCache.hxx"
14 #include "test_suite/FGTestApi/TestDataLogger.hxx"
15 #include "test_suite/FGTestApi/TestPilot.hxx"
16 #include "test_suite/FGTestApi/testGlobals.hxx"
17 #include "test_suite/FGTestApi/testStringUtils.hxx"
18
19 #include <simgear/timing/sg_time.hxx>
20
21 #include <AIModel/AIAircraft.hxx>
22 #include <AIModel/AIFlightPlan.hxx>
23 #include <AIModel/AIManager.hxx>
24
25 #include <ATC/atc_mgr.hxx>
26
27 #include <Traffic/SchedFlight.hxx>
28 #include <Traffic/Schedule.hxx>
29
30 #include <Airports/airport.hxx>
31 #include <Airports/airportdynamicsmanager.hxx>
32 #include <Main/fg_props.hxx>
33 #include <Main/globals.hxx>
34 #include <Navaids/NavDataCache.hxx>
35 #include <Navaids/navrecord.hxx>
36
37 using std::string;
38 using namespace flightgear;
39
40 /////////////////////////////////////////////////////////////////////////////
41
42 // Set up function for each test.
43 void AIFlightPlanTests::setUp()
44 {
45 FGTestApi::setUp::initTestGlobals("AI");
46 FGTestApi::setUp::initNavDataCache();
47
48 globals->get_subsystem_mgr()->add<FGATCManager>();
49 globals->get_subsystem_mgr()->add<FGAIManager>();
50 globals->get_subsystem_mgr()->add<flightgear::AirportDynamicsManager>();
51
52 auto props = globals->get_props();
53 props->setBoolValue("sim/ai/enabled", true);
54
55 globals->get_subsystem_mgr()->bind();
56 globals->get_subsystem_mgr()->init();
57 globals->get_subsystem_mgr()->postinit();
58 }
59
60 // Clean up after each test.
61 void AIFlightPlanTests::tearDown()
62 {
63 FGTestApi::tearDown::shutdownTestGlobals();
64 }
65
66 void AIFlightPlanTests::testAIFlightPlan()
67 {
68 std::unique_ptr<FGAIFlightPlan> aiFP(new FGAIFlightPlan);
69 aiFP->setName("Bob");
70 aiFP->setRunway("24");
71
72 CPPUNIT_ASSERT_EQUAL(string{"Bob"}, aiFP->getName());
73 CPPUNIT_ASSERT_EQUAL(string{"24"}, aiFP->getRunway());
74
75 CPPUNIT_ASSERT_EQUAL(0, aiFP->getNrOfWayPoints());
76 CPPUNIT_ASSERT_EQUAL(static_cast<FGAIWaypoint*>(nullptr), aiFP->getPreviousWaypoint());
77 CPPUNIT_ASSERT_EQUAL(static_cast<FGAIWaypoint*>(nullptr), aiFP->getCurrentWaypoint());
78 CPPUNIT_ASSERT_EQUAL(static_cast<FGAIWaypoint*>(nullptr), aiFP->getNextWaypoint());
79 CPPUNIT_ASSERT_EQUAL(0, aiFP->getLeg());
80
81 FGPositioned::TypeFilter ty(FGPositioned::VOR);
82 auto cache = flightgear::NavDataCache::instance();
83 auto shannonVOR = cache->findClosestWithIdent("SHA", SGGeod::fromDeg(-8, 52), &ty);
84 CPPUNIT_ASSERT_EQUAL(string{"SHANNON VOR-DME"}, shannonVOR->name());
85
86 auto wp1 = new FGAIWaypoint;
87 wp1->setPos(shannonVOR->geod());
88 wp1->setName("testWp_0");
89 wp1->setOn_ground(true);
90 wp1->setGear_down(true);
91 wp1->setSpeed(100);
92
93 auto wp2 = new FGAIWaypoint;
94 const auto g1 = SGGeodesy::direct(shannonVOR->geod(), 10.0, SG_NM_TO_METER * 5.0);
95 wp2->setPos(g1);
96 wp2->setName("upInTheAir");
97 wp2->setOn_ground(false);
98 wp2->setGear_down(true);
99 wp2->setSpeed(150);
100
101
102 aiFP->addWaypoint(wp1);
103 aiFP->addWaypoint(wp2);
104
105 CPPUNIT_ASSERT_EQUAL(2, aiFP->getNrOfWayPoints());
106 CPPUNIT_ASSERT_EQUAL(wp1, aiFP->getCurrentWaypoint());
107 CPPUNIT_ASSERT_EQUAL(wp2, aiFP->getNextWaypoint());
108 CPPUNIT_ASSERT_EQUAL(0, aiFP->getLeg());
109
110 CPPUNIT_ASSERT_DOUBLES_EQUAL(10.0, aiFP->getBearing(wp1, wp2), 0.1);
111
112 time_t startTime = 1498;
113 aiFP->setTime(startTime);
114 CPPUNIT_ASSERT(!aiFP->isActive(1400));
115 CPPUNIT_ASSERT(aiFP->isActive(1500));
116
117 aiFP->IncrementWaypoint(false);
118 CPPUNIT_ASSERT_EQUAL(2, aiFP->getNrOfWayPoints());
119 CPPUNIT_ASSERT_EQUAL(wp1, aiFP->getPreviousWaypoint());
120 CPPUNIT_ASSERT_EQUAL(wp2, aiFP->getCurrentWaypoint());
121 CPPUNIT_ASSERT_EQUAL(static_cast<FGAIWaypoint*>(nullptr), aiFP->getNextWaypoint());
122 CPPUNIT_ASSERT_EQUAL(0, aiFP->getLeg());
123
124 auto wp3 = new FGAIWaypoint;
125 auto diganWpt = cache->findClosestWithIdent("DIGAN", shannonVOR->geod(), nullptr);
126
127 wp3->setPos(diganWpt->geod());
128 wp3->setName("overDIGAN");
129 wp3->setOn_ground(false);
130 wp3->setGear_down(false);
131 wp3->setSpeed(180);
132
133 // check that adding a waypoint doesn't mess up the iterators or
134 // current position
135 aiFP->addWaypoint(wp3);
136 CPPUNIT_ASSERT_EQUAL(3, aiFP->getNrOfWayPoints());
137 CPPUNIT_ASSERT_EQUAL(wp1, aiFP->getPreviousWaypoint());
138 CPPUNIT_ASSERT_EQUAL(wp2, aiFP->getCurrentWaypoint());
139 CPPUNIT_ASSERT_EQUAL(wp3, aiFP->getNextWaypoint());
140 CPPUNIT_ASSERT_EQUAL(0, aiFP->getLeg());
141
142 auto p3 = SGGeodesy::direct(diganWpt->geod(), 45, SG_NM_TO_METER * 4);
143 p3.setElevationFt(12000);
144 auto wp4 = new FGAIWaypoint;
145 wp4->setPos(p3);
146 wp4->setName("passDIGAN");
147 wp4->setSpeed(200);
148 aiFP->addWaypoint(wp4);
149
150 auto ingur = cache->findClosestWithIdent("INGUR", shannonVOR->geod(), nullptr);
151 auto p4 = ingur->geod();
152 p4.setElevationFt(16000);
153 auto wp5 = new FGAIWaypoint;
154 wp5->setPos(p4);
155 wp5->setName("INGUR");
156 wp5->setSpeed(250);
157 aiFP->addWaypoint(wp5);
158
159 aiFP->IncrementWaypoint(false);
160 CPPUNIT_ASSERT_EQUAL(5, aiFP->getNrOfWayPoints());
161 CPPUNIT_ASSERT_EQUAL(wp2, aiFP->getPreviousWaypoint());
162 CPPUNIT_ASSERT_EQUAL(wp3, aiFP->getCurrentWaypoint());
163 CPPUNIT_ASSERT_EQUAL(wp4, aiFP->getNextWaypoint());
164 CPPUNIT_ASSERT_EQUAL(0, aiFP->getLeg());
165
166 // let's increment to the end
167 aiFP->IncrementWaypoint(false);
168 aiFP->IncrementWaypoint(false);
169 CPPUNIT_ASSERT_EQUAL(5, aiFP->getNrOfWayPoints());
170 CPPUNIT_ASSERT_EQUAL(wp4, aiFP->getPreviousWaypoint());
171 CPPUNIT_ASSERT_EQUAL(wp5, aiFP->getCurrentWaypoint());
172 CPPUNIT_ASSERT_EQUAL(static_cast<FGAIWaypoint*>(nullptr), aiFP->getNextWaypoint());
173 CPPUNIT_ASSERT_EQUAL(0, aiFP->getLeg());
174
175 // one more increment 'off the end'
176 aiFP->IncrementWaypoint(false);
177 CPPUNIT_ASSERT_EQUAL(5, aiFP->getNrOfWayPoints());
178 CPPUNIT_ASSERT_EQUAL(wp5, aiFP->getPreviousWaypoint());
179 CPPUNIT_ASSERT_EQUAL(static_cast<FGAIWaypoint*>(nullptr), aiFP->getCurrentWaypoint());
180 CPPUNIT_ASSERT_EQUAL(static_cast<FGAIWaypoint*>(nullptr), aiFP->getNextWaypoint());
181
182 // should put us back on the last waypoint
183 aiFP->DecrementWaypoint();
184 CPPUNIT_ASSERT_EQUAL(5, aiFP->getNrOfWayPoints());
185 CPPUNIT_ASSERT_EQUAL(wp4, aiFP->getPreviousWaypoint());
186 CPPUNIT_ASSERT_EQUAL(wp5, aiFP->getCurrentWaypoint());
187 CPPUNIT_ASSERT_EQUAL(static_cast<FGAIWaypoint*>(nullptr), aiFP->getNextWaypoint());
188 CPPUNIT_ASSERT_EQUAL(0, aiFP->getLeg());
189
190 aiFP->DecrementWaypoint(); // back to wp4
191 aiFP->DecrementWaypoint(); // back to wp3
192 aiFP->DecrementWaypoint(); // back to wp2
193
194 CPPUNIT_ASSERT_EQUAL(5, aiFP->getNrOfWayPoints());
195 CPPUNIT_ASSERT_EQUAL(wp1, aiFP->getPreviousWaypoint());
196 CPPUNIT_ASSERT_EQUAL(wp2, aiFP->getCurrentWaypoint());
197 CPPUNIT_ASSERT_EQUAL(wp3, aiFP->getNextWaypoint());
198 CPPUNIT_ASSERT_EQUAL(0, aiFP->getLeg());
199
200 // restart to the beginning
201 aiFP->restart();
202 CPPUNIT_ASSERT_EQUAL(5, aiFP->getNrOfWayPoints());
203 CPPUNIT_ASSERT_EQUAL(static_cast<FGAIWaypoint*>(nullptr), aiFP->getPreviousWaypoint());
204 CPPUNIT_ASSERT_EQUAL(wp1, aiFP->getCurrentWaypoint());
205 CPPUNIT_ASSERT_EQUAL(wp2, aiFP->getNextWaypoint());
206 CPPUNIT_ASSERT_EQUAL(0, aiFP->getLeg());
207
208 // test increment with delete
209 aiFP->IncrementWaypoint(true);
210 CPPUNIT_ASSERT_EQUAL(5, aiFP->getNrOfWayPoints());
211 CPPUNIT_ASSERT_EQUAL(wp1, aiFP->getPreviousWaypoint());
212 CPPUNIT_ASSERT_EQUAL(wp2, aiFP->getCurrentWaypoint());
213 CPPUNIT_ASSERT_EQUAL(wp3, aiFP->getNextWaypoint());
214
215 aiFP->IncrementWaypoint(true);
216 CPPUNIT_ASSERT_EQUAL(4, aiFP->getNrOfWayPoints());
217 CPPUNIT_ASSERT_EQUAL(wp2, aiFP->getPreviousWaypoint());
218 CPPUNIT_ASSERT_EQUAL(wp3, aiFP->getCurrentWaypoint());
219 CPPUNIT_ASSERT_EQUAL(wp4, aiFP->getNextWaypoint());
220
221 aiFP->IncrementWaypoint(true);
222 CPPUNIT_ASSERT_EQUAL(3, aiFP->getNrOfWayPoints());
223 CPPUNIT_ASSERT_EQUAL(wp3, aiFP->getPreviousWaypoint());
224 CPPUNIT_ASSERT_EQUAL(wp4, aiFP->getCurrentWaypoint());
225 CPPUNIT_ASSERT_EQUAL(wp5, aiFP->getNextWaypoint());
226
227 // let's run up to the end and check nothing explodes
228 aiFP->IncrementWaypoint(true);
229 CPPUNIT_ASSERT_EQUAL(2, aiFP->getNrOfWayPoints());
230 CPPUNIT_ASSERT_EQUAL(wp4, aiFP->getPreviousWaypoint());
231 CPPUNIT_ASSERT_EQUAL(wp5, aiFP->getCurrentWaypoint());
232 CPPUNIT_ASSERT_EQUAL(static_cast<FGAIWaypoint*>(nullptr), aiFP->getNextWaypoint());
233
234 aiFP->IncrementWaypoint(true);
235 CPPUNIT_ASSERT_EQUAL(1, aiFP->getNrOfWayPoints());
236 CPPUNIT_ASSERT_EQUAL(wp5, aiFP->getPreviousWaypoint());
237 CPPUNIT_ASSERT_EQUAL(static_cast<FGAIWaypoint*>(nullptr), aiFP->getCurrentWaypoint());
238 CPPUNIT_ASSERT_EQUAL(static_cast<FGAIWaypoint*>(nullptr), aiFP->getNextWaypoint());
239 }
240
241 void AIFlightPlanTests::testAIFlightPlanLeftCircle()
242 {
243 auto aiFP = new FGAIFlightPlan;
244 aiFP->setName("Bob");
245 aiFP->setRunway("24");
246
247 CPPUNIT_ASSERT_EQUAL(string{"Bob"}, aiFP->getName());
248 CPPUNIT_ASSERT_EQUAL(string{"24"}, aiFP->getRunway());
249
250 CPPUNIT_ASSERT_EQUAL(0, aiFP->getNrOfWayPoints());
251 CPPUNIT_ASSERT_EQUAL(static_cast<FGAIWaypoint*>(nullptr), aiFP->getPreviousWaypoint());
252 CPPUNIT_ASSERT_EQUAL(static_cast<FGAIWaypoint*>(nullptr), aiFP->getCurrentWaypoint());
253 CPPUNIT_ASSERT_EQUAL(static_cast<FGAIWaypoint*>(nullptr), aiFP->getNextWaypoint());
254 CPPUNIT_ASSERT_EQUAL(0, aiFP->getLeg());
255
256 FGPositioned::TypeFilter ty(FGPositioned::VOR);
257 auto cache = flightgear::NavDataCache::instance();
258 auto shannonVOR = cache->findClosestWithIdent("SHA", SGGeod::fromDeg(-8, 52), &ty);
259 CPPUNIT_ASSERT_EQUAL(string{"SHANNON VOR-DME"}, shannonVOR->name());
260 auto wp1 = new FGAIWaypoint;
261 wp1->setPos(shannonVOR->geod());
262 wp1->setName("testWp_0");
263 wp1->setOn_ground(true);
264 wp1->setGear_down(true);
265 wp1->setSpeed(10);
266 aiFP->addWaypoint(wp1);
267
268 auto lastWp = wp1;
269
270 int course = 0;
271
272 for(int i = 1; i <= 10; i++) {
273 auto wp = new FGAIWaypoint;
274 course += 10;
275 const auto g1 = SGGeodesy::direct(lastWp->getPos(), course, SG_NM_TO_METER * 5.0);
276 wp->setPos(g1);
277 wp->setName("testWp_" + std::to_string(i));
278 wp->setOn_ground(true);
279 wp->setGear_down(true);
280 wp->setSpeed(10);
281 aiFP->addWaypoint(wp);
282 lastWp = wp;
283 }
284 CPPUNIT_ASSERT_EQUAL(aiFP->getNrOfWayPoints(), 11);
285 }
286
287 void AIFlightPlanTests::testAIFlightPlans()
288 {
289 auto aiFP = new FGAIFlightPlan;
290 aiFP->setName("Bob");
291 aiFP->setRunway("24");
292
293 FGAirportRef egph = FGAirport::getByIdent("EGPH");
294 FGAirportRef egpf = FGAirport::getByIdent("EGPF");
295
296
297 // Time to depart
298 std::string dep = FGTestApi::strings::getTimeString(30);
299 // Time to arrive
300 std::string arr = FGTestApi::strings::getTimeString(320);
301
302 FGAISchedule* schedule = new FGAISchedule(
303 "B737", "KLM", "EGPH", "G-BLA", "ID", false, "B737", "KLM", "N", "cargo", 24, 8);
304 FGScheduledFlight* flight = new FGScheduledFlight("testPushbackCargo", "", "EGPH", "EGPF", 24, dep, arr, "WEEK", "HBR_BN_2");
305 schedule->assign(flight);
306
307 SGSharedPtr<FGAIAircraft> aiAircraft = new FGAIAircraft{schedule};
308
309 std::string activeRunway;
310 FGRunwayRef rwy;
311
312 // FLIGHTGEAR-1VBR
313 int aircraftHeading = 302;
314 int heading = 5;
315
316 // heading of vector towards threshold
317 egpf->getDynamics()->getActiveRunway("com", 2, activeRunway, heading);
318 rwy = egpf->getRunwayByIdent(activeRunway);
319 SGGeod threshold = rwy->threshold();
320 SGGeod aiAircraftPos = SGGeodesy::direct(threshold, aiAircraft->getTrueHeadingDeg(), 120000);
321
322 aiAircraft->setLatitude(aiAircraftPos.getLatitudeDeg());
323 aiAircraft->setLongitude(aiAircraftPos.getLongitudeDeg());
324 aiAircraft->setHeading(aircraftHeading);
325
326 bool isValid = aiFP->create(aiAircraft, egph, egpf, AILeg::APPROACH, 5000, 200, 51, 10,
327 false, 20, "cargo", "B737", "KLM", 1000);
328 CPPUNIT_ASSERT_EQUAL(true, isValid);
329 }
330
331 void AIFlightPlanTests::testAIFlightPlanLoadXML()
332 {
333 const auto xml = R"(<?xml version="1.0" encoding="UTF-8"?>
334 <PropertyList>
335 <flightplan>
336 <wp>
337 <name>onGroundWP</name>
338 <lat>57</lat>
339 <lon>3</lon>
340 <ktas>10</ktas>
341 <on-ground>1</on-ground>
342 </wp>
343 <wp>
344 <name>someWP</name>
345 <lat>57</lat>
346 <lon>4</lon>
347 <ktas>200</ktas>
348 <alt>8000</alt>
349 </wp>
350 <wp>
351 <name>END</name>
352 </wp>
353 </flightplan>
354 </PropertyList>
355 )";
356
357 std::istringstream is(xml);
358
359 std::unique_ptr<FGAIFlightPlan> aiFP(new FGAIFlightPlan);
360 bool ok = aiFP->readFlightplan(is, sg_location("In-memory test_ai_fp.xml"));
361 CPPUNIT_ASSERT(ok);
362
363 CPPUNIT_ASSERT_EQUAL(false, aiFP->getCurrentWaypoint()->getInAir());
364 CPPUNIT_ASSERT_EQUAL(true, aiFP->getCurrentWaypoint()->getGear_down());
365 CPPUNIT_ASSERT_DOUBLES_EQUAL(1.0, aiFP->getCurrentWaypoint()->getFlaps(), 0.1);
366
367 auto wp2 = aiFP->getNextWaypoint();
368 CPPUNIT_ASSERT_EQUAL(true, wp2->getInAir());
369 CPPUNIT_ASSERT_EQUAL(false, wp2->getGear_down());
370 CPPUNIT_ASSERT_DOUBLES_EQUAL(0.0, wp2->getFlaps(), 0.1);
371 }
372
373 void AIFlightPlanTests::testLeftTurnFlightplanXML()
374 {
375 std::unique_ptr<FGAIFlightPlan> aiFP(new FGAIFlightPlan);
376 const auto fpath = SGPath::fromUtf8(FG_TEST_SUITE_DATA) / "AI"/"Flightplan"/"left_onground.xml";
377
378 std::fstream fs(fpath.c_str());
379
380 bool ok = aiFP->readFlightplan(fs, sg_location("In-memory test_ai_fp.xml"));
381 CPPUNIT_ASSERT(ok);
382
383 CPPUNIT_ASSERT_EQUAL(false, aiFP->getCurrentWaypoint()->getInAir());
384
385 auto wp2 = aiFP->getNextWaypoint();
386 CPPUNIT_ASSERT_EQUAL(false, wp2->getInAir());
387 CPPUNIT_ASSERT_DOUBLES_EQUAL(10.0, wp2->getSpeed(), 0.1);
388 }
389
390 void AIFlightPlanTests::testRightTurnFlightplanXML()
391 {
392 std::unique_ptr<FGAIFlightPlan> aiFP(new FGAIFlightPlan);
393 const auto fpath = SGPath::fromUtf8(FG_TEST_SUITE_DATA) / "AI"/"Flightplan"/"right_onground.xml";
394
395 std::fstream fs(fpath.c_str());
396
397 bool ok = aiFP->readFlightplan(fs, sg_location("In-memory test_ai_fp.xml"));
398 CPPUNIT_ASSERT(ok);
399
400 CPPUNIT_ASSERT_EQUAL(false, aiFP->getCurrentWaypoint()->getInAir());
401
402 auto wp2 = aiFP->getNextWaypoint();
403 CPPUNIT_ASSERT_EQUAL(false, wp2->getInAir());
404 CPPUNIT_ASSERT_DOUBLES_EQUAL(10.0, wp2->getSpeed(), 0.1);
405 }