| 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 "TestPilot.hxx" |
| 21 | |
| 22 | #include <algorithm> |
| 23 | |
| 24 | #include <simgear/math/SGGeodesy.hxx> |
| 25 | #include <simgear/props/props.hxx> |
| 26 | #include <simgear/math/SGGeod.hxx> |
| 27 | |
| 28 | #include <Aircraft/AircraftPerformance.hxx> // for formulae |
| 29 | #include <Main/globals.hxx> |
| 30 | |
| 31 | #include "TestDataLogger.hxx" |
| 32 | |
| 33 | namespace FGTestApi { |
| 34 | |
| 35 | TestPilot::TestPilot(SGPropertyNode_ptr props) : |
| 36 | _propRoot(props) |
| 37 | { |
| 38 | if (!_propRoot) { |
| 39 | // use default properties |
| 40 | _propRoot = globals->get_props(); |
| 41 | } |
| 42 | |
| 43 | _latProp = _propRoot->getNode("position/latitude-deg", true); |
| 44 | _lonProp = _propRoot->getNode("position/longitude-deg", true); |
| 45 | _altitudeProp = _propRoot->getNode("position/altitude-ft", true); |
| 46 | _headingProp = _propRoot->getNode("orientation/heading-deg", true); |
| 47 | _speedKnotsProp = _propRoot->getNode("velocities/airspeed-kt", true); |
| 48 | _speedMachProp = _propRoot->getNode("velocities/mach", true); |
| 49 | _groundspeedKnotsProp = _propRoot->getNode("velocities/groundspeed-kt", true); |
| 50 | _verticalFPMProp = _propRoot->getNode("velocities/vertical-fpm", true); |
| 51 | |
| 52 | globals->get_subsystem_mgr()->add("flight", this); |
| 53 | } |
| 54 | |
| 55 | TestPilot::~TestPilot() |
| 56 | { |
| 57 | } |
| 58 | |
| 59 | void TestPilot::resetAtPosition(const SGGeod& pos) |
| 60 | { |
| 61 | _turnActive = false; |
| 62 | setPosition(pos); |
| 63 | } |
| 64 | |
| 65 | void TestPilot::init() |
| 66 | { |
| 67 | _vspeedFPM = 1200; |
| 68 | } |
| 69 | |
| 70 | void TestPilot::update(double dt) |
| 71 | { |
| 72 | updateValues(dt); |
| 73 | } |
| 74 | |
| 75 | void TestPilot::setSpeedKts(double knots) |
| 76 | { |
| 77 | _speedKnots = knots; |
| 78 | } |
| 79 | |
| 80 | void TestPilot::setVerticalFPM(double fpm) |
| 81 | { |
| 82 | _vspeedFPM = fpm; |
| 83 | } |
| 84 | |
| 85 | void TestPilot::setCourseTrue(double deg) |
| 86 | { |
| 87 | _trueCourseDeg = deg; |
| 88 | } |
| 89 | |
| 90 | void TestPilot::turnToCourse(double deg) |
| 91 | { |
| 92 | _turnActive = true; |
| 93 | _targetCourseDeg = deg; |
| 94 | } |
| 95 | |
| 96 | void TestPilot::flyHeading(double hdg) |
| 97 | { |
| 98 | _lateralMode = LateralMode::Heading; |
| 99 | _turnActive = true; |
| 100 | _targetCourseDeg = hdg; |
| 101 | } |
| 102 | |
| 103 | void TestPilot::flyGPSCourse(GPS *gps) |
| 104 | { |
| 105 | _gps = gps; |
| 106 | _gpsNode = globals->get_props()->getNode("instrumentation/gps"); |
| 107 | _gpsLegCourse = _gpsNode->getNode("wp/leg-true-course-deg", true); |
| 108 | _courseErrorNm = _gpsNode->getNode("wp/wp[1]/course-error-nm", true); |
| 109 | |
| 110 | _lateralMode = LateralMode::GPSCourse; |
| 111 | _turnActive = false; |
| 112 | } |
| 113 | |
| 114 | void TestPilot::flyGPSCourseOffset(GPS *gps, double offsetNm) |
| 115 | { |
| 116 | _gps = gps; |
| 117 | _gpsNode = globals->get_props()->getNode("instrumentation/gps"); |
| 118 | _gpsLegCourse = _gpsNode->getNode("wp/leg-true-course-deg", true); |
| 119 | _courseErrorNm = _gpsNode->getNode("wp/wp[1]/course-error-nm", true); |
| 120 | |
| 121 | _lateralMode = LateralMode::GPSOffset; |
| 122 | _courseOffsetNm = offsetNm; |
| 123 | _turnActive = false; |
| 124 | } |
| 125 | |
| 126 | void TestPilot::flyDirectTo(const SGGeod& target) |
| 127 | { |
| 128 | _lateralMode = LateralMode::Direct; |
| 129 | _targetPos = target; |
| 130 | } |
| 131 | |
| 132 | void TestPilot::updateValues(double dt) |
| 133 | { |
| 134 | auto dl = DataLogger::instance(); |
| 135 | |
| 136 | if (_gps && (_lateralMode == LateralMode::GPSCourse)) { |
| 137 | _targetCourseDeg = _gpsLegCourse->getDoubleValue(); |
| 138 | |
| 139 | // set how aggressively we try to correct our course |
| 140 | double courseCorrectionFactor = 64.0; |
| 141 | double crossTrack = _courseErrorNm->getDoubleValue(); |
| 142 | |
| 143 | dl->recordSamplePoint("TP-error-nm", crossTrack); |
| 144 | |
| 145 | SG_CLAMP_RANGE(crossTrack, -2.0, 2.0); // clamp to 2nm deviation |
| 146 | double correction = courseCorrectionFactor * crossTrack; |
| 147 | const double maxCorrectionAngle = 45; |
| 148 | |
| 149 | dl->recordSamplePoint("TP-base-correction-deg", correction); |
| 150 | |
| 151 | // within 1nm of the desired course, start to bias |
| 152 | // based on heading error. This is to reduce overshooting |
| 153 | // while still keeping the responsiveness high |
| 154 | if (fabs(crossTrack) < 1.0) { |
| 155 | // compensate for heading |
| 156 | double headingError = _targetCourseDeg - _trueCourseDeg; |
| 157 | SG_NORMALIZE_RANGE(headingError, -180.0, 180.0); |
| 158 | if (fabs(headingError) > 90.0) { |
| 159 | // we're pointing the wrong way, don't compensate |
| 160 | // otherwise we get into knots trying to make the |
| 161 | // turn back the right way |
| 162 | } else { |
| 163 | const double p = 1.0 - fabs(crossTrack); |
| 164 | const double headingErrorFactor = 0.6; |
| 165 | correction += p * headingError * headingErrorFactor; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | dl->recordSamplePoint("TP-correction-deg", correction); |
| 170 | |
| 171 | SG_CLAMP_RANGE(correction, -maxCorrectionAngle, maxCorrectionAngle); |
| 172 | _targetCourseDeg += correction; |
| 173 | |
| 174 | dl->recordSamplePoint("TP-target-deg", _targetCourseDeg); |
| 175 | |
| 176 | SG_NORMALIZE_RANGE(_targetCourseDeg, 0.0, 360.0); |
| 177 | if (!_turnActive &&(fabs(_trueCourseDeg - _targetCourseDeg) > 0.5)) { |
| 178 | _turnActive = true; |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | if (_gps && (_lateralMode == LateralMode::GPSOffset)) { |
| 183 | _targetCourseDeg = _gpsLegCourse->getDoubleValue(); |
| 184 | |
| 185 | double crossTrack = _courseErrorNm->getDoubleValue(); |
| 186 | double offsetError = crossTrack - _courseOffsetNm; |
| 187 | |
| 188 | const double offsetCorrectionFactor = 25.0; |
| 189 | const double correction = offsetError * offsetCorrectionFactor; |
| 190 | _targetCourseDeg += correction; |
| 191 | |
| 192 | SG_NORMALIZE_RANGE(_targetCourseDeg, 0.0, 360.0); |
| 193 | if (!_turnActive &&(fabs(_trueCourseDeg - _targetCourseDeg) > 0.5)) { |
| 194 | _turnActive = true; |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | if (_lateralMode == LateralMode::Direct) { |
| 199 | _targetCourseDeg = SGGeodesy::courseDeg(globals->get_aircraft_position(), _targetPos); |
| 200 | SG_NORMALIZE_RANGE(_targetCourseDeg, 0.0, 360.0); |
| 201 | if (!_turnActive && (fabs(_trueCourseDeg - _targetCourseDeg) > 0.5)) { |
| 202 | _turnActive = true; |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | if (_turnActive) { |
| 207 | if (fabs(_targetCourseDeg - _trueCourseDeg) < 0.1) { |
| 208 | _trueCourseDeg = _targetCourseDeg; |
| 209 | _turnActive = false; |
| 210 | } else { |
| 211 | // standard 2-minute turn, 180-deg min, thus 3-degrees per second |
| 212 | |
| 213 | double turnDeg = 5.0 * dt; |
| 214 | double errorDeg = _targetCourseDeg - _trueCourseDeg; |
| 215 | SG_NORMALIZE_RANGE(errorDeg, -180.0, 180.0); |
| 216 | |
| 217 | // clamp turn to error value |
| 218 | turnDeg = std::min(turnDeg, fabs(errorDeg)); |
| 219 | |
| 220 | // and now ensure we follow the correct sign |
| 221 | turnDeg = copysign(turnDeg, errorDeg); |
| 222 | |
| 223 | // simple integral |
| 224 | _trueCourseDeg += turnDeg; |
| 225 | SG_NORMALIZE_RANGE(_trueCourseDeg, 0.0, 360.0); |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | SGGeod currentPos = globals->get_aircraft_position(); |
| 230 | |
| 231 | const double M = flightgear::AircraftPerformance::machForCAS(currentPos.getElevationFt(), _speedKnots); |
| 232 | _speedMachProp->setDoubleValue(M); |
| 233 | const double gs = flightgear::AircraftPerformance::groundSpeedForMach(currentPos.getElevationFt(), M); |
| 234 | _groundspeedKnotsProp->setDoubleValue(gs); |
| 235 | double d = gs * SG_KT_TO_MPS * dt; |
| 236 | SGGeod newPos = SGGeodesy::direct(currentPos, _trueCourseDeg, d); |
| 237 | |
| 238 | if (_altActive) { |
| 239 | if (fabs(_targetAltitudeFt - currentPos.getElevationFt()) < 1) { |
| 240 | _altActive = false; |
| 241 | newPos.setElevationFt(_targetAltitudeFt); |
| 242 | } else { |
| 243 | double errorFt = _targetAltitudeFt - currentPos.getElevationFt(); |
| 244 | double vspeed = std::min(fabs(errorFt),_vspeedFPM * dt / 60.0); |
| 245 | double dv = copysign(vspeed, errorFt); |
| 246 | newPos.setElevationFt(currentPos.getElevationFt() + dv); |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | setPosition(newPos); |
| 251 | } |
| 252 | |
| 253 | void TestPilot::setPosition(const SGGeod& pos) |
| 254 | { |
| 255 | _latProp->setDoubleValue(pos.getLatitudeDeg()); |
| 256 | _lonProp->setDoubleValue(pos.getLongitudeDeg()); |
| 257 | _altitudeProp->setDoubleValue(pos.getElevationFt()); |
| 258 | |
| 259 | _headingProp->setDoubleValue(_trueCourseDeg); |
| 260 | _speedKnotsProp->setDoubleValue(_speedKnots); |
| 261 | _verticalFPMProp->setDoubleValue(_vspeedFPM); |
| 262 | } |
| 263 | |
| 264 | void TestPilot::setTargetAltitudeFtMSL(double altFt) |
| 265 | { |
| 266 | _targetAltitudeFt = altFt; |
| 267 | _altActive = true; |
| 268 | } |
| 269 | |
| 270 | bool TestPilot::isOnHeading(double heading) const |
| 271 | { |
| 272 | const double hdgDelta = (_trueCourseDeg - heading); |
| 273 | return fabs(hdgDelta) < 0.5; |
| 274 | } |
| 275 | |
| 276 | double TestPilot::trueCourseDeg() const |
| 277 | { |
| 278 | return _trueCourseDeg; |
| 279 | } |
| 280 | |
| 281 | |
| 282 | } // of namespace |