]> git.mxchange.org Git - flightgear.git/commitdiff
Tweaking the FGInterface interface so we can move it towards a more consistant
authorcurt <curt>
Tue, 16 Jan 2001 21:41:28 +0000 (21:41 +0000)
committercurt <curt>
Tue, 16 Jan 2001 21:41:28 +0000 (21:41 +0000)
fgSubsystem init() and update() interface.

16 files changed:
src/FDM/ADA.cxx
src/FDM/ADA.hxx
src/FDM/Balloon.cxx
src/FDM/Balloon.h
src/FDM/External.cxx
src/FDM/External.hxx
src/FDM/JSBSim.cxx
src/FDM/JSBSim.hxx
src/FDM/LaRCsim.cxx
src/FDM/LaRCsim.hxx
src/FDM/MagicCarpet.cxx
src/FDM/MagicCarpet.hxx
src/FDM/flight.cxx
src/FDM/flight.hxx
src/Main/fg_init.cxx
src/Main/main.cxx

index 5457d063559a6d34ea6a25d7cb8ed643a963c17e..61385090edc9a1d7a6e0b82d2e5a96ebba74fdfb 100644 (file)
@@ -142,9 +142,18 @@ struct {
 #define anzg sixdof_to_visuals.anzg
 
 
+FGADA::FGADA( double dt ) {
+    set_delta_t( dt );
+}
+
+
+FGADA::~FGADA() {
+}
+
+
 // Initialize the ADA flight model, dt is the time increment
 // for each subsequent iteration through the EOM
-bool FGADA::init( double dt ) {
+void FGADA::init() {
     // cout << "FGADA::init()" << endl;
 
     char Buffer[numberofbytes];
@@ -165,8 +174,6 @@ bool FGADA::init( double dt ) {
        int result = fdmsock->write(Buffer, numberofbytes);
        printf("Connection established.\n");
     }
-
-    return true;
 }
 
 
index 2ca68464ba0d637a2b66eb3cac2bfabb45ef7305..2d62d2d0bf81c3a94d27e43b4b4523bcb370098b 100644 (file)
@@ -72,8 +72,11 @@ private:
 
 public:
 
+    FGADA( double dt );
+    ~FGADA();
+
     // reset flight params to a specific position 
-    bool init( double dt );
+    void init();
 
     // update position based on inputs, positions, velocities, etc.
     bool update( int multiloop );
index af7a12350b54fbaaff584c3b154290140b69cbdf..06ea98db02fd374237cb7f7d2d36e29ddceb1ba7 100644 (file)
@@ -62,18 +62,25 @@ HISTORY
 /****************************************************************************/
 
 
+FGBalloonSim::FGBalloonSim( double dt ) {
+    //set the dt of the model
+    current_balloon.set_dt(dt);
+}
+
+
+FGBalloonSim::~FGBalloonSim() {
+}
+
+
 // Initialize the BalloonSim flight model, dt is the time increment for
 // each subsequent iteration through the EOM
-bool FGBalloonSim::init( double dt ) {
+void FGBalloonSim::init() {
     sgVec3 temp;
 
     FG_LOG( FG_FLIGHT, FG_INFO, "Starting initializing BalloonSim" );
 
     FG_LOG( FG_FLIGHT, FG_INFO, "  created a balloon" );
 
-    //set the dt of the model
-    current_balloon.set_dt(dt);
-
     //set position
     sgSetVec3( temp,
        get_Latitude(), 
@@ -96,8 +103,6 @@ bool FGBalloonSim::init( double dt ) {
     current_balloon.setVelocity( temp );
 
     FG_LOG( FG_FLIGHT, FG_INFO, "Finished initializing BalloonSim" );
-
-    return true;
 }
 
 
index e6e0ae095a943dbb4af766d49922cb63d2c80ca2..5661bebbcb8341a5c121aea8f58d98ea71fc0323 100644 (file)
@@ -55,17 +55,6 @@ HISTORY
 /* DECLARATIONS                                                            */
 /****************************************************************************/
 
-// reset flight params to a specific position 
-// int fgBalloonSimInit(double dt, FGInterface& f);
-
-// update position based on inputs, positions, velocities, etc.
-// int fgBalloonSimUpdate(FGInterface& f, int multiloop);
-
-// Convert from the FGInterface struct to the BalloonSim
-// int FGInterface_2_fgBalloonSim (FGInterface& f);
-
-// Convert from the BalloonSim to the FGInterface struct
-// int fgBalloonSim_2_FGInterface (FGInterface& f);
 
 class FGBalloonSim: public FGInterface {
 
@@ -73,6 +62,9 @@ class FGBalloonSim: public FGInterface {
 
 public:
 
+    FGBalloonSim( double dt );
+    ~FGBalloonSim();
+
     // copy FDM state to BalloonSim structures
     bool copy_to_BalloonSim();
 
@@ -80,7 +72,7 @@ public:
     bool copy_from_BalloonSim();
 
     // reset flight params to a specific position 
-    bool init( double dt );
+    void init();
 
     // update position based on inputs, positions, velocities, etc.
     bool update( int multiloop );
index 8790762d3ef9a91bbfb6625d6f17c01c71807dad..f196afa48d011419af2f73872f4cefe42ac51238 100644 (file)
 #include "External.hxx"
 
 
+FGExternal::FGExternal( double dt ) {
+    set_delta_t( dt );
+}
+
+
+FGExternal::~FGExternal() {
+}
+
+
 // Initialize the External flight model, dt is the time increment
 // for each subsequent iteration through the EOM
-bool FGExternal::init( double dt ) {
+void FGExternal::init() {
     // cout << "FGExternal::init()" << endl;
 
     // set valid time for this record
     stamp_time();
-
-    return true;
 }
 
 
index a9e8c8ef8691418b2822cf7ee6c760a3336f5c59..8d2adef3813940f1ec413aed7e17398efdb64328 100644 (file)
 class FGExternal: public FGInterface {
 
 public:
+    FGExternal::FGExternal( double dt );
+    FGExternal::~FGExternal();
+
     // reset flight params to a specific position 
-    bool init( double dt );
+    void init();
 
     // update position based on inputs, positions, velocities, etc.
     bool update( int multiloop );
index a2f61c59a4394e32b5a32198344301ecee126767..39af887e4acbaae3c6ad2c0cf66ba1f785ec54ba 100644 (file)
@@ -58,7 +58,7 @@
 
 /******************************************************************************/
 
-FGJSBsim::FGJSBsim(void) {
+FGJSBsim::FGJSBsim( double dt ) {
     bool result;
    
     runcount=0;
@@ -72,7 +72,7 @@ FGJSBsim::FGJSBsim(void) {
 
     FGPath engine_path( globals->get_fg_root() );
     engine_path.append( "Engine" );
-    float dt = 1.0 / fgGetInt("/sim/model-hz");
+    set_delta_t( dt );
     fdmex->GetState()->Setdt( dt );
 
     result = fdmex->LoadModel( aircraft_path.str(),
@@ -99,7 +99,7 @@ FGJSBsim::~FGJSBsim(void) {
 // Initialize the JSBsim flight model, dt is the time increment for
 // each subsequent iteration through the EOM
 
-bool FGJSBsim::init( double dt ) {
+void FGJSBsim::init() {
 
     bool result;
 
@@ -112,7 +112,7 @@ bool FGJSBsim::init( double dt ) {
     FGPath engine_path( globals->get_fg_root() );
     engine_path.append( "Engine" );
 
-    fdmex->GetState()->Setdt( dt );
+    fdmex->GetState()->Setdt( get_delta_t() );
 
     result = fdmex->LoadModel( aircraft_path.str(),
                               engine_path.str(),
@@ -125,7 +125,7 @@ bool FGJSBsim::init( double dt ) {
        FG_LOG( FG_FLIGHT, FG_INFO, "  aircraft "
                << fgGetString("/sim/aircraft")
                << " does not exist" );
-       return false;
+       exit(-1);
     }
     
     fdmex->GetAtmosphere()->UseInternal();
@@ -191,8 +191,6 @@ bool FGJSBsim::init( double dt ) {
     FG_LOG( FG_FLIGHT, FG_INFO, "  set dt" );
 
     FG_LOG( FG_FLIGHT, FG_INFO, "Finished initializing JSBSim" );
-
-    return true;
 }
 
 /******************************************************************************/
index 980ba90187e68b9f2754429cbb7a09e0ddbcee93..89b66ff5f8115660d14e9a3d3a2e82e24bf4b854 100644 (file)
@@ -79,7 +79,7 @@ class FGJSBsim: public FGInterface {
 
 public:
     /// Constructor
-    FGJSBsim::FGJSBsim(void);
+    FGJSBsim::FGJSBsim( double dt );
     /// Destructor
     FGJSBsim::~FGJSBsim();
 
@@ -90,7 +90,7 @@ public:
     bool copy_from_JSBsim();
 
     /// Reset flight params to a specific position
-    bool init( double dt );
+    void init();
 
     /// @name Position Parameter Set
     //@{
index c2117842a8f955d25ea928450efcd04d9163fece..3de7f911fc0071a6ce61b3e1f49fdfa0bf7c74d1 100644 (file)
@@ -38,7 +38,9 @@
 #include "IO360.hxx"
 #include "LaRCsim.hxx"
 
-FGLaRCsim::FGLaRCsim(void) {
+FGLaRCsim::FGLaRCsim( double dt ) {
+    set_delta_t( dt );
+
     ls_toplevel_init( 0.0, 
                      (char *)fgGetString("/sim/aircraft").c_str() );
     lsic=new LaRCsimIC; //this needs to be brought up after LaRCsim is
@@ -59,13 +61,13 @@ FGLaRCsim::~FGLaRCsim(void) {
 
 // Initialize the LaRCsim flight model, dt is the time increment for
 // each subsequent iteration through the EOM
-bool FGLaRCsim::init( double dt ) {
+void FGLaRCsim::init() {
 
     speed_up = fgGetValue("/sim/speed-up", true);
     
-    ls_set_model_dt(dt);
+    ls_set_model_dt( get_delta_t() );
     // Initialize our little engine that hopefully might
-    eng.init(dt);
+    eng.init( get_delta_t() );
     // 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)
 
@@ -102,8 +104,6 @@ bool FGLaRCsim::init( double dt ) {
 
     // set valid time for this record
     stamp_time();
-
-    return true;
 }
 
 
index 60be3614cced3d0033bec74361885d8c94fd1ac8..ee884af87b36d28a1fe01d0da6cb0772d6d8c046 100644 (file)
@@ -41,7 +41,7 @@ class FGLaRCsim: public FGInterface {
     SGValue * speed_up;
     
 public:
-    FGLaRCsim(void);
+    FGLaRCsim( double dt );
     ~FGLaRCsim(void);
     
     // copy FDM state to LaRCsim structures
@@ -51,7 +51,7 @@ public:
     bool copy_from_LaRCsim();
 
     // reset flight params to a specific position 
-    bool init( double dt );
+    void init();
 
     // update position based on inputs, positions, velocities, etc.
     bool update( int multiloop );
index ea8e43bd663d8972de205fffb97b6619bd60e771..67aeea398d27093855ff135d165c595ec09462bf 100644 (file)
 #include "MagicCarpet.hxx"
 
 
+FGMagicCarpet::FGMagicCarpet( double dt ) {
+    set_delta_t( dt );
+}
+
+
+FGMagicCarpet::~FGMagicCarpet() {
+}
+
+
 // Initialize the Magic Carpet flight model, dt is the time increment
 // for each subsequent iteration through the EOM
-bool FGMagicCarpet::init( double dt ) {
+void FGMagicCarpet::init() {
     // set valid time for this record
     stamp_time();
-    model_hz = fgGetValue("/sim/model-hz", true);
-
-    return true;
 }
 
 
@@ -47,8 +53,7 @@ bool FGMagicCarpet::init( double dt ) {
 bool FGMagicCarpet::update( int multiloop ) {
     // cout << "FGLaRCsim::update()" << endl;
 
-    double time_step = (1.0 / model_hz->getIntValue()) *
-       multiloop;
+    double time_step = get_delta_t() * multiloop;
 
     // speed and distance traveled
     double speed = controls.get_throttle( 0 ) * 2000; // meters/sec
index 4c11d3316fd1519be3f6abca4bc11f543781cdb3..e6f6efe80710e64c58d7381901b24a9a8bf32f3c 100644 (file)
 class FGMagicCarpet: public FGInterface {
 
 public:
+    FGMagicCarpet::FGMagicCarpet( double dt );
+    FGMagicCarpet::~FGMagicCarpet();
+
     // reset flight params to a specific position 
-    bool init( double dt );
+    void init();
 
     // update position based on inputs, positions, velocities, etc.
     bool update( int multiloop );
 
-private:
-    SGValue * model_hz;
 };
 
 
index 43163d4cd44aeff1b0a7a8097fcbdebdc827956c..5e3dac21d84b7bb832f2f8ce6bc28e3c28c9479a 100644 (file)
@@ -52,7 +52,7 @@ inline void init_vec(FG_VECTOR_3 vec) {
     vec[0] = 0.0; vec[1] = 0.0; vec[2] = 0.0;
 }  
 
-FGEngInterface::FGEngInterface(void) {
+FGEngInterface::FGEngInterface() {
     
     // inputs
     Throttle=0;
@@ -73,7 +73,7 @@ FGEngInterface::~FGEngInterface(void) {
 
 
 // Constructor
-FGInterface::FGInterface(void) {
+FGInterface::FGInterface() {
     init_vec( d_pilot_rp_body_v );
     init_vec( d_cg_rp_body_v );
     init_vec( f_body_total_v );
@@ -141,6 +141,12 @@ FGInterface::FGInterface(void) {
     altitude_agl=0;
 }  
 
+FGInterface::FGInterface( double dt ) {
+    FGInterface();
+
+    delta_t = dt;
+    remainder = elapsed = multi_loop = 0;
+}
 
 // Destructor
 FGInterface::~FGInterface() {
@@ -151,13 +157,29 @@ FGInterface::~FGInterface() {
 void
 FGInterface::init ()
 {
-  init(1.0 / fgGetInt("/sim/model-hz"));
 }
 
 void
 FGInterface::bind ()
 {
-                               // Aircraft position
+                                // Time management
+  fgTie("/fdm/time/delta_t", this, 
+       &(FGInterface::get_delta_t),
+       &(FGInterface::set_delta_t));
+
+  // 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),
+       &(FGInterface::set_elapsed));
+  fgTie("/fdm/time/remainder", this, 
+       &(FGInterface::get_remainder),
+       &(FGInterface::set_remainder)); */
+  fgTie("/fdm/time/multi_loop", this, 
+       &(FGInterface::get_multi_loop),
+       &(FGInterface::set_multi_loop));
+
+                       // Aircraft position
   fgTie("/position/latitude", this,
        &(FGInterface::get_Latitude_deg),
        &(FGInterface::set_Latitude_deg));
@@ -217,6 +239,10 @@ FGInterface::bind ()
 void
 FGInterface::unbind ()
 {
+  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");
@@ -241,12 +267,6 @@ FGInterface::update ()
 }
 
 
-bool FGInterface::init( double dt ) {
-    cout << "dummy init() ... SHOULDN'T BE CALLED!" << endl;
-    return false;
-}
-
-
 bool FGInterface::update( int multi_loop ) {
     cout << "dummy update() ... SHOULDN'T BE CALLED!" << endl;
     return false;
index 7014f82d5b1770107499cba95f04ebe6938f9111..205e442aa53bead039fa994a222ba27284ba972a 100644 (file)
@@ -167,6 +167,21 @@ class FGInterface : public FGSubsystem {
 
 private:
   
+    // periodic update management variable.  This is a scheme to run
+    // the fdm with a fixed delta-t.  We control how many iteration of
+    // the fdm to run with the fixed dt based on the elapsed time from
+    // the last update.  This allows us to maintain sync with the real
+    // time clock, even though each frame could take a random amount
+    // of time.  Since "dt" is unlikely to divide evenly into the
+    // elapse time, we keep track of the remainder and add it into the
+    // next elapsed time.  This yields a small amount of temporal
+    // jitter ( < dt ) but in practice seems to work well.
+
+    double delta_t;            // delta "t"
+    long elapsed;              // time elapsed since last run
+    long remainder;            // remainder time from last run
+    int multi_loop;            // number of iterations of "delta_t" to run
+
     // Pilot location rel to ref pt
     FG_VECTOR_3 d_pilot_rp_body_v;
 
@@ -261,9 +276,7 @@ private:
     SGTimeStamp next_stamp;           // time this record is valid
 
 protected:
-    virtual bool init( double dt );
-
-    void _busdump(void);
+     void _busdump(void);
     void _updatePosition( double lat_geoc, double lon, double alt );
     void _updateWeather( void );
 
@@ -419,7 +432,8 @@ protected:
 
 public:
   
-    FGInterface(void);
+    FGInterface();
+    FGInterface( double dt );
     virtual ~FGInterface();
 
     virtual void init ();
@@ -457,6 +471,16 @@ public:
        FG_EXTERNAL = 10
     };
 
+    // time and update management values
+    inline double get_delta_t() const { return delta_t; }
+    inline void set_delta_t( double dt ) { delta_t = dt; }
+    inline long get_elapsed() const { return elapsed; }
+    inline void set_elapsed( long e ) { elapsed = e; }
+    inline long get_remainder() const { return remainder; }
+    inline void set_remainder( long r ) { remainder = r; }
+    inline int get_multi_loop() const { return multi_loop; }
+    inline void set_multi_loop( int ml ) { multi_loop = ml; }
+
     // Positions
     virtual void set_Latitude(double lat);       // geocentric
     virtual void set_Longitude(double lon);    
index 565edd6d93af026777c1297d177840368f1ba9a9..43bd1d3265492294d60b818cb35b4e661c649879 100644 (file)
@@ -488,19 +488,21 @@ bool fgInitSubsystems( void ) {
            "Current terrain elevation after tile mgr init " <<
            scenery.cur_elev );
 
+    double dt = 1.0 / fgGetInt("/sim/model-hz");
+
     const string &model = fgGetString("/sim/flight-model");
     if (model == "larcsim") {
-       cur_fdm_state = new FGLaRCsim;
+       cur_fdm_state = new FGLaRCsim( dt );
     } else if (model == "jsb") {
-       cur_fdm_state = new FGJSBsim;
+       cur_fdm_state = new FGJSBsim( dt );
     } else if (model == "ada") {
-       cur_fdm_state = new FGADA;
+       cur_fdm_state = new FGADA( dt );
     } else if (model == "balloon") {
-       cur_fdm_state = new FGBalloonSim;
+       cur_fdm_state = new FGBalloonSim( dt );
     } else if (model == "magic") {
-       cur_fdm_state = new FGMagicCarpet;
+       cur_fdm_state = new FGMagicCarpet( dt );
     } else if (model == "external") {
-       cur_fdm_state = new FGExternal;
+       cur_fdm_state = new FGExternal( dt );
     } else {
        FG_LOG(FG_GENERAL, FG_ALERT,
               "Unrecognized flight model '" << model
index 204bf3a417c9da57409c6967ec846bd1141d4d52..5ee81ab933f316e42a0ed78f50113f0c07fb4cd3 100644 (file)
@@ -140,9 +140,6 @@ static int global_multi_loop;
 // attempt to avoid a large bounce at startup
 static bool initial_freeze = true;
 
-// Another hack
-int use_signals = 0;
-
 // forward declaration
 void fgReshape( int width, int height );
 
@@ -972,25 +969,24 @@ static void fgMainLoop( void ) {
 #endif
 
     // Run flight model
-    if ( ! use_signals ) {
-       // Calculate model iterations needed for next frame
-       elapsed += remainder;
-
-       global_multi_loop = (int)(((double)elapsed * 0.000001) * 
-                          fgGetInt("/sim/model-hz"));
-       remainder = elapsed - ( (global_multi_loop*1000000) / 
-                               fgGetInt("/sim/model-hz") );
-       FG_LOG( FG_ALL, FG_DEBUG, 
-               "Model iterations needed = " << global_multi_loop
-               << ", new remainder = " << remainder );
+
+    // Calculate model iterations needed for next frame
+    elapsed += remainder;
+
+    global_multi_loop = (int)(((double)elapsed * 0.000001) * 
+                             fgGetInt("/sim/model-hz"));
+    remainder = elapsed - ( (global_multi_loop*1000000) / 
+                           fgGetInt("/sim/model-hz") );
+    FG_LOG( FG_ALL, FG_DEBUG, 
+           "Model iterations needed = " << global_multi_loop
+           << ", new remainder = " << remainder );
        
-       // flight model
-       if ( global_multi_loop > 0 ) {
-           fgUpdateTimeDepCalcs(global_multi_loop, remainder);
-       } else {
-           FG_LOG( FG_ALL, FG_DEBUG, 
-                   "Elapsed time is zero ... we're zinging" );
-       }
+    // flight model
+    if ( global_multi_loop > 0 ) {
+       fgUpdateTimeDepCalcs(global_multi_loop, remainder);
+    } else {
+       FG_LOG( FG_ALL, FG_DEBUG, 
+               "Elapsed time is zero ... we're zinging" );
     }
 
 #if ! defined( macintosh )
@@ -1129,12 +1125,6 @@ static void fgIdleFunction ( void ) {
        // setup OpenGL view parameters
        fgInitVisuals();
 
-       if ( use_signals ) {
-           // init timer routines, signals, etc.  Arrange for an alarm
-           // signal to be generated, etc.
-           fgInitTimeDepCalcs();
-       }
-
        idle_state++;
     } else if ( idle_state == 5 ) {