CTRE Phoenix 6 C++ 25.0.0-beta-4
Loading...
Searching...
No Matches
TimeUtil.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) Cross The Road Electronics.  All rights reserved.
3 * License information can be found in CTRE_LICENSE.txt
4 * For support and suggestions contact support@ctr-electronics.com or file
5 * an issue tracker at https://github.com/CrossTheRoadElec/Phoenix-Releases
6 */
7#pragma once
8
9#ifdef __FRC_ROBORIO__
10#include <unistd.h>
11#else
12#include <thread>
13#endif
14
15#include <chrono>
16
17namespace ctre {
18namespace phoenix {
19namespace platform {
20
21 /**
22 * \param timeUs How long to yield current thread in microseconds (us).
23 * If platform cannot honor us resolution, round up to nearest
24 * value that platform can honor.
25 */
26 static inline void SleepUs(int timeUs)
27 {
28#ifdef __FRC_ROBORIO__
29 usleep(timeUs);
30#else
31 std::this_thread::sleep_for(std::chrono::microseconds{timeUs});
32#endif
33 }
34
35 /** The raw current time source. */
36 std::chrono::steady_clock::duration CurrentTimeRaw();
37
38 /**
39 * Returns the current time of the system, converted to the given duration.
40 * The default duration is in microseconds.
41 *
42 * This time source is typically continuous and monotonic. However, it may
43 * be overridden in simulation to use a non-monotonic, non-continuous source.
44 */
45 template <typename DURATION = std::chrono::microseconds>
46 static inline auto CurrentTime()
47 {
48 using namespace std::chrono;
49 return duration_cast<DURATION>(CurrentTimeRaw()).count();
50 }
51
52 /**
53 * Returns the monotonic time of the system, converted to the given duration.
54 * The default duration is in microseconds.
55 *
56 * This time source is guaranteed to be continuous and monotonic.
57 */
58 template <typename DURATION = std::chrono::microseconds>
59 static inline auto SystemTime()
60 {
61 using namespace std::chrono;
62 return duration_cast<DURATION>(steady_clock::now().time_since_epoch()).count();
63 }
64
65} // namespace platform
66} // namespace phoenix
67} // namespace ctre
static auto CurrentTime()
Returns the current time of the system, converted to the given duration.
Definition TimeUtil.hpp:46
static void SleepUs(int timeUs)
Definition TimeUtil.hpp:26
static auto SystemTime()
Returns the monotonic time of the system, converted to the given duration.
Definition TimeUtil.hpp:59
std::chrono::steady_clock::duration CurrentTimeRaw()
The raw current time source.
Definition StatusCodes.h:18