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)),
55 fmt_alt_freq_node(NULL),
56 sel_radial_node(NULL),
62 recip_radial_node(NULL),
63 target_radial_true_node(NULL),
64 target_auto_hdg_node(NULL),
68 cdi_deflection_node(NULL),
69 cdi_xtrack_error_node(NULL),
73 gs_deflection_node(NULL),
74 gs_rate_of_climb_node(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 = node->getChild("power-btn", 0, true);
149 power_btn_node->setBoolValue( true );
150 vol_btn_node = node->getChild("volume", 0, true);
151 ident_btn_node = node->getChild("ident", 0, true);
152 ident_btn_node->setBoolValue( true );
153 audio_btn_node = node->getChild("audio-btn", 0, true);
154 audio_btn_node->setBoolValue( true );
157 SGPropertyNode *subnode = node->getChild("frequencies", 0, true);
158 freq_node = subnode->getChild("selected-mhz", 0, true);
159 alt_freq_node = subnode->getChild("standby-mhz", 0, true);
160 fmt_freq_node = subnode->getChild("selected-mhz-fmt", 0, true);
161 fmt_alt_freq_node = subnode->getChild("standby-mhz-fmt", 0, true);
164 subnode = node->getChild("radials", 0, true);
165 sel_radial_node = subnode->getChild("selected-deg", 0, true);
166 radial_node = subnode->getChild("actual-deg", 0, true);
167 recip_radial_node = subnode->getChild("reciprocal-radial-deg", 0, true);
168 target_radial_true_node = subnode->getChild("target-radial-deg", 0, true);
169 target_auto_hdg_node = subnode->getChild("target-auto-hdg-deg", 0, true);
172 heading_node = node->getChild("heading-deg", 0, true);
173 to_flag_node = node->getChild("to-flag", 0, true);
174 from_flag_node = node->getChild("from-flag", 0, true);
175 inrange_node = node->getChild("in-range", 0, true);
176 cdi_deflection_node = node->getChild("heading-needle-deflection", 0, true);
177 cdi_xtrack_error_node = node->getChild("crosstrack-error-m", 0, true);
178 has_gs_node = node->getChild("has-gs", 0, true);
179 loc_node = node->getChild("nav-loc", 0, true);
180 loc_dist_node = node->getChild("nav-distance", 0, true);
181 gs_deflection_node = node->getChild("gs-needle-deflection", 0, true);
182 gs_rate_of_climb_node = node->getChild("gs-rate-of-climb", 0, true);
183 gs_dist_node = node->getChild("gs-distance", 0, true);
184 id_node = node->getChild("nav-id", 0, true);
185 id_c1_node = node->getChild("nav-id_asc1", 0, true);
186 id_c2_node = node->getChild("nav-id_asc2", 0, true);
187 id_c3_node = node->getChild("nav-id_asc3", 0, true);
188 id_c4_node = node->getChild("nav-id_asc4", 0, true);
190 nav_serviceable_node = node->getChild("serviceable", 0, true);
191 cdi_serviceable_node = (node->getChild("cdi", 0, true))
192 ->getChild("serviceable", 0, true);
193 gs_serviceable_node = (node->getChild("gs", 0, true))
194 ->getChild("serviceable");
195 tofrom_serviceable_node = (node->getChild("to-from", 0, true))
196 ->getChild("serviceable", 0, true);
197 nav_slaved_to_gps_node = node->getChild("slaved-to-gps", 0, true);
199 gps_cdi_deflection_node = fgGetNode("/instrumentation/gps/cdi-deflection", true);
200 gps_to_flag_node = fgGetNode("/instrumentation/gps/to-flag", true);
201 gps_from_flag_node = 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", freq_node->getDoubleValue() );
311 fmt_freq_node->setStringValue(tmp);
312 sprintf( tmp, "%.2f", alt_freq_node->getDoubleValue() );
313 fmt_alt_freq_node->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_node->getBoolValue()
327 // << " bus_power = " << bus_power_node->getDoubleValue()
328 // << " nav_serviceable = " << nav_serviceable->getBoolValue()
331 if ( nav_valid && power_btn_node->getBoolValue()
332 && (bus_power_node->getDoubleValue() > 1.0)
333 && nav_serviceable_node->getBoolValue() )
335 station = Point3D( nav_x, nav_y, nav_z );
336 loc_dist_node->setDoubleValue( aircraft.distance3D( station ) );
337 // cout << "station = " << station << " dist = "
338 // << loc_dist_node->getDoubleValue() << endl;
340 if ( has_gs_node->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 gs_dist_node->setDoubleValue( sqrt( dist ) );
349 // cout << "nav_gs_dist = " << gs_dist_node->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 = gs_dist_node->getDoubleValue();
367 nav_gs_dist_signed = -gs_dist_node->getDoubleValue();
369 /* cout << "Target Radial = " << nav_target_radial
370 << " Bearing = " << az1
371 << " dist (signed) = " << nav_gs_dist_signed
375 gs_dist_node->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 heading_node->setDoubleValue( hdg );
387 double radial = az2 - nav_twist;
388 double recip = radial + 180.0;
389 if ( recip >= 360.0 ) { recip -= 360.0; }
390 radial_node->setDoubleValue( radial );
391 recip_radial_node->setDoubleValue( recip );
392 // cout << " heading = " << heading_node->getDoubleValue()
393 // << " dist = " << nav_dist << endl;
395 if ( loc_node->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 loc_dist_node->getDoubleValue()
405 nav_effective_range = adjustNavRange( nav_elev, elev, nav_range );
407 // cout << "nav range = " << nav_effective_range
408 // << " (" << nav_range << ")" << endl;
410 if ( loc_dist_node->getDoubleValue()
411 < nav_effective_range * SG_NM_TO_METER )
413 inrange_node->setBoolValue( true );
414 } else if ( loc_dist_node->getDoubleValue()
415 < 2 * nav_effective_range * SG_NM_TO_METER )
417 inrange_node->setBoolValue( sg_random() <
418 ( 2 * nav_effective_range * SG_NM_TO_METER
419 - loc_dist_node->getDoubleValue() ) /
420 (nav_effective_range * SG_NM_TO_METER) );
422 inrange_node->setBoolValue( false );
425 if ( !loc_node->getBoolValue() ) {
426 nav_target_radial = sel_radial_node->getDoubleValue();
429 // Calculate some values for the nav/ils hold autopilot
431 double cur_radial = recip;
432 if ( loc_node->getBoolValue() ) {
433 // ILS localizers radials are already "true" in our
436 cur_radial += nav_twist;
438 if ( from_flag_node->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 ( loc_node->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 target_radial_true_node->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 (loc_dist_node->getDoubleValue() > 8000) {
469 adjustment = cdi_deflection_node->getDoubleValue() * 3.0;
471 adjustment = cdi_deflection_node->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 target_auto_hdg_node->setDoubleValue( nta_hdg );
485 // Calculate desired rate of climb for intercepting the GS
486 double x = gs_dist_node->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;
510 gs_rate_of_climb_node
511 ->setDoubleValue( -sin( des_angle * SGD_DEGREES_TO_RADIANS )
512 * horiz_vel * SG_METER_TO_FEET );
515 inrange_node->setBoolValue( false );
516 // cout << "not picking up vor. :-(" << endl;
519 // compute to/from flag status
520 double value = false;
521 double offset = fabs(radial_node->getDoubleValue() - nav_target_radial);
522 if ( nav_slaved_to_gps_node->getBoolValue() ) {
523 value = gps_to_flag_node->getBoolValue();
524 } else if ( inrange_node->getBoolValue()
525 && nav_serviceable_node->getBoolValue()
526 && tofrom_serviceable_node->getBoolValue() )
528 if ( loc_node->getBoolValue() ) {
531 value = !(offset <= 90.0 || offset >= 270.0);
534 to_flag_node->setBoolValue( value );
537 if ( nav_slaved_to_gps_node->getBoolValue() ) {
538 value = gps_from_flag_node->getBoolValue();
539 } else if ( inrange_node->getBoolValue()
540 && nav_serviceable_node->getBoolValue()
541 && tofrom_serviceable_node->getBoolValue() )
543 if ( loc_node->getBoolValue() ) {
546 value = !(offset > 90.0 && offset < 270.0);
549 from_flag_node->setBoolValue( value );
551 // compute the deflection of the CDI needle, clamped to the range
554 if ( nav_slaved_to_gps_node->getBoolValue() ) {
555 r = gps_cdi_deflection_node->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 ( inrange_node->getBoolValue()
560 && nav_serviceable_node->getBoolValue()
561 && cdi_serviceable_node->getBoolValue() )
563 r = radial_node->getDoubleValue() - nav_target_radial;
564 // cout << "Target radial = " << nav_target_radial
565 // << " Actual radial = " << radial_node->getDoubleValue()
568 while ( r > 180.0 ) { r -= 360.0;}
569 while ( r < -180.0 ) { r += 360.0;}
570 if ( fabs(r) > 90.0 ) {
571 r = ( r<0.0 ? -r-180.0 : -r+180.0 );
574 // According to Robin Peel, the ILS is 4x more sensitive than a vor
575 r = -r; // reverse, since radial is outbound
576 if ( loc_node->getBoolValue() ) { r *= 4.0; }
577 if ( r < -10.0 ) { r = -10.0; }
578 if ( r > 10.0 ) { r = 10.0; }
582 cdi_deflection_node->setDoubleValue( r );
584 // compute the amount of cross track distance error in meters
586 if ( inrange_node->getBoolValue()
587 && nav_serviceable_node->getBoolValue()
588 && cdi_serviceable_node->getBoolValue() )
590 r = radial_node->getDoubleValue() - nav_target_radial;
591 // cout << "Target radial = " << nav_target_radial
592 // << " Actual radial = " << radial_node->getDoubleValue()
593 // << " r = " << r << endl;
595 while ( r > 180.0 ) { r -= 360.0;}
596 while ( r < -180.0 ) { r += 360.0;}
597 if ( fabs(r) > 90.0 ) {
598 r = ( r<0.0 ? -r-180.0 : -r+180.0 );
601 r = -r; // reverse, since radial is outbound
603 m = loc_dist_node->getDoubleValue() * sin(r * SGD_DEGREES_TO_RADIANS);
608 cdi_xtrack_error_node->setDoubleValue( m );
610 // compute the amount of glide slope needle deflection (.i.e. the
611 // number of degrees we are off the glide slope * 5.0
613 if ( nav_slaved_to_gps_node->getBoolValue() ) {
614 // FIXME, what should be set here?
615 } else if ( inrange_node->getBoolValue() && has_gs_node->getBoolValue()
616 && nav_serviceable_node->getBoolValue()
617 && gs_serviceable_node->getBoolValue() )
619 double x = gs_dist_node->getDoubleValue();
620 double y = (fgGetDouble("/position/altitude-ft") - nav_elev)
622 // cout << "dist = " << x << " height = " << y << endl;
623 double angle = asin( y / x ) * SGD_RADIANS_TO_DEGREES;
624 r = (nav_target_gs - angle) * 5.0;
626 gs_deflection_node->setDoubleValue( r );
630 && inrange_node->getBoolValue()
631 && nav_serviceable_node->getBoolValue() )
633 // play station ident via audio system if on + ident,
634 // otherwise turn it off
635 if ( power_btn_node->getBoolValue()
636 && (bus_power_node->getDoubleValue() > 1.0)
637 && ident_btn_node->getBoolValue()
638 && audio_btn_node->getBoolValue() )
640 SGSoundSample *sound;
641 sound = globals->get_soundmgr()->find( nav_fx_name );
642 double vol = vol_btn_node->getDoubleValue();
643 if ( vol < 0.0 ) { vol = 0.0; }
644 if ( vol > 1.0 ) { vol = 1.0; }
645 if ( sound != NULL ) {
646 sound->set_volume( vol );
648 SG_LOG( SG_COCKPIT, SG_ALERT,
649 "Can't find nav-vor-ident sound" );
651 sound = globals->get_soundmgr()->find( dme_fx_name );
652 if ( sound != NULL ) {
653 sound->set_volume( vol );
655 SG_LOG( SG_COCKPIT, SG_ALERT,
656 "Can't find nav-dme-ident sound" );
658 // cout << "nav_last_time = " << nav_last_time << " ";
659 // cout << "cur_time = "
660 // << globals->get_time_params()->get_cur_time();
662 globals->get_time_params()->get_cur_time() - 30 ) {
663 nav_last_time = globals->get_time_params()->get_cur_time();
666 // cout << " nav_play_count = " << nav_play_count << endl;
667 // cout << "playing = "
668 // << globals->get_soundmgr()->is_playing(nav_fx_name)
670 if ( nav_play_count < 4 ) {
672 if ( !globals->get_soundmgr()->is_playing(nav_fx_name) ) {
673 globals->get_soundmgr()->play_once( nav_fx_name );
676 } else if ( nav_play_count < 5 && nav_has_dme ) {
678 if ( !globals->get_soundmgr()->is_playing(nav_fx_name) &&
679 !globals->get_soundmgr()->is_playing(dme_fx_name) ) {
680 globals->get_soundmgr()->play_once( dme_fx_name );
685 globals->get_soundmgr()->stop( nav_fx_name );
686 globals->get_soundmgr()->stop( dme_fx_name );
692 // Update current nav/adf radio stations based on current postition
693 void FGNavRadio::search()
697 _time_before_search_sec = 1.0;
699 double lon = lon_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
700 double lat = lat_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
701 double elev = alt_node->getDoubleValue() * SG_FEET_TO_METER;
703 FGNavRecord *nav = NULL;
704 FGNavRecord *loc = NULL;
705 FGNavRecord *dme = NULL;
706 FGNavRecord *gs = NULL;
708 ////////////////////////////////////////////////////////////////////////
710 ////////////////////////////////////////////////////////////////////////
712 double freq = freq_node->getDoubleValue();
713 nav = globals->get_navlist()->findByFreq(freq, lon, lat, elev);
714 dme = globals->get_dmelist()->findByFreq(freq, lon, lat, elev);
716 loc = globals->get_loclist()->findByFreq(freq, lon, lat, elev);
717 gs = globals->get_gslist()->findByFreq(freq, lon, lat, elev);
721 id_node->setStringValue( loc->get_ident() );
722 // cout << "localizer = " << id_node->getStringValue() << endl;
724 if ( last_id != id_node->getStringValue() || last_nav_vor ) {
725 nav_trans_ident = loc->get_trans_ident();
726 nav_target_radial = loc->get_multiuse();
727 while ( nav_target_radial < 0.0 ) { nav_target_radial += 360.0; }
728 while ( nav_target_radial > 360.0 ) { nav_target_radial -= 360.0; }
729 nav_loclon = loc->get_lon();
730 nav_loclat = loc->get_lat();
731 nav_x = loc->get_x();
732 nav_y = loc->get_y();
733 nav_z = loc->get_z();
734 last_id = id_node->getStringValue();
735 last_nav_vor = false;
736 loc_node->setBoolValue( true );
737 nav_has_dme = (dme != NULL);
738 has_gs_node->setBoolValue( gs != NULL );
739 if ( has_gs_node->getBoolValue() ) {
740 nav_gslon = gs->get_lon();
741 nav_gslat = gs->get_lat();
742 nav_elev = gs->get_elev_ft();
743 int tmp = (int)(gs->get_multiuse() / 1000.0);
744 nav_target_gs = (double)tmp / 100.0;
745 nav_gs_x = gs->get_x();
746 nav_gs_y = gs->get_y();
747 nav_gs_z = gs->get_z();
749 // derive GS baseline (perpendicular to the runay
751 double tlon, tlat, taz;
752 geo_direct_wgs_84 ( 0.0, nav_gslat, nav_gslon,
753 nav_target_radial + 90,
754 100.0, &tlat, &tlon, &taz );
755 // cout << "nav_target_radial = " << nav_target_radial << endl;
756 // cout << "nav_loc = " << loc_node->getBoolValue() << endl;
757 // cout << nav_gslon << "," << nav_gslat << " "
758 // << tlon << "," << tlat << " (" << nav_elev << ")"
760 Point3D p1 = sgGeodToCart( Point3D(tlon*SGD_DEGREES_TO_RADIANS,
761 tlat*SGD_DEGREES_TO_RADIANS,
762 nav_elev*SG_FEET_TO_METER)
764 // cout << nav_gs_x << "," << nav_gs_y << "," << nav_gs_z
766 // cout << p1 << endl;
767 sgdSetVec3( gs_base_vec,
768 p1.x()-nav_gs_x, p1.y()-nav_gs_y, p1.z()-nav_gs_z );
769 // cout << gs_base_vec[0] << "," << gs_base_vec[1] << ","
770 // << gs_base_vec[2] << endl;
772 nav_elev = loc->get_elev_ft();
775 nav_range = FG_LOC_DEFAULT_RANGE;
776 nav_effective_range = nav_range;
778 if ( globals->get_soundmgr()->exists( nav_fx_name ) ) {
779 globals->get_soundmgr()->remove( nav_fx_name );
781 SGSoundSample *sound;
782 sound = morse.make_ident( nav_trans_ident, LO_FREQUENCY );
783 sound->set_volume( 0.3 );
784 globals->get_soundmgr()->add( sound, nav_fx_name );
786 if ( globals->get_soundmgr()->exists( dme_fx_name ) ) {
787 globals->get_soundmgr()->remove( dme_fx_name );
789 sound = morse.make_ident( nav_trans_ident, HI_FREQUENCY );
790 sound->set_volume( 0.3 );
791 globals->get_soundmgr()->add( sound, dme_fx_name );
793 int offset = (int)(sg_random() * 30.0);
794 nav_play_count = offset / 4;
795 nav_last_time = globals->get_time_params()->get_cur_time() -
797 // cout << "offset = " << offset << " play_count = "
799 // << " nav_last_time = " << nav_last_time
800 // << " current time = "
801 // << globals->get_time_params()->get_cur_time() << endl;
803 // cout << "Found an loc station in range" << endl;
804 // cout << " id = " << loc->get_locident() << endl;
806 } else if ( nav != NULL ) {
807 id_node->setStringValue( nav->get_ident() );
808 // cout << "nav = " << id_node->getStringValue() << endl;
810 if ( last_id != id_node->getStringValue() || !last_nav_vor ) {
811 last_id = id_node->getStringValue();
813 nav_trans_ident = nav->get_trans_ident();
814 loc_node->setBoolValue( false );
815 nav_has_dme = (dme != NULL);
816 has_gs_node->setBoolValue( false );
817 nav_loclon = nav->get_lon();
818 nav_loclat = nav->get_lat();
819 nav_elev = nav->get_elev_ft();
820 nav_twist = nav->get_multiuse();
821 nav_range = nav->get_range();
822 nav_effective_range = adjustNavRange(nav_elev, elev, nav_range);
824 nav_target_radial = sel_radial_node->getDoubleValue();
825 nav_x = nav->get_x();
826 nav_y = nav->get_y();
827 nav_z = nav->get_z();
829 if ( globals->get_soundmgr()->exists( nav_fx_name ) ) {
830 globals->get_soundmgr()->remove( nav_fx_name );
832 SGSoundSample *sound;
833 sound = morse.make_ident( nav_trans_ident, LO_FREQUENCY );
834 sound->set_volume( 0.3 );
835 if ( globals->get_soundmgr()->add( sound, nav_fx_name ) ) {
836 // cout << "Added nav-vor-ident sound" << endl;
838 SG_LOG(SG_COCKPIT, SG_WARN, "Failed to add v1-vor-ident sound");
841 if ( globals->get_soundmgr()->exists( dme_fx_name ) ) {
842 globals->get_soundmgr()->remove( dme_fx_name );
844 sound = morse.make_ident( nav_trans_ident, HI_FREQUENCY );
845 sound->set_volume( 0.3 );
846 globals->get_soundmgr()->add( sound, dme_fx_name );
848 int offset = (int)(sg_random() * 30.0);
849 nav_play_count = offset / 4;
850 nav_last_time = globals->get_time_params()->get_cur_time() -
852 // cout << "offset = " << offset << " play_count = "
853 // << nav_play_count << " nav_last_time = "
854 // << nav_last_time << " current time = "
855 // << globals->get_time_params()->get_cur_time() << endl;
857 // cout << "Found a vor station in range" << endl;
858 // cout << " id = " << nav->get_ident() << endl;
862 id_node->setStringValue( "" );
863 nav_target_radial = 0;
864 nav_trans_ident = "";
866 if ( ! globals->get_soundmgr()->remove( nav_fx_name ) ) {
867 SG_LOG(SG_COCKPIT, SG_WARN, "Failed to remove nav-vor-ident sound");
869 globals->get_soundmgr()->remove( dme_fx_name );
870 // cout << "not picking up vor1. :-(" << endl;
874 strncpy( tmpid, id_node->getStringValue(), 5 );
875 id_c1_node->setIntValue( (int)tmpid[0] );
876 id_c2_node->setIntValue( (int)tmpid[1] );
877 id_c3_node->setIntValue( (int)tmpid[2] );
878 id_c4_node->setIntValue( (int)tmpid[3] );