]> git.mxchange.org Git - flightgear.git/blobdiff - src/FDM/flight.cxx
Initial revision of Andy Ross's YASim code. This is (Y)et (A)nother Flight
[flightgear.git] / src / FDM / flight.cxx
index 22ee59ef661a986cf5a269c471720f4b1be9ea6a..fc9d0cab400d2f88af49103129a2d9c50b3625e4 100644 (file)
@@ -1,4 +1,4 @@
-// flight.c -- a general interface to the various flight models
+// flight.cxx -- a general interface to the various flight models
 //
 // Written by Curtis Olson, started May 1997.
 //
@@ -23,6 +23,8 @@
 
 #include <stdio.h>
 
+#include <plib/sg.h>
+
 #include <simgear/constants.h>
 #include <simgear/debug/logstream.hxx>
 #include <simgear/math/sg_geodesy.hxx>
@@ -71,9 +73,43 @@ FGEngInterface::FGEngInterface() {
 FGEngInterface::~FGEngInterface(void) {
 }
 
+FGGearInterface::FGGearInterface(void) {
+    x=y=z=0.0;
+    brake=rolls=WoW=false;
+    position=1.0;
+}    
+
+FGGearInterface::~FGGearInterface() {
+}
 
 // Constructor
 FGInterface::FGInterface() {
+    _setup();
+}  
+
+FGInterface::FGInterface( double dt ) {
+    _setup();
+    delta_t = dt;
+    remainder = elapsed = multi_loop = 0;
+}
+
+// Destructor
+FGInterface::~FGInterface() {
+//   unbind();                 // FIXME: should be called explicitly
+}
+
+
+/**
+ * Set default values for the state of the FDM.
+ *
+ * This method is invoked by the constructors.
+ */
+void
+FGInterface::_setup ()
+{
+    inited = false;
+    bound = false;
+
     init_vec( d_pilot_rp_body_v );
     init_vec( d_cg_rp_body_v );
     init_vec( f_body_total_v );
@@ -139,123 +175,306 @@ FGInterface::FGInterface() {
     sin_latitude=cos_latitude=0;
     sin_longitude=cos_longitude=0;
     altitude_agl=0;
-}  
-
-FGInterface::FGInterface( double dt ) {
-    FGInterface();
-
-    delta_t = dt;
-    remainder = elapsed = multi_loop = 0;
 }
 
-// Destructor
-FGInterface::~FGInterface() {
-//   unbind();                 // FIXME: should be called explicitly
-}
-
-
 void
-FGInterface::init ()
+FGInterface::init () {}
+
+/**
+ * Initialize the state of the FDM.
+ *
+ * Subclasses of FGInterface may do their own, additional initialization,
+ * but there is some that is common to all.  Normally, they should call
+ * this before they begin their own init to make sure the basic structures 
+ * are set up properly.
+ */
+void
+FGInterface::common_init ()
 {
+    SG_LOG( SG_FLIGHT, SG_INFO, "Start common FDM init" );
+
+    set_inited( true );
+
+    stamp();
+    set_remainder( 0 );
+
+    // Set initial position
+    SG_LOG( SG_FLIGHT, SG_INFO, "...initializing position..." );
+    set_Longitude( fgGetDouble("/position/longitude-deg")
+                   * SGD_DEGREES_TO_RADIANS );
+    set_Latitude( fgGetDouble("/position/latitude-deg")
+                  * SGD_DEGREES_TO_RADIANS );
+    double ground_elev_m = scenery.get_cur_elev();
+    double ground_elev_ft = ground_elev_m * SG_METER_TO_FEET;
+    if ( fgGetBool("/sim/startup/onground")
+         || fgGetDouble("/position/altitude-ft") < ground_elev_ft ) {
+        fgSetDouble("/position/altitude-ft", ground_elev_ft);
+    }
+    set_Altitude( fgGetDouble("/position/altitude-ft") );
+
+    // Set ground elevation
+    SG_LOG( SG_FLIGHT, SG_INFO,
+            "...initializing ground elevation to " << ground_elev_ft
+            << "ft..." );
+    SG_LOG( SG_FLIGHT, SG_INFO, "common_init(): set ground elevation "
+            << ground_elev_ft ); 
+    base_fdm_state.set_Runway_altitude( ground_elev_ft );
+    set_Runway_altitude( ground_elev_ft );
+
+    // Set sea-level radius
+    SG_LOG( SG_FLIGHT, SG_INFO, "...initializing sea-level radius..." );
+    SG_LOG( SG_FLIGHT, SG_INFO, " lat = "
+            << fgGetDouble("/position/latitude-deg")
+            << " alt = " << fgGetDouble("/position/altitude-ft") );
+    double sea_level_radius_meters;
+    double lat_geoc;
+    sgGeodToGeoc( fgGetDouble("/position/latitude-deg")
+                    * SGD_DEGREES_TO_RADIANS,
+                  fgGetDouble("/position/altitude-ft") * SG_FEET_TO_METER,
+                  &sea_level_radius_meters, &lat_geoc );
+    set_Sea_level_radius( sea_level_radius_meters * SG_METER_TO_FEET );
+
+    // Set initial velocities
+    SG_LOG( SG_FLIGHT, SG_INFO, "...initializing velocities..." );
+    if ( !fgHasNode("/sim/startup/speed-set") ) {
+        set_V_calibrated_kts(0.0);
+    } else {
+        const string speedset = fgGetString("/sim/startup/speed-set");
+        if ( speedset == "knots" || speedset == "KNOTS" ) {
+            set_V_calibrated_kts( fgGetDouble("/velocities/airspeed-kt") );
+        } else if ( speedset == "mach" || speedset == "MACH" ) {
+            set_Mach_number( fgGetDouble("/velocities/mach") );
+        } else if ( speedset == "UVW" || speedset == "uvw" ) {
+            set_Velocities_Wind_Body(
+                                     fgGetDouble("/velocities/uBody-fps"),
+                                     fgGetDouble("/velocities/vBody-fps"),
+                                     fgGetDouble("/velocities/wBody-fps") );
+        } else if ( speedset == "NED" || speedset == "ned" ) {
+            set_Velocities_Local(
+                                 fgGetDouble("/velocities/speed-north-fps"),
+                                 fgGetDouble("/velocities/speed-east-fps"),
+                                 fgGetDouble("/velocities/speed-down-fps") );
+        } else {
+            SG_LOG( SG_FLIGHT, SG_ALERT,
+                    "Unrecognized value for /sim/startup/speed-set: "
+                    << speedset);
+            set_V_calibrated_kts( 0.0 );
+        }
+    }
+
+    // Set initial Euler angles
+    SG_LOG( SG_FLIGHT, SG_INFO, "...initializing Euler angles..." );
+    set_Euler_Angles( fgGetDouble("/orientation/roll-deg")
+                        * SGD_DEGREES_TO_RADIANS,
+                      fgGetDouble("/orientation/pitch-deg")
+                        * SGD_DEGREES_TO_RADIANS,
+                      fgGetDouble("/orientation/heading-deg")
+                        * SGD_DEGREES_TO_RADIANS );
+
+    SG_LOG( SG_FLIGHT, SG_INFO, "End common FDM init" );
 }
 
+
+/**
+ * Bind getters and setters to properties.
+ *
+ * The bind() method will be invoked after init().  Note that unlike
+ * the usual implementations of FGSubsystem::bind(), this method does
+ * not automatically pick up existing values for the properties at
+ * bind time; instead, all values are set explicitly in the init()
+ * method.
+ */
 void
 FGInterface::bind ()
 {
-                                // Time management
-  fgTie("/fdm/time/delta_t", this, 
-       &(FGInterface::get_delta_t));
+  bound = true;
 
-  // The following two can't be uncommented until we have support for
-  // the "long" data type in the property manager
-  /*  fgTie("/fdm/time/elapsed", this, 
-       &(FGInterface::get_elapsed));
+                                // Time management (read-only)
+  fgTie("/fdm/time/delta_t", this, 
+       &FGInterface::get_delta_t); // read-only
+  fgTie("/fdm/time/elapsed", this, 
+       &FGInterface::get_elapsed); // read-only
   fgTie("/fdm/time/remainder", this, 
-       &(FGInterface::get_remainder); */
+       &FGInterface::get_remainder); // read-only
   fgTie("/fdm/time/multi_loop", this, 
-       &(FGInterface::get_multi_loop));
+       &FGInterface::get_multi_loop); // read-only
 
                        // Aircraft position
-  fgTie("/position/latitude", this,
-       &(FGInterface::get_Latitude_deg),
-       &(FGInterface::set_Latitude_deg));
-  fgTie("/position/longitude", this,
-       &(FGInterface::get_Longitude_deg),
-       &(FGInterface::set_Longitude_deg));
-  fgTie("/position/altitude", this,
-       &(FGInterface::get_Altitude),
-       &(FGInterface::set_Altitude));
-  fgTie("/position/altitude-agl", this,
-       &(FGInterface::get_Altitude_AGL)); // read-only
+  fgTie("/position/latitude-deg", this,
+       &FGInterface::get_Latitude_deg,
+       &FGInterface::set_Latitude_deg,
+       false);
+  fgSetArchivable("/position/latitude-deg");
+  fgTie("/position/longitude-deg", this,
+       &FGInterface::get_Longitude_deg,
+       &FGInterface::set_Longitude_deg,
+       false);
+  fgSetArchivable("/position/longitude-deg");
+  fgTie("/position/altitude-ft", this,
+       &FGInterface::get_Altitude,
+       &FGInterface::set_Altitude,
+       false);
+  fgSetArchivable("/position/altitude-ft");
+  fgTie("/position/altitude-agl-ft", this,
+       &FGInterface::get_Altitude_AGL); // read-only
 
                                // Orientation
-  fgTie("/orientation/roll", this,
-       &(FGInterface::get_Phi_deg),
-       &(FGInterface::set_Phi_deg));
-  fgTie("/orientation/pitch", this,
-       &(FGInterface::get_Theta_deg),
-       &(FGInterface::set_Theta_deg));
-  fgTie("/orientation/heading", this,
-       &(FGInterface::get_Psi_deg),
-       &(FGInterface::set_Psi_deg));
+  fgTie("/orientation/roll-deg", this,
+       &FGInterface::get_Phi_deg,
+       &FGInterface::set_Phi_deg);
+  fgSetArchivable("/orientation/roll-deg");
+  fgTie("/orientation/pitch-deg", this,
+       &FGInterface::get_Theta_deg,
+       &FGInterface::set_Theta_deg);
+  fgSetArchivable("/orientation/pitch-deg");
+  fgTie("/orientation/heading-deg", this,
+       &FGInterface::get_Psi_deg,
+       &FGInterface::set_Psi_deg);
+  fgSetArchivable("/orientation/heading-deg");
 
                                // Calibrated airspeed
-  fgTie("/velocities/airspeed", this,
-       &(FGInterface::get_V_calibrated_kts),
-       &(FGInterface::set_V_calibrated_kts));
+  fgTie("/velocities/airspeed-kt", this,
+       &FGInterface::get_V_calibrated_kts,
+       &FGInterface::set_V_calibrated_kts,
+       false);
 
                                // Local velocities
-  fgTie("/velocities/speed-north", this,
-       &(FGInterface::get_V_north));
-  fgTie("/velocities/speed-east", this,
-       &(FGInterface::get_V_east),
-       &(FGInterface::set_V_east));
-  fgTie("/velocities/speed-down", this,
-       &(FGInterface::get_V_down),
-       &(FGInterface::set_V_down));
+//   fgTie("/velocities/speed-north-fps", this,
+//     &FGInterface::get_V_north,
+//     &FGInterface::set_V_north);
+//   fgSetArchivable("/velocities/speed-north-fps");
+//   fgTie("/velocities/speed-east-fps", this,
+//     &FGInterface::get_V_east,
+//     &FGInterface::set_V_east);
+//   fgSetArchivable("/velocities/speed-east-fps");
+//   fgTie("/velocities/speed-down-fps", this,
+//     &FGInterface::get_V_down,
+//     &FGInterface::set_V_down);
+//   fgSetArchivable("/velocities/speed-down-fps");
+                               // FIXME: Temporarily read-only, until the
+                               // incompatibilities between JSBSim and
+                               // LaRCSim are fixed (LaRCSim adds the
+                               // earth's rotation to the east velocity).
+  fgTie("/velocities/speed-north-fps", this,
+       &FGInterface::get_V_north);
+  fgTie("/velocities/speed-east-fps", this,
+       &FGInterface::get_V_east);
+  fgTie("/velocities/speed-down-fps", this,
+       &FGInterface::get_V_down);
 
                                // Relative wind
-  fgTie("/velocities/uBody", this,
-       &(FGInterface::get_uBody),
-       &(FGInterface::set_uBody));
-  fgTie("/velocities/vBody", this,
-       &(FGInterface::get_vBody),
-       &(FGInterface::set_vBody));
-  fgTie("/velocities/wBody", this,
-       &(FGInterface::get_wBody),
-       &(FGInterface::set_wBody));
+                               // FIXME: temporarily archivable, until
+                               // the NED problem is fixed.
+  fgTie("/velocities/uBody-fps", this,
+       &FGInterface::get_uBody,
+       &FGInterface::set_uBody,
+       false);
+  fgSetArchivable("/velocities/uBody-fps");
+  fgTie("/velocities/vBody-fps", this,
+       &FGInterface::get_vBody,
+       &FGInterface::set_vBody,
+       false);
+  fgSetArchivable("/velocities/vBody-fps");
+  fgTie("/velocities/wBody-fps", this,
+       &FGInterface::get_wBody,
+       &FGInterface::set_wBody,
+       false);
+  fgSetArchivable("/velocities/wBody-fps");
 
                                // Climb and slip (read-only)
-  fgTie("/velocities/vertical-speed", this,
-       &(FGInterface::get_Climb_Rate)); // read-only
-  fgTie("/velocities/side-slip", this,
-       &(FGInterface::get_Beta)); // read-only
+  fgTie("/velocities/vertical-speed-fps", this,
+       &FGInterface::get_Climb_Rate); // read-only
+  fgTie("/velocities/side-slip-rad", this,
+       &FGInterface::get_Beta); // read-only
+
+                               // Powerplant
+  for (int i = 0; i < get_num_engines(); i++) {
+    char buf[64];
+
+    sprintf(buf, "/engines/engine[%d]/rpm", i);
+    fgTie(buf, get_engine(i), &FGEngInterface::get_RPM);
+
+    sprintf(buf, "/engines/engine[%d]/egt-degf", i);
+    fgTie(buf, get_engine(i), &FGEngInterface::get_EGT);
+
+    sprintf(buf, "/engines/engine[%d]/cht-degf", i);
+    fgTie(buf, get_engine(i), &FGEngInterface::get_CHT);
+
+    sprintf(buf, "/engines/engine[%d]/oil-temperature-degf", i);
+    fgTie(buf, get_engine(i), &FGEngInterface::get_Oil_Temp);
+
+    sprintf(buf, "/engines/engine[%d]/mp-osi", i);
+    fgTie(buf, get_engine(i), &FGEngInterface::get_Manifold_Pressure);
+
+    sprintf(buf, "/engines/engine[%d]/fuel-flow-gph", i);
+    fgTie(buf, get_engine(i), &FGEngInterface::get_Fuel_Flow);
+
+    sprintf(buf, "/engines/engine[%d]/running", i);
+    fgTie(buf, get_engine(i), &FGEngInterface::get_Running_Flag);
+
+    sprintf(buf, "/engines/engine[%d]/cranking", i);
+    fgTie(buf, get_engine(i), &FGEngInterface::get_Cranking_Flag);
+
+  }
 }
 
+
+/**
+ * Unbind any properties bound to this FDM.
+ *
+ * This method allows the FDM to release properties so that a new
+ * FDM can bind them instead.
+ */
 void
 FGInterface::unbind ()
 {
+  bound = false;
+
   fgUntie("/fdm/time/delta_t");
   fgUntie("/fdm/time/elapsed");
   fgUntie("/fdm/time/remainder");
   fgUntie("/fdm/time/multi_loop");
-  fgUntie("/position/latitude");
-  fgUntie("/position/longitude");
-  fgUntie("/position/altitude");
-  fgUntie("/position/heading");
-  fgUntie("/position/pitch");
-  fgUntie("/position/roll");
-  fgUntie("/velocities/airspeed");
-  fgUntie("/velocities/speed-north");
-  fgUntie("/velocities/speed-east");
-  fgUntie("/velocities/speed-down");
-  fgUntie("/velocities/uBody");
-  fgUntie("/velocities/vBody");
-  fgUntie("/velocities/wBody");
-  fgUntie("/velocities/vertical-speed");
-  fgUntie("/velocities/side-slip");
+  fgUntie("/position/latitude-deg");
+  fgUntie("/position/longitude-deg");
+  fgUntie("/position/altitude-ft");
+  fgUntie("/position/altitude-agl-ft");
+  fgUntie("/orientation/heading-deg");
+  fgUntie("/orientation/pitch-deg");
+  fgUntie("/orientation/roll-deg");
+  fgUntie("/velocities/airspeed-kt");
+  fgUntie("/velocities/speed-north-fps");
+  fgUntie("/velocities/speed-east-fps");
+  fgUntie("/velocities/speed-down-fps");
+  fgUntie("/velocities/uBody-fps");
+  fgUntie("/velocities/vBody-fps");
+  fgUntie("/velocities/wBody-fps");
+  fgUntie("/velocities/vertical-speed-fps");
+  fgUntie("/velocities/side-slip-rad");
+  for (int i = 0; i < get_num_engines(); i++) {
+    char buf[64];
+    sprintf(buf, "/engines/engine[%d]/rpm", i);
+    fgUntie(buf);
+    sprintf(buf, "/engines/engine[%d]/egt-degf", i);
+    fgUntie(buf);
+    sprintf(buf, "/engines/engine[%d]/cht-degf", i);
+    fgUntie(buf);
+    sprintf(buf, "/engines/engine[%d]/oil-temperature-degf", i);
+    fgUntie(buf);
+    sprintf(buf, "/engines/engine[%d]/mp-osi", i);
+    fgUntie(buf);
+    sprintf(buf, "/engines/engine[%d]/fuel-flow-gph", i);
+    fgUntie(buf);
+    sprintf(buf, "/engines/engine[%d]/running", i);
+    fgUntie(buf);
+    sprintf(buf, "/engines/engine[%d]/cranking", i);
+    fgUntie(buf);
+  }
 }
 
+
+/**
+ * Update the state of the FDM (i.e. run the equations of motion).
+ */
 void
 FGInterface::update ()
 {
@@ -272,6 +491,8 @@ bool FGInterface::update( int multi_loop ) {
 void FGInterface::_updatePosition( double lat_geoc, double lon, double alt ) {
     double lat_geod, tmp_alt, sl_radius1, sl_radius2, tmp_lat_geoc;
        
+    // cout << "starting sea level rad = " << get_Sea_level_radius() << endl;
+
     sgGeocToGeod( lat_geoc, ( get_Sea_level_radius() + alt ) * SG_FEET_TO_METER,
                  &lat_geod, &tmp_alt, &sl_radius1 );
     sgGeodToGeoc( lat_geod, alt * SG_FEET_TO_METER, &sl_radius2, &tmp_lat_geoc );
@@ -291,7 +512,7 @@ void FGInterface::_updatePosition( double lat_geoc, double lon, double alt ) {
     _set_Geodetic_Position( lat_geod, lon, alt );
        
     _set_Sea_level_radius( sl_radius2 * SG_METER_TO_FEET );
-    _set_Runway_altitude( scenery.cur_elev*METERS_TO_FEET ); 
+    _set_Runway_altitude( scenery.get_cur_elev() * SG_METER_TO_FEET ); 
        
     _set_sin_lat_geocentric( lat_geoc );
     _set_cos_lat_geocentric( lat_geoc );
@@ -353,6 +574,8 @@ void FGInterface::extrapolate( int time_offset ) {
 
 // Set the altitude (force)
 void fgFDMForceAltitude(const string &model, double alt_meters) {
+    SG_LOG(SG_FLIGHT,SG_INFO, "fgFDMForceAltitude: " << alt_meters );
+    
     double sea_level_radius_meters;
     double lat_geoc;
 
@@ -363,7 +586,10 @@ void fgFDMForceAltitude(const string &model, double alt_meters) {
     base_fdm_state.set_Altitude( alt_meters * SG_METER_TO_FEET );
     base_fdm_state.set_Sea_level_radius( sea_level_radius_meters *
                                         SG_METER_TO_FEET ); 
-                                         
+               
+    cur_fdm_state->set_Altitude( alt_meters * SG_METER_TO_FEET );
+    cur_fdm_state->set_Sea_level_radius( sea_level_radius_meters * 
+           SG_METER_TO_FEET );                   
 
     // additional work needed for some flight models
     if ( model == "larcsim" ) {
@@ -372,15 +598,6 @@ void fgFDMForceAltitude(const string &model, double alt_meters) {
 }
 
 
-// Set the local ground elevation
-void fgFDMSetGroundElevation(const string &model, double ground_meters) {
-    SG_LOG( SG_FLIGHT,SG_INFO, "fgFDMSetGroundElevation: "
-           << ground_meters*SG_METER_TO_FEET ); 
-    base_fdm_state.set_Runway_altitude( ground_meters * SG_METER_TO_FEET );
-    cur_fdm_state->set_Runway_altitude( ground_meters * SG_METER_TO_FEET );
-}
-
-
 // Positions
 void FGInterface::set_Latitude(double lat) { 
     geodetic_position_v[0] = lat;
@@ -567,3 +784,7 @@ void FGInterface::_busdump(void) {
     SG_LOG(SG_FLIGHT,SG_INFO,"altitude_agl: " << altitude_agl );
 }  
 
+
+void fgToggleFDMdataLogging(void) {
+  cur_fdm_state->ToggleDataLogging();
+}