| 1 | /* |
| 2 | * SPDX-License-Identifier: GPL-2.0+ |
| 3 | * SPDX-FileCopyrightText: 2009 (C) Curtis L. Olson |
| 4 | * |
| 5 | * rnav_waypt_controller.hxx - Waypoint-specific behaviours for RNAV systems |
| 6 | * Written by James Turner, started 2009. |
| 7 | * |
| 8 | * Copyright (C) 2009 Curtis L. Olson |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or |
| 11 | * modify it under the terms of the GNU General Public License as |
| 12 | * published by the Free Software Foundation; either version 2 of the |
| 13 | * License, or (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, but |
| 16 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 18 | * General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License |
| 21 | * along with this program; if not, write to the Free Software |
| 22 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 23 | */ |
| 24 | |
| 25 | #pragma once |
| 26 | |
| 27 | #include <Navaids/waypoint.hxx> |
| 28 | #include <simgear/misc/simgear_optional.hxx> |
| 29 | |
| 30 | namespace flightgear |
| 31 | { |
| 32 | |
| 33 | /** |
| 34 | * Abstract RNAV interface, for devices which implement an RNAV |
| 35 | * system - INS / GPS / FMS |
| 36 | */ |
| 37 | class RNAV |
| 38 | { |
| 39 | public: |
| 40 | virtual SGGeod position() = 0; |
| 41 | |
| 42 | /** |
| 43 | * True track in degrees |
| 44 | */ |
| 45 | virtual double trackDeg() = 0; |
| 46 | |
| 47 | /** |
| 48 | * Ground speed (along the track) in knots |
| 49 | */ |
| 50 | virtual double groundSpeedKts() = 0; |
| 51 | |
| 52 | /** |
| 53 | * Vertical speed in ft/minute |
| 54 | */ |
| 55 | virtual double vspeedFPM()= 0; |
| 56 | |
| 57 | /** |
| 58 | * Magnetic variation at current position |
| 59 | */ |
| 60 | virtual double magvarDeg() = 0; |
| 61 | |
| 62 | /** |
| 63 | * device selected course (eg, from autopilot / MCP / OBS) in degrees |
| 64 | */ |
| 65 | virtual double selectedMagCourse() = 0; |
| 66 | |
| 67 | virtual bool canFlyBy() const |
| 68 | { |
| 69 | return false; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * maximum angle in degrees where flyBy is permitted. Turns larger |
| 74 | * than this value will always be executed as fly-over to avoid |
| 75 | * a very large cut. |
| 76 | */ |
| 77 | virtual double maxFlyByTurnAngleDeg() const |
| 78 | { |
| 79 | return 90.0; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * minimum distance to switch next waypoint. |
| 84 | */ |
| 85 | virtual double overflightDistanceM() = 0; |
| 86 | /** |
| 87 | * minimum distance to a waypoint for overflight sequencing. |
| 88 | */ |
| 89 | virtual double overflightArmDistanceM() = 0; |
| 90 | /** |
| 91 | * angle for overflight sequencing. |
| 92 | */ |
| 93 | virtual double overflightArmAngleDeg() = 0; |
| 94 | |
| 95 | struct LegData { |
| 96 | SGGeod position; |
| 97 | bool didFlyBy = false; |
| 98 | SGGeod flyByTurnCenter; |
| 99 | double flyByRadius = 0.0; |
| 100 | double turnAngle = 0.0; |
| 101 | }; |
| 102 | |
| 103 | /** |
| 104 | * device leg previous waypoint position(eg, from route manager) |
| 105 | */ |
| 106 | virtual simgear::optional<LegData> previousLegData() |
| 107 | { |
| 108 | return simgear::optional<LegData>(); |
| 109 | } |
| 110 | |
| 111 | virtual simgear::optional<double> nextLegTrack() |
| 112 | { |
| 113 | return simgear::optional<double>{}; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * @brief compute turn radius based on current ground-speed |
| 118 | */ |
| 119 | |
| 120 | double turnRadiusNm() |
| 121 | { |
| 122 | return turnRadiusNm(groundSpeedKts()); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * @brief compute the turn radius (based on standard rate turn) for |
| 127 | * a given ground speed in knots. |
| 128 | */ |
| 129 | virtual double turnRadiusNm(const double groundSpeedKnots) = 0; |
| 130 | }; |
| 131 | |
| 132 | class WayptController |
| 133 | { |
| 134 | public: |
| 135 | virtual ~WayptController(); |
| 136 | |
| 137 | virtual bool init(); |
| 138 | |
| 139 | virtual void update(double dt) = 0; |
| 140 | |
| 141 | /** |
| 142 | * Compute time until the waypoint is done |
| 143 | */ |
| 144 | virtual double timeToWaypt() const; |
| 145 | |
| 146 | /** |
| 147 | * Compute distance until the waypoint is done |
| 148 | */ |
| 149 | virtual double distanceToWayptM() const = 0; |
| 150 | |
| 151 | /** |
| 152 | * Bearing to the waypoint, if this value is meaningful. |
| 153 | * Default implementation returns the target track |
| 154 | */ |
| 155 | virtual double trueBearingDeg() const; |
| 156 | |
| 157 | virtual double targetTrackDeg() const; |
| 158 | |
| 159 | virtual double xtrackErrorNm() const; |
| 160 | |
| 161 | virtual double courseDeviationDeg() const; |
| 162 | |
| 163 | /** |
| 164 | * Position associated with the waypt. For static waypoints, this is |
| 165 | * simply the waypoint position itself; for dynamic points, it's the |
| 166 | * estimated location at which the controller will be done. |
| 167 | */ |
| 168 | virtual SGGeod position() const = 0; |
| 169 | |
| 170 | /** |
| 171 | * Is this controller finished? |
| 172 | */ |
| 173 | bool isDone() const; |
| 174 | |
| 175 | /** |
| 176 | * to/from flag - true = to, false = from. Defaults to 'true' because |
| 177 | * nearly all waypoint controllers become done as soon as this value would |
| 178 | * become false. |
| 179 | */ |
| 180 | virtual bool toFlag() const |
| 181 | { return true; } |
| 182 | |
| 183 | /** |
| 184 | * Allow waypoints to indicate a status value as a string. |
| 185 | * Useful for more complex controllers, which may have capture / exit |
| 186 | * states |
| 187 | */ |
| 188 | virtual std::string status() const; |
| 189 | |
| 190 | virtual simgear::optional<RNAV::LegData> legData() const |
| 191 | { |
| 192 | // defer to our subcontroller if it exists |
| 193 | if (_subController) |
| 194 | return _subController->legData(); |
| 195 | |
| 196 | return simgear::optional<RNAV::LegData>(); |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Static factory method, given a waypoint, return a controller bound |
| 201 | * to it, of the appropriate type |
| 202 | */ |
| 203 | static WayptController* createForWaypt(RNAV* rnav, const WayptRef& aWpt); |
| 204 | |
| 205 | WayptRef waypoint() const |
| 206 | { return _waypt; } |
| 207 | |
| 208 | protected: |
| 209 | WayptController(RNAV* aRNAV, const WayptRef& aWpt) : |
| 210 | _waypt(aWpt), |
| 211 | _targetTrack(0), |
| 212 | _rnav(aRNAV), |
| 213 | _isDone(false) |
| 214 | { } |
| 215 | |
| 216 | WayptRef _waypt; |
| 217 | double _targetTrack; |
| 218 | RNAV* _rnav; |
| 219 | |
| 220 | void setDone(); |
| 221 | |
| 222 | // take asubcontroller ref (will be destroyed automatically) |
| 223 | // pass nullptr to clear any activ esubcontroller |
| 224 | // the subcontroller will be initialised |
| 225 | void setSubController(WayptController* sub); |
| 226 | |
| 227 | // if a subcontroller exists, we can delegate to it automatically |
| 228 | std::unique_ptr<WayptController> _subController; |
| 229 | private: |
| 230 | bool _isDone; |
| 231 | }; |
| 232 | |
| 233 | /** |
| 234 | * Controller supports 'directTo' (DTO) navigation to a waypoint. This |
| 235 | * creates a course from a starting point, to the waypoint, and reports |
| 236 | * deviation from that course. |
| 237 | * |
| 238 | * The controller is done when the waypoint is reached (to/from goes to 'from') |
| 239 | */ |
| 240 | class DirectToController : public WayptController |
| 241 | { |
| 242 | public: |
| 243 | DirectToController(RNAV* aRNAV, const WayptRef& aWpt, const SGGeod& aOrigin); |
| 244 | |
| 245 | bool init() override; |
| 246 | virtual void update(double dt); |
| 247 | virtual double distanceToWayptM() const; |
| 248 | virtual double xtrackErrorNm() const; |
| 249 | virtual double courseDeviationDeg() const; |
| 250 | virtual double trueBearingDeg() const; |
| 251 | virtual SGGeod position() const; |
| 252 | private: |
| 253 | SGGeod _origin; |
| 254 | double _distanceAircraftTargetMeter; |
| 255 | double _courseDev; |
| 256 | double _courseAircraftToTarget; |
| 257 | }; |
| 258 | |
| 259 | /** |
| 260 | * |
| 261 | */ |
| 262 | class OBSController : public WayptController |
| 263 | { |
| 264 | public: |
| 265 | OBSController(RNAV* aRNAV, const WayptRef& aWpt); |
| 266 | |
| 267 | bool init() override; |
| 268 | virtual void update(double dt); |
| 269 | virtual double distanceToWayptM() const; |
| 270 | virtual double xtrackErrorNm() const; |
| 271 | virtual double courseDeviationDeg() const; |
| 272 | virtual double trueBearingDeg() const; |
| 273 | virtual bool toFlag() const; |
| 274 | virtual SGGeod position() const; |
| 275 | private: |
| 276 | double _distanceAircraftTargetMeter; |
| 277 | double _courseDev; |
| 278 | double _courseAircraftToTarget; |
| 279 | }; |
| 280 | |
| 281 | class HoldCtl : public WayptController |
| 282 | { |
| 283 | enum HoldState { |
| 284 | HOLD_INIT, |
| 285 | LEG_TO_HOLD, |
| 286 | ENTRY_DIRECT, |
| 287 | ENTRY_PARALLEL, // flying the outbound part of a parallel entry |
| 288 | ENTRY_PARALLEL_OUTBOUND, |
| 289 | ENTRY_PARALLEL_INBOUND, |
| 290 | ENTRY_TEARDROP, // flying the outbound leg of teardrop entry |
| 291 | HOLD_OUTBOUND, |
| 292 | HOLD_INBOUND, |
| 293 | HOLD_EXITING, // we are going to exit the hold |
| 294 | }; |
| 295 | |
| 296 | HoldState _state = HOLD_INIT; |
| 297 | double _holdCourse = 0.0; |
| 298 | double _holdLegTime = 60.0; |
| 299 | double _holdLegDistance = 0.0; |
| 300 | double _holdCount = 0; |
| 301 | bool _leftHandTurns = false; |
| 302 | |
| 303 | bool _inTurn = false; |
| 304 | SGGeod _turnCenter; |
| 305 | double _turnEndAngle, _turnRadius; |
| 306 | SGGeod _segmentEnd; |
| 307 | |
| 308 | bool checkOverHold(); |
| 309 | void checkInitialEntry(double dNm); |
| 310 | |
| 311 | void startInboundTurn(); |
| 312 | void startOutboundTurn(); |
| 313 | void startParallelEntryTurn(); |
| 314 | void exitTurn(); |
| 315 | |
| 316 | SGGeod outboundEndPoint(); |
| 317 | SGGeod outboundTurnCenter(); |
| 318 | SGGeod inboundTurnCenter(); |
| 319 | |
| 320 | double holdLegLengthNm() const; |
| 321 | |
| 322 | /** |
| 323 | * are we turning left? This is basically the )leftHandTurns member, |
| 324 | * but if we're in the inbound turn of a parallel entry, it's flipped |
| 325 | */ |
| 326 | bool inLeftTurn() const; |
| 327 | |
| 328 | void computeEntry(); |
| 329 | public: |
| 330 | HoldCtl(RNAV* aRNAV, const WayptRef& aWpt); |
| 331 | |
| 332 | void setHoldCount(int count); |
| 333 | void exitHold(); |
| 334 | |
| 335 | bool init() override; |
| 336 | void update(double) override; |
| 337 | |
| 338 | double distanceToWayptM() const override; |
| 339 | SGGeod position() const override; |
| 340 | double xtrackErrorNm() const override; |
| 341 | double courseDeviationDeg() const override; |
| 342 | |
| 343 | std::string status() const override; |
| 344 | }; |
| 345 | |
| 346 | } // of namespace flightgear |