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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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)),
56 fmt_alt_freq_node(NULL),
57 sel_radial_node(NULL),
61 nav_serviceable_node(NULL),
62 cdi_serviceable_node(NULL),
63 gs_serviceable_node(NULL),
64 tofrom_serviceable_node(NULL),
67 recip_radial_node(NULL),
68 target_radial_true_node(NULL),
69 target_auto_hdg_node(NULL),
70 time_to_intercept(NULL),
74 cdi_deflection_node(NULL),
75 cdi_xtrack_error_node(NULL),
76 cdi_xtrack_hdg_err_node(NULL),
80 gs_deflection_node(NULL),
81 gs_rate_of_climb_node(NULL),
88 nav_slaved_to_gps_node(NULL),
89 gps_cdi_deflection_node(NULL),
90 gps_to_flag_node(NULL),
91 gps_from_flag_node(NULL),
101 last_xtrack_error(0.0),
104 _time_before_search_sec(-1.0)
106 SGPath path( globals->get_fg_root() );
108 term.append( "Navaids/range.term" );
110 low.append( "Navaids/range.low" );
112 high.append( "Navaids/range.high" );
114 term_tbl = new SGInterpTable( term.str() );
115 low_tbl = new SGInterpTable( low.str() );
116 high_tbl = new SGInterpTable( high.str() );
119 for ( i = 0; i < node->nChildren(); ++i ) {
120 SGPropertyNode *child = node->getChild(i);
121 string cname = child->getName();
122 string cval = child->getStringValue();
123 if ( cname == "name" ) {
125 } else if ( cname == "number" ) {
126 num = child->getIntValue();
128 SG_LOG( SG_INSTR, SG_WARN,
129 "Error in nav radio config logic" );
130 if ( name.length() ) {
131 SG_LOG( SG_INSTR, SG_WARN, "Section = " << name );
140 FGNavRadio::~FGNavRadio()
154 branch = "/instrumentation/" + name;
156 SGPropertyNode *node = fgGetNode(branch.c_str(), num, true );
159 fgGetNode(("/systems/electrical/outputs/" + name).c_str(), true);
162 is_valid_node = node->getChild("data-is-valid", 0, true);
163 power_btn_node = node->getChild("power-btn", 0, true);
164 power_btn_node->setBoolValue( true );
165 vol_btn_node = node->getChild("volume", 0, true);
166 ident_btn_node = node->getChild("ident", 0, true);
167 ident_btn_node->setBoolValue( true );
168 audio_btn_node = node->getChild("audio-btn", 0, true);
169 audio_btn_node->setBoolValue( true );
170 nav_serviceable_node = node->getChild("serviceable", 0, true);
171 cdi_serviceable_node = (node->getChild("cdi", 0, true))
172 ->getChild("serviceable", 0, true);
173 gs_serviceable_node = (node->getChild("gs", 0, true))
174 ->getChild("serviceable");
175 tofrom_serviceable_node = (node->getChild("to-from", 0, true))
176 ->getChild("serviceable", 0, true);
179 SGPropertyNode *subnode = node->getChild("frequencies", 0, true);
180 freq_node = subnode->getChild("selected-mhz", 0, true);
181 alt_freq_node = subnode->getChild("standby-mhz", 0, true);
182 fmt_freq_node = subnode->getChild("selected-mhz-fmt", 0, true);
183 fmt_alt_freq_node = subnode->getChild("standby-mhz-fmt", 0, true);
186 subnode = node->getChild("radials", 0, true);
187 sel_radial_node = subnode->getChild("selected-deg", 0, true);
188 radial_node = subnode->getChild("actual-deg", 0, true);
189 recip_radial_node = subnode->getChild("reciprocal-radial-deg", 0, true);
190 target_radial_true_node = subnode->getChild("target-radial-deg", 0, true);
191 target_auto_hdg_node = subnode->getChild("target-auto-hdg-deg", 0, true);
194 heading_node = node->getChild("heading-deg", 0, true);
195 time_to_intercept = node->getChild("time-to-intercept-sec", 0, true);
196 to_flag_node = node->getChild("to-flag", 0, true);
197 from_flag_node = node->getChild("from-flag", 0, true);
198 inrange_node = node->getChild("in-range", 0, true);
199 cdi_deflection_node = node->getChild("heading-needle-deflection", 0, true);
200 cdi_xtrack_error_node = node->getChild("crosstrack-error-m", 0, true);
201 cdi_xtrack_hdg_err_node
202 = node->getChild("crosstrack-heading-error-deg", 0, true);
203 has_gs_node = node->getChild("has-gs", 0, true);
204 loc_node = node->getChild("nav-loc", 0, true);
205 loc_dist_node = node->getChild("nav-distance", 0, true);
206 gs_deflection_node = node->getChild("gs-needle-deflection", 0, true);
207 gs_rate_of_climb_node = node->getChild("gs-rate-of-climb", 0, true);
208 gs_dist_node = node->getChild("gs-distance", 0, true);
209 nav_id_node = node->getChild("nav-id", 0, true);
210 id_c1_node = node->getChild("nav-id_asc1", 0, true);
211 id_c2_node = node->getChild("nav-id_asc2", 0, true);
212 id_c3_node = node->getChild("nav-id_asc3", 0, true);
213 id_c4_node = node->getChild("nav-id_asc4", 0, true);
215 // gps slaving support
216 nav_slaved_to_gps_node = node->getChild("slaved-to-gps", 0, true);
217 gps_cdi_deflection_node = fgGetNode("/instrumentation/gps/cdi-deflection", true);
218 gps_to_flag_node = fgGetNode("/instrumentation/gps/to-flag", true);
219 gps_from_flag_node = fgGetNode("/instrumentation/gps/from-flag", true);
221 std::ostringstream temp;
222 temp << name << "nav-ident" << num;
223 nav_fx_name = temp.str();
224 temp << name << "dme-ident" << num;
225 dme_fx_name = temp.str();
231 std::ostringstream temp;
234 branch = "/instrumentation/" + name + "[" + temp.str() + "]";
239 FGNavRadio::unbind ()
241 std::ostringstream temp;
244 branch = "/instrumentation/" + name + "[" + temp.str() + "]";
248 // model standard VOR/DME/TACAN service volumes as per AIM 1-1-8
249 double FGNavRadio::adjustNavRange( double stationElev, double aircraftElev,
250 double nominalRange )
252 // extend out actual usable range to be 1.3x the published safe range
253 const double usability_factor = 1.3;
255 // assumptions we model the standard service volume, plus
256 // ... rather than specifying a cylinder, we model a cone that
257 // contains the cylinder. Then we put an upside down cone on top
258 // to model diminishing returns at too-high altitudes.
260 // altitude difference
261 double alt = ( aircraftElev * SG_METER_TO_FEET - stationElev );
262 // cout << "aircraft elev = " << aircraftElev * SG_METER_TO_FEET
263 // << " station elev = " << stationElev << endl;
265 if ( nominalRange < 25.0 + SG_EPSILON ) {
266 // Standard Terminal Service Volume
267 return term_tbl->interpolate( alt ) * usability_factor;
268 } else if ( nominalRange < 50.0 + SG_EPSILON ) {
269 // Standard Low Altitude Service Volume
270 // table is based on range of 40, scale to actual range
271 return low_tbl->interpolate( alt ) * nominalRange / 40.0
274 // Standard High Altitude Service Volume
275 // table is based on range of 130, scale to actual range
276 return high_tbl->interpolate( alt ) * nominalRange / 130.0
282 // model standard ILS service volumes as per AIM 1-1-9
283 double FGNavRadio::adjustILSRange( double stationElev, double aircraftElev,
284 double offsetDegrees, double distance )
286 // assumptions we model the standard service volume, plus
288 // altitude difference
289 // double alt = ( aircraftElev * SG_METER_TO_FEET - stationElev );
290 // double offset = fabs( offsetDegrees );
292 // if ( offset < 10 ) {
293 // return FG_ILS_DEFAULT_RANGE;
294 // } else if ( offset < 35 ) {
295 // return 10 + (35 - offset) * (FG_ILS_DEFAULT_RANGE - 10) / 25;
296 // } else if ( offset < 45 ) {
297 // return (45 - offset);
298 // } else if ( offset > 170 ) {
299 // return FG_ILS_DEFAULT_RANGE;
300 // } else if ( offset > 145 ) {
301 // return 10 + (offset - 145) * (FG_ILS_DEFAULT_RANGE - 10) / 25;
302 // } else if ( offset > 135 ) {
303 // return (offset - 135);
307 return FG_LOC_DEFAULT_RANGE;
311 //////////////////////////////////////////////////////////////////////////
312 // Update the various nav values based on position and valid tuned in navs
313 //////////////////////////////////////////////////////////////////////////
315 FGNavRadio::update(double dt)
317 // Do a nav station search only once a second to reduce
318 // unnecessary work. (Also, make sure to do this before caching
320 _time_before_search_sec -= dt;
321 if ( _time_before_search_sec < 0 ) {
325 // cache a few strategic values locally for speed
326 SGGeod pos = SGGeod::fromDegFt(lon_node->getDoubleValue(),
327 lat_node->getDoubleValue(),
328 alt_node->getDoubleValue());
329 bool power_btn = power_btn_node->getBoolValue();
330 bool nav_serviceable = nav_serviceable_node->getBoolValue();
331 bool cdi_serviceable = cdi_serviceable_node->getBoolValue();
332 bool tofrom_serviceable = tofrom_serviceable_node->getBoolValue();
333 bool inrange = inrange_node->getBoolValue();
334 bool has_gs = has_gs_node->getBoolValue();
335 bool is_loc = loc_node->getBoolValue();
336 double loc_dist = loc_dist_node->getDoubleValue();
340 // Create "formatted" versions of the nav frequencies for
341 // instrument displays.
343 sprintf( tmp, "%.2f", freq_node->getDoubleValue() );
344 fmt_freq_node->setStringValue(tmp);
345 sprintf( tmp, "%.2f", alt_freq_node->getDoubleValue() );
346 fmt_alt_freq_node->setStringValue(tmp);
348 // cout << "is_valid = " << is_valid
349 // << " power_btn = " << power_btn
350 // << " bus_power = " << bus_power_node->getDoubleValue()
351 // << " nav_serviceable = " << nav_serviceable
354 if ( is_valid && power_btn && (bus_power_node->getDoubleValue() > 1.0)
357 SGVec3d aircraft = SGVec3d::fromGeod(pos);
358 loc_dist = dist(aircraft, nav_xyz);
359 loc_dist_node->setDoubleValue( loc_dist );
360 // cout << "station = " << station << " dist = " << loc_dist << endl;
363 // find closest distance to the gs base line
364 SGVec3d p = aircraft;
365 double dist = sgdClosestPointToLineDistSquared(p.sg(), gs_xyz.sg(),
367 gs_dist_node->setDoubleValue( sqrt( dist ) );
368 // cout << "gs_dist = " << gs_dist_node->getDoubleValue()
371 // wgs84 heading to glide slope (to determine sign of distance)
372 geo_inverse_wgs_84( pos, SGGeod::fromDeg(gs_lon, gs_lat),
374 double r = az1 - target_radial;
375 while ( r > 180.0 ) { r -= 360.0;}
376 while ( r < -180.0 ) { r += 360.0;}
377 if ( r >= -90.0 && r <= 90.0 ) {
378 gs_dist_signed = gs_dist_node->getDoubleValue();
380 gs_dist_signed = -gs_dist_node->getDoubleValue();
382 /* cout << "Target Radial = " << target_radial
383 << " Bearing = " << az1
384 << " dist (signed) = " << gs_dist_signed
388 gs_dist_node->setDoubleValue( 0.0 );
391 //////////////////////////////////////////////////////////
392 // compute forward and reverse wgs84 headings to localizer
393 //////////////////////////////////////////////////////////
395 geo_inverse_wgs_84( pos, SGGeod::fromDeg(loc_lon, loc_lat),
397 // cout << "az1 = " << az1 << " magvar = " << nav_magvar << endl;
398 heading_node->setDoubleValue( hdg );
399 radial = az2 - twist;
400 double recip = radial + 180.0;
401 if ( recip >= 360.0 ) { recip -= 360.0; }
402 radial_node->setDoubleValue( radial );
403 recip_radial_node->setDoubleValue( recip );
404 // cout << " heading = " << heading_node->getDoubleValue()
405 // << " dist = " << nav_dist << endl;
407 //////////////////////////////////////////////////////////
408 // compute the target/selected radial in "true" heading
409 //////////////////////////////////////////////////////////
412 // ILS localizers radials are already "true" in our
414 trtrue = target_radial;
416 // VOR radials need to have that vor's offset added in
417 trtrue = target_radial + twist;
420 while ( trtrue < 0.0 ) { trtrue += 360.0; }
421 while ( trtrue > 360.0 ) { trtrue -= 360.0; }
422 target_radial_true_node->setDoubleValue( trtrue );
424 //////////////////////////////////////////////////////////
425 // adjust reception range for altitude
426 //////////////////////////////////////////////////////////
428 double offset = radial - target_radial;
429 while ( offset < -180.0 ) { offset += 360.0; }
430 while ( offset > 180.0 ) { offset -= 360.0; }
431 // cout << "ils offset = " << offset << endl;
433 = adjustILSRange( nav_elev, pos.getElevationM(), offset,
434 loc_dist * SG_METER_TO_NM );
436 effective_range = adjustNavRange( nav_elev, pos.getElevationM(), range );
438 // cout << "nav range = " << effective_range
439 // << " (" << range << ")" << endl;
441 if ( loc_dist < effective_range * SG_NM_TO_METER ) {
443 } else if ( loc_dist < 2 * effective_range * SG_NM_TO_METER ) {
444 inrange = sg_random() < ( 2 * effective_range * SG_NM_TO_METER
446 / (effective_range * SG_NM_TO_METER);
450 inrange_node->setBoolValue( inrange );
453 target_radial = sel_radial_node->getDoubleValue();
456 //////////////////////////////////////////////////////////
457 // compute to/from flag status
458 //////////////////////////////////////////////////////////
459 double value = false;
460 double offset = fabs(radial - target_radial);
461 if ( tofrom_serviceable ) {
462 if ( nav_slaved_to_gps_node->getBoolValue() ) {
463 value = gps_to_flag_node->getBoolValue();
464 } else if ( inrange ) {
468 value = !(offset <= 90.0 || offset >= 270.0);
474 to_flag_node->setBoolValue( value );
477 if ( tofrom_serviceable ) {
478 if ( nav_slaved_to_gps_node->getBoolValue() ) {
479 value = gps_from_flag_node->getBoolValue();
480 } else if ( inrange ) {
484 value = !(offset > 90.0 && offset < 270.0);
490 from_flag_node->setBoolValue( value );
492 //////////////////////////////////////////////////////////
493 // compute the deflection of the CDI needle, clamped to the range
495 //////////////////////////////////////////////////////////
497 if ( cdi_serviceable ) {
498 if ( nav_slaved_to_gps_node->getBoolValue() ) {
499 r = gps_cdi_deflection_node->getDoubleValue();
500 // We want +- 5 dots deflection for the gps, so clamp
502 if ( r < -12.5 ) { r = -12.5; }
503 if ( r > 12.5 ) { r = 12.5; }
504 } else if ( inrange ) {
505 r = radial - target_radial;
506 // cout << "Target radial = " << target_radial
507 // << " Actual radial = " << radial << endl;
509 while ( r > 180.0 ) { r -= 360.0;}
510 while ( r < -180.0 ) { r += 360.0;}
511 if ( fabs(r) > 90.0 ) {
512 r = ( r<0.0 ? -r-180.0 : -r+180.0 );
515 // According to Robin Peel, the ILS is 4x more
516 // sensitive than a vor
517 r = -r; // reverse, since radial is outbound
518 if ( is_loc ) { r *= 4.0; }
519 if ( r < -10.0 ) { r = -10.0; }
520 if ( r > 10.0 ) { r = 10.0; }
523 cdi_deflection_node->setDoubleValue( r );
525 //////////////////////////////////////////////////////////
526 // compute the amount of cross track distance error in meters
527 //////////////////////////////////////////////////////////
528 double xtrack_error = 0.0;
529 if ( inrange && nav_serviceable && cdi_serviceable ) {
530 r = radial - target_radial;
531 // cout << "Target radial = " << target_radial
532 // << " Actual radial = " << radial
533 // << " r = " << r << endl;
535 while ( r > 180.0 ) { r -= 360.0;}
536 while ( r < -180.0 ) { r += 360.0;}
537 if ( fabs(r) > 90.0 ) {
538 r = ( r<0.0 ? -r-180.0 : -r+180.0 );
541 r = -r; // reverse, since radial is outbound
543 xtrack_error = loc_dist * sin(r * SGD_DEGREES_TO_RADIANS);
547 cdi_xtrack_error_node->setDoubleValue( xtrack_error );
549 //////////////////////////////////////////////////////////
550 // compute an approximate ground track heading error
551 //////////////////////////////////////////////////////////
552 double hdg_error = 0.0;
553 if ( inrange && cdi_serviceable ) {
554 double ddist = last_loc_dist - loc_dist;
555 double dxtrack = last_xtrack_error - xtrack_error;
556 double a = atan2( dxtrack, ddist ) * SGD_RADIANS_TO_DEGREES;
557 if ( from_flag_node->getBoolValue() ) {
559 if ( a > 180.0 ) { a -= 360.0; }
560 if ( a < -180.0 ) { a += 360.0; }
562 SGPropertyNode *true_hdg
563 = fgGetNode("/orientation/heading-deg", true);
564 // cout << "true heading = " << true_hdg->getDoubleValue()
565 // << " selrad = " << sel_radial_node->getDoubleValue()
566 // << " artr = " << a
568 double est_hdg = trtrue + a;
569 if ( est_hdg < 0.0 ) { est_hdg += 360.0; }
570 if ( est_hdg >= 360.0 ) { est_hdg -= 360.0; }
571 hdg_error = est_hdg - true_hdg->getDoubleValue();
573 cdi_xtrack_hdg_err_node->setDoubleValue( hdg_error );
575 //////////////////////////////////////////////////////////
576 // compute the time to intercept selected radial (based on
577 // current and last cross track errors and dt
578 //////////////////////////////////////////////////////////
580 if ( inrange && cdi_serviceable ) {
581 double xrate_ms = (last_xtrack_error - xtrack_error) / dt;
582 if ( fabs(xrate_ms) > 0.00001 ) {
583 t = xtrack_error / xrate_ms;
588 time_to_intercept->setDoubleValue( t );
590 //////////////////////////////////////////////////////////
591 // compute the amount of glide slope needle deflection
592 // (.i.e. the number of degrees we are off the glide slope * 5.0
594 // CLO - 13 Mar 2006: The glide slope needle should peg at
595 // +/-0.7 degrees off the ideal glideslope. I'm not sure why
596 // we compute the factor the way we do (5*gs_error), but we
597 // need to compensate for our 'odd' number in the glideslope
598 // needle animation. This means that the needle should peg
599 // when this values is +/-3.5.
600 //////////////////////////////////////////////////////////
602 if ( has_gs && gs_serviceable_node->getBoolValue() ) {
603 if ( nav_slaved_to_gps_node->getBoolValue() ) {
604 // FIXME/FINISHME, what should be set here?
605 } else if ( inrange ) {
606 double x = gs_dist_node->getDoubleValue();
607 double y = (fgGetDouble("/position/altitude-ft") - nav_elev)
609 // cout << "dist = " << x << " height = " << y << endl;
610 double angle = asin( y / x ) * SGD_RADIANS_TO_DEGREES;
611 r = (target_gs - angle) * 5.0;
614 gs_deflection_node->setDoubleValue( r );
616 //////////////////////////////////////////////////////////
617 // Calculate desired rate of climb for intercepting the GS
618 //////////////////////////////////////////////////////////
619 double x = gs_dist_node->getDoubleValue();
620 double y = (alt_node->getDoubleValue() - nav_elev)
622 double current_angle = atan2( y, x ) * SGD_RADIANS_TO_DEGREES;
624 double target_angle = target_gs;
625 double gs_diff = target_angle - current_angle;
627 // convert desired vertical path angle into a climb rate
628 double des_angle = current_angle - 10 * gs_diff;
630 // estimate horizontal speed towards ILS in meters per minute
631 double dist = last_x - x;
635 double new_vel = ( dist / dt );
637 horiz_vel = 0.75 * horiz_vel + 0.25 * new_vel;
638 // double horiz_vel = cur_fdm_state->get_V_ground_speed()
639 // * SG_FEET_TO_METER * 60.0;
640 // double horiz_vel = airspeed_node->getFloatValue()
641 // * SG_FEET_TO_METER * 60.0;
643 gs_rate_of_climb_node
644 ->setDoubleValue( -sin( des_angle * SGD_DEGREES_TO_RADIANS )
645 * horiz_vel * SG_METER_TO_FEET );
648 //////////////////////////////////////////////////////////
649 // Calculate a suggested target heading to smoothly intercept
651 //////////////////////////////////////////////////////////
653 // FIXME: this smells odd, there must be a better (or more
656 // determine the heading adjustment needed.
657 // over 8km scale by 3.0
658 // (3 is chosen because max deflection is 10
659 // and 30 is clamped angle to radial)
660 // under 8km scale by 10.0
661 // because the overstated error helps drive it to the radial in a
662 // moderate cross wind.
663 double adjustment = 0.0;
664 if ( loc_dist > 8000 ) {
665 adjustment = cdi_deflection_node->getDoubleValue() * 3.0;
667 adjustment = cdi_deflection_node->getDoubleValue() * 10.0;
669 SG_CLAMP_RANGE( adjustment, -30.0, 30.0 );
671 // determine the target heading to fly to intercept the
672 // tgt_radial = target radial (true) + cdi offset adjustmest -
673 // xtrack heading error adjustment
674 double nta_hdg = trtrue + adjustment - hdg_error;
675 while ( nta_hdg < 0.0 ) { nta_hdg += 360.0; }
676 while ( nta_hdg > 360.0 ) { nta_hdg -= 360.0; }
677 target_auto_hdg_node->setDoubleValue( nta_hdg );
679 last_xtrack_error = xtrack_error;
681 inrange_node->setBoolValue( false );
682 cdi_deflection_node->setDoubleValue( 0.0 );
683 cdi_xtrack_error_node->setDoubleValue( 0.0 );
684 cdi_xtrack_hdg_err_node->setDoubleValue( 0.0 );
685 time_to_intercept->setDoubleValue( 0.0 );
686 gs_deflection_node->setDoubleValue( 0.0 );
687 to_flag_node->setBoolValue( false );
688 from_flag_node->setBoolValue( false );
689 // cout << "not picking up vor. :-(" << endl;
693 if ( is_valid && inrange && nav_serviceable ) {
694 // play station ident via audio system if on + ident,
695 // otherwise turn it off
697 && (bus_power_node->getDoubleValue() > 1.0)
698 && ident_btn_node->getBoolValue()
699 && audio_btn_node->getBoolValue() )
701 SGSoundSample *sound;
702 sound = globals->get_soundmgr()->find( nav_fx_name );
703 double vol = vol_btn_node->getDoubleValue();
704 if ( vol < 0.0 ) { vol = 0.0; }
705 if ( vol > 1.0 ) { vol = 1.0; }
706 if ( sound != NULL ) {
707 sound->set_volume( vol );
709 SG_LOG( SG_COCKPIT, SG_ALERT,
710 "Can't find nav-vor-ident sound" );
712 sound = globals->get_soundmgr()->find( dme_fx_name );
713 if ( sound != NULL ) {
714 sound->set_volume( vol );
716 SG_LOG( SG_COCKPIT, SG_ALERT,
717 "Can't find nav-dme-ident sound" );
719 // cout << "last_time = " << last_time << " ";
720 // cout << "cur_time = "
721 // << globals->get_time_params()->get_cur_time();
723 globals->get_time_params()->get_cur_time() - 30 ) {
724 last_time = globals->get_time_params()->get_cur_time();
727 // cout << " play_count = " << play_count << endl;
728 // cout << "playing = "
729 // << globals->get_soundmgr()->is_playing(nav_fx_name)
731 if ( play_count < 4 ) {
733 if ( !globals->get_soundmgr()->is_playing(nav_fx_name) ) {
734 globals->get_soundmgr()->play_once( nav_fx_name );
737 } else if ( play_count < 5 && has_dme ) {
739 if ( !globals->get_soundmgr()->is_playing(nav_fx_name) &&
740 !globals->get_soundmgr()->is_playing(dme_fx_name) ) {
741 globals->get_soundmgr()->play_once( dme_fx_name );
746 globals->get_soundmgr()->stop( nav_fx_name );
747 globals->get_soundmgr()->stop( dme_fx_name );
751 last_loc_dist = loc_dist;
755 // Update current nav/adf radio stations based on current postition
756 void FGNavRadio::search()
760 _time_before_search_sec = 1.0;
762 // cache values locally for speed
763 double lon = lon_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
764 double lat = lat_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
765 double elev = alt_node->getDoubleValue() * SG_FEET_TO_METER;
767 FGNavRecord *nav = NULL;
768 FGNavRecord *loc = NULL;
769 FGNavRecord *dme = NULL;
770 FGNavRecord *gs = NULL;
772 ////////////////////////////////////////////////////////////////////////
774 ////////////////////////////////////////////////////////////////////////
776 double freq = freq_node->getDoubleValue();
777 nav = globals->get_navlist()->findByFreq(freq, lon, lat, elev);
778 dme = globals->get_dmelist()->findByFreq(freq, lon, lat, elev);
780 loc = globals->get_loclist()->findByFreq(freq, lon, lat, elev);
781 gs = globals->get_gslist()->findByFreq(freq, lon, lat, elev);
787 nav_id = loc->get_ident();
788 nav_id_node->setStringValue( nav_id.c_str() );
789 // cout << "localizer = " << nav_id_node->getStringValue() << endl;
791 if ( last_nav_id != nav_id || last_nav_vor ) {
792 trans_ident = loc->get_trans_ident();
793 target_radial = loc->get_multiuse();
794 while ( target_radial < 0.0 ) { target_radial += 360.0; }
795 while ( target_radial > 360.0 ) { target_radial -= 360.0; }
796 loc_lon = loc->get_lon();
797 loc_lat = loc->get_lat();
798 nav_xyz = loc->get_cart();
799 last_nav_id = nav_id;
800 last_nav_vor = false;
801 loc_node->setBoolValue( true );
802 has_dme = (dme != NULL);
804 has_gs_node->setBoolValue( true );
805 gs_lon = gs->get_lon();
806 gs_lat = gs->get_lat();
807 nav_elev = gs->get_elev_ft();
808 int tmp = (int)(gs->get_multiuse() / 1000.0);
809 target_gs = (double)tmp / 100.0;
810 gs_xyz = gs->get_cart();
812 // derive GS baseline (perpendicular to the runay
814 double tlon, tlat, taz;
815 geo_direct_wgs_84 ( 0.0, gs_lat, gs_lon,
817 100.0, &tlat, &tlon, &taz );
818 // cout << "target_radial = " << target_radial << endl;
819 // cout << "nav_loc = " << loc_node->getBoolValue() << endl;
820 // cout << gs_lon << "," << gs_lat << " "
821 // << tlon << "," << tlat << " (" << nav_elev << ")"
823 SGGeod tpos = SGGeod::fromDegFt(tlon, tlat, nav_elev);
824 SGVec3d p1 = SGVec3d::fromGeod(tpos);
826 // cout << gs_xyz << endl;
827 // cout << p1 << endl;
828 gs_base_vec = p1 - gs_xyz;
829 // cout << gs_base_vec << endl;
831 has_gs_node->setBoolValue( false );
832 nav_elev = loc->get_elev_ft();
835 range = FG_LOC_DEFAULT_RANGE;
836 effective_range = range;
838 if ( globals->get_soundmgr()->exists( nav_fx_name ) ) {
839 globals->get_soundmgr()->remove( nav_fx_name );
841 SGSoundSample *sound;
842 sound = morse.make_ident( trans_ident, LO_FREQUENCY );
843 sound->set_volume( 0.3 );
844 globals->get_soundmgr()->add( sound, nav_fx_name );
846 if ( globals->get_soundmgr()->exists( dme_fx_name ) ) {
847 globals->get_soundmgr()->remove( dme_fx_name );
849 sound = morse.make_ident( trans_ident, HI_FREQUENCY );
850 sound->set_volume( 0.3 );
851 globals->get_soundmgr()->add( sound, dme_fx_name );
853 int offset = (int)(sg_random() * 30.0);
854 play_count = offset / 4;
855 last_time = globals->get_time_params()->get_cur_time() -
857 // cout << "offset = " << offset << " play_count = "
859 // << " last_time = " << last_time
860 // << " current time = "
861 // << globals->get_time_params()->get_cur_time() << endl;
863 // cout << "Found an loc station in range" << endl;
864 // cout << " id = " << loc->get_locident() << endl;
866 } else if ( nav != NULL ) {
867 nav_id = nav->get_ident();
868 nav_id_node->setStringValue( nav_id.c_str() );
869 // cout << "nav = " << nav_id << endl;
871 if ( last_nav_id != nav_id || !last_nav_vor ) {
872 last_nav_id = nav_id;
874 trans_ident = nav->get_trans_ident();
875 loc_node->setBoolValue( false );
876 has_dme = (dme != NULL);
877 has_gs_node->setBoolValue( false );
878 loc_lon = nav->get_lon();
879 loc_lat = nav->get_lat();
880 nav_elev = nav->get_elev_ft();
881 twist = nav->get_multiuse();
882 range = nav->get_range();
883 effective_range = adjustNavRange(nav_elev, elev, range);
885 target_radial = sel_radial_node->getDoubleValue();
886 nav_xyz = nav->get_cart();
888 if ( globals->get_soundmgr()->exists( nav_fx_name ) ) {
889 globals->get_soundmgr()->remove( nav_fx_name );
892 SGSoundSample *sound;
893 sound = morse.make_ident( trans_ident, LO_FREQUENCY );
894 sound->set_volume( 0.3 );
895 if ( globals->get_soundmgr()->add( sound, nav_fx_name ) ) {
896 // cout << "Added nav-vor-ident sound" << endl;
898 SG_LOG(SG_COCKPIT, SG_WARN, "Failed to add v1-vor-ident sound");
901 if ( globals->get_soundmgr()->exists( dme_fx_name ) ) {
902 globals->get_soundmgr()->remove( dme_fx_name );
904 sound = morse.make_ident( trans_ident, HI_FREQUENCY );
905 sound->set_volume( 0.3 );
906 globals->get_soundmgr()->add( sound, dme_fx_name );
908 int offset = (int)(sg_random() * 30.0);
909 play_count = offset / 4;
910 last_time = globals->get_time_params()->get_cur_time() - offset;
911 // cout << "offset = " << offset << " play_count = "
912 // << play_count << " last_time = "
913 // << last_time << " current time = "
914 // << globals->get_time_params()->get_cur_time() << endl;
916 // cout << "Found a vor station in range" << endl;
917 // cout << " id = " << nav->get_ident() << endl;
918 } catch ( sg_io_exception &e ) {
919 SG_LOG(SG_GENERAL, SG_ALERT, e.getFormattedMessage());
924 nav_id_node->setStringValue( "" );
928 if ( ! globals->get_soundmgr()->remove( nav_fx_name ) ) {
929 SG_LOG(SG_COCKPIT, SG_WARN, "Failed to remove nav-vor-ident sound");
931 globals->get_soundmgr()->remove( dme_fx_name );
932 // cout << "not picking up vor1. :-(" << endl;
935 is_valid_node->setBoolValue( is_valid );
938 strncpy( tmpid, nav_id.c_str(), 5 );
939 id_c1_node->setIntValue( (int)tmpid[0] );
940 id_c2_node->setIntValue( (int)tmpid[1] );
941 id_c3_node->setIntValue( (int)tmpid[2] );
942 id_c4_node->setIntValue( (int)tmpid[3] );