]> git.mxchange.org Git - flightgear.git/blob - src/ATC/approach.cxx
5d9670687398c047b15a87c57f5aebe88a2e30be
[flightgear.git] / src / ATC / approach.cxx
1 // FGApproach - a class to provide approach control at larger airports.
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 #include "approach.hxx"
22 #include "ATCdisplay.hxx"
23 #include <Airports/runways.hxx>
24
25 #include <simgear/misc/sg_path.hxx>
26 #include <WeatherCM/FGLocalWeatherDatabase.h>
27
28
29 //Constructor
30 FGApproach::FGApproach(){
31   comm1_node = fgGetNode("/radios/comm[0]/frequencies/selected-mhz", true);
32   comm2_node = fgGetNode("/radios/comm[1]/frequencies/selected-mhz", true);
33
34   num_planes = 0;
35   lon_node = fgGetNode("/position/longitude-deg", true);
36   lat_node = fgGetNode("/position/latitude-deg", true);
37   elev_node = fgGetNode("/position/altitude-ft", true);
38   first = true;
39   active_runway = "";
40   for ( int i=0; i<max_planes; i++) {
41     planes[i].contact = 0;
42     planes[i].wpn     = 0;
43     planes[i].dnwp    = -999.;
44     planes[i].on_crs  = true;
45   }
46 }
47
48 //Destructor
49 FGApproach::~FGApproach(){
50 }
51
52 void FGApproach::Init() {
53   display    = false;
54 }
55
56 // ============================================================================
57 // the main update function
58 // ============================================================================
59 void FGApproach::Update() {
60
61   int wpn;
62   double course, d;
63
64   update_plane_dat();
65   if ( active_runway == "" ) get_active_runway();
66   
67   for ( int i=0; i<num_planes; i++ ) {
68     
69     if ( planes[i].contact == 0) {
70       double comm1_freq = comm1_node->getDoubleValue();
71       if ( (int)(comm1_freq*100.0 + 0.5) == freq ) planes[i].contact = 1;
72     }
73     else if ( planes[i].contact == 1 ) {
74       if ( planes[i].wpn == 0 ) {    // calculate initial waypoints
75         wpn = planes[i].wpn;
76         // airport
77         planes[i].wpts[wpn][0] = active_rw_hdg;
78         planes[i].wpts[wpn][1] = 0.0;
79         planes[i].wpts[wpn][2] = elev;
80         planes[i].wpts[wpn][4] = 0.0;
81         planes[i].wpts[wpn][5] = 0.0;
82         wpn += 1;
83
84         planes[i].wpts[wpn][0] = active_rw_hdg + 180.0;
85         if ( planes[i].wpts[wpn][0] > 360.0 ) planes[i].wpts[wpn][0] -= 360.0;
86         planes[i].wpts[wpn][1] = 5;
87         planes[i].wpts[wpn][2] = elev + 1000.0;
88         calc_hd_course_dist(planes[i].wpts[wpn][0],   planes[i].wpts[wpn][1],
89                             planes[i].wpts[wpn-1][0], planes[i].wpts[wpn-1][1],
90                             &course, &d);
91         planes[i].wpts[wpn][4] = course;
92         planes[i].wpts[wpn][5] = d;
93         wpn += 1;
94         
95         planes[i].wpts[wpn][0] = planes[i].brg;
96         planes[i].wpts[wpn][1] = planes[i].dist;
97         planes[i].wpts[wpn][2] = planes[i].alt;
98         calc_hd_course_dist(planes[i].wpts[wpn][0],   planes[i].wpts[wpn][1],
99                             planes[i].wpts[wpn-1][0], planes[i].wpts[wpn-1][1],
100                             &course, &d);
101         planes[i].wpts[wpn][4] = course;
102         planes[i].wpts[wpn][5] = d;
103         wpn += 1;
104
105         planes[i].wpn = wpn;
106
107         planes[i].ahdg = planes[i].wpts[wpn-1][4];
108         cout << endl;
109         cout << "Contact " << planes[i].wpn << endl;
110         cout << "Turn to heading   = " << (int)(planes[i].ahdg) << endl;
111         cout << endl;
112         planes[i].on_crs = true;
113       }
114
115       // reached waypoint?
116       if ( fabs(planes[i].dnc) < 0.3 && planes[i].dnwp < 1.0 ) {
117         planes[i].wpn -= 1;
118         wpn = planes[i].wpn-1;
119         planes[i].ahdg = planes[i].wpts[wpn][4];
120         cout << endl;
121         cout << "Next waypoint = " << planes[i].wpn << endl;
122         cout << "New heading   = " << planes[i].ahdg << endl;
123         cout << endl;
124         planes[i].on_crs = true;
125       }
126
127       // update assigned parameters
128       wpn = planes[i].wpn-1;            // this is the current waypoint
129
130       planes[i].dcc  = calc_psl_dist(planes[i].brg, planes[i].dist,
131                                      planes[i].wpts[wpn][0], planes[i].wpts[wpn][1],
132                                      planes[i].wpts[wpn][4]);
133       planes[i].dnc  = calc_psl_dist(planes[i].brg, planes[i].dist,
134                                      planes[i].wpts[wpn-1][0], planes[i].wpts[wpn-1][1],
135                                      planes[i].wpts[wpn-1][4]);
136       calc_hd_course_dist(planes[i].brg, planes[i].dist, 
137                           planes[i].wpts[wpn-1][0], planes[i].wpts[wpn-1][1],
138                           &course, &d);
139       planes[i].dnwp = d;
140
141       //cout << planes[i].brg << " " << planes[i].dist << " " << planes[i].wpts[wpn+1][0] 
142       //<< " " << planes[i].wpts[wpn+1][1] << " " << planes[i].wpts[wpn+1][4] 
143       //cout << " distance to current course = " << planes[i].dcc << endl;
144
145       // come off course ?
146       if ( fabs(planes[i].dcc) > 0.5 && planes[i].on_crs) {
147         wpn = wpn-1;
148         if ( planes[i].wpts[wpn][4] < 0) {
149           planes[i].ahdg += 30.0;
150         }
151         else {
152           planes[i].ahdg -= 30.0;
153         }
154         planes[i].on_crs = false;
155
156         cout << endl;
157         cout << "Your are " << planes[i].dcc << " miles off the asigned course: " << endl;
158         cout << "New heading = " << (int)(planes[i].ahdg) << endl;
159         cout << endl;
160       }
161       else if ( fabs(planes[i].dcc) < 0.1 && !planes[i].on_crs) {
162         planes[i].ahdg = fabs(planes[i].wpts[wpn][4]);
163         planes[i].on_crs = true;
164         
165         cout << endl;
166         cout << "New heading = " << (int)(planes[i].ahdg) << endl;
167         cout << endl;
168         }
169       
170       // In range of tower?
171       if ( planes[i].wpn == 2 && planes[i].dnwp < 3. ) {
172         cout << endl;
173         cout << "Contact Tower";
174         cout << endl;
175         planes[i].contact = 2;
176       }
177     }
178   }
179
180 }
181
182 // ============================================================================
183 // get active runway
184 // ============================================================================
185 void FGApproach::get_active_runway() {
186
187   sgVec3 position = { lat, lon, elev };
188   FGPhysicalProperty stationweather = WeatherDatabase->get(position);
189
190   SGPath path( globals->get_fg_root() );
191   path.append( "Airports" );
192   path.append( "runways.mk4" );
193   FGRunways runways( path.c_str() );
194   
195   //Set the heading to into the wind
196   double wind_x = stationweather.Wind[0];
197   double wind_y = stationweather.Wind[1];
198   
199   double speed = sqrt( wind_x*wind_x + wind_y*wind_y ) * SG_METER_TO_NM / (60.0*60.0);
200   double hdg;
201   
202   //If no wind use 270degrees
203   if(speed == 0) {
204     hdg = 270;
205   } else {
206     // //normalize the wind to get the direction
207     //wind_x /= speed; wind_y /= speed;
208     
209     hdg = - atan2 ( wind_x, wind_y ) * SG_RADIANS_TO_DEGREES ;
210     if (hdg < 0.0)
211       hdg += 360.0;
212   }
213   
214   FGRunway runway;
215   if ( runways.search( ident, int(hdg), &runway) ) {
216     active_runway = runway.rwy_no;
217     active_rw_hdg = runway.heading;
218     //cout << "Active runway is: " << active_runway << "  heading = " 
219     // << active_rw_hdg << endl;
220   }
221   else cout << "FGRunways search failed" << endl;
222
223 }
224
225 // ========================================================================
226 // update infos about plane
227 // ========================================================================
228 void FGApproach::update_plane_dat() {
229   
230   //cout << "Update Approach " << ident << "   " << num_planes << " registered" << endl;
231   // update plane positions
232   for (int i=0; i<num_planes; i++) {
233     planes[i].lon = lon_node->getDoubleValue();
234     planes[i].lat = lat_node->getDoubleValue();
235     planes[i].alt = elev_node->getDoubleValue();
236 //    Point3D aircraft = sgGeodToCart( Point3D(planes[i].lon*SGD_DEGREES_TO_RADIANS, 
237 //                                           planes[i].lat*SGD_DEGREES_TO_RADIANS, 
238 //                                           planes[i].alt*SG_FEET_TO_METER) );
239     double course, distance;
240     calc_gc_course_dist(Point3D(lon*SGD_DEGREES_TO_RADIANS, lat*SGD_DEGREES_TO_RADIANS, 0.0),
241                         Point3D(planes[i].lon*SGD_DEGREES_TO_RADIANS,planes[i].lat*SGD_DEGREES_TO_RADIANS, 0.0 ),
242                         &course, &distance);
243     planes[i].dist = distance/SG_NM_TO_METER;
244     planes[i].brg  = 360.0-course*SGD_RADIANS_TO_DEGREES;
245
246     //cout << "Plane Id: " << planes[i].ident << "  Distance to " << ident 
247     //<< " is " << planes[i].dist << " m" << endl;
248     
249     //if (first) {
250     //transmission = ident;
251     //globals->get_ATC_display()->RegisterRepeatingMessage(transmission);
252     //first = false;
253     //}
254   }   
255 }
256
257 // =======================================================================
258 // Add plane to Approach list
259 // =======================================================================
260 void FGApproach::AddPlane(string pid) {
261   for ( int i=0; i<num_planes; i++) {
262     if ( planes[i].ident == pid) {
263       //cout << "Plane already registered: " << ident << " " << num_planes << endl;
264       return;
265     }
266   }
267   planes[num_planes].ident = pid;
268   ++num_planes;
269   //cout << "Plane added to list: " << ident << " " << num_planes << endl;
270   return;
271 }
272
273 // ========================================================================
274 // closest distance between a point and a straigt line in 2 dim.
275 // ========================================================================
276 double FGApproach::calc_psl_dist(const double &h1, const double &d1,
277                                  const double &h2, const double &d2,
278                                  const double &h3)
279 {
280   double a1 = h1 * SGD_DEGREES_TO_RADIANS;
281   double a2 = h2 * SGD_DEGREES_TO_RADIANS;
282   double a3 = h3 * SGD_DEGREES_TO_RADIANS;
283   double x1 = cos(a1) * d1;
284   double y1 = sin(a1) * d1;
285   double x2 = cos(a2) * d2;
286   double y2 = sin(a2) * d2;
287   double x3 = cos(a3);
288   double y3 = sin(a3);
289   
290   // formula: dis = sqrt( (v1-v2)**2 - ((v1-v2)*v3)**2 ); vi = (xi,yi)
291   double val1   = (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2);
292   double val2   = ((x1-x2)*x3 + (y1-y2)*y3) * ((x1-x2)*x3 + (y1-y2)*y3);
293   double dis    = val1 - val2;
294   // now get sign for offset 
295   //cout << x1 << " " << x2 << " " << y1 << " " << y2 << " " 
296   //     << x3 << " " << y3 << " " 
297   //     << val1 << " " << val2 << " " << dis << endl;
298   x3 *= sqrt(val2);
299   y3 *= sqrt(val2);
300   if ( x3*(x1-x2) < 0.0 && y3*(y1-y2) < 0.0) {
301     x3 *= -1.0;
302     y3 *= -1.0;
303   }
304   //cout << x3 << " " << y3 << endl;
305   double dis1   = x1-x2-x3;
306   double dis2   = y1-y2-y3;
307   dis = sqrt(dis);
308   if (atan2(dis2,dis1) < a3) dis *= -1.0;
309   //cout << dis1 << " " << dis2 << " " << atan2(dis2,dis1)*SGD_RADIANS_TO_DEGREES << " " << h3
310   //     << " " << sqrt(dis1*dis1 + dis2*dis2) << " " << dis << endl;
311   //cout << atan2(dis2,dis1)*SGD_RADIANS_TO_DEGREES << " " << dis << endl;
312
313   return dis;
314 }
315
316 // ========================================================================
317 // get heading and distance between two points; point1 ---> point2
318 // ========================================================================
319 void FGApproach::calc_hd_course_dist(const double &h1, const double &d1, 
320                                      const double &h2, const double &d2,
321                                      double *course, double *dist)
322 {
323   double a1 = h1 * SGD_DEGREES_TO_RADIANS;
324   double a2 = h2 * SGD_DEGREES_TO_RADIANS;
325   double x1 = cos(a1) * d1;
326   double y1 = sin(a1) * d1;
327   double x2 = cos(a2) * d2;
328   double y2 = sin(a2) * d2;
329            
330   *dist   = sqrt( (y2-y1)*(y2-y1) + (x2-x1)*(x2-x1) );
331   *course = atan2( (y2-y1), (x2-x1) ) * SGD_RADIANS_TO_DEGREES;
332   if ( *course < 0 ) *course = *course+360;
333   //cout << x1 << " " << y1 << " " << x2 << " " << y2 << " " << *dist << " " << *course << endl;
334 }
335
336
337
338 int FGApproach::RemovePlane() {
339
340   // first check if anything has to be done
341   bool rmplane = false;
342   for (int i=0; i<num_planes; i++) {
343     if (planes[i].dist > range*SG_NM_TO_METER) {
344       rmplane = true;
345       break;
346     }
347   }
348   if (!rmplane) return num_planes;
349
350   // now make a copy of the plane list
351   PlaneApp tmp[max_planes];
352   for (int i=0; i<num_planes; i++) {
353     tmp[i] = planes[i];
354   }
355   
356   int np = 0;
357   // now check which planes are still in range
358   for (int i=0; i<num_planes; i++) {
359     if (tmp[i].dist <= range*SG_NM_TO_METER) {
360       planes[np] = tmp[i];
361       np += 1;
362     }
363   }
364   num_planes = np;
365   return num_planes;
366 }