| 1 | /* |
| 2 | * Copyright (C) 2021 Colin Geniet |
| 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 "config.h" |
| 21 | |
| 22 | #include "test_submodels.hxx" |
| 23 | |
| 24 | #include "test_suite/FGTestApi/NavDataCache.hxx" |
| 25 | #include "test_suite/FGTestApi/TestPilot.hxx" |
| 26 | #include "test_suite/FGTestApi/testGlobals.hxx" |
| 27 | |
| 28 | #include <AIModel/AIAircraft.hxx> |
| 29 | #include <AIModel/AIManager.hxx> |
| 30 | #include <AIModel/submodel.hxx> |
| 31 | |
| 32 | #include <Airports/airport.hxx> |
| 33 | #include <Main/fg_props.hxx> |
| 34 | #include <Main/globals.hxx> |
| 35 | |
| 36 | #include <cmath> |
| 37 | |
| 38 | using std::string; |
| 39 | |
| 40 | ///////////////////////////////////////////////////////////////////////////// |
| 41 | |
| 42 | // Set up function for each test. |
| 43 | void SubmodelsTests::setUp() |
| 44 | { |
| 45 | FGTestApi::setUp::initTestGlobals("Submodels"); |
| 46 | FGTestApi::setUp::initNavDataCache(); |
| 47 | |
| 48 | globals->append_aircraft_path(SGPath::fromUtf8(FG_TEST_SUITE_DATA) / "Aircraft"); |
| 49 | |
| 50 | auto props = globals->get_props(); |
| 51 | props->setBoolValue("sim/ai/enabled", true); |
| 52 | props->setStringValue("sim/submodels/path", "Aircraft/Test/submodels.xml"); |
| 53 | |
| 54 | globals->get_subsystem_mgr()->add<FGAIManager>(); |
| 55 | globals->get_subsystem_mgr()->add<FGSubmodelMgr>(); |
| 56 | |
| 57 | globals->get_subsystem_mgr()->bind(); |
| 58 | globals->get_subsystem_mgr()->init(); |
| 59 | globals->get_subsystem_mgr()->postinit(); |
| 60 | } |
| 61 | |
| 62 | // Clean up after each test. |
| 63 | void SubmodelsTests::tearDown() |
| 64 | { |
| 65 | FGTestApi::tearDown::shutdownTestGlobals(); |
| 66 | } |
| 67 | |
| 68 | void SubmodelsTests::testLoadXML() |
| 69 | { |
| 70 | auto props = globals->get_props(); |
| 71 | auto sm_node = props->getNode("ai/submodels"); |
| 72 | CPPUNIT_ASSERT(sm_node->hasChild("submodel", 0)); |
| 73 | sm_node = sm_node->getChild("submodel", 0); |
| 74 | CPPUNIT_ASSERT_EQUAL(string{"testLoadXML"}, static_cast<string>(sm_node->getStringValue("name"))); |
| 75 | CPPUNIT_ASSERT_EQUAL(0, sm_node->getIntValue("id")); |
| 76 | CPPUNIT_ASSERT_EQUAL(42, sm_node->getIntValue("count")); |
| 77 | CPPUNIT_ASSERT(sm_node->getBoolValue("serviceable")); |
| 78 | } |
| 79 | |
| 80 | FGAIBase* SubmodelsTests::findAIModel(std::string &name) { |
| 81 | auto ai_list = globals->get_subsystem<FGAIManager>()->get_ai_list(); |
| 82 | auto filter = [name](FGAIBase *model) { return model->_getName() == name; }; |
| 83 | return *std::find_if(ai_list.begin(), ai_list.end(), filter); |
| 84 | } |
| 85 | |
| 86 | int SubmodelsTests::countAIModels(std::string &name) { |
| 87 | auto ai_list = globals->get_subsystem<FGAIManager>()->get_ai_list(); |
| 88 | auto filter = [name](FGAIBase *model) { return model->_getName() == name; }; |
| 89 | return static_cast<int>(std::count_if(ai_list.begin(), ai_list.end(), filter)); |
| 90 | } |
| 91 | |
| 92 | void SubmodelsTests::testRelease() |
| 93 | { |
| 94 | auto props = globals->get_props(); |
| 95 | auto sm_node = props->getNode("ai/submodels/submodel[1]"); |
| 96 | std::string name = sm_node->getStringValue("name"); |
| 97 | |
| 98 | // Setup reasonable flight conditions. |
| 99 | auto bikf = FGAirport::findByIdent("BIKF"); |
| 100 | auto pilot = SGSharedPtr<FGTestApi::TestPilot>(new FGTestApi::TestPilot); |
| 101 | FGTestApi::setPosition(bikf->geod()); |
| 102 | pilot->resetAtPosition(bikf->geod()); |
| 103 | pilot->setSpeedKts(0); |
| 104 | pilot->setCourseTrue(0.0); |
| 105 | pilot->setTargetAltitudeFtMSL(0); |
| 106 | |
| 107 | // Sanity check |
| 108 | CPPUNIT_ASSERT_EQUAL(0, countAIModels(name)); |
| 109 | |
| 110 | // Don't release anything |
| 111 | FGTestApi::runForTime(10.0); |
| 112 | CPPUNIT_ASSERT_EQUAL(0, countAIModels(name)); |
| 113 | |
| 114 | // Release submodel |
| 115 | sm_node->setBoolValue("trigger", true); |
| 116 | FGTestApi::runForTime(1); |
| 117 | sm_node->setBoolValue("trigger", false); |
| 118 | FGTestApi::runForTime(1); |
| 119 | CPPUNIT_ASSERT_EQUAL(1, countAIModels(name)); |
| 120 | |
| 121 | // Check validity |
| 122 | auto sm = findAIModel(name); |
| 123 | CPPUNIT_ASSERT(sm->isValid()); |
| 124 | CPPUNIT_ASSERT(!sm->getDie()); |
| 125 | |
| 126 | // Second time |
| 127 | sm_node->setBoolValue("trigger", true); |
| 128 | FGTestApi::runForTime(5); |
| 129 | sm_node->setBoolValue("trigger", false); |
| 130 | FGTestApi::runForTime(1); |
| 131 | CPPUNIT_ASSERT_EQUAL(2, countAIModels(name)); |
| 132 | |
| 133 | // Let submodels expire |
| 134 | FGTestApi::runForTime(20); |
| 135 | CPPUNIT_ASSERT_EQUAL(0, countAIModels(name)); |
| 136 | |
| 137 | // Switch to repeat release |
| 138 | sm_node->setBoolValue("repeat", true); |
| 139 | sm_node->setBoolValue("trigger", true); |
| 140 | FGTestApi::runForTime(4.2); // release interval is 1s |
| 141 | sm_node->setBoolValue("trigger", false); |
| 142 | CPPUNIT_ASSERT_EQUAL(4, countAIModels(name)); |
| 143 | |
| 144 | // Let submodels expire |
| 145 | FGTestApi::runForTime(20); |
| 146 | CPPUNIT_ASSERT_EQUAL(0, countAIModels(name)); |
| 147 | |
| 148 | // In repeat mode, release timer persists when trigger is false. |
| 149 | // Currently it is at ~0.2s, so this must not release anything. |
| 150 | sm_node->setBoolValue("trigger", true); |
| 151 | FGTestApi::runForTime(0.5); |
| 152 | sm_node->setBoolValue("trigger", false); |
| 153 | CPPUNIT_ASSERT_EQUAL(0, countAIModels(name)); |
| 154 | |
| 155 | // Set limited count |
| 156 | sm_node->setIntValue("count", 3); |
| 157 | sm_node->setBoolValue("trigger", true); |
| 158 | FGTestApi::runForTime(1); |
| 159 | CPPUNIT_ASSERT_EQUAL(2, sm_node->getIntValue("count")); |
| 160 | CPPUNIT_ASSERT_EQUAL(1, countAIModels(name)); |
| 161 | FGTestApi::runForTime(5); |
| 162 | CPPUNIT_ASSERT_EQUAL(0, sm_node->getIntValue("count")); |
| 163 | CPPUNIT_ASSERT_EQUAL(3, countAIModels(name)); |
| 164 | } |
| 165 | |
| 166 | void SubmodelsTests::testInitialState() |
| 167 | { |
| 168 | auto props = globals->get_props(); |
| 169 | auto sm_node = props->getNode("ai/submodels/submodel[2]"); |
| 170 | std::string name = sm_node->getStringValue("name"); |
| 171 | |
| 172 | // Submodel parameters |
| 173 | double x_offset = 10, y_offset = 6, z_offset = 1, yaw_offset = 30, pitch_offset = 50, speed = 100; |
| 174 | |
| 175 | // Setup reasonable flight conditions. |
| 176 | auto bikf = FGAirport::findByIdent("BIKF"); |
| 177 | auto pilot = SGSharedPtr<FGTestApi::TestPilot>(new FGTestApi::TestPilot); |
| 178 | FGTestApi::setPosition(bikf->geod()); |
| 179 | pilot->resetAtPosition(bikf->geod()); |
| 180 | pilot->setSpeedKts(0); |
| 181 | pilot->setCourseTrue(90); |
| 182 | pilot->setTargetAltitudeFtMSL(0); |
| 183 | props->setDoubleValue("/orientation/pitch-deg", 0); |
| 184 | props->setDoubleValue("/orientation/roll-deg", 0); |
| 185 | FGTestApi::runForTime(1); |
| 186 | |
| 187 | sm_node->setBoolValue("trigger", true); |
| 188 | // Run update loop with dt=0 to capture initial state. |
| 189 | globals->get_subsystem<FGSubmodelMgr>()->update(0); |
| 190 | globals->get_subsystem<FGAIManager>()->update(0); |
| 191 | sm_node->setBoolValue("trigger", false); |
| 192 | |
| 193 | CPPUNIT_ASSERT_EQUAL(1, countAIModels(name)); |
| 194 | auto sm = findAIModel(name); |
| 195 | auto sm_pos = sm->getCartPos(); |
| 196 | |
| 197 | auto ac_pos = globals->get_aircraft_position_cart(); //ac->getCartPosAt(SGVec3d(0, 0, 0)); |
| 198 | double heading, pitch, roll; |
| 199 | globals->get_aircraft_orientation(heading, pitch, roll); |
| 200 | |
| 201 | CPPUNIT_ASSERT_DOUBLES_EQUAL(90, heading, 0.1); |
| 202 | CPPUNIT_ASSERT_DOUBLES_EQUAL(0, pitch, 0.1); |
| 203 | CPPUNIT_ASSERT_DOUBLES_EQUAL(0, roll, 0.1); |
| 204 | |
| 205 | // Submodel release point |
| 206 | // Submodels offsets are in x-back,y-right,z-up frame. |
| 207 | // Computation is in x-forward,y-irght,z-down frame. |
| 208 | SGVec3d offset(-x_offset, y_offset, -z_offset); |
| 209 | SGQuatd local_frame_rotation = SGQuatd::fromLonLat(globals->get_aircraft_position()); |
| 210 | local_frame_rotation *= SGQuatd::fromYawPitchRollDeg(heading, pitch, roll); |
| 211 | offset = local_frame_rotation.backTransform(offset); |
| 212 | auto release_pos = ac_pos + offset; |
| 213 | |
| 214 | CPPUNIT_ASSERT_DOUBLES_EQUAL(release_pos.x(), sm_pos.x(), 0.01); |
| 215 | CPPUNIT_ASSERT_DOUBLES_EQUAL(release_pos.y(), sm_pos.y(), 0.01); |
| 216 | CPPUNIT_ASSERT_DOUBLES_EQUAL(release_pos.z(), sm_pos.z(), 0.01); |
| 217 | CPPUNIT_ASSERT_DOUBLES_EQUAL(remainder(heading + yaw_offset, 360), remainder(sm->getTrueHeadingDeg(), 360), 0.1); |
| 218 | CPPUNIT_ASSERT_DOUBLES_EQUAL(pitch + pitch_offset, sm->_getPitch(), 0.1); |
| 219 | CPPUNIT_ASSERT_DOUBLES_EQUAL(speed, sm->_getSpeed() * SG_KT_TO_FPS, 0.1); |
| 220 | |
| 221 | // Let submodels expire |
| 222 | FGTestApi::runForTime(20); |
| 223 | CPPUNIT_ASSERT_EQUAL(0, countAIModels(name)); |
| 224 | |
| 225 | // Second test for velocities |
| 226 | double speed_east = 100, wind_north = 20; |
| 227 | pilot->setSpeedKts(100 * SG_FPS_TO_KT); |
| 228 | props->setDoubleValue("/orientation/pitch-deg", 0); |
| 229 | props->setDoubleValue("/orientation/roll-deg", 0); |
| 230 | props->setDoubleValue("/velocities/speed-north-fps", 0); |
| 231 | props->setDoubleValue("/velocities/speed-east-fps", speed_east); |
| 232 | props->setDoubleValue("/velocities/speed-down-fps", 0); |
| 233 | props->setDoubleValue("/environment/wind-from-north-fps", wind_north); |
| 234 | props->setDoubleValue("/environment/wind-from-east-fps", 0); |
| 235 | FGTestApi::runForTime(1); |
| 236 | |
| 237 | sm_node->setBoolValue("trigger", true); |
| 238 | // Run update loop with dt=0 to capture initial state. |
| 239 | globals->get_subsystem<FGSubmodelMgr>()->update(0); |
| 240 | globals->get_subsystem<FGAIManager>()->update(0); |
| 241 | sm_node->setBoolValue("trigger", false); |
| 242 | |
| 243 | CPPUNIT_ASSERT_EQUAL(1, countAIModels(name)); |
| 244 | sm = findAIModel(name); |
| 245 | |
| 246 | // Check initial speeds. |
| 247 | // _get_speed_*_fps gives airspeed for submodels, which initially must be the vector |
| 248 | // parent_ground_velocity + opposed_wind_velocity + submodel_launch_velocity |
| 249 | CPPUNIT_ASSERT_DOUBLES_EQUAL(cos((heading + yaw_offset) * SG_DEGREES_TO_RADIANS) |
| 250 | * cos((pitch + pitch_offset) * SG_DEGREES_TO_RADIANS) |
| 251 | * speed |
| 252 | + wind_north, |
| 253 | sm->_get_speed_north_fps(), 0.1); |
| 254 | CPPUNIT_ASSERT_DOUBLES_EQUAL(sin((heading + yaw_offset) * SG_DEGREES_TO_RADIANS) |
| 255 | * cos((pitch + pitch_offset) * SG_DEGREES_TO_RADIANS) |
| 256 | * speed |
| 257 | + speed_east, |
| 258 | sm->_get_speed_east_fps(), 0.1); |
| 259 | CPPUNIT_ASSERT_DOUBLES_EQUAL(sin((pitch + pitch_offset) * SG_DEGREES_TO_RADIANS) * speed, |
| 260 | sm->_getVS_fps(), 0.1); |
| 261 | } |