Main MRPT website > C++ reference for MRPT 1.5.9
CRateTimer.cpp
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +---------------------------------------------------------------------------+ */
9 
10 #include "base-precomp.h" // Precompiled headers
11 
12 #include <mrpt/utils/CRateTimer.h>
13 #include <mrpt/utils/mrpt_macros.h>
14 #include <chrono>
15 #include <thread>
16 
17 using namespace mrpt::utils;
18 
19 CRateTimer::CRateTimer(const double rate_hz)
20 {
21  setRate(rate_hz);
22 }
24 {
25 }
26 
27 void CRateTimer::setRate(const double rate_hz)
28 {
29  ASSERT_ABOVE_(rate_hz, 0.0);
30  m_rate_hz = rate_hz;
31 }
33 {
34  const double elapsed_tim = m_tictac.Tac();
35  m_tictac.Tic();
36  const double period = 1.0 / m_rate_hz;
37  const int64_t wait_tim_us = 1000000L *( period- elapsed_tim);
38  if (wait_tim_us < 0)
39  return false;
40  std::this_thread::sleep_for( std::chrono::microseconds(wait_tim_us));
41  return true;
42 }
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
Definition: zip.h:16
#define ASSERT_ABOVE_(__A, __B)
bool sleep()
Sleeps for some time, such as the return of this method is 1/rate (seconds) after the return of the p...
Definition: CRateTimer.cpp:32
void setRate(const double rate_hz)
Changes the object loop rate (Hz)
Definition: CRateTimer.cpp:27
virtual ~CRateTimer()
Dtor.
Definition: CRateTimer.cpp:23
void Tic()
Starts the stopwatch.
Definition: CTicTac.cpp:77
__int64 int64_t
Definition: rptypes.h:51
mrpt::utils::CTicTac m_tictac
Definition: CRateTimer.h:36
CRateTimer(const double rate_hz=1.0)
Ctor: specifies the desired rate (Hz)
Definition: CRateTimer.cpp:19
double Tac()
Stops the stopwatch.
Definition: CTicTac.cpp:92



Page generated by Doxygen 1.8.14 for MRPT 1.5.9 Git: 690a4699f Wed Apr 15 19:29:53 2020 +0200 at miƩ abr 15 19:30:12 CEST 2020