Visual Servoing Platform  version 3.2.1 under development (2019-07-18)
vpTime.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2019 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See http://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * Time management and measurement.
33  *
34  * Authors:
35  * Eric Marchand
36  * Fabien Spindler
37  *
38  *****************************************************************************/
39 
40 #include <ctime>
41 
42 #include <visp3/core/vpDebug.h>
43 #include <visp3/core/vpTime.h>
44 
51 // Unix depend version
52 
53 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
54 #include <sys/time.h>
55 #include <unistd.h>
56 #elif defined(_WIN32)
57 //#include <winbase.h>
58 #include <windows.h>
59 #endif
60 
61 #ifndef DOXYGEN_SHOULD_SKIP_THIS
62 namespace vpTime
63 {
64 #endif
65 
73 double minTimeForUsleepCall = 4;
74 
80 double getMinTimeForUsleepCall() { return minTimeForUsleepCall; }
81 
88 double measureTimeMs()
89 {
90 #if defined(_WIN32)
91 #if !defined(WINRT)
92  LARGE_INTEGER time, frequency;
93  QueryPerformanceFrequency(&frequency);
94  if (frequency.QuadPart == 0) {
95  return (timeGetTime());
96  } else {
97  QueryPerformanceCounter(&time);
98  return (double)(1000.0 * time.QuadPart / frequency.QuadPart);
99  }
100 #else
101  throw(vpException(vpException::fatalError, "Cannot get time: not implemented on Universal Windows Platform"));
102 #endif
103 #elif !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
104  struct timeval tp;
105  gettimeofday(&tp, 0);
106  return (1000.0 * tp.tv_sec + tp.tv_usec / 1000.0);
107 #endif
108 }
109 
116 {
117 #if defined(_WIN32)
118 #if !defined(WINRT)
119  LARGE_INTEGER time, frequency;
120  QueryPerformanceFrequency(&frequency);
121  if (frequency.QuadPart == 0) {
122  return (timeGetTime());
123  } else {
124  QueryPerformanceCounter(&time);
125  return (double)(1000000.0 * time.QuadPart / frequency.QuadPart);
126  }
127 #else
128  throw(vpException(vpException::fatalError, "Cannot get time: not implemented on Universal Windows Platform"));
129 #endif
130 #elif !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
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((useconds_t)((timeToWait - vpTime::minTimeForUsleepCall) * 1000));
163  }
164  // Blocking loop to have an accurate waiting
165  do {
166  timeCurrent = measureTimeMs();
167  timeToWait = t0 + t - timeCurrent;
168 
169  } while (timeToWait > 0.);
170 
171  return 0;
172 #elif defined(_WIN32)
173 #if !defined(WINRT_8_0)
174  if (timeToWait > vpTime::minTimeForUsleepCall) {
175  Sleep((DWORD)(timeToWait - vpTime::minTimeForUsleepCall));
176  }
177  // Blocking loop to have an accurate waiting
178  do {
179  timeCurrent = measureTimeMs();
180  timeToWait = t0 + t - timeCurrent;
181 
182  } while (timeToWait > 0.);
183 
184  return 0;
185 #else
187  "vpTime::wait() is not implemented on Windows Phone 8.0"));
188 #endif
189 #endif
190  }
191 }
192 
202 void wait(double t)
203 {
204  double timeToWait = t;
205 
206  if (timeToWait <= 0.) // no need to wait
207  return;
208  else {
209 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
210  double t0 = measureTimeMs();
211  if (timeToWait > vpTime::minTimeForUsleepCall) {
212  usleep((useconds_t)((timeToWait - vpTime::minTimeForUsleepCall) * 1000));
213  }
214  // Blocking loop to have an accurate waiting
215  do {
216  double timeCurrent = measureTimeMs();
217  timeToWait = t0 + t - timeCurrent;
218 
219  } while (timeToWait > 0.);
220 
221  return;
222 #elif defined(_WIN32)
223 #if !defined(WINRT_8_0)
224  double t0 = measureTimeMs();
225  if (timeToWait > vpTime::minTimeForUsleepCall) {
226  Sleep((DWORD)(timeToWait - vpTime::minTimeForUsleepCall));
227  }
228  // Blocking loop to have an accurate waiting
229  do {
230  double timeCurrent = measureTimeMs();
231  timeToWait = t0 + t - timeCurrent;
232 
233  } while (timeToWait > 0.);
234 
235  return;
236 #else
238  "vpTime::wait() is not implemented on Windows Phone 8.0"));
239 #endif
240 #endif
241  }
242 }
243 
250 double measureTimeSecond() { return vpTime::measureTimeMs() / 1000.0; }
251 
258 void sleepMs(double t)
259 {
260 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
261  usleep((useconds_t)(t * 1000));
262 #elif defined(_WIN32)
263 #if !defined(WINRT_8_0)
264  Sleep((DWORD)(t));
265 #else
267  "vpTime::sleepMs() is not implemented on Windows Phone 8.0"));
268 #endif
269 #endif
270 }
271 
345 std::string getDateTime(const std::string &format)
346 {
347  time_t rawtime;
348  struct tm *timeinfo;
349  char buffer[80];
350 
351  time(&rawtime);
352  timeinfo = localtime(&rawtime);
353 
354  strftime(buffer, 80, format.c_str(), timeinfo);
355  std::string str(buffer);
356 
357  return str;
358 }
359 
360 #ifndef DOXYGEN_SHOULD_SKIP_THIS
361 };
362 #endif
VISP_EXPORT int wait(double t0, double t)
Definition: vpTime.cpp:150
Time management and measurement.
Definition: vpTime.h:76
VISP_EXPORT double measureTimeSecond()
Definition: vpTime.cpp:250
error that can be emited by ViSP classes.
Definition: vpException.h:71
VISP_EXPORT double measureTimeMs()
Definition: vpTime.cpp:88
VISP_EXPORT std::string getDateTime(const std::string &format="%Y/%m/%d %H:%M:%S")
Definition: vpTime.cpp:345
VISP_EXPORT void sleepMs(double t)
Definition: vpTime.cpp:258
VISP_EXPORT double measureTimeMicros()
Definition: vpTime.cpp:115
VISP_EXPORT double getMinTimeForUsleepCall()
Definition: vpTime.cpp:80
Function not implemented.
Definition: vpException.h:90