]> git.mxchange.org Git - flightgear.git/blobdiff - src/Instrumentation/kr_87.cxx
Reintroduce an old bugture: use localizer as dme
[flightgear.git] / src / Instrumentation / kr_87.cxx
index aa2ffab904636ff60011dbb37311da3caa26d104..0fa58012ecad43bfcc3fa86a86950eaa0800be6a 100644 (file)
@@ -18,7 +18,6 @@
 // along with this program; if not, write to the Free Software
 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 //
-// $Id$
 
 
 #ifdef HAVE_CONFIG_H
 
 #include <simgear/compiler.h>
 #include <simgear/math/sg_random.h>
+#include <simgear/math/sg_geodesy.hxx>
+#include <simgear/timing/sg_time.hxx>
 
-#include <Aircraft/aircraft.hxx>
 #include <Navaids/navlist.hxx>
 
 #include "kr_87.hxx"
 
+#include <Sound/morse.hxx>
 #include <string>
-SG_USING_STD(string);
+using std::string;
 
 static int play_count = 0;
 static time_t last_time = 0;
@@ -69,9 +70,6 @@ static double kludgeRange ( double stationElev, double aircraftElev,
 
 // Constructor
 FGKR_87::FGKR_87( SGPropertyNode *node ) :
-    lon_node(fgGetNode("/position/longitude-deg", true)),
-    lat_node(fgGetNode("/position/latitude-deg", true)),
-    alt_node(fgGetNode("/position/altitude-ft", true)),
     bus_power(fgGetNode("/systems/electrical/outputs/adf", true)),
     serviceable(fgGetNode("/instrumentation/adf/serviceable", true)),
     need_update(true),
@@ -103,7 +101,8 @@ FGKR_87::FGKR_87( SGPropertyNode *node ) :
     flight_timer(0.0),
     elapsed_timer(0.0),
     tmp_timer(0.0),
-    _time_before_search_sec(0)
+    _time_before_search_sec(0),
+    _sgr(NULL)
 {
 }
 
@@ -114,7 +113,9 @@ FGKR_87::~FGKR_87() {
 
 
 void FGKR_87::init () {
-    morse.init();
+    SGSoundMgr *smgr = globals->get_soundmgr();
+    _sgr = smgr->find("avionics", true);
+    _sgr->tie_to_listener();
 }
 
 
@@ -244,14 +245,10 @@ void FGKR_87::unbind () {
 
 // Update the various nav values based on position and valid tuned in navs
 void FGKR_87::update( double dt_sec ) {
-    double acft_lon = lon_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
-    double acft_lat = lat_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
-    double acft_elev = alt_node->getDoubleValue() * SG_FEET_TO_METER;
+    SGGeod acft = globals->get_aircraft_position();
 
     need_update = false;
 
-    Point3D aircraft = sgGeodToCart( Point3D( acft_lon, acft_lat, acft_elev ) );
-    Point3D station;
     double az1, az2, s;
 
     // On timeout, scan again
@@ -361,20 +358,17 @@ void FGKR_87::update( double dt_sec ) {
         if ( valid ) {
             // cout << "adf is valid" << endl;
             // staightline distance
-            station = Point3D( x, y, z );
-            dist = aircraft.distance3D( station );
+            // What a hack, dist is a class local variable
+            dist = sqrt(distSqr(SGVec3d::fromGeod(acft), xyz));
 
             // wgs84 heading
-            geo_inverse_wgs_84( acft_elev,
-                                acft_lat * SGD_RADIANS_TO_DEGREES,
-                                acft_lon * SGD_RADIANS_TO_DEGREES, 
-                                stn_lat, stn_lon,
+            geo_inverse_wgs_84( acft, SGGeod::fromDeg(stn_lon, stn_lat),
                                 &az1, &az2, &s );
             heading = az1;
             // cout << " heading = " << heading
             //      << " dist = " << dist << endl;
 
-            effective_range = kludgeRange(stn_elev, acft_elev, range);
+            effective_range = kludgeRange(stn_elev, acft.getElevationFt(), range);
             if ( dist < effective_range * SG_NM_TO_METER ) {
                 inrange = true;
             } else if ( dist < 2 * effective_range * SG_NM_TO_METER ) {
@@ -466,7 +460,7 @@ void FGKR_87::update( double dt_sec ) {
        // otherwise turn it off
        if ( vol_btn >= 0.01 && audio_btn ) {
            SGSoundSample *sound;
-           sound = globals->get_soundmgr()->find( "adf-ident" );
+           sound = _sgr->find( "adf-ident" );
             if ( sound != NULL ) {
                 if ( !adf_btn ) {
                     sound->set_volume( vol_btn );
@@ -483,13 +477,13 @@ void FGKR_87::update( double dt_sec ) {
            }
            if ( play_count < 4 ) {
                // play ADF ident
-               if ( !globals->get_soundmgr()->is_playing("adf-ident") ) {
-                   globals->get_soundmgr()->play_once( "adf-ident" );
+               if ( !_sgr->is_playing("adf-ident") && (vol_btn > 0.05) ) {
+                   _sgr->play_once( "adf-ident" );
                    ++play_count;
                }
            }
        } else {
-           globals->get_soundmgr()->stop( "adf-ident" );
+           _sgr->stop( "adf-ident" );
        }
     }
 }
@@ -497,10 +491,8 @@ void FGKR_87::update( double dt_sec ) {
 
 // Update current nav/adf radio stations based on current postition
 void FGKR_87::search() {
-    double acft_lon = lon_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
-    double acft_lat = lat_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
-    double acft_elev = alt_node->getDoubleValue() * SG_FEET_TO_METER;
-
+  SGGeod pos = globals->get_aircraft_position();
+  
                                // FIXME: the panel should handle this
     static string last_ident = "";
 
@@ -511,9 +503,8 @@ void FGKR_87::search() {
     // ADF.
     ////////////////////////////////////////////////////////////////////////
 
-    FGNavRecord *adf
-        = globals->get_navlist()->findByFreq( freq, acft_lon, acft_lat,
-                                              acft_elev );
+  
+    FGNavRecord *adf = globals->get_navlist()->findByFreq( freq, pos);
     if ( adf != NULL ) {
        char sfreq[128];
        snprintf( sfreq, 10, "%d", freq );
@@ -529,18 +520,16 @@ void FGKR_87::search() {
            stn_lat = adf->get_lat();
            stn_elev = adf->get_elev_ft();
            range = adf->get_range();
-           effective_range = kludgeRange(stn_elev, acft_elev, range);
-           x = adf->get_x();
-           y = adf->get_y();
-           z = adf->get_z();
+           effective_range = kludgeRange(stn_elev, pos.getElevationM(), range);
+           xyz = adf->cart();
 
-           if ( globals->get_soundmgr()->exists( "adf-ident" ) ) {
-               globals->get_soundmgr()->remove( "adf-ident" );
+           if ( _sgr->exists( "adf-ident" ) ) {
+               _sgr->remove( "adf-ident" );
            }
            SGSoundSample *sound;
-           sound = morse.make_ident( trans_ident, LO_FREQUENCY );
+        sound = FGMorse::instance()->make_ident( trans_ident, FGMorse::LO_FREQUENCY );
            sound->set_volume( 0.3 );
-           globals->get_soundmgr()->add( sound, "adf-ident" );
+           _sgr->add( sound, "adf-ident" );
 
            int offset = (int)(sg_random() * 30.0);
            play_count = offset / 4;
@@ -558,7 +547,7 @@ void FGKR_87::search() {
        valid = false;
        ident = "";
        trans_ident = "";
-       globals->get_soundmgr()->remove( "adf-ident" );
+       _sgr->remove( "adf-ident" );
        last_ident = "";
        // cout << "not picking up adf. :-(" << endl;
     }