]> git.mxchange.org Git - flightgear.git/blob - src/ATC/approach.hxx
Lots of changes to the ATC/AI system for initial revision of random AI GA VFR traffic
[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__ )
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 #include "transmission.hxx"
56
57 //DCL - a complete guess for now.
58 #define FG_APPROACH_DEFAULT_RANGE 100
59
60 // Contains all the information about a plane that the approach control needs
61 const int    max_planes = 20;  // max number of planes on the stack
62 const int    max_wp = 10;      // max number of waypoints for approach phase
63 const double max_ta = 130;     // max turning angle for plane during approach
64 const double tbm    = 20000.0; // min time (in ms) between two messages
65 const double lfl    = 10.0;    // length of final leg
66
67 struct PlaneApp {
68
69   // variables for plane if it's on the radar
70   string ident;          // indentification of plane
71   double lon;            // longitude in degrees
72   double lat;            // latitude in degrees
73   double alt;            // Altitute above sea level in feet
74   double hdg;            // heading of plane in degrees
75   double dist;           // distance to airport in miles
76   double brg;            // bearing relative to airport in degrees
77   double spd;            // speed above ground
78   int    contact;        // contact with approach established?
79                          // 0 = no contact yet
80                          // 1 = in contact
81                          // 2 = handed off to tower
82   double turn_rate;      // standard turning rate of the plane in seconds per degree
83   double desc_rate;      // standard descent rate of the plane in feets per minute
84   double clmb_rate;      // standard climb rate of the plane in feets per minute
85
86   // additional variables if contact has been established
87   int    wpn;                 // number of waypoints
88   double wpts[max_wp][6];     // assigned waypoints for approach phase 
89                               // first wp in list is airport
90                               // last waypoint point at which contact was established
91                               // second index: 0 = bearing to airport
92                               // second index: 1 = distance to airport
93                               // second index: 2 = alt 
94                               // second index: 3 = ETA
95                               // second index: 4 = heading to next waypoint
96                               // second index: 5 = distance to next waypoint
97
98   double dnwp;           // distance to next waypoint
99   double dcc;            // closest distance to current assigned course
100   double dnc;            // closest distance to course from next to next to next wp
101   double aalt;           // assigned altitude
102   double ahdg;           // assigned heading
103   bool   on_crs;         // is the plane on course?
104   bool   wp_change;      // way point has changed
105   double tlm;            // time when last message was sent
106   TransCode lmc;         // code of last message
107 };
108
109
110 class FGApproach : public FGATC {
111
112   int      bucket;
113
114   string active_runway;         
115   double active_rw_hdg;
116   double active_rw_lon;
117   double active_rw_lat;
118   double active_rw_len;
119
120   bool     display;             // Flag to indicate whether we should be outputting to the display.
121   bool     displaying;          // Flag to indicate whether we are outputting to the display.
122   int      num_planes;          // number of planes on the stack
123   PlaneApp planes[max_planes];  // Array of planes
124   string   transmission;
125   bool     first;
126
127   SGPropertyNode *comm1_node;
128   SGPropertyNode *comm2_node;
129
130   SGPropertyNode *atcmenu_node;
131   SGPropertyNode *atcopt0_node;
132   SGPropertyNode *atcopt1_node;
133   SGPropertyNode *atcopt2_node;
134   SGPropertyNode *atcopt3_node;
135   SGPropertyNode *atcopt4_node;
136   SGPropertyNode *atcopt5_node;
137   SGPropertyNode *atcopt6_node;
138   SGPropertyNode *atcopt7_node;
139   SGPropertyNode *atcopt8_node;
140   SGPropertyNode *atcopt9_node;
141
142   // for failure modeling
143   string trans_ident;   // transmitted ident
144   bool approach_failed; // approach failed?
145
146 public:
147
148   FGApproach(void);
149   ~FGApproach(void);
150
151   void Init();
152
153   void Update(double dt);
154
155   // Add new plane to stack if not already registered 
156   // Input:  pid - id of plane (name) 
157   // Output: "true" if added; "false" if already existend
158   void AddPlane(string pid);
159
160   // Remove plane from stack if out of range
161   int RemovePlane();
162   
163   //Indicate that this instance should be outputting to the ATC display
164   inline void SetDisplay(void) {display = true;}
165   
166   //Indicate that this instance should not be outputting to the ATC display
167   inline void SetNoDisplay(void) {display = false;}
168   
169   inline double get_bucket() const { return bucket; }
170   inline int get_pnum() const { return num_planes; }
171   inline string get_trans_ident() { return trans_ident; }
172   
173 private:
174
175   void calc_wp( const int &i);
176
177   void update_plane_dat();
178
179   void get_active_runway();
180
181   void update_param(const int &i);
182
183   double round_alt( bool hl, double alt );
184
185   double angle_diff_deg( const double &a1, const double &a2);
186
187 // ========================================================================
188 // get point2 given starting point1 and course and distance
189 // input:  point1 = heading in degrees, distance
190 // input:  course in degrees, distance
191 // output: point2 = heading in degrees, distance
192 // ========================================================================
193   void calc_cd_head_dist(const double &h1, const double &d1,
194                          const double &course, const double &dist,
195                          double *h2, double *d2);
196
197
198 // ========================================================================
199 // get heading and distance between two points; point2 ---> point1
200 // input:  point1 = heading in degrees, distance
201 // input:  point2 = heading in degrees, distance
202 // output: course in degrees, distance
203 // ========================================================================
204   void calc_hd_course_dist(const double &h1, const double &d1,
205                            const double &h2, const double &d2,
206                            double *course, double *dist);
207
208
209
210 // ========================================================================
211 // closest distance between a point and a straigt line in 2 dim.
212 // the input variables are given in (heading, distance) 
213 // relative to a common point
214 // input:  point        = heading in degrees, distance
215 // input:  straigt line = anker vector (heading in degrees, distance), 
216 //                        heading of direction vector
217 // output: distance
218 // ========================================================================
219   double calc_psl_dist(const double &h1, const double &d1,
220                        const double &h2, const double &d2,
221                        const double &h3);
222
223   // Pointers to current users position
224   SGPropertyNode *lon_node;
225   SGPropertyNode *lat_node;
226   SGPropertyNode *elev_node;
227   SGPropertyNode *hdg_node;
228   SGPropertyNode *speed_node;
229   SGPropertyNode *etime_node;
230   
231   //Update the transmission string
232   void UpdateTransmission(void);
233   
234   friend istream& operator>> ( istream&, FGApproach& );
235 };
236
237 #endif // _FG_APPROACH_HXX