Rate.hpp
Go to the documentation of this file.
1 /**********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2018, Remo Diethelm
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of Robotic Systems Lab nor ETH Zurich
18  * nor the names of its contributors may be used to endorse or
19  * promote products derived from this software without specific
20  * prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  */
35 
42 #pragma once
43 
44 // std
45 #include <ctime>
46 
47 // any worker
49 
50 namespace bota_worker
51 {
79 class Rate
80 {
81 protected:
83  static constexpr long int N_SEC_PER_SEC = static_cast<long int>(1e9);
85  static constexpr double SEC_PER_N_SEC = 1.0 / N_SEC_PER_SEC;
86 
89 
91  timespec sleepStartTime_{};
93  timespec sleepEndTime_{};
96  timespec stepTime_{};
98  unsigned int numTimeSteps_ = 0;
100  unsigned int numWarnings_ = 0;
102  unsigned int numErrors_ = 0;
108  double awakeTime_ = 0.0;
110  double awakeTimeMean_ = 0.0;
112  double awakeTimeM2_ = 0.0;
113 
114 public:
121  Rate(const std::string& name, const double timeStep);
122 
128  explicit Rate(RateOptions options);
129 
134  Rate(Rate&& other) noexcept;
135 
141  {
142  return options_;
143  }
144 
149  const RateOptions& getOptions() const
150  {
151  return options_;
152  }
153 
157  void reset();
158 
162  void sleep();
163 
168  const timespec& getSleepStartTime() const
169  {
170  return sleepStartTime_;
171  }
172 
177  const timespec& getSleepEndTime() const
178  {
179  return sleepEndTime_;
180  }
181 
186  const timespec& getStepTime() const
187  {
188  return stepTime_;
189  }
190 
195  unsigned int getNumTimeSteps() const
196  {
197  return numTimeSteps_;
198  }
199 
204  unsigned int getNumWarnings() const
205  {
206  return numWarnings_;
207  }
208 
213  unsigned int getNumErrors() const
214  {
215  return numErrors_;
216  }
217 
222  double getAwakeTime() const;
223 
228  double getAwakeTimeMean() const;
229 
234  double getAwakeTimeVar() const;
235 
240  double getAwakeTimeStdDev() const;
241 
248  static double getDuration(const timespec& start, const timespec& end); // NOLINT(readability-identifier-naming)
249 
255  static void addDuration(timespec& time, const double duration); // NOLINT(readability-identifier-naming)
256 };
257 
258 } // namespace bota_worker
double getAwakeTimeMean() const
Definition: Rate.cpp:185
static void addDuration(timespec &time, const double duration)
Definition: Rate.cpp:219
unsigned int numWarnings_
Counter storing how many times a time step took longer than the warning threshold.
Definition: Rate.hpp:100
timespec stepTime_
Definition: Rate.hpp:96
ROSCPP_DECL void start()
double getAwakeTime() const
Definition: Rate.cpp:173
unsigned int getNumWarnings() const
Definition: Rate.hpp:204
const RateOptions & getOptions() const
Definition: Rate.hpp:149
unsigned int getNumTimeSteps() const
Definition: Rate.hpp:195
double awakeTime_
Most recent time which elapsed between subsequent calls of sleep().
Definition: Rate.hpp:108
double getAwakeTimeStdDev() const
Definition: Rate.cpp:209
void sleep()
Definition: Rate.cpp:96
unsigned int numTimeSteps_
Counter storing how many times sleep has been called.
Definition: Rate.hpp:98
static constexpr long int N_SEC_PER_SEC
Factor storing nanoseconds per seconds.
Definition: Rate.hpp:83
Rate(const std::string &name, const double timeStep)
Definition: Rate.cpp:48
RateOptions & getOptions()
Definition: Rate.hpp:140
const timespec & getSleepStartTime() const
Definition: Rate.hpp:168
const timespec & getSleepEndTime() const
Definition: Rate.hpp:177
RateOptions options_
Rate options.
Definition: Rate.hpp:88
timespec lastErrorPrintTime_
Point in time when the last error message was printed.
Definition: Rate.hpp:106
timespec lastWarningPrintTime_
Point in time when the last warning message was printed.
Definition: Rate.hpp:104
double awakeTimeMean_
Mean of the time which elapsed between subsequent calls of sleep().
Definition: Rate.hpp:110
double getAwakeTimeVar() const
Definition: Rate.cpp:197
double awakeTimeM2_
Helper variable to compute the variance of the time step which elapsed between subsequent calls of sl...
Definition: Rate.hpp:112
timespec sleepStartTime_
Point in time when the most recent sleep() started.
Definition: Rate.hpp:91
unsigned int numErrors_
Counter storing how many times a time step took longer than the error threshold, not considering warn...
Definition: Rate.hpp:102
timespec sleepEndTime_
Point in time when the most recent sleep() ended.
Definition: Rate.hpp:93
const timespec & getStepTime() const
Definition: Rate.hpp:186
unsigned int getNumErrors() const
Definition: Rate.hpp:213
static double getDuration(const timespec &start, const timespec &end)
Definition: Rate.cpp:214
static constexpr double SEC_PER_N_SEC
Factor storing seconds per nanoseconds.
Definition: Rate.hpp:85
void reset()
Definition: Rate.cpp:74


bota_worker
Author(s):
autogenerated on Wed Mar 3 2021 03:09:10