Timer.cpp
Go to the documentation of this file.
00001 /*
00002  *   Katana Native Interface - A C++ interface to the robot arm Katana.
00003  *   Copyright (C) 2005 Neuronics AG
00004  *   Check out the AUTHORS file for detailed contact information.
00005  *
00006  *   This program is free software; you can redistribute it and/or modify
00007  *   it under the terms of the GNU General Public License as published by
00008  *   the Free Software Foundation; either version 2 of the License, or
00009  *   (at your option) any later version.
00010  *
00011  *   This program is distributed in the hope that it will be useful,
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *   GNU General Public License for more details.
00015  *
00016  *   You should have received a copy of the GNU General Public License
00017  *   along with this program; if not, write to the Free Software
00018  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  */
00020 
00021 #include "common/Timer.h"
00022 
00023 #ifdef WIN32
00024 #include <Windows.h>
00025 #endif
00026 
00027 namespace KNI {
00028 
00029     void
00030     Timer::Set(long timeout) {
00031         _timeout = timeout;
00032     }
00033     
00034 #ifdef WIN32
00035     void Timer::Start() {
00036         _ct = clock();
00037     }
00038 
00039     long Timer::_ElapsedTime() const {
00040         return static_cast<long>(1000.0*(static_cast<double>((clock()-_ct))/static_cast<double>(CLOCKS_PER_SEC)));
00041     }
00042 
00043     Timer::Timer() : _timeout(0), _ct(0) {}
00044     Timer::Timer(long timeout) : _timeout(timeout), _ct(0) {}
00045 
00046 #else
00047 
00048     Timer::Timer() : _timeout(0), _ct() {}
00049     Timer::Timer(long timeout) : _timeout(timeout), _ct() {}
00050 
00051     void Timer::Start() {
00052         gettimeofday(&_ct, 0);
00053     }
00054 
00055     long Timer::_ElapsedTime() const {
00056         struct timeval end;
00057         gettimeofday(&end, 0);
00058         return (end.tv_sec*1000+end.tv_usec/1000) - (_ct.tv_sec*1000+_ct.tv_usec/1000);
00059     }
00060 
00061 #endif
00062 
00063     void 
00064     Timer::Set_And_Start(long timeout) {
00065         Set(timeout); 
00066         Start(); 
00067     }
00068 
00069     bool Timer::Elapsed() const {
00070         if( _timeout <= 0 ) return false; // (timeout <= 0) => INFINITE
00071         if( _ElapsedTime() < _timeout) return false;
00072         return true;
00073     }
00074     long Timer::ElapsedTime() const {
00075         return _ElapsedTime();
00076     }
00077 
00078     void Timer::WaitUntilElapsed() const {
00079         if(Elapsed()) return;
00083         sleep(_timeout - _ElapsedTime());
00084     }
00085 
00086     void sleep(long time) {
00087         if(time <= 0) return; 
00088     #ifdef WIN32
00089         Sleep(time);
00090     #else // POSIX 1b
00091         timespec time2sleep;
00092         time2sleep.tv_sec = 0;
00093         time2sleep.tv_nsec = time * 1000 * 1000; // SLEEP in milliseconds
00094         nanosleep(&time2sleep, NULL);
00095     #endif
00096     }
00097 
00098 }


kni
Author(s): Neuronics AG (see AUTHORS.txt); ROS wrapper by Martin Günther
autogenerated on Mon Oct 6 2014 10:45:33