]> git.mxchange.org Git - flightgear.git/blobdiff - src/Instrumentation/navradio.cxx
Pull Sound-manager out of FGGlobals
[flightgear.git] / src / Instrumentation / navradio.cxx
index 076839c36362ab147b1df84202c9f3a2e8e6b735..073e28621429d085fdf99d366a31770e8f368e67 100644 (file)
 #  include <config.h>
 #endif
 
-#include "navradio.hxx"
-
 #include <sstream>
 #include <cstring>
+#include <cstdio>
 
 #include <simgear/sg_inlines.h>
 #include <simgear/timing/sg_time.hxx>
@@ -36,6 +35,7 @@
 #include <simgear/structure/exception.hxx>
 #include <simgear/math/interpolater.hxx>
 #include <simgear/misc/strutils.hxx>
+#include <simgear/sound/sample_group.hxx>
 
 #include <Navaids/navrecord.hxx>
 #include <Sound/audioident.hxx>
@@ -43,6 +43,8 @@
 #include <Navaids/navlist.hxx>
 #include <Main/util.hxx>
 
+#include "navradio.hxx"
+
 using std::string;
 
 // General-purpose sawtooth function.  Graph looks like this:
@@ -96,14 +98,15 @@ SGPropertyNode_ptr createServiceableProp(SGPropertyNode* aParent,
   return n;  
 }
 
+static std::auto_ptr<SGInterpTable> static_terminalRangeInterp,
+  static_lowRangeInterp, static_highRangeInterp;
+
 // Constructor
 FGNavRadio::FGNavRadio(SGPropertyNode *node) :
-    term_tbl(NULL),
-    low_tbl(NULL),
-    high_tbl(NULL),
     _operable(false),
     play_count(0),
-    last_time(0),
+    _nav_search(true),
+    _last_freq(0.0),
     target_radial(0.0),
     effective_range(0.0),
     target_gs(0.0),
@@ -127,18 +130,21 @@ FGNavRadio::FGNavRadio(SGPropertyNode *node) :
     _gsNeedleDeflectionNorm(0.0),
     _audioIdent(NULL)
 {
-    SGPath path( globals->get_fg_root() );
-    SGPath term = path;
-    term.append( "Navaids/range.term" );
-    SGPath low = path;
-    low.append( "Navaids/range.low" );
-    SGPath high = path;
-    high.append( "Navaids/range.high" );
-
-    term_tbl = new SGInterpTable( term.str() );
-    low_tbl = new SGInterpTable( low.str() );
-    high_tbl = new SGInterpTable( high.str() );
-
+    if (!static_terminalRangeInterp.get()) {
+    // one-time interpolator init
+      SGPath path( globals->get_fg_root() );
+      SGPath term = path;
+      term.append( "Navaids/range.term" );
+      SGPath low = path;
+      low.append( "Navaids/range.low" );
+      SGPath high = path;
+      high.append( "Navaids/range.high" );
+      
+      static_terminalRangeInterp.reset(new SGInterpTable(term.str()));
+      static_lowRangeInterp.reset(new SGInterpTable(low.str()));
+      static_highRangeInterp.reset(new SGInterpTable(high.str()));
+    }
+  
     string branch("/instrumentation/" + _name);
     _radio_node = fgGetNode(branch.c_str(), _num, true);
 }
@@ -155,10 +161,6 @@ FGNavRadio::~FGNavRadio()
       nav_slaved_to_gps_node->removeChangeListener(this);
     }
     
-    delete term_tbl;
-    delete low_tbl;
-    delete high_tbl;
-
     delete _audioIdent;
 }
 
@@ -168,7 +170,7 @@ FGNavRadio::init ()
 {
     SGPropertyNode* node = _radio_node.get();
     bus_power_node = 
-       fgGetNode(("/systems/electrical/outputs/" + _name).c_str(), true);
+        fgGetNode(("/systems/electrical/outputs/" + _name).c_str(), true);
 
     // inputs
     is_valid_node = node->getChild("data-is-valid", 0, true);
@@ -201,6 +203,7 @@ FGNavRadio::init ()
     alt_freq_node = subnode->getChild("standby-mhz", 0, true);
     fmt_freq_node = subnode->getChild("selected-mhz-fmt", 0, true);
     fmt_alt_freq_node = subnode->getChild("standby-mhz-fmt", 0, true);
+    is_loc_freq_node = subnode->getChild("is-localizer-frequency", 0, true );
 
     // radials
     subnode = node->getChild("radials", 0, true);
@@ -266,6 +269,12 @@ FGNavRadio::init ()
     node->getNode( "dme-in-range", true )->alias( fgGetNode("/instrumentation/dme[0]/in-range", true ) );
 }
 
+void
+FGNavRadio::reinit ()
+{
+    _time_before_search_sec = -1.0;
+}
+
 void
 FGNavRadio::bind ()
 {
@@ -302,17 +311,17 @@ double FGNavRadio::adjustNavRange( double stationElev, double aircraftElev,
     //      << " station elev = " << stationElev << endl;
 
     if ( nominalRange < 25.0 + SG_EPSILON ) {
-       // Standard Terminal Service Volume
-       return term_tbl->interpolate( alt ) * usability_factor;
+      // Standard Terminal Service Volume
+      return static_terminalRangeInterp->interpolate( alt ) * usability_factor;
     } else if ( nominalRange < 50.0 + SG_EPSILON ) {
        // Standard Low Altitude Service Volume
        // table is based on range of 40, scale to actual range
-       return low_tbl->interpolate( alt ) * nominalRange / 40.0
+      return static_lowRangeInterp->interpolate( alt ) * nominalRange / 40.0
            * usability_factor;
     } else {
        // Standard High Altitude Service Volume
        // table is based on range of 130, scale to actual range
-       return high_tbl->interpolate( alt ) * nominalRange / 130.0
+      return static_highRangeInterp->interpolate( alt ) * nominalRange / 130.0
            * usability_factor;
     }
 }
@@ -346,6 +355,16 @@ double FGNavRadio::adjustILSRange( double stationElev, double aircraftElev,
     return FG_LOC_DEFAULT_RANGE;
 }
 
+// Frequencies with odd 100kHz numbers in the range from 108.00 - 111.95
+// are LOC/GS (ILS) frequency pairs
+// (108.00, 108.05, 108.20, 108.25.. =VOR)
+// (108.10, 108.15, 108.30, 108.35.. =ILS)
+static inline bool IsLocalizerFrequency( double f )
+{
+  if( f < 108.0 || f >= 112.00 ) return false;
+  return (((SGMiscd::roundToInt(f * 100.0) % 100)/10) % 2) != 0;
+}
+
 
 //////////////////////////////////////////////////////////////////////////
 // Update the various nav values based on position and valid tuned in navs
@@ -364,6 +383,7 @@ FGNavRadio::update(double dt)
   fmt_freq_node->setStringValue(tmp);
   sprintf( tmp, "%.2f", alt_freq_node->getDoubleValue() );
   fmt_alt_freq_node->setStringValue(tmp);
+  is_loc_freq_node->setBoolValue( IsLocalizerFrequency( freq_node->getDoubleValue() ));
 
   if (power_btn_node->getBoolValue() 
       && (bus_power_node->getDoubleValue() > 1.0)
@@ -441,6 +461,8 @@ void FGNavRadio::updateReceiver(double dt)
     heading_node->setDoubleValue(0.0);
     inrange_node->setBoolValue(false);
     signal_quality_norm_node->setDoubleValue(0.0);
+    gs_dist_node->setDoubleValue( 0.0 );
+    gs_inrange_node->setBoolValue(false);
     return;
   }
 
@@ -550,7 +572,7 @@ void FGNavRadio::updateReceiver(double dt)
     } // of false courses disabled
     
     const double VOR_FULL_ARC = 20.0; // VOR is -10 .. 10 degree swing
-    _cdiDeflection *= VOR_FULL_ARC / _localizerWidth; // increased localiser sensitivity
+    _cdiDeflection *= VOR_FULL_ARC / _localizerWidth; // increased localizer sensitivity
     
     if (backcourse_node->getBoolValue()) {
       _cdiDeflection = -_cdiDeflection;
@@ -561,12 +583,12 @@ void FGNavRadio::updateReceiver(double dt)
       r = ( r<0.0 ? -r-180.0 : -r+180.0 );
     }
     _cdiDeflection = r;
-  } // of non-localiser case
+  } // of non-localizer case
   
   SG_CLAMP_RANGE(_cdiDeflection, -10.0, 10.0 );
   _cdiDeflection *= signal_quality_norm;
   
-  // cross-track error (in metres)
+  // cross-track error (in meters)
   _cdiCrossTrackErrorM = loc_dist * sin(r * SGD_DEGREES_TO_RADIANS);
   
   updateGlideSlope(dt, aircraft, signal_quality_norm);
@@ -574,22 +596,25 @@ void FGNavRadio::updateReceiver(double dt)
 
 void FGNavRadio::updateGlideSlope(double dt, const SGVec3d& aircraft, double signal_quality_norm)
 {
-  _gsNeedleDeflection = 0.0;
-  if (!_gs || !inrange_node->getBoolValue()) {
-    gs_dist_node->setDoubleValue( 0.0 );
-    gs_inrange_node->setBoolValue(false);
+  bool gsInRange = (_gs && inrange_node->getBoolValue());
+  double gsDist = 0;
+
+  if (gsInRange)
+  {
+    gsDist = dist(aircraft, _gsCart);
+    gsInRange = (gsDist < (_gs->get_range() * SG_NM_TO_METER));
+  }
+
+  gs_inrange_node->setBoolValue(gsInRange);
+  gs_dist_node->setDoubleValue( gsDist );
+
+  if (!gsInRange)
+  {
     _gsNeedleDeflection = 0.0;
     _gsNeedleDeflectionNorm = 0.0;
     return;
   }
   
-  double gsDist = dist(aircraft, _gsCart);
-  gs_dist_node->setDoubleValue(gsDist);
-  bool gsInRange = (gsDist < (_gs->get_range() * SG_NM_TO_METER));
-  gs_inrange_node->setBoolValue(gsInRange);
-        
-  if (!gsInRange) return;
-  
   SGVec3d pos = aircraft - _gsCart; // relative vector from gs antenna to aircraft
   // The positive GS axis points along the runway in the landing direction,
   // toward the far end, not toward the approach area, so we need a - sign here:
@@ -780,7 +805,7 @@ void FGNavRadio::updateCDI(double dt)
   SG_CLAMP_RANGE( adjustment, -30.0, 30.0 );
 
   // determine the target heading to fly to intercept the
-  // tgt_radial = target radial (true) + cdi offset adjustmest -
+  // tgt_radial = target radial (true) + cdi offset adjustment -
   // xtrack heading error adjustment
   double nta_hdg;
   double trtrue = target_radial_true_node->getDoubleValue();
@@ -848,26 +873,60 @@ void FGNavRadio::updateAudio( double dt )
 
 FGNavRecord* FGNavRadio::findPrimaryNavaid(const SGGeod& aPos, double aFreqMHz)
 {
-  FGNavRecord* nav = globals->get_navlist()->findByFreq(aFreqMHz, aPos);
-  if (nav) {
-    return nav;
-  }
-  
-  return globals->get_loclist()->findByFreq(aFreqMHz, aPos);
+  return FGNavList::findByFreq(aFreqMHz, aPos, FGNavList::navFilter());
 }
 
-// Update current nav/adf radio stations based on current postition
+// Update current nav/adf radio stations based on current position
 void FGNavRadio::search() 
 {
+  // set delay for next search
   _time_before_search_sec = 1.0;
+
   double freq = freq_node->getDoubleValue();
-  
-  FGNavRecord* nav = findPrimaryNavaid(globals->get_aircraft_position(), freq);
-  if (nav == _navaid) {
-    return; // found the same as last search, we're done
+
+  // immediate NAV search when frequency has changed (toggle between nav and g/s search otherwise)
+  _nav_search |= (_last_freq != freq);
+
+  // do we need to search a new NAV station in this iteration?
+  if (_nav_search)
+  {
+      _last_freq = freq;
+      FGNavRecord* nav = findPrimaryNavaid(globals->get_aircraft_position(), freq);
+      if (nav == _navaid) {
+        if (nav && (nav->type() != FGPositioned::VOR))
+            _nav_search = false;  // search glideslope on next iteration
+        return; // nav hasn't changed, we're done
+      }
+      // remember new navaid station
+      _navaid = nav;
   }
-  
-  _navaid = nav;
+
+  // search glideslope station
+  if ((_navaid.valid()) && (_navaid->type() != FGPositioned::VOR))
+  {
+    FGNavList::TypeFilter gsFilter(FGPositioned::GS);
+    FGNavRecord* gs = FGNavList::findByFreq(freq, globals->get_aircraft_position(),
+                                           &gsFilter);
+      if ((!_nav_search) && (gs == _gs))
+      {
+          _nav_search = true; // search NAV on next iteration
+          return; // g/s hasn't changed, neither has nav - we're done
+      }
+      // remember new glideslope station
+      _gs = gs;
+  }
+
+  _nav_search = true; // search NAV on next iteration
+
+  // nav or gs station has changed
+  updateNav();
+}
+
+// Update current nav/adf/glideslope outputs when station has changed
+void FGNavRadio::updateNav()
+{
+  // update necessary, nav and/or gs has changed
+  FGNavRecord* nav = _navaid;
   string identBuffer(4, ' ');
   if (nav) {
     nav_id_node->setStringValue(nav->get_ident());
@@ -880,17 +939,14 @@ void FGNavRadio::search()
     if (nav->type() == FGPositioned::VOR) {
       target_radial = sel_radial_node->getDoubleValue();
       _gs = NULL;
-      has_gs_node->setBoolValue(false);
     } else { // ILS or LOC
-      _gs = globals->get_gslist()->findByFreq(freq, globals->get_aircraft_position());
-      has_gs_node->setBoolValue(_gs != NULL);
       _localizerWidth = nav->localizerWidth();
       twist = 0.0;
-           effective_range = nav->get_range();
+      effective_range = nav->get_range();
       
       target_radial = nav->get_multiuse();
       SG_NORMALIZE_RANGE(target_radial, 0.0, 360.0);
-      
+
       if (_gs) {
         int tmp = (int)(_gs->get_multiuse() / 1000.0);
         target_gs = (double)tmp / 100.0;
@@ -904,7 +960,7 @@ void FGNavRadio::search()
         _gsAxis = tangentVector(_gs->geod(), gs_radial);
 
         // GS baseline unit tangent vector
-        // (transverse to the runay along the ground)
+        // (transverse to the runway along the ground)
         _gsBaseline = tangentVector(_gs->geod(), gs_radial + 90.0);
         _gsVertical = cross(_gsBaseline, _gsAxis);
       } // of have glideslope
@@ -914,10 +970,10 @@ void FGNavRadio::search()
     _gs = NULL;
     nav_id_node->setStringValue("");
     loc_node->setBoolValue(false);
-    has_gs_node->setBoolValue(false);
     _audioIdent->setIdent("", 0.0 );
   }
 
+  has_gs_node->setBoolValue(_gs != NULL);
   is_valid_node->setBoolValue(nav != NULL);
   id_c1_node->setIntValue( (int)identBuffer[0] );
   id_c2_node->setIntValue( (int)identBuffer[1] );