]> git.mxchange.org Git - flightgear.git/blob - src/ATC/approach.hxx
Bugfix. The engine thrust is recalculated based on the current N1 value
[flightgear.git] / src / ATC / approach.hxx
1 // approach.hxx -- Approach class
2 //
3 // Written by Alexander Kappes, started March 2002.
4 //
5 // Copyright (C) 2002  Alexander Kappes
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20
21
22 #ifndef _FG_APPROACH_HXX
23 #define _FG_APPROACH_HXX
24
25 #include <stdio.h>
26
27 #include <simgear/compiler.h>
28 #include <simgear/math/sg_geodesy.hxx>
29 #include <simgear/misc/sgstream.hxx>
30 #include <simgear/magvar/magvar.hxx>
31 #include <simgear/timing/sg_time.hxx>
32 #include <simgear/bucket/newbucket.hxx>
33
34 #include <Main/fg_props.hxx>
35
36 #ifdef SG_HAVE_STD_INCLUDES
37 #  include <istream>
38 #include <iomanip>
39 #elif defined( SG_HAVE_NATIVE_SGI_COMPILERS )
40 #  include <iostream.h>
41 #elif defined( __BORLANDC__ ) || (__APPLE__)
42 #  include <iostream>
43 #else
44 #  include <istream.h>
45 #include <iomanip.h>
46 #endif
47
48 #if ! defined( SG_HAVE_NATIVE_SGI_COMPILERS )
49 SG_USING_STD(istream);
50 #endif
51
52 SG_USING_STD(string);
53
54 #include "ATC.hxx"
55
56 //DCL - a complete guess for now.
57 #define FG_APPROACH_DEFAULT_RANGE 100
58
59 // Contains all information about a plane that the approach control needs
60 const int max_planes = 20;  // max number of planes on the stack
61 const int max_wp = 10;      // max number of waypoints for approach phase
62 struct PlaneApp {
63
64   // variables for plane if it's on the radar
65   string ident;          // indentification of plane
66   double lon;            // longitude in degrees
67   double lat;            // latitude in degrees
68   double alt;            // Altitute above sea level in feet
69   double hdg;            // heading of plane in degrees
70   double dist;           // distance to airport in miles
71   double brg;            // bearing relative to airport in degrees
72   double spd;            // speed above ground
73   int    contact;        // contact with approach established?
74                          // 0 = no contact yet
75                          // 1 = in contact
76                          // 2 = handed off to tower
77
78   // additional variables if contact has been established
79   int    wpn;                 // number of waypoints
80   double wpts[max_wp][6];     // assigned waypoints for approach phase 
81                               // first wp in list is airport
82                               // last waypoint point at which contact was established
83                               // second index: 0 = bearing to airport
84                               // second index: 1 = distance to aiport
85                               // second index: 2 = alt 
86                               // second index: 3 = ETA
87                               // second index: 4 = heading to next waypoint
88                               // second index: 5 = distance to next waypoint
89
90   double dnwp;           // distance to next waypoint
91   double dcc;            // closest distance to current assigned course
92   double dnc;            // closest distance to course from next to next to next wp
93   double aalt;           // assigned alt at next waypoint
94   double ahdg;           // assigned heading
95   bool   on_crs;         // is the plane on course?
96   double tlm;            // time when last message was sent
97 };
98
99
100 class FGApproach : public FGATC {
101
102   char     type;
103   double   lon, lat;
104   double   elev;
105   double   x, y, z;
106   int      freq;
107   int      bucket;
108   double   range;
109
110   string active_runway;         
111   double active_rw_hdg;
112
113   bool     display;             // Flag to indicate whether we should be outputting to the display.
114   bool     displaying;          // Flag to indicate whether we are outputting to the display.
115   string   ident;               // Code of the airport its at.
116   string   name;                // Name transmitted in the broadcast.
117   int      num_planes;          // number of planes on the stack
118   PlaneApp planes[max_planes];  // Array of planes
119   string   transmission;
120   bool     first;
121
122   SGPropertyNode *comm1_node;
123   SGPropertyNode *comm2_node;
124
125   // for failure modeling
126   string trans_ident;   // transmitted ident
127   bool approach_failed; // approach failed?
128
129 public:
130
131   FGApproach(void);
132   ~FGApproach(void);
133
134   void Init();
135
136   void Update();
137
138   // Add new plane to stack if not already registered 
139   // Input:  pid - id of plane (name) 
140   // Output: "true" if added; "false" if already existend
141   void AddPlane(string pid);
142
143   // Remove plane from stack if out of range
144   int RemovePlane();
145   
146   //Indicate that this instance should be outputting to the ATC display
147   inline void SetDisplay(void) {display = true;}
148   
149   //Indicate that this instance should not be outputting to the ATC display
150   inline void SetNoDisplay(void) {display = false;}
151   
152   inline char get_type() const { return type; }
153   inline double get_lon() const { return lon; }
154   inline double get_lat() const { return lat; }
155   inline double get_elev() const { return elev; }
156   inline double get_x() const { return x; }
157   inline double get_y() const { return y; }
158   inline double get_z() const { return z; }
159   inline double get_bucket() const { return bucket; }
160   inline int get_freq() const { return freq; }
161   inline double get_range() const { return range; }
162   inline int get_pnum() const { return num_planes; }
163   inline const char* GetIdent() { return ident.c_str(); }
164   inline string get_trans_ident() { return trans_ident; }
165   inline string get_name() { return name; }
166   inline atc_type GetType() { return APPROACH; }
167   
168 private:
169
170   void update_plane_dat();
171
172   void get_active_runway();
173
174 // ========================================================================
175 // get heading and distance between two points; point2 ---> point1
176 // input: point1 = heading in degrees, distance
177 // input: point2 = heading in degrees, distance
178 // ouput: course in degrees, distance
179 // ========================================================================
180   void calc_hd_course_dist(const double &h1, const double &d1,
181                            const double &h2, const double &d2,
182                            double *course, double *dist);
183
184 // ========================================================================
185 // closest distance between a point and a straigt line in 2 dim.
186 // the input variables are given in (heading, distance) 
187 // relative to a common point
188 // input:  point        = heading in degrees, distance
189 // input:  straigt line = anker vector (heading in degrees, distance), 
190 //                        heading of direction vector
191 // output: distance
192 // ========================================================================
193   double calc_psl_dist(const double &h1, const double &d1,
194                        const double &h2, const double &d2,
195                        const double &h3);
196
197   // Pointers to current users position
198   SGPropertyNode *lon_node;
199   SGPropertyNode *lat_node;
200   SGPropertyNode *elev_node;
201   
202   //Update the transmission string
203   void UpdateTransmission(void);
204   
205   friend istream& operator>> ( istream&, FGApproach& );
206 };
207
208
209 inline istream&
210 operator >> ( istream& in, FGApproach& a )
211 {
212     double f;
213     char ch;
214
215     static bool first_time = true;
216     static double julian_date = 0;
217     static const double MJD0    = 2415020.0;
218     if ( first_time ) {
219         julian_date = sgTimeCurrentMJD(0, 0) + MJD0;
220         first_time = false;
221     }
222
223     in >> a.type;
224     
225     if ( a.type == '[' )
226       return in >> skipeol;
227
228     in >> a.lat >> a.lon >> a.elev >> f >> a.range 
229        >> a.ident;
230
231     a.name = "";
232     in >> ch;
233     a.name += ch;
234     while(1) {
235         //in >> noskipws
236         in.unsetf(ios::skipws);
237         in >> ch;
238         a.name += ch;
239         if((ch == '"') || (ch == 0x0A)) {
240             break;
241         }   // we shouldn't need the 0x0A but it makes a nice safely in case someone leaves off the "
242     }
243     in.setf(ios::skipws);
244     //cout << "approach.name = " << a.name << '\n';
245
246     a.freq = (int)(f*100.0 + 0.5);
247
248     // generate cartesian coordinates
249     Point3D geod( a.lon * SGD_DEGREES_TO_RADIANS , a.lat * SGD_DEGREES_TO_RADIANS, 
250                   a.elev * SG_FEET_TO_METER );
251     Point3D cart = sgGeodToCart( geod );
252     a.x = cart.x();
253     a.y = cart.y();
254     a.z = cart.z();
255
256     // get bucket number
257     SGBucket buck(a.lon, a.lat);
258     a.bucket = buck.gen_index();
259
260     a.trans_ident = a.ident;
261     a.approach_failed = false;
262
263     return in >> skipeol;
264 }
265
266 #endif // _FG_APPROACH_HXX