From: curt Date: Tue, 16 Jan 2001 21:41:28 +0000 (+0000) Subject: Tweaking the FGInterface interface so we can move it towards a more consistant X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=d346e53aeec57c83bcfb560b5ef8b7de465741c9;p=flightgear.git Tweaking the FGInterface interface so we can move it towards a more consistant fgSubsystem init() and update() interface. --- diff --git a/src/FDM/ADA.cxx b/src/FDM/ADA.cxx index 5457d0635..61385090e 100644 --- a/src/FDM/ADA.cxx +++ b/src/FDM/ADA.cxx @@ -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; } diff --git a/src/FDM/ADA.hxx b/src/FDM/ADA.hxx index 2ca68464b..2d62d2d0b 100644 --- a/src/FDM/ADA.hxx +++ b/src/FDM/ADA.hxx @@ -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 ); diff --git a/src/FDM/Balloon.cxx b/src/FDM/Balloon.cxx index af7a12350..06ea98db0 100644 --- a/src/FDM/Balloon.cxx +++ b/src/FDM/Balloon.cxx @@ -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; } diff --git a/src/FDM/Balloon.h b/src/FDM/Balloon.h index e6e0ae095..5661bebbc 100644 --- a/src/FDM/Balloon.h +++ b/src/FDM/Balloon.h @@ -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 ); diff --git a/src/FDM/External.cxx b/src/FDM/External.cxx index 8790762d3..f196afa48 100644 --- a/src/FDM/External.cxx +++ b/src/FDM/External.cxx @@ -24,15 +24,22 @@ #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; } diff --git a/src/FDM/External.hxx b/src/FDM/External.hxx index a9e8c8ef8..8d2adef38 100644 --- a/src/FDM/External.hxx +++ b/src/FDM/External.hxx @@ -31,8 +31,11 @@ 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 ); diff --git a/src/FDM/JSBSim.cxx b/src/FDM/JSBSim.cxx index a2f61c59a..39af887e4 100644 --- a/src/FDM/JSBSim.cxx +++ b/src/FDM/JSBSim.cxx @@ -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; } /******************************************************************************/ diff --git a/src/FDM/JSBSim.hxx b/src/FDM/JSBSim.hxx index 980ba9018..89b66ff5f 100644 --- a/src/FDM/JSBSim.hxx +++ b/src/FDM/JSBSim.hxx @@ -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 //@{ diff --git a/src/FDM/LaRCsim.cxx b/src/FDM/LaRCsim.cxx index c2117842a..3de7f911f 100644 --- a/src/FDM/LaRCsim.cxx +++ b/src/FDM/LaRCsim.cxx @@ -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; } diff --git a/src/FDM/LaRCsim.hxx b/src/FDM/LaRCsim.hxx index 60be3614c..ee884af87 100644 --- a/src/FDM/LaRCsim.hxx +++ b/src/FDM/LaRCsim.hxx @@ -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 ); diff --git a/src/FDM/MagicCarpet.cxx b/src/FDM/MagicCarpet.cxx index ea8e43bd6..67aeea398 100644 --- a/src/FDM/MagicCarpet.cxx +++ b/src/FDM/MagicCarpet.cxx @@ -32,14 +32,20 @@ #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 diff --git a/src/FDM/MagicCarpet.hxx b/src/FDM/MagicCarpet.hxx index 4c11d3316..e6f6efe80 100644 --- a/src/FDM/MagicCarpet.hxx +++ b/src/FDM/MagicCarpet.hxx @@ -31,14 +31,15 @@ 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; }; diff --git a/src/FDM/flight.cxx b/src/FDM/flight.cxx index 43163d4cd..5e3dac21d 100644 --- a/src/FDM/flight.cxx +++ b/src/FDM/flight.cxx @@ -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; diff --git a/src/FDM/flight.hxx b/src/FDM/flight.hxx index 7014f82d5..205e442aa 100644 --- a/src/FDM/flight.hxx +++ b/src/FDM/flight.hxx @@ -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); diff --git a/src/Main/fg_init.cxx b/src/Main/fg_init.cxx index 565edd6d9..43bd1d326 100644 --- a/src/Main/fg_init.cxx +++ b/src/Main/fg_init.cxx @@ -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 diff --git a/src/Main/main.cxx b/src/Main/main.cxx index 204bf3a41..5ee81ab93 100644 --- a/src/Main/main.cxx +++ b/src/Main/main.cxx @@ -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 ) {