]> git.mxchange.org Git - flightgear.git/blobdiff - src/Instrumentation/kr_87.cxx
Autopilot: clean up the helpers code (which drives the various /internal/) properties...
[flightgear.git] / src / Instrumentation / kr_87.cxx
index fcdf8dfbe96af9e5b41d8a1a7c3da28267605cfb..6683f8f935b563f3d784eb6991051836cba7fa23 100644 (file)
@@ -16,7 +16,7 @@
 //
 // You should have received a copy of the GNU General Public License
 // along with this program; if not, write to the Free Software
-// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 //
 // $Id$
 
@@ -29,6 +29,8 @@
 
 #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>
@@ -36,7 +38,7 @@
 #include "kr_87.hxx"
 
 #include <string>
-SG_USING_STD(string);
+using std::string;
 
 static int play_count = 0;
 static time_t last_time = 0;
@@ -120,128 +122,136 @@ void FGKR_87::init () {
 
 void FGKR_87::bind () {
     // internal values
-    fgTie("/radios/kr-87/internal/valid", this, &FGKR_87::get_valid);
-    fgTie("/radios/kr-87/internal/inrange", this, &FGKR_87::get_inrange);
-    fgTie("/radios/kr-87/internal/dist", this, &FGKR_87::get_dist);
-    fgTie("/radios/kr-87/internal/heading", this, &FGKR_87::get_heading);
+    fgTie("/instrumentation/kr-87/internal/valid", this, &FGKR_87::get_valid);
+    fgTie("/instrumentation/kr-87/internal/inrange", this,
+          &FGKR_87::get_inrange);
+    fgTie("/instrumentation/kr-87/internal/dist", this,
+          &FGKR_87::get_dist);
+    fgTie("/instrumentation/kr-87/internal/heading", this,
+          &FGKR_87::get_heading);
 
     // modes
-    fgTie("/radios/kr-87/modes/ant", this,
+    fgTie("/instrumentation/kr-87/modes/ant", this,
          &FGKR_87::get_ant_mode);
-    fgTie("/radios/kr-87/modes/stby", this,
+    fgTie("/instrumentation/kr-87/modes/stby", this,
          &FGKR_87::get_stby_mode);
-    fgTie("/radios/kr-87/modes/timer", this,
+    fgTie("/instrumentation/kr-87/modes/timer", this,
          &FGKR_87::get_timer_mode);
-    fgTie("/radios/kr-87/modes/count", this,
+    fgTie("/instrumentation/kr-87/modes/count", this,
          &FGKR_87::get_count_mode);
 
     // input and buttons
-    fgTie("/radios/kr-87/inputs/rotation-deg", this,
+    fgTie("/instrumentation/kr-87/inputs/rotation-deg", this,
          &FGKR_87::get_rotation, &FGKR_87::set_rotation);
-    fgSetArchivable("/radios/kr-87/inputs/rotation-deg");
-    fgTie("/radios/kr-87/inputs/power-btn", this,
+    fgSetArchivable("/instrumentation/kr-87/inputs/rotation-deg");
+    fgTie("/instrumentation/kr-87/inputs/power-btn", this,
          &FGKR_87::get_power_btn,
          &FGKR_87::set_power_btn);
-    fgSetArchivable("/radios/kr-87/inputs/power-btn");
-    fgTie("/radios/kr-87/inputs/audio-btn", this,
+    fgSetArchivable("/instrumentation/kr-87/inputs/power-btn");
+    fgTie("/instrumentation/kr-87/inputs/audio-btn", this,
          &FGKR_87::get_audio_btn,
          &FGKR_87::set_audio_btn);
-    fgSetArchivable("/radios/kr-87/inputs/audio-btn");
-    fgTie("/radios/kr-87/inputs/volume", this,
+    fgSetArchivable("/instrumentation/kr-87/inputs/audio-btn");
+    fgTie("/instrumentation/kr-87/inputs/volume", this,
          &FGKR_87::get_vol_btn,
          &FGKR_87::set_vol_btn);
-    fgSetArchivable("/radios/kr-87/inputs/volume");
-    fgTie("/radios/kr-87/inputs/adf-btn", this,
+    fgSetArchivable("/instrumentation/kr-87/inputs/volume");
+    fgTie("/instrumentation/kr-87/inputs/adf-btn", this,
          &FGKR_87::get_adf_btn,
          &FGKR_87::set_adf_btn);
-    fgTie("/radios/kr-87/inputs/bfo-btn", this,
+    fgTie("/instrumentation/kr-87/inputs/bfo-btn", this,
          &FGKR_87::get_bfo_btn,
          &FGKR_87::set_bfo_btn);
-    fgTie("/radios/kr-87/inputs/frq-btn", this,
+    fgTie("/instrumentation/kr-87/inputs/frq-btn", this,
          &FGKR_87::get_frq_btn,
          &FGKR_87::set_frq_btn);
-    fgTie("/radios/kr-87/inputs/flt-et-btn", this,
+    fgTie("/instrumentation/kr-87/inputs/flt-et-btn", this,
          &FGKR_87::get_flt_et_btn,
          &FGKR_87::set_flt_et_btn);
-    fgTie("/radios/kr-87/inputs/set-rst-btn", this,
+    fgTie("/instrumentation/kr-87/inputs/set-rst-btn", this,
          &FGKR_87::get_set_rst_btn,
          &FGKR_87::set_set_rst_btn);
 
     // outputs
-    fgTie("/radios/kr-87/outputs/selected-khz", this,
+    fgTie("/instrumentation/kr-87/outputs/selected-khz", this,
          &FGKR_87::get_freq, &FGKR_87::set_freq);
-    fgSetArchivable("/radios/kr-87/outputs/selected-khz");
-    fgTie("/radios/kr-87/outputs/standby-khz", this,
+    fgSetArchivable("/instrumentation/kr-87/outputs/selected-khz");
+    fgTie("/instrumentation/kr-87/outputs/standby-khz", this,
          &FGKR_87::get_stby_freq, &FGKR_87::set_stby_freq);
-    fgSetArchivable("/radios/kr-87/outputs/standby-khz");
-    fgTie("/radios/kr-87/outputs/needle-deg", this,
+    fgSetArchivable("/instrumentation/kr-87/outputs/standby-khz");
+    fgTie("/instrumentation/kr-87/outputs/needle-deg", this,
          &FGKR_87::get_needle_deg);
-    fgTie("/radios/kr-87/outputs/flight-timer", this, &FGKR_87::get_flight_timer);
-    fgTie("/radios/kr-87/outputs/elapsed-timer", this,
+    fgTie("/instrumentation/kr-87/outputs/flight-timer", this,
+          &FGKR_87::get_flight_timer);
+    fgTie("/instrumentation/kr-87/outputs/elapsed-timer", this,
           &FGKR_87::get_elapsed_timer,
           &FGKR_87::set_elapsed_timer);
 
     // annunciators
-    fgTie("/radios/kr-87/annunciators/ant", this, &FGKR_87::get_ant_ann );
-    fgTie("/radios/kr-87/annunciators/adf", this, &FGKR_87::get_adf_ann );
-    fgTie("/radios/kr-87/annunciators/bfo", this, &FGKR_87::get_bfo_ann );
-    fgTie("/radios/kr-87/annunciators/frq", this, &FGKR_87::get_frq_ann );
-    fgTie("/radios/kr-87/annunciators/flt", this, &FGKR_87::get_flt_ann );
-    fgTie("/radios/kr-87/annunciators/et", this, &FGKR_87::get_et_ann );
+    fgTie("/instrumentation/kr-87/annunciators/ant", this,
+          &FGKR_87::get_ant_ann );
+    fgTie("/instrumentation/kr-87/annunciators/adf", this,
+          &FGKR_87::get_adf_ann );
+    fgTie("/instrumentation/kr-87/annunciators/bfo", this,
+          &FGKR_87::get_bfo_ann );
+    fgTie("/instrumentation/kr-87/annunciators/frq", this,
+          &FGKR_87::get_frq_ann );
+    fgTie("/instrumentation/kr-87/annunciators/flt", this,
+          &FGKR_87::get_flt_ann );
+    fgTie("/instrumentation/kr-87/annunciators/et", this,
+          &FGKR_87::get_et_ann );
 }
 
 
 void FGKR_87::unbind () {
     // internal values
-    fgUntie("/radios/kr-87/internal/valid");
-    fgUntie("/radios/kr-87/internal/inrange");
-    fgUntie("/radios/kr-87/internal/dist");
-    fgUntie("/radios/kr-87/internal/heading");
+    fgUntie("/instrumentation/kr-87/internal/valid");
+    fgUntie("/instrumentation/kr-87/internal/inrange");
+    fgUntie("/instrumentation/kr-87/internal/dist");
+    fgUntie("/instrumentation/kr-87/internal/heading");
 
     // modes
-    fgUntie("/radios/kr-87/modes/ant");
-    fgUntie("/radios/kr-87/modes/stby");
-    fgUntie("/radios/kr-87/modes/timer");
-    fgUntie("/radios/kr-87/modes/count");
+    fgUntie("/instrumentation/kr-87/modes/ant");
+    fgUntie("/instrumentation/kr-87/modes/stby");
+    fgUntie("/instrumentation/kr-87/modes/timer");
+    fgUntie("/instrumentation/kr-87/modes/count");
 
     // input and buttons
-    fgUntie("/radios/kr-87/inputs/rotation-deg");
-    fgUntie("/radios/kr-87/inputs/power-btn");
-    fgUntie("/radios/kr-87/inputs/volume");
-    fgUntie("/radios/kr-87/inputs/adf-btn");
-    fgUntie("/radios/kr-87/inputs/bfo-btn");
-    fgUntie("/radios/kr-87/inputs/frq-btn");
-    fgUntie("/radios/kr-87/inputs/flt-et-btn");
-    fgUntie("/radios/kr-87/inputs/set-rst-btn");
-    fgUntie("/radios/kr-87/inputs/ident-btn");
+    fgUntie("/instrumentation/kr-87/inputs/rotation-deg");
+    fgUntie("/instrumentation/kr-87/inputs/power-btn");
+    fgUntie("/instrumentation/kr-87/inputs/volume");
+    fgUntie("/instrumentation/kr-87/inputs/adf-btn");
+    fgUntie("/instrumentation/kr-87/inputs/bfo-btn");
+    fgUntie("/instrumentation/kr-87/inputs/frq-btn");
+    fgUntie("/instrumentation/kr-87/inputs/flt-et-btn");
+    fgUntie("/instrumentation/kr-87/inputs/set-rst-btn");
+    fgUntie("/instrumentation/kr-87/inputs/ident-btn");
 
     // outputs
-    fgUntie("/radios/kr-87/outputs/selected-khz");
-    fgUntie("/radios/kr-87/outputs/standby-khz");
-    fgUntie("/radios/kr-87/outputs/needle-deg");
-    fgUntie("/radios/kr-87/outputs/flight-timer");
-    fgUntie("/radios/kr-87/outputs/elapsed-timer");
+    fgUntie("/instrumentation/kr-87/outputs/selected-khz");
+    fgUntie("/instrumentation/kr-87/outputs/standby-khz");
+    fgUntie("/instrumentation/kr-87/outputs/needle-deg");
+    fgUntie("/instrumentation/kr-87/outputs/flight-timer");
+    fgUntie("/instrumentation/kr-87/outputs/elapsed-timer");
 
     // annunciators
-    fgUntie("/radios/kr-87/annunciators/ant");
-    fgUntie("/radios/kr-87/annunciators/adf");
-    fgUntie("/radios/kr-87/annunciators/bfo");
-    fgUntie("/radios/kr-87/annunciators/frq");
-    fgUntie("/radios/kr-87/annunciators/flt");
-    fgUntie("/radios/kr-87/annunciators/et");
+    fgUntie("/instrumentation/kr-87/annunciators/ant");
+    fgUntie("/instrumentation/kr-87/annunciators/adf");
+    fgUntie("/instrumentation/kr-87/annunciators/bfo");
+    fgUntie("/instrumentation/kr-87/annunciators/frq");
+    fgUntie("/instrumentation/kr-87/annunciators/flt");
+    fgUntie("/instrumentation/kr-87/annunciators/et");
 }
 
 
 // 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 = SGGeod::fromDegFt(lon_node->getDoubleValue(),
+                                    lat_node->getDoubleValue(),
+                                    alt_node->getDoubleValue());
 
     need_update = false;
 
-    Point3D aircraft = sgGeodToCart( Point3D( acft_lon, acft_lat, acft_elev ) );
-    Point3D station;
     double az1, az2, s;
 
     // On timeout, scan again
@@ -351,20 +361,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 ) {
@@ -432,7 +439,8 @@ void FGKR_87::update( double dt_sec ) {
     char formatted_timer[128];
     // cout << big << ":" << little << endl;
     snprintf(formatted_timer, 6, "%02d:%02d", big, little);
-    fgSetString( "/radios/kr-87/outputs/timer-string", formatted_timer );
+    fgSetString( "/instrumentation/kr-87/outputs/timer-string",
+                 formatted_timer );
 
     while ( goal_needle_deg < 0.0 ) { goal_needle_deg += 360.0; }
     while ( goal_needle_deg >= 360.0 ) { goal_needle_deg -= 360.0; }
@@ -486,10 +494,9 @@ 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 = SGGeod::fromDegFt(lon_node->getDoubleValue(), 
+    lat_node->getDoubleValue(), alt_node->getDoubleValue());
+  
                                // FIXME: the panel should handle this
     static string last_ident = "";
 
@@ -500,9 +507,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 );
@@ -518,10 +524,8 @@ 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" );