Visual Servoing Platform  version 3.0.0
vpTime.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2015 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See http://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Time management and measurement.
32  *
33  * Authors:
34  * Eric Marchand
35  * Fabien Spindler
36  *
37  *****************************************************************************/
38 
39 
40 
41 #include <visp3/core/vpTime.h>
42 #include <visp3/core/vpDebug.h>
43 #include <iostream>
44 
45 
53 // Unix depend version
54 
55 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
56 # include <sys/time.h>
57 # include <unistd.h>
58 #elif defined(_WIN32)
59 # include <windows.h>
60 # include <winbase.h>
61 #endif
62 
63 #ifndef DOXYGEN_SHOULD_SKIP_THIS
64 namespace vpTime
65 {
66 #endif
67 
75 double minTimeForUsleepCall = 4;
76 
83 {
84  return minTimeForUsleepCall;
85 }
86 
93 double measureTimeMs()
94 {
95 #if defined(_WIN32)
96  LARGE_INTEGER time, frequency;
97  QueryPerformanceFrequency(&frequency);
98  if(frequency.QuadPart == 0){
99  return(timeGetTime());
100  }
101  else{
102  QueryPerformanceCounter(&time);
103  return (double)(1000.0*time.QuadPart/frequency.QuadPart);
104  }
105 #elif !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
106  struct timeval tp;
107  gettimeofday(&tp,0);
108  return(1000.0*tp.tv_sec + tp.tv_usec/1000.0);
109 #endif
110 }
111 
118 {
119 #if defined(_WIN32)
120  LARGE_INTEGER time, frequency;
121  QueryPerformanceFrequency(&frequency);
122  if(frequency.QuadPart == 0){
123  return(timeGetTime());
124  }
125  else{
126  QueryPerformanceCounter(&time);
127  return (double)(1000000.0*time.QuadPart/frequency.QuadPart);
128  }
129 #else
130 
131  struct timeval tp;
132  gettimeofday(&tp,0);
133  return(1000000.0*tp.tv_sec + tp.tv_usec);
134 #endif
135 }
136 
150 int wait(double t0, double t)
151 {
152  double timeCurrent, timeToWait;
153  timeCurrent = measureTimeMs();
154 
155  timeToWait = t0 + t - timeCurrent;
156 
157  if ( timeToWait <= 0. ) // no need to wait
158  return(1);
159  else {
160 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
161  if (timeToWait > vpTime::minTimeForUsleepCall) {
162  usleep((unsigned long )((timeToWait-vpTime::minTimeForUsleepCall)*1000));
163  }
164 #elif defined(_WIN32)
165  if (timeToWait > vpTime::minTimeForUsleepCall) {
166  Sleep((DWORD)(timeToWait-vpTime::minTimeForUsleepCall));
167  }
168 #endif
169  // Blocking loop to have an accurate waiting
170  do {
171  timeCurrent = measureTimeMs();
172  timeToWait = t0 + t - timeCurrent;
173 
174  } while (timeToWait > 0.);
175 
176  return 0;
177  }
178 }
179 
189 void wait(double t)
190 {
191  double t0, timeCurrent, timeToWait;
192  t0 = timeCurrent = measureTimeMs();
193 
194  timeToWait = t;
195 
196  if ( timeToWait <= 0. ) // no need to wait
197  return;
198  else {
199 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
200  if (timeToWait > vpTime::minTimeForUsleepCall) {
201  usleep((unsigned long )((timeToWait-vpTime::minTimeForUsleepCall)*1000));
202  }
203 #elif defined(_WIN32)
204  if (timeToWait > vpTime::minTimeForUsleepCall) {
205  Sleep((DWORD)(timeToWait-vpTime::minTimeForUsleepCall));
206  }
207 #endif
208  // Blocking loop to have an accurate waiting
209  do {
210  timeCurrent = measureTimeMs();
211  timeToWait = t0 + t - timeCurrent;
212 
213  } while (timeToWait > 0.);
214 
215  return;
216  }
217 }
218 
226 {
227  return vpTime::measureTimeMs()/1000.0 ;
228 }
229 
236 void sleepMs(double t)
237 {
238 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
239  usleep((unsigned long )(t*1000));
240 #elif defined(_WIN32)
241  Sleep((DWORD)(t));
242 #endif
243 }
244 
245 #ifndef DOXYGEN_SHOULD_SKIP_THIS
246 };
247 #endif
VISP_EXPORT int wait(double t0, double t)
Definition: vpTime.cpp:150
Definition: vpTime.h:75
VISP_EXPORT double measureTimeSecond()
Definition: vpTime.cpp:225
VISP_EXPORT void sleepMs(double t)
Definition: vpTime.cpp:236
VISP_EXPORT double measureTimeMicros()
Definition: vpTime.cpp:117
VISP_EXPORT double measureTimeMs()
Definition: vpTime.cpp:93
VISP_EXPORT double getMinTimeForUsleepCall()
Definition: vpTime.cpp:82