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.
27 #include "approach.hxx"
28 #include "transmission.hxx"
29 #include "transmissionlist.hxx"
30 #include "ATCDialog.hxx"
32 #include <Airports/runways.hxx>
33 #include <simgear/constants.h>
34 #include <simgear/math/polar3d.hxx>
35 #include <simgear/misc/sg_path.hxx>
37 #include <Environment/environment_mgr.hxx>
38 #include <Environment/environment.hxx>
47 FGApproach::FGApproach(){
48 comm1_node = fgGetNode("/instrumentation/comm[0]/frequencies/selected-mhz", true);
49 comm2_node = fgGetNode("/instrumentation/comm[1]/frequencies/selected-mhz", true);
54 lon_node = fgGetNode("/position/longitude-deg", true);
55 lat_node = fgGetNode("/position/latitude-deg", true);
56 elev_node = fgGetNode("/position/altitude-ft", true);
57 hdg_node = fgGetNode("/orientation/heading-deg", true);
58 speed_node = fgGetNode("/velocities/airspeed-kt", true);
59 etime_node = fgGetNode("/sim/time/elapsed-sec", true);
64 for ( i=0; i<max_planes; i++) {
65 planes[i].contact = 0;
67 planes[i].dnwp = -999.;
68 planes[i].on_crs = true;
69 planes[i].turn_rate = 10.0;
70 planes[i].desc_rate = 1000.0;
71 planes[i].clmb_rate = 500.0;
75 planes[i].lmc.c3 = -1;
76 planes[i].wp_change = false;
81 FGApproach::~FGApproach(){
84 void FGApproach::Init() {
89 // ============================================================================
90 // the main update function
91 // ============================================================================
92 void FGApproach::Update(double dt) {
94 const int max_trans = 20;
95 FGTransmission tmissions[max_trans];
97 atc_type station = APPROACH;
105 //static string atcmsg1[10];
106 //static string atcmsg2[10];
110 //static bool TransDisplayed = false;
113 if ( active_runway == "" ) get_active_runway();
115 double comm1_freq = comm1_node->getDoubleValue();
117 //bool DisplayTransmissions = true;
119 for (i=0; i<num_planes; i++) {
120 if ( planes[i].ident == "Player") {
122 tpars.station = name;
123 tpars.callsign = "Player";
124 tpars.airport = ident;
126 //cout << "ident = " << ident << " name = " << name << '\n';
129 // is the frequency of the station tuned in?
130 if ( freq == (int)(comm1_freq*100.0 + 0.5) ) {
131 current_transmissionlist->query_station( station, tmissions, max_trans, num_trans );
132 // loop over all transmissions for station
133 for ( j=0; j<=num_trans-1; j++ ) {
134 code = tmissions[j].get_code();
135 //cout << "code is " << code.c1 << " " << code.c2 << " " << code.c3 << '\n';
136 // select proper transmissions
137 if(code.c3 != 2) { // DCL - hack to prevent request crossing airspace being displayed since this isn't implemented yet.
138 if ( ( code.c2 == -1 && planes[i].lmc.c3 == 0 ) ||
139 ( code.c1 == 0 && code.c2 == planes[i].lmc.c2 ) ) {
140 mentry = current_transmissionlist->gen_text(station, code, tpars, false);
141 transm = current_transmissionlist->gen_text(station, code, tpars, true);
142 // is the transmission already registered?
143 if (!current_atcdialog->trans_reg( ident, transm, APPROACH )) {
144 current_atcdialog->add_entry( ident, transm, mentry, APPROACH, 0 );
153 for ( i=0; i<num_planes; i++ ) {
154 //cout << "TPar.airport = " << TPar.airport << " TPar.station = " << TPar.station << " TPar.callsign = " << TPar.callsign << '\n';
155 //if ( planes[i].ident == TPar.callsign && name == TPar.airport && TPar.station == "approach" ) {
156 //if ( TPar.request && TPar.intention == "landing" && ident == TPar.intid) {
157 if(planes[i].ident == "Player" && fgGetBool("/sim/atc/opt0")) {
158 //cout << "Landing requested\n";
159 fgSetBool("/sim/atc/opt0", false);
161 // ===========================
162 // === calculate waypoints ===
163 // ===========================
166 wpn = planes[i].wpn-1;
167 planes[i].aalt = planes[i].wpts[wpn-1][2];
168 planes[i].ahdg = planes[i].wpts[wpn][4];
170 // generate the message
174 adif = angle_diff_deg( planes[i].hdg, planes[i].ahdg );
175 tpars.station = name;
176 tpars.callsign = "Player";
177 if ( adif < 0 ) tpars.tdir = 1;
179 tpars.heading = planes[i].ahdg;
180 if (planes[i].alt-planes[i].aalt > 100.0) tpars.VDir = 1;
181 else if (planes[i].alt-planes[i].aalt < -100.0) tpars.VDir = 3;
183 tpars.alt = planes[i].aalt;
184 message = current_transmissionlist->gen_text(station, code, tpars, true );
185 //cout << message << '\n';
186 set_message(message);
187 planes[i].lmc = code;
188 planes[i].tlm = etime_node->getDoubleValue();
189 planes[i].on_crs = true;
190 planes[i].contact = 1;
195 if ( planes[i].contact == 1 ) {
196 // =========================
197 // === update parameters ===
198 // =========================
200 //cout << planes[i].brg << " " << planes[i].dist << " " << planes[i].wpts[wpn+1][0]
201 //<< " " << planes[i].wpts[wpn+1][1] << " " << planes[i].wpts[wpn+1][4]
202 //cout << wpn << " distance to current course = " << planes[i].dcc << endl;
203 //cout << etime_node->getDoubleValue() << endl;
205 // =========================
206 // === reached waypoint? ===
207 // =========================
208 wpn = planes[i].wpn-2;
209 adif = angle_diff_deg( planes[i].hdg, planes[i].wpts[wpn][4] )
210 * SGD_DEGREES_TO_RADIANS;
211 datp = 2*sin(fabs(adif)/2.0)*sin(fabs(adif)/2.0) *
212 planes[i].spd/3600. * planes[i].turn_rate +
213 planes[i].spd/3600. * 3.0;
214 //cout << adif/SGD_DEGREES_TO_RADIANS << " "
215 // << datp << " " << planes[i].dnc << " " << planes[i].dcc <<endl;
216 if ( fabs(planes[i].dnc) < datp ) {
217 //if ( fabs(planes[i].dnc) < 0.3 && planes[i].dnwp < 1.0 ) {
218 //cout << "Reached next waypoint!\n";
220 wpn = planes[i].wpn-1;
221 planes[i].ahdg = planes[i].wpts[wpn][4];
222 planes[i].aalt = planes[i].wpts[wpn-1][2];
223 planes[i].wp_change = true;
225 // generate the message
226 adif = angle_diff_deg( planes[i].hdg, planes[i].ahdg );
227 tpars.station = name;
228 tpars.callsign = "Player";
229 if ( adif < 0 ) tpars.tdir = 1;
231 tpars.heading = planes[i].ahdg;
237 if (planes[i].alt-planes[i].aalt > 100.0) tpars.VDir = 1;
238 else if (planes[i].alt-planes[i].aalt < -100.0) tpars.VDir = 3;
240 tpars.alt = planes[i].aalt;
241 message = current_transmissionlist->gen_text(station, code, tpars, true );
242 //cout << "Approach transmitting...\n";
243 //cout << message << endl;
244 set_message(message);
250 tpars.runway = active_runway;
251 message = current_transmissionlist->gen_text(station, code, tpars, true);
252 //cout << "Approach transmitting 2 ...\n";
253 //cout << message << endl;
254 set_message(message);
256 planes[i].lmc = code;
257 planes[i].tlm = etime_node->getDoubleValue();
258 planes[i].on_crs = true;
263 // =========================
264 // === come off course ? ===
265 // =========================
266 if ( fabs(planes[i].dcc) > 1.0 &&
267 ( !planes[i].wp_change || etime_node->getDoubleValue() - planes[i].tlm > tbm ) ) {
268 //cout << "Off course!\n";
269 if ( planes[i].on_crs ) {
270 if ( planes[i].dcc < 0) {
271 planes[i].ahdg += 30.0;
274 planes[i].ahdg -= 30.0;
276 if (planes[i].ahdg > 360.0) planes[i].ahdg -= 360.0;
277 else if (planes[i].ahdg < 0.0) planes[i].ahdg += 360.0;
279 //cout << planes[i].on_crs << " "
280 // << angle_diff_deg( planes[i].hdg, planes[i].ahdg) << " "
281 // << etime_node->getDoubleValue() << " "
282 // << planes[i].tlm << endl;
283 // generate the message
284 if ( planes[i].on_crs ||
285 ( fabs(angle_diff_deg( planes[i].hdg, planes[i].ahdg )) > 30.0 &&
286 etime_node->getDoubleValue() - planes[i].tlm > tbm) ) {
287 // generate the message
291 adif = angle_diff_deg( planes[i].hdg, planes[i].ahdg );
292 tpars.station = name;
293 tpars.callsign = "Player";
294 tpars.miles = fabs(planes[i].dcc);
295 if ( adif < 0 ) tpars.tdir = 1;
297 tpars.heading = planes[i].ahdg;
298 message = current_transmissionlist->gen_text(station, code, tpars, true);
299 //cout << "Approach transmitting 3 ...\n";
300 //cout << message << '\n';
301 set_message(message);
302 planes[i].lmc = code;
303 planes[i].tlm = etime_node->getDoubleValue();
306 planes[i].on_crs = false;
308 else if ( !planes[i].on_crs ) {
309 //cout << "Off course 2!\n";
310 wpn = planes[i].wpn-1;
311 adif = angle_diff_deg( planes[i].hdg, planes[i].wpts[wpn][4] )
312 * SGD_DEGREES_TO_RADIANS;
313 datp = 2*sin(fabs(adif)/2.0)*sin(fabs(adif)/2.0) *
314 planes[i].spd/3600. * planes[i].turn_rate +
315 planes[i].spd/3600. * 3.0;
316 if ( fabs(planes[i].dcc) < datp ) {
317 planes[i].ahdg = fabs(planes[i].wpts[wpn][4]);
319 // generate the message
323 tpars.station = name;
324 tpars.callsign = "Player";
325 if ( adif < 0 ) tpars.tdir = 1;
327 tpars.heading = planes[i].ahdg;
328 message = current_transmissionlist->gen_text(station, code, tpars, true);
329 //cout << "Approach transmitting 4 ...\n";
330 //cout << message << '\n';
331 set_message(message);
332 planes[i].lmc = code;
333 planes[i].tlm = etime_node->getDoubleValue();
335 planes[i].on_crs = true;
338 else if ( planes[i].wp_change ) {
339 planes[i].wp_change = false;
342 // ===================================================================
343 // === Less than two minutes away from touchdown? -> Contact Tower ===
344 // ===================================================================
345 if ( planes[i].wpn == 2 && planes[i].dnwp < planes[i].spd/60.*2.0 ) {
347 double freq = 121.95; // Hardwired - FIXME
352 tpars.station = name;
353 tpars.callsign = "Player";
355 message = current_transmissionlist->gen_text(station, code, tpars, true);
356 //cout << "Approach transmitting 5 ...\n";
357 //cout << message << '\n';
358 set_message(message);
359 planes[i].lmc = code;
360 planes[i].tlm = etime_node->getDoubleValue();
362 planes[i].contact = 2;
369 // ============================================================================
370 // update course parameters
371 // ============================================================================
372 void FGApproach::update_param( const int &i ) {
376 int wpn = planes[i].wpn-1; // this is the current waypoint
378 planes[i].dcc = calc_psl_dist(planes[i].brg, planes[i].dist,
379 planes[i].wpts[wpn][0], planes[i].wpts[wpn][1],
380 planes[i].wpts[wpn][4]);
381 planes[i].dnc = calc_psl_dist(planes[i].brg, planes[i].dist,
382 planes[i].wpts[wpn-1][0], planes[i].wpts[wpn-1][1],
383 planes[i].wpts[wpn-1][4]);
384 calc_hd_course_dist(planes[i].brg, planes[i].dist,
385 planes[i].wpts[wpn-1][0], planes[i].wpts[wpn-1][1],
391 // ============================================================================
392 // smallest difference between two angles in degree
393 // difference is negative if a1 > a2 and positive if a2 > a1
394 // ===========================================================================
395 double FGApproach::angle_diff_deg( const double &a1, const double &a2) {
398 if (a3 < 180.0) a3 += 360.0;
399 if (a3 > 180.0) a3 -= 360.0;
404 // ============================================================================
405 // calculate waypoints
406 // ============================================================================
407 void FGApproach::calc_wp( const int &i ) {
410 double course, d, cd, a1;
412 int wpn = planes[i].wpn;
413 // waypoint 0: Threshold of active runway
414 calc_gc_course_dist(Point3D(lon*SGD_DEGREES_TO_RADIANS, lat*SGD_DEGREES_TO_RADIANS, 0.0),
415 Point3D(active_rw_lon*SGD_DEGREES_TO_RADIANS,active_rw_lat*SGD_DEGREES_TO_RADIANS, 0.0 ),
417 double d1 = active_rw_hdg+180.0;
418 if ( d1 > 360.0 ) d1 -=360.0;
419 calc_cd_head_dist(360.0-course*SGD_RADIANS_TO_DEGREES, d/SG_NM_TO_METER,
420 d1, active_rw_len/SG_NM_TO_METER/2.0,
421 &planes[i].wpts[wpn][0], &planes[i].wpts[wpn][1]);
422 planes[i].wpts[wpn][2] = elev;
423 planes[i].wpts[wpn][4] = 0.0;
424 planes[i].wpts[wpn][5] = 0.0;
427 // ======================
428 // horizontal navigation
429 // ======================
430 // waypoint 1: point for turning onto final
431 calc_cd_head_dist(planes[i].wpts[wpn-1][0], planes[i].wpts[wpn-1][1], d1, lfl,
432 &planes[i].wpts[wpn][0], &planes[i].wpts[wpn][1]);
433 calc_hd_course_dist(planes[i].wpts[wpn][0], planes[i].wpts[wpn][1],
434 planes[i].wpts[wpn-1][0], planes[i].wpts[wpn-1][1],
436 planes[i].wpts[wpn][4] = course;
437 planes[i].wpts[wpn][5] = d;
440 // calculate course and distance from plane position to waypoint 1
441 calc_hd_course_dist(planes[i].brg, planes[i].dist, planes[i].wpts[1][0],
442 planes[i].wpts[1][1], &course, &d);
443 // check if airport is not between plane and waypoint 1 and
444 // DCA to airport on course to waypoint 1 is larger than 10 miles
446 if ( fabs(angle_diff_deg( planes[i].wpts[1][0], planes[i].brg )) < 90.0 ||
447 calc_psl_dist( zero, zero, planes[i].brg, planes[i].dist, course ) > 10.0 ) {
448 // check if turning angle at waypoint 1 would be > max_ta
449 if ( fabs(angle_diff_deg( planes[i].wpts[1][4], course )) > max_ta ) {
450 cd = calc_psl_dist(planes[i].brg, planes[i].dist,
451 planes[i].wpts[1][0], planes[i].wpts[1][1],
452 planes[i].wpts[1][4]);
453 a1 = atan2(cd,planes[i].wpts[1][1]);
454 planes[i].wpts[wpn][0] = planes[i].wpts[1][0] - a1/SGD_DEGREES_TO_RADIANS;
455 if ( planes[i].wpts[wpn][0] < 0.0) planes[i].wpts[wpn][0] += 360.0;
456 if ( planes[i].wpts[wpn][0] > 360.0) planes[i].wpts[wpn][0] -= 360.0;
457 planes[i].wpts[wpn][1] = fabs(cd) / sin(fabs(a1));
458 calc_hd_course_dist(planes[i].wpts[wpn][0], planes[i].wpts[wpn][1],
459 planes[i].wpts[wpn-1][0], planes[i].wpts[wpn-1][1],
461 planes[i].wpts[wpn][4] = course;
462 planes[i].wpts[wpn][5] = d;
465 calc_hd_course_dist(planes[i].brg, planes[i].dist, planes[i].wpts[wpn-1][0],
466 planes[i].wpts[wpn-1][1], &course, &d);
470 a1 = atan2(planes[i].wpts[1][1], leg );
472 if ( angle_diff_deg(planes[i].brg,planes[i].wpts[1][0]) < 0 )
473 planes[i].wpts[wpn][0] = planes[i].wpts[1][0] + a1/SGD_DEGREES_TO_RADIANS;
474 else planes[i].wpts[wpn][0] = planes[i].wpts[1][0] - a1/SGD_DEGREES_TO_RADIANS;
476 planes[i].wpts[wpn][1] = sqrt( planes[i].wpts[1][1]*planes[i].wpts[1][1] + leg*leg );
477 calc_hd_course_dist(planes[i].wpts[wpn][0], planes[i].wpts[wpn][1],
478 planes[i].wpts[wpn-1][0], planes[i].wpts[wpn-1][1],
480 planes[i].wpts[wpn][4] = course;
481 planes[i].wpts[wpn][5] = d;
484 calc_hd_course_dist(planes[i].brg, planes[i].dist,
485 planes[i].wpts[wpn-1][0], planes[i].wpts[wpn-1][1],
489 planes[i].wpts[wpn][0] = planes[i].brg;
490 planes[i].wpts[wpn][1] = planes[i].dist;
491 planes[i].wpts[wpn][2] = planes[i].alt;
492 planes[i].wpts[wpn][4] = course;
493 planes[i].wpts[wpn][5] = d;
498 // Now check if legs are too short or if legs can be shortend
499 // legs must be at least 2 flight minutes long
500 double mdist = planes[i].spd / 60.0 * 2.0;
501 for ( j=2; j<wpn-1; ++j ) {
502 if ( planes[i].wpts[j][1] < mdist) {
506 // ====================
507 // vertical navigation
508 // ====================
509 double alt = elev+3000.0;
510 planes[i].wpts[1][2] = round_alt( true, alt );
511 for ( j=2; j<wpn-1; ++j ) {
512 double dalt = planes[i].alt - planes[i].wpts[j-1][2];
514 alt = planes[i].wpts[j-1][2] +
515 (planes[i].wpts[j][5] / planes[i].spd) * 60.0 * planes[i].desc_rate;
516 planes[i].wpts[j][2] = round_alt( false, alt );
517 if ( planes[i].wpts[j][2] > planes[i].alt )
518 planes[i].wpts[j][2] = round_alt( false, planes[i].alt );
521 planes[i].wpts[j][2] = planes[i].wpts[1][2];
525 cout << "Plane position: " << planes[i].brg << " " << planes[i].dist << endl;
526 for ( j=0; j<wpn; ++j ) {
527 cout << "Waypoint " << j << endl;
528 cout << "------------------" << endl;
529 cout << planes[i].wpts[j][0] << " " << planes[i].wpts[j][1]
530 << " " << planes[i].wpts[j][2] << " " << planes[i].wpts[j][5];
531 cout << endl << endl;
537 // ============================================================================
538 // round altitude value to next highest/lowest 500 feet
539 // ============================================================================
540 double FGApproach::round_alt( const bool hl, double alt ) {
544 if ( alt > (int)(alt)+0.5 ) alt = ((int)(alt)+1)*1000.0;
545 else alt = ((int)(alt)+0.5)*1000.0;
548 if ( alt > (int)(alt)+0.5 ) alt = ((int)(alt)+0.5)*1000.0;
549 else alt = ((int)(alt))*1000.0;
556 // ============================================================================
558 // ============================================================================
559 void FGApproach::get_active_runway() {
560 //cout << "Entering FGApproach::get_active_runway()\n";
562 FGEnvironment stationweather =
563 ((FGEnvironmentMgr *)globals->get_subsystem("environment"))
564 ->getEnvironment(lat, lon, elev);
566 double hdg = stationweather.get_wind_from_heading_deg();
569 if ( globals->get_runways()->search( ident, int(hdg), &runway) ) {
570 active_runway = runway._rwy_no;
571 active_rw_hdg = runway._heading;
572 active_rw_lon = runway._lon;
573 active_rw_lat = runway._lat;
574 active_rw_len = runway._length;
575 //cout << "Active runway is: " << active_runway << " heading = "
577 // << " lon = " << active_rw_lon
578 // << " lat = " << active_rw_lat <<endl;
580 else cout << "FGRunways search failed\n";
584 // ========================================================================
585 // update infos about plane
586 // ========================================================================
587 void FGApproach::update_plane_dat() {
589 //cout << "Update Approach " << ident << " " << num_planes << " registered" << endl;
590 // update plane positions
592 for (i=0; i<num_planes; i++) {
593 planes[i].lon = lon_node->getDoubleValue();
594 planes[i].lat = lat_node->getDoubleValue();
595 planes[i].alt = elev_node->getDoubleValue();
596 planes[i].hdg = hdg_node->getDoubleValue();
597 planes[i].spd = speed_node->getDoubleValue();
599 /*Point3D aircraft = sgGeodToCart( Point3D(planes[i].lon*SGD_DEGREES_TO_RADIANS,
600 planes[i].lat*SGD_DEGREES_TO_RADIANS,
601 planes[i].alt*SG_FEET_TO_METER) );*/
602 double course, distance;
603 calc_gc_course_dist(Point3D(lon*SGD_DEGREES_TO_RADIANS, lat*SGD_DEGREES_TO_RADIANS, 0.0),
604 Point3D(planes[i].lon*SGD_DEGREES_TO_RADIANS,planes[i].lat*SGD_DEGREES_TO_RADIANS, 0.0 ),
606 planes[i].dist = distance/SG_NM_TO_METER;
607 planes[i].brg = 360.0-course*SGD_RADIANS_TO_DEGREES;
609 //cout << "Plane Id: " << planes[i].ident << " Distance to " << ident
610 // << " is " << planes[i].dist << " miles " << "Bearing " << planes[i].brg << endl;
615 // =======================================================================
616 // Add plane to Approach list
617 // =======================================================================
618 void FGApproach::AddPlane(const string& pid) {
621 for ( i=0; i<num_planes; i++) {
622 if ( planes[i].ident == pid) {
623 //cout << "Plane already registered: " << planes[i].ident << ' ' << ident << ' ' << num_planes << endl;
627 planes[num_planes].ident = pid;
629 //cout << "Plane added to list: " << ident << " " << num_planes << endl;
633 // ================================================================================
634 // closest distance between a point (h1,d1) and a straigt line (h2,d2,h3) in 2 dim.
635 // ================================================================================
636 double FGApproach::calc_psl_dist(const double &h1, const double &d1,
637 const double &h2, const double &d2,
640 double a1 = h1 * SGD_DEGREES_TO_RADIANS;
641 double a2 = h2 * SGD_DEGREES_TO_RADIANS;
642 double a3 = h3 * SGD_DEGREES_TO_RADIANS;
643 double x1 = cos(a1) * d1;
644 double y1 = sin(a1) * d1;
645 double x2 = cos(a2) * d2;
646 double y2 = sin(a2) * d2;
650 // formula: dis = sqrt( (v1-v2)**2 - ((v1-v2)*v3)**2 ); vi = (xi,yi)
651 double val1 = (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2);
652 double val2 = ((x1-x2)*x3 + (y1-y2)*y3) * ((x1-x2)*x3 + (y1-y2)*y3);
653 double dis = val1 - val2;
654 // now get sign for offset
655 //cout << x1 << " " << x2 << " " << y1 << " " << y2 << " "
656 // << x3 << " " << y3 << " "
657 // << val1 << " " << val2 << " " << dis << endl;
660 double da = fabs(atan2(y3,x3) - atan2(y1-y2,x1-x2));
661 if ( da > SGD_PI ) da -= SGD_2PI;
662 if ( fabs(da) > SGD_PI_2) {
663 //if ( x3*(x1-x2) < 0.0 && y3*(y1-y2) < 0.0) {
667 //cout << x3 << " " << y3 << endl;
668 double dis1 = x1-x2-x3;
669 double dis2 = y1-y2-y3;
671 da = atan2(dis2,dis1);
672 if ( da < 0.0 ) da += SGD_2PI;
673 if ( da < a3 ) dis *= -1.0;
674 //cout << dis1 << " " << dis2 << " " << da*SGD_RADIANS_TO_DEGREES << " " << h3
675 // << " " << sqrt(dis1*dis1 + dis2*dis2) << " " << dis << endl;
676 //cout << atan2(dis2,dis1)*SGD_RADIANS_TO_DEGREES << " " << dis << endl;
682 // ========================================================================
683 // Calculate new bear/dist given starting bear/dis, and offset radial,
685 // ========================================================================
686 void FGApproach::calc_cd_head_dist(const double &h1, const double &d1,
687 const double &course, const double &dist,
688 double *h2, double *d2)
690 double a1 = h1 * SGD_DEGREES_TO_RADIANS;
691 double a2 = course * SGD_DEGREES_TO_RADIANS;
692 double x1 = cos(a1) * d1;
693 double y1 = sin(a1) * d1;
694 double x2 = cos(a2) * dist;
695 double y2 = sin(a2) * dist;
697 *d2 = sqrt((x1+x2)*(x1+x2) + (y1+y2)*(y1+y2));
698 *h2 = atan2( (y1+y2), (x1+x2) ) * SGD_RADIANS_TO_DEGREES;
699 if ( *h2 < 0 ) *h2 = *h2+360;
704 // ========================================================================
705 // get heading and distance between two points; point1 ---> point2
706 // ========================================================================
707 void FGApproach::calc_hd_course_dist(const double &h1, const double &d1,
708 const double &h2, const double &d2,
709 double *course, double *dist)
711 double a1 = h1 * SGD_DEGREES_TO_RADIANS;
712 double a2 = h2 * SGD_DEGREES_TO_RADIANS;
713 double x1 = cos(a1) * d1;
714 double y1 = sin(a1) * d1;
715 double x2 = cos(a2) * d2;
716 double y2 = sin(a2) * d2;
718 *dist = sqrt( (y2-y1)*(y2-y1) + (x2-x1)*(x2-x1) );
719 *course = atan2( (y2-y1), (x2-x1) ) * SGD_RADIANS_TO_DEGREES;
720 if ( *course < 0 ) *course = *course+360;
721 //cout << x1 << " " << y1 << " " << x2 << " " << y2 << " " << *dist << " " << *course << endl;
726 int FGApproach::RemovePlane() {
728 // first check if anything has to be done
729 bool rmplane = false;
732 for (i=0; i<num_planes; i++) {
733 if (planes[i].dist > range*SG_NM_TO_METER) {
738 if (!rmplane) return num_planes;
740 // now make a copy of the plane list
741 PlaneApp tmp[max_planes];
742 for (i=0; i<num_planes; i++) {
747 // now check which planes are still in range
748 for (i=0; i<num_planes; i++) {
749 if (tmp[i].dist <= range*SG_NM_TO_METER) {
760 void FGApproach::set_message(const string &msg)
762 fgSetString("/sim/messages/approach", msg.c_str());