/home/fwph/code/wurde/rde/core/Time.H

Go to the documentation of this file.
00001 #ifndef ROBOT_TIME_H
00002 #define ROBOT_TIME_H
00003 
00029 //$Id: Time.H 68 2007-02-01 17:56:57Z fwph $
00030 #include <time.h>
00031 #include <sys/time.h>
00032 
00033 namespace WURDE{
00034        
00044         class Time {
00045               
00046         public:
00047                 // Constructors and Destructor
00048                 Time(){
00049                         gettimeofday(&m_tv,&m_tz);
00050                 }
00051                
00052                 Time(const Time &m){
00053                         m_tv=m.m_tv;
00054                         m_tz=m.m_tz;
00055                 }
00056                
00057                 Time(time_t value){
00058                         Time();
00059                        
00060                         m_tv.tv_sec=value;
00061                         m_tv.tv_usec=0;
00062                 }
00063                
00064                 Time(struct timeval _tv){
00065                         Time();
00066                         m_tv=_tv;
00067                 }
00068                
00069                 Time(struct timespec ts){
00070                         Time();
00071                         m_tv.tv_sec=ts.tv_sec;
00072                         m_tv.tv_usec=ts.tv_nsec/1000;  
00073                        
00074                 }
00075                
00076                 ~Time(){}
00077                
00078                
00079                 // Operators with itself
00080                
00081                 const Time &operator=(const Time &right){
00082                         m_tv=right.m_tv;
00083                         m_tz=right.m_tz;
00084                        
00085                         return *this;
00086                 }
00087                
00088                 bool operator==(const Time &right) const{
00089                         if(m_tv.tv_sec==right.m_tv.tv_sec&&
00090                            m_tv.tv_usec==right.m_tv.tv_usec){
00091                                 return true;
00092                         }
00093                        
00094                         return false;
00095                 }
00096                
00097                 bool operator!=(const Time &right) const{
00098                         if(m_tv.tv_sec!=right.m_tv.tv_sec||
00099                            m_tv.tv_usec!=right.m_tv.tv_usec){
00100                                 return true;
00101                         }
00102                        
00103                         return false;
00104                 }
00105                
00106                 bool operator<(const Time &right) const{
00107                         if(m_tv.tv_sec<right.m_tv.tv_sec||
00108                            (m_tv.tv_sec==right.m_tv.tv_sec&&
00109                             m_tv.tv_usec<right.m_tv.tv_usec)){
00110                                 return true;
00111                         }
00112                        
00113                         return false;
00114                 }
00115                
00116                 bool operator>(const Time &right) const{
00117                         if(m_tv.tv_sec>right.m_tv.tv_sec||
00118                            (m_tv.tv_sec==right.m_tv.tv_sec&&
00119                             m_tv.tv_usec>right.m_tv.tv_usec)){
00120                                 return true;
00121                         }
00122                        
00123                         return false;
00124                 }
00125                
00126                 bool operator<=(const Time &right) const{
00127                         if(m_tv.tv_sec>right.m_tv.tv_sec||
00128                            (m_tv.tv_sec==right.m_tv.tv_sec&&
00129                             m_tv.tv_usec>right.m_tv.tv_usec)){
00130                                 return false;
00131                         }
00132                         return true;
00133                 }
00134                
00135                 bool operator>=(const Time &right) const{
00136                         if(m_tv.tv_sec<right.m_tv.tv_sec||
00137                            (m_tv.tv_sec==right.m_tv.tv_sec&&
00138                             m_tv.tv_usec<right.m_tv.tv_usec)){
00139                                 return false;
00140                         }
00141                         return true;
00142                 }
00143                
00144                 Time operator+(const Time &right) const{
00145                         Time temp;
00146                         temp.m_tz=m_tz;
00147                         temp.m_tv.tv_sec=m_tv.tv_sec+right.m_tv.tv_sec;
00148                         temp.m_tv.tv_usec=m_tv.tv_usec+right.m_tv.tv_usec;
00149                         return temp;
00150                 }
00151                
00152                 Time operator-(const Time &right) const{
00153                         Time temp;
00154                         temp.m_tz=m_tz;
00155                         temp.m_tv.tv_sec= m_tv.tv_sec-right.m_tv.tv_sec;
00156                         temp.m_tv.tv_usec= m_tv.tv_usec-right.m_tv.tv_usec;
00157                         return temp;
00158                 }
00159                
00160                
00161                 // Operators with timeval struct
00162                 const Time &operator=(const struct timeval &right){
00163                         m_tv=right;
00164                         return *this;
00165                 }
00166                
00167                 bool operator==(const struct timeval &right) const{
00168                         if(m_tv.tv_sec==right.tv_sec&&
00169                            m_tv.tv_usec==right.tv_usec){
00170                                 return true;
00171                         }
00172                        
00173                         return false;
00174                 }
00175                
00176                 bool operator!=(const struct timeval &right) const{
00177                         if(m_tv.tv_sec==right.tv_sec&&
00178                            m_tv.tv_usec==right.tv_usec){
00179                                 return false;
00180                         }
00181                        
00182                         return true;
00183                 }
00184                
00185                
00186                 bool operator<(const struct timeval &right) const{
00187                         if(m_tv.tv_sec<right.tv_sec||
00188                            (m_tv.tv_sec==right.tv_sec&&
00189                             m_tv.tv_usec<right.tv_usec)){
00190                                 return true;
00191                         }
00192                        
00193                         return false;
00194                 }
00195                
00196                
00197                 bool operator>(const struct timeval &right) const{
00198                         if(m_tv.tv_sec>right.tv_sec||
00199                            (m_tv.tv_sec==right.tv_sec&&
00200                             m_tv.tv_usec>right.tv_usec)){
00201                                 return true;
00202                         }
00203                        
00204                         return false;
00205                 }
00206                
00207                 bool operator<=(const struct timeval &right) const{
00208                         if(m_tv.tv_sec>right.tv_sec||
00209                            (m_tv.tv_sec==right.tv_sec&&m_tv.tv_usec>right.tv_usec)){
00210                                 return false;
00211                         }
00212                        
00213                         return true;
00214                 }
00215                
00216                
00217                 bool operator>=(const struct timeval &right) const{
00218                         if(m_tv.tv_sec<right.tv_sec||
00219                            (m_tv.tv_sec==right.tv_sec&&m_tv.tv_usec<right.tv_usec)){
00220                                 return false;
00221                         }
00222                        
00223                         return true;
00224                 }
00225                
00226                
00227                 Time operator+(const struct timeval &right) const{
00228                         Time temp;
00229                         temp.m_tz=m_tz;
00230                         temp.m_tv.tv_sec= m_tv.tv_sec+right.tv_sec;
00231                         temp.m_tv.tv_usec= m_tv.tv_usec+right.tv_usec;
00232                        
00233                         return temp;
00234                 }
00235                
00236                 Time operator-(const struct timeval &right) const{
00237                         Time temp;
00238                         temp.m_tz=m_tz;
00239                         temp.m_tv.tv_sec= m_tv.tv_sec-right.tv_sec;
00240                         temp.m_tv.tv_usec=m_tv.tv_usec-right.tv_usec;
00241                        
00242                         return temp;
00243                 }
00244                
00245                 // Operators with time_t
00246                 const Time &operator=(const time_t &right){
00247                         m_tv.tv_usec=0;
00248                         m_tv.tv_sec=right;
00249                        
00250                         return *this;
00251                 }
00252                
00253                 bool operator==(const time_t &right) const{
00254                         if(m_tv.tv_sec==right){
00255                                 return true;
00256                         }
00257                        
00258                         return false;
00259                 }
00260                
00261                 bool operator<(const time_t &right) const{
00262                         if(m_tv.tv_sec<right){
00263                                 return true;
00264                         }
00265                        
00266                         return false;
00267                 }
00268                
00269                
00270                 bool operator>(const time_t &right) const{
00271                         if(m_tv.tv_sec>right){
00272                                 return true;
00273                         }
00274                        
00275                         return false;
00276                 }
00277 
00278                 bool operator<=(const time_t &right) const{
00279                         if(m_tv.tv_sec<=right){
00280                                 return true;
00281                         }
00282 
00283                         return false;
00284                 }
00285 
00286                 bool operator>=(const time_t &right) const{
00287                         if(m_tv.tv_sec>=right){
00288                                 return true;
00289                         }
00290 
00291                         return false;
00292                 }
00293 
00294                 Time operator+(const time_t &right) const{
00295                         Time temp;
00296                         temp.m_tz=m_tz;
00297                         temp.m_tv.tv_sec=m_tv.tv_sec+right;
00298                         return temp;
00299                 }
00300 
00301                 Time operator-(const time_t &right) const{
00302                         Time temp;
00303                         temp.m_tz=m_tz;
00304                         temp.m_tv.tv_sec=m_tv.tv_sec-right;
00305 
00306                         return temp;
00307                 }
00308 
00309                 // Operators with timespec struct
00310                 const Time &operator=(const struct timespec &right){
00311                         m_tv.tv_sec=right.tv_sec;
00312                         m_tv.tv_usec=right.tv_nsec/1000;
00313                         return *this;
00314                 }
00315 
00316                 bool operator==(const struct timespec &right) const{
00317                         if(m_tv.tv_sec==right.tv_sec&&
00318                            m_tv.tv_usec==right.tv_nsec/1000){
00319                                 return true;
00320                         }
00321 
00322                         return false;
00323                 }
00324 
00325                 bool operator!=(const struct timespec &right) const{
00326                         if(m_tv.tv_sec==right.tv_sec&&
00327                            m_tv.tv_usec==right.tv_nsec/1000){
00328                                 return false;
00329                         }
00330 
00331                         return true;
00332                 }
00333 
00334 
00335                 bool operator<(const struct timespec &right) const{
00336                         if(m_tv.tv_sec<right.tv_sec||
00337                            (m_tv.tv_sec==right.tv_sec&&
00338                             m_tv.tv_usec<right.tv_nsec/1000)){
00339                                 return true;
00340                         }
00341 
00342                         return false;
00343                 }
00344 
00345 
00346                 bool operator>(const struct timespec &right) const{
00347                         if(m_tv.tv_sec>right.tv_sec||
00348                            (m_tv.tv_sec==right.tv_sec&&
00349                             m_tv.tv_usec>right.tv_nsec/1000)){
00350                                 return true;
00351                         }
00352 
00353                         return false;
00354                 }
00355 
00356                 bool operator<=(const struct timespec &right) const{
00357                         if(m_tv.tv_sec>right.tv_sec||
00358                            (m_tv.tv_sec==right.tv_sec&&m_tv.tv_usec>right.tv_nsec/1000)){
00359                                 return false;
00360                         }
00361   
00362                         return true;
00363                 }
00364 
00365 
00366                 bool operator>=(const struct timespec &right) const{
00367                         if(m_tv.tv_sec<right.tv_sec||
00368                            (m_tv.tv_sec==right.tv_sec&&m_tv.tv_usec<right.tv_nsec/1000)){
00369                                 return false;
00370                         }
00371   
00372                         return true;
00373                 }
00374 
00375 
00376                 Time operator+(const struct timespec &right) const{
00377                         Time temp;
00378                         temp.m_tz=m_tz;
00379                         temp.m_tv.tv_sec= m_tv.tv_sec+right.tv_sec;
00380                         temp.m_tv.tv_usec= m_tv.tv_usec+(right.tv_nsec/1000);
00381      
00382                         return temp;
00383                 }
00384 
00385                 Time operator-(const struct timespec &right) const{
00386                         Time temp;
00387                         temp.m_tz=m_tz;
00388                         temp.m_tv.tv_sec= m_tv.tv_sec-right.tv_sec;
00389                         temp.m_tv.tv_usec=m_tv.tv_usec-(right.tv_nsec);
00390      
00391                         return temp;
00392                 }
00393                
00394 
00395                 const Time &operator=(const double &right){
00396                         double temp=1.0/right;
00397                         unsigned int count=0;
00398                         if(temp<0)
00399                                 temp=0;
00400                         while(count>1){
00401                                 count++;
00402                                 temp--;
00403                         }
00404                         m_tv.tv_sec=count;
00405                         m_tv.tv_usec=(long int) (temp*1000000);
00406                        
00407                         return *this;
00408                 }
00409 
00410                 Time operator*(const float &right) const{
00411                         Time retval;
00412                         double temp=m_tv.tv_sec+(double)m_tv.tv_usec/(1000000.0);
00413                         
00414                         temp=temp*right;
00415 
00416                         retval=temp;
00417 
00418                         return retval;
00419                 }
00420                
00421                 
00425                 void now(){gettimeofday(&m_tv,&m_tz);}
00429                 void zero(){m_tv.tv_sec=0;m_tv.tv_usec=0;}
00430                
00434                 time_t getSeconds() const {return m_tv.tv_sec;}
00435               
00439                 time_t getUSeconds() const { return m_tv.tv_usec; }
00440               
00441               
00445                 void setSeconds(time_t sec){ m_tv.tv_sec=sec;}
00446               
00450                 void setUSeconds(time_t usec){m_tv.tv_usec=usec;}
00451 
00455                 struct timeval getTimeval(){return m_tv;}
00456 
00457                 struct timespec timespec(){
00458                         struct timespec retval;
00459                         retval.tv_sec=m_tv.tv_sec;
00460                         retval.tv_nsec=m_tv.tv_usec*1000;
00461                         return retval;
00462                 }
00463       
00464         protected:
00465                 /* Time value member variable */
00466                 struct timeval m_tv;
00467                 /* Time zone member variable */
00468                 struct timezone m_tz;
00469               
00470         };
00471         
00476         class Timer {
00477         public:
00478                 Timer(){m_lastSleep.now();}
00479 
00480                 void setFrequency(float freq){
00481                         freq=1.0/freq;
00482                         int count=0;
00483                         while(count>1){
00484                                 count++;
00485                                 freq--;
00486                         }
00487                         m_sleepInterval.setSeconds(count);
00488                         m_sleepInterval.setUSeconds((long int) (freq*1000000));
00489                         
00490                 }
00491                 void setSleepInterval(struct timespec interval){
00492                         m_sleepInterval=interval;
00493                 }
00494                 void setMinSleep(float min){m_minSleep=min;}
00495 
00496                 virtual void sleep(){
00497                         
00498                         m_current.now();
00499                         m_current=m_current-m_lastSleep;
00500                         
00501                         if(m_current<m_sleepInterval){
00502                                 m_current=m_sleepInterval-m_current;
00503                         }else{
00504                                 m_current.zero();
00505                         }
00506                         
00507                         /* setting m_sleeptime to shut up a compiler warning */
00508                         if(m_current>m_minSleep){
00509                                 m_sleeptime=m_current.timespec();
00510                         }else{
00511                                 m_sleeptime=m_minSleep.timespec();
00512                         }
00513                         nanosleep(&m_sleeptime,NULL);                
00514                         m_lastSleep.now();
00515                 }
00516 
00517         protected:
00518                 float m_freq; // how many times per second we should be waking up
00519                 Time m_minSleep; // minimum sleep time
00520 
00521                 Time m_lastSleep;
00522                 Time m_current;
00523                 Time m_sleepInterval;
00524                 struct timespec m_sleeptime;
00525         };
00526 
00527         class PlaybackTimer : public Timer {
00528 
00529         public:
00530                 PlaybackTimer(){m_timeMultiplier=0;}
00531                 Time getTime(){
00532                         Time now;
00533                         now=now-m_realZeroTime;
00534                         now=now*m_timeMultiplier;
00535                         now=now+m_fakeZeroTime;
00536                         return now;
00537                 }
00538 
00539                 virtual void sleep(){
00540                         
00541                         m_current=getTime();
00542                         m_current=m_current-m_lastSleep;
00543                         
00544                         if(m_current<m_sleepInterval){
00545                                 m_current=m_sleepInterval-m_current;
00546                         }else{
00547                                 m_current.zero();
00548                         }
00549                         
00550                         /* setting m_sleeptime to shut up a compiler warning */
00551                         if(m_current>m_minSleep){
00552                                 m_current=m_current*m_timeMultiplier;
00553                                 m_sleeptime=m_current.timespec();
00554                         }else{
00555                                 m_sleeptime=(m_minSleep*m_timeMultiplier).timespec();
00556                         }
00557                         nanosleep(&m_sleeptime,NULL);                
00558                         m_lastSleep=getTime();
00559                 }
00560 
00561                 void setRealZeroTime(Time time){m_realZeroTime=time;}
00562                 void setFakeZeroTime(Time time){m_fakeZeroTime=time;}
00563         protected:
00564                 float m_timeMultiplier;
00565                 Time m_realZeroTime;
00566                 Time m_fakeZeroTime;
00567         };
00568        
00569 };
00570 #endif 

Generated on Thu Feb 1 15:31:54 2007 for WURDE by  doxygen 1.5.1