1 // navradio.cxx -- class to manage a nav radio instance
3 // Written by Curtis Olson, started April 2000.
5 // Copyright (C) 2000 - 2002 Curtis L. Olson - http://www.flightgear.org/~curt
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.
32 #include <simgear/compiler.h>
33 #include <simgear/sg_inlines.h>
34 #include <simgear/math/sg_random.h>
35 #include <simgear/math/vector.hxx>
37 #include <Aircraft/aircraft.hxx>
38 #include <Navaids/navlist.hxx>
40 #include "navradio.hxx"
47 FGNavRadio::FGNavRadio(SGPropertyNode *node) :
48 lon_node(fgGetNode("/position/longitude-deg", true)),
49 lat_node(fgGetNode("/position/latitude-deg", true)),
50 alt_node(fgGetNode("/position/altitude-ft", true)),
62 nav_target_radial(0.0),
63 nav_target_radial_true(0.0),
64 nav_target_auto_hdg(0.0),
65 nav_gs_rate_of_climb(0.0),
73 SGPath path( globals->get_fg_root() );
75 term.append( "Navaids/range.term" );
77 low.append( "Navaids/range.low" );
79 high.append( "Navaids/range.high" );
81 term_tbl = new SGInterpTable( term.str() );
82 low_tbl = new SGInterpTable( low.str() );
83 high_tbl = new SGInterpTable( high.str() );
86 for ( i = 0; i < node->nChildren(); ++i ) {
87 SGPropertyNode *child = node->getChild(i);
88 string cname = child->getName();
89 string cval = child->getStringValue();
90 if ( cname == "name" ) {
92 } else if ( cname == "number" ) {
93 num = child->getIntValue();
95 SG_LOG( SG_INSTR, SG_WARN,
96 "Error in nav radio config logic" );
97 if ( name.length() ) {
98 SG_LOG( SG_INSTR, SG_WARN, "Section = " << name );
107 FGNavRadio::~FGNavRadio()
121 branch = "/instrumentation/" + name;
123 SGPropertyNode *node = fgGetNode(branch.c_str(), num, true );
126 fgGetNode(("/systems/electrical/outputs/" + name).c_str(), true);
127 nav_serviceable = node->getChild("serviceable", 0, true);
128 cdi_serviceable = (node->getChild("cdi", 0, true))
129 ->getChild("serviceable", 0, true);
130 gs_serviceable = (node->getChild("gs", 0, true))
131 ->getNode("serviceable");
132 tofrom_serviceable = (node->getChild("to-from", 0, true))
133 ->getChild("serviceable", 0, true);
139 std::ostringstream temp;
142 branch = "/instrumentation/" + name + "[" + temp.str() + "]";
145 fgTie( (branch + "/frequencies/selected-mhz").c_str() , this,
146 &FGNavRadio::get_nav_freq, &FGNavRadio::set_nav_freq );
147 fgSetArchivable( (branch + "/frequencies/selected-mhz").c_str() );
149 fgTie( (branch + "/frequencies/standby-mhz").c_str() , this,
150 &FGNavRadio::get_nav_alt_freq, &FGNavRadio::set_nav_alt_freq);
151 fgSetArchivable( (branch + "/frequencies/standby-mhz").c_str() );
153 fgTie( (branch + "/radials/selected-deg").c_str() , this,
154 &FGNavRadio::get_nav_sel_radial, &FGNavRadio::set_nav_sel_radial );
155 fgSetArchivable((branch + "/radials/selected-deg").c_str() );
157 fgTie( (branch + "/volume").c_str() , this,
158 &FGNavRadio::get_nav_vol_btn, &FGNavRadio::set_nav_vol_btn );
159 fgSetArchivable( (branch + "/volume").c_str() );
161 fgTie( (branch + "/ident").c_str(), this,
162 &FGNavRadio::get_nav_ident_btn, &FGNavRadio::set_nav_ident_btn );
163 fgSetArchivable( (branch + "/ident").c_str() );
166 fgTie( (branch + "/audio-btn").c_str(), this,
167 &FGNavRadio::get_audio_btn, &FGNavRadio::set_audio_btn );
168 fgSetArchivable( (branch + "/audio-btn").c_str() );
170 fgTie( (branch + "/heading-deg").c_str(),
171 this, &FGNavRadio::get_nav_heading );
173 fgTie( (branch + "/radials/actual-deg").c_str(),
174 this, &FGNavRadio::get_nav_radial );
176 fgTie( (branch + "/radials/target-radial-deg").c_str(),
177 this, &FGNavRadio::get_nav_target_radial_true );
179 fgTie( (branch + "/radials/target-auto-hdg-deg").c_str(),
180 this, &FGNavRadio::get_nav_target_auto_hdg );
182 fgTie( (branch + "/to-flag").c_str(),
183 this, &FGNavRadio::get_nav_to_flag );
185 fgTie( (branch + "/from-flag").c_str(),
186 this, &FGNavRadio::get_nav_from_flag );
188 fgTie( (branch + "/in-range").c_str(),
189 this, &FGNavRadio::get_nav_inrange );
191 fgTie( (branch + "/heading-needle-deflection").c_str(),
192 this, &FGNavRadio::get_nav_cdi_deflection );
194 fgTie( (branch + "/crosstrack-error-m").c_str(),
195 this, &FGNavRadio::get_nav_cdi_xtrack_error );
197 fgTie( (branch + "/has-gs").c_str(),
198 this, &FGNavRadio::get_nav_has_gs );
200 fgTie( (branch + "/nav-loc").c_str(),
201 this, &FGNavRadio::get_nav_loc );
203 fgTie( (branch + "/gs-rate-of-climb").c_str(),
204 this, &FGNavRadio::get_nav_gs_rate_of_climb );
206 fgTie( (branch + "/gs-needle-deflection").c_str(),
207 this, &FGNavRadio::get_nav_gs_deflection );
209 fgTie( (branch + "/nav-id").c_str(),
210 this, &FGNavRadio::get_nav_id );
212 // put nav_id characters into seperate properties for instrument displays
213 fgTie( (branch + "/nav-id_asc1").c_str(),
214 this, &FGNavRadio::get_nav_id_c1 );
216 fgTie( (branch + "/nav-id_asc2").c_str(),
217 this, &FGNavRadio::get_nav_id_c2 );
219 fgTie( (branch + "/nav-id_asc3").c_str(),
220 this, &FGNavRadio::get_nav_id_c3 );
222 fgTie( (branch + "/nav-id_asc4").c_str(),
223 this, &FGNavRadio::get_nav_id_c4 );
230 FGNavRadio::unbind ()
232 std::ostringstream temp;
235 branch = "/instrumentation/" + name + "[" + temp.str() + "]";
237 fgUntie( (branch + "/frequencies/selected-mhz").c_str() );
238 fgUntie( (branch + "/frequencies/standby-mhz").c_str() );
239 fgUntie( (branch + "/radials/actual-deg").c_str() );
240 fgUntie( (branch + "/radials/selected-deg").c_str() );
241 fgUntie( (branch + "/ident").c_str() );
242 fgUntie( (branch + "/to-flag").c_str() );
243 fgUntie( (branch + "/from-flag").c_str() );
244 fgUntie( (branch + "/in-range").c_str() );
245 fgUntie( (branch + "/heading-needle-deflection").c_str() );
246 fgUntie( (branch + "/gs-needle-deflection").c_str() );
250 // model standard VOR/DME/TACAN service volumes as per AIM 1-1-8
251 double FGNavRadio::adjustNavRange( double stationElev, double aircraftElev,
252 double nominalRange )
254 // extend out actual usable range to be 1.3x the published safe range
255 const double usability_factor = 1.3;
257 // assumptions we model the standard service volume, plus
258 // ... rather than specifying a cylinder, we model a cone that
259 // contains the cylinder. Then we put an upside down cone on top
260 // to model diminishing returns at too-high altitudes.
262 // altitude difference
263 double alt = ( aircraftElev * SG_METER_TO_FEET - stationElev );
264 // cout << "aircraft elev = " << aircraftElev * SG_METER_TO_FEET
265 // << " station elev = " << stationElev << endl;
267 if ( nominalRange < 25.0 + SG_EPSILON ) {
268 // Standard Terminal Service Volume
269 return term_tbl->interpolate( alt ) * usability_factor;
270 } else if ( nominalRange < 50.0 + SG_EPSILON ) {
271 // Standard Low Altitude Service Volume
272 // table is based on range of 40, scale to actual range
273 return low_tbl->interpolate( alt ) * nominalRange / 40.0
276 // Standard High Altitude Service Volume
277 // table is based on range of 130, scale to actual range
278 return high_tbl->interpolate( alt ) * nominalRange / 130.0
284 // model standard ILS service volumes as per AIM 1-1-9
285 double FGNavRadio::adjustILSRange( double stationElev, double aircraftElev,
286 double offsetDegrees, double distance )
288 // assumptions we model the standard service volume, plus
290 // altitude difference
291 // double alt = ( aircraftElev * SG_METER_TO_FEET - stationElev );
292 // double offset = fabs( offsetDegrees );
294 // if ( offset < 10 ) {
295 // return FG_ILS_DEFAULT_RANGE;
296 // } else if ( offset < 35 ) {
297 // return 10 + (35 - offset) * (FG_ILS_DEFAULT_RANGE - 10) / 25;
298 // } else if ( offset < 45 ) {
299 // return (45 - offset);
300 // } else if ( offset > 170 ) {
301 // return FG_ILS_DEFAULT_RANGE;
302 // } else if ( offset > 145 ) {
303 // return 10 + (offset - 145) * (FG_ILS_DEFAULT_RANGE - 10) / 25;
304 // } else if ( offset > 135 ) {
305 // return (offset - 135);
309 return FG_LOC_DEFAULT_RANGE;
313 // Update the various nav values based on position and valid tuned in navs
315 FGNavRadio::update(double dt)
317 double lon = lon_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
318 double lat = lat_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
319 double elev = alt_node->getDoubleValue() * SG_FEET_TO_METER;
323 Point3D aircraft = sgGeodToCart( Point3D( lon, lat, elev ) );
327 // On timeout, scan again
328 _time_before_search_sec -= dt;
329 if ( _time_before_search_sec < 0 ) {
333 ////////////////////////////////////////////////////////////////////////
335 ////////////////////////////////////////////////////////////////////////
337 if ( nav_valid && power_btn && (bus_power->getDoubleValue() > 1.0)
338 && nav_serviceable->getBoolValue() )
340 station = Point3D( nav_x, nav_y, nav_z );
341 nav_loc_dist = aircraft.distance3D( station );
344 // find closest distance to the gs base line
346 sgdSetVec3( p, aircraft.x(), aircraft.y(), aircraft.z() );
348 sgdSetVec3( p0, nav_gs_x, nav_gs_y, nav_gs_z );
349 double dist = sgdClosestPointToLineDistSquared( p, p0,
351 nav_gs_dist = sqrt( dist );
352 // cout << "nav_gs_dist = " << nav_gs_dist << endl;
354 Point3D tmp( nav_gs_x, nav_gs_y, nav_gs_z );
355 // cout << " (" << aircraft.distance3D( tmp ) << ")" << endl;
357 // wgs84 heading to glide slope (to determine sign of distance)
358 geo_inverse_wgs_84( elev,
359 lat * SGD_RADIANS_TO_DEGREES,
360 lon * SGD_RADIANS_TO_DEGREES,
361 nav_gslat, nav_gslon,
363 double r = az1 - nav_target_radial;
364 while ( r > 180.0 ) { r -= 360.0;}
365 while ( r < -180.0 ) { r += 360.0;}
366 if ( r >= -90.0 && r <= 90.0 ) {
367 nav_gs_dist_signed = nav_gs_dist;
369 nav_gs_dist_signed = -nav_gs_dist;
371 /* cout << "Target Radial = " << nav_target_radial
372 << " Bearing = " << az1
373 << " dist (signed) = " << nav_gs_dist_signed
380 // wgs84 heading to localizer
381 geo_inverse_wgs_84( elev,
382 lat * SGD_RADIANS_TO_DEGREES,
383 lon * SGD_RADIANS_TO_DEGREES,
384 nav_loclat, nav_loclon,
385 &nav_heading, &az2, &s );
386 // cout << "az1 = " << az1 << " magvar = " << nav_magvar << endl;
387 nav_radial = az2 - nav_twist;
388 // cout << " heading = " << nav_heading
389 // << " dist = " << nav_dist << endl;
392 double offset = nav_radial - nav_target_radial;
393 while ( offset < -180.0 ) { offset += 360.0; }
394 while ( offset > 180.0 ) { offset -= 360.0; }
395 // cout << "ils offset = " << offset << endl;
397 = adjustILSRange( nav_elev, elev, offset,
398 nav_loc_dist * SG_METER_TO_NM );
400 nav_effective_range = adjustNavRange( nav_elev, elev, nav_range );
402 // cout << "nav range = " << nav_effective_range
403 // << " (" << nav_range << ")" << endl;
405 if ( nav_loc_dist < nav_effective_range * SG_NM_TO_METER ) {
407 } else if ( nav_loc_dist < 2 * nav_effective_range * SG_NM_TO_METER ) {
408 nav_inrange = sg_random() <
409 ( 2 * nav_effective_range * SG_NM_TO_METER - nav_loc_dist ) /
410 (nav_effective_range * SG_NM_TO_METER);
416 nav_target_radial = nav_sel_radial;
419 // Calculate some values for the nav/ils hold autopilot
421 double cur_radial = get_nav_reciprocal_radial();
423 // ILS localizers radials are already "true" in our
426 cur_radial += nav_twist;
428 if ( get_nav_from_flag() ) {
430 while ( cur_radial >= 360.0 ) { cur_radial -= 360.0; }
435 // determine the target radial in "true" heading
436 nav_target_radial_true = nav_target_radial;
438 // ILS localizers radials are already "true" in our
441 // VOR radials need to have that vor's offset added in
442 nav_target_radial_true += nav_twist;
445 while ( nav_target_radial_true < 0.0 ) {
446 nav_target_radial_true += 360.0;
448 while ( nav_target_radial_true > 360.0 ) {
449 nav_target_radial_true -= 360.0;
452 // determine the heading adjustment needed.
453 // over 8km scale by 3.0
454 // (3 is chosen because max deflection is 10
455 // and 30 is clamped angle to radial)
456 // under 8km scale by 10.0
457 // because the overstated error helps drive it to the radial in a
458 // moderate cross wind.
459 double adjustment = 0.0;
460 if (nav_loc_dist > 8000) {
461 adjustment = get_nav_cdi_deflection() * 3.0;
463 adjustment = get_nav_cdi_deflection() * 10.0;
465 SG_CLAMP_RANGE( adjustment, -30.0, 30.0 );
467 // determine the target heading to fly to intercept the
469 nav_target_auto_hdg = nav_target_radial_true + adjustment;
470 while ( nav_target_auto_hdg < 0.0 ) { nav_target_auto_hdg += 360.0; }
471 while ( nav_target_auto_hdg > 360.0 ) { nav_target_auto_hdg -= 360.0; }
476 // Calculate desired rate of climb for intercepting the GS
477 double x = nav_gs_dist;
478 double y = (alt_node->getDoubleValue() - nav_elev)
480 double current_angle = atan2( y, x ) * SGD_RADIANS_TO_DEGREES;
482 double target_angle = nav_target_gs;
483 double gs_diff = target_angle - current_angle;
485 // convert desired vertical path angle into a climb rate
486 double des_angle = current_angle - 10 * gs_diff;
488 // estimate horizontal speed towards ILS in meters per minute
489 double dist = last_x - x;
493 double new_vel = ( dist / dt );
495 horiz_vel = 0.75 * horiz_vel + 0.25 * new_vel;
496 // double horiz_vel = cur_fdm_state->get_V_ground_speed()
497 // * SG_FEET_TO_METER * 60.0;
498 // double horiz_vel = airspeed_node->getFloatValue()
499 // * SG_FEET_TO_METER * 60.0;
501 nav_gs_rate_of_climb = -sin( des_angle * SGD_DEGREES_TO_RADIANS )
502 * horiz_vel * SG_METER_TO_FEET;
506 // cout << "not picking up vor. :-(" << endl;
509 if ( nav_valid && nav_inrange && nav_serviceable->getBoolValue() ) {
510 // play station ident via audio system if on + ident,
511 // otherwise turn it off
512 if ( power_btn && (bus_power->getDoubleValue() > 1.0)
513 && nav_ident_btn && audio_btn )
515 SGSoundSample *sound;
516 sound = globals->get_soundmgr()->find( nav_fx_name );
517 if ( sound != NULL ) {
518 sound->set_volume( nav_vol_btn );
520 SG_LOG( SG_COCKPIT, SG_ALERT,
521 "Can't find nav-vor-ident sound" );
523 sound = globals->get_soundmgr()->find( dme_fx_name );
524 if ( sound != NULL ) {
525 sound->set_volume( nav_vol_btn );
527 SG_LOG( SG_COCKPIT, SG_ALERT,
528 "Can't find nav-dme-ident sound" );
530 // cout << "nav_last_time = " << nav_last_time << " ";
531 // cout << "cur_time = "
532 // << globals->get_time_params()->get_cur_time();
534 globals->get_time_params()->get_cur_time() - 30 ) {
535 nav_last_time = globals->get_time_params()->get_cur_time();
538 // cout << " nav_play_count = " << nav_play_count << endl;
539 // cout << "playing = "
540 // << globals->get_soundmgr()->is_playing(nav_fx_name)
542 if ( nav_play_count < 4 ) {
544 if ( !globals->get_soundmgr()->is_playing(nav_fx_name) ) {
545 globals->get_soundmgr()->play_once( nav_fx_name );
548 } else if ( nav_play_count < 5 && nav_has_dme ) {
550 if ( !globals->get_soundmgr()->is_playing(nav_fx_name) &&
551 !globals->get_soundmgr()->is_playing(dme_fx_name) ) {
552 globals->get_soundmgr()->play_once( dme_fx_name );
557 globals->get_soundmgr()->stop( nav_fx_name );
558 globals->get_soundmgr()->stop( dme_fx_name );
564 // Update current nav/adf radio stations based on current postition
565 void FGNavRadio::search()
569 _time_before_search_sec = 1.0;
571 double lon = lon_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
572 double lat = lat_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
573 double elev = alt_node->getDoubleValue() * SG_FEET_TO_METER;
575 FGNavRecord *nav = NULL;
576 FGNavRecord *loc = NULL;
577 FGNavRecord *dme = NULL;
578 FGNavRecord *gs = NULL;
580 ////////////////////////////////////////////////////////////////////////
582 ////////////////////////////////////////////////////////////////////////
584 nav = globals->get_navlist()->findByFreq(nav_freq, lon, lat, elev);
585 dme = globals->get_dmelist()->findByFreq(nav_freq, lon, lat, elev);
587 loc = globals->get_loclist()->findByFreq(nav_freq, lon, lat, elev);
588 gs = globals->get_gslist()->findByFreq(nav_freq, lon, lat, elev);
592 nav_id = loc->get_ident();
593 // cout << "localizer = " << nav_id << endl;
595 if ( last_nav_id != nav_id || last_nav_vor ) {
596 nav_trans_ident = loc->get_trans_ident();
597 nav_target_radial = loc->get_multiuse();
598 while ( nav_target_radial < 0.0 ) { nav_target_radial += 360.0; }
599 while ( nav_target_radial > 360.0 ) { nav_target_radial -= 360.0; }
600 nav_loclon = loc->get_lon();
601 nav_loclat = loc->get_lat();
602 nav_x = loc->get_x();
603 nav_y = loc->get_y();
604 nav_z = loc->get_z();
605 last_nav_id = nav_id;
606 last_nav_vor = false;
608 nav_has_dme = (dme != NULL);
609 nav_has_gs = (gs != NULL);
611 nav_gslon = gs->get_lon();
612 nav_gslat = gs->get_lat();
613 nav_elev = gs->get_elev_ft();
614 int tmp = (int)(gs->get_multiuse() / 1000.0);
615 nav_target_gs = (double)tmp / 100.0;
616 nav_gs_x = gs->get_x();
617 nav_gs_y = gs->get_y();
618 nav_gs_z = gs->get_z();
620 // derive GS baseline (perpendicular to the runay
622 double tlon, tlat, taz;
623 geo_direct_wgs_84 ( 0.0, nav_gslat, nav_gslon,
624 nav_target_radial + 90,
625 100.0, &tlat, &tlon, &taz );
626 // cout << "nav_target_radial = " << nav_target_radial << endl;
627 // cout << "nav_loc = " << nav_loc << endl;
628 // cout << nav_gslon << "," << nav_gslat << " "
629 // << tlon << "," << tlat << " (" << nav_elev << ")"
631 Point3D p1 = sgGeodToCart( Point3D(tlon*SGD_DEGREES_TO_RADIANS,
632 tlat*SGD_DEGREES_TO_RADIANS,
633 nav_elev*SG_FEET_TO_METER)
635 // cout << nav_gs_x << "," << nav_gs_y << "," << nav_gs_z
637 // cout << p1 << endl;
638 sgdSetVec3( gs_base_vec,
639 p1.x()-nav_gs_x, p1.y()-nav_gs_y, p1.z()-nav_gs_z );
640 // cout << gs_base_vec[0] << "," << gs_base_vec[1] << ","
641 // << gs_base_vec[2] << endl;
643 nav_elev = loc->get_elev_ft();
646 nav_range = FG_LOC_DEFAULT_RANGE;
647 nav_effective_range = nav_range;
649 if ( globals->get_soundmgr()->exists( nav_fx_name ) ) {
650 globals->get_soundmgr()->remove( nav_fx_name );
652 SGSoundSample *sound;
653 sound = morse.make_ident( nav_trans_ident, LO_FREQUENCY );
654 sound->set_volume( 0.3 );
655 globals->get_soundmgr()->add( sound, nav_fx_name );
657 if ( globals->get_soundmgr()->exists( dme_fx_name ) ) {
658 globals->get_soundmgr()->remove( dme_fx_name );
660 sound = morse.make_ident( nav_trans_ident, HI_FREQUENCY );
661 sound->set_volume( 0.3 );
662 globals->get_soundmgr()->add( sound, dme_fx_name );
664 int offset = (int)(sg_random() * 30.0);
665 nav_play_count = offset / 4;
666 nav_last_time = globals->get_time_params()->get_cur_time() -
668 // cout << "offset = " << offset << " play_count = "
670 // << " nav_last_time = " << nav_last_time
671 // << " current time = "
672 // << globals->get_time_params()->get_cur_time() << endl;
674 // cout << "Found an loc station in range" << endl;
675 // cout << " id = " << loc->get_locident() << endl;
677 } else if ( nav != NULL ) {
678 nav_id = nav->get_ident();
679 // cout << "nav = " << nav_id << endl;
681 if ( last_nav_id != nav_id || !last_nav_vor ) {
682 last_nav_id = nav_id;
684 nav_trans_ident = nav->get_trans_ident();
686 nav_has_dme = (dme != NULL);
688 nav_loclon = nav->get_lon();
689 nav_loclat = nav->get_lat();
690 nav_elev = nav->get_elev_ft();
691 nav_twist = nav->get_multiuse();
692 nav_range = nav->get_range();
693 nav_effective_range = adjustNavRange(nav_elev, elev, nav_range);
695 nav_target_radial = nav_sel_radial;
696 nav_x = nav->get_x();
697 nav_y = nav->get_y();
698 nav_z = nav->get_z();
700 if ( globals->get_soundmgr()->exists( nav_fx_name ) ) {
701 globals->get_soundmgr()->remove( nav_fx_name );
703 SGSoundSample *sound;
704 sound = morse.make_ident( nav_trans_ident, LO_FREQUENCY );
705 sound->set_volume( 0.3 );
706 if ( globals->get_soundmgr()->add( sound, nav_fx_name ) ) {
707 // cout << "Added nav-vor-ident sound" << endl;
709 SG_LOG(SG_COCKPIT, SG_WARN, "Failed to add v1-vor-ident sound");
712 if ( globals->get_soundmgr()->exists( dme_fx_name ) ) {
713 globals->get_soundmgr()->remove( dme_fx_name );
715 sound = morse.make_ident( nav_trans_ident, HI_FREQUENCY );
716 sound->set_volume( 0.3 );
717 globals->get_soundmgr()->add( sound, dme_fx_name );
719 int offset = (int)(sg_random() * 30.0);
720 nav_play_count = offset / 4;
721 nav_last_time = globals->get_time_params()->get_cur_time() -
723 // cout << "offset = " << offset << " play_count = "
724 // << nav_play_count << " nav_last_time = "
725 // << nav_last_time << " current time = "
726 // << globals->get_time_params()->get_cur_time() << endl;
728 // cout << "Found a vor station in range" << endl;
729 // cout << " id = " << nav->get_ident() << endl;
734 nav_target_radial = 0;
735 nav_trans_ident = "";
737 if ( ! globals->get_soundmgr()->remove( nav_fx_name ) ) {
738 SG_LOG(SG_COCKPIT, SG_WARN, "Failed to remove nav-vor-ident sound");
740 globals->get_soundmgr()->remove( dme_fx_name );
741 // cout << "not picking up vor1. :-(" << endl;
746 // return the amount of heading needle deflection, returns a value
747 // clamped to the range of ( -10 , 10 )
748 double FGNavRadio::get_nav_cdi_deflection() const {
752 && nav_serviceable->getBoolValue() && cdi_serviceable->getBoolValue() )
754 r = nav_radial - nav_target_radial;
755 // cout << "Target radial = " << nav_target_radial
756 // << " Actual radial = " << nav_radial << endl;
758 while ( r > 180.0 ) { r -= 360.0;}
759 while ( r < -180.0 ) { r += 360.0;}
760 if ( fabs(r) > 90.0 )
761 r = ( r<0.0 ? -r-180.0 : -r+180.0 );
763 // According to Robin Peel, the ILS is 4x more sensitive than a vor
764 r = -r; // reverse, since radial is outbound
765 if ( nav_loc ) { r *= 4.0; }
766 if ( r < -10.0 ) { r = -10.0; }
767 if ( r > 10.0 ) { r = 10.0; }
775 // return the amount of cross track distance error, returns a meters
776 double FGNavRadio::get_nav_cdi_xtrack_error() const {
780 && nav_serviceable->getBoolValue() && cdi_serviceable->getBoolValue() )
782 r = nav_radial - nav_target_radial;
783 // cout << "Target radial = " << nav_target_radial
784 // << " Actual radial = " << nav_radial
785 // << " r = " << r << endl;
787 while ( r > 180.0 ) { r -= 360.0;}
788 while ( r < -180.0 ) { r += 360.0;}
789 if ( fabs(r) > 90.0 )
790 r = ( r<0.0 ? -r-180.0 : -r+180.0 );
792 r = -r; // reverse, since radial is outbound
794 m = nav_loc_dist * sin(r * SGD_DEGREES_TO_RADIANS);
803 // return the amount of glide slope needle deflection (.i.e. the
804 // number of degrees we are off the glide slope * 5.0
805 double FGNavRadio::get_nav_gs_deflection() const {
806 if ( nav_inrange && nav_has_gs
807 && nav_serviceable->getBoolValue() && gs_serviceable->getBoolValue() )
809 double x = nav_gs_dist;
810 double y = (fgGetDouble("/position/altitude-ft") - nav_elev)
812 // cout << "dist = " << x << " height = " << y << endl;
813 double angle = asin( y / x ) * SGD_RADIANS_TO_DEGREES;
814 return (nav_target_gs - angle) * 5.0;
822 * Return true if the NAV TO flag should be active.
825 FGNavRadio::get_nav_to_flag () const
828 && nav_serviceable->getBoolValue()
829 && tofrom_serviceable->getBoolValue() )
831 double offset = fabs(nav_radial - nav_target_radial);
835 return !(offset <= 90.0 || offset >= 270.0);
844 * Return true if the NAV FROM flag should be active.
847 FGNavRadio::get_nav_from_flag () const
850 && nav_serviceable->getBoolValue()
851 && tofrom_serviceable->getBoolValue() ) {
852 double offset = fabs(nav_radial - nav_target_radial);
856 return !(offset > 90.0 && offset < 270.0);
865 * Return the true heading to station
868 FGNavRadio::get_nav_heading () const
875 * Return the current radial.
878 FGNavRadio::get_nav_radial () const
884 FGNavRadio::get_nav_reciprocal_radial () const
886 double recip = nav_radial + 180;
887 if ( recip >= 360 ) {