| 1 | #include "test_dme.hxx" |
| 2 | |
| 3 | #include <cstring> |
| 4 | #include <memory> |
| 5 | |
| 6 | #include "test_suite/FGTestApi/NavDataCache.hxx" |
| 7 | #include "test_suite/FGTestApi/TestPilot.hxx" |
| 8 | #include "test_suite/FGTestApi/testGlobals.hxx" |
| 9 | |
| 10 | #include <Airports/airport.hxx> |
| 11 | #include <Navaids/NavDataCache.hxx> |
| 12 | #include <Navaids/navlist.hxx> |
| 13 | #include <Navaids/navrecord.hxx> |
| 14 | |
| 15 | #include <Instrumentation/dme.hxx> |
| 16 | |
| 17 | #include <Main/fg_props.hxx> |
| 18 | |
| 19 | // Set up function for each test. |
| 20 | void DMEReceiverTests::setUp() |
| 21 | { |
| 22 | FGTestApi::setUp::initTestGlobals("navradio"); |
| 23 | FGTestApi::setUp::initNavDataCache(); |
| 24 | } |
| 25 | |
| 26 | |
| 27 | // Clean up after each test. |
| 28 | void DMEReceiverTests::tearDown() |
| 29 | { |
| 30 | FGTestApi::tearDown::shutdownTestGlobals(); |
| 31 | } |
| 32 | |
| 33 | void DMEReceiverTests::setPositionAndStabilise(DME* r, const SGGeod& g) |
| 34 | { |
| 35 | FGTestApi::setPosition(g); |
| 36 | for (int i = 0; i < 60; ++i) { |
| 37 | r->update(0.1); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | SGSharedPtr<DME> DMEReceiverTests::setupStandardDME() |
| 42 | { |
| 43 | SGPropertyNode_ptr configNode(new SGPropertyNode); |
| 44 | configNode->setStringValue("name", "dmetest"); |
| 45 | configNode->setIntValue("number", 2); |
| 46 | |
| 47 | return new DME(configNode); |
| 48 | } |
| 49 | |
| 50 | void DMEReceiverTests::testBasic() |
| 51 | { |
| 52 | SGSharedPtr<DME> r = setupStandardDME(); |
| 53 | |
| 54 | // set a source string pointing at a fictious nav-receiver |
| 55 | fgSetString("/instrumentation/dmetest[2]/frequencies/source", |
| 56 | "/instrumentation/nav[0]/frequencies/selected-mhz"); |
| 57 | |
| 58 | r->bind(); |
| 59 | r->init(); |
| 60 | globals->get_subsystem_mgr()->add("dme", r.get()); |
| 61 | |
| 62 | auto arlanda = fgFindAirportID("ESSA"); |
| 63 | |
| 64 | // set the nav frequency |
| 65 | fgSetDouble("/instrumentation/nav[0]/frequencies/selected-mhz", 113.30); |
| 66 | |
| 67 | |
| 68 | SGPropertyNode_ptr node = globals->get_props()->getNode("instrumentation/dmetest[2]"); |
| 69 | node->setBoolValue("serviceable", true); |
| 70 | |
| 71 | fgSetDouble("systems/electrical/outputs/dme[2]", 12.0); |
| 72 | |
| 73 | setPositionAndStabilise(r.get(), arlanda->geod()); |
| 74 | |
| 75 | |
| 76 | CPPUNIT_ASSERT_EQUAL(true, node->getBoolValue("in-range")); |
| 77 | CPPUNIT_ASSERT_DOUBLES_EQUAL(4.4, node->getDoubleValue("indicated-distance-nm"), 0.1); |
| 78 | |
| 79 | // fly towards the station at constant speed |
| 80 | |
| 81 | auto pilot = SGSharedPtr<FGTestApi::TestPilot>(new FGTestApi::TestPilot); |
| 82 | pilot->setSpeedKts(150); |
| 83 | |
| 84 | |
| 85 | FGPositioned::TypeFilter f{FGPositioned::DME}; |
| 86 | FGNavRecordRef arlandaDME = fgpositioned_cast<FGNavRecord>( |
| 87 | FGPositioned::findClosestWithIdent("ANE", arlanda->geod(), &f)); |
| 88 | |
| 89 | const double trueCourseToANE = SGGeodesy::courseDeg(arlanda->geod(), arlandaDME->geod()); |
| 90 | pilot->setCourseTrue(trueCourseToANE); |
| 91 | FGTestApi::runForTime(30.0); |
| 92 | |
| 93 | CPPUNIT_ASSERT_DOUBLES_EQUAL(150, node->getDoubleValue("indicated-ground-speed-kt"), 0.5); |
| 94 | // should have travelled (150 / 3600 * 30 ) = 1.25nm |
| 95 | CPPUNIT_ASSERT_DOUBLES_EQUAL(3.15, node->getDoubleValue("indicated-distance-nm"), 0.1); |
| 96 | } |
| 97 | |
| 98 | void DMEReceiverTests::testRCFN_04DME() |
| 99 | { |
| 100 | // disabled pending discussion about the data for this one |
| 101 | return; |
| 102 | |
| 103 | auto rcfn = fgFindAirportID("RCFN"); |
| 104 | |
| 105 | auto dmeReceiver = setupStandardDME(); |
| 106 | |
| 107 | FGRunwayRef rwy04 = rcfn->getRunwayByIdent("04"); |
| 108 | |
| 109 | FGPositioned::TypeFilter filter(FGPositioned::DME); |
| 110 | auto matches = FGPositioned::findAllWithIdent("IFNN", &filter); |
| 111 | FGPositioned::sortByRange(matches, rcfn->geod()); |
| 112 | |
| 113 | CPPUNIT_ASSERT(!matches.empty()); |
| 114 | // should be size two, really |
| 115 | |
| 116 | auto station = fgpositioned_cast<FGNavRecord>(matches.front()); |
| 117 | CPPUNIT_ASSERT(station); |
| 118 | |
| 119 | CPPUNIT_ASSERT_DOUBLES_EQUAL(110.9, station->get_freq(), 0.01); |
| 120 | } |