time.cpp
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 #include <corbo-core/time.h>
26 
27 namespace corbo {
28 
29 // implementation based on ros::Rate
30 // (http://docs.ros.org/diamondback/api/rostime/html/rate_8cpp_source.html#l00040)
32 {
33  Time expected_end = _start + _cycle_time;
34  Time actual_end = Time::now();
35 
36  // backwards jump in time
37  if (actual_end < _start) expected_end = actual_end + _cycle_time;
38 
39  Duration sleep_duration = expected_end - actual_end;
40  _last_cycle_time = actual_end - _start;
41 
42  _start = expected_end;
43 
44  // only sleep if positive sleep duration
45  if (sleep_duration <= Duration(0))
46  {
47  if (actual_end > expected_end + _cycle_time) _start = actual_end;
48  return !(sleep_duration < Duration(0));
49  }
50  sleep_duration.sleep();
51  return true;
52 }
53 
54 Time Duration::toTime(double basis_time) { return Time(basis_time) + *this; }
55 
56 } // namespace corbo
static Time now()
Retrieve current system time.
Definition: time.h:275
Representation of time stamps.
Definition: time.h:251
Time _start
Definition: time.h:371
Time toTime(double basis_time=0)
Definition: time.cpp:54
void sleep()
Sleep (current thread) for the specified duration.
Definition: time.h:178
bool sleep()
Sleep for the remaining duration to met the desired frequency (w.r.t previous sleep call) ...
Definition: time.cpp:31
Duration _cycle_time
Definition: time.h:372
Duration _last_cycle_time
Definition: time.h:373
Representation of time durations.
Definition: time.h:106


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