]> git.mxchange.org Git - flightgear.git/blobdiff - src/Network/native_fdm.cxx
Update Mac configure script for new ALUT scheme; support --with-alut-framework.
[flightgear.git] / src / Network / native_fdm.cxx
index 985ac08b4894b17cd2940e13266cb49f9d50a4ae..4fa6275cedd7937179b35feaf855ae69576d0841 100644 (file)
@@ -2,7 +2,7 @@
 //
 // Written by Curtis Olson, started September 2001.
 //
-// Copyright (C) 2001  Curtis L. Olson - curt@flightgear.org
+// Copyright (C) 2001  Curtis L. Olson - http://www.flightgear.org/~curt
 //
 // This program is free software; you can redistribute it and/or
 // modify it under the terms of the GNU General Public License as
@@ -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$
 
 #include <Time/tmp.hxx>
 #include <Main/fg_props.hxx>
 #include <Main/globals.hxx>
+#include <Scenery/scenery.hxx>
 
 #include "native_fdm.hxx"
 
 // FreeBSD works better with this included last ... (?)
-#if defined(WIN32) && !defined(__CYGWIN__)
+#if defined( _MSC_VER )
 #  include <windows.h>
+#elif defined( __MINGW32__ )
+#  include <winsock2.h>
 #else
 #  include <netinet/in.h>      // htonl() ntohl()
 #endif
@@ -70,6 +73,22 @@ static void htond (double &x)
     }
 }
 
+// Float version
+static void htonf (float &x)   
+{
+    if ( sgIsLittleEndian() ) {
+        int    *Float_Overlay;
+        int     Holding_Buffer;
+    
+        Float_Overlay = (int *) &x;
+        Holding_Buffer = Float_Overlay [0];
+    
+        Float_Overlay [0] = htonl (Holding_Buffer);
+    } else {
+        return;
+    }
+}
+
 
 FGNativeFDM::FGNativeFDM() {
 }
@@ -95,13 +114,15 @@ bool FGNativeFDM::open() {
 
     set_enabled( true );
 
+    // Is this really needed here ????
     cur_fdm_state->_set_Sea_level_radius( SG_EQUATORIAL_RADIUS_FT );
+
     return true;
 }
 
 
 void FGProps2NetFDM( FGNetFDM *net, bool net_byte_order ) {
-    int i;
+    unsigned int i;
 
     // Version sanity checking
     net->version = FG_NET_FDM_VERSION;
@@ -110,9 +131,12 @@ void FGProps2NetFDM( FGNetFDM *net, bool net_byte_order ) {
     net->longitude = cur_fdm_state->get_Longitude();
     net->latitude = cur_fdm_state->get_Latitude();
     net->altitude = cur_fdm_state->get_Altitude() * SG_FEET_TO_METER;
+    net->agl = cur_fdm_state->get_Altitude_AGL() * SG_FEET_TO_METER;
     net->phi = cur_fdm_state->get_Phi();
     net->theta = cur_fdm_state->get_Theta();
     net->psi = cur_fdm_state->get_Psi();
+    net->alpha = cur_fdm_state->get_Alpha();
+    net->beta = cur_fdm_state->get_Beta();
     net->phidot = cur_fdm_state->get_Phi_dot_degps() * SG_DEGREES_TO_RADIANS;
     net->thetadot = cur_fdm_state->get_Theta_dot_degps()
         * SG_DEGREES_TO_RADIANS;
@@ -127,27 +151,34 @@ void FGProps2NetFDM( FGNetFDM *net, bool net_byte_order ) {
     net->v_wind_body_north = cur_fdm_state->get_uBody();
     net->v_wind_body_east = cur_fdm_state->get_vBody();
     net->v_wind_body_down = cur_fdm_state->get_wBody();
-    net->stall_warning = fgGetDouble("/sim/alarms/stall-warning", 0.0);
 
     net->A_X_pilot = cur_fdm_state->get_A_X_pilot();
     net->A_Y_pilot = cur_fdm_state->get_A_Y_pilot();
     net->A_Z_pilot = cur_fdm_state->get_A_Z_pilot();
 
+    net->stall_warning = fgGetDouble("/sim/alarms/stall-warning", 0.0);
+    net->slip_deg
+      = fgGetDouble("/instrumentation/slip-skid-ball/indicated-slip-skid");
+
     // Engine parameters
     net->num_engines = FGNetFDM::FG_MAX_ENGINES;
     for ( i = 0; i < net->num_engines; ++i ) {
         SGPropertyNode *node = fgGetNode("engines/engine", i, true);
         if ( node->getBoolValue( "running" ) ) {
-            net->eng_state[0] = 2;
+            net->eng_state[i] = 2;
         } else if ( node->getBoolValue( "cranking" ) ) {
-            net->eng_state[0] = 1;
+            net->eng_state[i] = 1;
         } else {
-            net->eng_state[0] = 0;
+            net->eng_state[i] = 0;
         }
         net->rpm[i] = node->getDoubleValue( "rpm" );
         net->fuel_flow[i] = node->getDoubleValue( "fuel-flow-gph" );
-        net->EGT[i] = node->getDoubleValue( "egt-degf" );
+        net->fuel_px[i] = node->getDoubleValue( "fuel-px-psi" );
+        net->egt[i] = node->getDoubleValue( "egt-degf" );
         // cout << "egt = " << aero->EGT << endl;
+        net->cht[i] = node->getDoubleValue( "cht-degf" );
+        net->mp_osi[i] = node->getDoubleValue( "mp-osi" );
+        net->tit[i] = node->getDoubleValue( "tit" );
         net->oil_temp[i] = node->getDoubleValue( "oil-temperature-degf" );
         net->oil_px[i] = node->getDoubleValue( "oil-pressure-psi" );
     }
@@ -163,17 +194,32 @@ void FGProps2NetFDM( FGNetFDM *net, bool net_byte_order ) {
     net->num_wheels = FGNetFDM::FG_MAX_WHEELS;
     for (i = 0; i < net->num_wheels; ++i ) {
         SGPropertyNode *node = fgGetNode("/gear/gear", i, true);
-        net->wow[i] = node->getDoubleValue("wow");
+        net->wow[i] = node->getIntValue("wow");
+        net->gear_pos[i] = node->getDoubleValue("position-norm");
+        net->gear_steer[i] = node->getDoubleValue("steering-norm");
+        net->gear_compression[i] = node->getDoubleValue("compression-norm");
     }
 
-    // cout << "Flap deflection = " << aero->dflap << endl;
-    net->flap_deflection = fgGetDouble("/surface-positions/flap-pos-norm" );
-
     // the following really aren't used in this context
     net->cur_time = globals->get_time_params()->get_cur_time();
     net->warp = globals->get_warp();
     net->visibility = fgGetDouble("/environment/visibility-m");
 
+    // Control surface positions
+    SGPropertyNode *node = fgGetNode("/surface-positions", true);
+    net->elevator = node->getDoubleValue( "elevator-pos-norm" );
+    net->elevator_trim_tab
+        = node->getDoubleValue( "elevator-trim-tab-pos-norm" );
+    // FIXME: CLO 10/28/04 - This really should be separated out into 2 values
+    net->left_flap = node->getDoubleValue( "flap-pos-norm" );
+    net->right_flap = node->getDoubleValue( "flap-pos-norm" );
+    net->left_aileron = node->getDoubleValue( "left-aileron-pos-norm" );
+    net->right_aileron = node->getDoubleValue( "right-aileron-pos-norm" );
+    net->rudder = node->getDoubleValue( "rudder-pos-norm" );
+    net->nose_wheel = node->getDoubleValue( "nose-wheel-pos-norm" );
+    net->speedbrake = node->getDoubleValue( "speedbrake-pos-norm" );
+    net->spoilers = node->getDoubleValue( "spoilers-pos-norm" );
+
     if ( net_byte_order ) {
         // Convert the net buffer to network format
         net->version = htonl(net->version);
@@ -181,57 +227,79 @@ void FGProps2NetFDM( FGNetFDM *net, bool net_byte_order ) {
         htond(net->longitude);
         htond(net->latitude);
         htond(net->altitude);
-        htond(net->phi);
-        htond(net->theta);
-        htond(net->psi);
-
-        htond(net->phidot);
-        htond(net->thetadot);
-        htond(net->psidot);
-        htond(net->vcas);
-        htond(net->climb_rate);
-        htond(net->v_north);
-        htond(net->v_east);
-        htond(net->v_down);
-        htond(net->v_wind_body_north);
-        htond(net->v_wind_body_east);
-        htond(net->v_wind_body_down);
-        htond(net->stall_warning);
-
-        htond(net->A_X_pilot);
-        htond(net->A_Y_pilot);
-        htond(net->A_Z_pilot);
+        htonf(net->agl);
+        htonf(net->phi);
+        htonf(net->theta);
+        htonf(net->psi);
+        htonf(net->alpha);
+        htonf(net->beta);
+
+        htonf(net->phidot);
+        htonf(net->thetadot);
+        htonf(net->psidot);
+        htonf(net->vcas);
+        htonf(net->climb_rate);
+        htonf(net->v_north);
+        htonf(net->v_east);
+        htonf(net->v_down);
+        htonf(net->v_wind_body_north);
+        htonf(net->v_wind_body_east);
+        htonf(net->v_wind_body_down);
+
+        htonf(net->A_X_pilot);
+        htonf(net->A_Y_pilot);
+        htonf(net->A_Z_pilot);
+
+        htonf(net->stall_warning);
+        htonf(net->slip_deg);
 
         for ( i = 0; i < net->num_engines; ++i ) {
-            htonl(net->eng_state[i]);
-            htond(net->rpm[i]);
-            htond(net->fuel_flow[i]);
-            htond(net->EGT[i]);
-            htond(net->oil_temp[i]);
-            htond(net->oil_px[i]);
+            net->eng_state[i] = htonl(net->eng_state[i]);
+            htonf(net->rpm[i]);
+            htonf(net->fuel_flow[i]);
+            htonf(net->fuel_px[i]);
+            htonf(net->egt[i]);
+            htonf(net->cht[i]);
+            htonf(net->mp_osi[i]);
+            htonf(net->tit[i]);
+            htonf(net->oil_temp[i]);
+            htonf(net->oil_px[i]);
         }
         net->num_engines = htonl(net->num_engines);
 
         for ( i = 0; i < net->num_tanks; ++i ) {
-            htond(net->fuel_quantity[i]);
+            htonf(net->fuel_quantity[i]);
         }
         net->num_tanks = htonl(net->num_tanks);
 
         for ( i = 0; i < net->num_wheels; ++i ) {
             net->wow[i] = htonl(net->wow[i]);
+            htonf(net->gear_pos[i]);
+            htonf(net->gear_steer[i]);
+            htonf(net->gear_compression[i]);
         }
         net->num_wheels = htonl(net->num_wheels);
-        htond(net->flap_deflection);
 
         net->cur_time = htonl( net->cur_time );
         net->warp = htonl( net->warp );
-        htond(net->visibility);
+        htonf(net->visibility);
+
+        htonf(net->elevator);
+        htonf(net->elevator_trim_tab);
+        htonf(net->left_flap);
+        htonf(net->right_flap);
+        htonf(net->left_aileron);
+        htonf(net->right_aileron);
+        htonf(net->rudder);
+        htonf(net->nose_wheel);
+        htonf(net->speedbrake);
+        htonf(net->spoilers);
     }
 }
 
 
 void FGNetFDM2Props( FGNetFDM *net, bool net_byte_order ) {
-    int i;
+    unsigned int i;
 
     if ( net_byte_order ) {
         // Convert to the net buffer from network format
@@ -240,50 +308,73 @@ void FGNetFDM2Props( FGNetFDM *net, bool net_byte_order ) {
         htond(net->longitude);
         htond(net->latitude);
         htond(net->altitude);
-        htond(net->phi);
-        htond(net->theta);
-        htond(net->psi);
-
-        htond(net->phidot);
-        htond(net->thetadot);
-        htond(net->psidot);
-        htond(net->vcas);
-        htond(net->climb_rate);
-        htond(net->v_north);
-        htond(net->v_east);
-        htond(net->v_down);
-        htond(net->v_wind_body_north);
-        htond(net->v_wind_body_east);
-        htond(net->v_wind_body_down);
-        htond(net->stall_warning);
-
-        htond(net->A_X_pilot);
-        htond(net->A_Y_pilot);
-        htond(net->A_Z_pilot);
+        htonf(net->agl);
+        htonf(net->phi);
+        htonf(net->theta);
+        htonf(net->psi);
+        htonf(net->alpha);
+        htonf(net->beta);
+
+        htonf(net->phidot);
+        htonf(net->thetadot);
+        htonf(net->psidot);
+        htonf(net->vcas);
+        htonf(net->climb_rate);
+        htonf(net->v_north);
+        htonf(net->v_east);
+        htonf(net->v_down);
+        htonf(net->v_wind_body_north);
+        htonf(net->v_wind_body_east);
+        htonf(net->v_wind_body_down);
+
+        htonf(net->A_X_pilot);
+        htonf(net->A_Y_pilot);
+        htonf(net->A_Z_pilot);
+
+        htonf(net->stall_warning);
+        htonf(net->slip_deg);
 
         net->num_engines = htonl(net->num_engines);
         for ( i = 0; i < net->num_engines; ++i ) {
-            htonl(net->eng_state[i]);
-            htond(net->rpm[i]);
-            htond(net->fuel_flow[i]);
-            htond(net->EGT[i]);
-            htond(net->oil_temp[i]);
-            htond(net->oil_px[i]);
+            net->eng_state[i] = htonl(net->eng_state[i]);
+            htonf(net->rpm[i]);
+            htonf(net->fuel_flow[i]);
+            htonf(net->fuel_px[i]);
+            htonf(net->egt[i]);
+            htonf(net->cht[i]);
+            htonf(net->mp_osi[i]);
+            htonf(net->tit[i]);
+            htonf(net->oil_temp[i]);
+            htonf(net->oil_px[i]);
         }
 
         net->num_tanks = htonl(net->num_tanks);
         for ( i = 0; i < net->num_tanks; ++i ) {
-            htond(net->fuel_quantity[i]);
+            htonf(net->fuel_quantity[i]);
         }
 
         net->num_wheels = htonl(net->num_wheels);
-        // I don't need to convert the Wow flags, since they are one
-        // byte in size
-        htond(net->flap_deflection);
+        for ( i = 0; i < net->num_wheels; ++i ) {
+            net->wow[i] = htonl(net->wow[i]);
+            htonf(net->gear_pos[i]);
+            htonf(net->gear_steer[i]);
+            htonf(net->gear_compression[i]);
+        }
 
-        net->cur_time = ntohl(net->cur_time);
+        net->cur_time = htonl(net->cur_time);
         net->warp = ntohl(net->warp);
-        htond(net->visibility);
+        htonf(net->visibility);
+
+        htonf(net->elevator);
+        htonf(net->elevator_trim_tab);
+        htonf(net->left_flap);
+        htonf(net->right_flap);
+        htonf(net->left_aileron);
+        htonf(net->right_aileron);
+        htonf(net->rudder);
+        htonf(net->nose_wheel);
+        htonf(net->speedbrake);
+        htonf(net->spoilers);
     }
 
     if ( net->version == FG_NET_FDM_VERSION ) {
@@ -294,9 +385,18 @@ void FGNetFDM2Props( FGNetFDM *net, bool net_byte_order ) {
                                                 net->longitude,
                                                 net->altitude
                                                   * SG_METER_TO_FEET );
+       if ( net->agl > -9000 ) {
+           cur_fdm_state->_set_Altitude_AGL( net->agl * SG_METER_TO_FEET );
+       } else {
+           double agl_m = net->altitude
+              - cur_fdm_state->get_Runway_altitude_m();
+           cur_fdm_state->_set_Altitude_AGL( agl_m * SG_METER_TO_FEET );
+       }
         cur_fdm_state->_set_Euler_Angles( net->phi,
                                           net->theta,
                                           net->psi );
+        cur_fdm_state->_set_Alpha( net->alpha );
+        cur_fdm_state->_set_Beta( net->beta );
         cur_fdm_state->_set_Euler_Rates( net->phidot,
                                         net->thetadot,
                                         net->psidot );
@@ -309,11 +409,15 @@ void FGNetFDM2Props( FGNetFDM *net, bool net_byte_order ) {
                                                   net->v_wind_body_east,
                                                   net->v_wind_body_down );
 
-        fgSetDouble( "/sim/alarms/stall-warning", net->stall_warning );
         cur_fdm_state->_set_Accels_Pilot_Body( net->A_X_pilot,
                                               net->A_Y_pilot,
                                               net->A_Z_pilot );
 
+        fgSetDouble( "/sim/alarms/stall-warning", net->stall_warning );
+       fgSetDouble( "/instrumentation/slip-skid-ball/indicated-slip-skid",
+                    net->slip_deg );
+       fgSetBool( "/instrumentation/slip-skid-ball/override", true );
+
        for ( i = 0; i < net->num_engines; ++i ) {
            SGPropertyNode *node = fgGetNode( "engines/engine", i, true );
            
@@ -334,7 +438,11 @@ void FGNetFDM2Props( FGNetFDM *net, bool net_byte_order ) {
 
            node->setDoubleValue( "rpm", net->rpm[i] );
            node->setDoubleValue( "fuel-flow-gph", net->fuel_flow[i] );
-           node->setDoubleValue( "egt-degf", net->EGT[i] );
+           node->setDoubleValue( "fuel-px-psi", net->fuel_px[i] );
+           node->setDoubleValue( "egt-degf", net->egt[i] );
+           node->setDoubleValue( "cht-degf", net->cht[i] );
+           node->setDoubleValue( "mp-osi", net->mp_osi[i] );
+           node->setDoubleValue( "tit", net->tit[i] );
            node->setDoubleValue( "oil-temperature-degf", net->oil_temp[i] );
            node->setDoubleValue( "oil-pressure-psi", net->oil_px[i] );         
        }
@@ -346,22 +454,13 @@ void FGNetFDM2Props( FGNetFDM *net, bool net_byte_order ) {
        }
 
        for (i = 0; i < net->num_wheels; ++i ) {
-           SGPropertyNode * node
-               = fgGetNode("/gear/gear", i, true);
+           SGPropertyNode * node = fgGetNode("/gear/gear", i, true);
            node->setDoubleValue("wow", net->wow[i] );
+           node->setDoubleValue("position-norm", net->gear_pos[i] );
+           node->setDoubleValue("steering-norm", net->gear_steer[i] );
+           node->setDoubleValue("compression-norm", net->gear_compression[i] );
        }
 
-        fgSetDouble("/surface-positions/flap-pos-norm", net->flap_deflection);
-       SGPropertyNode * node = fgGetNode("/controls", true);
-        fgSetDouble("/surface-positions/elevator-pos-norm", 
-                   node->getDoubleValue( "elevator" ));
-        fgSetDouble("/surface-positions/rudder-pos-norm", 
-                   node->getDoubleValue( "rudder" ));
-        fgSetDouble("/surface-positions/left-aileron-pos-norm", 
-                   node->getDoubleValue( "aileron" ));
-        fgSetDouble("/surface-positions/right-aileron-pos-norm", 
-                   -node->getDoubleValue( "aileron" ));
-
        /* these are ignored for now  ... */
        /*
        if ( net->cur_time ) {
@@ -371,6 +470,21 @@ void FGNetFDM2Props( FGNetFDM *net, bool net_byte_order ) {
         globals->set_warp( net->warp );
         last_warp = net->warp;
        */
+
+        SGPropertyNode *node = fgGetNode("/surface-positions", true);
+        node->setDoubleValue("elevator-pos-norm", net->elevator);
+        node->setDoubleValue("elevator-trim-tab-pos-norm",
+                             net->elevator_trim_tab);
+       // FIXME: CLO 10/28/04 - This really should be separated out
+       // into 2 values
+        node->setDoubleValue("flap-pos-norm", net->left_flap);
+        node->setDoubleValue("flap-pos-norm", net->right_flap);
+        node->setDoubleValue("left-aileron-pos-norm", net->left_aileron);
+        node->setDoubleValue("right-aileron-pos-norm", net->right_aileron);
+        node->setDoubleValue("rudder-pos-norm", net->rudder);
+        node->setDoubleValue("nose-wheel-pos-norm", net->nose_wheel);
+        node->setDoubleValue("speedbrake-pos-norm", net->speedbrake);
+        node->setDoubleValue("spoilers-pos-norm", net->spoilers);
     } else {
        SG_LOG( SG_IO, SG_ALERT,
                 "Error: version mismatch in FGNetFDM2Props()" );
@@ -401,23 +515,15 @@ bool FGNativeFDM::process() {
                FGNetFDM2Props( &buf );
            }
        } else {
-            // double dt = 1000000.0 / 30.0;
-            // SGTimeStamp current; current.stamp();
             int result;
             result = io->read( (char *)(& buf), length );
-            if ( result == length ) {
-                SG_LOG( SG_IO, SG_DEBUG, "  Success reading data." );
-                FGNetFDM2Props( &buf );
-            }
-           while ( result == length /* || current - last_time < dt */ ) {
-                result = io->read( (char *)(& buf), length );
+           while ( result == length ) {
                 if ( result == length ) {
                     SG_LOG( SG_IO, SG_DEBUG, "  Success reading data." );
                     FGNetFDM2Props( &buf );
                 }
-                // current.stamp();
+                result = io->read( (char *)(& buf), length );
            }
-            // last_time = current;
        }
     }