Main MRPT website > C++ reference for MRPT 1.9.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-2018, 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 "system-precomp.h" // Precompiled headers
11 
12 #include <mrpt/system/CRateTimer.h>
13 #include <mrpt/core/exceptions.h>
14 #include <chrono>
15 #include <thread>
16 
17 using namespace mrpt::system;
18 
19 CRateTimer::CRateTimer(const double rate_hz) { setRate(rate_hz); }
21 void CRateTimer::setRate(const double rate_hz)
22 {
23  ASSERT_ABOVE_(rate_hz, 0.0);
24  m_rate_hz = rate_hz;
25 }
27 {
28  const double elapsed_tim = m_tictac.Tac();
29  m_tictac.Tic();
30  const double period = 1.0 / m_rate_hz;
31  const int64_t wait_tim_us = 1000000L * (period - elapsed_tim);
32  if (wait_tim_us < 0) return false;
33  std::this_thread::sleep_for(std::chrono::microseconds(wait_tim_us));
34  return true;
35 }
ASSERT_ABOVE_
#define ASSERT_ABOVE_(__A, __B)
Definition: exceptions.h:171
exceptions.h
system-precomp.h
int64_t
__int64 int64_t
Definition: rptypes.h:49
mrpt::system::CTicTac::Tac
double Tac() noexcept
Stops the stopwatch.
Definition: CTicTac.cpp:90
mrpt::system::CRateTimer::m_tictac
mrpt::system::CTicTac m_tictac
Definition: system/CRateTimer.h:42
mrpt::system::CRateTimer::sleep
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:26
mrpt::system::CRateTimer::CRateTimer
CRateTimer(const double rate_hz=1.0)
Ctor: specifies the desired rate (Hz)
Definition: CRateTimer.cpp:19
mrpt::system::CRateTimer::setRate
void setRate(const double rate_hz)
Changes the object loop rate (Hz)
Definition: CRateTimer.cpp:21
mrpt::system::CTicTac::Tic
void Tic() noexcept
Starts the stopwatch.
Definition: CTicTac.cpp:79
CRateTimer.h
mrpt::system::CRateTimer::~CRateTimer
virtual ~CRateTimer()
Dtor.
Definition: CRateTimer.cpp:20
mrpt::system::CRateTimer::m_rate_hz
double m_rate_hz
Definition: system/CRateTimer.h:41
mrpt::system
This namespace provides a OS-independent interface to many useful functions: filenames manipulation,...
Definition: math_frwds.h:25



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST