1 // navcom.cxx -- class to manage a navcom instance
3 // Written by Curtis Olson, started April 2000.
5 // Copyright (C) 2000 - 2002 Curtis L. Olson - curt@flightgear.org
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.
28 #include <stdio.h> // snprintf
30 #include <simgear/compiler.h>
31 #include <simgear/sg_inlines.h>
32 #include <simgear/math/sg_random.h>
33 #include <simgear/math/vector.hxx>
35 #include <Aircraft/aircraft.hxx>
36 #include <Navaids/navlist.hxx>
45 FGNavCom::FGNavCom() :
46 lon_node(fgGetNode("/position/longitude-deg", true)),
47 lat_node(fgGetNode("/position/latitude-deg", true)),
48 alt_node(fgGetNode("/position/altitude-ft", true)),
63 nav_target_radial(0.0),
64 nav_target_radial_true(0.0),
65 nav_target_auto_hdg(0.0),
66 nav_gs_rate_of_climb(0.0),
72 SGPath path( globals->get_fg_root() );
74 term.append( "Navaids/range.term" );
76 low.append( "Navaids/range.low" );
78 high.append( "Navaids/range.high" );
80 term_tbl = new SGInterpTable( term.str() );
81 low_tbl = new SGInterpTable( low.str() );
82 high_tbl = new SGInterpTable( high.str() );
101 // We assume that index is valid now (it must be set before init()
104 // FIXME: Get rid of snprintf
106 snprintf(propname, 256, "/systems/electrical/outputs/navcom[%d]", index);
107 // default to true in case no electrical system defined.
108 fgSetDouble( propname, 60.0 );
109 bus_power = fgGetNode( propname, true );
111 snprintf(propname, 256, "/instrumentation/comm[%d]/serviceable", index);
112 com_serviceable = fgGetNode( propname, true );
113 com_serviceable->setBoolValue( true );
115 snprintf(propname, 256, "/instrumentation/nav[%d]/serviceable", index);
116 nav_serviceable = fgGetNode( propname, true );
117 nav_serviceable->setBoolValue( true );
119 snprintf(propname, 256, "/instrumentation/vor[%d]/cdi/serviceable", index);
120 cdi_serviceable = fgGetNode( propname, true );
121 cdi_serviceable->setBoolValue( true );
123 snprintf(propname, 256, "/instrumentation/vor[%d]/gs/serviceable", index);
124 gs_serviceable = fgGetNode( propname, true );
125 gs_serviceable->setBoolValue( true );
127 snprintf(propname, 256, "/instrumentation/vor[%d]/to-from/serviceable", index);
128 tofrom_serviceable = fgGetNode( propname, true );
129 tofrom_serviceable->setBoolValue( true );
136 // FIXME: Get rid of snprintf
139 snprintf(propname, 256, "/radios/comm[%d]/inputs/power-btn", index);
140 fgTie( propname, this,
141 &FGNavCom::get_power_btn, &FGNavCom::set_power_btn );
142 fgSetArchivable( propname );
144 snprintf(propname, 256, "/radios/comm[%d]/frequencies/selected-mhz", index);
145 fgTie( propname, this, &FGNavCom::get_comm_freq, &FGNavCom::set_comm_freq );
146 fgSetArchivable( propname );
148 snprintf(propname, 256, "/radios/comm[%d]/frequencies/standby-mhz", index);
149 fgTie( propname, this,
150 &FGNavCom::get_comm_alt_freq, &FGNavCom::set_comm_alt_freq );
151 fgSetArchivable( propname );
153 snprintf(propname, 256, "/radios/comm[%d]/volume", index);
154 fgTie( propname, this,
155 &FGNavCom::get_comm_vol_btn, &FGNavCom::set_comm_vol_btn );
156 fgSetArchivable( propname );
158 snprintf(propname, 256, "/radios/nav[%d]/frequencies/selected-mhz", index);
159 fgTie( propname, this,
160 &FGNavCom::get_nav_freq, &FGNavCom::set_nav_freq );
161 fgSetArchivable( propname );
163 snprintf(propname, 256, "/radios/nav[%d]/frequencies/standby-mhz", index);
164 fgTie( propname , this,
165 &FGNavCom::get_nav_alt_freq, &FGNavCom::set_nav_alt_freq);
166 fgSetArchivable( propname );
168 snprintf(propname, 256, "/radios/nav[%d]/radials/selected-deg", index);
169 fgTie( propname, this,
170 &FGNavCom::get_nav_sel_radial, &FGNavCom::set_nav_sel_radial );
171 fgSetArchivable( propname );
173 snprintf(propname, 256, "/radios/nav[%d]/volume", index);
174 fgTie( propname, this,
175 &FGNavCom::get_nav_vol_btn, &FGNavCom::set_nav_vol_btn );
176 fgSetArchivable( propname );
178 snprintf(propname, 256, "/radios/nav[%d]/ident", index);
179 fgTie( propname, this,
180 &FGNavCom::get_nav_ident_btn, &FGNavCom::set_nav_ident_btn );
181 fgSetArchivable( propname );
184 snprintf(propname, 256, "/radios/nav[%d]/audio-btn", index);
185 fgTie( propname, this,
186 &FGNavCom::get_audio_btn, &FGNavCom::set_audio_btn );
187 fgSetArchivable( propname );
189 snprintf(propname, 256, "/radios/nav[%d]/heading-deg", index);
190 fgTie( propname, this, &FGNavCom::get_nav_heading );
192 snprintf(propname, 256, "/radios/nav[%d]/radials/actual-deg", index);
193 fgTie( propname, this, &FGNavCom::get_nav_radial );
195 snprintf(propname, 256, "/radios/nav[%d]/radials/target-radial-deg", index);
196 fgTie( propname, this, &FGNavCom::get_nav_target_radial_true );
198 snprintf(propname, 256, "/radios/nav[%d]/radials/target-auto-hdg-deg",
200 fgTie( propname, this, &FGNavCom::get_nav_target_auto_hdg );
202 snprintf(propname, 256, "/radios/nav[%d]/to-flag", index);
203 fgTie( propname, this, &FGNavCom::get_nav_to_flag );
205 snprintf(propname, 256, "/radios/nav[%d]/from-flag", index);
206 fgTie( propname, this, &FGNavCom::get_nav_from_flag );
208 snprintf(propname, 256, "/radios/nav[%d]/in-range", index);
209 fgTie( propname, this, &FGNavCom::get_nav_inrange );
211 snprintf(propname, 256, "/radios/nav[%d]/heading-needle-deflection", index);
212 fgTie( propname, this, &FGNavCom::get_nav_cdi_deflection );
214 snprintf(propname, 256, "/radios/nav[%d]/crosstrack-error-m", index);
215 fgTie( propname, this, &FGNavCom::get_nav_cdi_xtrack_error );
217 snprintf(propname, 256, "/radios/nav[%d]/has-gs", index);
218 fgTie( propname, this, &FGNavCom::get_nav_has_gs );
220 snprintf(propname, 256, "/radios/nav[%d]/nav-loc", index);
221 fgTie( propname, this, &FGNavCom::get_nav_loc );
223 snprintf(propname, 256, "/radios/nav[%d]/gs-rate-of-climb", index);
224 fgTie( propname, this, &FGNavCom::get_nav_gs_rate_of_climb );
226 snprintf(propname, 256, "/radios/nav[%d]/gs-needle-deflection", index);
227 fgTie( propname, this, &FGNavCom::get_nav_gs_deflection );
229 snprintf(propname, 256, "/radios/nav[%d]/nav-id", index);
230 fgTie( propname, this, &FGNavCom::get_nav_id );
232 // put nav_id characters into seperate properties for instrument displays
233 snprintf(propname, 256, "/radios/nav[%d]/nav-id_asc1", index);
234 fgTie( propname, this, &FGNavCom::get_nav_id_c1 );
236 snprintf(propname, 256, "/radios/nav[%d]/nav-id_asc2", index);
237 fgTie( propname, this, &FGNavCom::get_nav_id_c2 );
239 snprintf(propname, 256, "/radios/nav[%d]/nav-id_asc3", index);
240 fgTie( propname, this, &FGNavCom::get_nav_id_c3 );
242 snprintf(propname, 256, "/radios/nav[%d]/nav-id_asc4", index);
243 fgTie( propname, this, &FGNavCom::get_nav_id_c4 );
253 // FIXME: Get rid of snprintf
255 snprintf(propname, 256, "/radios/comm[%d]/inputs/power-btn", index);
257 snprintf(propname, 256, "/radios/comm[%d]/frequencies/selected-mhz", index);
259 snprintf(propname, 256, "/radios/comm[%d]/frequencies/standby-mhz", index);
262 snprintf(propname, 256, "/radios/nav[%d]/frequencies/selected-mhz", index);
264 snprintf(propname, 256, "/radios/nav[%d]/frequencies/standby-mhz", index);
266 snprintf(propname, 256, "/radios/nav[%d]/radials/actual-deg", index);
268 snprintf(propname, 256, "/radios/nav[%d]/radials/selected-deg", index);
270 snprintf(propname, 256, "/radios/nav[%d]/ident", index);
272 snprintf(propname, 256, "/radios/nav[%d]/to-flag", index);
274 snprintf(propname, 256, "/radios/nav[%d]/from-flag", index);
276 snprintf(propname, 256, "/radios/nav[%d]/in-range", index);
278 snprintf(propname, 256, "/radios/nav[%d]/heading-needle-deflection", index);
280 snprintf(propname, 256, "/radios/nav[%d]/gs-needle-deflection", index);
285 // model standard VOR/DME/TACAN service volumes as per AIM 1-1-8
286 double FGNavCom::adjustNavRange( double stationElev, double aircraftElev,
287 double nominalRange )
289 // extend out actual usable range to be 1.3x the published safe range
290 const double usability_factor = 1.3;
292 // assumptions we model the standard service volume, plus
293 // ... rather than specifying a cylinder, we model a cone that
294 // contains the cylinder. Then we put an upside down cone on top
295 // to model diminishing returns at too-high altitudes.
297 // altitude difference
298 double alt = ( aircraftElev * SG_METER_TO_FEET - stationElev );
299 // cout << "aircraft elev = " << aircraftElev * SG_METER_TO_FEET
300 // << " station elev = " << stationElev << endl;
302 if ( nominalRange < 25.0 + SG_EPSILON ) {
303 // Standard Terminal Service Volume
304 return term_tbl->interpolate( alt ) * usability_factor;
305 } else if ( nominalRange < 50.0 + SG_EPSILON ) {
306 // Standard Low Altitude Service Volume
307 // table is based on range of 40, scale to actual range
308 return low_tbl->interpolate( alt ) * nominalRange / 40.0
311 // Standard High Altitude Service Volume
312 // table is based on range of 130, scale to actual range
313 return high_tbl->interpolate( alt ) * nominalRange / 130.0
319 // model standard ILS service volumes as per AIM 1-1-9
320 double FGNavCom::adjustILSRange( double stationElev, double aircraftElev,
321 double offsetDegrees, double distance )
323 // assumptions we model the standard service volume, plus
325 // altitude difference
326 // double alt = ( aircraftElev * SG_METER_TO_FEET - stationElev );
327 // double offset = fabs( offsetDegrees );
329 // if ( offset < 10 ) {
330 // return FG_ILS_DEFAULT_RANGE;
331 // } else if ( offset < 35 ) {
332 // return 10 + (35 - offset) * (FG_ILS_DEFAULT_RANGE - 10) / 25;
333 // } else if ( offset < 45 ) {
334 // return (45 - offset);
335 // } else if ( offset > 170 ) {
336 // return FG_ILS_DEFAULT_RANGE;
337 // } else if ( offset > 145 ) {
338 // return 10 + (offset - 145) * (FG_ILS_DEFAULT_RANGE - 10) / 25;
339 // } else if ( offset > 135 ) {
340 // return (offset - 135);
344 return FG_LOC_DEFAULT_RANGE;
348 // Update the various nav values based on position and valid tuned in navs
350 FGNavCom::update(double dt)
352 double lon = lon_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
353 double lat = lat_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
354 double elev = alt_node->getDoubleValue() * SG_FEET_TO_METER;
358 Point3D aircraft = sgGeodToCart( Point3D( lon, lat, elev ) );
362 ////////////////////////////////////////////////////////////////////////
364 ////////////////////////////////////////////////////////////////////////
366 if ( nav_valid && power_btn && (bus_power->getDoubleValue() > 1.0)
367 && nav_serviceable->getBoolValue() )
369 station = Point3D( nav_x, nav_y, nav_z );
370 nav_loc_dist = aircraft.distance3D( station );
373 // find closest distance to the gs base line
375 sgdSetVec3( p, aircraft.x(), aircraft.y(), aircraft.z() );
377 sgdSetVec3( p0, nav_gs_x, nav_gs_y, nav_gs_z );
378 double dist = sgdClosestPointToLineDistSquared( p, p0,
380 nav_gs_dist = sqrt( dist );
381 // cout << nav_gs_dist;
383 // Point3D tmp( nav_gs_x, nav_gs_y, nav_gs_z );
384 // cout << " (" << aircraft.distance3D( tmp ) << ")" << endl;
386 // wgs84 heading to glide slope (to determine sign of distance)
387 geo_inverse_wgs_84( elev,
388 lat * SGD_RADIANS_TO_DEGREES,
389 lon * SGD_RADIANS_TO_DEGREES,
390 nav_gslat, nav_gslon,
392 double r = az1 - nav_target_radial;
393 while ( r > 180.0 ) { r -= 360.0;}
394 while ( r < -180.0 ) { r += 360.0;}
395 if ( r >= -90.0 && r <= 90.0 ) {
396 nav_gs_dist_signed = nav_gs_dist;
398 nav_gs_dist_signed = -nav_gs_dist;
400 /* cout << "Target Radial = " << nav_target_radial
401 << " Bearing = " << az1
402 << " dist (signed) = " << nav_gs_dist_signed
409 // wgs84 heading to localizer
410 geo_inverse_wgs_84( elev,
411 lat * SGD_RADIANS_TO_DEGREES,
412 lon * SGD_RADIANS_TO_DEGREES,
413 nav_loclat, nav_loclon,
414 &nav_heading, &az2, &s );
415 // cout << "az1 = " << az1 << " magvar = " << nav_magvar << endl;
416 nav_radial = az2 - nav_twist;
417 // cout << " heading = " << nav_heading
418 // << " dist = " << nav_dist << endl;
421 double offset = nav_radial - nav_target_radial;
422 while ( offset < -180.0 ) { offset += 360.0; }
423 while ( offset > 180.0 ) { offset -= 360.0; }
424 // cout << "ils offset = " << offset << endl;
426 = adjustILSRange( nav_elev, elev, offset,
427 nav_loc_dist * SG_METER_TO_NM );
429 nav_effective_range = adjustNavRange( nav_elev, elev, nav_range );
431 // cout << "nav range = " << nav_effective_range
432 // << " (" << nav_range << ")" << endl;
434 if ( nav_loc_dist < nav_effective_range * SG_NM_TO_METER ) {
436 } else if ( nav_loc_dist < 2 * nav_effective_range * SG_NM_TO_METER ) {
437 nav_inrange = sg_random() <
438 ( 2 * nav_effective_range * SG_NM_TO_METER - nav_loc_dist ) /
439 (nav_effective_range * SG_NM_TO_METER);
445 nav_target_radial = nav_sel_radial;
448 // Calculate some values for the nav/ils hold autopilot
450 double cur_radial = get_nav_reciprocal_radial();
452 // ILS localizers radials are already "true" in our
455 cur_radial += nav_twist;
457 if ( get_nav_from_flag() ) {
459 while ( cur_radial >= 360.0 ) { cur_radial -= 360.0; }
464 // determine the target radial in "true" heading
465 nav_target_radial_true = nav_target_radial;
467 // ILS localizers radials are already "true" in our
470 // VOR radials need to have that vor's offset added in
471 nav_target_radial_true += nav_twist;
474 while ( nav_target_radial_true < 0.0 ) {
475 nav_target_radial_true += 360.0;
477 while ( nav_target_radial_true > 360.0 ) {
478 nav_target_radial_true -= 360.0;
481 // determine the heading adjustment needed.
482 // over 8km scale by 3.0
483 // (3 is chosen because max deflection is 10
484 // and 30 is clamped angle to radial)
485 // under 8km scale by 10.0
486 // because the overstated error helps drive it to the radial in a
487 // moderate cross wind.
488 double adjustment = 0.0;
489 if (nav_loc_dist > 8000) {
490 adjustment = get_nav_cdi_deflection() * 3.0;
492 adjustment = get_nav_cdi_deflection() * 10.0;
494 SG_CLAMP_RANGE( adjustment, -30.0, 30.0 );
496 // determine the target heading to fly to intercept the
498 nav_target_auto_hdg = nav_target_radial_true + adjustment;
499 while ( nav_target_auto_hdg < 0.0 ) { nav_target_auto_hdg += 360.0; }
500 while ( nav_target_auto_hdg > 360.0 ) { nav_target_auto_hdg -= 360.0; }
505 // Calculate desired rate of climb for intercepting the GS
506 double x = nav_gs_dist;
507 double y = (alt_node->getDoubleValue() - nav_elev)
509 double current_angle = atan2( y, x ) * SGD_RADIANS_TO_DEGREES;
511 double target_angle = nav_target_gs;
512 double gs_diff = target_angle - current_angle;
514 // convert desired vertical path angle into a climb rate
515 double des_angle = current_angle - 10 * gs_diff;
517 // estimate horizontal speed towards ILS in meters per minute
518 double dist = last_x - x;
522 double new_vel = ( dist / dt );
524 horiz_vel = 0.75 * horiz_vel + 0.25 * new_vel;
525 // double horiz_vel = cur_fdm_state->get_V_ground_speed()
526 // * SG_FEET_TO_METER * 60.0;
527 // double horiz_vel = airspeed_node->getFloatValue()
528 // * SG_FEET_TO_METER * 60.0;
530 nav_gs_rate_of_climb = -sin( des_angle * SGD_DEGREES_TO_RADIANS )
531 * horiz_vel * SG_METER_TO_FEET;
535 // cout << "not picking up vor. :-(" << endl;
538 if ( nav_valid && nav_inrange && nav_serviceable->getBoolValue() ) {
539 // play station ident via audio system if on + ident,
540 // otherwise turn it off
541 if ( power_btn && (bus_power->getDoubleValue() > 1.0)
542 && nav_ident_btn && audio_btn )
544 SGSoundSample *sound;
545 sound = globals->get_soundmgr()->find( nav_fx_name );
546 if ( sound != NULL ) {
547 sound->set_volume( nav_vol_btn );
549 SG_LOG( SG_COCKPIT, SG_ALERT,
550 "Can't find nav-vor-ident sound" );
552 sound = globals->get_soundmgr()->find( dme_fx_name );
553 if ( sound != NULL ) {
554 sound->set_volume( nav_vol_btn );
556 SG_LOG( SG_COCKPIT, SG_ALERT,
557 "Can't find nav-dme-ident sound" );
559 // cout << "nav_last_time = " << nav_last_time << " ";
560 // cout << "cur_time = "
561 // << globals->get_time_params()->get_cur_time();
563 globals->get_time_params()->get_cur_time() - 30 ) {
564 nav_last_time = globals->get_time_params()->get_cur_time();
567 // cout << " nav_play_count = " << nav_play_count << endl;
568 // cout << "playing = "
569 // << globals->get_soundmgr()->is_playing(nav_fx_name)
571 if ( nav_play_count < 4 ) {
573 if ( !globals->get_soundmgr()->is_playing(nav_fx_name) ) {
574 globals->get_soundmgr()->play_once( nav_fx_name );
577 } else if ( nav_play_count < 5 && nav_has_dme ) {
579 if ( !globals->get_soundmgr()->is_playing(nav_fx_name) &&
580 !globals->get_soundmgr()->is_playing(dme_fx_name) ) {
581 globals->get_soundmgr()->play_once( dme_fx_name );
586 globals->get_soundmgr()->stop( nav_fx_name );
587 globals->get_soundmgr()->stop( dme_fx_name );
593 // Update current nav/adf radio stations based on current postition
594 void FGNavCom::search()
596 double lon = lon_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
597 double lat = lat_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
598 double elev = alt_node->getDoubleValue() * SG_FEET_TO_METER;
600 FGNavRecord *nav = NULL;
601 FGNavRecord *loc = NULL;
602 FGNavRecord *dme = NULL;
603 FGNavRecord *gs = NULL;
605 ////////////////////////////////////////////////////////////////////////
607 ////////////////////////////////////////////////////////////////////////
609 nav = globals->get_navlist()->findByFreq(nav_freq, lon, lat, elev);
610 dme = globals->get_dmelist()->findByFreq(nav_freq, lon, lat, elev);
612 loc = globals->get_loclist()->findByFreq(nav_freq, lon, lat, elev);
613 gs = globals->get_gslist()->findByFreq(nav_freq, lon, lat, elev);
618 nav_id = loc->get_ident();
620 if ( last_nav_id != nav_id || last_nav_vor ) {
621 nav_trans_ident = loc->get_trans_ident();
622 last_nav_id = nav_id;
623 last_nav_vor = false;
625 nav_has_dme = (dme != NULL);
626 nav_has_gs = (gs != NULL);
628 nav_gslon = gs->get_lon();
629 nav_gslat = gs->get_lat();
630 nav_elev = gs->get_elev_ft();
631 int tmp = (int)(gs->get_multiuse() / 1000.0);
632 nav_target_gs = (double)tmp / 100.0;
633 nav_gs_x = gs->get_x();
634 nav_gs_y = gs->get_y();
635 nav_gs_z = gs->get_z();
637 // derive GS baseline
638 double tlon, tlat, taz;
639 geo_direct_wgs_84 ( 0.0, nav_gslat, nav_gslon,
640 nav_target_radial + 90,
641 100.0, &tlat, &tlon, &taz );
642 // cout << nav_gslon << "," << nav_gslat << " "
643 // << tlon << "," << tlat << " (" << nav_elev << ")"
645 Point3D p1 = sgGeodToCart( Point3D(tlon*SGD_DEGREES_TO_RADIANS,
646 tlat*SGD_DEGREES_TO_RADIANS,
647 nav_elev*SG_FEET_TO_METER)
649 // cout << nav_gs_x << "," << nav_gs_y << "," << nav_gs_z
651 // cout << p1 << endl;
652 sgdSetVec3( gs_base_vec,
653 p1.x()-nav_gs_x, p1.y()-nav_gs_y, p1.z()-nav_gs_z );
654 // cout << gs_base_vec[0] << "," << gs_base_vec[1] << ","
655 // << gs_base_vec[2] << endl;
657 nav_elev = loc->get_elev_ft();
659 nav_loclon = loc->get_lon();
660 nav_loclat = loc->get_lat();
662 nav_range = FG_LOC_DEFAULT_RANGE;
663 nav_effective_range = nav_range;
664 nav_target_radial = loc->get_multiuse();
665 while ( nav_target_radial < 0.0 ) { nav_target_radial += 360.0; }
666 while ( nav_target_radial > 360.0 ) { nav_target_radial -= 360.0; }
667 nav_x = loc->get_x();
668 nav_y = loc->get_y();
669 nav_z = loc->get_z();
671 if ( globals->get_soundmgr()->exists( nav_fx_name ) ) {
672 globals->get_soundmgr()->remove( nav_fx_name );
674 SGSoundSample *sound;
675 sound = morse.make_ident( nav_trans_ident, LO_FREQUENCY );
676 sound->set_volume( 0.3 );
677 globals->get_soundmgr()->add( sound, nav_fx_name );
679 if ( globals->get_soundmgr()->exists( dme_fx_name ) ) {
680 globals->get_soundmgr()->remove( dme_fx_name );
682 sound = morse.make_ident( nav_trans_ident, HI_FREQUENCY );
683 sound->set_volume( 0.3 );
684 globals->get_soundmgr()->add( sound, dme_fx_name );
686 int offset = (int)(sg_random() * 30.0);
687 nav_play_count = offset / 4;
688 nav_last_time = globals->get_time_params()->get_cur_time() -
690 // cout << "offset = " << offset << " play_count = "
692 // << " nav_last_time = " << nav_last_time
693 // << " current time = "
694 // << globals->get_time_params()->get_cur_time() << endl;
696 // cout << "Found an loc station in range" << endl;
697 // cout << " id = " << loc->get_locident() << endl;
699 } else if ( nav != NULL ) {
700 nav_id = nav->get_ident();
702 if ( last_nav_id != nav_id || !last_nav_vor ) {
703 last_nav_id = nav_id;
705 nav_trans_ident = nav->get_trans_ident();
707 nav_has_dme = (dme != NULL);
709 nav_loclon = nav->get_lon();
710 nav_loclat = nav->get_lat();
711 nav_elev = nav->get_elev_ft();
712 nav_twist = nav->get_multiuse();
713 nav_range = nav->get_range();
714 nav_effective_range = adjustNavRange(nav_elev, elev, nav_range);
716 nav_target_radial = nav_sel_radial;
717 nav_x = nav->get_x();
718 nav_y = nav->get_y();
719 nav_z = nav->get_z();
721 if ( globals->get_soundmgr()->exists( nav_fx_name ) ) {
722 globals->get_soundmgr()->remove( nav_fx_name );
724 SGSoundSample *sound;
725 sound = morse.make_ident( nav_trans_ident, LO_FREQUENCY );
726 sound->set_volume( 0.3 );
727 if ( globals->get_soundmgr()->add( sound, nav_fx_name ) ) {
728 // cout << "Added nav-vor-ident sound" << endl;
730 SG_LOG(SG_COCKPIT, SG_WARN, "Failed to add v1-vor-ident sound");
733 if ( globals->get_soundmgr()->exists( dme_fx_name ) ) {
734 globals->get_soundmgr()->remove( dme_fx_name );
736 sound = morse.make_ident( nav_trans_ident, HI_FREQUENCY );
737 sound->set_volume( 0.3 );
738 globals->get_soundmgr()->add( sound, dme_fx_name );
740 int offset = (int)(sg_random() * 30.0);
741 nav_play_count = offset / 4;
742 nav_last_time = globals->get_time_params()->get_cur_time() -
744 // cout << "offset = " << offset << " play_count = "
745 // << nav_play_count << " nav_last_time = "
746 // << nav_last_time << " current time = "
747 // << globals->get_time_params()->get_cur_time() << endl;
749 // cout << "Found a vor station in range" << endl;
750 // cout << " id = " << nav->get_ident() << endl;
755 nav_target_radial = 0;
756 nav_trans_ident = "";
758 if ( ! globals->get_soundmgr()->remove( nav_fx_name ) ) {
759 SG_LOG(SG_COCKPIT, SG_WARN, "Failed to remove nav-vor-ident sound");
761 globals->get_soundmgr()->remove( dme_fx_name );
762 // cout << "not picking up vor1. :-(" << endl;
767 // return the amount of heading needle deflection, returns a value
768 // clamped to the range of ( -10 , 10 )
769 double FGNavCom::get_nav_cdi_deflection() const {
773 && nav_serviceable->getBoolValue() && cdi_serviceable->getBoolValue() )
775 r = nav_radial - nav_target_radial;
776 // cout << "Target radial = " << nav_target_radial
777 // << " Actual radial = " << nav_radial << endl;
779 while ( r > 180.0 ) { r -= 360.0;}
780 while ( r < -180.0 ) { r += 360.0;}
781 if ( fabs(r) > 90.0 )
782 r = ( r<0.0 ? -r-180.0 : -r+180.0 );
784 // According to Robin Peel, the ILS is 4x more sensitive than a vor
785 r = -r; // reverse, since radial is outbound
786 if ( nav_loc ) { r *= 4.0; }
787 if ( r < -10.0 ) { r = -10.0; }
788 if ( r > 10.0 ) { r = 10.0; }
796 // return the amount of cross track distance error, returns a meters
797 double FGNavCom::get_nav_cdi_xtrack_error() const {
801 && nav_serviceable->getBoolValue() && cdi_serviceable->getBoolValue() )
803 r = nav_radial - nav_target_radial;
804 // cout << "Target radial = " << nav_target_radial
805 // << " Actual radial = " << nav_radial
806 // << " r = " << r << endl;
808 while ( r > 180.0 ) { r -= 360.0;}
809 while ( r < -180.0 ) { r += 360.0;}
810 if ( fabs(r) > 90.0 )
811 r = ( r<0.0 ? -r-180.0 : -r+180.0 );
813 r = -r; // reverse, since radial is outbound
815 m = nav_loc_dist * sin(r * SGD_DEGREES_TO_RADIANS);
824 // return the amount of glide slope needle deflection (.i.e. the
825 // number of degrees we are off the glide slope * 5.0
826 double FGNavCom::get_nav_gs_deflection() const {
827 if ( nav_inrange && nav_has_gs
828 && nav_serviceable->getBoolValue() && gs_serviceable->getBoolValue() )
830 double x = nav_gs_dist;
831 double y = (fgGetDouble("/position/altitude-ft") - nav_elev)
833 // cout << "dist = " << x << " height = " << y << endl;
834 double angle = asin( y / x ) * SGD_RADIANS_TO_DEGREES;
835 return (nav_target_gs - angle) * 5.0;
843 * Return true if the NAV TO flag should be active.
846 FGNavCom::get_nav_to_flag () const
849 && nav_serviceable->getBoolValue()
850 && tofrom_serviceable->getBoolValue() )
852 double offset = fabs(nav_radial - nav_target_radial);
856 return !(offset <= 90.0 || offset >= 270.0);
865 * Return true if the NAV FROM flag should be active.
868 FGNavCom::get_nav_from_flag () const
871 && nav_serviceable->getBoolValue()
872 && tofrom_serviceable->getBoolValue() ) {
873 double offset = fabs(nav_radial - nav_target_radial);
877 return !(offset > 90.0 && offset < 270.0);
886 * Return the true heading to station
889 FGNavCom::get_nav_heading () const
896 * Return the current radial.
899 FGNavCom::get_nav_radial () const
905 FGNavCom::get_nav_reciprocal_radial () const
907 double recip = nav_radial + 180;
908 if ( recip >= 360 ) {