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
45class Clock : private sas::Object
46{
47public:
48 enum class TimeType{
49 Computational,
50 EffectiveSampling,
51 Idle
52 };
53private:
54
55 // https://stackoverflow.com/questions/65397041/apple-clang-why-can-i-not-create-a-time-point-from-stdchrononanoseconds
56 std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds> time_initial_;
57 std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds> next_loop_deadline_;
58
59 std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds> time_before_sleep_;
60 std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds> time_after_sleep_;
61
62 const std::chrono::nanoseconds target_sampling_time_;
63
64 std::map<TimeType,std::chrono::nanoseconds> kept_times_map_;
65
66 long overrun_sampling_time_count_;
67
68 const bool enable_statistics_;
69
70 std::map<std::tuple<TimeType,Statistics>,std::tuple<std::chrono::nanoseconds,long>> statistics_map_;
71 void _compute_statistics_();
72
73public:
74
75 Clock()=delete;
76 Clock(const int&)=delete;
77
83 explicit Clock(const double& sampling_time_in_seconds, const bool& enable_statistics=true);
84
88 void init();
89
93 void update_and_sleep();
94
99 double get_elapsed_time_sec() const;
100
105 std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds> get_initial_time() const;
106
111 std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds> get_last_update_time() const;
112
118 void safe_sleep_seconds(const double& seconds, std::atomic_bool* break_loop);
119
124 void blocking_sleep_seconds(const double& seconds);
125
131
136 long get_overrun_count() const;
137
143 double get_time(const TimeType& time_type) const;
144
151 double get_statistics(const Statistics &statistics, const TimeType &time_type) const;
152
154 [[deprecated("Use get_time(sas::Clock::Computational) instead.")]]
155 double get_computation_time() const;
156 [[deprecated("Use get_time(sas::Clock::Idle) instead.")]]
157 double get_sleep_time() const;
158 [[deprecated("Use get_time(sas::Clock::EffectiveSampling) instead.")]]
159 double get_effective_thread_sampling_time_sec() const;
160};
161
162}
163
164
165
166
Definition sas_clock.hpp:46
long get_overrun_count() const
Return the number of times the sampling has overrun the target period.
Definition sas_clock.cpp:152
double get_time(const TimeType &time_type) const
Get a time value for the provided TimeType.
Definition sas_clock.cpp:157
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:136
double get_computation_time() const
Deprecated.
Definition sas_clock.cpp:115
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:110
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:164
void blocking_sleep_seconds(const double &seconds)
Block the calling thread for the specified duration (no early exit)
Definition sas_clock.cpp:144
void update_and_sleep()
Update internal timing measurements and sleep to respect target sampling time.
Definition sas_clock.cpp:64
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:100
double get_desired_thread_sampling_time_sec() const
Return the desired sampling time for the thread in seconds.
Definition sas_clock.cpp:120
void init()
Initialize the clock internal state and timers.
Definition sas_clock.cpp:51
double get_elapsed_time_sec() const
Get elapsed time since last update in seconds.
Definition sas_clock.cpp:130
Definition sas_object.hpp:39
Core numerical utilities and helpers used across the project.
Base object class for identification and license utilities.