| 1 | #include "testPidController.hxx" |
| 2 | #include "testPidControllerData.hxx" |
| 3 | |
| 4 | #include "test_suite/FGTestApi/testGlobals.hxx" |
| 5 | |
| 6 | |
| 7 | #include <Autopilot/autopilot.hxx> |
| 8 | #include <Autopilot/pidcontroller.hxx> |
| 9 | #include <Main/fg_props.hxx> |
| 10 | #include <Main/globals.hxx> |
| 11 | |
| 12 | |
| 13 | #include <simgear/math/sg_random.hxx> |
| 14 | #include <simgear/props/props_io.hxx> |
| 15 | |
| 16 | // Set up function for each test. |
| 17 | void PidControllerTests::setUp() |
| 18 | { |
| 19 | FGTestApi::setUp::initTestGlobals("ap-pidcontroller"); |
| 20 | } |
| 21 | |
| 22 | |
| 23 | // Clean up after each test. |
| 24 | void PidControllerTests::tearDown() |
| 25 | { |
| 26 | FGTestApi::tearDown::shutdownTestGlobals(); |
| 27 | } |
| 28 | |
| 29 | |
| 30 | SGPropertyNode_ptr PidControllerTests::configFromString(const std::string& s) |
| 31 | { |
| 32 | SGPropertyNode_ptr config = new SGPropertyNode; |
| 33 | |
| 34 | std::istringstream iss(s); |
| 35 | readProperties(iss, config); |
| 36 | return config; |
| 37 | } |
| 38 | |
| 39 | void PidControllerTests::test0() |
| 40 | { |
| 41 | test(false /*startup_current*/); |
| 42 | } |
| 43 | |
| 44 | void PidControllerTests::test1() |
| 45 | { |
| 46 | test(true /*startup_current*/); |
| 47 | } |
| 48 | |
| 49 | void PidControllerTests::test(bool startup_current) |
| 50 | { |
| 51 | sg_srandom(999); |
| 52 | |
| 53 | // Define vertical-hold pid-controller (based on Harrier-GR3). |
| 54 | // |
| 55 | std::string config_text0 = |
| 56 | R"(<?xml version="1.0" encoding="UTF-8"?> |
| 57 | <PropertyList> |
| 58 | <pid-controller> |
| 59 | <name>Vertical Speed Hold</name> |
| 60 | <debug>false</debug> |
| 61 | <startup-current></startup-current> |
| 62 | <enable>true</enable> |
| 63 | <input> |
| 64 | <prop>/velocities/vertical-speed-fps</prop> |
| 65 | </input> |
| 66 | <reference> |
| 67 | <prop>/autopilot/settings/vertical-speed-fpm</prop> |
| 68 | <scale>0.01667</scale> |
| 69 | </reference> |
| 70 | <output> |
| 71 | <prop>/controls/flight/elevator</prop> |
| 72 | </output> |
| 73 | <config> |
| 74 | <Kp>-0.025</Kp> <!-- proportional gain --> |
| 75 | <beta>1.0</beta> <!-- input value weighing factor --> |
| 76 | <alpha>0.1</alpha> <!-- low pass filter weighing factor --> |
| 77 | <gamma>0.0</gamma> <!-- input value weighing factor for --> |
| 78 | <!-- unfiltered derivative error --> |
| 79 | <Ti>5</Ti> <!-- integrator time --> |
| 80 | <Td>0.01</Td> <!-- derivator time --> |
| 81 | |
| 82 | <!-- Restrict elevator values to +/-0.35 at high speed, but allow full |
| 83 | +/-1 range at low speed. --> |
| 84 | <u_min>-1</u_min> |
| 85 | <u_max>1</u_max> |
| 86 | |
| 87 | </config> |
| 88 | </pid-controller> |
| 89 | </PropertyList> |
| 90 | )"; |
| 91 | |
| 92 | // Set the <startup-current> element. |
| 93 | std::string from = "<startup-current></startup-current>"; |
| 94 | std::string config_text = config_text0; |
| 95 | config_text.replace(config_text.find(from), from.size(), |
| 96 | (startup_current) ? "<startup-current>true</startup-current>" : "<startup-current>false</startup-current>" |
| 97 | ); |
| 98 | assert(config_text != config_text0); |
| 99 | std::cout << "config_text is:\n" << config_text << "\n"; |
| 100 | |
| 101 | SGPropertyNode_ptr config = configFromString(config_text); |
| 102 | |
| 103 | auto ap = new FGXMLAutopilot::Autopilot(globals->get_props(), config); |
| 104 | |
| 105 | globals->get_subsystem_mgr()->add("ap", ap); |
| 106 | ap->bind(); |
| 107 | ap->init(); |
| 108 | |
| 109 | const std::vector<PidControllerOutput>& outputs = (startup_current) ? pidControllerOutputs1 : pidControllerOutputs0; |
| 110 | assert(pidControllerInputs.size() == outputs.size()); |
| 111 | |
| 112 | fgSetDouble("/controls/flight/elevator", 0); |
| 113 | |
| 114 | for (unsigned i=0; i<pidControllerInputs.size(); ++i) { |
| 115 | const PidControllerInput& input = pidControllerInputs[i]; |
| 116 | const PidControllerOutput& output = outputs[i]; |
| 117 | fgSetDouble("/velocities/vertical-speed-fps", input.vspeed_fps); |
| 118 | fgSetDouble("/autopilot/settings/vertical-speed-fpm", input.reference / 0.01667); |
| 119 | ap->update(0.00833333 /*dt*/); |
| 120 | double elevator = fgGetDouble("/controls/flight/elevator"); |
| 121 | CPPUNIT_ASSERT_DOUBLES_EQUAL(output.output, elevator, 0.0001); |
| 122 | if (0) { |
| 123 | // This generates C++ text for setting the <outputs> vector above. |
| 124 | std::cout << "{ " << elevator << "},\n"; |
| 125 | } |
| 126 | } |
| 127 | } |