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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 #include "approach.hxx"
26 #include "transmission.hxx"
27 #include "transmissionlist.hxx"
28 #include "ATCDialog.hxx"
30 #include <Airports/runways.hxx>
31 #include <simgear/constants.h>
32 #include <simgear/math/polar3d.hxx>
33 #include <simgear/misc/sg_path.hxx>
35 #include <Environment/environment_mgr.hxx>
36 #include <Environment/environment.hxx>
42 FGApproach::FGApproach(){
43 comm1_node = fgGetNode("/instrumentation/comm[0]/frequencies/selected-mhz", true);
44 comm2_node = fgGetNode("/instrumentation/comm[1]/frequencies/selected-mhz", true);
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-sec", true);
59 for ( i=0; i<max_planes; i++) {
60 planes[i].contact = 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;
70 planes[i].lmc.c3 = -1;
71 planes[i].wp_change = false;
76 FGApproach::~FGApproach(){
79 void FGApproach::Init() {
84 // ============================================================================
85 // the main update function
86 // ============================================================================
87 void FGApproach::Update(double dt) {
89 const int max_trans = 20;
90 FGTransmission tmissions[max_trans];
92 atc_type station = APPROACH;
100 //static string atcmsg1[10];
101 //static string atcmsg2[10];
105 //static bool TransDisplayed = false;
108 if ( active_runway == "" ) get_active_runway();
110 double comm1_freq = comm1_node->getDoubleValue();
112 //bool DisplayTransmissions = true;
114 for (i=0; i<num_planes; i++) {
115 if ( planes[i].ident == "Player") {
117 tpars.station = name;
118 tpars.callsign = "Player";
119 tpars.airport = ident;
121 //cout << "ident = " << ident << " name = " << name << '\n';
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 );
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);
156 // ===========================
157 // === calculate waypoints ===
158 // ===========================
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];
165 // generate the message
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;
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;
178 tpars.alt = planes[i].aalt;
179 message = current_transmissionlist->gen_text(station, code, tpars, true );
180 //cout << message << '\n';
181 set_message(message);
182 planes[i].lmc = code;
183 planes[i].tlm = etime_node->getDoubleValue();
184 planes[i].on_crs = true;
185 planes[i].contact = 1;
190 if ( planes[i].contact == 1 ) {
191 // =========================
192 // === update parameters ===
193 // =========================
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;
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";
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;
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;
226 tpars.heading = planes[i].ahdg;
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;
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 set_message(message);
245 tpars.runway = active_runway;
246 message = current_transmissionlist->gen_text(station, code, tpars, true);
247 //cout << "Approach transmitting 2 ...\n";
248 //cout << message << endl;
249 set_message(message);
251 planes[i].lmc = code;
252 planes[i].tlm = etime_node->getDoubleValue();
253 planes[i].on_crs = true;
258 // =========================
259 // === come off course ? ===
260 // =========================
261 if ( fabs(planes[i].dcc) > 1.0 &&
262 ( !planes[i].wp_change || etime_node->getDoubleValue() - planes[i].tlm > tbm ) ) {
263 //cout << "Off course!\n";
264 if ( planes[i].on_crs ) {
265 if ( planes[i].dcc < 0) {
266 planes[i].ahdg += 30.0;
269 planes[i].ahdg -= 30.0;
271 if (planes[i].ahdg > 360.0) planes[i].ahdg -= 360.0;
272 else if (planes[i].ahdg < 0.0) planes[i].ahdg += 360.0;
274 //cout << planes[i].on_crs << " "
275 // << angle_diff_deg( planes[i].hdg, planes[i].ahdg) << " "
276 // << etime_node->getDoubleValue() << " "
277 // << planes[i].tlm << endl;
278 // generate the message
279 if ( planes[i].on_crs ||
280 ( fabs(angle_diff_deg( planes[i].hdg, planes[i].ahdg )) > 30.0 &&
281 etime_node->getDoubleValue() - planes[i].tlm > tbm) ) {
282 // generate the message
286 adif = angle_diff_deg( planes[i].hdg, planes[i].ahdg );
287 tpars.station = name;
288 tpars.callsign = "Player";
289 tpars.miles = fabs(planes[i].dcc);
290 if ( adif < 0 ) tpars.tdir = 1;
292 tpars.heading = planes[i].ahdg;
293 message = current_transmissionlist->gen_text(station, code, tpars, true);
294 //cout << "Approach transmitting 3 ...\n";
295 //cout << message << '\n';
296 set_message(message);
297 planes[i].lmc = code;
298 planes[i].tlm = etime_node->getDoubleValue();
301 planes[i].on_crs = false;
303 else if ( !planes[i].on_crs ) {
304 //cout << "Off course 2!\n";
305 wpn = planes[i].wpn-1;
306 adif = angle_diff_deg( planes[i].hdg, planes[i].wpts[wpn][4] )
307 * SGD_DEGREES_TO_RADIANS;
308 datp = 2*sin(fabs(adif)/2.0)*sin(fabs(adif)/2.0) *
309 planes[i].spd/3600. * planes[i].turn_rate +
310 planes[i].spd/3600. * 3.0;
311 if ( fabs(planes[i].dcc) < datp ) {
312 planes[i].ahdg = fabs(planes[i].wpts[wpn][4]);
314 // generate the message
318 tpars.station = name;
319 tpars.callsign = "Player";
320 if ( adif < 0 ) tpars.tdir = 1;
322 tpars.heading = planes[i].ahdg;
323 message = current_transmissionlist->gen_text(station, code, tpars, true);
324 //cout << "Approach transmitting 4 ...\n";
325 //cout << message << '\n';
326 set_message(message);
327 planes[i].lmc = code;
328 planes[i].tlm = etime_node->getDoubleValue();
330 planes[i].on_crs = true;
333 else if ( planes[i].wp_change ) {
334 planes[i].wp_change = false;
337 // ===================================================================
338 // === Less than two minutes away from touchdown? -> Contact Tower ===
339 // ===================================================================
340 if ( planes[i].wpn == 2 && planes[i].dnwp < planes[i].spd/60.*2.0 ) {
342 double freq = 121.95; // Hardwired - FIXME
347 tpars.station = name;
348 tpars.callsign = "Player";
350 message = current_transmissionlist->gen_text(station, code, tpars, true);
351 //cout << "Approach transmitting 5 ...\n";
352 //cout << message << '\n';
353 set_message(message);
354 planes[i].lmc = code;
355 planes[i].tlm = etime_node->getDoubleValue();
357 planes[i].contact = 2;
364 // ============================================================================
365 // update course parameters
366 // ============================================================================
367 void FGApproach::update_param( const int &i ) {
371 int wpn = planes[i].wpn-1; // this is the current waypoint
373 planes[i].dcc = calc_psl_dist(planes[i].brg, planes[i].dist,
374 planes[i].wpts[wpn][0], planes[i].wpts[wpn][1],
375 planes[i].wpts[wpn][4]);
376 planes[i].dnc = calc_psl_dist(planes[i].brg, planes[i].dist,
377 planes[i].wpts[wpn-1][0], planes[i].wpts[wpn-1][1],
378 planes[i].wpts[wpn-1][4]);
379 calc_hd_course_dist(planes[i].brg, planes[i].dist,
380 planes[i].wpts[wpn-1][0], planes[i].wpts[wpn-1][1],
386 // ============================================================================
387 // smallest difference between two angles in degree
388 // difference is negative if a1 > a2 and positive if a2 > a1
389 // ===========================================================================
390 double FGApproach::angle_diff_deg( const double &a1, const double &a2) {
393 if (a3 < 180.0) a3 += 360.0;
394 if (a3 > 180.0) a3 -= 360.0;
399 // ============================================================================
400 // calculate waypoints
401 // ============================================================================
402 void FGApproach::calc_wp( const int &i ) {
405 double course, d, cd, a1;
407 int wpn = planes[i].wpn;
408 // waypoint 0: Threshold of active runway
409 calc_gc_course_dist(Point3D(lon*SGD_DEGREES_TO_RADIANS, lat*SGD_DEGREES_TO_RADIANS, 0.0),
410 Point3D(active_rw_lon*SGD_DEGREES_TO_RADIANS,active_rw_lat*SGD_DEGREES_TO_RADIANS, 0.0 ),
412 double d1 = active_rw_hdg+180.0;
413 if ( d1 > 360.0 ) d1 -=360.0;
414 calc_cd_head_dist(360.0-course*SGD_RADIANS_TO_DEGREES, d/SG_NM_TO_METER,
415 d1, active_rw_len/SG_NM_TO_METER/2.0,
416 &planes[i].wpts[wpn][0], &planes[i].wpts[wpn][1]);
417 planes[i].wpts[wpn][2] = elev;
418 planes[i].wpts[wpn][4] = 0.0;
419 planes[i].wpts[wpn][5] = 0.0;
422 // ======================
423 // horizontal navigation
424 // ======================
425 // waypoint 1: point for turning onto final
426 calc_cd_head_dist(planes[i].wpts[wpn-1][0], planes[i].wpts[wpn-1][1], d1, lfl,
427 &planes[i].wpts[wpn][0], &planes[i].wpts[wpn][1]);
428 calc_hd_course_dist(planes[i].wpts[wpn][0], planes[i].wpts[wpn][1],
429 planes[i].wpts[wpn-1][0], planes[i].wpts[wpn-1][1],
431 planes[i].wpts[wpn][4] = course;
432 planes[i].wpts[wpn][5] = d;
435 // calculate course and distance from plane position to waypoint 1
436 calc_hd_course_dist(planes[i].brg, planes[i].dist, planes[i].wpts[1][0],
437 planes[i].wpts[1][1], &course, &d);
438 // check if airport is not between plane and waypoint 1 and
439 // DCA to airport on course to waypoint 1 is larger than 10 miles
441 if ( fabs(angle_diff_deg( planes[i].wpts[1][0], planes[i].brg )) < 90.0 ||
442 calc_psl_dist( zero, zero, planes[i].brg, planes[i].dist, course ) > 10.0 ) {
443 // check if turning angle at waypoint 1 would be > max_ta
444 if ( fabs(angle_diff_deg( planes[i].wpts[1][4], course )) > max_ta ) {
445 cd = calc_psl_dist(planes[i].brg, planes[i].dist,
446 planes[i].wpts[1][0], planes[i].wpts[1][1],
447 planes[i].wpts[1][4]);
448 a1 = atan2(cd,planes[i].wpts[1][1]);
449 planes[i].wpts[wpn][0] = planes[i].wpts[1][0] - a1/SGD_DEGREES_TO_RADIANS;
450 if ( planes[i].wpts[wpn][0] < 0.0) planes[i].wpts[wpn][0] += 360.0;
451 if ( planes[i].wpts[wpn][0] > 360.0) planes[i].wpts[wpn][0] -= 360.0;
452 planes[i].wpts[wpn][1] = fabs(cd) / sin(fabs(a1));
453 calc_hd_course_dist(planes[i].wpts[wpn][0], planes[i].wpts[wpn][1],
454 planes[i].wpts[wpn-1][0], planes[i].wpts[wpn-1][1],
456 planes[i].wpts[wpn][4] = course;
457 planes[i].wpts[wpn][5] = d;
460 calc_hd_course_dist(planes[i].brg, planes[i].dist, planes[i].wpts[wpn-1][0],
461 planes[i].wpts[wpn-1][1], &course, &d);
465 a1 = atan2(planes[i].wpts[1][1], leg );
467 if ( angle_diff_deg(planes[i].brg,planes[i].wpts[1][0]) < 0 )
468 planes[i].wpts[wpn][0] = planes[i].wpts[1][0] + a1/SGD_DEGREES_TO_RADIANS;
469 else planes[i].wpts[wpn][0] = planes[i].wpts[1][0] - a1/SGD_DEGREES_TO_RADIANS;
471 planes[i].wpts[wpn][1] = sqrt( planes[i].wpts[1][1]*planes[i].wpts[1][1] + leg*leg );
472 calc_hd_course_dist(planes[i].wpts[wpn][0], planes[i].wpts[wpn][1],
473 planes[i].wpts[wpn-1][0], planes[i].wpts[wpn-1][1],
475 planes[i].wpts[wpn][4] = course;
476 planes[i].wpts[wpn][5] = d;
479 calc_hd_course_dist(planes[i].brg, planes[i].dist,
480 planes[i].wpts[wpn-1][0], planes[i].wpts[wpn-1][1],
484 planes[i].wpts[wpn][0] = planes[i].brg;
485 planes[i].wpts[wpn][1] = planes[i].dist;
486 planes[i].wpts[wpn][2] = planes[i].alt;
487 planes[i].wpts[wpn][4] = course;
488 planes[i].wpts[wpn][5] = d;
493 // Now check if legs are too short or if legs can be shortend
494 // legs must be at least 2 flight minutes long
495 double mdist = planes[i].spd / 60.0 * 2.0;
496 for ( j=2; j<wpn-1; ++j ) {
497 if ( planes[i].wpts[j][1] < mdist) {
501 // ====================
502 // vertical navigation
503 // ====================
504 double alt = elev+3000.0;
505 planes[i].wpts[1][2] = round_alt( true, alt );
506 for ( j=2; j<wpn-1; ++j ) {
507 double dalt = planes[i].alt - planes[i].wpts[j-1][2];
509 alt = planes[i].wpts[j-1][2] +
510 (planes[i].wpts[j][5] / planes[i].spd) * 60.0 * planes[i].desc_rate;
511 planes[i].wpts[j][2] = round_alt( false, alt );
512 if ( planes[i].wpts[j][2] > planes[i].alt )
513 planes[i].wpts[j][2] = round_alt( false, planes[i].alt );
516 planes[i].wpts[j][2] = planes[i].wpts[1][2];
520 cout << "Plane position: " << planes[i].brg << " " << planes[i].dist << endl;
521 for ( j=0; j<wpn; ++j ) {
522 cout << "Waypoint " << j << endl;
523 cout << "------------------" << endl;
524 cout << planes[i].wpts[j][0] << " " << planes[i].wpts[j][1]
525 << " " << planes[i].wpts[j][2] << " " << planes[i].wpts[j][5];
526 cout << endl << endl;
532 // ============================================================================
533 // round altitude value to next highest/lowest 500 feet
534 // ============================================================================
535 double FGApproach::round_alt( const bool hl, double alt ) {
539 if ( alt > (int)(alt)+0.5 ) alt = ((int)(alt)+1)*1000.0;
540 else alt = ((int)(alt)+0.5)*1000.0;
543 if ( alt > (int)(alt)+0.5 ) alt = ((int)(alt)+0.5)*1000.0;
544 else alt = ((int)(alt))*1000.0;
551 // ============================================================================
553 // ============================================================================
554 void FGApproach::get_active_runway() {
555 //cout << "Entering FGApproach::get_active_runway()\n";
557 FGEnvironment stationweather =
558 ((FGEnvironmentMgr *)globals->get_subsystem("environment"))
559 ->getEnvironment(lat, lon, elev);
561 double hdg = stationweather.get_wind_from_heading_deg();
564 if ( globals->get_runways()->search( ident, int(hdg), &runway) ) {
565 active_runway = runway._rwy_no;
566 active_rw_hdg = runway._heading;
567 active_rw_lon = runway._lon;
568 active_rw_lat = runway._lat;
569 active_rw_len = runway._length;
570 //cout << "Active runway is: " << active_runway << " heading = "
572 // << " lon = " << active_rw_lon
573 // << " lat = " << active_rw_lat <<endl;
575 else cout << "FGRunways search failed\n";
579 // ========================================================================
580 // update infos about plane
581 // ========================================================================
582 void FGApproach::update_plane_dat() {
584 //cout << "Update Approach " << ident << " " << num_planes << " registered" << endl;
585 // update plane positions
587 for (i=0; i<num_planes; i++) {
588 planes[i].lon = lon_node->getDoubleValue();
589 planes[i].lat = lat_node->getDoubleValue();
590 planes[i].alt = elev_node->getDoubleValue();
591 planes[i].hdg = hdg_node->getDoubleValue();
592 planes[i].spd = speed_node->getDoubleValue();
594 /*Point3D aircraft = sgGeodToCart( Point3D(planes[i].lon*SGD_DEGREES_TO_RADIANS,
595 planes[i].lat*SGD_DEGREES_TO_RADIANS,
596 planes[i].alt*SG_FEET_TO_METER) );*/
597 double course, distance;
598 calc_gc_course_dist(Point3D(lon*SGD_DEGREES_TO_RADIANS, lat*SGD_DEGREES_TO_RADIANS, 0.0),
599 Point3D(planes[i].lon*SGD_DEGREES_TO_RADIANS,planes[i].lat*SGD_DEGREES_TO_RADIANS, 0.0 ),
601 planes[i].dist = distance/SG_NM_TO_METER;
602 planes[i].brg = 360.0-course*SGD_RADIANS_TO_DEGREES;
604 //cout << "Plane Id: " << planes[i].ident << " Distance to " << ident
605 // << " is " << planes[i].dist << " miles " << "Bearing " << planes[i].brg << endl;
610 // =======================================================================
611 // Add plane to Approach list
612 // =======================================================================
613 void FGApproach::AddPlane(const string& pid) {
616 for ( i=0; i<num_planes; i++) {
617 if ( planes[i].ident == pid) {
618 //cout << "Plane already registered: " << planes[i].ident << ' ' << ident << ' ' << num_planes << endl;
622 planes[num_planes].ident = pid;
624 //cout << "Plane added to list: " << ident << " " << num_planes << endl;
628 // ================================================================================
629 // closest distance between a point (h1,d1) and a straigt line (h2,d2,h3) in 2 dim.
630 // ================================================================================
631 double FGApproach::calc_psl_dist(const double &h1, const double &d1,
632 const double &h2, const double &d2,
635 double a1 = h1 * SGD_DEGREES_TO_RADIANS;
636 double a2 = h2 * SGD_DEGREES_TO_RADIANS;
637 double a3 = h3 * SGD_DEGREES_TO_RADIANS;
638 double x1 = cos(a1) * d1;
639 double y1 = sin(a1) * d1;
640 double x2 = cos(a2) * d2;
641 double y2 = sin(a2) * d2;
645 // formula: dis = sqrt( (v1-v2)**2 - ((v1-v2)*v3)**2 ); vi = (xi,yi)
646 double val1 = (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2);
647 double val2 = ((x1-x2)*x3 + (y1-y2)*y3) * ((x1-x2)*x3 + (y1-y2)*y3);
648 double dis = val1 - val2;
649 // now get sign for offset
650 //cout << x1 << " " << x2 << " " << y1 << " " << y2 << " "
651 // << x3 << " " << y3 << " "
652 // << val1 << " " << val2 << " " << dis << endl;
655 double da = fabs(atan2(y3,x3) - atan2(y1-y2,x1-x2));
656 if ( da > SGD_PI ) da -= SGD_2PI;
657 if ( fabs(da) > SGD_PI_2) {
658 //if ( x3*(x1-x2) < 0.0 && y3*(y1-y2) < 0.0) {
662 //cout << x3 << " " << y3 << endl;
663 double dis1 = x1-x2-x3;
664 double dis2 = y1-y2-y3;
666 da = atan2(dis2,dis1);
667 if ( da < 0.0 ) da += SGD_2PI;
668 if ( da < a3 ) dis *= -1.0;
669 //cout << dis1 << " " << dis2 << " " << da*SGD_RADIANS_TO_DEGREES << " " << h3
670 // << " " << sqrt(dis1*dis1 + dis2*dis2) << " " << dis << endl;
671 //cout << atan2(dis2,dis1)*SGD_RADIANS_TO_DEGREES << " " << dis << endl;
677 // ========================================================================
678 // Calculate new bear/dist given starting bear/dis, and offset radial,
680 // ========================================================================
681 void FGApproach::calc_cd_head_dist(const double &h1, const double &d1,
682 const double &course, const double &dist,
683 double *h2, double *d2)
685 double a1 = h1 * SGD_DEGREES_TO_RADIANS;
686 double a2 = course * SGD_DEGREES_TO_RADIANS;
687 double x1 = cos(a1) * d1;
688 double y1 = sin(a1) * d1;
689 double x2 = cos(a2) * dist;
690 double y2 = sin(a2) * dist;
692 *d2 = sqrt((x1+x2)*(x1+x2) + (y1+y2)*(y1+y2));
693 *h2 = atan2( (y1+y2), (x1+x2) ) * SGD_RADIANS_TO_DEGREES;
694 if ( *h2 < 0 ) *h2 = *h2+360;
699 // ========================================================================
700 // get heading and distance between two points; point1 ---> point2
701 // ========================================================================
702 void FGApproach::calc_hd_course_dist(const double &h1, const double &d1,
703 const double &h2, const double &d2,
704 double *course, double *dist)
706 double a1 = h1 * SGD_DEGREES_TO_RADIANS;
707 double a2 = h2 * SGD_DEGREES_TO_RADIANS;
708 double x1 = cos(a1) * d1;
709 double y1 = sin(a1) * d1;
710 double x2 = cos(a2) * d2;
711 double y2 = sin(a2) * d2;
713 *dist = sqrt( (y2-y1)*(y2-y1) + (x2-x1)*(x2-x1) );
714 *course = atan2( (y2-y1), (x2-x1) ) * SGD_RADIANS_TO_DEGREES;
715 if ( *course < 0 ) *course = *course+360;
716 //cout << x1 << " " << y1 << " " << x2 << " " << y2 << " " << *dist << " " << *course << endl;
721 int FGApproach::RemovePlane() {
723 // first check if anything has to be done
724 bool rmplane = false;
727 for (i=0; i<num_planes; i++) {
728 if (planes[i].dist > range*SG_NM_TO_METER) {
733 if (!rmplane) return num_planes;
735 // now make a copy of the plane list
736 PlaneApp tmp[max_planes];
737 for (i=0; i<num_planes; i++) {
742 // now check which planes are still in range
743 for (i=0; i<num_planes; i++) {
744 if (tmp[i].dist <= range*SG_NM_TO_METER) {
755 void FGApproach::set_message(const string &msg)
757 fgSetString("/sim/messages/approach", msg.c_str());