]> git.mxchange.org Git - flightgear.git/blob - src/ATC/approach.cxx
Roy Vegard Ovesen:
[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 "transmission.hxx"
23 #include "transmissionlist.hxx"
24 #include "ATCdisplay.hxx"
25 #include "ATCDialog.hxx"
26
27 #include <Airports/runways.hxx>
28 #include <simgear/math/polar3d.hxx>
29 #include <simgear/misc/sg_path.hxx>
30
31 #ifdef FG_WEATHERCM
32 # include <WeatherCM/FGLocalWeatherDatabase.h>
33 #else
34 # include <Environment/environment_mgr.hxx>
35 # include <Environment/environment.hxx>
36 #endif
37
38
39 #include <GUI/gui.h>
40
41 //Constructor
42 FGApproach::FGApproach(){
43   comm1_node = fgGetNode("/radios/comm[0]/frequencies/selected-mhz", true);
44   comm2_node = fgGetNode("/radios/comm[1]/frequencies/selected-mhz", true);
45   
46   _type = APPROACH;
47
48   num_planes = 0;
49   lon_node   = fgGetNode("/position/longitude-deg", true);
50   lat_node   = fgGetNode("/position/latitude-deg", true);
51   elev_node  = fgGetNode("/position/altitude-ft", true);
52   hdg_node   = fgGetNode("/orientation/heading-deg", true);
53   speed_node = fgGetNode("/velocities/airspeed-kt", true);
54   etime_node = fgGetNode("/sim/time/elapsed-ms", true);
55
56   first = true;
57   active_runway = "";
58   int i;
59   for ( i=0; i<max_planes; i++) {
60     planes[i].contact   = 0;
61     planes[i].wpn       = 0;
62     planes[i].dnwp      = -999.;
63     planes[i].on_crs    = true;
64     planes[i].turn_rate = 10.0;
65     planes[i].desc_rate = 1000.0;
66     planes[i].clmb_rate = 500.0;
67     planes[i].tlm       = 0.0;
68     planes[i].lmc.c1    = 0;
69     planes[i].lmc.c2    = 0;
70     planes[i].lmc.c3    = -1;
71     planes[i].wp_change = false;
72   }
73 }
74
75 //Destructor
76 FGApproach::~FGApproach(){
77 }
78
79 void FGApproach::Init() {
80 }
81
82
83
84 // ============================================================================
85 // the main update function
86 // ============================================================================
87 void FGApproach::Update(double dt) {
88         
89         const int max_trans = 20;
90         FGTransmission tmissions[max_trans];
91         int    wpn;
92         atc_type station = APPROACH;
93         TransCode code;
94         TransPar TPar;
95         int    i,j;
96         //double course, d, 
97         double adif, datp;
98         //char   buf[10];
99         string message;
100         //static string atcmsg1[10];
101         //static string atcmsg2[10];
102         string mentry;
103         string transm;
104         TransPar tpars;
105         //static bool TransDisplayed = false;
106         
107         update_plane_dat();
108         if ( active_runway == "" ) get_active_runway();
109         
110         double comm1_freq = comm1_node->getDoubleValue();
111         
112         //bool DisplayTransmissions = true;
113         
114         for (i=0; i<num_planes; i++) {
115                 if ( planes[i].ident == "Player") { 
116                         station = APPROACH;
117                         tpars.station = name;
118                         tpars.callsign = "Player";
119                         tpars.airport = ident;
120                         
121                         //cout << "ident = " << ident << " name = " << name << '\n';
122                         
123                         int num_trans = 0;
124                         // is the frequency of the station tuned in?
125                         if ( freq == (int)(comm1_freq*100.0 + 0.5) ) {
126                                 current_transmissionlist->query_station( station, tmissions, max_trans, num_trans );
127                                 // loop over all transmissions for station
128                                 for ( j=0; j<=num_trans-1; j++ ) {
129                                         code = tmissions[j].get_code();
130                                         //cout << "code is " << code.c1 << "  " << code.c2 << "  " << code.c3 << '\n';
131                                         // select proper transmissions
132                                         if(code.c3 != 2) {    // DCL - hack to prevent request crossing airspace being displayed since this isn't implemented yet.
133                                             if ( ( code.c2 == -1 && planes[i].lmc.c3 == 0 ) || 
134                                                     ( code.c1 == 0  && code.c2 == planes[i].lmc.c2 ) ) {
135                                                     mentry = current_transmissionlist->gen_text(station, code, tpars, false);
136                                                     transm = current_transmissionlist->gen_text(station, code, tpars, true);
137                                                     // is the transmission already registered?
138                                                     if (!current_atcdialog->trans_reg( ident, transm, APPROACH )) {
139                                                             current_atcdialog->add_entry( ident, transm, mentry, APPROACH, 0 );
140                                                     }
141                                             }
142                                         }
143                                 }
144                         }
145                 }
146         }
147         
148         for ( i=0; i<num_planes; i++ ) {
149                 //cout << "TPar.airport = " << TPar.airport << " TPar.station = " << TPar.station << " TPar.callsign = " << TPar.callsign << '\n';
150                 //if ( planes[i].ident == TPar.callsign && name == TPar.airport && TPar.station == "approach" ) {
151                         //if ( TPar.request && TPar.intention == "landing" && ident == TPar.intid) {
152                         if(planes[i].ident == "Player" && fgGetBool("/sim/atc/opt0")) {
153                                 //cout << "Landing requested\n";
154                                 fgSetBool("/sim/atc/opt0", false);
155                                 planes[i].wpn = 0; 
156                                 // ===========================
157                                 // === calculate waypoints ===
158                                 // ===========================
159                                 calc_wp( i );  
160                                 update_param( i );
161                                 wpn = planes[i].wpn-1;
162                                 planes[i].aalt = planes[i].wpts[wpn-1][2];
163                                 planes[i].ahdg = planes[i].wpts[wpn][4];
164                                 
165                                 // generate the message
166                                 code.c1 = 1;
167                                 code.c2 = 1;
168                                 code.c3 = 0;
169                                 adif = angle_diff_deg( planes[i].hdg, planes[i].ahdg );
170                                 tpars.station = name;
171                                 tpars.callsign = "Player";
172                                 if ( adif < 0 ) tpars.tdir = 1;
173                                 else            tpars.tdir = 2;
174                                 tpars.heading = planes[i].ahdg;
175                                 if      (planes[i].alt-planes[i].aalt > 100.0)  tpars.VDir = 1;
176                                 else if (planes[i].alt-planes[i].aalt < -100.0) tpars.VDir = 3;
177                                 else tpars.VDir = 2;
178                                 tpars.alt = planes[i].aalt;
179                                 message = current_transmissionlist->gen_text(station, code, tpars, true );
180                                 //cout << message << '\n';
181                                 globals->get_ATC_display()->RegisterSingleMessage( message, 0 );
182                                 planes[i].lmc = code;
183                                 planes[i].tlm = etime_node->getDoubleValue();
184                                 planes[i].on_crs = true;
185                                 planes[i].contact = 1;
186                         }
187                 //}
188                 
189                 //if(1) {
190                 if ( planes[i].contact == 1 ) {
191                         // =========================
192                         // === update parameters ===
193                         // =========================
194                         update_param( i );
195                         //cout << planes[i].brg << " " << planes[i].dist << " " << planes[i].wpts[wpn+1][0] 
196                         //<< " " << planes[i].wpts[wpn+1][1] << " " << planes[i].wpts[wpn+1][4] 
197                         //cout << wpn << " distance to current course = " << planes[i].dcc << endl;
198                         //cout << etime_node->getDoubleValue() << endl;
199                         
200                         // =========================
201                         // === reached waypoint? ===
202                         // =========================
203                         wpn = planes[i].wpn-2;
204                         adif = angle_diff_deg( planes[i].hdg, planes[i].wpts[wpn][4] ) 
205                         * SGD_DEGREES_TO_RADIANS;
206                         datp = 2*sin(fabs(adif)/2.0)*sin(fabs(adif)/2.0) *
207                                planes[i].spd/3600. * planes[i].turn_rate + 
208                                planes[i].spd/3600. * 3.0;
209                         //cout << adif/SGD_DEGREES_TO_RADIANS << " " 
210                         //     << datp << " " << planes[i].dnc << " " << planes[i].dcc <<endl;
211                         if ( fabs(planes[i].dnc) < datp ) {
212                         //if ( fabs(planes[i].dnc) < 0.3 && planes[i].dnwp < 1.0 ) {
213                                 //cout << "Reached next waypoint!\n";
214                                 planes[i].wpn -= 1;
215                                 wpn = planes[i].wpn-1;
216                                 planes[i].ahdg = planes[i].wpts[wpn][4];
217                                 planes[i].aalt = planes[i].wpts[wpn-1][2];
218                                 planes[i].wp_change = true;
219                                 
220                                 // generate the message
221                                 adif = angle_diff_deg( planes[i].hdg, planes[i].ahdg );
222                                 tpars.station = name;
223                                 tpars.callsign = "Player";
224                                 if ( adif < 0 ) tpars.tdir = 1;
225                                 else            tpars.tdir = 2;
226                                 tpars.heading = planes[i].ahdg;
227                                 
228                                 if ( wpn-1 != 0) { 
229                                         code.c1 = 1;
230                                         code.c2 = 1;
231                                         code.c3 = 0;
232                                         if      (planes[i].alt-planes[i].aalt > 100.0)  tpars.VDir = 1;
233                                         else if (planes[i].alt-planes[i].aalt < -100.0) tpars.VDir = 3;
234                                         else tpars.VDir = 2;
235                                         tpars.alt = planes[i].aalt;
236                                         message = current_transmissionlist->gen_text(station, code, tpars, true );
237                                         //cout << "Approach transmitting...\n";
238                                         //cout << message << endl;
239                                         globals->get_ATC_display()->RegisterSingleMessage( message, 0 );
240                                         
241                                 }
242                                 else {
243                                         code.c1 = 1;
244                                         code.c2 = 3;
245                                         code.c3 = 0;
246                                         tpars.runway = active_runway;
247                                         message = current_transmissionlist->gen_text(station, code, tpars, true);
248                                         //cout << "Approach transmitting 2 ...\n";
249                                         //cout << message << endl;
250                                         globals->get_ATC_display()->RegisterSingleMessage( message, 0 );
251                                 }
252                                 planes[i].lmc = code;
253                                 planes[i].tlm = etime_node->getDoubleValue();
254                                 planes[i].on_crs = true;
255                                 
256                                 update_param( i );
257                         }
258                         
259                         // =========================
260                         // === come off course ? ===
261                         // =========================
262                         if ( fabs(planes[i].dcc) > 1.0 && 
263                            ( !planes[i].wp_change || etime_node->getDoubleValue() - planes[i].tlm > tbm ) ) {
264                                 //cout << "Off course!\n";
265                                 if ( planes[i].on_crs ) {
266                                         if ( planes[i].dcc < 0) {
267                                                 planes[i].ahdg += 30.0;
268                                         }
269                                         else {
270                                                 planes[i].ahdg -= 30.0;
271                                         }
272                                         if (planes[i].ahdg > 360.0) planes[i].ahdg -= 360.0;
273                                         else if (planes[i].ahdg < 0.0) planes[i].ahdg += 360.0;
274                                 }
275                                 //cout << planes[i].on_crs << " " 
276                                 //     << angle_diff_deg( planes[i].hdg, planes[i].ahdg) << " "
277                                 //     << etime_node->getDoubleValue() << " "
278                                 //     << planes[i].tlm << endl;
279                                 // generate the message
280                                 if ( planes[i].on_crs || 
281                                    ( fabs(angle_diff_deg( planes[i].hdg, planes[i].ahdg )) >  30.0  && 
282                                     etime_node->getDoubleValue() - planes[i].tlm > tbm) ) {
283                                         // generate the message
284                                         code.c1 = 1;
285                                         code.c2 = 4;
286                                         code.c3 = 0;
287                                         adif = angle_diff_deg( planes[i].hdg, planes[i].ahdg );
288                                         tpars.station = name;
289                                         tpars.callsign = "Player";
290                                         tpars.miles   = fabs(planes[i].dcc);
291                                         if ( adif < 0 ) tpars.tdir = 1;
292                                         else            tpars.tdir = 2;
293                                         tpars.heading = planes[i].ahdg;
294                                         message = current_transmissionlist->gen_text(station, code, tpars, true);
295                                         //cout << "Approach transmitting 3 ...\n";
296                                         //cout << message << '\n';
297                                         globals->get_ATC_display()->RegisterSingleMessage( message, 0 );
298                                         planes[i].lmc = code;
299                                         planes[i].tlm = etime_node->getDoubleValue();
300                                 }
301                                 
302                                 planes[i].on_crs = false;
303                         }
304                         else if ( !planes[i].on_crs ) {
305                                 //cout << "Off course 2!\n";
306                                 wpn = planes[i].wpn-1;
307                                 adif = angle_diff_deg( planes[i].hdg, planes[i].wpts[wpn][4] ) 
308                                        * SGD_DEGREES_TO_RADIANS;
309                                 datp = 2*sin(fabs(adif)/2.0)*sin(fabs(adif)/2.0) *
310                                 planes[i].spd/3600. * planes[i].turn_rate + 
311                                 planes[i].spd/3600. * 3.0;
312                                 if ( fabs(planes[i].dcc) < datp ) { 
313                                         planes[i].ahdg = fabs(planes[i].wpts[wpn][4]);
314                                         
315                                         // generate the message
316                                         code.c1 = 1;
317                                         code.c2 = 2;
318                                         code.c3 = 0;
319                                         tpars.station = name;
320                                         tpars.callsign = "Player";
321                                         if ( adif < 0 ) tpars.tdir = 1;
322                                         else            tpars.tdir = 2;
323                                         tpars.heading = planes[i].ahdg;
324                                         message = current_transmissionlist->gen_text(station, code, tpars, true);
325                                         //cout << "Approach transmitting 4 ...\n";
326                                         //cout << message << '\n';
327                                         globals->get_ATC_display()->RegisterSingleMessage( message, 0 );
328                                         planes[i].lmc = code;
329                                         planes[i].tlm = etime_node->getDoubleValue();
330                                         
331                                         planes[i].on_crs = true;          
332                                 } 
333                         }
334                         else if ( planes[i].wp_change  ) {
335                                 planes[i].wp_change = false;
336                         }
337                         
338                         // ===================================================================
339                         // === Less than two minutes away from touchdown? -> Contact Tower ===
340                         // ===================================================================
341                         if ( planes[i].wpn == 2 && planes[i].dnwp < planes[i].spd/60.*2.0 ) {
342                                 
343                                 double freq = 121.95;   // Hardwired - FIXME
344                                 // generate message
345                                 code.c1 = 1;
346                                 code.c2 = 5;
347                                 code.c3 = 0;
348                                 tpars.station = name;
349                                 tpars.callsign = "Player";
350                                 tpars.freq    = freq;
351                                 message = current_transmissionlist->gen_text(station, code, tpars, true);
352                                 //cout << "Approach transmitting 5 ...\n";
353                                 //cout << message << '\n';
354                                 globals->get_ATC_display()->RegisterSingleMessage( message, 0 );
355                                 planes[i].lmc = code;
356                                 planes[i].tlm = etime_node->getDoubleValue();
357                                 
358                                 planes[i].contact = 2;
359                         }
360                 }
361         }
362 }
363
364
365 // ============================================================================
366 // update course parameters
367 // ============================================================================
368 void FGApproach::update_param( const int &i ) {
369   
370   double course, d;
371
372   int wpn = planes[i].wpn-1;            // this is the current waypoint
373
374   planes[i].dcc  = calc_psl_dist(planes[i].brg, planes[i].dist,
375                                  planes[i].wpts[wpn][0], planes[i].wpts[wpn][1],
376                                  planes[i].wpts[wpn][4]);
377   planes[i].dnc  = calc_psl_dist(planes[i].brg, planes[i].dist,
378                                  planes[i].wpts[wpn-1][0], planes[i].wpts[wpn-1][1],
379                                  planes[i].wpts[wpn-1][4]);
380   calc_hd_course_dist(planes[i].brg, planes[i].dist, 
381                       planes[i].wpts[wpn-1][0], planes[i].wpts[wpn-1][1],
382                       &course, &d);
383   planes[i].dnwp = d;
384
385 }
386
387 // ============================================================================
388 // smallest difference between two angles in degree
389 // difference is negative if a1 > a2 and positive if a2 > a1
390 // ===========================================================================
391 double FGApproach::angle_diff_deg( const double &a1, const double &a2) {
392   
393   double a3 = a2 - a1;
394   if (a3 < 180.0) a3 += 360.0;
395   if (a3 > 180.0) a3 -= 360.0;
396
397   return a3;
398 }
399
400 // ============================================================================
401 // calculate waypoints
402 // ============================================================================
403 void FGApproach::calc_wp( const int &i ) {
404         
405         int j;
406         double course, d, cd, a1;
407         
408         int wpn = planes[i].wpn;
409         // waypoint 0: Threshold of active runway
410         calc_gc_course_dist(Point3D(lon*SGD_DEGREES_TO_RADIANS, lat*SGD_DEGREES_TO_RADIANS, 0.0),
411                             Point3D(active_rw_lon*SGD_DEGREES_TO_RADIANS,active_rw_lat*SGD_DEGREES_TO_RADIANS, 0.0 ),
412                             &course, &d);
413         double d1 = active_rw_hdg+180.0;
414         if ( d1 > 360.0 ) d1 -=360.0;
415         calc_cd_head_dist(360.0-course*SGD_RADIANS_TO_DEGREES, d/SG_NM_TO_METER, 
416                           d1, active_rw_len/SG_NM_TO_METER/2.0, 
417                           &planes[i].wpts[wpn][0], &planes[i].wpts[wpn][1]);
418         planes[i].wpts[wpn][2] = elev;
419         planes[i].wpts[wpn][4] = 0.0;
420         planes[i].wpts[wpn][5] = 0.0;
421         wpn += 1;
422         
423         // ======================
424         // horizontal navigation
425         // ======================
426         // waypoint 1: point for turning onto final
427         calc_cd_head_dist(planes[i].wpts[wpn-1][0], planes[i].wpts[wpn-1][1], d1, lfl,
428                           &planes[i].wpts[wpn][0], &planes[i].wpts[wpn][1]);
429         calc_hd_course_dist(planes[i].wpts[wpn][0],   planes[i].wpts[wpn][1],
430                             planes[i].wpts[wpn-1][0], planes[i].wpts[wpn-1][1],
431                             &course, &d);
432         planes[i].wpts[wpn][4] = course;
433         planes[i].wpts[wpn][5] = d;
434         wpn += 1;
435         
436         // calculate course and distance from plane position to waypoint 1
437         calc_hd_course_dist(planes[i].brg, planes[i].dist, planes[i].wpts[1][0], 
438                             planes[i].wpts[1][1], &course, &d);
439         // check if airport is not between plane and waypoint 1 and
440         // DCA to airport on course to waypoint 1 is larger than 10 miles
441         double zero = 0.0;
442         if ( fabs(angle_diff_deg( planes[i].wpts[1][0], planes[i].brg  )) < 90.0 ||
443                 calc_psl_dist( zero, zero, planes[i].brg, planes[i].dist, course ) > 10.0 ) {
444                 // check if turning angle at waypoint 1 would be > max_ta
445                 if ( fabs(angle_diff_deg( planes[i].wpts[1][4], course )) > max_ta ) {
446                         cd = calc_psl_dist(planes[i].brg, planes[i].dist,
447                              planes[i].wpts[1][0], planes[i].wpts[1][1],
448                              planes[i].wpts[1][4]);
449                         a1 = atan2(cd,planes[i].wpts[1][1]);
450                         planes[i].wpts[wpn][0] = planes[i].wpts[1][0] - a1/SGD_DEGREES_TO_RADIANS;
451                         if ( planes[i].wpts[wpn][0] < 0.0)   planes[i].wpts[wpn][0] += 360.0;   
452                         if ( planes[i].wpts[wpn][0] > 360.0) planes[i].wpts[wpn][0] -= 360.0;   
453                         planes[i].wpts[wpn][1] = fabs(cd) / sin(fabs(a1));
454                         calc_hd_course_dist(planes[i].wpts[wpn][0],   planes[i].wpts[wpn][1],
455                                             planes[i].wpts[wpn-1][0], planes[i].wpts[wpn-1][1],
456                                             &course, &d);
457                         planes[i].wpts[wpn][4] = course;
458                         planes[i].wpts[wpn][5] = d;
459                         wpn += 1;
460                         
461                         calc_hd_course_dist(planes[i].brg, planes[i].dist, planes[i].wpts[wpn-1][0], 
462                                             planes[i].wpts[wpn-1][1], &course, &d);
463                 }
464         } else {
465                 double leg = 10.0;
466                 a1 = atan2(planes[i].wpts[1][1], leg );
467                 
468                 if ( angle_diff_deg(planes[i].brg,planes[i].wpts[1][0]) < 0 ) 
469                         planes[i].wpts[wpn][0] = planes[i].wpts[1][0] + a1/SGD_DEGREES_TO_RADIANS;
470                 else planes[i].wpts[wpn][0] = planes[i].wpts[1][0] - a1/SGD_DEGREES_TO_RADIANS;
471                 
472                 planes[i].wpts[wpn][1] = sqrt( planes[i].wpts[1][1]*planes[i].wpts[1][1] + leg*leg );
473                 calc_hd_course_dist(planes[i].wpts[wpn][0],   planes[i].wpts[wpn][1],
474                                     planes[i].wpts[wpn-1][0], planes[i].wpts[wpn-1][1],
475                                     &course, &d);
476                 planes[i].wpts[wpn][4] = course;
477                 planes[i].wpts[wpn][5] = d;
478                 wpn += 1;
479                 
480                 calc_hd_course_dist(planes[i].brg, planes[i].dist,
481                                     planes[i].wpts[wpn-1][0], planes[i].wpts[wpn-1][1],
482                                     &course, &d);
483         }
484         
485         planes[i].wpts[wpn][0] = planes[i].brg;
486         planes[i].wpts[wpn][1] = planes[i].dist;
487         planes[i].wpts[wpn][2] = planes[i].alt;
488         planes[i].wpts[wpn][4] = course;
489         planes[i].wpts[wpn][5] = d;
490         wpn += 1;
491         
492         planes[i].wpn = wpn;
493         
494         // Now check if legs are too short or if legs can be shortend
495         // legs must be at least 2 flight minutes long
496         double mdist = planes[i].spd / 60.0 * 2.0;
497         for ( j=2; j<wpn-1; ++j ) {
498                 if ( planes[i].wpts[j][1] < mdist) {
499                 }
500         }
501         
502         // ====================
503         // vertical navigation
504         // ====================
505         double alt = elev+3000.0;
506         planes[i].wpts[1][2] = round_alt( true, alt );
507         for ( j=2; j<wpn-1; ++j ) {
508                 double dalt = planes[i].alt - planes[i].wpts[j-1][2];
509                 if ( dalt > 0 ) {
510                         alt = planes[i].wpts[j-1][2] + 
511                               (planes[i].wpts[j][5] / planes[i].spd) * 60.0 * planes[i].desc_rate;
512                         planes[i].wpts[j][2] = round_alt( false, alt );
513                         if ( planes[i].wpts[j][2] > planes[i].alt ) 
514                                 planes[i].wpts[j][2] = round_alt( false, planes[i].alt );
515                 }
516                 else {
517                         planes[i].wpts[j][2] = planes[i].wpts[1][2];
518                 }
519         }
520         
521         cout << "Plane position: " << planes[i].brg << " " << planes[i].dist << endl;
522         for ( j=0; j<wpn; ++j ) {
523                 cout << "Waypoint " << j << endl;
524                 cout << "------------------" << endl;
525                 cout << planes[i].wpts[j][0] << "   " << planes[i].wpts[j][1]
526                      << "   " << planes[i].wpts[j][2] << "   " << planes[i].wpts[j][5]; 
527                 cout << endl << endl;
528         }
529         
530 }
531
532
533 // ============================================================================
534 // round altitude value to next highest/lowest 500 feet
535 // ============================================================================
536 double FGApproach::round_alt( const bool hl, double alt ) {
537
538   alt = alt/1000.0;
539   if ( hl ) {
540     if ( alt > (int)(alt)+0.5 ) alt = ((int)(alt)+1)*1000.0;
541     else alt = ((int)(alt)+0.5)*1000.0;
542   }
543   else {
544     if ( alt > (int)(alt)+0.5 ) alt = ((int)(alt)+0.5)*1000.0;
545     else alt = ((int)(alt))*1000.0;
546   }
547   
548   return alt;
549 }
550
551
552 // ============================================================================
553 // get active runway
554 // ============================================================================
555 void FGApproach::get_active_runway() {
556         //cout << "Entering FGApproach::get_active_runway()\n";
557
558 #ifdef FG_WEATHERCM
559   sgVec3 position = { lat, lon, elev };
560   FGPhysicalProperty stationweather = WeatherDatabase->get(position);
561 #else
562   FGEnvironment stationweather =
563       ((FGEnvironmentMgr *)globals->get_subsystem("environment"))
564         ->getEnvironment(lat, lon, elev);
565 #endif
566
567 #ifdef FG_WEATHERCM
568   //Set the heading to into the wind
569   double wind_x = stationweather.Wind[0];
570   double wind_y = stationweather.Wind[1];
571   
572   double speed = sqrt( wind_x*wind_x + wind_y*wind_y ) * SG_METER_TO_NM / (60.0*60.0);
573   double hdg;
574   
575   //If no wind use 270degrees
576   if(speed == 0) {
577     hdg = 270;
578   } else {
579     // //normalize the wind to get the direction
580     //wind_x /= speed; wind_y /= speed;
581     
582     hdg = - atan2 ( wind_x, wind_y ) * SG_RADIANS_TO_DEGREES ;
583     if (hdg < 0.0)
584       hdg += 360.0;
585   }
586 #else
587   double hdg = stationweather.get_wind_from_heading_deg();
588 #endif
589   
590   FGRunway runway;
591   if ( globals->get_runways()->search( ident, int(hdg), &runway) ) {
592     active_runway = runway.rwy_no;
593     active_rw_hdg = runway.heading;
594     active_rw_lon = runway.lon;
595     active_rw_lat = runway.lat;
596     active_rw_len = runway.length;
597     //cout << "Active runway is: " << active_runway << "  heading = " 
598     // << active_rw_hdg 
599     // << " lon = " << active_rw_lon 
600     // << " lat = " << active_rw_lat <<endl;
601   }
602   else cout << "FGRunways search failed\n";
603
604 }
605
606 // ========================================================================
607 // update infos about plane
608 // ========================================================================
609 void FGApproach::update_plane_dat() {
610   
611   //cout << "Update Approach " << ident << "   " << num_planes << " registered" << endl;
612   // update plane positions
613   int i;
614   for (i=0; i<num_planes; i++) {
615     planes[i].lon = lon_node->getDoubleValue();
616     planes[i].lat = lat_node->getDoubleValue();
617     planes[i].alt = elev_node->getDoubleValue();
618     planes[i].hdg = hdg_node->getDoubleValue();
619     planes[i].spd = speed_node->getDoubleValue();
620
621     /*Point3D aircraft = sgGeodToCart( Point3D(planes[i].lon*SGD_DEGREES_TO_RADIANS, 
622                                              planes[i].lat*SGD_DEGREES_TO_RADIANS, 
623                                              planes[i].alt*SG_FEET_TO_METER) );*/
624     double course, distance;
625     calc_gc_course_dist(Point3D(lon*SGD_DEGREES_TO_RADIANS, lat*SGD_DEGREES_TO_RADIANS, 0.0),
626                         Point3D(planes[i].lon*SGD_DEGREES_TO_RADIANS,planes[i].lat*SGD_DEGREES_TO_RADIANS, 0.0 ),
627                         &course, &distance);
628     planes[i].dist = distance/SG_NM_TO_METER;
629     planes[i].brg  = 360.0-course*SGD_RADIANS_TO_DEGREES;
630
631     //cout << "Plane Id: " << planes[i].ident << "  Distance to " << ident 
632     // << " is " << planes[i].dist << " miles   " << "Bearing " << planes[i].brg << endl;
633     
634   }   
635 }
636
637 // =======================================================================
638 // Add plane to Approach list
639 // =======================================================================
640 void FGApproach::AddPlane(string pid) {
641
642   int i;
643   for ( i=0; i<num_planes; i++) {
644     if ( planes[i].ident == pid) {
645       //cout << "Plane already registered: " << planes[i].ident << ' ' << ident << ' ' << num_planes << endl;
646       return;
647     }
648   }
649   planes[num_planes].ident = pid;
650   ++num_planes;
651   //cout << "Plane added to list: " << ident << " " << num_planes << endl;
652   return;
653 }
654
655 // ================================================================================
656 // closest distance between a point (h1,d1) and a straigt line (h2,d2,h3) in 2 dim.
657 // ================================================================================
658 double FGApproach::calc_psl_dist(const double &h1, const double &d1,
659                                  const double &h2, const double &d2,
660                                  const double &h3)
661 {
662   double a1 = h1 * SGD_DEGREES_TO_RADIANS;
663   double a2 = h2 * SGD_DEGREES_TO_RADIANS;
664   double a3 = h3 * SGD_DEGREES_TO_RADIANS;
665   double x1 = cos(a1) * d1;
666   double y1 = sin(a1) * d1;
667   double x2 = cos(a2) * d2;
668   double y2 = sin(a2) * d2;
669   double x3 = cos(a3);
670   double y3 = sin(a3);
671   
672   // formula: dis = sqrt( (v1-v2)**2 - ((v1-v2)*v3)**2 ); vi = (xi,yi)
673   double val1   = (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2);
674   double val2   = ((x1-x2)*x3 + (y1-y2)*y3) * ((x1-x2)*x3 + (y1-y2)*y3);
675   double dis    = val1 - val2;
676   // now get sign for offset 
677   //cout << x1 << " " << x2 << " " << y1 << " " << y2 << " " 
678   //     << x3 << " " << y3 << " " 
679   //     << val1 << " " << val2 << " " << dis << endl;
680   x3 *= sqrt(val2);
681   y3 *= sqrt(val2);
682   double da = fabs(atan2(y3,x3) - atan2(y1-y2,x1-x2));
683   if ( da > SGD_PI ) da -= 2*SGD_PI;
684   if ( fabs(da) > SGD_PI/2.) {
685     //if ( x3*(x1-x2) < 0.0 && y3*(y1-y2) < 0.0) {
686     x3 *= -1.0;
687     y3 *= -1.0;
688   }
689   //cout << x3 << " " << y3 << endl;
690   double dis1   = x1-x2-x3;
691   double dis2   = y1-y2-y3;
692   dis = sqrt(dis);
693   da = atan2(dis2,dis1);
694   if ( da < 0.0 ) da  += 2*SGD_PI;
695   if ( da < a3 )  dis *= -1.0;
696   //cout << dis1 << " " << dis2 << " " << da*SGD_RADIANS_TO_DEGREES << " " << h3
697   //     << " " << sqrt(dis1*dis1 + dis2*dis2) << " " << dis << endl;
698   //cout << atan2(dis2,dis1)*SGD_RADIANS_TO_DEGREES << " " << dis << endl;
699
700   return dis;
701 }
702
703
704 // ========================================================================
705 // Calculate new bear/dist given starting bear/dis, and offset radial,
706 // and distance.
707 // ========================================================================
708 void FGApproach::calc_cd_head_dist(const double &h1, const double &d1, 
709                                    const double &course, const double &dist,
710                                    double *h2, double *d2)
711 {
712   double a1 = h1 * SGD_DEGREES_TO_RADIANS;
713   double a2 = course * SGD_DEGREES_TO_RADIANS;
714   double x1 = cos(a1) * d1;
715   double y1 = sin(a1) * d1;
716   double x2 = cos(a2) * dist;
717   double y2 = sin(a2) * dist;
718     
719   *d2 = sqrt((x1+x2)*(x1+x2) + (y1+y2)*(y1+y2));
720   *h2 = atan2( (y1+y2), (x1+x2) ) * SGD_RADIANS_TO_DEGREES;
721   if ( *h2 < 0 ) *h2 = *h2+360;
722 }
723
724
725
726 // ========================================================================
727 // get heading and distance between two points; point1 ---> point2
728 // ========================================================================
729 void FGApproach::calc_hd_course_dist(const double &h1, const double &d1, 
730                                      const double &h2, const double &d2,
731                                      double *course, double *dist)
732 {
733   double a1 = h1 * SGD_DEGREES_TO_RADIANS;
734   double a2 = h2 * SGD_DEGREES_TO_RADIANS;
735   double x1 = cos(a1) * d1;
736   double y1 = sin(a1) * d1;
737   double x2 = cos(a2) * d2;
738   double y2 = sin(a2) * d2;
739            
740   *dist   = sqrt( (y2-y1)*(y2-y1) + (x2-x1)*(x2-x1) );
741   *course = atan2( (y2-y1), (x2-x1) ) * SGD_RADIANS_TO_DEGREES;
742   if ( *course < 0 ) *course = *course+360;
743   //cout << x1 << " " << y1 << " " << x2 << " " << y2 << " " << *dist << " " << *course << endl;
744 }
745
746
747
748 int FGApproach::RemovePlane() {
749
750   // first check if anything has to be done
751   bool rmplane = false;
752   int i;
753
754   for (i=0; i<num_planes; i++) {
755     if (planes[i].dist > range*SG_NM_TO_METER) {
756       rmplane = true;
757       break;
758     }
759   }
760   if (!rmplane) return num_planes;
761
762   // now make a copy of the plane list
763   PlaneApp tmp[max_planes];
764   for (i=0; i<num_planes; i++) {
765     tmp[i] = planes[i];
766   }
767   
768   int np = 0;
769   // now check which planes are still in range
770   for (i=0; i<num_planes; i++) {
771     if (tmp[i].dist <= range*SG_NM_TO_METER) {
772       planes[np] = tmp[i];
773       np += 1;
774     }
775   }
776   num_planes = np;
777
778   return num_planes;
779 }