time.h
Go to the documentation of this file.
1 /*********************************************************************
2  *
3  * Software License Agreement
4  *
5  * Copyright (c) 2020,
6  * TU Dortmund - Institute of Control Theory and Systems Engineering.
7  * All rights reserved.
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <https://www.gnu.org/licenses/>.
21  *
22  * Authors: Christoph Rösmann
23  *********************************************************************/
24 
25 #ifndef SRC_CORE_INCLUDE_CORBO_CORE_TIME_H_
26 #define SRC_CORE_INCLUDE_CORBO_CORE_TIME_H_
27 
28 #include <chrono>
29 #include <thread>
30 
31 #ifndef DISABLE_IO
32 #include <iostream>
33 #include <string>
34 #endif
35 
36 #if _WIN32
37 #include <Windows.h> // for wait / sleep
38 #undef min // this macro conflicts with std::min
39 #undef max // this macro conflicts with std::max
40 #else
41 #include <unistd.h> // for wait /sleep
42 #endif
43 
44 namespace corbo {
45 
46 namespace internals {
47 
48 template <typename T>
50 {
51  static constexpr bool value = false;
52 };
53 
54 template <typename Rep, typename Period>
55 struct is_chrono_duration<std::chrono::duration<Rep, Period>>
56 {
57  static constexpr bool value = true;
58 };
59 
60 template <typename T>
62 {
63  static constexpr bool value = false;
64 };
65 
66 template <typename Clock>
67 struct is_chrono_timepoint<std::chrono::time_point<Clock>>
68 {
69  static constexpr bool value = true;
70 };
71 } // end namespace internals
72 
73 class Time;
74 
106 class Duration
107 {
108  friend class Time;
109 
110  public:
112  Duration() {}
114  explicit Duration(int t) { fromSec(static_cast<double>(t)); }
116  explicit Duration(double t) { fromSec(t); }
117 
131  template <typename ChronoDuration>
132  explicit Duration(const ChronoDuration& duration)
133  {
134  fromChrono(duration);
135  }
136 
138  double toSec() const { return std::chrono::duration_cast<std::chrono::duration<double>>(_duration).count(); }
140  void fromSec(double t) { _duration = std::chrono::duration_cast<DurationType>(std::chrono::duration<double>(t)); }
141 
143  template <typename ChronoDuration>
144  void fromChrono(const ChronoDuration& duration)
145  {
147  "Template parameter ChronoDuration must be of type std::chrono::duration");
148  _duration = std::chrono::duration_cast<DurationType>(duration);
149  }
150 
163  template <typename DurType = std::chrono::duration<double>>
164  DurType toChrono()
165  {
166  return std::chrono::duration_cast<DurType>(_duration);
167  }
168 
169  Time toTime(double basis_time = 0);
170 
178  void sleep() { std::this_thread::sleep_for(_duration); }
179 
180  // operators
181  Duration operator+(const Duration& rhs) const { return Duration(_duration + rhs._duration); }
182  Duration operator-(const Duration& rhs) const { return Duration(_duration - rhs._duration); }
183  Duration operator-() const { return Duration(-_duration); }
184  Duration operator*(double scale) const { return Duration(toSec() * scale); }
186  {
187  _duration += rhs._duration;
188  return *static_cast<Duration*>(this);
189  }
191  {
192  _duration -= rhs._duration;
193  return *static_cast<Duration*>(this);
194  }
195  Duration& operator*=(double scale)
196  {
197  fromSec(toSec() * scale);
198  return *static_cast<Duration*>(this);
199  }
200  bool operator==(const Duration& rhs) const { return _duration == rhs._duration; }
201  inline bool operator!=(const Duration& rhs) const { return !(*static_cast<const Duration*>(this) == rhs); }
202  bool operator>(const Duration& rhs) const { return _duration > rhs._duration; }
203  bool operator<(const Duration& rhs) const { return _duration < rhs._duration; }
204  bool operator>=(const Duration& rhs) const { return _duration >= rhs._duration; }
205  bool operator<=(const Duration& rhs) const { return _duration <= rhs._duration; }
206 #ifndef DISABLE_IO
207  friend std::ostream& operator<<(std::ostream& os, const Duration& rhs)
208  {
209  os << rhs.toSec();
210  return os;
211  }
212 #endif
213 
214  private:
215  using DurationType = std::chrono::nanoseconds;
217 };
218 
251 class Time
252 {
253  public:
255  Time() {}
256 
258  explicit Time(int t) { fromSec(static_cast<double>(t)); }
260  explicit Time(double t) { fromSec(t); }
261 
268  template <typename ChronoTimePoint>
269  explicit Time(const ChronoTimePoint& timepoint)
270  {
271  fromChrono(timepoint);
272  }
273 
275  static Time now() { return Time(Clock::now()); }
276 
278  static void sleepUntil(const Time& time) { std::this_thread::sleep_until(time._timepoint); }
279 
281  double toSec() const { return std::chrono::duration_cast<std::chrono::duration<double>>(_timepoint.time_since_epoch()).count(); }
283  void fromSec(double t) { _timepoint = TimePoint(std::chrono::duration_cast<TimePoint::duration>(std::chrono::duration<double>(t))); }
284 
286  template <typename ChronoTimePoint>
287  void fromChrono(const ChronoTimePoint& timepoint)
288  {
290  "Template parameter ChronoTimePoint must be of type std::chrono::time_point");
291  _timepoint = std::chrono::time_point_cast<Clock::duration>(timepoint);
292  }
293 
294  // Operators
295  Duration operator-(const Time& rhs) const { return Duration(_timepoint - rhs._timepoint); }
296  Time operator+(const Duration& rhs) const { return Time(_timepoint + rhs._duration); }
297  Time operator-(const Duration& rhs) const { return Time(_timepoint - rhs._duration); }
298  Time& operator+=(const Duration& rhs)
299  {
300  _timepoint += rhs._duration;
301  return *static_cast<Time*>(this);
302  }
303  Time& operator-=(const Duration& rhs)
304  {
305  _timepoint -= rhs._duration;
306  return *static_cast<Time*>(this);
307  }
308  bool operator==(const Time& rhs) const { return _timepoint == rhs._timepoint; }
309  inline bool operator!=(const Time& rhs) const { return !(*static_cast<const Time*>(this) == rhs); }
310  bool operator>(const Time& rhs) const { return _timepoint > rhs._timepoint; }
311  bool operator<(const Time& rhs) const { return _timepoint < rhs._timepoint; }
312  bool operator>=(const Time& rhs) const { return _timepoint >= rhs._timepoint; }
313  bool operator<=(const Time& rhs) const { return _timepoint <= rhs._timepoint; }
314 #ifndef DISABLE_IO
315  friend std::ostream& operator<<(std::ostream& os, const Time& rhs)
316  {
317  os << rhs.toSec();
318  return os;
319  }
320 #endif
321 
322  private:
324  using Clock = std::chrono::high_resolution_clock;
326  using TimePoint = Clock::time_point;
327 
329 };
330 
353 class Rate
354 {
355  public:
357  explicit Rate(double frequency) : _cycle_time(1.0 / frequency) {}
359  explicit Rate(const Duration& dt) : _cycle_time(dt) {}
361  bool sleep();
362  void reset() { _start = Time::now(); }
363 
365  void updateCycleTime(const Duration& dt) { _cycle_time = dt; }
366 
368  Duration lastCycleTime() const { return _last_cycle_time; }
369 
370  private:
371  Time _start = Time::now();
373  Duration _last_cycle_time = Duration(0);
374 };
375 
376 } // namespace corbo
377 
378 #endif // SRC_CORE_INCLUDE_CORBO_CORE_TIME_H_
Time(int t)
Construct time object from integer (seconds)
Definition: time.h:258
std::chrono::nanoseconds DurationType
Definition: time.h:215
bool operator<(const Time &rhs) const
Definition: time.h:311
Duration & operator+=(const Duration &rhs)
Definition: time.h:185
Duration operator+(const Duration &rhs) const
Definition: time.h:181
static void sleepUntil(const Time &time)
Sleep (current thread) until the specified timestamp.
Definition: time.h:278
std::chrono::high_resolution_clock Clock
Datatype for the clock.
Definition: time.h:324
Time(double t)
Construct time object from double (seconds)
Definition: time.h:260
Duration lastCycleTime() const
Return actual execution time of the last cycle.
Definition: time.h:368
DurationType _duration
Definition: time.h:216
static Time now()
Retrieve current system time.
Definition: time.h:275
Time & operator-=(const Duration &rhs)
Definition: time.h:303
bool operator!=(const Duration &rhs) const
Definition: time.h:201
Clock::time_point TimePoint
Datatype for the timepoint.
Definition: time.h:326
void fromSec(double t)
Set duration from seconds (see Duration(double t))
Definition: time.h:140
Time()
Default constructor.
Definition: time.h:255
Duration(int t)
Construct duration from integer (seconds)
Definition: time.h:114
Duration operator-() const
Definition: time.h:183
Time(const ChronoTimePoint &timepoint)
Construct time from std::chrono::time_point object.
Definition: time.h:269
Definition: Half.h:150
Duration operator-(const Duration &rhs) const
Definition: time.h:182
bool operator<=(const Duration &rhs) const
Definition: time.h:205
static constexpr bool value
Definition: time.h:51
Time operator+(const Duration &rhs) const
Definition: time.h:296
Duration operator*(double scale) const
Definition: time.h:184
Representation of time stamps.
Definition: time.h:251
Time & operator+=(const Duration &rhs)
Definition: time.h:298
bool operator>(const Duration &rhs) const
Definition: time.h:202
Duration & operator-=(const Duration &rhs)
Definition: time.h:190
bool operator>(const Time &rhs) const
Definition: time.h:310
Rate objects can be used to run loops at a desired rate.
Definition: time.h:353
void fromChrono(const ChronoTimePoint &timepoint)
Set time stamp from std::chrono::time_stamp type.
Definition: time.h:287
friend std::ostream & operator<<(std::ostream &os, const Time &rhs)
Definition: time.h:315
TimePoint _timepoint
Definition: time.h:328
Duration operator-(const Time &rhs) const
Definition: time.h:295
void reset()
Definition: time.h:362
void sleep()
Sleep (current thread) for the specified duration.
Definition: time.h:178
bool operator!=(const Time &rhs) const
Definition: time.h:309
bool operator>=(const Time &rhs) const
Definition: time.h:312
Duration()
Default constructor.
Definition: time.h:112
bool operator<=(const Time &rhs) const
Definition: time.h:313
bool operator>=(const Duration &rhs) const
Definition: time.h:204
double toSec() const
Return duration in seconds.
Definition: time.h:138
void fromChrono(const ChronoDuration &duration)
Set duration from std::chrono::duration object (see Duration(const ChronoDuration& duration)) ...
Definition: time.h:144
Rate(const Duration &dt)
Cosntruct rate object from duration object (interval length)
Definition: time.h:359
double toSec() const
Cast time stamp to seconds.
Definition: time.h:281
DurType toChrono()
Convert duration to std::chrono::duration object.
Definition: time.h:164
Duration(const ChronoDuration &duration)
Construct duration from std::chrono::duration object.
Definition: time.h:132
Duration(double t)
Construct duration from double (seconds)
Definition: time.h:116
Duration _cycle_time
Definition: time.h:372
Time operator-(const Duration &rhs) const
Definition: time.h:297
void fromSec(double t)
Set time stamp from seconds.
Definition: time.h:283
void updateCycleTime(const Duration &dt)
Update cycle time without resetting start time.
Definition: time.h:365
bool operator==(const Time &rhs) const
Definition: time.h:308
friend std::ostream & operator<<(std::ostream &os, const Duration &rhs)
Definition: time.h:207
bool operator==(const Duration &rhs) const
Definition: time.h:200
Rate(double frequency)
Construct rate object from frequency [1/s].
Definition: time.h:357
Representation of time durations.
Definition: time.h:106
Duration & operator*=(double scale)
Definition: time.h:195
bool operator<(const Duration &rhs) const
Definition: time.h:203


control_box_rst
Author(s): Christoph Rösmann
autogenerated on Mon Feb 28 2022 22:07:52