1 // FGApproach - a class to provide approach control at larger airports.
3 // Written by Alexander Kappes, started March 2002.
5 // Copyright (C) 2002 Alexander Kappes
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.
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.
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.
25 #include "approach.hxx"
26 #include "transmission.hxx"
27 #include "transmissionlist.hxx"
28 #include "ATCdisplay.hxx"
29 #include "ATCDialog.hxx"
31 #include <Airports/runways.hxx>
32 #include <simgear/constants.h>
33 #include <simgear/math/polar3d.hxx>
34 #include <simgear/misc/sg_path.hxx>
36 #include <Environment/environment_mgr.hxx>
37 #include <Environment/environment.hxx>
43 FGApproach::FGApproach(){
44 comm1_node = fgGetNode("/instrumentation/comm[0]/frequencies/selected-mhz", true);
45 comm2_node = fgGetNode("/instrumentation/comm[1]/frequencies/selected-mhz", true);
50 lon_node = fgGetNode("/position/longitude-deg", true);
51 lat_node = fgGetNode("/position/latitude-deg", true);
52 elev_node = fgGetNode("/position/altitude-ft", true);
53 hdg_node = fgGetNode("/orientation/heading-deg", true);
54 speed_node = fgGetNode("/velocities/airspeed-kt", true);
55 etime_node = fgGetNode("/sim/time/elapsed-sec", true);
60 for ( i=0; i<max_planes; i++) {
61 planes[i].contact = 0;
63 planes[i].dnwp = -999.;
64 planes[i].on_crs = true;
65 planes[i].turn_rate = 10.0;
66 planes[i].desc_rate = 1000.0;
67 planes[i].clmb_rate = 500.0;
71 planes[i].lmc.c3 = -1;
72 planes[i].wp_change = false;
77 FGApproach::~FGApproach(){
80 void FGApproach::Init() {
85 // ============================================================================
86 // the main update function
87 // ============================================================================
88 void FGApproach::Update(double dt) {
90 const int max_trans = 20;
91 FGTransmission tmissions[max_trans];
93 atc_type station = APPROACH;
101 //static string atcmsg1[10];
102 //static string atcmsg2[10];
106 //static bool TransDisplayed = false;
109 if ( active_runway == "" ) get_active_runway();
111 double comm1_freq = comm1_node->getDoubleValue();
113 //bool DisplayTransmissions = true;
115 for (i=0; i<num_planes; i++) {
116 if ( planes[i].ident == "Player") {
118 tpars.station = name;
119 tpars.callsign = "Player";
120 tpars.airport = ident;
122 //cout << "ident = " << ident << " name = " << name << '\n';
125 // is the frequency of the station tuned in?
126 if ( freq == (int)(comm1_freq*100.0 + 0.5) ) {
127 current_transmissionlist->query_station( station, tmissions, max_trans, num_trans );
128 // loop over all transmissions for station
129 for ( j=0; j<=num_trans-1; j++ ) {
130 code = tmissions[j].get_code();
131 //cout << "code is " << code.c1 << " " << code.c2 << " " << code.c3 << '\n';
132 // select proper transmissions
133 if(code.c3 != 2) { // DCL - hack to prevent request crossing airspace being displayed since this isn't implemented yet.
134 if ( ( code.c2 == -1 && planes[i].lmc.c3 == 0 ) ||
135 ( code.c1 == 0 && code.c2 == planes[i].lmc.c2 ) ) {
136 mentry = current_transmissionlist->gen_text(station, code, tpars, false);
137 transm = current_transmissionlist->gen_text(station, code, tpars, true);
138 // is the transmission already registered?
139 if (!current_atcdialog->trans_reg( ident, transm, APPROACH )) {
140 current_atcdialog->add_entry( ident, transm, mentry, APPROACH, 0 );
149 for ( i=0; i<num_planes; i++ ) {
150 //cout << "TPar.airport = " << TPar.airport << " TPar.station = " << TPar.station << " TPar.callsign = " << TPar.callsign << '\n';
151 //if ( planes[i].ident == TPar.callsign && name == TPar.airport && TPar.station == "approach" ) {
152 //if ( TPar.request && TPar.intention == "landing" && ident == TPar.intid) {
153 if(planes[i].ident == "Player" && fgGetBool("/sim/atc/opt0")) {
154 //cout << "Landing requested\n";
155 fgSetBool("/sim/atc/opt0", false);
157 // ===========================
158 // === calculate waypoints ===
159 // ===========================
162 wpn = planes[i].wpn-1;
163 planes[i].aalt = planes[i].wpts[wpn-1][2];
164 planes[i].ahdg = planes[i].wpts[wpn][4];
166 // generate the message
170 adif = angle_diff_deg( planes[i].hdg, planes[i].ahdg );
171 tpars.station = name;
172 tpars.callsign = "Player";
173 if ( adif < 0 ) tpars.tdir = 1;
175 tpars.heading = planes[i].ahdg;
176 if (planes[i].alt-planes[i].aalt > 100.0) tpars.VDir = 1;
177 else if (planes[i].alt-planes[i].aalt < -100.0) tpars.VDir = 3;
179 tpars.alt = planes[i].aalt;
180 message = current_transmissionlist->gen_text(station, code, tpars, true );
181 //cout << message << '\n';
182 set_message(message);
183 planes[i].lmc = code;
184 planes[i].tlm = etime_node->getDoubleValue();
185 planes[i].on_crs = true;
186 planes[i].contact = 1;
191 if ( planes[i].contact == 1 ) {
192 // =========================
193 // === update parameters ===
194 // =========================
196 //cout << planes[i].brg << " " << planes[i].dist << " " << planes[i].wpts[wpn+1][0]
197 //<< " " << planes[i].wpts[wpn+1][1] << " " << planes[i].wpts[wpn+1][4]
198 //cout << wpn << " distance to current course = " << planes[i].dcc << endl;
199 //cout << etime_node->getDoubleValue() << endl;
201 // =========================
202 // === reached waypoint? ===
203 // =========================
204 wpn = planes[i].wpn-2;
205 adif = angle_diff_deg( planes[i].hdg, planes[i].wpts[wpn][4] )
206 * SGD_DEGREES_TO_RADIANS;
207 datp = 2*sin(fabs(adif)/2.0)*sin(fabs(adif)/2.0) *
208 planes[i].spd/3600. * planes[i].turn_rate +
209 planes[i].spd/3600. * 3.0;
210 //cout << adif/SGD_DEGREES_TO_RADIANS << " "
211 // << datp << " " << planes[i].dnc << " " << planes[i].dcc <<endl;
212 if ( fabs(planes[i].dnc) < datp ) {
213 //if ( fabs(planes[i].dnc) < 0.3 && planes[i].dnwp < 1.0 ) {
214 //cout << "Reached next waypoint!\n";
216 wpn = planes[i].wpn-1;
217 planes[i].ahdg = planes[i].wpts[wpn][4];
218 planes[i].aalt = planes[i].wpts[wpn-1][2];
219 planes[i].wp_change = true;
221 // generate the message
222 adif = angle_diff_deg( planes[i].hdg, planes[i].ahdg );
223 tpars.station = name;
224 tpars.callsign = "Player";
225 if ( adif < 0 ) tpars.tdir = 1;
227 tpars.heading = planes[i].ahdg;
233 if (planes[i].alt-planes[i].aalt > 100.0) tpars.VDir = 1;
234 else if (planes[i].alt-planes[i].aalt < -100.0) tpars.VDir = 3;
236 tpars.alt = planes[i].aalt;
237 message = current_transmissionlist->gen_text(station, code, tpars, true );
238 //cout << "Approach transmitting...\n";
239 //cout << message << endl;
240 set_message(message);
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 set_message(message);
252 planes[i].lmc = code;
253 planes[i].tlm = etime_node->getDoubleValue();
254 planes[i].on_crs = true;
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;
270 planes[i].ahdg -= 30.0;
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;
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
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;
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 set_message(message);
298 planes[i].lmc = code;
299 planes[i].tlm = etime_node->getDoubleValue();
302 planes[i].on_crs = false;
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]);
315 // generate the message
319 tpars.station = name;
320 tpars.callsign = "Player";
321 if ( adif < 0 ) tpars.tdir = 1;
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 set_message(message);
328 planes[i].lmc = code;
329 planes[i].tlm = etime_node->getDoubleValue();
331 planes[i].on_crs = true;
334 else if ( planes[i].wp_change ) {
335 planes[i].wp_change = false;
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 ) {
343 double freq = 121.95; // Hardwired - FIXME
348 tpars.station = name;
349 tpars.callsign = "Player";
351 message = current_transmissionlist->gen_text(station, code, tpars, true);
352 //cout << "Approach transmitting 5 ...\n";
353 //cout << message << '\n';
354 set_message(message);
355 planes[i].lmc = code;
356 planes[i].tlm = etime_node->getDoubleValue();
358 planes[i].contact = 2;
365 // ============================================================================
366 // update course parameters
367 // ============================================================================
368 void FGApproach::update_param( const int &i ) {
372 int wpn = planes[i].wpn-1; // this is the current waypoint
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],
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) {
394 if (a3 < 180.0) a3 += 360.0;
395 if (a3 > 180.0) a3 -= 360.0;
400 // ============================================================================
401 // calculate waypoints
402 // ============================================================================
403 void FGApproach::calc_wp( const int &i ) {
406 double course, d, cd, a1;
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 ),
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;
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],
432 planes[i].wpts[wpn][4] = course;
433 planes[i].wpts[wpn][5] = d;
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
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],
457 planes[i].wpts[wpn][4] = course;
458 planes[i].wpts[wpn][5] = d;
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);
466 a1 = atan2(planes[i].wpts[1][1], leg );
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;
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],
476 planes[i].wpts[wpn][4] = course;
477 planes[i].wpts[wpn][5] = d;
480 calc_hd_course_dist(planes[i].brg, planes[i].dist,
481 planes[i].wpts[wpn-1][0], planes[i].wpts[wpn-1][1],
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;
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) {
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];
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 );
517 planes[i].wpts[j][2] = planes[i].wpts[1][2];
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;
533 // ============================================================================
534 // round altitude value to next highest/lowest 500 feet
535 // ============================================================================
536 double FGApproach::round_alt( const bool hl, double alt ) {
540 if ( alt > (int)(alt)+0.5 ) alt = ((int)(alt)+1)*1000.0;
541 else alt = ((int)(alt)+0.5)*1000.0;
544 if ( alt > (int)(alt)+0.5 ) alt = ((int)(alt)+0.5)*1000.0;
545 else alt = ((int)(alt))*1000.0;
552 // ============================================================================
554 // ============================================================================
555 void FGApproach::get_active_runway() {
556 //cout << "Entering FGApproach::get_active_runway()\n";
558 FGEnvironment stationweather =
559 ((FGEnvironmentMgr *)globals->get_subsystem("environment"))
560 ->getEnvironment(lat, lon, elev);
562 double hdg = stationweather.get_wind_from_heading_deg();
565 if ( globals->get_runways()->search( ident, int(hdg), &runway) ) {
566 active_runway = runway._rwy_no;
567 active_rw_hdg = runway._heading;
568 active_rw_lon = runway._lon;
569 active_rw_lat = runway._lat;
570 active_rw_len = runway._length;
571 //cout << "Active runway is: " << active_runway << " heading = "
573 // << " lon = " << active_rw_lon
574 // << " lat = " << active_rw_lat <<endl;
576 else cout << "FGRunways search failed\n";
580 // ========================================================================
581 // update infos about plane
582 // ========================================================================
583 void FGApproach::update_plane_dat() {
585 //cout << "Update Approach " << ident << " " << num_planes << " registered" << endl;
586 // update plane positions
588 for (i=0; i<num_planes; i++) {
589 planes[i].lon = lon_node->getDoubleValue();
590 planes[i].lat = lat_node->getDoubleValue();
591 planes[i].alt = elev_node->getDoubleValue();
592 planes[i].hdg = hdg_node->getDoubleValue();
593 planes[i].spd = speed_node->getDoubleValue();
595 /*Point3D aircraft = sgGeodToCart( Point3D(planes[i].lon*SGD_DEGREES_TO_RADIANS,
596 planes[i].lat*SGD_DEGREES_TO_RADIANS,
597 planes[i].alt*SG_FEET_TO_METER) );*/
598 double course, distance;
599 calc_gc_course_dist(Point3D(lon*SGD_DEGREES_TO_RADIANS, lat*SGD_DEGREES_TO_RADIANS, 0.0),
600 Point3D(planes[i].lon*SGD_DEGREES_TO_RADIANS,planes[i].lat*SGD_DEGREES_TO_RADIANS, 0.0 ),
602 planes[i].dist = distance/SG_NM_TO_METER;
603 planes[i].brg = 360.0-course*SGD_RADIANS_TO_DEGREES;
605 //cout << "Plane Id: " << planes[i].ident << " Distance to " << ident
606 // << " is " << planes[i].dist << " miles " << "Bearing " << planes[i].brg << endl;
611 // =======================================================================
612 // Add plane to Approach list
613 // =======================================================================
614 void FGApproach::AddPlane(const string& pid) {
617 for ( i=0; i<num_planes; i++) {
618 if ( planes[i].ident == pid) {
619 //cout << "Plane already registered: " << planes[i].ident << ' ' << ident << ' ' << num_planes << endl;
623 planes[num_planes].ident = pid;
625 //cout << "Plane added to list: " << ident << " " << num_planes << endl;
629 // ================================================================================
630 // closest distance between a point (h1,d1) and a straigt line (h2,d2,h3) in 2 dim.
631 // ================================================================================
632 double FGApproach::calc_psl_dist(const double &h1, const double &d1,
633 const double &h2, const double &d2,
636 double a1 = h1 * SGD_DEGREES_TO_RADIANS;
637 double a2 = h2 * SGD_DEGREES_TO_RADIANS;
638 double a3 = h3 * SGD_DEGREES_TO_RADIANS;
639 double x1 = cos(a1) * d1;
640 double y1 = sin(a1) * d1;
641 double x2 = cos(a2) * d2;
642 double y2 = sin(a2) * d2;
646 // formula: dis = sqrt( (v1-v2)**2 - ((v1-v2)*v3)**2 ); vi = (xi,yi)
647 double val1 = (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2);
648 double val2 = ((x1-x2)*x3 + (y1-y2)*y3) * ((x1-x2)*x3 + (y1-y2)*y3);
649 double dis = val1 - val2;
650 // now get sign for offset
651 //cout << x1 << " " << x2 << " " << y1 << " " << y2 << " "
652 // << x3 << " " << y3 << " "
653 // << val1 << " " << val2 << " " << dis << endl;
656 double da = fabs(atan2(y3,x3) - atan2(y1-y2,x1-x2));
657 if ( da > SGD_PI ) da -= SGD_2PI;
658 if ( fabs(da) > SGD_PI_2) {
659 //if ( x3*(x1-x2) < 0.0 && y3*(y1-y2) < 0.0) {
663 //cout << x3 << " " << y3 << endl;
664 double dis1 = x1-x2-x3;
665 double dis2 = y1-y2-y3;
667 da = atan2(dis2,dis1);
668 if ( da < 0.0 ) da += SGD_2PI;
669 if ( da < a3 ) dis *= -1.0;
670 //cout << dis1 << " " << dis2 << " " << da*SGD_RADIANS_TO_DEGREES << " " << h3
671 // << " " << sqrt(dis1*dis1 + dis2*dis2) << " " << dis << endl;
672 //cout << atan2(dis2,dis1)*SGD_RADIANS_TO_DEGREES << " " << dis << endl;
678 // ========================================================================
679 // Calculate new bear/dist given starting bear/dis, and offset radial,
681 // ========================================================================
682 void FGApproach::calc_cd_head_dist(const double &h1, const double &d1,
683 const double &course, const double &dist,
684 double *h2, double *d2)
686 double a1 = h1 * SGD_DEGREES_TO_RADIANS;
687 double a2 = course * SGD_DEGREES_TO_RADIANS;
688 double x1 = cos(a1) * d1;
689 double y1 = sin(a1) * d1;
690 double x2 = cos(a2) * dist;
691 double y2 = sin(a2) * dist;
693 *d2 = sqrt((x1+x2)*(x1+x2) + (y1+y2)*(y1+y2));
694 *h2 = atan2( (y1+y2), (x1+x2) ) * SGD_RADIANS_TO_DEGREES;
695 if ( *h2 < 0 ) *h2 = *h2+360;
700 // ========================================================================
701 // get heading and distance between two points; point1 ---> point2
702 // ========================================================================
703 void FGApproach::calc_hd_course_dist(const double &h1, const double &d1,
704 const double &h2, const double &d2,
705 double *course, double *dist)
707 double a1 = h1 * SGD_DEGREES_TO_RADIANS;
708 double a2 = h2 * SGD_DEGREES_TO_RADIANS;
709 double x1 = cos(a1) * d1;
710 double y1 = sin(a1) * d1;
711 double x2 = cos(a2) * d2;
712 double y2 = sin(a2) * d2;
714 *dist = sqrt( (y2-y1)*(y2-y1) + (x2-x1)*(x2-x1) );
715 *course = atan2( (y2-y1), (x2-x1) ) * SGD_RADIANS_TO_DEGREES;
716 if ( *course < 0 ) *course = *course+360;
717 //cout << x1 << " " << y1 << " " << x2 << " " << y2 << " " << *dist << " " << *course << endl;
722 int FGApproach::RemovePlane() {
724 // first check if anything has to be done
725 bool rmplane = false;
728 for (i=0; i<num_planes; i++) {
729 if (planes[i].dist > range*SG_NM_TO_METER) {
734 if (!rmplane) return num_planes;
736 // now make a copy of the plane list
737 PlaneApp tmp[max_planes];
738 for (i=0; i<num_planes; i++) {
743 // now check which planes are still in range
744 for (i=0; i<num_planes; i++) {
745 if (tmp[i].dist <= range*SG_NM_TO_METER) {
756 void FGApproach::set_message(const string &msg)
758 fgSetString("/sim/messages/approach", msg.c_str());
759 globals->get_ATC_display()->RegisterSingleMessage( msg, 0 );