Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
vpTime.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2017 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 #include <ctime>
41 
42 #include <visp3/core/vpTime.h>
43 #include <visp3/core/vpDebug.h>
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 #if !defined(WINRT)
97  LARGE_INTEGER time, frequency;
98  QueryPerformanceFrequency(&frequency);
99  if(frequency.QuadPart == 0){
100  return(timeGetTime());
101  }
102  else{
103  QueryPerformanceCounter(&time);
104  return (double)(1000.0*time.QuadPart/frequency.QuadPart);
105  }
106 # else
107  throw(vpException(vpException::fatalError, "Cannot get time: not implemented on Universal Windows Platform"));
108 # endif
109 #elif !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
110  struct timeval tp;
111  gettimeofday(&tp,0);
112  return(1000.0*tp.tv_sec + tp.tv_usec/1000.0);
113 #endif
114 }
115 
122 {
123 #if defined(_WIN32)
124 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
125  LARGE_INTEGER time, frequency;
126  QueryPerformanceFrequency(&frequency);
127  if(frequency.QuadPart == 0){
128  return(timeGetTime());
129  }
130  else{
131  QueryPerformanceCounter(&time);
132  return (double)(1000000.0*time.QuadPart/frequency.QuadPart);
133  }
134 # elif WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP)
135  throw(vpException(vpException::fatalError, "Cannot get time: not implemented on Universal Windows Platform"));
136 # endif
137 #else
138  struct timeval tp;
139  gettimeofday(&tp,0);
140  return(1000000.0*tp.tv_sec + tp.tv_usec);
141 #endif
142 }
143 
157 int wait(double t0, double t)
158 {
159  double timeCurrent, timeToWait;
160  timeCurrent = measureTimeMs();
161 
162  timeToWait = t0 + t - timeCurrent;
163 
164  if ( timeToWait <= 0. ) // no need to wait
165  return(1);
166  else {
167 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
168  if (timeToWait > vpTime::minTimeForUsleepCall) {
169  usleep((useconds_t)((timeToWait-vpTime::minTimeForUsleepCall)*1000));
170  }
171  // Blocking loop to have an accurate waiting
172  do {
173  timeCurrent = measureTimeMs();
174  timeToWait = t0 + t - timeCurrent;
175 
176  } while (timeToWait > 0.);
177 
178  return 0;
179 #elif defined(_WIN32)
180 # if !defined(WINRT_8_0)
181  if (timeToWait > vpTime::minTimeForUsleepCall) {
182  Sleep((DWORD)(timeToWait-vpTime::minTimeForUsleepCall));
183  }
184  // Blocking loop to have an accurate waiting
185  do {
186  timeCurrent = measureTimeMs();
187  timeToWait = t0 + t - timeCurrent;
188 
189  } while (timeToWait > 0.);
190 
191  return 0;
192 # else
193  throw(vpException(vpException::functionNotImplementedError, "vpTime::wait() is not implemented on Windows Phone 8.0"));
194 # endif
195 #endif
196  }
197 }
198 
208 void wait(double t)
209 {
210  double timeToWait = t;
211 
212  if ( timeToWait <= 0. ) // no need to wait
213  return;
214  else {
215 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
216  double t0 = measureTimeMs();
217  if (timeToWait > vpTime::minTimeForUsleepCall) {
218  usleep((useconds_t)((timeToWait-vpTime::minTimeForUsleepCall)*1000));
219  }
220  // Blocking loop to have an accurate waiting
221  do {
222  double timeCurrent = measureTimeMs();
223  timeToWait = t0 + t - timeCurrent;
224 
225  } while (timeToWait > 0.);
226 
227  return;
228 #elif defined(_WIN32)
229 # if !defined(WINRT_8_0)
230  double t0 = measureTimeMs();
231  if (timeToWait > vpTime::minTimeForUsleepCall) {
232  Sleep((DWORD)(timeToWait-vpTime::minTimeForUsleepCall));
233  }
234  // Blocking loop to have an accurate waiting
235  do {
236  double timeCurrent = measureTimeMs();
237  timeToWait = t0 + t - timeCurrent;
238 
239  } while (timeToWait > 0.);
240 
241  return;
242 # else
243  throw(vpException(vpException::functionNotImplementedError, "vpTime::wait() is not implemented on Windows Phone 8.0"));
244 # endif
245 #endif
246  }
247 }
248 
256 {
257  return vpTime::measureTimeMs()/1000.0 ;
258 }
259 
266 void sleepMs(double t)
267 {
268 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
269  usleep((useconds_t)(t*1000));
270 #elif defined(_WIN32)
271 # if !defined(WINRT_8_0)
272  Sleep((DWORD)(t));
273 # else
274  throw(vpException(vpException::functionNotImplementedError, "vpTime::sleepMs() is not implemented on Windows Phone 8.0"));
275 # endif
276 #endif
277 }
278 
351 std::string getDateTime(const std::string &format) {
352  time_t rawtime;
353  struct tm * timeinfo;
354  char buffer[80];
355 
356  time (&rawtime);
357  timeinfo = localtime(&rawtime);
358 
359  strftime(buffer, 80, format.c_str(), timeinfo);
360  std::string str(buffer);
361 
362  return str;
363 }
364 
365 #ifndef DOXYGEN_SHOULD_SKIP_THIS
366 };
367 #endif
VISP_EXPORT int wait(double t0, double t)
Definition: vpTime.cpp:157
VISP_EXPORT double measureTimeSecond()
Definition: vpTime.cpp:255
error that can be emited by ViSP classes.
Definition: vpException.h:73
VISP_EXPORT double measureTimeMs()
Definition: vpTime.cpp:93
VISP_EXPORT std::string getDateTime(const std::string &format="%Y/%m/%d %H:%M:%S")
Definition: vpTime.cpp:351
VISP_EXPORT void sleepMs(double t)
Definition: vpTime.cpp:266
VISP_EXPORT double measureTimeMicros()
Definition: vpTime.cpp:121
VISP_EXPORT double getMinTimeForUsleepCall()
Definition: vpTime.cpp:82