MRPT  2.0.4
CControlledRateTimer.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/bits_math.h>
13 #include <mrpt/core/exceptions.h>
15 
16 #include <cmath> // std::abs(double)
17 
18 using namespace mrpt::system;
19 
21  : mrpt::system::COutputLogger("CControlledRateTimer")
22 {
23  m_tic.Tic();
24  setRate(rate_hz);
25 }
26 void CControlledRateTimer::setRate(const double rate_hz)
27 {
28  if (rate_hz == m_rate_hz) return;
29 
30  ASSERT_ABOVE_(rate_hz, 0.0);
31  m_rate_hz = rate_hz;
34  m_lastTic = 0;
35 }
36 
38 {
39  const bool validRateEstimate = internalUpdateRateEstimate();
40 
41  const double rawError = m_rate_hz - m_currentEstimatedRate;
42 
43  const double controlError =
44  mrpt::saturate_val(rawError, -0.2 * m_rate_hz, 0.2 * m_rate_hz);
45 
46  if (std::abs(rawError) / m_rate_hz > m_followErrorRatioForWarning)
47  {
49  2.0,
50  "Cannot run at the expected rate: actual_rate=%.03f Hz "
51  "desired_rate=%.03f Hz",
53  }
54 
55  // Trapezoidal approx. of PI(s) controller equation
56  // integral e(t)->s=(2/T)*(1-z^-1)/(1+z^-1)
57  // derivative e(t)->s=(1/T)*(1-z^-1)
58  const double q0 = m_Kp * (1 + 1.0 / (m_rate_hz * 2 * m_Ti));
59  const double q1 = m_Kp * (-1 + 1.0 / (m_rate_hz * 2 * m_Ti));
60 
61  double newRate;
62  if (validRateEstimate)
63  {
64  newRate =
65  m_ratetimer.rate() + q0 * controlError + q1 * m_lastControlError;
66  }
67  else
68  {
69  newRate = m_rate_hz;
70  }
71 
72  // Set control output:
73  m_ratetimer.setRate(newRate);
74 
75  m_lastControlError = controlError;
76 
77  return m_ratetimer.sleep();
78 }
79 
81 {
82  bool valid = false;
83  const double t_now = m_tic.Tac();
84 
85  if (m_lastTic > 0 && t_now > m_lastTic)
86  {
87  const double measuredPeriod = t_now - m_lastTic;
88  m_lastRawRate = (1.0 / measuredPeriod);
89 
90  // Filter:
92  (1.0 - m_lowPass_a0) * m_lastRawRate;
93  valid = true;
94  }
95 
96  m_lastTic = t_now;
97  return valid;
98 }
double Tac() noexcept
Stops the stopwatch.
Definition: CTicTac.cpp:87
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:30
void setRate(const double rate_hz)
Changes the object loop rate (Hz)
Versatile class for consistent logging and management of output messages.
mrpt::system::CRateTimer m_ratetimer
the one control acts on
#define MRPT_LOG_THROTTLE_WARN_FMT(_PERIOD_SECONDS, _FMT_STRING,...)
void setRate(const double rate_hz)
Changes the object loop rate (Hz)
Definition: CRateTimer.cpp:25
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
double rate() const
Gets current rate (Hz)
#define ASSERT_ABOVE_(__A, __B)
Definition: exceptions.h:155
CControlledRateTimer(const double rate_hz=1.0)
Ctor: specifies the desired rate (Hz)
void Tic() noexcept
Starts the stopwatch.
Definition: CTicTac.cpp:76
T saturate_val(const T &value, const T sat_min, const T sat_max)
Like saturate() but it returns the value instead of modifying the variable.
bool sleep()
Sleeps for some time, such as the return of this method is 1/rate (seconds) after the return of the p...



Page generated by Doxygen 1.8.14 for MRPT 2.0.4 Git: 33de1d0ad Sat Jun 20 11:02:42 2020 +0200 at sáb jun 20 17:35:17 CEST 2020