| 1 | /* |
| 2 | * SPDX-FileCopyrightText: (C) 2024 James Turner <[email protected]> |
| 3 | * SPDX-License-Identifier: GPL-2.0-or-later |
| 4 | */ |
| 5 | |
| 6 | |
| 7 | #include "test_headingIndicator.hxx" |
| 8 | |
| 9 | #include <cstring> |
| 10 | #include <memory> |
| 11 | |
| 12 | #include "test_suite/FGTestApi/NavDataCache.hxx" |
| 13 | #include "test_suite/FGTestApi/testGlobals.hxx" |
| 14 | |
| 15 | #include <Airports/airport.hxx> |
| 16 | #include <Navaids/NavDataCache.hxx> |
| 17 | |
| 18 | #include <Instrumentation/heading_indicator_dg.hxx> |
| 19 | #include <Main/fg_props.hxx> |
| 20 | #include <Main/locale.hxx> |
| 21 | |
| 22 | // Set up function for each test. |
| 23 | void HeadingIndicatorTests::setUp() |
| 24 | { |
| 25 | FGTestApi::setUp::initTestGlobals("heading-indicator-dg"); |
| 26 | FGTestApi::setUp::initNavDataCache(); |
| 27 | |
| 28 | // otherwise ATCSPeech will call locale functions and assert |
| 29 | globals->get_locale()->selectLanguage({}); |
| 30 | } |
| 31 | |
| 32 | |
| 33 | // Clean up after each test. |
| 34 | void HeadingIndicatorTests::tearDown() |
| 35 | { |
| 36 | FGTestApi::tearDown::shutdownTestGlobals(); |
| 37 | } |
| 38 | |
| 39 | |
| 40 | // std::string NavRadioTests::formatFrequency(double f) |
| 41 | // { |
| 42 | // char buf[16]; |
| 43 | // ::snprintf(buf, 16, "%3.2f", f); |
| 44 | // return buf; |
| 45 | // } |
| 46 | |
| 47 | SGSubsystemRef HeadingIndicatorTests::setupInstrument(const std::string& name, int index) |
| 48 | { |
| 49 | SGPropertyNode_ptr configNode(new SGPropertyNode); |
| 50 | configNode->setStringValue("name", name); |
| 51 | configNode->setIntValue("number", index); |
| 52 | configNode->setBoolValue("new-default-power-path", true); |
| 53 | |
| 54 | auto r = new HeadingIndicatorDG(configNode); |
| 55 | |
| 56 | r->bind(); |
| 57 | r->init(); |
| 58 | |
| 59 | globals->get_subsystem_mgr()->add("heading-indicator-dg", r); |
| 60 | |
| 61 | auto elecOuts = fgGetNode("/systems/electrical/outputs/", true); |
| 62 | elecOuts->getChild("heading-indicator-dg", index, true)->setDoubleValue(12.0); |
| 63 | |
| 64 | fgSetDouble("/accelerations/pilot-g", 1.0); |
| 65 | |
| 66 | return r; |
| 67 | } |
| 68 | |
| 69 | void HeadingIndicatorTests::testBasic() |
| 70 | { |
| 71 | auto r = setupInstrument("hi", 2); |
| 72 | FGAirportRef apt = FGAirport::getByIdent("EDDM"); |
| 73 | FGTestApi::setPositionAndStabilise(apt->geod()); |
| 74 | |
| 75 | SGPropertyNode_ptr n = globals->get_props()->getNode("instrumentation/hi[2]"); |
| 76 | auto indicatedHeadingNode = n->getChild("indicated-heading-deg"); |
| 77 | |
| 78 | fgSetDouble("/orientation/heading-deg", 77.0); |
| 79 | FGTestApi::runForTime(6.0); |
| 80 | CPPUNIT_ASSERT_DOUBLES_EQUAL(1.0, n->getDoubleValue("spin"), 1e-2); |
| 81 | |
| 82 | n->setDoubleValue("offset-deg", 2.0); |
| 83 | r->update(0.01); |
| 84 | CPPUNIT_ASSERT_DOUBLES_EQUAL(79.0, indicatedHeadingNode->getDoubleValue(), 0.1); |
| 85 | |
| 86 | // setup a wrap condition around 360.0 / 0.0 |
| 87 | fgSetDouble("/orientation/heading-deg", 358.0); |
| 88 | n->setDoubleValue("offset-deg", -2); |
| 89 | FGTestApi::runForTime(6.0); |
| 90 | |
| 91 | CPPUNIT_ASSERT_DOUBLES_EQUAL(356.0, indicatedHeadingNode->getDoubleValue(), 0.1); |
| 92 | |
| 93 | fgSetDouble("/orientation/heading-deg", 5.0); |
| 94 | for (int i=0; i<10; ++i) { |
| 95 | r->update(0.01); |
| 96 | auto indicated = n->getDoubleValue("indicated-heading-deg"); |
| 97 | CPPUNIT_ASSERT((indicated >= 356) || (indicated < 3.0)); |
| 98 | } |
| 99 | |
| 100 | FGTestApi::runForTime(1.0); |
| 101 | CPPUNIT_ASSERT_DOUBLES_EQUAL(3.0, indicatedHeadingNode->getDoubleValue(), 0.1); |
| 102 | |
| 103 | // setup a wrap condition around 180.0 |
| 104 | fgSetDouble("/orientation/heading-deg", 182.0); |
| 105 | n->setDoubleValue("offset-deg", 2); |
| 106 | FGTestApi::runForTime(6.0); |
| 107 | |
| 108 | CPPUNIT_ASSERT_DOUBLES_EQUAL(184.0, indicatedHeadingNode->getDoubleValue(), 0.1); |
| 109 | |
| 110 | fgSetDouble("/orientation/heading-deg", 175.0); |
| 111 | for (int i=0; i<10; ++i) { |
| 112 | r->update(0.01); |
| 113 | auto indicated = indicatedHeadingNode->getDoubleValue(); |
| 114 | CPPUNIT_ASSERT((indicated >= 176.0) && (indicated < 184.0)); |
| 115 | } |
| 116 | |
| 117 | FGTestApi::runForTime(1.0); |
| 118 | CPPUNIT_ASSERT_DOUBLES_EQUAL(177, indicatedHeadingNode->getDoubleValue(), 0.1); |
| 119 | |
| 120 | // one more wrap condition |
| 121 | fgSetDouble("/orientation/heading-deg", 270.0); |
| 122 | n->setDoubleValue("offset-deg", 87); |
| 123 | FGTestApi::runForTime(6.0); // stabilize |
| 124 | |
| 125 | CPPUNIT_ASSERT_DOUBLES_EQUAL(357.0, indicatedHeadingNode->getDoubleValue(), 0.1); |
| 126 | |
| 127 | fgSetDouble("/orientation/heading-deg", 250.0); |
| 128 | for (int i = 0; i < 10; ++i) { |
| 129 | r->update(0.01); |
| 130 | auto indicated = indicatedHeadingNode->getDoubleValue(); |
| 131 | |
| 132 | CPPUNIT_ASSERT((indicated < 357.0) && (indicated > 336.0)); |
| 133 | } |
| 134 | |
| 135 | FGTestApi::runForTime(1.0); |
| 136 | CPPUNIT_ASSERT_DOUBLES_EQUAL(337.0, indicatedHeadingNode->getDoubleValue(), 0.1); |
| 137 | |
| 138 | // one more wrap condition |
| 139 | fgSetDouble("/orientation/heading-deg", 270.0); |
| 140 | n->setDoubleValue("offset-deg", 97); |
| 141 | FGTestApi::runForTime(2.0); // stabilize |
| 142 | |
| 143 | CPPUNIT_ASSERT_DOUBLES_EQUAL(7.0, indicatedHeadingNode->getDoubleValue(), 0.1); |
| 144 | |
| 145 | fgSetDouble("/orientation/heading-deg", 250.0); |
| 146 | for (int i = 0; i < 10; ++i) { |
| 147 | r->update(0.01); |
| 148 | auto indicated = indicatedHeadingNode->getDoubleValue(); |
| 149 | std::cerr << "Indicated:" << indicated << std::endl; |
| 150 | CPPUNIT_ASSERT((indicated < 7.0) || (indicated > 346.0)); |
| 151 | } |
| 152 | |
| 153 | FGTestApi::runForTime(1.0); |
| 154 | CPPUNIT_ASSERT_DOUBLES_EQUAL(347.0, indicatedHeadingNode->getDoubleValue(), 0.1); |
| 155 | |
| 156 | // test alignment adjustment |
| 157 | fgSetDouble("/orientation/heading-deg", 182.0); |
| 158 | n->setDoubleValue("offset-deg", 0.0); |
| 159 | n->setDoubleValue("align-deg", 42.0); |
| 160 | FGTestApi::runForTime(1.0); |
| 161 | |
| 162 | CPPUNIT_ASSERT_DOUBLES_EQUAL(224.0, indicatedHeadingNode->getDoubleValue(), 0.1); |
| 163 | |
| 164 | // test error adjustment |
| 165 | n->setDoubleValue("align-deg", 10.0); |
| 166 | n->setDoubleValue("error-deg", 13.0); |
| 167 | r->update(0.01); |
| 168 | |
| 169 | CPPUNIT_ASSERT_DOUBLES_EQUAL(205.0, indicatedHeadingNode->getDoubleValue(), 0.1); |
| 170 | } |
| 171 | |
| 172 | void HeadingIndicatorTests::testTumble() |
| 173 | { |
| 174 | auto r = setupInstrument("hi", 2); |
| 175 | SGPropertyNode_ptr n = globals->get_props()->getNode("instrumentation/hi[2]"); |
| 176 | |
| 177 | FGAirportRef apt = FGAirport::getByIdent("EDDM"); |
| 178 | FGTestApi::setPositionAndStabilise(apt->geod()); |
| 179 | |
| 180 | // spin-up |
| 181 | FGTestApi::runForTime(6.0); |
| 182 | CPPUNIT_ASSERT(!n->getBoolValue("is-caged")); |
| 183 | |
| 184 | // not tumbled |
| 185 | CPPUNIT_ASSERT(!n->getBoolValue("tumble-flag")); |
| 186 | fgSetDouble("/accelerations/pilot-g", 1.4); |
| 187 | r->update(0.1); |
| 188 | CPPUNIT_ASSERT(!n->getBoolValue("tumble-flag")); |
| 189 | |
| 190 | fgSetDouble("/accelerations/pilot-g", 2.5); |
| 191 | FGTestApi::runForTime(1.0); |
| 192 | CPPUNIT_ASSERT(n->getBoolValue("tumble-flag")); |
| 193 | |
| 194 | // back to normal Gs, should stay tumbled |
| 195 | fgSetDouble("/accelerations/pilot-g", 1.0); |
| 196 | FGTestApi::runForTime(3.0); |
| 197 | CPPUNIT_ASSERT(n->getBoolValue("tumble-flag")); |
| 198 | } |
| 199 | |
| 200 | |
| 201 | void HeadingIndicatorTests::testLatitudeNut() |
| 202 | { |
| 203 | auto r = setupInstrument("hi", 2); |
| 204 | |
| 205 | FGAirportRef apt = FGAirport::getByIdent("EDDM"); |
| 206 | FGTestApi::setPositionAndStabilise(apt->geod()); |
| 207 | |
| 208 | SGPropertyNode_ptr n = globals->get_props()->getNode("instrumentation/hi[2]"); |
| 209 | fgSetDouble("/orientation/heading-deg", 39.0); |
| 210 | FGTestApi::runForTime(6.0); |
| 211 | |
| 212 | r->update(0.1); |
| 213 | n->setDoubleValue("offset-deg", 0.0); // remove spin-up offset |
| 214 | // check wander |
| 215 | CPPUNIT_ASSERT_DOUBLES_EQUAL(-10.6, n->getDoubleValue("drift-per-hour-deg"), 0.1); |
| 216 | n->setDoubleValue("latitude-nut-setting", apt->latitude()); |
| 217 | |
| 218 | // set nut, should negate the drift |
| 219 | FGTestApi::runForTime(1.0); |
| 220 | CPPUNIT_ASSERT_DOUBLES_EQUAL(0.0, n->getDoubleValue("drift-per-hour-deg"), 0.1); |
| 221 | } |
| 222 | |
| 223 | void HeadingIndicatorTests::testVacuumGyro() |
| 224 | { |
| 225 | SGPropertyNode_ptr configNode(new SGPropertyNode); |
| 226 | configNode->setStringValue("name", "hi-dg"); |
| 227 | configNode->setIntValue("number", 0); |
| 228 | configNode->setStringValue("suction", "/test/suction"); |
| 229 | configNode->setDoubleValue("minimum-vacuum", 4.0); |
| 230 | auto r = new HeadingIndicatorDG(configNode); |
| 231 | |
| 232 | r->bind(); |
| 233 | r->init(); |
| 234 | |
| 235 | globals->get_subsystem_mgr()->add("heading-indicator-dg", r); |
| 236 | SGPropertyNode_ptr n = globals->get_props()->getNode("instrumentation/hi-dg[0]"); |
| 237 | |
| 238 | fgSetDouble("/test/suction", 3.0); |
| 239 | CPPUNIT_ASSERT_DOUBLES_EQUAL(0.0, n->getDoubleValue("spin"), 0.1); |
| 240 | |
| 241 | fgSetDouble("/test/suction", 6.0); |
| 242 | FGTestApi::runForTime(2.0); |
| 243 | |
| 244 | // should still be spinning up |
| 245 | CPPUNIT_ASSERT_DOUBLES_EQUAL(0.75, n->getDoubleValue("spin"), 0.1); |
| 246 | |
| 247 | FGTestApi::runForTime(6.0); |
| 248 | |
| 249 | CPPUNIT_ASSERT_DOUBLES_EQUAL(1.0, n->getDoubleValue("spin"), 0.1); |
| 250 | } |