sas
Modularised monitoring, logging, and control of robots.
Loading...
Searching...
No Matches
sas_clock.hpp
Go to the documentation of this file.
1#pragma once
2/*
3# Copyright (c) 2016-2023 Murilo Marques Marinho
4#
5# This file is part of sas_core.
6#
7# sas_core is free software: you can redistribute it and/or modify
8# it under the terms of the GNU Lesser General Public License as published by
9# the Free Software Foundation, either version 3 of the License, or
10# (at your option) any later version.
11#
12# sas_core is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU Lesser General Public License for more details.
16#
17# You should have received a copy of the GNU Lesser General Public License
18# along with sas_core. If not, see <https://www.gnu.org/licenses/>.
19#
20# ################################################################
21#
22# Author: Murilo M. Marinho, email: murilomarinho@ieee.org
23#
24# ################################################################*/
25
35#include <atomic>
36#include <chrono>
37#include <map>
38
40#include <sas_core/sas_core.hpp>
41
42namespace sas
43{
44
48class Clock : private sas::Object
49{
50public:
57private:
58
59 // https://stackoverflow.com/questions/65397041/apple-clang-why-can-i-not-create-a-time-point-from-stdchrononanoseconds
60 std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds> time_initial_;
61 std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds> next_loop_deadline_;
62
63 std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds> time_before_sleep_;
64 std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds> time_after_sleep_;
65
66 const std::chrono::nanoseconds target_sampling_time_;
67
68 std::map<TimeType,std::chrono::nanoseconds> kept_times_map_;
69
70 long overrun_sampling_time_count_;
71
72 const bool enable_statistics_;
73
74 std::map<std::tuple<TimeType,Statistics>,std::tuple<std::chrono::nanoseconds,long>> statistics_map_;
75 void _compute_statistics_();
76
77public:
78
79 Clock()=delete;
80 Clock(const int&)=delete;
81
87 explicit Clock(const double& sampling_time_in_seconds, const bool& enable_statistics=true);
88
92 void init();
93
97 void update_and_sleep();
98
103 double get_elapsed_time_sec() const;
104
109 std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds> get_initial_time() const;
110
115 std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds> get_last_update_time() const;
116
122 void safe_sleep_seconds(const double& seconds, std::atomic_bool* break_loop);
123
128 void blocking_sleep_seconds(const double& seconds);
129
135
140 long get_overrun_count() const;
141
147 double get_time(const TimeType& time_type) const;
148
157 double get_statistics(const Statistics &statistics, const TimeType &time_type) const;
158
160 [[deprecated("Use get_time(sas::Clock::Computational) instead.")]]
161 double get_computation_time() const;
162 [[deprecated("Use get_time(sas::Clock::Idle) instead.")]]
163 double get_sleep_time() const;
164 [[deprecated("Use get_time(sas::Clock::EffectiveSampling) instead.")]]
165 double get_effective_thread_sampling_time_sec() const;
166};
167
168}
169
170
171
172
Clock utility for timing and statistics in control loops.
Definition sas_clock.hpp:49
long get_overrun_count() const
Return the number of times the sampling has overrun the target period.
Definition sas_clock.cpp:157
TimeType
Enumeration of time types, see get_time() and get_statistics().
Definition sas_clock.hpp:52
@ EffectiveSampling
The effective sampling time between sleeps (EffectiveSampling = Computational + Idle).
Definition sas_clock.hpp:54
@ Computational
Time spent between when the instant the previous sleep ended and the instant when the current one sta...
Definition sas_clock.hpp:53
@ Idle
Time spent between the instant when the previous sleep started and the instant when it ended.
Definition sas_clock.hpp:55
double get_time(const TimeType &time_type) const
Get a time value for the provided TimeType.
Definition sas_clock.cpp:162
void safe_sleep_seconds(const double &seconds, std::atomic_bool *break_loop)
Sleep for the specified duration while allowing early exit via break_loop.
Definition sas_clock.cpp:141
double get_computation_time() const
Deprecated.
Definition sas_clock.cpp:120
std::chrono::time_point< std::chrono::system_clock, std::chrono::nanoseconds > get_last_update_time() const
Get the time point of the last update.
Definition sas_clock.cpp:115
double get_statistics(const Statistics &statistics, const TimeType &time_type) const
Get a statistic value for the given statistic type and TimeType.
Definition sas_clock.cpp:169
void blocking_sleep_seconds(const double &seconds)
Block the calling thread for the specified duration (no early exit).
Definition sas_clock.cpp:149
void update_and_sleep()
Update internal timing measurements and sleep to respect target sampling time.
Definition sas_clock.cpp:69
std::chrono::time_point< std::chrono::system_clock, std::chrono::nanoseconds > get_initial_time() const
Get the initial time point recorded by the clock.
Definition sas_clock.cpp:105
double get_desired_thread_sampling_time_sec() const
Return the desired sampling time for the thread in seconds.
Definition sas_clock.cpp:125
void init()
Initialize the clock internal state and timers.
Definition sas_clock.cpp:56
double get_elapsed_time_sec() const
Get elapsed time since last update in seconds.
Definition sas_clock.cpp:135
Base class for SAS objects.
Definition sas_object.hpp:40
Core numerical utilities used across the project.
Statistics
Statistical measures supported by sas_core utilities.
Definition sas_core.hpp:49
Base object class for identification and license utilities.