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 reciprocal_radial(NULL),
63 nav_target_radial_true(NULL),
64 nav_target_auto_hdg(NULL),
68 nav_cdi_deflection(NULL),
69 nav_cdi_xtrack_error(NULL),
73 nav_gs_deflection(NULL),
74 nav_gs_rate_of_climb(NULL),
85 nav_target_radial(0.0),
90 _time_before_search_sec(-1.0)
92 SGPath path( globals->get_fg_root() );
94 term.append( "Navaids/range.term" );
96 low.append( "Navaids/range.low" );
98 high.append( "Navaids/range.high" );
100 term_tbl = new SGInterpTable( term.str() );
101 low_tbl = new SGInterpTable( low.str() );
102 high_tbl = new SGInterpTable( high.str() );
105 for ( i = 0; i < node->nChildren(); ++i ) {
106 SGPropertyNode *child = node->getChild(i);
107 string cname = child->getName();
108 string cval = child->getStringValue();
109 if ( cname == "name" ) {
111 } else if ( cname == "number" ) {
112 num = child->getIntValue();
114 SG_LOG( SG_INSTR, SG_WARN,
115 "Error in nav radio config logic" );
116 if ( name.length() ) {
117 SG_LOG( SG_INSTR, SG_WARN, "Section = " << name );
126 FGNavRadio::~FGNavRadio()
140 branch = "/instrumentation/" + name;
142 SGPropertyNode *node = fgGetNode(branch.c_str(), num, true );
145 fgGetNode(("/systems/electrical/outputs/" + name).c_str(), true);
148 power_btn = node->getChild("power-btn", 0, true);
149 power_btn->setBoolValue( true );
150 nav_vol_btn = node->getChild("volume", 0, true);
151 nav_ident_btn = node->getChild("ident", 0, true);
152 nav_ident_btn->setBoolValue( true );
153 audio_btn = node->getChild("audio-btn", 0, true);
154 audio_btn->setBoolValue( true );
157 SGPropertyNode *subnode = node->getChild("frequencies", 0, true);
158 nav_freq = subnode->getChild("selected-mhz", 0, true);
159 nav_alt_freq = subnode->getChild("standby-mhz", 0, true);
160 fmt_freq = subnode->getChild("selected-mhz-fmt", 0, true);
161 fmt_alt_freq = subnode->getChild("standby-mhz-fmt", 0, true);
164 subnode = node->getChild("radials", 0, true);
165 nav_sel_radial = subnode->getChild("selected-deg", 0, true);
166 nav_radial = subnode->getChild("actual-deg", 0, true);
167 reciprocal_radial = subnode->getChild("reciprocal-radial-deg", 0, true);
168 nav_target_radial_true = subnode->getChild("target-radial-deg", 0, true);
169 nav_target_auto_hdg = subnode->getChild("target-auto-hdg-deg", 0, true);
172 nav_heading = node->getChild("heading-deg", 0, true);
173 nav_to_flag = node->getChild("to-flag", 0, true);
174 nav_from_flag = node->getChild("from-flag", 0, true);
175 nav_inrange = node->getChild("in-range", 0, true);
176 nav_cdi_deflection = node->getChild("heading-needle-deflection", 0, true);
177 nav_cdi_xtrack_error = node->getChild("crosstrack-error-m", 0, true);
178 nav_has_gs = node->getChild("has-gs", 0, true);
179 nav_loc = node->getChild("nav-loc", 0, true);
180 nav_loc_dist = node->getChild("nav-distance", 0, true);
181 nav_gs_deflection = node->getChild("gs-needle-deflection", 0, true);
182 nav_gs_rate_of_climb = node->getChild("gs-rate-of-climb", 0, true);
183 nav_gs_dist = node->getChild("gs-distance", 0, true);
184 nav_id = node->getChild("nav-id", 0, true);
185 nav_id_c1 = node->getChild("nav-id_asc1", 0, true);
186 nav_id_c2 = node->getChild("nav-id_asc2", 0, true);
187 nav_id_c3 = node->getChild("nav-id_asc3", 0, true);
188 nav_id_c4 = node->getChild("nav-id_asc4", 0, true);
190 nav_serviceable = node->getChild("serviceable", 0, true);
191 cdi_serviceable = (node->getChild("cdi", 0, true))
192 ->getChild("serviceable", 0, true);
193 gs_serviceable = (node->getChild("gs", 0, true))
194 ->getChild("serviceable");
195 tofrom_serviceable = (node->getChild("to-from", 0, true))
196 ->getChild("serviceable", 0, true);
197 nav_slaved_to_gps = node->getChild("slaved-to-gps", 0, true);
199 gps_cdi_deflection = fgGetNode("/instrumentation/gps/cdi-deflection", true);
200 gps_to_flag = fgGetNode("/instrumentation/gps/to-flag", true);
201 gps_from_flag = fgGetNode("/instrumentation/gps/from-flag", true);
203 std::ostringstream temp;
204 temp << name << "nav-ident" << num;
205 nav_fx_name = temp.str();
206 temp << name << "dme-ident" << num;
207 dme_fx_name = temp.str();
213 std::ostringstream temp;
216 branch = "/instrumentation/" + name + "[" + temp.str() + "]";
221 FGNavRadio::unbind ()
223 std::ostringstream temp;
226 branch = "/instrumentation/" + name + "[" + temp.str() + "]";
230 // model standard VOR/DME/TACAN service volumes as per AIM 1-1-8
231 double FGNavRadio::adjustNavRange( double stationElev, double aircraftElev,
232 double nominalRange )
234 // extend out actual usable range to be 1.3x the published safe range
235 const double usability_factor = 1.3;
237 // assumptions we model the standard service volume, plus
238 // ... rather than specifying a cylinder, we model a cone that
239 // contains the cylinder. Then we put an upside down cone on top
240 // to model diminishing returns at too-high altitudes.
242 // altitude difference
243 double alt = ( aircraftElev * SG_METER_TO_FEET - stationElev );
244 // cout << "aircraft elev = " << aircraftElev * SG_METER_TO_FEET
245 // << " station elev = " << stationElev << endl;
247 if ( nominalRange < 25.0 + SG_EPSILON ) {
248 // Standard Terminal Service Volume
249 return term_tbl->interpolate( alt ) * usability_factor;
250 } else if ( nominalRange < 50.0 + SG_EPSILON ) {
251 // Standard Low Altitude Service Volume
252 // table is based on range of 40, scale to actual range
253 return low_tbl->interpolate( alt ) * nominalRange / 40.0
256 // Standard High Altitude Service Volume
257 // table is based on range of 130, scale to actual range
258 return high_tbl->interpolate( alt ) * nominalRange / 130.0
264 // model standard ILS service volumes as per AIM 1-1-9
265 double FGNavRadio::adjustILSRange( double stationElev, double aircraftElev,
266 double offsetDegrees, double distance )
268 // assumptions we model the standard service volume, plus
270 // altitude difference
271 // double alt = ( aircraftElev * SG_METER_TO_FEET - stationElev );
272 // double offset = fabs( offsetDegrees );
274 // if ( offset < 10 ) {
275 // return FG_ILS_DEFAULT_RANGE;
276 // } else if ( offset < 35 ) {
277 // return 10 + (35 - offset) * (FG_ILS_DEFAULT_RANGE - 10) / 25;
278 // } else if ( offset < 45 ) {
279 // return (45 - offset);
280 // } else if ( offset > 170 ) {
281 // return FG_ILS_DEFAULT_RANGE;
282 // } else if ( offset > 145 ) {
283 // return 10 + (offset - 145) * (FG_ILS_DEFAULT_RANGE - 10) / 25;
284 // } else if ( offset > 135 ) {
285 // return (offset - 135);
289 return FG_LOC_DEFAULT_RANGE;
293 // Update the various nav values based on position and valid tuned in navs
295 FGNavRadio::update(double dt)
297 double lon = lon_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
298 double lat = lat_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
299 double elev = alt_node->getDoubleValue() * SG_FEET_TO_METER;
301 // SGPropertyNode *node = fgGetNode(branch.c_str(), num, true );
303 Point3D aircraft = sgGeodToCart( Point3D( lon, lat, elev ) );
307 // Create "formatted" versions of the nav frequencies for
308 // consistant display output.
310 sprintf( tmp, "%.2f", nav_freq->getDoubleValue() );
311 fmt_freq->setStringValue(tmp);
312 sprintf( tmp, "%.2f", nav_alt_freq->getDoubleValue() );
313 fmt_alt_freq->setStringValue(tmp);
315 // On timeout, scan again
316 _time_before_search_sec -= dt;
317 if ( _time_before_search_sec < 0 ) {
321 ////////////////////////////////////////////////////////////////////////
323 ////////////////////////////////////////////////////////////////////////
325 // cout << "nav_valid = " << nav_valid
326 // << " power_btn = " << power_btn->getBoolValue()
327 // << " bus_power = " << bus_power->getDoubleValue()
328 // << " nav_serviceable = " << nav_serviceable->getBoolValue()
331 if ( nav_valid && power_btn->getBoolValue()
332 && (bus_power->getDoubleValue() > 1.0)
333 && nav_serviceable->getBoolValue() )
335 station = Point3D( nav_x, nav_y, nav_z );
336 nav_loc_dist->setDoubleValue( aircraft.distance3D( station ) );
337 // cout << "station = " << station << " dist = "
338 // << nav_loc_dist->getDoubleValue() << endl;
340 if ( nav_has_gs->getBoolValue() ) {
341 // find closest distance to the gs base line
343 sgdSetVec3( p, aircraft.x(), aircraft.y(), aircraft.z() );
345 sgdSetVec3( p0, nav_gs_x, nav_gs_y, nav_gs_z );
346 double dist = sgdClosestPointToLineDistSquared( p, p0,
348 nav_gs_dist->setDoubleValue( sqrt( dist ) );
349 // cout << "nav_gs_dist = " << nav_gs_dist->getDoubleValue()
352 Point3D tmp( nav_gs_x, nav_gs_y, nav_gs_z );
353 // cout << " (" << aircraft.distance3D( tmp ) << ")" << endl;
355 // wgs84 heading to glide slope (to determine sign of distance)
356 geo_inverse_wgs_84( elev,
357 lat * SGD_RADIANS_TO_DEGREES,
358 lon * SGD_RADIANS_TO_DEGREES,
359 nav_gslat, nav_gslon,
361 double r = az1 - nav_target_radial;
362 while ( r > 180.0 ) { r -= 360.0;}
363 while ( r < -180.0 ) { r += 360.0;}
364 if ( r >= -90.0 && r <= 90.0 ) {
365 nav_gs_dist_signed = nav_gs_dist->getDoubleValue();
367 nav_gs_dist_signed = -nav_gs_dist->getDoubleValue();
369 /* cout << "Target Radial = " << nav_target_radial
370 << " Bearing = " << az1
371 << " dist (signed) = " << nav_gs_dist_signed
375 nav_gs_dist->setDoubleValue( 0.0 );
378 // wgs84 heading to localizer
380 geo_inverse_wgs_84( elev,
381 lat * SGD_RADIANS_TO_DEGREES,
382 lon * SGD_RADIANS_TO_DEGREES,
383 nav_loclat, nav_loclon,
385 // cout << "az1 = " << az1 << " magvar = " << nav_magvar << endl;
386 nav_heading->setDoubleValue( hdg );
387 double radial = az2 - nav_twist;
388 double recip = radial + 180.0;
389 if ( recip >= 360.0 ) { recip -= 360.0; }
390 nav_radial->setDoubleValue( radial );
391 reciprocal_radial->setDoubleValue( recip );
392 // cout << " heading = " << nav_heading
393 // << " dist = " << nav_dist << endl;
395 if ( nav_loc->getBoolValue() ) {
396 double offset = radial - nav_target_radial;
397 while ( offset < -180.0 ) { offset += 360.0; }
398 while ( offset > 180.0 ) { offset -= 360.0; }
399 // cout << "ils offset = " << offset << endl;
401 = adjustILSRange( nav_elev, elev, offset,
402 nav_loc_dist->getDoubleValue()
405 nav_effective_range = adjustNavRange( nav_elev, elev, nav_range );
407 // cout << "nav range = " << nav_effective_range
408 // << " (" << nav_range << ")" << endl;
410 if ( nav_loc_dist->getDoubleValue()
411 < nav_effective_range * SG_NM_TO_METER )
413 nav_inrange->setBoolValue( true );
414 } else if ( nav_loc_dist->getDoubleValue()
415 < 2 * nav_effective_range * SG_NM_TO_METER )
417 nav_inrange->setBoolValue( sg_random() <
418 ( 2 * nav_effective_range * SG_NM_TO_METER
419 - nav_loc_dist->getDoubleValue() ) /
420 (nav_effective_range * SG_NM_TO_METER) );
422 nav_inrange->setBoolValue( false );
425 if ( !nav_loc->getBoolValue() ) {
426 nav_target_radial = nav_sel_radial->getDoubleValue();
429 // Calculate some values for the nav/ils hold autopilot
431 double cur_radial = recip;
432 if ( nav_loc->getBoolValue() ) {
433 // ILS localizers radials are already "true" in our
436 cur_radial += nav_twist;
438 if ( nav_from_flag->getBoolValue() ) {
440 while ( cur_radial >= 360.0 ) { cur_radial -= 360.0; }
443 // AUTOPILOT/FLIGHT-DIRECTOR HELPERS
445 // determine the target radial in "true" heading
447 if ( nav_loc->getBoolValue() ) {
448 // ILS localizers radials are already "true" in our
450 trtrue = nav_target_radial;
452 // VOR radials need to have that vor's offset added in
453 trtrue = nav_target_radial + nav_twist;
456 while ( trtrue < 0.0 ) { trtrue += 360.0; }
457 while ( trtrue > 360.0 ) { trtrue -= 360.0; }
458 nav_target_radial_true->setDoubleValue( trtrue );
460 // determine the heading adjustment needed.
461 // over 8km scale by 3.0
462 // (3 is chosen because max deflection is 10
463 // and 30 is clamped angle to radial)
464 // under 8km scale by 10.0
465 // because the overstated error helps drive it to the radial in a
466 // moderate cross wind.
467 double adjustment = 0.0;
468 if (nav_loc_dist->getDoubleValue() > 8000) {
469 adjustment = nav_cdi_deflection->getDoubleValue() * 3.0;
471 adjustment = nav_cdi_deflection->getDoubleValue() * 10.0;
473 SG_CLAMP_RANGE( adjustment, -30.0, 30.0 );
475 // determine the target heading to fly to intercept the
477 double nta_hdg = trtrue + adjustment;
478 while ( nta_hdg < 0.0 ) { nta_hdg += 360.0; }
479 while ( nta_hdg > 360.0 ) { nta_hdg -= 360.0; }
480 nav_target_auto_hdg->setDoubleValue( nta_hdg );
485 // Calculate desired rate of climb for intercepting the GS
486 double x = nav_gs_dist->getDoubleValue();
487 double y = (alt_node->getDoubleValue() - nav_elev)
489 double current_angle = atan2( y, x ) * SGD_RADIANS_TO_DEGREES;
491 double target_angle = nav_target_gs;
492 double gs_diff = target_angle - current_angle;
494 // convert desired vertical path angle into a climb rate
495 double des_angle = current_angle - 10 * gs_diff;
497 // estimate horizontal speed towards ILS in meters per minute
498 double dist = last_x - x;
502 double new_vel = ( dist / dt );
504 horiz_vel = 0.75 * horiz_vel + 0.25 * new_vel;
505 // double horiz_vel = cur_fdm_state->get_V_ground_speed()
506 // * SG_FEET_TO_METER * 60.0;
507 // double horiz_vel = airspeed_node->getFloatValue()
508 // * SG_FEET_TO_METER * 60.0;
511 ->setDoubleValue( -sin( des_angle * SGD_DEGREES_TO_RADIANS )
512 * horiz_vel * SG_METER_TO_FEET );
515 nav_inrange->setBoolValue( false );
516 // cout << "not picking up vor. :-(" << endl;
519 // compute to/from flag status
520 double value = false;
521 double offset = fabs(nav_radial->getDoubleValue() - nav_target_radial);
522 if ( nav_slaved_to_gps->getBoolValue() ) {
523 value = gps_to_flag->getBoolValue();
524 } else if ( nav_inrange->getBoolValue()
525 && nav_serviceable->getBoolValue()
526 && tofrom_serviceable->getBoolValue() )
528 if ( nav_loc->getBoolValue() ) {
531 value = !(offset <= 90.0 || offset >= 270.0);
534 nav_to_flag->setBoolValue( value );
537 if ( nav_slaved_to_gps->getBoolValue() ) {
538 value = gps_from_flag->getBoolValue();
539 } else if ( nav_inrange->getBoolValue()
540 && nav_serviceable->getBoolValue()
541 && tofrom_serviceable->getBoolValue() )
543 if ( nav_loc->getBoolValue() ) {
546 value = !(offset > 90.0 && offset < 270.0);
549 nav_from_flag->setBoolValue( value );
551 // compute the deflection of the CDI needle, clamped to the range
554 if ( nav_slaved_to_gps->getBoolValue() ) {
555 r = gps_cdi_deflection->getDoubleValue();
556 // We want +- 5 dots deflection for the gps, so clamp to -12.5/12.5
557 if ( r < -12.5 ) { r = -12.5; }
558 if ( r > 12.5 ) { r = 12.5; }
559 } else if ( nav_inrange->getBoolValue() && nav_serviceable->getBoolValue()
560 && cdi_serviceable->getBoolValue()
561 && !nav_slaved_to_gps->getBoolValue() )
563 r = nav_radial->getDoubleValue() - nav_target_radial;
564 // cout << "Target radial = " << nav_target_radial
565 // << " Actual radial = " << nav_radial->getDoubleValue() << endl;
567 while ( r > 180.0 ) { r -= 360.0;}
568 while ( r < -180.0 ) { r += 360.0;}
569 if ( fabs(r) > 90.0 ) {
570 r = ( r<0.0 ? -r-180.0 : -r+180.0 );
573 // According to Robin Peel, the ILS is 4x more sensitive than a vor
574 r = -r; // reverse, since radial is outbound
575 if ( nav_loc->getBoolValue() ) { r *= 4.0; }
576 if ( r < -10.0 ) { r = -10.0; }
577 if ( r > 10.0 ) { r = 10.0; }
581 nav_cdi_deflection->setDoubleValue( r );
583 // compute the amount of cross track distance error in meters
585 if ( nav_inrange->getBoolValue()
586 && nav_serviceable->getBoolValue() && cdi_serviceable->getBoolValue() )
588 r = nav_radial->getDoubleValue() - nav_target_radial;
589 // cout << "Target radial = " << nav_target_radial
590 // << " Actual radial = " << nav_radial->getDoubleValue()
591 // << " r = " << r << endl;
593 while ( r > 180.0 ) { r -= 360.0;}
594 while ( r < -180.0 ) { r += 360.0;}
595 if ( fabs(r) > 90.0 ) {
596 r = ( r<0.0 ? -r-180.0 : -r+180.0 );
599 r = -r; // reverse, since radial is outbound
601 m = nav_loc_dist->getDoubleValue() * sin(r * SGD_DEGREES_TO_RADIANS);
606 nav_cdi_xtrack_error->setDoubleValue( m );
608 // compute the amount of glide slope needle deflection (.i.e. the
609 // number of degrees we are off the glide slope * 5.0
610 if ( nav_inrange->getBoolValue() && nav_has_gs->getBoolValue()
611 && nav_serviceable->getBoolValue()
612 && gs_serviceable->getBoolValue()
613 && !nav_slaved_to_gps->getBoolValue() )
615 double x = nav_gs_dist->getDoubleValue();
616 double y = (fgGetDouble("/position/altitude-ft") - nav_elev)
618 // cout << "dist = " << x << " height = " << y << endl;
619 double angle = asin( y / x ) * SGD_RADIANS_TO_DEGREES;
620 r = (nav_target_gs - angle) * 5.0;
624 nav_gs_deflection->setDoubleValue( r );
628 && nav_inrange->getBoolValue()
629 && nav_serviceable->getBoolValue() )
631 // play station ident via audio system if on + ident,
632 // otherwise turn it off
633 if ( power_btn->getBoolValue() && (bus_power->getDoubleValue() > 1.0)
634 && nav_ident_btn->getBoolValue() && audio_btn->getBoolValue() )
636 SGSoundSample *sound;
637 sound = globals->get_soundmgr()->find( nav_fx_name );
638 double vol = nav_vol_btn->getDoubleValue();
639 if ( vol < 0.0 ) { vol = 0.0; }
640 if ( vol > 1.0 ) { vol = 1.0; }
641 if ( sound != NULL ) {
642 sound->set_volume( vol );
644 SG_LOG( SG_COCKPIT, SG_ALERT,
645 "Can't find nav-vor-ident sound" );
647 sound = globals->get_soundmgr()->find( dme_fx_name );
648 if ( sound != NULL ) {
649 sound->set_volume( vol );
651 SG_LOG( SG_COCKPIT, SG_ALERT,
652 "Can't find nav-dme-ident sound" );
654 // cout << "nav_last_time = " << nav_last_time << " ";
655 // cout << "cur_time = "
656 // << globals->get_time_params()->get_cur_time();
658 globals->get_time_params()->get_cur_time() - 30 ) {
659 nav_last_time = globals->get_time_params()->get_cur_time();
662 // cout << " nav_play_count = " << nav_play_count << endl;
663 // cout << "playing = "
664 // << globals->get_soundmgr()->is_playing(nav_fx_name)
666 if ( nav_play_count < 4 ) {
668 if ( !globals->get_soundmgr()->is_playing(nav_fx_name) ) {
669 globals->get_soundmgr()->play_once( nav_fx_name );
672 } else if ( nav_play_count < 5 && nav_has_dme ) {
674 if ( !globals->get_soundmgr()->is_playing(nav_fx_name) &&
675 !globals->get_soundmgr()->is_playing(dme_fx_name) ) {
676 globals->get_soundmgr()->play_once( dme_fx_name );
681 globals->get_soundmgr()->stop( nav_fx_name );
682 globals->get_soundmgr()->stop( dme_fx_name );
688 // Update current nav/adf radio stations based on current postition
689 void FGNavRadio::search()
693 _time_before_search_sec = 1.0;
695 double lon = lon_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
696 double lat = lat_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
697 double elev = alt_node->getDoubleValue() * SG_FEET_TO_METER;
699 FGNavRecord *nav = NULL;
700 FGNavRecord *loc = NULL;
701 FGNavRecord *dme = NULL;
702 FGNavRecord *gs = NULL;
704 ////////////////////////////////////////////////////////////////////////
706 ////////////////////////////////////////////////////////////////////////
708 double freq = nav_freq->getDoubleValue();
709 nav = globals->get_navlist()->findByFreq(freq, lon, lat, elev);
710 dme = globals->get_dmelist()->findByFreq(freq, lon, lat, elev);
712 loc = globals->get_loclist()->findByFreq(freq, lon, lat, elev);
713 gs = globals->get_gslist()->findByFreq(freq, lon, lat, elev);
717 nav_id->setStringValue( loc->get_ident() );
718 // cout << "localizer = " << nav_id->getStringValue() << endl;
720 if ( last_nav_id != nav_id->getStringValue() || last_nav_vor ) {
721 nav_trans_ident = loc->get_trans_ident();
722 nav_target_radial = loc->get_multiuse();
723 while ( nav_target_radial < 0.0 ) { nav_target_radial += 360.0; }
724 while ( nav_target_radial > 360.0 ) { nav_target_radial -= 360.0; }
725 nav_loclon = loc->get_lon();
726 nav_loclat = loc->get_lat();
727 nav_x = loc->get_x();
728 nav_y = loc->get_y();
729 nav_z = loc->get_z();
730 last_nav_id = nav_id->getStringValue();
731 last_nav_vor = false;
732 nav_loc->setBoolValue( true );
733 nav_has_dme = (dme != NULL);
734 nav_has_gs->setBoolValue( gs != NULL );
735 if ( nav_has_gs->getBoolValue() ) {
736 nav_gslon = gs->get_lon();
737 nav_gslat = gs->get_lat();
738 nav_elev = gs->get_elev_ft();
739 int tmp = (int)(gs->get_multiuse() / 1000.0);
740 nav_target_gs = (double)tmp / 100.0;
741 nav_gs_x = gs->get_x();
742 nav_gs_y = gs->get_y();
743 nav_gs_z = gs->get_z();
745 // derive GS baseline (perpendicular to the runay
747 double tlon, tlat, taz;
748 geo_direct_wgs_84 ( 0.0, nav_gslat, nav_gslon,
749 nav_target_radial + 90,
750 100.0, &tlat, &tlon, &taz );
751 // cout << "nav_target_radial = " << nav_target_radial << endl;
752 // cout << "nav_loc = " << nav_loc->getBoolValue() << endl;
753 // cout << nav_gslon << "," << nav_gslat << " "
754 // << tlon << "," << tlat << " (" << nav_elev << ")"
756 Point3D p1 = sgGeodToCart( Point3D(tlon*SGD_DEGREES_TO_RADIANS,
757 tlat*SGD_DEGREES_TO_RADIANS,
758 nav_elev*SG_FEET_TO_METER)
760 // cout << nav_gs_x << "," << nav_gs_y << "," << nav_gs_z
762 // cout << p1 << endl;
763 sgdSetVec3( gs_base_vec,
764 p1.x()-nav_gs_x, p1.y()-nav_gs_y, p1.z()-nav_gs_z );
765 // cout << gs_base_vec[0] << "," << gs_base_vec[1] << ","
766 // << gs_base_vec[2] << endl;
768 nav_elev = loc->get_elev_ft();
771 nav_range = FG_LOC_DEFAULT_RANGE;
772 nav_effective_range = nav_range;
774 if ( globals->get_soundmgr()->exists( nav_fx_name ) ) {
775 globals->get_soundmgr()->remove( nav_fx_name );
777 SGSoundSample *sound;
778 sound = morse.make_ident( nav_trans_ident, LO_FREQUENCY );
779 sound->set_volume( 0.3 );
780 globals->get_soundmgr()->add( sound, nav_fx_name );
782 if ( globals->get_soundmgr()->exists( dme_fx_name ) ) {
783 globals->get_soundmgr()->remove( dme_fx_name );
785 sound = morse.make_ident( nav_trans_ident, HI_FREQUENCY );
786 sound->set_volume( 0.3 );
787 globals->get_soundmgr()->add( sound, dme_fx_name );
789 int offset = (int)(sg_random() * 30.0);
790 nav_play_count = offset / 4;
791 nav_last_time = globals->get_time_params()->get_cur_time() -
793 // cout << "offset = " << offset << " play_count = "
795 // << " nav_last_time = " << nav_last_time
796 // << " current time = "
797 // << globals->get_time_params()->get_cur_time() << endl;
799 // cout << "Found an loc station in range" << endl;
800 // cout << " id = " << loc->get_locident() << endl;
802 } else if ( nav != NULL ) {
803 nav_id->setStringValue( nav->get_ident() );
804 // cout << "nav = " << nav_id->getStringValue() << endl;
806 if ( last_nav_id != nav_id->getStringValue() || !last_nav_vor ) {
807 last_nav_id = nav_id->getStringValue();
809 nav_trans_ident = nav->get_trans_ident();
810 nav_loc->setBoolValue( false );
811 nav_has_dme = (dme != NULL);
812 nav_has_gs->setBoolValue( false );
813 nav_loclon = nav->get_lon();
814 nav_loclat = nav->get_lat();
815 nav_elev = nav->get_elev_ft();
816 nav_twist = nav->get_multiuse();
817 nav_range = nav->get_range();
818 nav_effective_range = adjustNavRange(nav_elev, elev, nav_range);
820 nav_target_radial = nav_sel_radial->getDoubleValue();
821 nav_x = nav->get_x();
822 nav_y = nav->get_y();
823 nav_z = nav->get_z();
825 if ( globals->get_soundmgr()->exists( nav_fx_name ) ) {
826 globals->get_soundmgr()->remove( nav_fx_name );
828 SGSoundSample *sound;
829 sound = morse.make_ident( nav_trans_ident, LO_FREQUENCY );
830 sound->set_volume( 0.3 );
831 if ( globals->get_soundmgr()->add( sound, nav_fx_name ) ) {
832 // cout << "Added nav-vor-ident sound" << endl;
834 SG_LOG(SG_COCKPIT, SG_WARN, "Failed to add v1-vor-ident sound");
837 if ( globals->get_soundmgr()->exists( dme_fx_name ) ) {
838 globals->get_soundmgr()->remove( dme_fx_name );
840 sound = morse.make_ident( nav_trans_ident, HI_FREQUENCY );
841 sound->set_volume( 0.3 );
842 globals->get_soundmgr()->add( sound, dme_fx_name );
844 int offset = (int)(sg_random() * 30.0);
845 nav_play_count = offset / 4;
846 nav_last_time = globals->get_time_params()->get_cur_time() -
848 // cout << "offset = " << offset << " play_count = "
849 // << nav_play_count << " nav_last_time = "
850 // << nav_last_time << " current time = "
851 // << globals->get_time_params()->get_cur_time() << endl;
853 // cout << "Found a vor station in range" << endl;
854 // cout << " id = " << nav->get_ident() << endl;
858 nav_id->setStringValue( "" );
859 nav_target_radial = 0;
860 nav_trans_ident = "";
862 if ( ! globals->get_soundmgr()->remove( nav_fx_name ) ) {
863 SG_LOG(SG_COCKPIT, SG_WARN, "Failed to remove nav-vor-ident sound");
865 globals->get_soundmgr()->remove( dme_fx_name );
866 // cout << "not picking up vor1. :-(" << endl;
870 strncpy( tmpid, nav_id->getStringValue(), 5 );
871 nav_id_c1->setIntValue( (int)tmpid[0] );
872 nav_id_c2->setIntValue( (int)tmpid[1] );
873 nav_id_c3->setIntValue( (int)tmpid[2] );
874 nav_id_c4->setIntValue( (int)tmpid[3] );