Revision: 6412
          http://playerstage.svn.sourceforge.net/playerstage/?rev=6412&view=rev
Author:   robotos
Date:     2008-05-10 03:33:22 -0700 (Sat, 10 May 2008)

Log Message:
-----------
Changing the updating algorithm to something not naive

Modified Paths:
--------------
    code/gazebo/trunk/server/Simulator.cc
    code/gazebo/trunk/server/Simulator.hh

Modified: code/gazebo/trunk/server/Simulator.cc
===================================================================
--- code/gazebo/trunk/server/Simulator.cc       2008-05-10 06:58:37 UTC (rev 
6411)
+++ code/gazebo/trunk/server/Simulator.cc       2008-05-10 10:33:22 UTC (rev 
6412)
@@ -68,8 +68,10 @@
 
   this->xmlFile=NULL;
   this->gazeboConfig=NULL;
-  this->prevPhysicsTime=0.0;
-  this->prevRenderTime=0.0;
+  this->checkpoint=0.0;
+  this->physicsUpdates=0;
+  this->renderUpdates=0;
+
 }
 
 
////////////////////////////////////////////////////////////////////////////////
@@ -227,11 +229,21 @@
   while (!this->userQuit)
   {    
     bool updated=false;
+     
+    //During 3 seconds we want to keep balance between how time pass and 
update limits
+    //this is a time slot
+    if ((this->checkpoint + 3 )< this->GetRealTime())
+    {
+      this->checkpoint = this->GetRealTime();
+      this->physicsUpdates = 0;
+      this->renderUpdates = 0; 
+    }
 
       // Update the physics engine
-    double currentPhysicsTime = this->GetRealTime();
-
-    if ((currentPhysicsTime - this->prevPhysicsTime) >= maxPhysicsUpdateRate) 
+      // maxPhysicsUpdateRate * physicsUpdates means how much we have 
_advanced_ in this time slot so far.
+      // getRealTime - ckeckpoint means our +current point_ in the slot, we 
only update if our current point has 
+      // surpassed what we have already advanced.
+    if ((this->GetRealTime() - this->checkpoint) > (maxPhysicsUpdateRate * 
this->physicsUpdates)) 
     {
 
       if (!this->GetUserPause() && !this->GetUserStep() ||
@@ -250,31 +262,28 @@
 
       World::Instance()->Update(); //physics
 
-      this->prevPhysicsTime = currentPhysicsTime;
+      this->physicsUpdates++;
       updated=true;
     }
 
     // Update the rendering and gui
-    double currentRenderTime = this->GetRealTime();
-
-    if ((currentRenderTime - this->prevRenderTime) >= maxRenderUpdateRate)
+    if ((this->GetRealTime() - this->checkpoint) > (maxRenderUpdateRate * 
this->renderUpdates))
     {
       gazebo::OgreAdaptor::Instance()->Render(); 
-      this->prevRenderTime = currentRenderTime;
       this->gui->Update();
-      this->prevRenderTime = currentRenderTime;
+      this->renderUpdates++;
       updated=true;
     }
 
     if (!updated)
     {
       double nextUpdate;
-      nextUpdate=MAX(this->prevRenderTime+maxRenderUpdateRate, 
this->prevPhysicsTime+maxPhysicsUpdateRate);
-      int realStep = static_cast<int>(this->GetRealTime() - nextUpdate);
+      nextUpdate=MIN(this->renderUpdates+maxRenderUpdateRate, 
this->renderUpdates+maxPhysicsUpdateRate);
+      int realStep = static_cast<int>(nextUpdate - this->GetRealTime());
       struct timespec waiting;
       waiting.tv_sec=0;
-      waiting.tv_nsec=realStep *1000000; //TODO: binary
-      nanosleep(&waiting,0);
+      waiting.tv_nsec=realStep *1000000000; //nanoseconds to seconds
+      //nanosleep(&waiting,0);
     }
   }
 }

Modified: code/gazebo/trunk/server/Simulator.hh
===================================================================
--- code/gazebo/trunk/server/Simulator.hh       2008-05-10 06:58:37 UTC (rev 
6411)
+++ code/gazebo/trunk/server/Simulator.hh       2008-05-10 10:33:22 UTC (rev 
6412)
@@ -160,8 +160,13 @@
 
     /// Current simulation time
     private: double simTime, pauseTime, startTime;
-    private: double prevPhysicsTime, prevRenderTime;
 
+    //upper limits on updating
+    //how many updates we have done in this slot
+    private: int physicsUpdates, renderUpdates;
+    // when the slot started
+    private: double checkpoint;
+
     // UserIteractions 
     /// \brief Set to true to pause the simulation
     private: bool userPause;


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

-------------------------------------------------------------------------
This SF.net email is sponsored by the 2008 JavaOne(SM) Conference 
Don't miss this year's exciting event. There's still time to save $100. 
Use priority code J8TL2D2. 
http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone
_______________________________________________
Playerstage-commit mailing list
Playerstage-commit@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/playerstage-commit

Reply via email to