]> git.mxchange.org Git - flightgear.git/blobdiff - src/FDM/LaRCsim.cxx
Updated to match changes in radiostack.[ch]xx
[flightgear.git] / src / FDM / LaRCsim.cxx
index cf3682814c9fbe146ff4a756737171c7a8d56f3a..43223bf4cf9bfbde1f8fc6a0df2a7a126bf3e165 100644 (file)
 //
 // $Id$
 
+#include <string.h>            // strcmp()
 
 #include <simgear/constants.h>
 #include <simgear/debug/logstream.hxx>
 
-#include <Scenery/scenery.hxx>
-
 #include <Main/fg_props.hxx>
 #include <Aircraft/aircraft.hxx>
 #include <Controls/controls.hxx>
 #include "LaRCsim.hxx"
 
 FGLaRCsim::FGLaRCsim( double dt ) {
-    set_delta_t( dt );
+//     set_delta_t( dt );
 
     speed_up = fgGetNode("/sim/speed-up", true);
     aero = fgGetNode("/sim/aero", true);
 
-    ls_toplevel_init( 0.0, (char *)(aero->getStringValue().c_str()) );
+    ls_toplevel_init( 0.0, (char *)(aero->getStringValue()) );
 
     lsic=new LaRCsimIC; //this needs to be brought up after LaRCsim is
-    if ( aero->getStringValue() == "c172" ) {
+    if ( !strcmp(aero->getStringValue(), "c172") ) {
         copy_to_LaRCsim(); // initialize all of LaRCsim's vars
 
         //this should go away someday -- formerly done in fg_init.cxx
@@ -59,28 +58,16 @@ FGLaRCsim::FGLaRCsim( double dt ) {
         I_xz = 0.000000E+00;
     }
 
-    ls_set_model_dt( get_delta_t() );
+    ls_set_model_dt(dt);
 
             // Initialize our little engine that hopefully might
-    eng.init( get_delta_t() );
+    eng.init(dt);
     // dcl - in passing dt to init rather than update I am assuming
     // that the LaRCsim dt is fixed at one value (yes it is 120hz CLO)
-
-    // update the engines interface
-    FGEngInterface e;
-    add_engine( e );
-
-    // Fill the fuel tanks
-    // Hardwired to C172 full tanks for now - need to fix this sometime
-    // Also note that this is the max quantity - the usable quantity
-    // is slightly less
-    set_Tank1Fuel(28.0);
-    set_Tank2Fuel(28.0);
 }
 
 FGLaRCsim::~FGLaRCsim(void) {
     if ( lsic != NULL ) {
-        free_engines();
         delete lsic;
         lsic = NULL;
     }
@@ -91,15 +78,18 @@ FGLaRCsim::~FGLaRCsim(void) {
 void FGLaRCsim::init() {
    //do init common to all FDM's
    common_init();
-
-   //now do any specific to LaRCsim
 }
 
 
 // Run an iteration of the EOM (equations of motion)
-bool FGLaRCsim::update( int multiloop ) {
+void FGLaRCsim::update( double dt ) {
+
+    if (is_suspended())
+      return;
 
-    if ( aero->getStringValue() == "c172" ) {
+    int multiloop = _calc_multiloop(dt);
+
+    if ( !strcmp(aero->getStringValue(), "c172") ) {
        // set control inputs
        // cout << "V_calibrated_kts = " << V_calibrated_kts << '\n';
        eng.set_IAS( V_calibrated_kts );
@@ -116,42 +106,52 @@ bool FGLaRCsim::update( int multiloop ) {
        // update engine model
        eng.update();
 
+       // Fake control-surface positions
+       fgSetDouble("/surface-positions/flap-pos-norm",
+                   fgGetDouble("/controls/flaps"));
+                               // FIXME: ignoring trim
+       fgSetDouble("/surface-positions/elevator-pos-norm",
+                   fgGetDouble("/controls/elevator"));
+                               // FIXME: ignoring trim
+       fgSetDouble("/surface-positions/left-aileron-pos-norm",
+                   fgGetDouble("/controls/aileron"));
+                               // FIXME: ignoring trim
+       fgSetDouble("/surface-positions/right-aileron-pos-norm",
+                   -1 * fgGetDouble("/controls/aileron"));
+                               // FIXME: ignoring trim
+       fgSetDouble("/surface-positions/rudder-pos-norm",
+                   fgGetDouble("/controls/rudder"));
+
        // copy engine state values onto "bus"
-       FGEngInterface *e = get_engine( 0 );
-       e->set_Throttle( globals->get_controls()->get_throttle(0) * 100.0 );
-       e->set_Mixture( 80 );   // ???????
-       e->set_Prop_Advance( 100 );
-       e->set_RPM( eng.get_RPM() );
-       e->set_Manifold_Pressure( eng.get_Manifold_Pressure() );
-       e->set_MaxHP( eng.get_MaxHP() );
-       e->set_Percentage_Power( eng.get_Percentage_Power() );
-       e->set_EGT( eng.get_EGT() );
-       e->set_CHT( eng.get_CHT() );
-       e->set_prop_thrust( eng.get_prop_thrust_SI() );
-       e->set_Fuel_Flow( eng.get_fuel_flow_gals_hr() );
-       e->set_Oil_Temp( eng.get_oil_temp() );
-       e->set_Running_Flag( eng.getRunningFlag() );
-       e->set_Cranking_Flag( eng.getCrankingFlag() );
-
-        //Assume we are using both tanks equally for now
-       reduce_Tank1Fuel( (eng.get_fuel_flow_gals_hr() / (2 * 3600))
-                         * get_delta_t() );
-       reduce_Tank2Fuel( (eng.get_fuel_flow_gals_hr() / (2 * 3600))
-                         * get_delta_t() ); 
-
-#if 0
-       SG_LOG( SG_FLIGHT, SG_INFO, "Throttle = "
-               << globals->get_controls()->get_throttle( 0 ) * 100.0);
-       SG_LOG( SG_FLIGHT, SG_INFO, " Mixture = " << 80);
-       SG_LOG( SG_FLIGHT, SG_INFO, " RPM = " << eng.get_RPM());
-       SG_LOG( SG_FLIGHT, SG_INFO, " MP = " << eng.get_Manifold_Pressure());
-       SG_LOG( SG_FLIGHT, SG_INFO, " HP = "
-               << ( eng.get_MaxHP() * eng.get_Percentage_Power()/ 100.0) );
-       SG_LOG( SG_FLIGHT, SG_INFO, " EGT = " << eng.get_EGT());
-       SG_LOG( SG_FLIGHT, SG_INFO, " Thrust (N) "
-               << eng.get_prop_thrust_SI());   // Thrust in Newtons
-       SG_LOG( SG_FLIGHT, SG_INFO, '\n');
-#endif
+       fgSetDouble("/engines/engine/rpm", eng.get_RPM());
+       fgSetDouble("/engines/engine/mp-osi", eng.get_Manifold_Pressure());
+       fgSetDouble("/engines/engine/max-hp", eng.get_MaxHP());
+       fgSetDouble("/engines/engine/power-pct", eng.get_Percentage_Power());
+       fgSetDouble("/engines/engine/egt-degf", eng.get_EGT());
+       fgSetDouble("/engines/engine/cht-degf", eng.get_CHT());
+       fgSetDouble("/engines/engine/prop-thrust", eng.get_prop_thrust_SI());
+       fgSetDouble("/engines/engine/fuel-flow-gph",
+                   eng.get_fuel_flow_gals_hr());
+       fgSetDouble("/engines/engine/oil-temperature-degf",
+                   eng.get_oil_temp());
+       fgSetDouble("/engines/engine/running", eng.getRunningFlag());
+       fgSetDouble("/engines/engine/cranking", eng.getCrankingFlag());
+
+       static const SGPropertyNode *fuel_freeze
+           = fgGetNode("/sim/freeze/fuel");
+
+       if ( ! fuel_freeze->getBoolValue() ) {
+           //Assume we are using both tanks equally for now
+           fgSetDouble("/consumables/fuel/tank[0]/level-gal_us",
+                       fgGetDouble("/consumables/fuel/tank[0]/level-gal_us")
+                       - (eng.get_fuel_flow_gals_hr() / (2 * 3600))
+                       * dt);
+           fgSetDouble("/consumables/fuel/tank[1]/level-gal_us",
+                       fgGetDouble("/consumables/fuel/tank[1]/level-gal_us")
+                       - (eng.get_fuel_flow_gals_hr() / (2 * 3600))
+                       * dt);
+       }
+
         F_X_engine = eng.get_prop_thrust_lbs();
        // cout << "F_X_engine = " << F_X_engine << '\n';
     }
@@ -173,7 +173,7 @@ bool FGLaRCsim::update( int multiloop ) {
         speed_up->getIntValue();
     Flap_handle = 30.0 * globals->get_controls()->get_flaps();
 
-    if ( aero->getStringValue() == "c172" ) {
+    if ( !strcmp(aero->getStringValue(), "c172") ) {
         Use_External_Engine = 1;
     } else {
        Use_External_Engine = 0;
@@ -186,8 +186,7 @@ bool FGLaRCsim::update( int multiloop ) {
 
     // Inform LaRCsim of the local terrain altitude
     // Runway_altitude = get_Runway_altitude();
-    Runway_altitude = scenery.get_cur_elev() * SG_METER_TO_FEET;
-
+    Runway_altitude = getACModel()->get3DModel()->getFGLocation()->get_cur_elev_m() * SG_METER_TO_FEET;
     // Weather
     /* V_north_airmass = get_V_north_airmass();
        V_east_airmass =  get_V_east_airmass();
@@ -201,6 +200,15 @@ bool FGLaRCsim::update( int multiloop ) {
     // printf("Altitude = %.2f\n", Altitude * 0.3048);
     // printf("Radius to Vehicle = %.2f\n", Radius_to_vehicle * 0.3048);
 
+    // for engine functions (sounds and instruments)
+    // drive the rpm gauge
+    fgSetDouble("/engines/engine/rpm", (globals->get_controls()->get_throttle( 0 ) * 100.0 * 25 ));
+    // manifold air pressure, which drives the sound (see *sound.xml file)
+    fgSetDouble("/engines/engine/mp-osi", (globals->get_controls()->get_throttle( 0 ) * 100.0 ));
+    // make the engine cranking and running sounds when fgfs starts up
+    fgSetDouble("/engines/engine/cranking", 1);
+    fgSetDouble("/engines/engine/running", 1);
+
     ls_update(multiloop);
 
     // printf("%d FG_Altitude = %.2f\n", i, FG_Altitude * 0.3048);
@@ -215,8 +223,6 @@ bool FGLaRCsim::update( int multiloop ) {
     if ( save_alt < -9000.0 ) {
        set_Altitude( save_alt );
     }
-
-    return true;
 }
 
 
@@ -545,7 +551,7 @@ bool FGLaRCsim::copy_from_LaRCsim() {
     _set_Climb_Rate( -1 * V_down );
     // cout << "climb rate = " << -V_down * 60 << endl;
 
-    if ( aero->getStringValue() == "uiuc" ) {
+    if ( !strcmp(aero->getStringValue(), "uiuc") ) {
        if (pilot_elev_no) {
            globals->get_controls()->set_elevator(Long_control);
            globals->get_controls()->set_elevator_trim(Long_trim);
@@ -581,25 +587,11 @@ void FGLaRCsim::set_ls(void) {
     Latitude=lsic->GetLatitudeGDRadIC();
     Longitude=lsic->GetLongitudeRadIC();
     Runway_altitude=lsic->GetRunwayAltitudeFtIC();
-    V_north_airmass = lsic->GetVnorthAirmassFpsIC();
-    V_east_airmass = lsic->GetVeastAirmassFpsIC();
-    V_down_airmass = lsic->GetVdownAirmassFpsIC();
+    V_north_airmass = lsic->GetVnorthAirmassFpsIC() * -1;
+    V_east_airmass = lsic->GetVeastAirmassFpsIC() * -1;
+    V_down_airmass = lsic->GetVdownAirmassFpsIC() * -1;
     ls_loop(0.0,-1);
     copy_from_LaRCsim();
-    SG_LOG( SG_FLIGHT, SG_INFO, "  FGLaRCsim::set_ls(): "  );
-    SG_LOG( SG_FLIGHT, SG_INFO, "     Phi: " <<  Phi  );
-    SG_LOG( SG_FLIGHT, SG_INFO, "     Theta: " <<  Theta  );
-    SG_LOG( SG_FLIGHT, SG_INFO, "     Psi: " <<  Psi  );
-    SG_LOG( SG_FLIGHT, SG_INFO, "     V_north: " <<  V_north  );
-    SG_LOG( SG_FLIGHT, SG_INFO, "     V_east: " <<  V_east  );
-    SG_LOG( SG_FLIGHT, SG_INFO, "     V_down: " <<  V_down  );
-    SG_LOG( SG_FLIGHT, SG_INFO, "     Altitude: " <<  Altitude  );
-    SG_LOG( SG_FLIGHT, SG_INFO, "     Latitude: " <<  Latitude  );
-    SG_LOG( SG_FLIGHT, SG_INFO, "     Longitude: " <<  Longitude  );
-    SG_LOG( SG_FLIGHT, SG_INFO, "     Runway_altitude: " <<  Runway_altitude  );
-    SG_LOG( SG_FLIGHT, SG_INFO, "     V_north_airmass: " <<  V_north_airmass  );
-    SG_LOG( SG_FLIGHT, SG_INFO, "     V_east_airmass: " <<  V_east_airmass  );
-    SG_LOG( SG_FLIGHT, SG_INFO, "     V_down_airmass: " <<  V_down_airmass  );
 }
 
 void FGLaRCsim::snap_shot(void) {
@@ -707,14 +699,6 @@ void FGLaRCsim::set_Gamma_vert_rad( double gamma) {
     copy_from_LaRCsim(); //update the bus
 }
 
-void FGLaRCsim::set_Runway_altitude(double ralt) {
-    SG_LOG( SG_FLIGHT, SG_INFO, "FGLaRCsim::set_Runway_altitude: " << ralt  );
-    snap_shot();
-    lsic->SetRunwayAltitudeFtIC(ralt);
-    set_ls();
-    copy_from_LaRCsim(); //update the bus
-}
-
 void FGLaRCsim::set_AltitudeAGL(double altagl) {
     SG_LOG( SG_FLIGHT, SG_INFO, "FGLaRCsim::set_AltitudeAGL: " << altagl  );
     snap_shot();
@@ -723,6 +707,7 @@ void FGLaRCsim::set_AltitudeAGL(double altagl) {
     copy_from_LaRCsim();
 }
 
+/*  getting a namespace conflict...
 void FGLaRCsim::set_Velocities_Local_Airmass (double wnorth, 
                                              double weast, 
                                              double wdown ) {
@@ -733,6 +718,7 @@ void FGLaRCsim::set_Velocities_Local_Airmass (double wnorth,
     set_ls();
     copy_from_LaRCsim();
 }
+*/
 
 void FGLaRCsim::set_Static_pressure(double p) { 
     SG_LOG( SG_FLIGHT, SG_INFO,