]> git.mxchange.org Git - flightgear.git/blobdiff - src/Cockpit/navcom.cxx
Multiplayer client/server system -- MessageBuf class and test harness complete
[flightgear.git] / src / Cockpit / navcom.cxx
index 3f8f21aee6db7666e3ec13ef1e18a98162a51e9b..a3525f5e4d66f9b750b57f02d9cfe6050308674a 100644 (file)
 
 #include <simgear/compiler.h>
 #include <simgear/math/sg_random.h>
+#include <simgear/math/vector.hxx>
 
 #include <Aircraft/aircraft.hxx>
 #include <Navaids/ilslist.hxx>
 #include <Navaids/navlist.hxx>
-#include <Time/FGEventMgr.hxx>
 
 #include "navcom.hxx"
 
@@ -58,7 +58,7 @@ FGNavCom::FGNavCom() :
     comm_vol_btn(0.0),
     nav_freq(0.0),
     nav_alt_freq(0.0),
-    nav_radial(0.0),
+    nav_target_radial(0.0),
     nav_vol_btn(0.0),
     nav_ident_btn(true)
 {
@@ -94,109 +94,130 @@ FGNavCom::init ()
     // We assume that index is valid now (it must be set before init()
     // is called.)
     char propname[256];
+    // FIXME: Get rid of snprintf
 
-    sprintf( propname, "/systems/electrical/outputs/navcom[%d]", index );
+    snprintf(propname, 256, "/systems/electrical/outputs/navcom[%d]", index);
     // default to true in case no electrical system defined.
     fgSetDouble( propname, 60.0 );
     bus_power = fgGetNode( propname, true );
 
-    sprintf( propname, "/instrumentation/comm[%d]/servicable", index );
-    com_servicable = fgGetNode( propname, true );
-    com_servicable->setBoolValue( true );
+    snprintf(propname, 256, "/instrumentation/comm[%d]/serviceable", index);
+    com_serviceable = fgGetNode( propname, true );
+    com_serviceable->setBoolValue( true );
 
-    sprintf( propname, "/instrumentation/nav[%d]/servicable", index );
-    nav_servicable = fgGetNode( propname, true );
-    nav_servicable->setBoolValue( true );
+    snprintf(propname, 256, "/instrumentation/nav[%d]/serviceable", index);
+    nav_serviceable = fgGetNode( propname, true );
+    nav_serviceable->setBoolValue( true );
 
-    sprintf( propname, "/instrumentation/vor[%d]/cdi/servicable", index );
-    cdi_servicable = fgGetNode( propname, true );
-    cdi_servicable->setBoolValue( true );
+    snprintf(propname, 256, "/instrumentation/vor[%d]/cdi/serviceable", index);
+    cdi_serviceable = fgGetNode( propname, true );
+    cdi_serviceable->setBoolValue( true );
 
-    sprintf( propname, "/instrumentation/vor[%d]/gs/servicable", index );
-    gs_servicable = fgGetNode( propname, true );
-    gs_servicable->setBoolValue( true );
+    snprintf(propname, 256, "/instrumentation/vor[%d]/gs/serviceable", index);
+    gs_serviceable = fgGetNode( propname, true );
+    gs_serviceable->setBoolValue( true );
 
-    sprintf( propname, "/instrumentation/vor[%d]/to-from/servicable", index );
-    tofrom_servicable = fgGetNode( propname, true );
-    tofrom_servicable->setBoolValue( true );
+    snprintf(propname, 256, "/instrumentation/vor[%d]/to-from/serviceable", index);
+    tofrom_serviceable = fgGetNode( propname, true );
+    tofrom_serviceable->setBoolValue( true );
 }
 
 void
 FGNavCom::bind ()
 {
     char propname[256];
+    // FIXME: Get rid of snprintf
 
                                // User inputs
-    sprintf( propname, "/radios/comm[%d]/inputs/power-btn", index );
+    snprintf(propname, 256, "/radios/comm[%d]/inputs/power-btn", index);
     fgTie( propname, this,
            &FGNavCom::get_power_btn, &FGNavCom::set_power_btn );
     fgSetArchivable( propname );
 
-    sprintf( propname, "/radios/comm[%d]/frequencies/selected-mhz", index );
+    snprintf(propname, 256, "/radios/comm[%d]/frequencies/selected-mhz", index);
     fgTie( propname, this, &FGNavCom::get_comm_freq, &FGNavCom::set_comm_freq );
     fgSetArchivable( propname );
 
-    sprintf( propname, "/radios/comm[%d]/frequencies/standby-mhz", index );
+    snprintf(propname, 256, "/radios/comm[%d]/frequencies/standby-mhz", index);
     fgTie( propname, this,
            &FGNavCom::get_comm_alt_freq, &FGNavCom::set_comm_alt_freq );
     fgSetArchivable( propname );
 
-    sprintf( propname, "/radios/comm[%d]/volume", index );
+    snprintf(propname, 256, "/radios/comm[%d]/volume", index);
     fgTie( propname, this,
            &FGNavCom::get_comm_vol_btn, &FGNavCom::set_comm_vol_btn );
     fgSetArchivable( propname );
 
-    sprintf( propname, "/radios/nav[%d]/frequencies/selected-mhz", index );
+    snprintf(propname, 256, "/radios/nav[%d]/frequencies/selected-mhz", index);
     fgTie( propname, this,
          &FGNavCom::get_nav_freq, &FGNavCom::set_nav_freq );
     fgSetArchivable( propname );
 
-    sprintf( propname, "/radios/nav[%d]/frequencies/standby-mhz", index );
+    snprintf(propname, 256, "/radios/nav[%d]/frequencies/standby-mhz", index);
     fgTie( propname , this,
            &FGNavCom::get_nav_alt_freq, &FGNavCom::set_nav_alt_freq);
     fgSetArchivable( propname );
 
-    sprintf( propname, "/radios/nav[%d]/radials/selected-deg", index );
+    snprintf(propname, 256, "/radios/nav[%d]/radials/selected-deg", index);
     fgTie( propname, this,
            &FGNavCom::get_nav_sel_radial, &FGNavCom::set_nav_sel_radial );
     fgSetArchivable( propname );
 
-    sprintf( propname, "/radios/nav[%d]/volume", index );
+    snprintf(propname, 256, "/radios/nav[%d]/volume", index);
     fgTie( propname, this,
            &FGNavCom::get_nav_vol_btn, &FGNavCom::set_nav_vol_btn );
     fgSetArchivable( propname );
 
-    sprintf( propname, "/radios/nav[%d]/ident", index );
+    snprintf(propname, 256, "/radios/nav[%d]/ident", index);
     fgTie( propname, this,
            &FGNavCom::get_nav_ident_btn, &FGNavCom::set_nav_ident_btn );
     fgSetArchivable( propname );
 
                                // Radio outputs
-    sprintf( propname, "/radios/nav[%d]/audio-btn", index );
+    snprintf(propname, 256, "/radios/nav[%d]/audio-btn", index);
     fgTie( propname, this,
            &FGNavCom::get_audio_btn, &FGNavCom::set_audio_btn );
     fgSetArchivable( propname );
 
-    sprintf( propname, "/radios/nav[%d]/radials/actual-deg", index );
+    snprintf(propname, 256, "/radios/nav[%d]/radials/actual-deg", index);
     fgTie( propname,  this, &FGNavCom::get_nav_radial );
 
-    sprintf( propname, "/radios/nav[%d]/to-flag", index );
+    snprintf(propname, 256, "/radios/nav[%d]/to-flag", index);
     fgTie( propname, this, &FGNavCom::get_nav_to_flag );
 
-    sprintf( propname, "/radios/nav[%d]/from-flag", index );
+    snprintf(propname, 256, "/radios/nav[%d]/from-flag", index);
     fgTie( propname, this, &FGNavCom::get_nav_from_flag );
 
-    sprintf( propname, "/radios/nav[%d]/in-range", index );
+    snprintf(propname, 256, "/radios/nav[%d]/in-range", index);
     fgTie( propname, this, &FGNavCom::get_nav_inrange );
 
-    sprintf( propname, "/radios/nav[%d]/heading-needle-deflection", index );
-    fgTie( propname, this, &FGNavCom::get_nav_heading_needle_deflection );
+    snprintf(propname, 256, "/radios/nav[%d]/heading-needle-deflection", index);
+    fgTie( propname, this, &FGNavCom::get_nav_cdi_deflection );
 
-    sprintf( propname, "/radios/nav[%d]/has-gs", index );
+    snprintf(propname, 256, "/radios/nav[%d]/has-gs", index);
     fgTie( propname, this, &FGNavCom::get_nav_has_gs );
 
-    sprintf( propname, "/radios/nav[%d]/gs-needle-deflection", index );
-    fgTie( propname, this, &FGNavCom::get_nav_gs_needle_deflection );
+    snprintf(propname, 256, "/radios/nav[%d]/nav-loc", index);
+    fgTie( propname, this, &FGNavCom::get_nav_loc );
+
+    snprintf(propname, 256, "/radios/nav[%d]/gs-needle-deflection", index);
+    fgTie( propname, this, &FGNavCom::get_nav_gs_deflection );
+
+    snprintf(propname, 256, "/radios/nav[%d]/nav-id", index);
+    fgTie( propname, this, &FGNavCom::get_nav_id );
+
+    // put nav_id characters into seperate properties for instrument displays
+    snprintf(propname, 256, "/radios/nav[%d]/nav-id_asc1", index);
+    fgTie( propname, this, &FGNavCom::get_nav_id_c1 );
+
+    snprintf(propname, 256, "/radios/nav[%d]/nav-id_asc2", index);
+    fgTie( propname, this, &FGNavCom::get_nav_id_c2 );
+
+    snprintf(propname, 256, "/radios/nav[%d]/nav-id_asc3", index);
+    fgTie( propname, this, &FGNavCom::get_nav_id_c3 );
+
+    snprintf(propname, 256, "/radios/nav[%d]/nav-id_asc4", index);
+    fgTie( propname, this, &FGNavCom::get_nav_id_c4 );
 
     // end of binding
 }
@@ -206,33 +227,34 @@ void
 FGNavCom::unbind ()
 {
     char propname[256];
+    // FIXME: Get rid of snprintf
 
-    sprintf( propname, "/radios/comm[%d]/inputs/power-btn", index );
+    snprintf(propname, 256, "/radios/comm[%d]/inputs/power-btn", index);
     fgUntie( propname );
-    sprintf( propname, "/radios/comm[%d]/frequencies/selected-mhz", index );
+    snprintf(propname, 256, "/radios/comm[%d]/frequencies/selected-mhz", index);
     fgUntie( propname );
-    sprintf( propname, "/radios/comm[%d]/frequencies/standby-mhz", index );
+    snprintf(propname, 256, "/radios/comm[%d]/frequencies/standby-mhz", index);
     fgUntie( propname );
 
-    sprintf( propname, "/radios/nav[%d]/frequencies/selected-mhz", index );
+    snprintf(propname, 256, "/radios/nav[%d]/frequencies/selected-mhz", index);
     fgUntie( propname );
-    sprintf( propname, "/radios/nav[%d]/frequencies/standby-mhz", index );
+    snprintf(propname, 256, "/radios/nav[%d]/frequencies/standby-mhz", index);
     fgUntie( propname );
-    sprintf( propname, "/radios/nav[%d]/radials/actual-deg", index );
+    snprintf(propname, 256, "/radios/nav[%d]/radials/actual-deg", index);
     fgUntie( propname );
-    sprintf( propname, "/radios/nav[%d]/radials/selected-deg", index );
+    snprintf(propname, 256, "/radios/nav[%d]/radials/selected-deg", index);
     fgUntie( propname );
-    sprintf( propname, "/radios/nav[%d]/ident", index );
+    snprintf(propname, 256, "/radios/nav[%d]/ident", index);
     fgUntie( propname );
-    sprintf( propname, "/radios/nav[%d]/to-flag", index );
+    snprintf(propname, 256, "/radios/nav[%d]/to-flag", index);
     fgUntie( propname );
-    sprintf( propname, "/radios/nav[%d]/from-flag", index );
+    snprintf(propname, 256, "/radios/nav[%d]/from-flag", index);
     fgUntie( propname );
-    sprintf( propname, "/radios/nav[%d]/in-range", index );
+    snprintf(propname, 256, "/radios/nav[%d]/in-range", index);
     fgUntie( propname );
-    sprintf( propname, "/radios/nav[%d]/heading-needle-deflection", index );
+    snprintf(propname, 256, "/radios/nav[%d]/heading-needle-deflection", index);
     fgUntie( propname );
-    sprintf( propname, "/radios/nav[%d]/gs-needle-deflection", index );
+    snprintf(propname, 256, "/radios/nav[%d]/gs-needle-deflection", index);
     fgUntie( propname );
 }
 
@@ -319,21 +341,32 @@ FGNavCom::update(double dt)
     ////////////////////////////////////////////////////////////////////////
 
     if ( nav_valid && power_btn && (bus_power->getDoubleValue() > 1.0)
-         && nav_servicable->getBoolValue() )
+         && nav_serviceable->getBoolValue() )
     {
        station = Point3D( nav_x, nav_y, nav_z );
        nav_loc_dist = aircraft.distance3D( station );
 
        if ( nav_has_gs ) {
-           station = Point3D( nav_gs_x, nav_gs_y, nav_gs_z );
-           nav_gs_dist = aircraft.distance3D( station );
-            // wgs84 heading to glide slope
+            // find closest distance to the gs base line
+            sgdVec3 p;
+            sgdSetVec3( p, aircraft.x(), aircraft.y(), aircraft.z() );
+            sgdVec3 p0;
+            sgdSetVec3( p0, nav_gs_x, nav_gs_y, nav_gs_z );
+            double dist = sgdClosestPointToLineDistSquared( p, p0,
+                                                            gs_base_vec );
+            nav_gs_dist = sqrt( dist );
+            // cout << nav_gs_dist;
+
+            // Point3D tmp( nav_gs_x, nav_gs_y, nav_gs_z );
+            // cout << " (" << aircraft.distance3D( tmp ) << ")" << endl;
+
+            // wgs84 heading to glide slope (to determine sign of distance)
             geo_inverse_wgs_84( elev,
                                 lat * SGD_RADIANS_TO_DEGREES,
                                 lon * SGD_RADIANS_TO_DEGREES, 
                                 nav_gslat, nav_gslon,
                                 &az1, &az2, &s );
-            double r = az1 - nav_radial;
+            double r = az1 - nav_target_radial;
             while ( r >  180.0 ) { r -= 360.0;}
             while ( r < -180.0 ) { r += 360.0;}
             if ( r >= -90.0 && r <= 90.0 ) {
@@ -341,7 +374,7 @@ FGNavCom::update(double dt)
             } else {
                 nav_gs_dist_signed = -nav_gs_dist;
             }
-            /* cout << "Target Radial = " << nav_radial 
+            /* cout << "Target Radial = " << nav_target_radial 
                  << "  Bearing = " << az1
                  << "  dist (signed) = " << nav_gs_dist_signed
                  << endl; */
@@ -357,12 +390,12 @@ FGNavCom::update(double dt)
                            nav_loclat, nav_loclon,
                            &az1, &az2, &s );
        // cout << "az1 = " << az1 << " magvar = " << nav_magvar << endl;
-       nav_heading = az1 - nav_magvar;
+       nav_radial = az2 - nav_twist;
        // cout << " heading = " << nav_heading
        //      << " dist = " << nav_dist << endl;
 
        if ( nav_loc ) {
-           double offset = nav_heading - nav_radial;
+           double offset = nav_radial - nav_target_radial;
            while ( offset < -180.0 ) { offset += 360.0; }
            while ( offset > 180.0 ) { offset -= 360.0; }
            // cout << "ils offset = " << offset << endl;
@@ -385,20 +418,20 @@ FGNavCom::update(double dt)
        }
 
        if ( !nav_loc ) {
-           nav_radial = nav_sel_radial;
+           nav_target_radial = nav_sel_radial;
        }
     } else {
        nav_inrange = false;
        // cout << "not picking up vor. :-(" << endl;
     }
 
-    if ( nav_valid && nav_inrange && nav_servicable->getBoolValue() ) {
+    if ( nav_valid && nav_inrange && nav_serviceable->getBoolValue() ) {
        // play station ident via audio system if on + ident,
        // otherwise turn it off
        if ( power_btn && (bus_power->getDoubleValue() > 1.0)
              && nav_ident_btn && audio_btn )
         {
-           FGSimpleSound *sound;
+           SGSimpleSound *sound;
            sound = globals->get_soundmgr()->find( nav_fx_name );
             if ( sound != NULL ) {
                 sound->set_volume( nav_vol_btn );
@@ -477,13 +510,13 @@ void FGNavCom::search()
            nav_gslon = ils->get_gslon();
            nav_gslat = ils->get_gslat();
            nav_elev = ils->get_gselev();
-           nav_magvar = 0;
+           nav_twist = 0;
            nav_range = FG_ILS_DEFAULT_RANGE;
            nav_effective_range = nav_range;
            nav_target_gs = ils->get_gsangle();
-           nav_radial = ils->get_locheading();
-           while ( nav_radial <   0.0 ) { nav_radial += 360.0; }
-           while ( nav_radial > 360.0 ) { nav_radial -= 360.0; }
+           nav_target_radial = ils->get_locheading();
+           while ( nav_target_radial <   0.0 ) { nav_target_radial += 360.0; }
+           while ( nav_target_radial > 360.0 ) { nav_target_radial -= 360.0; }
            nav_x = ils->get_x();
            nav_y = ils->get_y();
            nav_z = ils->get_z();
@@ -491,10 +524,26 @@ void FGNavCom::search()
            nav_gs_y = ils->get_gs_y();
            nav_gs_z = ils->get_gs_z();
 
+            // derive GS baseline
+            double tlon, tlat, taz;
+            geo_direct_wgs_84 ( 0.0, nav_gslat, nav_gslon, nav_target_radial + 90,  
+                                100.0, &tlat, &tlon, &taz );
+            // cout << nav_gslon << "," << nav_gslat << "  "
+            //      << tlon << "," << tlat << "  (" << nav_elev << ")" << endl;
+            Point3D p1 = sgGeodToCart( Point3D(tlon*SGD_DEGREES_TO_RADIANS,
+                                               tlat*SGD_DEGREES_TO_RADIANS,
+                                               nav_elev*SG_FEET_TO_METER) );
+            // cout << nav_gs_x << "," << nav_gs_y << "," << nav_gs_z << endl;
+            // cout << p1 << endl;
+            sgdSetVec3( gs_base_vec,
+                        p1.x()-nav_gs_x, p1.y()-nav_gs_y, p1.z()-nav_gs_z );
+            // cout << gs_base_vec[0] << "," << gs_base_vec[1] << ","
+            //      << gs_base_vec[2] << endl;
+
            if ( globals->get_soundmgr()->exists( nav_fx_name ) ) {
                globals->get_soundmgr()->remove( nav_fx_name );
            }
-           FGSimpleSound *sound;
+           SGSimpleSound *sound;
            sound = morse.make_ident( nav_trans_ident, LO_FREQUENCY );
            sound->set_volume( 0.3 );
            globals->get_soundmgr()->add( sound, nav_fx_name );
@@ -532,11 +581,11 @@ void FGNavCom::search()
            nav_loclon = nav->get_lon();
            nav_loclat = nav->get_lat();
            nav_elev = nav->get_elev();
-           nav_magvar = nav->get_magvar();
+           nav_twist = nav->get_magvar();
            nav_range = nav->get_range();
            nav_effective_range = adjustNavRange(nav_elev, elev, nav_range);
            nav_target_gs = 0.0;
-           nav_radial = nav_sel_radial;
+           nav_target_radial = nav_sel_radial;
            nav_x = nav->get_x();
            nav_y = nav->get_y();
            nav_z = nav->get_z();
@@ -544,7 +593,7 @@ void FGNavCom::search()
            if ( globals->get_soundmgr()->exists( nav_fx_name ) ) {
                globals->get_soundmgr()->remove( nav_fx_name );
            }
-           FGSimpleSound *sound;
+           SGSimpleSound *sound;
            sound = morse.make_ident( nav_trans_ident, LO_FREQUENCY );
            sound->set_volume( 0.3 );
            if ( globals->get_soundmgr()->add( sound, nav_fx_name ) ) {
@@ -575,7 +624,7 @@ void FGNavCom::search()
     } else {
        nav_valid = false;
        nav_id = "";
-       nav_radial = 0;
+       nav_target_radial = 0;
        nav_trans_ident = "";
        last_nav_id = "";
        if ( ! globals->get_soundmgr()->remove( nav_fx_name ) ) {
@@ -589,15 +638,15 @@ void FGNavCom::search()
 
 // return the amount of heading needle deflection, returns a value
 // clamped to the range of ( -10 , 10 )
-double FGNavCom::get_nav_heading_needle_deflection() const {
+double FGNavCom::get_nav_cdi_deflection() const {
     double r;
 
     if ( nav_inrange
-         && nav_servicable->getBoolValue() && cdi_servicable->getBoolValue() )
+         && nav_serviceable->getBoolValue() && cdi_serviceable->getBoolValue() )
     {
-        r = nav_heading - nav_radial;
-       // cout << "Radial = " << nav_radial 
-       //      << "  Bearing = " << nav_heading << endl;
+        r = nav_radial - nav_target_radial;
+       // cout << "Target radial = " << nav_target_radial 
+       //      << "  Actual radial = " << nav_radial << endl;
     
        while ( r >  180.0 ) { r -= 360.0;}
        while ( r < -180.0 ) { r += 360.0;}
@@ -605,6 +654,7 @@ double FGNavCom::get_nav_heading_needle_deflection() const {
            r = ( r<0.0 ? -r-180.0 : -r+180.0 );
 
        // According to Robin Peel, the ILS is 4x more sensitive than a vor
+        r = -r;                 // reverse, since radial is outbound
        if ( nav_loc ) { r *= 4.0; }
        if ( r < -10.0 ) { r = -10.0; }
        if ( r >  10.0 ) { r = 10.0; }
@@ -618,14 +668,15 @@ double FGNavCom::get_nav_heading_needle_deflection() const {
 
 // return the amount of glide slope needle deflection (.i.e. the
 // number of degrees we are off the glide slope * 5.0
-double FGNavCom::get_nav_gs_needle_deflection() const {
+double FGNavCom::get_nav_gs_deflection() const {
     if ( nav_inrange && nav_has_gs
-         && nav_servicable->getBoolValue() && gs_servicable->getBoolValue() )
+         && nav_serviceable->getBoolValue() && gs_serviceable->getBoolValue() )
     {
        double x = nav_gs_dist;
        double y = (fgGetDouble("/position/altitude-ft") - nav_elev)
             * SG_FEET_TO_METER;
-       double angle = atan2( y, x ) * SGD_RADIANS_TO_DEGREES;
+        // cout << "dist = " << x << " height = " << y << endl;
+       double angle = asin( y / x ) * SGD_RADIANS_TO_DEGREES;
        return (nav_target_gs - angle) * 5.0;
     } else {
        return 0.0;
@@ -640,14 +691,14 @@ bool
 FGNavCom::get_nav_to_flag () const
 {
     if ( nav_inrange
-         && nav_servicable->getBoolValue()
-         && tofrom_servicable->getBoolValue() )
+         && nav_serviceable->getBoolValue()
+         && tofrom_serviceable->getBoolValue() )
     {
-        double offset = fabs(nav_heading - nav_radial);
+        double offset = fabs(nav_radial - nav_target_radial);
         if (nav_loc) {
             return true;
         } else {
-            return (offset <= 90.0 || offset >= 270.0);
+            return !(offset <= 90.0 || offset >= 270.0);
         }
     } else {
         return false;
@@ -662,15 +713,35 @@ bool
 FGNavCom::get_nav_from_flag () const
 {
     if ( nav_inrange
-         && nav_servicable->getBoolValue()
-         && tofrom_servicable->getBoolValue() ) {
-        double offset = fabs(nav_heading - nav_radial);
+         && nav_serviceable->getBoolValue()
+         && tofrom_serviceable->getBoolValue() ) {
+        double offset = fabs(nav_radial - nav_target_radial);
         if (nav_loc) {
             return false;
         } else {
-          return (offset > 90.0 && offset < 270.0);
+          return !(offset > 90.0 && offset < 270.0);
         }
     } else {
         return false;
     }
 }
+
+
+/**
+ * Return the current radial.
+ */
+double
+FGNavCom::get_nav_radial () const
+{
+    return nav_radial;
+}
+
+double
+FGNavCom::get_nav_reciprocal_radial () const
+{
+    double recip = nav_radial + 180;
+    if ( recip >= 360 ) {
+        recip -= 360;
+    }
+    return recip;
+}