MRPT  2.0.1
CRateTimer.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include "system-precomp.h" // Precompiled headers
11 
12 #include <mrpt/core/exceptions.h>
13 #include <mrpt/system/CRateTimer.h>
14 #include <chrono>
15 #include <thread>
16 
17 using namespace mrpt::system;
18 
19 CRateTimer::CRateTimer(const double rate_hz) { setRate(rate_hz); }
20 void CRateTimer::setRate(const double rate_hz)
21 {
22  ASSERT_ABOVE_(rate_hz, 0.0);
23  m_rate_hz = rate_hz;
24 }
26 {
27  const double elapsed_tim = m_tictac.Tac();
28  const double period = 1.0 / m_rate_hz;
29  const int64_t wait_tim_us =
30  static_cast<int64_t>(1000000L * (period - elapsed_tim));
31  if (elapsed_tim > period)
32  {
33  m_tictac.Tic();
34  return false;
35  }
36  std::this_thread::sleep_for(std::chrono::microseconds(wait_tim_us));
37  m_tictac.Tic();
38  return true;
39 }
double Tac() noexcept
Stops the stopwatch.
Definition: CTicTac.cpp:86
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:25
CRateTimer(const double rate_hz=1.0)
Ctor: specifies the desired rate (Hz)
Definition: CRateTimer.cpp:19
mrpt::system::CTicTac m_tictac
void setRate(const double rate_hz)
Changes the object loop rate (Hz)
Definition: CRateTimer.cpp:20
#define ASSERT_ABOVE_(__A, __B)
Definition: exceptions.h:155
void Tic() noexcept
Starts the stopwatch.
Definition: CTicTac.cpp:75



Page generated by Doxygen 1.8.14 for MRPT 2.0.1 Git: 0fef1a6d7 Fri Apr 3 23:00:21 2020 +0200 at vie abr 3 23:20:28 CEST 2020