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.
30 #include <simgear/sg_inlines.h>
31 #include <simgear/timing/sg_time.hxx>
32 #include <simgear/math/vector.hxx>
33 #include <simgear/math/sg_random.h>
34 #include <simgear/misc/sg_path.hxx>
35 #include <simgear/math/sg_geodesy.hxx>
36 #include <simgear/structure/exception.hxx>
38 #include <Navaids/navlist.hxx>
39 #include <Main/util.hxx>
40 #include "navradio.hxx"
45 FGNavRadio::FGNavRadio(SGPropertyNode *node) :
46 lon_node(fgGetNode("/position/longitude-deg", true)),
47 lat_node(fgGetNode("/position/latitude-deg", true)),
48 alt_node(fgGetNode("/position/altitude-ft", true)),
53 sel_radial_node(NULL),
57 backcourse_node(NULL),
58 nav_serviceable_node(NULL),
59 cdi_serviceable_node(NULL),
60 gs_serviceable_node(NULL),
61 tofrom_serviceable_node(NULL),
63 fmt_alt_freq_node(NULL),
66 recip_radial_node(NULL),
67 target_radial_true_node(NULL),
68 target_auto_hdg_node(NULL),
69 time_to_intercept(NULL),
73 signal_quality_norm_node(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),
102 _name(node->getStringValue("name", "nav")),
103 _num(node->getIntValue("number", 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() );
121 FGNavRadio::~FGNavRadio()
135 branch = "/instrumentation/" + _name;
137 SGPropertyNode *node = fgGetNode(branch.c_str(), _num, true );
140 fgGetNode(("/systems/electrical/outputs/" + _name).c_str(), true);
143 is_valid_node = node->getChild("data-is-valid", 0, true);
144 power_btn_node = node->getChild("power-btn", 0, true);
145 power_btn_node->setBoolValue( true );
146 vol_btn_node = node->getChild("volume", 0, true);
147 ident_btn_node = node->getChild("ident", 0, true);
148 ident_btn_node->setBoolValue( true );
149 audio_btn_node = node->getChild("audio-btn", 0, true);
150 audio_btn_node->setBoolValue( true );
151 backcourse_node = node->getChild("back-course-btn", 0, true);
152 backcourse_node->setBoolValue( false );
153 nav_serviceable_node = node->getChild("serviceable", 0, true);
154 cdi_serviceable_node = (node->getChild("cdi", 0, true))
155 ->getChild("serviceable", 0, true);
156 gs_serviceable_node = (node->getChild("gs", 0, true))
157 ->getChild("serviceable");
158 tofrom_serviceable_node = (node->getChild("to-from", 0, true))
159 ->getChild("serviceable", 0, true);
162 SGPropertyNode *subnode = node->getChild("frequencies", 0, true);
163 freq_node = subnode->getChild("selected-mhz", 0, true);
164 alt_freq_node = subnode->getChild("standby-mhz", 0, true);
165 fmt_freq_node = subnode->getChild("selected-mhz-fmt", 0, true);
166 fmt_alt_freq_node = subnode->getChild("standby-mhz-fmt", 0, true);
169 subnode = node->getChild("radials", 0, true);
170 sel_radial_node = subnode->getChild("selected-deg", 0, true);
171 radial_node = subnode->getChild("actual-deg", 0, true);
172 recip_radial_node = subnode->getChild("reciprocal-radial-deg", 0, true);
173 target_radial_true_node = subnode->getChild("target-radial-deg", 0, true);
174 target_auto_hdg_node = subnode->getChild("target-auto-hdg-deg", 0, true);
177 heading_node = node->getChild("heading-deg", 0, true);
178 time_to_intercept = node->getChild("time-to-intercept-sec", 0, true);
179 to_flag_node = node->getChild("to-flag", 0, true);
180 from_flag_node = node->getChild("from-flag", 0, true);
181 inrange_node = node->getChild("in-range", 0, true);
182 signal_quality_norm_node = node->getChild("signal-quality-norm", 0, true);
183 cdi_deflection_node = node->getChild("heading-needle-deflection", 0, true);
184 cdi_xtrack_error_node = node->getChild("crosstrack-error-m", 0, true);
185 cdi_xtrack_hdg_err_node
186 = node->getChild("crosstrack-heading-error-deg", 0, true);
187 has_gs_node = node->getChild("has-gs", 0, true);
188 loc_node = node->getChild("nav-loc", 0, true);
189 loc_dist_node = node->getChild("nav-distance", 0, true);
190 gs_deflection_node = node->getChild("gs-needle-deflection", 0, true);
191 gs_rate_of_climb_node = node->getChild("gs-rate-of-climb", 0, true);
192 gs_dist_node = node->getChild("gs-distance", 0, true);
193 nav_id_node = node->getChild("nav-id", 0, true);
194 id_c1_node = node->getChild("nav-id_asc1", 0, true);
195 id_c2_node = node->getChild("nav-id_asc2", 0, true);
196 id_c3_node = node->getChild("nav-id_asc3", 0, true);
197 id_c4_node = node->getChild("nav-id_asc4", 0, true);
199 // gps slaving support
200 nav_slaved_to_gps_node = node->getChild("slaved-to-gps", 0, true);
201 gps_cdi_deflection_node = fgGetNode("/instrumentation/gps/cdi-deflection", true);
202 gps_to_flag_node = fgGetNode("/instrumentation/gps/to-flag", true);
203 gps_from_flag_node = fgGetNode("/instrumentation/gps/from-flag", true);
205 std::ostringstream temp;
206 temp << _name << "nav-ident" << _num;
207 nav_fx_name = temp.str();
208 temp << _name << "dme-ident" << _num;
209 dme_fx_name = temp.str();
215 std::ostringstream temp;
218 branch = "/instrumentation/" + _name + "[" + temp.str() + "]";
223 FGNavRadio::unbind ()
225 std::ostringstream temp;
228 branch = "/instrumentation/" + _name + "[" + temp.str() + "]";
232 // model standard VOR/DME/TACAN service volumes as per AIM 1-1-8
233 double FGNavRadio::adjustNavRange( double stationElev, double aircraftElev,
234 double nominalRange )
236 // extend out actual usable range to be 1.3x the published safe range
237 const double usability_factor = 1.3;
239 // assumptions we model the standard service volume, plus
240 // ... rather than specifying a cylinder, we model a cone that
241 // contains the cylinder. Then we put an upside down cone on top
242 // to model diminishing returns at too-high altitudes.
244 // altitude difference
245 double alt = ( aircraftElev * SG_METER_TO_FEET - stationElev );
246 // cout << "aircraft elev = " << aircraftElev * SG_METER_TO_FEET
247 // << " station elev = " << stationElev << endl;
249 if ( nominalRange < 25.0 + SG_EPSILON ) {
250 // Standard Terminal Service Volume
251 return term_tbl->interpolate( alt ) * usability_factor;
252 } else if ( nominalRange < 50.0 + SG_EPSILON ) {
253 // Standard Low Altitude Service Volume
254 // table is based on range of 40, scale to actual range
255 return low_tbl->interpolate( alt ) * nominalRange / 40.0
258 // Standard High Altitude Service Volume
259 // table is based on range of 130, scale to actual range
260 return high_tbl->interpolate( alt ) * nominalRange / 130.0
266 // model standard ILS service volumes as per AIM 1-1-9
267 double FGNavRadio::adjustILSRange( double stationElev, double aircraftElev,
268 double offsetDegrees, double distance )
270 // assumptions we model the standard service volume, plus
272 // altitude difference
273 // double alt = ( aircraftElev * SG_METER_TO_FEET - stationElev );
274 // double offset = fabs( offsetDegrees );
276 // if ( offset < 10 ) {
277 // return FG_ILS_DEFAULT_RANGE;
278 // } else if ( offset < 35 ) {
279 // return 10 + (35 - offset) * (FG_ILS_DEFAULT_RANGE - 10) / 25;
280 // } else if ( offset < 45 ) {
281 // return (45 - offset);
282 // } else if ( offset > 170 ) {
283 // return FG_ILS_DEFAULT_RANGE;
284 // } else if ( offset > 145 ) {
285 // return 10 + (offset - 145) * (FG_ILS_DEFAULT_RANGE - 10) / 25;
286 // } else if ( offset > 135 ) {
287 // return (offset - 135);
291 return FG_LOC_DEFAULT_RANGE;
295 //////////////////////////////////////////////////////////////////////////
296 // Update the various nav values based on position and valid tuned in navs
297 //////////////////////////////////////////////////////////////////////////
299 FGNavRadio::update(double dt)
301 // Do a nav station search only once a second to reduce
302 // unnecessary work. (Also, make sure to do this before caching
304 _time_before_search_sec -= dt;
305 if ( _time_before_search_sec < 0 ) {
309 // cache a few strategic values locally for speed
310 SGGeod pos = SGGeod::fromDegFt(lon_node->getDoubleValue(),
311 lat_node->getDoubleValue(),
312 alt_node->getDoubleValue());
313 bool power_btn = power_btn_node->getBoolValue();
314 bool nav_serviceable = nav_serviceable_node->getBoolValue();
315 bool cdi_serviceable = cdi_serviceable_node->getBoolValue();
316 bool tofrom_serviceable = tofrom_serviceable_node->getBoolValue();
317 bool inrange = inrange_node->getBoolValue();
318 bool has_gs = has_gs_node->getBoolValue();
319 bool is_loc = loc_node->getBoolValue();
320 double loc_dist = loc_dist_node->getDoubleValue();
321 double effective_range_m;
322 double signal_quality_norm = signal_quality_norm_node->getDoubleValue();
326 // Create "formatted" versions of the nav frequencies for
327 // instrument displays.
329 sprintf( tmp, "%.2f", freq_node->getDoubleValue() );
330 fmt_freq_node->setStringValue(tmp);
331 sprintf( tmp, "%.2f", alt_freq_node->getDoubleValue() );
332 fmt_alt_freq_node->setStringValue(tmp);
334 // cout << "is_valid = " << is_valid
335 // << " power_btn = " << power_btn
336 // << " bus_power = " << bus_power_node->getDoubleValue()
337 // << " nav_serviceable = " << nav_serviceable
340 if ( is_valid && power_btn && (bus_power_node->getDoubleValue() > 1.0)
343 SGVec3d aircraft = SGVec3d::fromGeod(pos);
344 loc_dist = dist(aircraft, nav_xyz);
345 loc_dist_node->setDoubleValue( loc_dist );
346 // cout << "dt = " << dt << " dist = " << loc_dist << endl;
349 // find closest distance to the gs base line
350 SGVec3d p = aircraft;
351 double dist = sgdClosestPointToLineDistSquared(p.sg(), gs_xyz.sg(),
353 gs_dist_node->setDoubleValue( sqrt( dist ) );
354 // cout << "gs_dist = " << gs_dist_node->getDoubleValue()
357 // wgs84 heading to glide slope (to determine sign of distance)
358 geo_inverse_wgs_84( pos, SGGeod::fromDeg(gs_lon, gs_lat),
360 double r = az1 - target_radial;
361 while ( r > 180.0 ) { r -= 360.0;}
362 while ( r < -180.0 ) { r += 360.0;}
363 if ( r >= -90.0 && r <= 90.0 ) {
364 gs_dist_signed = gs_dist_node->getDoubleValue();
366 gs_dist_signed = -gs_dist_node->getDoubleValue();
368 /* cout << "Target Radial = " << target_radial
369 << " Bearing = " << az1
370 << " dist (signed) = " << gs_dist_signed
374 gs_dist_node->setDoubleValue( 0.0 );
377 //////////////////////////////////////////////////////////
378 // compute forward and reverse wgs84 headings to localizer
379 //////////////////////////////////////////////////////////
381 geo_inverse_wgs_84( pos, SGGeod::fromDeg(loc_lon, loc_lat),
383 // cout << "az1 = " << az1 << " magvar = " << nav_magvar << endl;
384 heading_node->setDoubleValue( hdg );
385 radial = az2 - twist;
386 double recip = radial + 180.0;
387 if ( recip >= 360.0 ) { recip -= 360.0; }
388 radial_node->setDoubleValue( radial );
389 recip_radial_node->setDoubleValue( recip );
390 // cout << " heading = " << heading_node->getDoubleValue()
391 // << " dist = " << nav_dist << endl;
393 //////////////////////////////////////////////////////////
394 // compute the target/selected radial in "true" heading
395 //////////////////////////////////////////////////////////
398 // ILS localizers radials are already "true" in our
400 trtrue = target_radial;
402 // VOR radials need to have that vor's offset added in
403 trtrue = target_radial + twist;
406 while ( trtrue < 0.0 ) { trtrue += 360.0; }
407 while ( trtrue > 360.0 ) { trtrue -= 360.0; }
408 target_radial_true_node->setDoubleValue( trtrue );
410 //////////////////////////////////////////////////////////
411 // adjust reception range for altitude
412 // FIXME: make sure we are using the navdata range now that
413 // it is valid in the data file
414 //////////////////////////////////////////////////////////
416 double offset = radial - target_radial;
417 while ( offset < -180.0 ) { offset += 360.0; }
418 while ( offset > 180.0 ) { offset -= 360.0; }
419 // cout << "ils offset = " << offset << endl;
421 = adjustILSRange( nav_elev, pos.getElevationM(), offset,
422 loc_dist * SG_METER_TO_NM );
425 = adjustNavRange( nav_elev, pos.getElevationM(), range );
428 effective_range_m = effective_range * SG_NM_TO_METER;
430 // cout << "nav range = " << effective_range
431 // << " (" << range << ")" << endl;
433 //////////////////////////////////////////////////////////
434 // compute signal quality
435 // 100% within effective_range
436 // decreases 1/x^2 further out
437 //////////////////////////////////////////////////////////
439 double last_signal_quality_norm = signal_quality_norm;
441 if ( loc_dist < effective_range_m ) {
442 signal_quality_norm = 1.0;
444 double range_exceed_norm = loc_dist/effective_range_m;
445 signal_quality_norm = 1/(range_exceed_norm*range_exceed_norm);
448 signal_quality_norm = fgGetLowPass( last_signal_quality_norm,
449 signal_quality_norm, dt );
451 signal_quality_norm_node->setDoubleValue( signal_quality_norm );
452 inrange = signal_quality_norm > 0.2;
453 inrange_node->setBoolValue( inrange );
456 target_radial = sel_radial_node->getDoubleValue();
459 //////////////////////////////////////////////////////////
460 // compute to/from flag status
461 //////////////////////////////////////////////////////////
463 double offset = fabs(radial - target_radial);
464 if ( tofrom_serviceable ) {
465 if ( nav_slaved_to_gps_node->getBoolValue() ) {
466 value = gps_to_flag_node->getBoolValue();
467 } else if ( inrange ) {
471 value = !(offset <= 90.0 || offset >= 270.0);
477 to_flag_node->setBoolValue( value );
480 if ( tofrom_serviceable ) {
481 if ( nav_slaved_to_gps_node->getBoolValue() ) {
482 value = gps_from_flag_node->getBoolValue();
483 } else if ( inrange ) {
487 value = !(offset > 90.0 && offset < 270.0);
493 from_flag_node->setBoolValue( value );
495 //////////////////////////////////////////////////////////
496 // compute the deflection of the CDI needle, clamped to the range
498 //////////////////////////////////////////////////////////
500 bool loc_backside = false; // an in-code flag indicating that we are
501 // on a localizer backcourse.
502 if ( cdi_serviceable ) {
503 if ( nav_slaved_to_gps_node->getBoolValue() ) {
504 r = gps_cdi_deflection_node->getDoubleValue();
505 // We want +- 5 dots deflection for the gps, so clamp
507 SG_CLAMP_RANGE( r, -12.5, 12.5 );
508 } else if ( inrange ) {
509 r = radial - target_radial;
510 // cout << "Target radial = " << target_radial
511 // << " Actual radial = " << radial << endl;
513 while ( r > 180.0 ) { r -= 360.0;}
514 while ( r < -180.0 ) { r += 360.0;}
515 if ( fabs(r) > 90.0 ) {
516 r = ( r<0.0 ? -r-180.0 : -r+180.0 );
523 r = -r; // reverse, since radial is outbound
525 // According to Robin Peel, the ILS is 4x more
526 // sensitive than a vor
529 SG_CLAMP_RANGE( r, -10.0, 10.0 );
530 r *= signal_quality_norm;
533 cdi_deflection_node->setDoubleValue( r );
535 //////////////////////////////////////////////////////////
536 // compute the amount of cross track distance error in meters
537 //////////////////////////////////////////////////////////
538 double xtrack_error = 0.0;
539 if ( inrange && nav_serviceable && cdi_serviceable ) {
540 r = radial - target_radial;
541 // cout << "Target radial = " << target_radial
542 // << " Actual radial = " << radial
543 // << " r = " << r << endl;
545 while ( r > 180.0 ) { r -= 360.0;}
546 while ( r < -180.0 ) { r += 360.0;}
547 if ( fabs(r) > 90.0 ) {
548 r = ( r<0.0 ? -r-180.0 : -r+180.0 );
551 r = -r; // reverse, since radial is outbound
553 xtrack_error = loc_dist * sin(r * SGD_DEGREES_TO_RADIANS);
557 cdi_xtrack_error_node->setDoubleValue( xtrack_error );
559 //////////////////////////////////////////////////////////
560 // compute an approximate ground track heading error
561 //////////////////////////////////////////////////////////
562 double hdg_error = 0.0;
563 if ( inrange && cdi_serviceable ) {
564 double vn = fgGetDouble( "/velocities/speed-north-fps" );
565 double ve = fgGetDouble( "/velocities/speed-east-fps" );
566 double gnd_trk_true = atan2( ve, vn ) * SGD_RADIANS_TO_DEGREES;
567 if ( gnd_trk_true < 0.0 ) { gnd_trk_true += 360.0; }
569 SGPropertyNode *true_hdg
570 = fgGetNode("/orientation/heading-deg", true);
571 hdg_error = gnd_trk_true - true_hdg->getDoubleValue();
573 // cout << "ground track = " << gnd_trk_true
574 // << " orientation = " << true_hdg->getDoubleValue() << endl;
576 cdi_xtrack_hdg_err_node->setDoubleValue( hdg_error );
578 //////////////////////////////////////////////////////////
579 // compute the time to intercept selected radial (based on
580 // current and last cross track errors and dt
581 //////////////////////////////////////////////////////////
583 if ( inrange && cdi_serviceable ) {
584 double xrate_ms = (last_xtrack_error - xtrack_error) / dt;
585 if ( fabs(xrate_ms) > 0.00001 ) {
586 t = xtrack_error / xrate_ms;
591 time_to_intercept->setDoubleValue( t );
593 //////////////////////////////////////////////////////////
594 // compute the amount of glide slope needle deflection
595 // (.i.e. the number of degrees we are off the glide slope * 5.0
597 // CLO - 13 Mar 2006: The glide slope needle should peg at
598 // +/-0.7 degrees off the ideal glideslope. I'm not sure why
599 // we compute the factor the way we do (5*gs_error), but we
600 // need to compensate for our 'odd' number in the glideslope
601 // needle animation. This means that the needle should peg
602 // when this values is +/-3.5.
603 //////////////////////////////////////////////////////////
605 if ( has_gs && gs_serviceable_node->getBoolValue() ) {
606 if ( nav_slaved_to_gps_node->getBoolValue() ) {
607 // FIXME/FINISHME, what should be set here?
608 } else if ( inrange ) {
609 double x = gs_dist_node->getDoubleValue();
610 double y = (fgGetDouble("/position/altitude-ft") - nav_elev)
612 // cout << "dist = " << x << " height = " << y << endl;
613 double angle = asin( y / x ) * SGD_RADIANS_TO_DEGREES;
614 r = (target_gs - angle) * 5.0;
615 r *= signal_quality_norm;
618 gs_deflection_node->setDoubleValue( r );
620 //////////////////////////////////////////////////////////
621 // Calculate desired rate of climb for intercepting the GS
622 //////////////////////////////////////////////////////////
623 double x = gs_dist_node->getDoubleValue();
624 double y = (alt_node->getDoubleValue() - nav_elev)
626 double current_angle = atan2( y, x ) * SGD_RADIANS_TO_DEGREES;
628 double target_angle = target_gs;
629 double gs_diff = target_angle - current_angle;
631 // convert desired vertical path angle into a climb rate
632 double des_angle = current_angle - 10 * gs_diff;
634 // estimate horizontal speed towards ILS in meters per minute
635 double dist = last_x - x;
639 double new_vel = ( dist / dt );
641 horiz_vel = 0.75 * horiz_vel + 0.25 * new_vel;
642 // double horiz_vel = cur_fdm_state->get_V_ground_speed()
643 // * SG_FEET_TO_METER * 60.0;
644 // double horiz_vel = airspeed_node->getFloatValue()
645 // * SG_FEET_TO_METER * 60.0;
647 gs_rate_of_climb_node
648 ->setDoubleValue( -sin( des_angle * SGD_DEGREES_TO_RADIANS )
649 * horiz_vel * SG_METER_TO_FEET );
652 //////////////////////////////////////////////////////////
653 // Calculate a suggested target heading to smoothly intercept
655 //////////////////////////////////////////////////////////
657 // Now that we have cross track heading adjustment built in,
658 // we shouldn't need to overdrive the heading angle within 8km
661 // The cdi deflection should be +/-10 for a full range of deflection
662 // so multiplying this by 3 gives us +/- 30 degrees heading
664 double adjustment = cdi_deflection_node->getDoubleValue() * 3.0;
665 SG_CLAMP_RANGE( adjustment, -30.0, 30.0 );
667 // determine the target heading to fly to intercept the
668 // tgt_radial = target radial (true) + cdi offset adjustmest -
669 // xtrack heading error adjustment
671 if ( is_loc && backcourse_node->getBoolValue() ) {
672 // tuned to a localizer and backcourse mode activated
673 trtrue += 180.0; // reverse the target localizer heading
674 while ( trtrue > 360.0 ) { trtrue -= 360.0; }
675 nta_hdg = trtrue - adjustment - hdg_error;
677 nta_hdg = trtrue + adjustment - hdg_error;
680 while ( nta_hdg < 0.0 ) { nta_hdg += 360.0; }
681 while ( nta_hdg >= 360.0 ) { nta_hdg -= 360.0; }
682 target_auto_hdg_node->setDoubleValue( nta_hdg );
684 last_xtrack_error = xtrack_error;
686 inrange_node->setBoolValue( false );
687 cdi_deflection_node->setDoubleValue( 0.0 );
688 cdi_xtrack_error_node->setDoubleValue( 0.0 );
689 cdi_xtrack_hdg_err_node->setDoubleValue( 0.0 );
690 time_to_intercept->setDoubleValue( 0.0 );
691 gs_deflection_node->setDoubleValue( 0.0 );
692 to_flag_node->setBoolValue( false );
693 from_flag_node->setBoolValue( false );
694 // cout << "not picking up vor. :-(" << endl;
698 if ( is_valid && inrange && nav_serviceable ) {
699 // play station ident via audio system if on + ident,
700 // otherwise turn it off
702 && (bus_power_node->getDoubleValue() > 1.0)
703 && ident_btn_node->getBoolValue()
704 && audio_btn_node->getBoolValue() )
706 SGSoundSample *sound;
707 sound = globals->get_soundmgr()->find( nav_fx_name );
708 double vol = vol_btn_node->getDoubleValue();
709 if ( vol < 0.0 ) { vol = 0.0; }
710 if ( vol > 1.0 ) { vol = 1.0; }
711 if ( sound != NULL ) {
712 sound->set_volume( vol );
714 SG_LOG( SG_COCKPIT, SG_ALERT,
715 "Can't find nav-vor-ident sound" );
717 sound = globals->get_soundmgr()->find( dme_fx_name );
718 if ( sound != NULL ) {
719 sound->set_volume( vol );
721 SG_LOG( SG_COCKPIT, SG_ALERT,
722 "Can't find nav-dme-ident sound" );
724 // cout << "last_time = " << last_time << " ";
725 // cout << "cur_time = "
726 // << globals->get_time_params()->get_cur_time();
728 globals->get_time_params()->get_cur_time() - 30 ) {
729 last_time = globals->get_time_params()->get_cur_time();
732 // cout << " play_count = " << play_count << endl;
733 // cout << "playing = "
734 // << globals->get_soundmgr()->is_playing(nav_fx_name)
736 if ( play_count < 4 ) {
738 if ( !globals->get_soundmgr()->is_playing(nav_fx_name) ) {
739 globals->get_soundmgr()->play_once( nav_fx_name );
742 } else if ( play_count < 5 && has_dme ) {
744 if ( !globals->get_soundmgr()->is_playing(nav_fx_name) &&
745 !globals->get_soundmgr()->is_playing(dme_fx_name) ) {
746 globals->get_soundmgr()->play_once( dme_fx_name );
751 globals->get_soundmgr()->stop( nav_fx_name );
752 globals->get_soundmgr()->stop( dme_fx_name );
756 last_loc_dist = loc_dist;
760 // Update current nav/adf radio stations based on current postition
761 void FGNavRadio::search()
765 _time_before_search_sec = 1.0;
767 // cache values locally for speed
768 SGGeod pos = SGGeod::fromDegFt(lon_node->getDoubleValue(),
769 lat_node->getDoubleValue(), alt_node->getDoubleValue());
770 FGNavRecord *nav = NULL;
771 FGNavRecord *loc = NULL;
772 FGNavRecord *dme = NULL;
773 FGNavRecord *gs = NULL;
775 ////////////////////////////////////////////////////////////////////////
777 ////////////////////////////////////////////////////////////////////////
779 double freq = freq_node->getDoubleValue();
780 nav = globals->get_navlist()->findByFreq(freq, pos);
781 dme = globals->get_dmelist()->findByFreq(freq, pos);
783 loc = globals->get_loclist()->findByFreq(freq, pos);
784 gs = globals->get_gslist()->findByFreq(freq, pos);
790 nav_id = loc->get_ident();
791 nav_id_node->setStringValue( nav_id.c_str() );
792 // cout << "localizer = " << nav_id_node->getStringValue() << endl;
794 if ( last_nav_id != nav_id || last_nav_vor ) {
795 trans_ident = loc->get_trans_ident();
796 target_radial = loc->get_multiuse();
797 while ( target_radial < 0.0 ) { target_radial += 360.0; }
798 while ( target_radial > 360.0 ) { target_radial -= 360.0; }
799 loc_lon = loc->get_lon();
800 loc_lat = loc->get_lat();
801 nav_xyz = loc->cart();
802 last_nav_id = nav_id;
803 last_nav_vor = false;
804 loc_node->setBoolValue( true );
805 has_dme = (dme != NULL);
807 has_gs_node->setBoolValue( true );
808 gs_lon = gs->get_lon();
809 gs_lat = gs->get_lat();
810 nav_elev = gs->get_elev_ft();
811 int tmp = (int)(gs->get_multiuse() / 1000.0);
812 target_gs = (double)tmp / 100.0;
815 // derive GS baseline (perpendicular to the runay
817 double tlon, tlat, taz;
818 geo_direct_wgs_84 ( 0.0, gs_lat, gs_lon,
820 100.0, &tlat, &tlon, &taz );
821 // cout << "target_radial = " << target_radial << endl;
822 // cout << "nav_loc = " << loc_node->getBoolValue() << endl;
823 // cout << gs_lon << "," << gs_lat << " "
824 // << tlon << "," << tlat << " (" << nav_elev << ")"
826 SGGeod tpos = SGGeod::fromDegFt(tlon, tlat, nav_elev);
827 SGVec3d p1 = SGVec3d::fromGeod(tpos);
829 // cout << gs_xyz << endl;
830 // cout << p1 << endl;
831 gs_base_vec = p1 - gs_xyz;
832 // cout << gs_base_vec << endl;
834 has_gs_node->setBoolValue( false );
835 nav_elev = loc->get_elev_ft();
838 range = FG_LOC_DEFAULT_RANGE;
839 effective_range = range;
841 if ( globals->get_soundmgr()->exists( nav_fx_name ) ) {
842 globals->get_soundmgr()->remove( nav_fx_name );
844 SGSoundSample *sound;
845 sound = morse.make_ident( trans_ident, LO_FREQUENCY );
846 sound->set_volume( 0.3 );
847 globals->get_soundmgr()->add( sound, nav_fx_name );
849 if ( globals->get_soundmgr()->exists( dme_fx_name ) ) {
850 globals->get_soundmgr()->remove( dme_fx_name );
852 sound = morse.make_ident( trans_ident, HI_FREQUENCY );
853 sound->set_volume( 0.3 );
854 globals->get_soundmgr()->add( sound, dme_fx_name );
856 int offset = (int)(sg_random() * 30.0);
857 play_count = offset / 4;
858 last_time = globals->get_time_params()->get_cur_time() -
860 // cout << "offset = " << offset << " play_count = "
862 // << " last_time = " << last_time
863 // << " current time = "
864 // << globals->get_time_params()->get_cur_time() << endl;
866 // cout << "Found an loc station in range" << endl;
867 // cout << " id = " << loc->get_locident() << endl;
869 } else if ( nav != NULL ) {
870 nav_id = nav->get_ident();
871 nav_id_node->setStringValue( nav_id.c_str() );
872 // cout << "nav = " << nav_id << endl;
874 if ( last_nav_id != nav_id || !last_nav_vor ) {
875 last_nav_id = nav_id;
877 trans_ident = nav->get_trans_ident();
878 loc_node->setBoolValue( false );
879 has_dme = (dme != NULL);
880 has_gs_node->setBoolValue( false );
881 loc_lon = nav->get_lon();
882 loc_lat = nav->get_lat();
883 nav_elev = nav->get_elev_ft();
884 twist = nav->get_multiuse();
885 range = nav->get_range();
886 effective_range = adjustNavRange(nav_elev, pos.getElevationM(), range);
888 target_radial = sel_radial_node->getDoubleValue();
889 nav_xyz = nav->cart();
891 if ( globals->get_soundmgr()->exists( nav_fx_name ) ) {
892 globals->get_soundmgr()->remove( nav_fx_name );
895 SGSoundSample *sound;
896 sound = morse.make_ident( trans_ident, LO_FREQUENCY );
897 sound->set_volume( 0.3 );
898 if ( globals->get_soundmgr()->add( sound, nav_fx_name ) ) {
899 // cout << "Added nav-vor-ident sound" << endl;
901 SG_LOG(SG_COCKPIT, SG_WARN, "Failed to add v1-vor-ident sound");
904 if ( globals->get_soundmgr()->exists( dme_fx_name ) ) {
905 globals->get_soundmgr()->remove( dme_fx_name );
907 sound = morse.make_ident( trans_ident, HI_FREQUENCY );
908 sound->set_volume( 0.3 );
909 globals->get_soundmgr()->add( sound, dme_fx_name );
911 int offset = (int)(sg_random() * 30.0);
912 play_count = offset / 4;
913 last_time = globals->get_time_params()->get_cur_time() - offset;
914 // cout << "offset = " << offset << " play_count = "
915 // << play_count << " last_time = "
916 // << last_time << " current time = "
917 // << globals->get_time_params()->get_cur_time() << endl;
919 // cout << "Found a vor station in range" << endl;
920 // cout << " id = " << nav->get_ident() << endl;
921 } catch ( sg_io_exception &e ) {
922 SG_LOG(SG_GENERAL, SG_ALERT, e.getFormattedMessage());
927 nav_id_node->setStringValue( "" );
931 globals->get_soundmgr()->remove( nav_fx_name );
932 globals->get_soundmgr()->remove( dme_fx_name );
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] );