Debian Codegraph @

test_suite/unit_tests/Navaids/test_routeManager.cxx

raw symbols SCIP
1 #include "config.h"
2
3 #include "test_routeManager.hxx"
4
5 #include <memory>
6 #include <cstring>
7
8 #include <simgear/io/iostreams/sgstream.hxx>
9 #include <simgear/misc/sg_dir.hxx>
10 #include <simgear/structure/commands.hxx>
11
12 #include "test_suite/FGTestApi/testGlobals.hxx"
13 #include "test_suite/FGTestApi/NavDataCache.hxx"
14 #include "test_suite/FGTestApi/TestPilot.hxx"
15
16 #include <Navaids/FlightPlan.hxx>
17 #include <Navaids/NavDataCache.hxx>
18 #include <Navaids/navrecord.hxx>
19 #include <Navaids/navlist.hxx>
20 #include <Navaids/routePath.hxx>
21
22 #include <Main/globals.hxx>
23 // we need a default GPS instrument, hard to test seperately for now
24 #include <Instrumentation/gps.hxx>
25
26 #include <Autopilot/route_mgr.hxx>
27
28 using namespace flightgear;
29 using namespace std::string_literals;
30
31 static bool static_haveProcedures = false;
32
33 static FlightPlanRef makeTestFP(const std::string& depICAO, const std::string& depRunway,
34 const std::string& destICAO, const std::string& destRunway,
35 const std::string& waypoints)
36 {
37 FlightPlanRef f = FlightPlan::create();
38 FGTestApi::setUp::populateFPWithNasal(f, depICAO, depRunway, destICAO, destRunway, waypoints);
39 return f;
40 }
41
42
43 // Set up function for each test.
44 void RouteManagerTests::setUp()
45 {
46 FGTestApi::setUp::initTestGlobals("routemanager");
47 FGTestApi::setUp::initNavDataCache();
48
49 SGPath proceduresPath = SGPath::fromEnv("FG_PROCEDURES_PATH");
50 if (proceduresPath.exists()) {
51 static_haveProcedures = true;
52 globals->append_fg_scenery(proceduresPath);
53 }
54
55 globals->get_subsystem_mgr()->add<FGRouteMgr>();
56
57 // setup the default GPS, which is needed for waypoint
58 // sequencing to work
59 SGPropertyNode_ptr configNode(new SGPropertyNode);
60 configNode->setStringValue("name", "gps");
61 configNode->setIntValue("number", 0);
62 GPS* gps(new GPS(configNode, true /* default mode */));
63 m_gps = gps;
64
65 SGPropertyNode_ptr node = globals->get_props()->getNode("instrumentation", true)->getChild("gps", 0, true);
66 // node->setBoolValue("serviceable", true);
67 // globals->get_props()->setDoubleValue("systems/electrical/outputs/gps", 6.0);
68 globals->get_subsystem_mgr()->add("gps", gps);
69
70 globals->get_subsystem_mgr()->bind();
71 globals->get_subsystem_mgr()->init();
72
73 FGTestApi::setUp::initStandardNasal();
74 globals->get_subsystem_mgr()->postinit();
75 }
76
77
78 // Clean up after each test.
79 void RouteManagerTests::tearDown()
80 {
81 m_gps = nullptr;
82 FGTestApi::tearDown::shutdownTestGlobals();
83 }
84
85 void RouteManagerTests::setPositionAndStabilise(const SGGeod& g)
86 {
87 FGTestApi::setPosition(g);
88 for (int i=0; i<60; ++i) {
89 globals->get_subsystem_mgr()->update(0.02);
90 }
91 }
92
93 void RouteManagerTests::testBasic()
94 {
95 //FGTestApi::setUp::logPositionToKML("rm_basic");
96
97 FlightPlanRef fp1 = makeTestFP("EGLC", "27", "EHAM", "06",
98 "CLN IDESI RINIS VALKO RIVER RTM EKROS");
99 fp1->setIdent("testplan");
100 fp1->setCruiseFlightLevel(360);
101
102 CPPUNIT_ASSERT_EQUAL("RIVER"s, fp1->legAtIndex(5)->waypoint()->ident());
103
104 auto rm = globals->get_subsystem<FGRouteMgr>();
105 rm->setFlightPlan(fp1);
106
107 auto gpsNode = globals->get_props()->getNode("instrumentation/gps", true);
108 CPPUNIT_ASSERT(gpsNode->getStringValue("mode") == "obs");
109
110 rm->activate();
111
112 CPPUNIT_ASSERT(fp1->isActive());
113
114 // Nasal deleagte should have placed GPS into leg mode
115 auto rmNode = globals->get_props()->getNode("autopilot/route-manager", true);
116
117 CPPUNIT_ASSERT(gpsNode->getStringValue("mode") == "leg");
118
119 CPPUNIT_ASSERT(rmNode->getStringValue("departure/airport") == "EGLC");
120 CPPUNIT_ASSERT(rmNode->getStringValue("departure/runway") == "27");
121 CPPUNIT_ASSERT(rmNode->getStringValue("departure/sid") == "");
122 CPPUNIT_ASSERT(rmNode->getStringValue("departure/name") == "London City");
123
124 CPPUNIT_ASSERT(rmNode->getStringValue("destination/airport") == "EHAM");
125 CPPUNIT_ASSERT(rmNode->getStringValue("destination/runway") == "06");
126
127 CPPUNIT_ASSERT_EQUAL(360, rmNode->getIntValue("cruise/flight-level"));
128 CPPUNIT_ASSERT_EQUAL(false, rmNode->getBoolValue("airborne"));
129
130 CPPUNIT_ASSERT_EQUAL(0, rmNode->getIntValue("current-wp"));
131 auto wp0Node = rmNode->getNode("wp");
132 CPPUNIT_ASSERT(wp0Node->getStringValue("id") == "EGLC-27");
133
134 auto wp1Node = rmNode->getNode("wp[1]");
135 CPPUNIT_ASSERT(wp1Node->getStringValue("id") == "CLN");
136
137 FGPositioned::TypeFilter f{FGPositioned::VOR};
138 auto clactonVOR = fgpositioned_cast<FGNavRecord>(FGPositioned::findClosestWithIdent("CLN", SGGeod::fromDeg(0.0, 51.0), &f));
139
140 // verify hold entry course
141 auto pilot = SGSharedPtr<FGTestApi::TestPilot>(new FGTestApi::TestPilot);
142 FGTestApi::setPosition(fp1->departureRunway()->geod());
143 pilot->resetAtPosition(fp1->departureRunway()->geod());
144
145 pilot->setSpeedKts(220);
146 pilot->setCourseTrue(fp1->departureRunway()->headingDeg());
147 pilot->setTargetAltitudeFtMSL(10000);
148
149 bool ok = FGTestApi::runForTimeWithCheck(30.0, [rmNode] () {
150 if (rmNode->getIntValue("current-wp") == 1) return true;
151 return false;
152 });
153
154 CPPUNIT_ASSERT(ok);
155
156 // continue outbound for some time
157 pilot->setSpeedKts(250);
158 FGTestApi::runForTime(30.0);
159
160 // turn towards Clacton VOR
161 pilot->flyDirectTo(clactonVOR->geod());
162 pilot->setTargetAltitudeFtMSL(30000);
163 pilot->setSpeedKts(280);
164 ok = FGTestApi::runForTimeWithCheck(6000.0, [rmNode] () {
165 if (rmNode->getIntValue("current-wp") == 2) return true;
166 return false;
167 });
168 CPPUNIT_ASSERT(ok);
169
170 // short straight leg for testing
171 pilot->flyHeading(90.0);
172 pilot->setSpeedKts(330);
173 FGTestApi::runForTime(30.0);
174
175 // let's engage LNAV mode :)
176 pilot->flyGPSCourse(m_gps);
177
178 ok = FGTestApi::runForTimeWithCheck(6000.0, [rmNode] () {
179 if (rmNode->getIntValue("current-wp") == 5) return true;
180 return false;
181 });
182 CPPUNIT_ASSERT(ok);
183
184 // check where we are - should be heading to RIVER from VALKO
185 CPPUNIT_ASSERT_EQUAL(5, rmNode->getIntValue("current-wp"));
186 CPPUNIT_ASSERT(wp0Node->getStringValue("id") == "RIVER");
187 CPPUNIT_ASSERT(wp1Node->getStringValue("id") == "RTM");
188 // slightly rapid descent
189 pilot->setTargetAltitudeFtMSL(3000);
190
191 ok = FGTestApi::runForTimeWithCheck(6000.0, [rmNode] () {
192 if (rmNode->getIntValue("current-wp") == 7) return true;
193 return false;
194 });
195 CPPUNIT_ASSERT(ok);
196
197 // run until the GPS reverts to OBS mode at the end of the flight plan
198 ok = FGTestApi::runForTimeWithCheck(6000.0, [gpsNode] () {
199 if (gpsNode->getStringValue("mode") == "obs") return true;
200 return false;
201 });
202
203 CPPUNIT_ASSERT(ok);
204 CPPUNIT_ASSERT_EQUAL(-1, fp1->currentIndex());
205 CPPUNIT_ASSERT(!fp1->isActive());
206 CPPUNIT_ASSERT_EQUAL(-1, rmNode->getIntValue("current-wp"));
207 CPPUNIT_ASSERT_EQUAL(false, rmNode->getBoolValue("active"));
208 }
209
210 void RouteManagerTests::testDefaultSID()
211 {
212 FlightPlanRef fp1 = makeTestFP("EGLC", "27", "EHAM", "24",
213 "CLN IDRID VALKO");
214 fp1->setIdent("testplan");
215
216 auto rm = globals->get_subsystem<FGRouteMgr>();
217 rm->setFlightPlan(fp1);
218
219 auto rmNode = globals->get_props()->getNode("autopilot/route-manager", true);
220 rmNode->setStringValue("departure/sid", "DEFAULT");
221
222 // let's see what we got :)
223
224 rm->activate();
225
226 CPPUNIT_ASSERT(fp1->isActive());
227 }
228
229 void RouteManagerTests::testDirectToLegOnFlightplanAndResume()
230 {
231 // FGTestApi::setUp::logPositionToKML("rm_dto_resume_leg");
232
233 // this is very similar to the identiucally name dtest in GPSTests, but relies on the Nasal
234 // route manager delegate to perform the same task
235 FlightPlanRef fp1 = makeTestFP("EBBR", "07L", "EGGD", "27",
236 "NIK COA DVR TAWNY WOD");
237 auto rm = globals->get_subsystem<FGRouteMgr>();
238 rm->setFlightPlan(fp1);
239 // FGTestApi::writeFlightPlanToKML(fp1);
240
241 auto gpsNode = globals->get_props()->getNode("instrumentation/gps", true);
242 auto rmNode = globals->get_props()->getNode("autopilot/route-manager", true);
243
244 CPPUNIT_ASSERT(gpsNode->getStringValue("mode") == "obs");
245 rm->activate();
246
247 CPPUNIT_ASSERT(fp1->isActive());
248
249 FGTestApi::setPosition(fp1->departureRunway()->pointOnCenterline(0.0));
250 FGTestApi::runForTime(10.0); // let the GPS stabilize
251
252 CPPUNIT_ASSERT_EQUAL(std::string{"leg"}, gpsNode->getStringValue("mode"));
253 CPPUNIT_ASSERT_EQUAL(std::string{"EBBR-07L"}, gpsNode->getStringValue("wp/wp[1]/ID"));
254
255 CPPUNIT_ASSERT_EQUAL(0, rmNode->getIntValue("current-wp"));
256 auto wp0Node = rmNode->getNode("wp");
257 CPPUNIT_ASSERT(wp0Node->getStringValue("id") == "EBBR-07L");
258
259 auto wp1Node = rmNode->getNode("wp[1]");
260 CPPUNIT_ASSERT(wp1Node->getStringValue("id") == "NIK");
261
262 // initiate a direct to
263 SGGeod p2 = fp1->departureRunway()->pointOnCenterline(5.0* SG_NM_TO_METER);
264 FGTestApi::setPosition(p2);
265
266 auto doverVOR = fp1->legAtIndex(3)->waypoint()->source();
267
268 double distanceToDover = SGGeodesy::distanceNm(p2, doverVOR->geod());
269 double bearingToDover = SGGeodesy::courseDeg(p2, doverVOR->geod());
270
271 CPPUNIT_ASSERT_EQUAL(std::string{"DVR"}, doverVOR->ident());
272 gpsNode->setStringValue("scratch/ident", "DVR");
273 gpsNode->setDoubleValue("scratch/longitude-deg", doverVOR->geod().getLongitudeDeg());
274 gpsNode->setDoubleValue("scratch/latitude-deg", doverVOR->geod().getLatitudeDeg());
275 gpsNode->setStringValue("command", "direct");
276 CPPUNIT_ASSERT_EQUAL(std::string{"dto"}, gpsNode->getStringValue("mode"));
277
278 // check that upon reaching DOVER, we sequence to TAWNY and resume leg mode
279 // this is handled by the default delegate in Nasal
280
281 SGGeod posNearDover = SGGeodesy::direct(p2, bearingToDover, (distanceToDover - 8.0) * SG_NM_TO_METER);
282 FGTestApi::setPosition(posNearDover);
283
284 auto pilot = SGSharedPtr<FGTestApi::TestPilot>(new FGTestApi::TestPilot);
285 pilot->resetAtPosition(posNearDover);
286 pilot->setSpeedKts(250);
287 pilot->flyGPSCourse(m_gps);
288
289 bool ok = FGTestApi::runForTimeWithCheck(180.0, [fp1] () {
290 if (fp1->currentIndex() == 4) {
291 return true;
292 }
293 return false;
294 });
295
296 CPPUNIT_ASSERT(ok);
297 CPPUNIT_ASSERT_EQUAL(std::string{"leg"}, gpsNode->getStringValue("mode"));
298 }
299
300 void RouteManagerTests::testSequenceDiscontinuityAndResume()
301 {
302 // FGTestApi::setUp::logPositionToKML("rm_seq_discon_resume_leg");
303
304 FlightPlanRef fp1 = makeTestFP("LIRF", "16R", "LEBL", "07L",
305 "GITRI BALEN MUREN TOSNU");
306
307
308 fp1->insertWayptAtIndex(new Discontinuity(fp1), 3);
309
310 auto rm = globals->get_subsystem<FGRouteMgr>();
311 rm->setFlightPlan(fp1);
312
313 auto gpsNode = globals->get_props()->getNode("instrumentation/gps", true);
314 // auto rmNode = globals->get_props()->getNode("autopilot/route-manager", true);
315
316 auto balenLeg = fp1->legAtIndex(2);
317 auto pos = fp1->pointAlongRoute(2, -8.0); // 8nm before BALEN
318 FGTestApi::setPosition(pos);
319 FGTestApi::runForTime(10.0); // let the GPS stabilize
320
321 CPPUNIT_ASSERT(gpsNode->getStringValue("mode") == "obs");
322 rm->activate();
323
324 CPPUNIT_ASSERT(fp1->isActive());
325
326 CPPUNIT_ASSERT(gpsNode->getStringValue("mode") == "leg");
327 fp1->setCurrentIndex(2);
328
329 auto pilot = SGSharedPtr<FGTestApi::TestPilot>(new FGTestApi::TestPilot);
330 pilot->resetAtPosition(pos);
331 pilot->setCourseTrue(270);
332 pilot->setSpeedKts(250);
333 pilot->flyGPSCourse(m_gps);
334
335 bool ok = FGTestApi::runForTimeWithCheck(180.0, [gpsNode] () {
336 return (gpsNode->getStringValue("mode") == "obs");
337 });
338
339 CPPUNIT_ASSERT(ok);
340 CPPUNIT_ASSERT_EQUAL(2, fp1->currentIndex()); // shouldn't sequence
341
342 FGTestApi::runForTime(30.0);
343
344
345 // 2nm before MUREN : this should be on the GC course from BALEN
346 auto pos2 = fp1->pointAlongRoute(4, -6.0);
347 FGTestApi::setPosition(pos2);
348 FGTestApi::runForTime(2.0); // let the GPS stabilize
349
350 // initiate a direct-to the next real WP
351 const auto murenPos = fp1->legAtIndex(4)->waypoint()->position();
352 gpsNode->setStringValue("scratch/ident", "MUREN");
353 gpsNode->setDoubleValue("scratch/longitude-deg", murenPos.getLongitudeDeg());
354 gpsNode->setDoubleValue("scratch/latitude-deg", murenPos.getLatitudeDeg());
355 gpsNode->setStringValue("command", "direct");
356 CPPUNIT_ASSERT_EQUAL(std::string{"dto"}, gpsNode->getStringValue("mode"));
357
358 pilot->resetAtPosition(pos2);
359 pilot->flyGPSCourse(m_gps);
360
361 ok = FGTestApi::runForTimeWithCheck(600.0, [fp1] () {
362 if (fp1->currentIndex() == 5) {
363 return true;
364 }
365 return false;
366 });
367
368 CPPUNIT_ASSERT(ok);
369 CPPUNIT_ASSERT_EQUAL(std::string{"leg"}, gpsNode->getStringValue("mode"));
370 CPPUNIT_ASSERT_EQUAL(5, fp1->currentIndex());
371
372 FGTestApi::runForTime(30.0);
373
374 }
375
376 void RouteManagerTests::testDefaultApproach()
377 {
378
379 }
380
381 void RouteManagerTests::testHiddenWaypoints()
382 {
383 FlightPlanRef fp1 = makeTestFP("NZCH", "02", "NZAA", "05L",
384 "ALADA NS WB WN MAMOD KAPTI OH");
385 fp1->setIdent("testplan");
386 fp1->setCruiseFlightLevel(360);
387
388 auto rm = globals->get_subsystem<FGRouteMgr>();
389 rm->setFlightPlan(fp1);
390
391 auto gpsNode = globals->get_props()->getNode("instrumentation/gps", true);
392 CPPUNIT_ASSERT(gpsNode->getStringValue("mode") == "obs");
393
394 // FIXME: use real Nasal test macros soon
395 auto testNode = globals->get_props()->getNode("test-data", true);
396
397 fp1->legAtIndex(3)->waypoint()->setFlag(WPT_HIDDEN);
398
399 // ensure no visual path is generated for hidden waypoints
400 RoutePath path(fp1);
401
402 // no path should be generated between 2 and 3
403 CPPUNIT_ASSERT(path.pathForIndex(3).empty());
404
405 // no path should be generated between 3 and 4
406 CPPUNIT_ASSERT(path.pathForIndex(4).empty());
407 CPPUNIT_ASSERT(!path.pathForIndex(5).empty());
408
409
410 bool ok = FGTestApi::executeNasal(R"(
411 var fp = flightplan(); # retrieve the global flightplan
412 setprop("/test-data/a", fp.numRemainingWaypoints());
413 )");
414 CPPUNIT_ASSERT(ok);
415 CPPUNIT_ASSERT_EQUAL(fp1->numLegs(), testNode->getIntValue("a"));
416
417 rm->activate();
418 fp1->setCurrentIndex(2);
419
420 ok = FGTestApi::executeNasal(R"(
421 var fp = flightplan(); # retrieve the global flightplan
422 setprop("/test-data/a", fp.numRemainingWaypoints());
423 setprop("/test-data/b", fp.currentWP(2).id);
424 )");
425
426 CPPUNIT_ASSERT(ok);
427 CPPUNIT_ASSERT_EQUAL(7, testNode->getIntValue("a"));
428 CPPUNIT_ASSERT_EQUAL(std::string{"WN"}, testNode->getStringValue("b"));
429
430 ok = FGTestApi::executeNasal(R"(
431 var fp = flightplan(); # retrieve the global flightplan
432 setprop("/test-data/c", fp.currentWP(-1).id);
433
434 # ensure invalid offset returns nil
435 setprop("/test-data/d", fp.currentWP(-100) == nil);
436 )");
437
438 CPPUNIT_ASSERT(ok);
439 CPPUNIT_ASSERT_EQUAL(std::string{"ALADA"}, testNode->getStringValue("c"));
440 CPPUNIT_ASSERT_EQUAL(true, testNode->getBoolValue("d"));
441 }
442
443 void RouteManagerTests::testHoldFromNasal()
444 {
445 // FGTestApi::setUp::logPositionToKML("rm_hold_from_nasal");
446
447 // test that Nasal can set a hold-count (implicitly converting a leg
448 // to a Hold waypt), configure the hold radial, and exit the hold
449
450 FlightPlanRef fp1 = makeTestFP("NZCH", "02", "NZAA", "05L",
451 "ALADA NS WB WN MAMOD KAPTI OH");
452 fp1->setIdent("testplan");
453 fp1->setCruiseFlightLevel(360);
454
455 auto rm = globals->get_subsystem<FGRouteMgr>();
456 rm->setFlightPlan(fp1);
457 // FGTestApi::writeFlightPlanToKML(fp1);
458
459 auto gpsNode = globals->get_props()->getNode("instrumentation/gps", true);
460 CPPUNIT_ASSERT(gpsNode->getStringValue("mode") == "obs");
461
462 rm->activate();
463
464 CPPUNIT_ASSERT(fp1->isActive());
465 CPPUNIT_ASSERT(gpsNode->getStringValue("mode") == "leg");
466
467 SGGeod posEnrouteToWB = fp1->pointAlongRoute(3, -10.0);
468 FGTestApi::setPositionAndStabilise(posEnrouteToWB);
469
470 // sequence everything to the correct wp
471 fp1->setCurrentIndex(3);
472
473 // setup some hold data from Nasal. To make it more challenging,
474 // do it once the wp is already active
475 bool ok = FGTestApi::executeNasal(R"(
476 var fp = flightplan(); # retrieve the global flightplan
477 var leg = fp.getWP(3);
478 leg.hold_count = 4;
479 leg.hold_heading_radial_deg = 310;
480 )");
481 CPPUNIT_ASSERT(ok);
482
483 // check the value updated in the leg
484 CPPUNIT_ASSERT_EQUAL(4, fp1->legAtIndex(3)->holdCount());
485
486 // check we converted to a hold
487 auto wp = fp1->legAtIndex(3)->waypoint();
488 auto holdWpt = static_cast<flightgear::Hold*>(wp);
489
490 CPPUNIT_ASSERT_EQUAL(wp->type(), std::string{"hold"});
491 CPPUNIT_ASSERT_DOUBLES_EQUAL(310.0, holdWpt->headingRadialDeg(), 0.5);
492
493 // establish the test pilot at this position too
494 auto pilot = SGSharedPtr<FGTestApi::TestPilot>(new FGTestApi::TestPilot);
495 pilot->resetAtPosition(posEnrouteToWB);
496 pilot->setSpeedKts(250);
497 pilot->flyGPSCourse(m_gps);
498 pilot->setCourseTrue(gpsNode->getDoubleValue("wp/leg-true-course-deg"));
499
500 // run for a bit to stabilize everything
501 FGTestApi::runForTime(5.0);
502
503 // check we upgraded to a hold controller internally, and are flying to it :)
504 auto statusNode = gpsNode->getNode("rnav-controller-status");
505 CPPUNIT_ASSERT_EQUAL(std::string{"leg-to-hold"}, statusNode->getStringValue());
506
507 // check we're on the leg
508 CPPUNIT_ASSERT_EQUAL(std::string{"NELSON VOR-DME"}, fp1->legAtIndex(2)->waypoint()->source()->name());
509
510 auto wbPos = fp1->legAtIndex(3)->waypoint()->position();
511 auto nsPos = fp1->legAtIndex(2)->waypoint()->position();
512 const double crsToWB = SGGeodesy::courseDeg(globals->get_aircraft_position(), wbPos);
513 const double crsNSWB = SGGeodesy::courseDeg(nsPos,wbPos);
514
515 CPPUNIT_ASSERT_DOUBLES_EQUAL(crsToWB, gpsNode->getDoubleValue("wp/wp[1]/bearing-true-deg"), 0.5);
516 CPPUNIT_ASSERT_DOUBLES_EQUAL(crsNSWB, gpsNode->getDoubleValue("wp/leg-true-course-deg"), 0.5);
517 CPPUNIT_ASSERT_DOUBLES_EQUAL(0.0, gpsNode->getDoubleValue("wp/wp[1]/course-error-nm"), 0.05);
518 CPPUNIT_ASSERT_DOUBLES_EQUAL(0.0, gpsNode->getDoubleValue("wp/wp[1]/course-deviation-deg"), 0.5);
519
520 // fly into the hold, should be teardrop entry
521 ok = FGTestApi::runForTimeWithCheck(600.0, [statusNode] () {
522 if (statusNode->getStringValue() == "entry-teardrop") return true;
523 return false;
524 });
525
526 CPPUNIT_ASSERT(ok);
527
528 ok = FGTestApi::runForTimeWithCheck(600.0, [statusNode] () {
529 if (statusNode->getStringValue() == "hold-inbound") return true;
530 return false;
531 });
532
533 ok = FGTestApi::runForTimeWithCheck(600.0, [statusNode] () {
534 if (statusNode->getStringValue() == "hold-outbound") return true;
535 return false;
536 });
537 CPPUNIT_ASSERT(ok);
538
539 // half way through the outbound turn
540 FGTestApi::runForTime(30.0);
541
542 ok = FGTestApi::executeNasal(R"(
543 setprop("/instrumentation/gps/command", "exit-hold");
544 )");
545 CPPUNIT_ASSERT(ok);
546
547 // no change yet
548 CPPUNIT_ASSERT_EQUAL(std::string{"hold-outbound"}, statusNode->getStringValue());
549
550 // then we fly inbound
551 ok = FGTestApi::runForTimeWithCheck(600.0, [statusNode] () {
552 if (statusNode->getStringValue() == "hold-inbound") return true;
553 return false;
554 });
555 CPPUNIT_ASSERT(ok);
556
557 // and then we exit
558 ok = FGTestApi::runForTimeWithCheck(600.0, [fp1] () {
559 if (fp1->currentIndex() == 4) return true;
560 return false;
561 });
562 CPPUNIT_ASSERT(ok);
563
564 // get back on course
565 FGTestApi::runForTime(60.0);
566 }
567
568 // check that when loading a GPX, airport waypoints are created
569 // by the default delegate
570 // https://sourceforge.net/p/flightgear/codetickets/2227/
571 void RouteManagerTests::loadGPX()
572 {
573 auto rm = globals->get_subsystem<FGRouteMgr>();
574
575 FlightPlanRef f = FlightPlan::create();
576 rm->setFlightPlan(f);
577
578 SGPath gpxPath = simgear::Dir::current().path() / "test_gpx.gpx";
579 {
580 sg_ofstream s(gpxPath);
581 s << R"(<?xml version="1.0" encoding="UTF-8"?>
582 <gpx xmlns="http://www.topografix.com/GPX/1/1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" creator="SkyVector" version="1.1" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd">
583 <rte>
584 <name>KJFK-KBOS</name>
585 <rtept lat="40.639928" lon="-73.778692">
586 <name>KJFK</name>
587 <overfly>false</overfly>
588 </rtept>
589 <rtept lat="41.641106" lon="-72.547419">
590 <name>HFD</name>
591 <overfly>false</overfly>
592 </rtept>
593 <rtept lat="42.362944" lon="-71.006389">
594 <name>KBOS</name>
595 <overfly>false</overfly>
596 </rtept>
597
598 </rte>
599 </gpx>
600 )";
601 }
602
603 CPPUNIT_ASSERT(f->load(gpxPath));
604
605 auto kbos = FGAirport::getByIdent("KBOS");
606 auto kjfk = FGAirport::getByIdent("KJFK");
607 CPPUNIT_ASSERT_EQUAL(kjfk, f->departureAirport());
608 CPPUNIT_ASSERT_EQUAL(static_cast<FGRunway*>(nullptr), f->departureRunway());
609 CPPUNIT_ASSERT_EQUAL(3, f->numLegs());
610 CPPUNIT_ASSERT_EQUAL(kbos, f->destinationAirport());
611
612 auto wp1 = f->legAtIndex(1);
613 CPPUNIT_ASSERT_EQUAL(std::string{"HFD"}, wp1->waypoint()->ident());
614 auto wp2 = f->legAtIndex(2);
615 CPPUNIT_ASSERT_EQUAL(std::string{"KBOS"}, wp2->waypoint()->ident());
616 }
617
618 const std::string flightPlanXMLData =
619 R"(<?xml version="1.0" encoding="UTF-8"?>
620 <PropertyList>
621 <version type="int">2</version>
622 <departure>
623 <airport type="string">EDDM</airport>
624 <runway type="string">08R</runway>
625 </departure>
626 <destination>
627 <airport type="string">EDDF</airport>
628 </destination>
629 <route>
630 <wp>
631 <type type="string">runway</type>
632 <departure type="bool">true</departure>
633 <ident type="string">08R</ident>
634 <icao type="string">EDDM</icao>
635 </wp>
636 <wp n="1">
637 <type type="string">navaid</type>
638 <ident type="string">GIVMI</ident>
639 <lon type="double">11.364700</lon>
640 <lat type="double">48.701100</lat>
641 </wp>
642 <wp n="2">
643 <type type="string">navaid</type>
644 <ident type="string">ERNAS</ident>
645 <lon type="double">11.219400</lon>
646 <lat type="double">48.844700</lat>
647 </wp>
648 <wp n="3">
649 <type type="string">navaid</type>
650 <ident type="string">TALAL</ident>
651 <lon type="double">11.085300</lon>
652 <lat type="double">49.108300</lat>
653 </wp>
654 <wp n="4">
655 <type type="string">navaid</type>
656 <ident type="string">ERMEL</ident>
657 <lon type="double">11.044700</lon>
658 <lat type="double">49.187800</lat>
659 </wp>
660 <wp n="5">
661 <type type="string">navaid</type>
662 <ident type="string">PSA</ident>
663 <lon type="double">9.348300</lon>
664 <lat type="double">49.862200</lat>
665 </wp>
666 </route>
667 </PropertyList>
668 )";
669
670
671 // The same test as above, but for a file exported from the route manager or online
672 void RouteManagerTests::loadFGFP()
673 {
674 auto rm = globals->get_subsystem<FGRouteMgr>();
675
676 FlightPlanRef f = FlightPlan::create();
677 rm->setFlightPlan(f);
678
679 SGPath fgfpPath = simgear::Dir::current().path() / "test_fgfp.fgfp";
680 {
681 sg_ofstream s(fgfpPath);
682 s << flightPlanXMLData;
683 }
684
685 CPPUNIT_ASSERT(f->load(fgfpPath));
686
687 auto eddm = FGAirport::getByIdent("EDDM");
688 auto eddf = FGAirport::getByIdent("EDDF");
689 CPPUNIT_ASSERT_EQUAL(eddm, f->departureAirport());
690 CPPUNIT_ASSERT_EQUAL(eddm->getRunwayByIdent("08R")->ident(), f->departureRunway()->ident());
691 CPPUNIT_ASSERT_EQUAL(7, f->numLegs());
692 CPPUNIT_ASSERT_EQUAL(eddf, f->destinationAirport());
693
694 auto wp1 = f->legAtIndex(1);
695 CPPUNIT_ASSERT_EQUAL(std::string{"GIVMI"}, wp1->waypoint()->ident());
696 auto wp2 = f->legAtIndex(6);
697 CPPUNIT_ASSERT_EQUAL(std::string{"EDDF"}, wp2->waypoint()->ident());
698
699 }
700
701 void RouteManagerTests::testRouteWithProcedures()
702 {
703 if (!static_haveProcedures)
704 return;
705
706 auto rm = globals->get_subsystem<FGRouteMgr>();
707
708 FlightPlanRef f = FlightPlan::create();
709 rm->setFlightPlan(f);
710
711 auto kjfk = FGAirport::findByIdent("KJFK");
712 auto eham = FGAirport::findByIdent("EHAM");
713 f->setDeparture(kjfk->getRunwayByIdent("13L"));
714 f->setSID(kjfk->findSIDWithIdent("DEEZZ5.13L"), "CANDR");
715
716 f->setDestination(eham->getRunwayByIdent("18R"));
717 f->setSTAR(eham->findSTARWithIdent("EEL1A"), "BEDUM");
718
719 f->setApproach(eham->findApproachWithIdent("VDM18R"));
720
721 auto w = f->waypointFromString("TOMYE");
722 f->insertWayptAtIndex(w, f->indexOfFirstNonDepartureWaypoint());
723
724 auto w2 = f->waypointFromString("DEVOL");
725 f->insertWayptAtIndex(w2, f->indexOfFirstArrivalWaypoint());
726
727 // let's check what we got
728
729 auto endOfSID = f->legAtIndex(f->indexOfFirstNonDepartureWaypoint() - 1);
730 CPPUNIT_ASSERT_EQUAL(endOfSID->waypoint()->ident(), "CANDR"s);
731
732 auto startOfSTAR = f->legAtIndex(f->indexOfFirstArrivalWaypoint());
733 CPPUNIT_ASSERT_EQUAL(startOfSTAR->waypoint()->ident(), "BEDUM"s);
734
735 auto endOfSTAR = f->legAtIndex(f->indexOfFirstApproachWaypoint() - 1);
736 CPPUNIT_ASSERT_EQUAL(endOfSTAR->waypoint()->ident(), "ARTIP"s);
737
738 auto startOfApproach = f->legAtIndex(f->indexOfFirstApproachWaypoint());
739 CPPUNIT_ASSERT_EQUAL(startOfApproach->waypoint()->ident(), "D070O"s);
740
741 auto landingRunway = f->legAtIndex(f->indexOfDestinationRunwayWaypoint());
742 CPPUNIT_ASSERT(landingRunway->waypoint()->source() == f->destinationRunway());
743
744 auto firstMiss = f->legAtIndex(f->indexOfDestinationRunwayWaypoint() + 1);
745 CPPUNIT_ASSERT_EQUAL(firstMiss->waypoint()->ident(), "(461)"s);
746
747 // check it in Nasal too
748 bool ok = FGTestApi::executeNasal(
749 R"(
750 var f = flightplan();
751 var depEnd = f.getWP(f.firstNonDepartureLeg - 1);
752 var firstArrival = f.getWP(f.firstArrivalLeg);
753 var firstApproach = f.getWP(f.firstApproachLeg);
754 var destRunway = f.getWP(f.destination_runway_leg);
755
756 unitTest.assert_equal(depEnd.id, 'CANDR');
757
758 var firstEnroute = f.getWP(f.firstNonDepartureLeg );
759 unitTest.assert_equal(firstEnroute.id, 'TOMYE');
760
761 unitTest.assert_equal(firstArrival.id, 'BEDUM');
762 unitTest.assert_equal(firstApproach.id, 'D070O');
763 unitTest.assert_equal(destRunway.id, '18R');
764 )");
765
766 CPPUNIT_ASSERT(ok);
767 }
768
769 void RouteManagerTests::testRouteWithApproachProcedures()
770 {
771 if (!static_haveProcedures)
772 return;
773
774 auto rm = globals->get_subsystem<FGRouteMgr>();
775
776 FlightPlanRef f = FlightPlan::create();
777 rm->setFlightPlan(f);
778
779 auto kjfk = FGAirport::findByIdent("KJFK");
780 auto eddm = FGAirport::findByIdent("EDDM");
781 f->setDeparture(kjfk->getRunwayByIdent("13L"));
782
783 f->setDestination(eddm->getRunwayByIdent("08R"));
784 f->setSTAR(eddm->findSTARWithIdent("ABGA3A.08R"));
785
786 f->setApproach(eddm->findApproachWithIdent("ILS08R"), "NAP08");
787
788 auto w = f->waypointFromString("TOMYE");
789 f->insertWayptAtIndex(w, f->indexOfFirstNonDepartureWaypoint());
790
791 auto w2 = f->waypointFromString("DEVOL");
792 f->insertWayptAtIndex(w2, f->indexOfFirstArrivalWaypoint());
793
794 // let's check what we got
795
796 auto startOfSTAR = f->legAtIndex(f->indexOfFirstArrivalWaypoint());
797 CPPUNIT_ASSERT_EQUAL(startOfSTAR->waypoint()->ident(), "ABGAS"s);
798
799 auto endOfSTAR = f->legAtIndex(f->indexOfFirstApproachWaypoint() - 1);
800 CPPUNIT_ASSERT_EQUAL(endOfSTAR->waypoint()->ident(), "MIQ"s);
801
802 auto startOfApproach = f->legAtIndex(f->indexOfFirstApproachWaypoint());
803 CPPUNIT_ASSERT_EQUAL(startOfApproach->waypoint()->ident(), "NAPSA"s);
804
805 auto startOfCoreApproach = f->legAtIndex(f->indexOfFirstApproachWaypoint() + 6);
806 CPPUNIT_ASSERT_EQUAL(startOfCoreApproach->waypoint()->ident(), "BEGEN"s);
807
808 auto landingRunway = f->legAtIndex(f->indexOfDestinationRunwayWaypoint());
809 CPPUNIT_ASSERT(landingRunway->waypoint()->source() == f->destinationRunway());
810
811
812 // // check it in Nasal too
813 // bool ok = FGTestApi::executeNasal(
814 // R"(
815 // var f = flightplan();
816 // var depEnd = f.getWP(f.firstNonDepartureLeg - 1);
817 // var firstArrival = f.getWP(f.firstArrivalLeg);
818 // var firstApproach = f.getWP(f.firstApproachLeg);
819 // var destRunway = f.getWP(f.destination_runway_leg);
820 //
821 // unitTest.assert_equal(depEnd.id, 'CANDR');
822 //
823 // var firstEnroute = f.getWP(f.firstNonDepartureLeg );
824 // unitTest.assert_equal(firstEnroute.id, 'TOMYE');
825 //
826 // unitTest.assert_equal(firstArrival.id, 'BEDUM');
827 // unitTest.assert_equal(firstApproach.id, 'D070O');
828 // unitTest.assert_equal(destRunway.id, '18R');
829 // )");
830 //
831 // CPPUNIT_ASSERT(ok);
832 }
833
834 void RouteManagerTests::testsSelectNavaid()
835 {
836 // this captures the issue at:
837 // https://sourceforge.net/p/flightgear/codetickets/2372/
838
839 auto rm = globals->get_subsystem<FGRouteMgr>();
840
841 FlightPlanRef f = FlightPlan::create();
842 rm->setFlightPlan(f);
843
844 auto usss = FGAirport::findByIdent("USSS");
845 auto eddh = FGAirport::findByIdent("EDDH");
846 f->setDeparture(usss); // Yekaterinberg
847 f->setDestination(eddh);
848
849 auto rmNode = globals->get_props()->getNode("autopilot/route-manager", true);
850 rmNode->setStringValue("input", "@INSERT1:UUDD");
851 rmNode->setStringValue("input", "@INSERT2:UKKM");
852 rmNode->setStringValue("input", "@INSERT2:IP");
853 rmNode->setStringValue("input", "@INSERT3:OD");
854
855 auto leg = f->legAtIndex(2);
856 auto wp1 = leg->waypoint();
857 CPPUNIT_ASSERT_EQUAL(wp1->ident(), "IP"s);
858 CPPUNIT_ASSERT_EQUAL(wp1->source()->name(), "ZAKHAROVKA NDB"s);
859
860 CPPUNIT_ASSERT_DOUBLES_EQUAL(227, leg->courseDeg(), 0.5);
861 CPPUNIT_ASSERT_DOUBLES_EQUAL(59, leg->distanceNm(), 0.5);
862
863 leg = f->legAtIndex(3);
864 auto wp2 = leg->waypoint();
865 CPPUNIT_ASSERT_EQUAL(wp2->ident(), "OD"s);
866 CPPUNIT_ASSERT_EQUAL(wp2->source()->name(), "BRYANSK NDB"s);
867 }
868
869 void RouteManagerTests::testsSelectWaypoint()
870 {
871 // another part of the issue at
872 // https://sourceforge.net/p/flightgear/codetickets/2372/
873
874 auto rm = globals->get_subsystem<FGRouteMgr>();
875
876 FlightPlanRef f = FlightPlan::create();
877 rm->setFlightPlan(f);
878
879 auto rmNode = globals->get_props()->getNode("autopilot/route-manager", true);
880 rmNode->setStringValue("input", "70N,015E");
881 rmNode->setStringValue("input", "55N,015E");
882 rmNode->setStringValue("input", "@INSERT1:OSS");
883 rmNode->setStringValue("input", "@INSERT2:BOR");
884
885 auto leg = f->legAtIndex(1);
886 auto wp1 = leg->waypoint();
887 CPPUNIT_ASSERT_EQUAL(wp1->ident(), "OSS"s);
888 CPPUNIT_ASSERT_EQUAL(wp1->source()->name(), "OSTERSUND VOR-DME"s);
889
890 // CPPUNIT_ASSERT_DOUBLES_EQUAL(227, leg->courseDeg(), 0.5);
891 // CPPUNIT_ASSERT_DOUBLES_EQUAL(59, leg->distanceNm(), 0.5);
892
893 leg = f->legAtIndex(2);
894 auto wp2 = leg->waypoint();
895 CPPUNIT_ASSERT_EQUAL(wp2->ident(), "BOR"s);
896 CPPUNIT_ASSERT_EQUAL(wp2->source()->name(), "BORLANGE VOR-DME"s);
897 }
898
899 void RouteManagerTests::testCommandAPI()
900 {
901 auto rm = globals->get_subsystem<FGRouteMgr>();
902 SGPath fgfpPath = simgear::Dir::current().path() / "test_fgfp_2.fgfp";
903 {
904 sg_ofstream s(fgfpPath);
905 s << flightPlanXMLData;
906 }
907
908 {
909 SGPropertyNode_ptr args(new SGPropertyNode);
910 args->setStringValue("path", fgfpPath.utf8Str());
911 CPPUNIT_ASSERT(globals->get_commands()->execute("load-flightplan", args));
912 }
913
914 auto f = rm->flightPlan();
915 CPPUNIT_ASSERT_EQUAL(7, f->numLegs());
916
917 CPPUNIT_ASSERT(!f->isActive());
918
919 {
920 SGPropertyNode_ptr args(new SGPropertyNode);
921 CPPUNIT_ASSERT(globals->get_commands()->execute("activate-flightplan", args));
922 }
923
924 CPPUNIT_ASSERT(f->isActive());
925
926 {
927 SGPropertyNode_ptr args(new SGPropertyNode);
928 args->setIntValue("index", 3);
929 CPPUNIT_ASSERT(globals->get_commands()->execute("set-active-waypt", args));
930 }
931
932 CPPUNIT_ASSERT_EQUAL(3, f->currentIndex());
933
934 {
935 SGPropertyNode_ptr args(new SGPropertyNode);
936 args->setIntValue("index", 4);
937 args->setStringValue("navaid", "WLD");
938
939 // let's build an offset waypoint for fun
940 args->setDoubleValue("offset-nm", 10.0);
941 args->setDoubleValue("radial", 30);
942 CPPUNIT_ASSERT(globals->get_commands()->execute("insert-waypt", args));
943 }
944
945 auto waldaWpt = f->legAtIndex(4)->waypoint();
946 auto waldaVOR = waldaWpt->source();
947 CPPUNIT_ASSERT_EQUAL("WALDA VOR-DME"s, waldaVOR->name());
948
949 auto d = SGGeodesy::distanceNm(waldaVOR->geod(), waldaWpt->position());
950 CPPUNIT_ASSERT_DOUBLES_EQUAL(10.0, d, 0.1);
951 }
952
953 void RouteManagerTests::testRMBug2616()
954 {
955 auto edty = FGAirport::findByIdent("EDTY"s);
956 edty->testSuiteInjectProceduresXML(SGPath::fromUtf8(FG_TEST_SUITE_DATA) / "EDTY.procedures.xml");
957
958 auto ils28Approach = edty->findApproachWithIdent("ILS28"s);
959 CPPUNIT_ASSERT(ils28Approach);
960
961 auto rm = globals->get_subsystem<FGRouteMgr>();
962 auto f = rm->flightPlan();
963 f->clearLegs();
964
965 auto edds = FGAirport::findByIdent("EDDS");
966 f->setDeparture(edds->getRunwayByIdent("25"));
967 f->setDestination(edty->getRunwayByIdent("28"));
968 f->setApproach(ils28Approach);
969
970 CPPUNIT_ASSERT(f->destinationRunway()->ident() == "28"s);
971 CPPUNIT_ASSERT(f->approach()->ident() == "ILS28"s);
972
973
974 rm->activate();
975 CPPUNIT_ASSERT(f->isActive());
976 }
977
978 void RouteManagerTests::testsSelectWaypoint2()
979 {
980 auto rm = globals->get_subsystem<FGRouteMgr>();
981 FlightPlanRef f = FlightPlan::create();
982 rm->setFlightPlan(f);
983 auto rmNode = globals->get_props()->getNode("autopilot/route-manager", true);
984 rmNode->setStringValue("input", "UAAA");
985 rmNode->setStringValue("input", "EDDH");
986 rmNode->setStringValue("input", "@INSERT1:ALM");
987 auto leg = f->legAtIndex(1);
988 auto wp1 = leg->waypoint();
989 CPPUNIT_ASSERT_EQUAL("ALM"s, wp1->ident());
990 // CPPUNIT_ASSERT_EQUAL(wp1->source()->name(), string{"ALMATY VOR-DME"});
991 }
992
993 void RouteManagerTests::testAppendWaypoint() {
994 // https://sourceforge.net/p/flightgear/codetickets/2829/
995
996 auto rm = globals->get_subsystem<FGRouteMgr>();
997 FlightPlanRef f = FlightPlan::create();
998 rm->setFlightPlan(f);
999 auto rmNode = globals->get_props()->getNode("autopilot/route-manager", true);
1000 rmNode->setStringValue("input", "LFOH");
1001 rmNode->setStringValue("input", "NK");
1002 rmNode->setStringValue("input", "@INSERT2:NK");
1003
1004 CPPUNIT_ASSERT_EQUAL("NK"s, f->legAtIndex(1)->waypoint()->source()->name());
1005 CPPUNIT_ASSERT_EQUAL("NK"s, f->legAtIndex(2)->waypoint()->source()->name());
1006 }