| 1 | /* |
| 2 | * Copyright (C) 2019 James Turner |
| 3 | * |
| 4 | * This file is part of the program FlightGear. |
| 5 | * |
| 6 | * This program is free software: you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation, either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
| 20 | #include "test_rnav_procedures.hxx" |
| 21 | |
| 22 | #include <memory> |
| 23 | #include <cstring> |
| 24 | |
| 25 | #include "test_suite/FGTestApi/testGlobals.hxx" |
| 26 | #include "test_suite/FGTestApi/NavDataCache.hxx" |
| 27 | #include "test_suite/FGTestApi/TestPilot.hxx" |
| 28 | |
| 29 | #include <simgear/structure/exception.hxx> |
| 30 | |
| 31 | #include <Airports/airport.hxx> |
| 32 | #include <Navaids/NavDataCache.hxx> |
| 33 | #include <Navaids/navrecord.hxx> |
| 34 | #include <Navaids/navlist.hxx> |
| 35 | #include <Navaids/FlightPlan.hxx> |
| 36 | |
| 37 | #include <Instrumentation/gps.hxx> |
| 38 | |
| 39 | #include <Autopilot/route_mgr.hxx> |
| 40 | #include <Main/globals.hxx> |
| 41 | |
| 42 | using namespace flightgear; |
| 43 | |
| 44 | ///////////////////////////////////////////////////////////////////////////// |
| 45 | |
| 46 | namespace { |
| 47 | class TestFPDelegate : public FlightPlan::Delegate |
| 48 | { |
| 49 | public: |
| 50 | FlightPlanRef thePlan; |
| 51 | int sequenceCount = 0; |
| 52 | |
| 53 | virtual ~TestFPDelegate() |
| 54 | { |
| 55 | } |
| 56 | |
| 57 | void sequence() override |
| 58 | { |
| 59 | |
| 60 | ++sequenceCount; |
| 61 | int newIndex = thePlan->currentIndex() + 1; |
| 62 | if (newIndex >= thePlan->numLegs()) { |
| 63 | thePlan->finish(); |
| 64 | return; |
| 65 | } |
| 66 | |
| 67 | thePlan->setCurrentIndex(newIndex); |
| 68 | } |
| 69 | |
| 70 | void currentWaypointChanged() override |
| 71 | { |
| 72 | } |
| 73 | |
| 74 | void departureChanged() override |
| 75 | { |
| 76 | // mimic the default delegate, inserting the SID waypoints |
| 77 | |
| 78 | // clear anything existing |
| 79 | thePlan->clearWayptsWithFlag(WPT_DEPARTURE); |
| 80 | |
| 81 | // insert waypt for the dpearture runway |
| 82 | auto dr = new RunwayWaypt(thePlan->departureRunway(), thePlan); |
| 83 | dr->setFlag(WPT_DEPARTURE); |
| 84 | dr->setFlag(WPT_GENERATED); |
| 85 | thePlan->insertWayptAtIndex(dr, 0); |
| 86 | |
| 87 | if (thePlan->sid()) { |
| 88 | WayptVec sidRoute; |
| 89 | bool ok = thePlan->sid()->route(thePlan->departureRunway(), thePlan->sidTransition(), sidRoute); |
| 90 | if (!ok) |
| 91 | throw sg_exception("failed to route via SID"); |
| 92 | int insertIndex = 1; |
| 93 | for (auto w : sidRoute) { |
| 94 | w->setFlag(WPT_DEPARTURE); |
| 95 | w->setFlag(WPT_GENERATED); |
| 96 | thePlan->insertWayptAtIndex(w, insertIndex++); |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | void arrivalChanged() override |
| 102 | { |
| 103 | // mimic the default delegate, inserting the STAR waypoints |
| 104 | |
| 105 | // clear anything existing |
| 106 | thePlan->clearWayptsWithFlag(WPT_ARRIVAL); |
| 107 | |
| 108 | // insert waypt for the destination runway |
| 109 | auto dr = new RunwayWaypt(thePlan->destinationRunway(), thePlan); |
| 110 | dr->setFlag(WPT_ARRIVAL); |
| 111 | dr->setFlag(WPT_GENERATED); |
| 112 | auto leg = thePlan->insertWayptAtIndex(dr, -1); |
| 113 | |
| 114 | if (thePlan->star()) { |
| 115 | WayptVec starRoute; |
| 116 | bool ok = thePlan->star()->route(thePlan->destinationRunway(), thePlan->starTransition(), starRoute); |
| 117 | if (!ok) |
| 118 | throw sg_exception("failed to route via STAR"); |
| 119 | int insertIndex = leg->index(); |
| 120 | for (auto w : starRoute) { |
| 121 | w->setFlag(WPT_ARRIVAL); |
| 122 | w->setFlag(WPT_GENERATED); |
| 123 | thePlan->insertWayptAtIndex(w, insertIndex++); |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | }; |
| 128 | |
| 129 | } // of anonymous namespace |
| 130 | |
| 131 | ///////////////////////////////////////////////////////////////////////////// |
| 132 | |
| 133 | // Set up function for each test. |
| 134 | void RNAVProcedureTests::setUp() |
| 135 | { |
| 136 | FGTestApi::setUp::initTestGlobals("rnav-procedures"); |
| 137 | FGTestApi::setUp::initNavDataCache(); |
| 138 | |
| 139 | globals->get_subsystem_mgr()->bind(); |
| 140 | globals->get_subsystem_mgr()->init(); |
| 141 | |
| 142 | SGPath proceduresPath = SGPath::fromEnv("FG_PROCEDURES_PATH"); |
| 143 | if (proceduresPath.exists()) { |
| 144 | globals->append_fg_scenery(proceduresPath); |
| 145 | } |
| 146 | |
| 147 | setupRouteManager(); |
| 148 | } |
| 149 | |
| 150 | // Clean up after each test. |
| 151 | void RNAVProcedureTests::tearDown() |
| 152 | { |
| 153 | FGTestApi::tearDown::shutdownTestGlobals(); |
| 154 | } |
| 155 | |
| 156 | GPS* RNAVProcedureTests::setupStandardGPS(SGPropertyNode_ptr config, |
| 157 | const std::string name, const int index) |
| 158 | { |
| 159 | SGPropertyNode_ptr configNode(config.valid() ? config |
| 160 | : SGPropertyNode_ptr{new SGPropertyNode}); |
| 161 | configNode->setStringValue("name", name); |
| 162 | configNode->setIntValue("number", index); |
| 163 | |
| 164 | GPS* gps(new GPS(configNode)); |
| 165 | m_gps = gps; |
| 166 | |
| 167 | m_gpsNode = globals->get_props()->getNode("instrumentation", true)->getChild(name, index, true); |
| 168 | m_gpsNode->setBoolValue("serviceable", true); |
| 169 | globals->get_props()->setDoubleValue("systems/electrical/outputs/gps", 6.0); |
| 170 | |
| 171 | gps->bind(); |
| 172 | gps->init(); |
| 173 | |
| 174 | globals->get_subsystem_mgr()->add("gps", gps); |
| 175 | return gps; |
| 176 | } |
| 177 | |
| 178 | void RNAVProcedureTests::setupRouteManager() |
| 179 | { |
| 180 | auto rm = globals->get_subsystem_mgr()->add<FGRouteMgr>(); |
| 181 | rm->bind(); |
| 182 | rm->init(); |
| 183 | rm->postinit(); |
| 184 | } |
| 185 | |
| 186 | void RNAVProcedureTests::setPositionAndStabilise(const SGGeod& g) |
| 187 | { |
| 188 | FGTestApi::setPosition(g); |
| 189 | for (int i=0; i<60; ++i) { |
| 190 | m_gps->update(0.015); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | ///////////////////////////////////////////////////////////////////////////// |
| 195 | |
| 196 | #if 0 |
| 197 | void RNAVProcedureTests::testBasic() |
| 198 | { |
| 199 | setupStandardGPS(); |
| 200 | |
| 201 | FGPositioned::TypeFilter f{FGPositioned::VOR}; |
| 202 | auto bodrumVOR = fgpositioned_cast<FGNavRecord>(FGPositioned::findClosestWithIdent("BDR", SGGeod::fromDeg(27.6, 37), &f)); |
| 203 | SGGeod p1 = SGGeodesy::direct(bodrumVOR->geod(), 45.0, 5.0 * SG_NM_TO_METER); |
| 204 | |
| 205 | FGTestApi::setPositionAndStabilise(p1); |
| 206 | |
| 207 | |
| 208 | } |
| 209 | #endif |
| 210 | |
| 211 | void RNAVProcedureTests::testHeadingToAlt() |
| 212 | { |
| 213 | auto vhhh = FGAirport::findByIdent("VHHH"); |
| 214 | // FGTestApi::setUp::logPositionToKML("heading_to_alt"); |
| 215 | |
| 216 | auto rm = globals->get_subsystem<FGRouteMgr>(); |
| 217 | auto fp = FlightPlan::create(); |
| 218 | |
| 219 | auto testDelegate = new TestFPDelegate; |
| 220 | testDelegate->thePlan = fp; |
| 221 | fp->addDelegate(testDelegate); |
| 222 | |
| 223 | rm->setFlightPlan(fp); |
| 224 | |
| 225 | // we don't have Nasal, but our delegate does the same work |
| 226 | FGTestApi::setUp::populateFPWithNasal(fp, "VHHH", "25R", "EGLL", "27R", "HAZEL"); |
| 227 | |
| 228 | auto wp = new HeadingToAltitude(fp, "TO_4000", 270); |
| 229 | wp->setAltitude(4000, RESTRICT_ABOVE); |
| 230 | fp->insertWayptAtIndex(wp, 1); // between the runway WP and HAZEL |
| 231 | |
| 232 | // FGTestApi::writeFlightPlanToKML(fp); |
| 233 | |
| 234 | auto depRwy = fp->departureRunway(); |
| 235 | |
| 236 | setupStandardGPS(); |
| 237 | setPositionAndStabilise(fp->departureRunway()->pointOnCenterline(0.0)); |
| 238 | |
| 239 | fp->activate(); |
| 240 | m_gpsNode->setStringValue("command", "leg"); |
| 241 | |
| 242 | CPPUNIT_ASSERT_EQUAL(fp->currentIndex(), 0); |
| 243 | |
| 244 | auto pilot = SGSharedPtr<FGTestApi::TestPilot>(new FGTestApi::TestPilot); |
| 245 | pilot->resetAtPosition(depRwy->pointOnCenterline(0.0)); |
| 246 | pilot->setCourseTrue(depRwy->headingDeg()); |
| 247 | pilot->setSpeedKts(200); |
| 248 | pilot->flyGPSCourse(m_gps); |
| 249 | |
| 250 | CPPUNIT_ASSERT_EQUAL(fp->currentIndex(), 0); |
| 251 | |
| 252 | // check we sequence to the heading-to-alt wp |
| 253 | bool ok = FGTestApi::runForTimeWithCheck(300.0, [fp] () { |
| 254 | if (fp->currentIndex() == 1) { |
| 255 | return true; |
| 256 | } |
| 257 | return false; |
| 258 | }); |
| 259 | CPPUNIT_ASSERT(ok); |
| 260 | |
| 261 | // leisurely climb out |
| 262 | pilot->setVerticalFPM(1800); |
| 263 | pilot->setTargetAltitudeFtMSL(8000); |
| 264 | |
| 265 | CPPUNIT_ASSERT_EQUAL(std::string{"VHHH-25R"}, std::string{m_gpsNode->getStringValue("wp/wp[0]/ID")}); |
| 266 | CPPUNIT_ASSERT_EQUAL(std::string{"TO_4000"}, std::string{m_gpsNode->getStringValue("wp/wp[1]/ID")}); |
| 267 | |
| 268 | CPPUNIT_ASSERT_DOUBLES_EQUAL(270.0, m_gpsNode->getDoubleValue("wp/wp[1]/bearing-true-deg"), 0.5); |
| 269 | CPPUNIT_ASSERT_DOUBLES_EQUAL(270.0, m_gpsNode->getDoubleValue("wp/leg-true-course-deg"), 0.5); |
| 270 | CPPUNIT_ASSERT_DOUBLES_EQUAL(0.0, m_gpsNode->getDoubleValue("wp/wp[1]/course-error-nm"), 0.05); |
| 271 | CPPUNIT_ASSERT_DOUBLES_EQUAL(0.0, m_gpsNode->getDoubleValue("wp/wp[1]/course-deviation-deg"), 0.5); |
| 272 | |
| 273 | // fly until we're turned to to heading |
| 274 | ok = FGTestApi::runForTimeWithCheck(20, [pilot] () { |
| 275 | return pilot->isOnHeading(270.); |
| 276 | }); |
| 277 | CPPUNIT_ASSERT(ok); |
| 278 | |
| 279 | // capture the position now |
| 280 | SGGeod posAtHdgAltStart = globals->get_aircraft_position(); |
| 281 | FGTestApi::runForTime(40.0); |
| 282 | |
| 283 | CPPUNIT_ASSERT_DOUBLES_EQUAL(270.0, m_gpsNode->getDoubleValue("wp/wp[1]/bearing-true-deg"), 0.5); |
| 284 | CPPUNIT_ASSERT_DOUBLES_EQUAL(270.0, m_gpsNode->getDoubleValue("wp/leg-true-course-deg"), 0.5); |
| 285 | CPPUNIT_ASSERT_DOUBLES_EQUAL(0.0, m_gpsNode->getDoubleValue("wp/wp[1]/course-error-nm"), 0.05); |
| 286 | CPPUNIT_ASSERT_DOUBLES_EQUAL(0.0, m_gpsNode->getDoubleValue("wp/wp[1]/course-deviation-deg"), 0.5); |
| 287 | |
| 288 | const double crs = SGGeodesy::courseDeg(posAtHdgAltStart, globals->get_aircraft_position()); |
| 289 | CPPUNIT_ASSERT_DOUBLES_EQUAL(270.0, crs, 1.0); |
| 290 | |
| 291 | ok = FGTestApi::runForTimeWithCheck(180.0, [fp] () { |
| 292 | return (fp->currentIndex() == 2); |
| 293 | }); |
| 294 | CPPUNIT_ASSERT(ok); |
| 295 | |
| 296 | CPPUNIT_ASSERT_DOUBLES_EQUAL(4000.0, globals->get_aircraft_position().getElevationFt(), 100.0); |
| 297 | |
| 298 | FGTestApi::runForTime(40.0); |
| 299 | } |
| 300 | |
| 301 | // ugly version: the heading to hold will be very mis-aligned with |
| 302 | // the course when the leg is sequenced. (more than 90 degrees turn( |
| 303 | void RNAVProcedureTests::testUglyHeadingToAlt() |
| 304 | { |
| 305 | auto vhhh = FGAirport::findByIdent("VHHH"); |
| 306 | // FGTestApi::setUp::logPositionToKML("heading_to_alt_ugly"); |
| 307 | |
| 308 | auto rm = globals->get_subsystem<FGRouteMgr>(); |
| 309 | auto fp = FlightPlan::create(); |
| 310 | |
| 311 | auto testDelegate = new TestFPDelegate; |
| 312 | testDelegate->thePlan = fp; |
| 313 | fp->addDelegate(testDelegate); |
| 314 | |
| 315 | rm->setFlightPlan(fp); |
| 316 | |
| 317 | // we don't have Nasal, but our delegate does the same work |
| 318 | FGTestApi::setUp::populateFPWithNasal(fp, "VHHH", "07L", "EGLL", "27R", "HAZEL"); |
| 319 | |
| 320 | auto wp = new HeadingToAltitude(fp, "TO_4000", 210); |
| 321 | wp->setAltitude(4000, RESTRICT_ABOVE); |
| 322 | fp->insertWayptAtIndex(wp, 1); // between the runway WP and HAZEL |
| 323 | |
| 324 | // FGTestApi::writeFlightPlanToKML(fp); |
| 325 | |
| 326 | auto depRwy = fp->departureRunway(); |
| 327 | |
| 328 | setupStandardGPS(); |
| 329 | setPositionAndStabilise(fp->departureRunway()->pointOnCenterline(0.0)); |
| 330 | |
| 331 | fp->activate(); |
| 332 | m_gpsNode->setStringValue("command", "leg"); |
| 333 | |
| 334 | CPPUNIT_ASSERT_EQUAL(fp->currentIndex(), 0); |
| 335 | |
| 336 | auto pilot = SGSharedPtr<FGTestApi::TestPilot>(new FGTestApi::TestPilot); |
| 337 | pilot->resetAtPosition(depRwy->pointOnCenterline(0.0)); |
| 338 | pilot->setCourseTrue(depRwy->headingDeg()); |
| 339 | pilot->setSpeedKts(200); |
| 340 | pilot->flyGPSCourse(m_gps); |
| 341 | |
| 342 | // check we sequence to the heading-to-alt wp |
| 343 | bool ok = FGTestApi::runForTimeWithCheck(240.0, [fp] () { |
| 344 | if (fp->currentIndex() == 1) { |
| 345 | return true; |
| 346 | } |
| 347 | return false; |
| 348 | }); |
| 349 | CPPUNIT_ASSERT(ok); |
| 350 | |
| 351 | // leisurely climb out |
| 352 | pilot->setVerticalFPM(1800); |
| 353 | pilot->setTargetAltitudeFtMSL(8000); |
| 354 | |
| 355 | CPPUNIT_ASSERT_EQUAL(std::string{"VHHH-07L"}, std::string{m_gpsNode->getStringValue("wp/wp[0]/ID")}); |
| 356 | CPPUNIT_ASSERT_EQUAL(std::string{"TO_4000"}, std::string{m_gpsNode->getStringValue("wp/wp[1]/ID")}); |
| 357 | |
| 358 | CPPUNIT_ASSERT_DOUBLES_EQUAL(210.0, m_gpsNode->getDoubleValue("wp/wp[1]/bearing-true-deg"), 0.5); |
| 359 | CPPUNIT_ASSERT_DOUBLES_EQUAL(210.0, m_gpsNode->getDoubleValue("wp/leg-true-course-deg"), 0.5); |
| 360 | CPPUNIT_ASSERT_DOUBLES_EQUAL(0.0, m_gpsNode->getDoubleValue("wp/wp[1]/course-error-nm"), 0.05); |
| 361 | CPPUNIT_ASSERT_DOUBLES_EQUAL(0.0, m_gpsNode->getDoubleValue("wp/wp[1]/course-deviation-deg"), 0.5); |
| 362 | |
| 363 | // fly until we're turned to to heading |
| 364 | ok = FGTestApi::runForTimeWithCheck(120, [pilot] () { |
| 365 | return pilot->isOnHeading(210.0); |
| 366 | }); |
| 367 | CPPUNIT_ASSERT(ok); |
| 368 | |
| 369 | // capture the position now |
| 370 | SGGeod posAtHdgAltStart = globals->get_aircraft_position(); |
| 371 | FGTestApi::runForTime(40.0); |
| 372 | |
| 373 | CPPUNIT_ASSERT_DOUBLES_EQUAL(210.0, m_gpsNode->getDoubleValue("wp/wp[1]/bearing-true-deg"), 0.5); |
| 374 | CPPUNIT_ASSERT_DOUBLES_EQUAL(210.0, m_gpsNode->getDoubleValue("wp/leg-true-course-deg"), 0.5); |
| 375 | CPPUNIT_ASSERT_DOUBLES_EQUAL(0.0, m_gpsNode->getDoubleValue("wp/wp[1]/course-error-nm"), 0.05); |
| 376 | CPPUNIT_ASSERT_DOUBLES_EQUAL(0.0, m_gpsNode->getDoubleValue("wp/wp[1]/course-deviation-deg"), 0.5); |
| 377 | |
| 378 | const double crs = SGGeodesy::courseDeg(posAtHdgAltStart, globals->get_aircraft_position()); |
| 379 | CPPUNIT_ASSERT_DOUBLES_EQUAL(210.0, crs, 1.0); |
| 380 | |
| 381 | ok = FGTestApi::runForTimeWithCheck(180.0, [fp] () { |
| 382 | return (fp->currentIndex() == 2); |
| 383 | }); |
| 384 | CPPUNIT_ASSERT(ok); |
| 385 | |
| 386 | CPPUNIT_ASSERT_DOUBLES_EQUAL(4000.0, globals->get_aircraft_position().getElevationFt(), 100.0); |
| 387 | |
| 388 | FGTestApi::runForTime(40.0); |
| 389 | } |
| 390 | |
| 391 | |
| 392 | void RNAVProcedureTests::testEGPH_TLA6C() |
| 393 | { |
| 394 | |
| 395 | auto egph = FGAirport::findByIdent("EGPH"); |
| 396 | |
| 397 | auto sid = egph->findSIDWithIdent("TLA6C"); |
| 398 | // procedures not loaded, abandon test |
| 399 | if (!sid) |
| 400 | return; |
| 401 | |
| 402 | // FGTestApi::setUp::logPositionToKML("procedure_egph_tla6c"); |
| 403 | |
| 404 | auto rm = globals->get_subsystem<FGRouteMgr>(); |
| 405 | auto fp = FlightPlan::create(); |
| 406 | |
| 407 | auto testDelegate = new TestFPDelegate; |
| 408 | testDelegate->thePlan = fp; |
| 409 | fp->addDelegate(testDelegate); |
| 410 | |
| 411 | rm->setFlightPlan(fp); |
| 412 | FGTestApi::setUp::populateFPWithNasal(fp, "EGPH", "24", "EGLL", "27R", "DCS POL DTY"); |
| 413 | |
| 414 | fp->setSID(sid); |
| 415 | |
| 416 | FGRunwayRef departureRunway = fp->departureRunway(); |
| 417 | CPPUNIT_ASSERT_EQUAL(std::string{"24"}, fp->legAtIndex(0)->waypoint()->source()->name()); |
| 418 | |
| 419 | CPPUNIT_ASSERT_EQUAL(std::string{"UW"}, fp->legAtIndex(1)->waypoint()->ident()); |
| 420 | |
| 421 | auto d242Wpt = fp->legAtIndex(2)->waypoint(); |
| 422 | CPPUNIT_ASSERT_EQUAL(std::string{"D242H"}, d242Wpt->ident()); |
| 423 | CPPUNIT_ASSERT_EQUAL(true, d242Wpt->flag(WPT_OVERFLIGHT)); |
| 424 | |
| 425 | const auto wp3Ident = fp->legAtIndex(3)->waypoint()->ident(); |
| 426 | |
| 427 | // depeding which versino fo the procedures we loaded, we can find |
| 428 | // one ID or the other |
| 429 | CPPUNIT_ASSERT((wp3Ident == "D346T") || (wp3Ident == "D345T")); |
| 430 | |
| 431 | // FGTestApi::writeFlightPlanToKML(fp); |
| 432 | |
| 433 | CPPUNIT_ASSERT(rm->activate()); |
| 434 | |
| 435 | |
| 436 | setupStandardGPS(); |
| 437 | |
| 438 | FGTestApi::setPositionAndStabilise(departureRunway->threshold()); |
| 439 | m_gpsNode->setStringValue("command", "leg"); |
| 440 | |
| 441 | auto pilot = SGSharedPtr<FGTestApi::TestPilot>(new FGTestApi::TestPilot); |
| 442 | pilot->resetAtPosition(globals->get_aircraft_position()); |
| 443 | CPPUNIT_ASSERT_DOUBLES_EQUAL(departureRunway->headingDeg(), m_gpsNode->getDoubleValue("wp/leg-true-course-deg"), 0.5); |
| 444 | pilot->setCourseTrue(m_gpsNode->getDoubleValue("wp/leg-true-course-deg")); |
| 445 | pilot->setSpeedKts(220); |
| 446 | pilot->flyGPSCourse(m_gps); |
| 447 | |
| 448 | FGTestApi::runForTime(20.0); |
| 449 | // check we're somewhere along the runway, on the centerline |
| 450 | // and still on waypoint zero |
| 451 | |
| 452 | bool ok = FGTestApi::runForTimeWithCheck(180.0, [fp] () { |
| 453 | if (fp->currentIndex() == 1) { |
| 454 | return true; |
| 455 | } |
| 456 | return false; |
| 457 | }); |
| 458 | CPPUNIT_ASSERT(ok); |
| 459 | |
| 460 | // check what we sequenced to |
| 461 | double elapsed = globals->get_sim_time_sec(); |
| 462 | ok = FGTestApi::runForTimeWithCheck(180.0, [fp] () { |
| 463 | if (fp->currentIndex() == 2) { |
| 464 | return true; |
| 465 | } |
| 466 | return false; |
| 467 | }); |
| 468 | CPPUNIT_ASSERT(ok); |
| 469 | CPPUNIT_ASSERT_DOUBLES_EQUAL(0.0, m_gpsNode->getDoubleValue("wp/wp[1]/course-error-nm"), 0.05); |
| 470 | |
| 471 | elapsed = globals->get_sim_time_sec(); |
| 472 | |
| 473 | ok = FGTestApi::runForTimeWithCheck(180.0, [fp] () { |
| 474 | if (fp->currentIndex() == 3) { |
| 475 | return true; |
| 476 | } |
| 477 | return false; |
| 478 | }); |
| 479 | CPPUNIT_ASSERT(ok); |
| 480 | CPPUNIT_ASSERT_DOUBLES_EQUAL(0.0, m_gpsNode->getDoubleValue("wp/wp[1]/course-error-nm"), 0.05); |
| 481 | |
| 482 | elapsed = globals->get_sim_time_sec(); |
| 483 | |
| 484 | ok = FGTestApi::runForTimeWithCheck(180.0, [fp] () { |
| 485 | if (fp->currentIndex() == 4) { |
| 486 | return true; |
| 487 | } |
| 488 | return false; |
| 489 | }); |
| 490 | CPPUNIT_ASSERT(ok); |
| 491 | CPPUNIT_ASSERT_DOUBLES_EQUAL(0.0, m_gpsNode->getDoubleValue("wp/wp[1]/course-error-nm"), 0.05); |
| 492 | |
| 493 | ok = FGTestApi::runForTimeWithCheck(180.0, [fp] () { |
| 494 | if (fp->currentIndex() == 5) { |
| 495 | return true; |
| 496 | } |
| 497 | return false; |
| 498 | }); |
| 499 | |
| 500 | CPPUNIT_ASSERT(ok); |
| 501 | |
| 502 | CPPUNIT_ASSERT_EQUAL(std::string{"TLA"}, fp->legAtIndex(5)->waypoint()->ident()); |
| 503 | CPPUNIT_ASSERT_EQUAL(std::string{"TLA"}, std::string{m_gpsNode->getStringValue("wp/wp[1]/ID")}); |
| 504 | |
| 505 | SG_UNUSED(elapsed); |
| 506 | } |
| 507 | |
| 508 | void RNAVProcedureTests::testLFKC_AJO1R() |
| 509 | { |
| 510 | auto lfkc = FGAirport::findByIdent("LFKC"); |
| 511 | auto sid = lfkc->findSIDWithIdent("AJO1R"); |
| 512 | // procedures not loaded, abandon test |
| 513 | if (!sid) |
| 514 | return; |
| 515 | |
| 516 | // FGTestApi::setUp::logPositionToKML("procedure_LFKC_AJO1R"); |
| 517 | |
| 518 | auto rm = globals->get_subsystem<FGRouteMgr>(); |
| 519 | auto fp = FlightPlan::create(); |
| 520 | |
| 521 | auto testDelegate = new TestFPDelegate; |
| 522 | testDelegate->thePlan = fp; |
| 523 | fp->addDelegate(testDelegate); |
| 524 | |
| 525 | rm->setFlightPlan(fp); |
| 526 | FGTestApi::setUp::populateFPWithNasal(fp, "LFKC", "36", "EGLL", "27R", ""); |
| 527 | |
| 528 | fp->setSID(sid); |
| 529 | |
| 530 | CPPUNIT_ASSERT_EQUAL(std::string{"BEBEV"}, fp->legAtIndex(4)->waypoint()->ident()); |
| 531 | CPPUNIT_ASSERT_EQUAL(std::string{"AJO"}, fp->legAtIndex(5)->waypoint()->ident()); |
| 532 | double d = fp->legAtIndex(5)->distanceAlongRoute(); |
| 533 | CPPUNIT_ASSERT_DOUBLES_EQUAL(72, d, 1.0); // ensure the route didn't blow up to 0,0 |
| 534 | |
| 535 | FGRunwayRef departureRunway = fp->departureRunway(); |
| 536 | // FGTestApi::writeFlightPlanToKML(fp); |
| 537 | |
| 538 | CPPUNIT_ASSERT(rm->activate()); |
| 539 | |
| 540 | setupStandardGPS(); |
| 541 | |
| 542 | FGTestApi::setPositionAndStabilise(departureRunway->threshold()); |
| 543 | m_gpsNode->setStringValue("command", "leg"); |
| 544 | |
| 545 | auto pilot = SGSharedPtr<FGTestApi::TestPilot>(new FGTestApi::TestPilot); |
| 546 | pilot->resetAtPosition(globals->get_aircraft_position()); |
| 547 | CPPUNIT_ASSERT_DOUBLES_EQUAL(departureRunway->headingDeg(), m_gpsNode->getDoubleValue("wp/leg-true-course-deg"), 0.5); |
| 548 | pilot->setCourseTrue(m_gpsNode->getDoubleValue("wp/leg-true-course-deg")); |
| 549 | pilot->setSpeedKts(220); |
| 550 | pilot->flyGPSCourse(m_gps); |
| 551 | |
| 552 | FGTestApi::runForTime(20.0); |
| 553 | // check we're somewhere along the runway, on the centerline |
| 554 | // and still on waypoint zero |
| 555 | |
| 556 | bool ok = FGTestApi::runForTimeWithCheck(180.0, [fp] () { |
| 557 | if (fp->currentIndex() == 1) { |
| 558 | return true; |
| 559 | } |
| 560 | return false; |
| 561 | }); |
| 562 | CPPUNIT_ASSERT(ok); |
| 563 | } |
| 564 | |
| 565 | void RNAVProcedureTests::testTransitionsSID() |
| 566 | { |
| 567 | auto kjfk = FGAirport::findByIdent("kjfk"); |
| 568 | auto runway = kjfk->getRunwayByIdent("13L"); |
| 569 | |
| 570 | |
| 571 | auto sid = kjfk->selectSIDByTransition(runway, "CANDR"); |
| 572 | // procedures not loaded, abandon test |
| 573 | if (!sid) |
| 574 | return; |
| 575 | |
| 576 | auto rm = globals->get_subsystem<FGRouteMgr>(); |
| 577 | auto fp = FlightPlan::create(); |
| 578 | |
| 579 | auto testDelegate = new TestFPDelegate; |
| 580 | testDelegate->thePlan = fp; |
| 581 | fp->addDelegate(testDelegate); |
| 582 | |
| 583 | rm->setFlightPlan(fp); |
| 584 | FGTestApi::setUp::populateFPWithNasal(fp, "KJFK", "13L", "KCLE", "24R", ""); |
| 585 | |
| 586 | fp->setSID(sid); |
| 587 | CPPUNIT_ASSERT_EQUAL(8, fp->numLegs()); |
| 588 | auto wp = fp->legAtIndex(6); |
| 589 | CPPUNIT_ASSERT_EQUAL(std::string{"CANDR"}, wp->waypoint()->ident()); |
| 590 | CPPUNIT_ASSERT(rm->activate()); |
| 591 | } |
| 592 | |
| 593 | void RNAVProcedureTests::testTransitionsSTAR() |
| 594 | { |
| 595 | auto kjfk = FGAirport::findByIdent("kjfk"); |
| 596 | auto runway = kjfk->getRunwayByIdent("22L"); |
| 597 | auto star = kjfk->selectSTARByTransition(runway, "SEY"); |
| 598 | // procedures not loaded, abandon test |
| 599 | if (!star) |
| 600 | return; |
| 601 | |
| 602 | auto rm = globals->get_subsystem<FGRouteMgr>(); |
| 603 | auto fp = FlightPlan::create(); |
| 604 | |
| 605 | auto testDelegate = new TestFPDelegate; |
| 606 | testDelegate->thePlan = fp; |
| 607 | fp->addDelegate(testDelegate); |
| 608 | |
| 609 | rm->setFlightPlan(fp); |
| 610 | FGTestApi::setUp::populateFPWithNasal(fp, "KBOS", "22R", "KJFK", "22L", ""); |
| 611 | |
| 612 | fp->setSTAR(star); |
| 613 | CPPUNIT_ASSERT_EQUAL(9, fp->numLegs()); |
| 614 | auto wp = fp->legAtIndex(1); |
| 615 | CPPUNIT_ASSERT_EQUAL(std::string{"SEY"}, wp->waypoint()->ident()); |
| 616 | CPPUNIT_ASSERT(rm->activate()); |
| 617 | } |
| 618 | |
| 619 | void RNAVProcedureTests::testLEBL_LARP2F() |
| 620 | { |
| 621 | auto lebl = FGAirport::findByIdent("LEBL"); |
| 622 | auto sid = lebl->findSIDWithIdent("LARP1F.25L"); |
| 623 | // procedures not loaded, abandon test |
| 624 | if (!sid) |
| 625 | return; |
| 626 | |
| 627 | FGTestApi::setUp::logPositionToKML("procedure_LEBL-LARP2F"); |
| 628 | |
| 629 | auto rm = globals->get_subsystem<FGRouteMgr>(); |
| 630 | auto fp = FlightPlan::create(); |
| 631 | |
| 632 | auto testDelegate = new TestFPDelegate; |
| 633 | testDelegate->thePlan = fp; |
| 634 | fp->addDelegate(testDelegate); |
| 635 | |
| 636 | rm->setFlightPlan(fp); |
| 637 | FGTestApi::setUp::populateFPWithNasal(fp, "LEBL", "25L", "LEIB", "06", ""); |
| 638 | |
| 639 | fp->setSID(sid); |
| 640 | // I don't know if this should pass or not! |
| 641 | // If its a bug, wrap next line in CPPUNIT_ASSERT_ASSERTION_FAIL |
| 642 | CPPUNIT_ASSERT_EQUAL(fp->legAtIndex(1)->waypoint()->position(), SGGeod::fromDeg(0, 0)); |
| 643 | CPPUNIT_ASSERT_EQUAL(std::string{"LARPA"}, fp->legAtIndex(8)->waypoint()->ident()); |
| 644 | double d = fp->legAtIndex(8)->distanceAlongRoute(); |
| 645 | CPPUNIT_ASSERT_DOUBLES_EQUAL(46.6, d, 1.0); // ensure the route didn't blow up |
| 646 | |
| 647 | FGTestApi::writeFlightPlanToKML(fp); |
| 648 | |
| 649 | CPPUNIT_ASSERT(rm->activate()); |
| 650 | |
| 651 | FGRunwayRef departureRunway = fp->departureRunway(); |
| 652 | |
| 653 | setupStandardGPS(); |
| 654 | |
| 655 | FGTestApi::setPositionAndStabilise(departureRunway->threshold()); |
| 656 | m_gpsNode->setStringValue("command", "leg"); |
| 657 | |
| 658 | auto pilot = SGSharedPtr<FGTestApi::TestPilot>(new FGTestApi::TestPilot); |
| 659 | pilot->resetAtPosition(globals->get_aircraft_position()); |
| 660 | CPPUNIT_ASSERT_DOUBLES_EQUAL(departureRunway->headingDeg(), m_gpsNode->getDoubleValue("wp/leg-true-course-deg"), 0.5); |
| 661 | pilot->setCourseTrue(m_gpsNode->getDoubleValue("wp/leg-true-course-deg")); |
| 662 | pilot->setSpeedKts(220); |
| 663 | pilot->flyGPSCourse(m_gps); |
| 664 | pilot->setTargetAltitudeFtMSL(8000); |
| 665 | pilot->setVerticalFPM(1800); |
| 666 | FGTestApi::runForTime(20.0); |
| 667 | bool ok = FGTestApi::runForTimeWithCheck(180.0, [fp] () { |
| 668 | if (fp->currentIndex() == 1) { |
| 669 | return true; |
| 670 | } |
| 671 | return false; |
| 672 | }); |
| 673 | CPPUNIT_ASSERT(ok); |
| 674 | |
| 675 | FGTestApi::runForTime(180.0); |
| 676 | CPPUNIT_ASSERT_DOUBLES_EQUAL(199, m_gpsNode->getDoubleValue("wp/leg-true-course-deg"), 0.5); |
| 677 | } |
| 678 | |
| 679 | // This could probably be in a better place but this allows it to access TestDelegate. |
| 680 | // Also it does relate to procedures as its a bug that only occurs with procedures |
| 681 | void RNAVProcedureTests::testIndexOf() |
| 682 | { |
| 683 | auto egkk = FGAirport::findByIdent("EGKK"); |
| 684 | auto sid = egkk->findSIDWithIdent("SAM3P"); |
| 685 | // procedures not loaded, abandon test |
| 686 | if (!sid) |
| 687 | return; |
| 688 | |
| 689 | auto rm = globals->get_subsystem<FGRouteMgr>(); |
| 690 | auto fp = FlightPlan::create(); |
| 691 | |
| 692 | auto testDelegate = new TestFPDelegate; |
| 693 | testDelegate->thePlan = fp; |
| 694 | fp->addDelegate(testDelegate); |
| 695 | |
| 696 | rm->setFlightPlan(fp); |
| 697 | FGTestApi::setUp::populateFPWithNasal(fp, "EGKK", "08R", "EGJJ", "27", "LELNA"); |
| 698 | |
| 699 | fp->setSID(sid); |
| 700 | FGPositioned::TypeFilter f{FGPositioned::VOR}; |
| 701 | auto southamptonVOR = fgpositioned_cast<FGNavRecord>(FGPositioned::findClosestWithIdent("SAM", SGGeod::fromDeg(-1.25, 51.0), &f)); |
| 702 | auto SAM = fp->legAtIndex(6)->waypoint(); |
| 703 | CPPUNIT_ASSERT_EQUAL(southamptonVOR->ident(), SAM->ident()); |
| 704 | CPPUNIT_ASSERT_EQUAL(6, fp->findWayptIndex(southamptonVOR)); |
| 705 | } |