]> git.mxchange.org Git - flightgear.git/blobdiff - src/FDM/JSBSim/JSBSim.cxx
Merge branch 'next' of http://git.gitorious.org/fg/flightgear into next
[flightgear.git] / src / FDM / JSBSim / JSBSim.cxx
index e8a96c0185854c8b858813a4bdce6ce424226d3d..806401cf9654f80587ff61cb2c28222b47a21a6e 100644 (file)
@@ -26,6 +26,7 @@
 #endif
 
 #include <simgear/compiler.h>
+#include <simgear/sg_inlines.h>
 
 #include <stdio.h>    //    size_t
 #include <string>
@@ -113,12 +114,38 @@ private:
   FGJSBsim* mInterface;
 };
 
+// FG uses a squared normalized magnitude for turbulence
+// this lookup table maps fg's severity levels 
+// none(0), light(1/3), moderate(2/3) and severe(3/3)
+// to the POE table indexes 0, 3, 4 and 7
+class FGTurbulenceSeverityTable : public FGTable {
+public:
+  FGTurbulenceSeverityTable() : FGTable(4) {
+    *this << (0.0/9.0) << 0.0;
+    *this << (1.0/9.0) << 3.0;
+    *this << (4.0/9.0) << 4.0;
+    *this << (9.0/9.0) << 7.0;
+  }
+};
+
 /******************************************************************************/
+std::map<std::string,int> FGJSBsim::TURBULENCE_TYPE_NAMES;
+
+static FGTurbulenceSeverityTable TurbulenceSeverityTable;
 
 FGJSBsim::FGJSBsim( double dt )
   : FGInterface(dt), got_wire(false)
 {
     bool result;
+    if( TURBULENCE_TYPE_NAMES.empty() ) {
+        TURBULENCE_TYPE_NAMES["ttNone"]     = FGAtmosphere::ttNone;
+        TURBULENCE_TYPE_NAMES["ttStandard"] = FGAtmosphere::ttStandard;
+        TURBULENCE_TYPE_NAMES["ttBerndt"]   = FGAtmosphere::ttBerndt;
+        TURBULENCE_TYPE_NAMES["ttCulp"]     = FGAtmosphere::ttCulp;
+        TURBULENCE_TYPE_NAMES["ttMilspec"]  = FGAtmosphere::ttMilspec;
+        TURBULENCE_TYPE_NAMES["ttTustin"]   = FGAtmosphere::ttTustin;
+    }
+
                                 // Set up the debugging level
                                 // FIXME: this will not respond to
                                 // runtime changes
@@ -212,15 +239,25 @@ FGJSBsim::FGJSBsim( double dt )
 
     // Set initial fuel levels if provided.
     for (unsigned int i = 0; i < Propulsion->GetNumTanks(); i++) {
+      double d;
       SGPropertyNode * node = fgGetNode("/consumables/fuel/tank", i, true);
-      if (node->getChild("level-gal_us", 0, false) != 0) {
-        Propulsion->GetTank(i)->SetContents(node->getDoubleValue("level-gal_us") * 6.6);
+      FGTank* tank = Propulsion->GetTank(i);
+
+      d = node->getNode( "density-ppg", true )->getDoubleValue();
+      if( d > 0.0 ) {
+        tank->SetDensity( d );
       } else {
-        node->setDoubleValue("level-lbs", Propulsion->GetTank(i)->GetContents());
-        node->setDoubleValue("level-gal_us", Propulsion->GetTank(i)->GetContents() / 6.6);
+        node->getNode( "density-ppg", true )->setDoubleValue( SG_MAX2<double>(tank->GetDensity(), 0.1) );
       }
-      node->setDoubleValue("capacity-gal_us",
-                           Propulsion->GetTank(i)->GetCapacity() / 6.6);
+
+      d = node->getNode( "level-lbs", true )->getDoubleValue();
+      if( d > 0.0 ) {
+        tank->SetContents( d );
+      } else {
+        node->getNode( "level-lbs", true )->setDoubleValue( tank->GetContents() );
+      }
+      /* Capacity is read-only in FGTank and can't be overwritten from FlightGear */
+      node->getNode("capacity-gal_us", true )->setDoubleValue( tank->GetCapacityGallons() );
     }
     Propulsion->SetFuelFreeze((fgGetNode("/sim/freeze/fuel",true))->getBoolValue());
 
@@ -271,8 +308,10 @@ FGJSBsim::FGJSBsim( double dt )
     temperature = fgGetNode("/environment/temperature-degc",true);
     pressure = fgGetNode("/environment/pressure-inhg",true);
     density = fgGetNode("/environment/density-slugft3",true);
+    ground_wind = fgGetNode("/environment/config/boundary/entry[0]/wind-speed-kt",true);
     turbulence_gain = fgGetNode("/environment/turbulence/magnitude-norm",true);
     turbulence_rate = fgGetNode("/environment/turbulence/rate-hz",true);
+    turbulence_model = fgGetNode("/environment/params/jsbsim-turbulence-model",true);
 
     wind_from_north= fgGetNode("/environment/wind-from-north-fps",true);
     wind_from_east = fgGetNode("/environment/wind-from-east-fps" ,true);
@@ -290,6 +329,8 @@ FGJSBsim::FGJSBsim( double dt )
         fgGetDouble("/fdm/jsbsim/systems/hook/tailhook-offset-x-in", 196),
         fgGetDouble("/fdm/jsbsim/systems/hook/tailhook-offset-y-in", 0),
         fgGetDouble("/fdm/jsbsim/systems/hook/tailhook-offset-z-in", -16));
+    last_hook_tip[0] = 0; last_hook_tip[1] = 0; last_hook_tip[2] = 0;
+    last_hook_root[0] = 0; last_hook_root[1] = 0; last_hook_root[2] = 0;
 
     crashed = false;
 }
@@ -318,10 +359,12 @@ void FGJSBsim::init()
                   9.0/5.0*(temperature->getDoubleValue()+273.15) );
       Atmosphere->SetExPressure(pressure->getDoubleValue()*70.726566);
       Atmosphere->SetExDensity(density->getDoubleValue());
-      Atmosphere->SetTurbType(FGAtmosphere::ttCulp);
-      Atmosphere->SetTurbGain(turbulence_gain->getDoubleValue());
-      Atmosphere->SetTurbRate(turbulence_rate->getDoubleValue());
-
+      // initialize to no turbulence, these values get set in the update loop
+      Atmosphere->SetTurbType(FGAtmosphere::ttNone);
+      Atmosphere->SetTurbGain(0.0);
+      Atmosphere->SetTurbRate(0.0);
+      Atmosphere->SetWindspeed20ft(0.0);
+      Atmosphere->SetProbabilityOfExceedence(0.0);
     } else {
       Atmosphere->UseInternal();
     }
@@ -338,14 +381,12 @@ void FGJSBsim::init()
      << ", " << fdmex->GetAtmosphere()->GetDensity() );
 
 // deprecate egt_degf for egt-degf to have consistent naming
-// TODO: raise log-level to ALERT in summer 2010, 
-// remove alias in fall 2010, 
-// remove this code in winter 2010
+// TODO: remove this by end of 2011
     for (unsigned int i=0; i < Propulsion->GetNumEngines(); i++) {
       SGPropertyNode * node = fgGetNode("engines/engine", i, true);
       SGPropertyNode * egtn = node->getNode( "egt_degf" );
       if( egtn != NULL ) {
-        SG_LOG(SG_FLIGHT,SG_WARN,
+        SG_LOG(SG_FLIGHT,SG_ALERT,
                "Aircraft uses deprecated node egt_degf. Please upgrade to egt-degf");
         node->getNode("egt-degf", true)->alias( egtn );
       }
@@ -420,6 +461,14 @@ void FGJSBsim::init()
 
 /******************************************************************************/
 
+void FGJSBsim::unbind()
+{
+  fdmex->Unbind();
+  FGInterface::unbind();
+}
+
+/******************************************************************************/
+
 // Run an iteration of the EOM (equations of motion)
 
 void FGJSBsim::update( double dt )
@@ -519,7 +568,7 @@ void FGJSBsim::update( double dt )
     }
 
     FGJSBBase::Message* msg;
-    while (msg = fdmex->ProcessNextMessage()) {
+    while ((msg = fdmex->ProcessNextMessage()) != NULL) {
 //      msg = fdmex->ProcessNextMessage();
       switch (msg->type) {
       case FGJSBBase::Message::eText:
@@ -549,11 +598,26 @@ void FGJSBsim::update( double dt )
 
 /******************************************************************************/
 
+void FGJSBsim::suspend()
+{
+  fdmex->Hold();
+  SGSubsystem::suspend();
+}
+
+/******************************************************************************/
+
+void FGJSBsim::resume()
+{
+  fdmex->Resume();
+  SGSubsystem::resume();
+}
+
+/******************************************************************************/
+
 // Convert from the FGInterface struct to the JSBsim generic_ struct
 
 bool FGJSBsim::copy_to_JSBsim()
 {
-    double tmp;
     unsigned int i;
 
     // copy control positions into the JSBsim structure
@@ -615,7 +679,7 @@ bool FGJSBsim::copy_to_JSBsim()
         } // end FGTurbine code block
       case FGEngine::etRocket:
         { // FGRocket code block
-        FGRocket* eng = (FGRocket*)Propulsion->GetEngine(i);
+//        FGRocket* eng = (FGRocket*)Propulsion->GetEngine(i);
         break;
         } // end FGRocket code block
       case FGEngine::etTurboprop:
@@ -649,11 +713,31 @@ bool FGJSBsim::copy_to_JSBsim()
     Atmosphere->SetExPressure(pressure->getDoubleValue()*70.726566);
     Atmosphere->SetExDensity(density->getDoubleValue());
 
-    tmp = turbulence_gain->getDoubleValue();
-    //Atmosphere->SetTurbGain(tmp * tmp * 100.0);
+    Atmosphere->SetTurbType((FGAtmosphere::tType)TURBULENCE_TYPE_NAMES[turbulence_model->getStringValue()]);
+    switch( Atmosphere->GetTurbType() ) {
+        case FGAtmosphere::ttStandard:
+        case FGAtmosphere::ttCulp:
+        case FGAtmosphere::ttBerndt: {
+            double tmp = turbulence_gain->getDoubleValue();
+            Atmosphere->SetTurbGain(tmp * tmp * 100.0);
+            Atmosphere->SetTurbRate(turbulence_rate->getDoubleValue());
+            break;
+        }
+        case FGAtmosphere::ttMilspec:
+        case FGAtmosphere::ttTustin: {
+            // milspec turbulence: 3=light, 4=moderate, 6=severe turbulence
+            // turbulence_gain normalized: 0: none, 1/3: light, 2/3: moderate, 3/3: severe
+            double tmp = turbulence_gain->getDoubleValue();
+            Atmosphere->SetProbabilityOfExceedence(
+              SGMiscd::roundToInt(TurbulenceSeverityTable.GetValue( tmp ) )
+            );
+            Atmosphere->SetWindspeed20ft(ground_wind->getDoubleValue());
+            break;
+        }
 
-    tmp = turbulence_rate->getDoubleValue();
-    //Atmosphere->SetTurbRate(tmp);
+        default:
+            break;
+    }
 
     Atmosphere->SetWindNED( -wind_from_north->getDoubleValue(),
                             -wind_from_east->getDoubleValue(),
@@ -666,8 +750,13 @@ bool FGJSBsim::copy_to_JSBsim()
     for (i = 0; i < Propulsion->GetNumTanks(); i++) {
       SGPropertyNode * node = fgGetNode("/consumables/fuel/tank", i, true);
       FGTank * tank = Propulsion->GetTank(i);
-      tank->SetContents(node->getDoubleValue("level-gal_us") * 6.6);
-//       tank->SetContents(node->getDoubleValue("level-lbs"));
+      double fuelDensity = node->getDoubleValue("density-ppg");
+
+      if (fuelDensity < 0.1)
+        fuelDensity = 6.0; // Use average fuel value
+
+      tank->SetDensity(fuelDensity);
+      tank->SetContents(node->getDoubleValue("level-lbs"));
     }
 
     Propulsion->SetFuelFreeze((fgGetNode("/sim/freeze/fuel",true))->getBoolValue());
@@ -802,7 +891,7 @@ bool FGJSBsim::copy_from_JSBsim()
         break;
       case FGEngine::etRocket:
         { // FGRocket code block
-        FGRocket* eng = (FGRocket*)Propulsion->GetEngine(i);
+//        FGRocket* eng = (FGRocket*)Propulsion->GetEngine(i);
         } // end FGRocket code block
         break;
       case FGEngine::etTurbine:
@@ -813,7 +902,7 @@ bool FGJSBsim::copy_from_JSBsim()
         node->setDoubleValue("egt-degf", 32 + eng->GetEGT()*9/5);
         node->setBoolValue("augmentation", eng->GetAugmentation());
         node->setBoolValue("water-injection", eng->GetInjection());
-        node->setBoolValue("ignition", eng->GetIgnition());
+        node->setBoolValue("ignition", eng->GetIgnition() != 0);
         node->setDoubleValue("nozzle-pos-norm", eng->GetNozzle());
         node->setDoubleValue("inlet-pos-norm", eng->GetInlet());
         node->setDoubleValue("oil-pressure-psi", eng->getOilPressure_psi());
@@ -832,7 +921,7 @@ bool FGJSBsim::copy_from_JSBsim()
         node->setDoubleValue("n1", eng->GetN1());
         //node->setDoubleValue("n2", eng->GetN2());
         node->setDoubleValue("itt_degf", 32 + eng->GetITT()*9/5);
-        node->setBoolValue("ignition", eng->GetIgnition());
+        node->setBoolValue("ignition", eng->GetIgnition() != 0);
         node->setDoubleValue("nozzle-pos-norm", eng->GetNozzle());
         node->setDoubleValue("inlet-pos-norm", eng->GetInlet());
         node->setDoubleValue("oil-pressure-psi", eng->getOilPressure_psi());
@@ -840,7 +929,7 @@ bool FGJSBsim::copy_from_JSBsim()
         node->setBoolValue("cutoff", eng->GetCutoff());
         node->setBoolValue("starting", eng->GetEngStarting());
         node->setBoolValue("generator-power", eng->GetGeneratorPower());
-        node->setBoolValue("damaged", eng->GetCondition());
+        node->setBoolValue("damaged", eng->GetCondition() != 0);
         node->setBoolValue("ielu-intervent", eng->GetIeluIntervent());
         node->setDoubleValue("oil-temperature-degf", eng->getOilTemp_degF());
 //        node->setBoolValue("onfire", eng->GetFire());
@@ -872,7 +961,7 @@ bool FGJSBsim::copy_from_JSBsim()
       switch (thruster->GetType()) {
       case FGThruster::ttNozzle:
         { // FGNozzle code block
-        FGNozzle* noz = (FGNozzle*)thruster;
+//        FGNozzle* noz = (FGNozzle*)thruster;
         } // end FGNozzle code block
         break;
       case FGThruster::ttPropeller:
@@ -886,7 +975,7 @@ bool FGJSBsim::copy_from_JSBsim()
         break;
       case FGThruster::ttRotor:
         { // FGRotor code block
-        FGRotor* rotor = (FGRotor*)thruster;
+//        FGRotor* rotor = (FGRotor*)thruster;
         } // end FGRotor code block
         break;
       case FGThruster::ttDirect:
@@ -905,7 +994,12 @@ bool FGJSBsim::copy_from_JSBsim()
         FGTank* tank = Propulsion->GetTank(i);
         double contents = tank->GetContents();
         double temp = tank->GetTemperature_degC();
-        node->setDoubleValue("level-gal_us", contents/6.6);
+        double fuelDensity = tank->GetDensity();
+
+        if (fuelDensity < 0.1)
+          fuelDensity = 6.0; // Use average fuel value
+
+        node->setDoubleValue("density-ppg" , fuelDensity);
         node->setDoubleValue("level-lbs", contents);
         if (temp != -9999.0) node->setDoubleValue("temperature_degC", temp);
       }
@@ -980,7 +1074,9 @@ void FGJSBsim::set_Latitude(double lat)
     _set_Sea_level_radius( sea_level_radius_meters * SG_METER_TO_FEET  );
     fgic->SetSeaLevelRadiusFtIC( sea_level_radius_meters * SG_METER_TO_FEET  );
     fgic->SetLatitudeRadIC( lat_geoc );
-    needTrim=true;
+
+    if (!fdmex->Holding())
+      needTrim=true;
 }
 
 
@@ -993,7 +1089,9 @@ void FGJSBsim::set_Longitude(double lon)
 
     update_ic();
     fgic->SetLongitudeRadIC( lon );
-    needTrim=true;
+
+    if (!fdmex->Holding())
+      needTrim=true;
 }
 
 // Sets the altitude above sea level.
@@ -1018,7 +1116,9 @@ void FGJSBsim::set_Altitude(double alt)
           "Terrain elevation: " << FGInterface::get_Runway_altitude() * SG_METER_TO_FEET );
     fgic->SetLatitudeRadIC( lat_geoc );
     fgic->SetAltitudeASLFtIC(alt);
-    needTrim=true;
+
+    if (!fdmex->Holding())
+      needTrim=true;
 }
 
 void FGJSBsim::set_V_calibrated_kts(double vc)
@@ -1030,7 +1130,9 @@ void FGJSBsim::set_V_calibrated_kts(double vc)
 
     update_ic();
     fgic->SetVcalibratedKtsIC(vc);
-    needTrim=true;
+
+    if (!fdmex->Holding())
+      needTrim=true;
 }
 
 void FGJSBsim::set_Mach_number(double mach)
@@ -1042,7 +1144,9 @@ void FGJSBsim::set_Mach_number(double mach)
 
     update_ic();
     fgic->SetMachIC(mach);
-    needTrim=true;
+
+    if (!fdmex->Holding())
+      needTrim=true;
 }
 
 void FGJSBsim::set_Velocities_Local( double north, double east, double down )
@@ -1057,7 +1161,9 @@ void FGJSBsim::set_Velocities_Local( double north, double east, double down )
     fgic->SetVNorthFpsIC(north);
     fgic->SetVEastFpsIC(east);
     fgic->SetVDownFpsIC(down);
-    needTrim=true;
+
+    if (!fdmex->Holding())
+      needTrim=true;
 }
 
 void FGJSBsim::set_Velocities_Wind_Body( double u, double v, double w)
@@ -1072,7 +1178,9 @@ void FGJSBsim::set_Velocities_Wind_Body( double u, double v, double w)
     fgic->SetUBodyFpsIC(u);
     fgic->SetVBodyFpsIC(v);
     fgic->SetWBodyFpsIC(w);
-    needTrim=true;
+
+    if (!fdmex->Holding())
+      needTrim=true;
 }
 
 //Euler angles
@@ -1088,7 +1196,9 @@ void FGJSBsim::set_Euler_Angles( double phi, double theta, double psi )
     fgic->SetThetaRadIC(theta);
     fgic->SetPhiRadIC(phi);
     fgic->SetPsiRadIC(psi);
-    needTrim=true;
+
+    if (!fdmex->Holding())
+      needTrim=true;
 }
 
 //Flight Path
@@ -1106,7 +1216,9 @@ void FGJSBsim::set_Climb_Rate( double roc)
     if( !(fabs(roc) > 1 && fabs(fgic->GetFlightPathAngleRadIC()) < 0.01) ) {
       fgic->SetClimbRateFpsIC(roc);
     }
-    needTrim=true;
+
+    if (!fdmex->Holding())
+      needTrim=true;
 }
 
 void FGJSBsim::set_Gamma_vert_rad( double gamma)
@@ -1117,7 +1229,9 @@ void FGJSBsim::set_Gamma_vert_rad( double gamma)
     if( !(fabs(gamma) < 0.01 && fabs(fgic->GetClimbRateFpsIC()) > 1) ) {
       fgic->SetFlightPathAngleRadIC(gamma);
     }
-    needTrim=true;
+
+    if (!fdmex->Holding())
+      needTrim=true;
 }
 
 void FGJSBsim::init_gear(void )
@@ -1186,8 +1300,8 @@ void FGJSBsim::do_trim(void)
 
   globals->get_controls()->set_elevator_trim(FCS->GetPitchTrimCmd());
   globals->get_controls()->set_elevator(FCS->GetDeCmd());
-  globals->get_controls()->set_throttle(FGControls::ALL_ENGINES,
-  FCS->GetThrottleCmd(0));
+  for( unsigned i = 0; i < Propulsion->GetNumEngines(); i++ )
+    globals->get_controls()->set_throttle(i, FCS->GetThrottleCmd(i));
 
   globals->get_controls()->set_aileron(FCS->GetDaCmd());
   globals->get_controls()->set_rudder( FCS->GetDrCmd());