]> git.mxchange.org Git - simgear.git/commitdiff
Extend SGSubsystemGroup, to allow running a fixed dt time, internally
authorJames Turner <jmt@Dulux.local>
Wed, 16 Jun 2010 17:02:41 +0000 (18:02 +0100)
committerJames Turner <jmt@Dulux.local>
Wed, 16 Jun 2010 17:02:41 +0000 (18:02 +0100)
simgear/structure/subsystem_mgr.cxx
simgear/structure/subsystem_mgr.hxx

index ee9c15d1750dab1ab90d5e02b1612f1a4712fa1d..26f026cd279976ecb49c876d70ced86ae0266610 100644 (file)
@@ -104,7 +104,9 @@ void SGSubsystem::stamp(const string& name)
 // Implementation of SGSubsystemGroup.
 ////////////////////////////////////////////////////////////////////////
 
-SGSubsystemGroup::SGSubsystemGroup ()
+SGSubsystemGroup::SGSubsystemGroup () :
+  _fixedUpdateTime(-1.0),
+  _updateTimeRemainder(0.0)
 {
 }
 
@@ -157,18 +159,31 @@ SGSubsystemGroup::unbind ()
 void
 SGSubsystemGroup::update (double delta_time_sec)
 {
-    for (unsigned int i = 0; i < _members.size(); i++)
-    {
-         SGTimeStamp timeStamp = SGTimeStamp::now();
-         _members[i]->update(delta_time_sec); // indirect call
-         timeStamp = timeStamp - SGTimeStamp::now();
-         double b = timeStamp.toUSecs();
-         _members[i]->updateExecutionTime(b);
-         double threshold = _members[i]->getTimeWarningThreshold();
-         if (( b > threshold ) && (b > 10000)) {
-             _members[i]->printTimingInformation(b);
-         }
+    int loopCount = 1;
+    // if dt == 0.0, we are paused, so we need to run one iteration
+    // of our members; if we have a fixed update time, we compute a
+    // loop count, and locally adjust dt
+    if ((delta_time_sec > 0.0) && (_fixedUpdateTime > 0.0)) {
+      double localDelta = delta_time_sec + _updateTimeRemainder;
+      loopCount = SGMiscd::roundToInt(localDelta / _fixedUpdateTime);
+      _updateTimeRemainder = delta_time_sec - (loopCount * _fixedUpdateTime);
+      delta_time_sec = _fixedUpdateTime;
     }
+
+    while (loopCount-- > 0) {
+      for (unsigned int i = 0; i < _members.size(); i++)
+      {
+           SGTimeStamp timeStamp = SGTimeStamp::now();
+           _members[i]->update(delta_time_sec); // indirect call
+           timeStamp = timeStamp - SGTimeStamp::now();
+           double b = timeStamp.toUSecs();
+           _members[i]->updateExecutionTime(b);
+           double threshold = _members[i]->getTimeWarningThreshold();
+           if (( b > threshold ) && (b > 10000)) {
+               _members[i]->printTimingInformation(b);
+           }
+      }
+    } // of multiple update loop
 }
 
 void 
@@ -233,6 +248,12 @@ SGSubsystemGroup::remove_subsystem (const string &name)
     }
 }
 
+void
+SGSubsystemGroup::set_fixed_update_time(double dt)
+{
+  _fixedUpdateTime = dt;
+}
+
 void
 SGSubsystemGroup::Member::printTimingStatistics ()
 {
index a751d30699426c14561447c1acbd741e78421384..87654d9c51e52e077586dc6837ccbf3d9ba7fbc9 100644 (file)
@@ -317,6 +317,10 @@ public:
 
     void collectDebugTiming(bool collect);
 
+    /**
+     * 
+     */
+    void set_fixed_update_time(double fixed_dt);
 private:
 
     class Member {
@@ -345,6 +349,9 @@ private:
     Member * get_member (const string &name, bool create = false);
 
     vector<Member *> _members;
+    
+    double _fixedUpdateTime;
+    double _updateTimeRemainder;
 };
 
 
@@ -376,6 +383,7 @@ public:
     enum GroupType {
         INIT = 0,
         GENERAL,
+        FDM,  ///< flight model, autopilot, instruments that run coupled
         MAX_GROUPS
     };