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