]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/rnav_waypt_controller.hxx
Fix flight-plan course and distance computations.
[flightgear.git] / src / Instrumentation / rnav_waypt_controller.hxx
1 // rnav_waypt_controller.hxx - Waypoint-specific behaviours for RNAV systems
2 // Written by James Turner, started 2009.
3 //
4 // Copyright (C) 2009  Curtis L. Olson
5 //
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License as
8 // published by the Free Software Foundation; either version 2 of the
9 // License, or (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful, but
12 // WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // 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, write to the Free Software
18 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19
20 #ifndef FG_WAYPT_CONTROLLER_HXX
21 #define FG_WAYPT_CONTROLLER_HXX
22
23 #include <Navaids/waypoint.hxx>
24
25 namespace flightgear
26 {
27
28 /**
29  * Abstract RNAV interface, for devices which implement an RNAV
30  * system - INS / GPS / FMS
31  */
32 class RNAV
33 {
34 public:
35   virtual SGGeod position() = 0;
36   
37   /**
38    * True track in degrees
39    */
40   virtual double trackDeg() = 0;
41   
42   /**
43    * Ground speed (along the track) in knots
44    */
45   virtual double groundSpeedKts() = 0;
46   
47   /**
48    * Vertical speed in ft/minute
49    */
50   virtual double vspeedFPM()= 0;
51   
52   /**
53    * Magnetic variation at current position
54    */
55   virtual double magvarDeg() = 0;
56   
57   /**
58    * device selected course (eg, from autopilot / MCP / OBS) in degrees
59    */
60   virtual double selectedMagCourse() = 0;
61
62   /**
63    * minimum distance to switch next waypoint.
64    */
65   virtual double overflightDistanceM() = 0;
66   /**
67    * minimum distance to a waypoint for overflight sequencing. 
68    */
69   virtual double overflightArmDistanceM() = 0;
70   /**
71      * angle for overflight sequencing.
72      */
73     virtual double overflightArmAngleDeg() = 0;
74
75   /**
76    * device leg previous waypoint position(eg, from route manager)
77    */
78   virtual SGGeod previousLegWaypointPosition(bool& isValid)= 0;
79
80 };
81
82 class WayptController
83 {
84 public:
85   virtual ~WayptController();
86   
87   virtual void init();
88
89   virtual void update() = 0;
90
91   /**
92    * Compute time until the waypoint is done
93    */
94   virtual double timeToWaypt() const;
95
96   /**
97    * Compute distance until the waypoint is done
98    */
99   virtual double distanceToWayptM() const = 0;
100
101   /**
102    * Bearing to the waypoint, if this value is meaningful.
103    * Default implementation returns the target track
104    */
105   virtual double trueBearingDeg() const
106     { return _targetTrack; }
107
108   virtual double targetTrackDeg() const 
109     { return _targetTrack; }
110
111   virtual double xtrackErrorNm() const
112     { return 0.0; }
113
114   virtual double courseDeviationDeg() const
115     { return 0.0; }
116
117   /**
118    * Position associated with the waypt. For static waypoints, this is
119    * simply the waypoint position itself; for dynamic points, it's the
120    * estimated location at which the controller will be done.
121    */
122   virtual SGGeod position() const = 0;
123
124   /**
125    * Is this controller finished?
126    */
127   bool isDone() const
128     { return _isDone; }
129
130   /**
131    * to/from flag - true = to, false = from. Defaults to 'true' because
132    * nearly all waypoint controllers become done as soon as this value would
133    * become false.
134    */
135   virtual bool toFlag() const
136     { return true; }
137     
138   /**
139    * Static factory method, given a waypoint, return a controller bound
140    * to it, of the appropriate type
141    */
142   static WayptController* createForWaypt(RNAV* rnav, const WayptRef& aWpt);
143 protected:
144   WayptController(RNAV* aRNAV, const WayptRef& aWpt) :
145     _waypt(aWpt),
146     _targetTrack(0),
147     _rnav(aRNAV),
148     _isDone(false)
149   { }
150   
151   WayptRef _waypt;
152   double _targetTrack;
153   RNAV* _rnav;
154   
155   void setDone();
156 private:
157   bool _isDone;
158 };
159
160 /**
161  * Controller supports 'directTo' (DTO) navigation to a waypoint. This
162  * creates a course from a starting point, to the waypoint, and reports
163  * deviation from that course.
164  *
165  * The controller is done when the waypoint is reached (to/from goes to 'from')
166  */
167 class DirectToController : public WayptController
168 {
169 public:
170   DirectToController(RNAV* aRNAV, const WayptRef& aWpt, const SGGeod& aOrigin);
171   
172   virtual void init();
173   virtual void update();
174   virtual double distanceToWayptM() const;  
175   virtual double xtrackErrorNm() const;  
176   virtual double courseDeviationDeg() const;  
177   virtual double trueBearingDeg() const;
178   virtual SGGeod position() const;
179 private:
180   SGGeod _origin;
181   double _distanceAircraftTargetMeter;
182   double _courseDev;
183   double _courseAircraftToTarget;
184 };
185
186 /**
187  *
188  */
189 class OBSController : public WayptController
190 {
191 public:
192   OBSController(RNAV* aRNAV, const WayptRef& aWpt);
193   
194   virtual void init();
195   virtual void update();
196   virtual double distanceToWayptM() const;  
197   virtual double xtrackErrorNm() const;  
198   virtual double courseDeviationDeg() const;  
199   virtual double trueBearingDeg() const;
200   virtual bool toFlag() const;
201   virtual SGGeod position() const;
202 private:
203   double _distanceAircraftTargetMeter;
204   double _courseDev;
205   double _courseAircraftToTarget;
206 };
207
208 } // of namespace flightgear
209
210 #endif