CTRE Phoenix 6 C++ 26.0.0-beta-1
Loading...
Searching...
No Matches
Timestamp.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
10
11namespace ctre {
12namespace phoenix6 {
13
14 /**
15 * \brief Information about the timestamp of a signal.
16 */
17 class Timestamp final {
18 public:
19 /**
20 * \brief Source of the timestamp.
21 */
22 enum class TimestampSource {
23 /**
24 * Timestamp as reported by the system.
25 * This timestamp is captured when the system receives the signal value.
26 *
27 * This timestamp is present on all systems and is guaranteed to be monotonic.
28 * However, this timestamp is the least accurate due to latency from processing
29 * delays within the system.
30 */
31 System = 0,
32 /**
33 * Timestamp as reported by the CANivore.
34 * This timestamp is captured when the CANivore receives the signal value.
35 *
36 * The CANivore is synchronized to the system monotonic clock and benefits
37 * from reduced latency over the TimestampSource#System timestamp.
38 *
39 * On the native roboRIO CAN bus, this timestamp is equivalent to the TimestampSource#System timestamp.
40 *
41 * When used with CANivore, the only inaccuracy in this measurement is latency
42 * from CAN bus arbitration.
43 */
44 CANivore = 1,
45 /**
46 * This timestamp source requires Phoenix Pro.
47 *
48 * Timestamp as reported by the device.
49 * This timestamp is captured when the device transmits the signal value.
50 * Because it is timestamped in the device, it is the most accurate timestamp source.
51 *
52 * This timestamp is synchronized to the CANivore clock, which is itself synchronized
53 * to the system monotonic clock. As a result, this timestamp source requires a CANivore.
54 *
55 * It can be assumed there is no latency between this timestamp and when the data was taken.
56 */
57 Device = 2,
58 };
59
60 private:
61 units::time::second_t time{};
62 TimestampSource source{};
63 bool valid{false};
64
65 public:
66 /**
67 * \brief Construct a new invalid Timestamp.
68 */
69 constexpr Timestamp() = default;
70
71 /**
72 * \brief Construct a new Timestamp for the given source.
73 *
74 * \param time The time in seconds
75 * \param source The timestamp source
76 * \param valid Whether the timestamp is valid
77 */
78 constexpr Timestamp(units::time::second_t time, TimestampSource source, bool valid = true) :
79 time{time},
81 valid{valid}
82 {}
83
84 /**
85 * \brief Get the time in seconds as reported from this timestamp
86 *
87 * \returns Time in seconds
88 */
89 constexpr units::time::second_t GetTime() const
90 {
91 return this->time;
92 }
93 /**
94 * \brief Get the source of this timestamp
95 *
96 * \returns Source of this timestamp
97 */
98 constexpr TimestampSource GetSource() const
99 {
100 return this->source;
101 }
102 /**
103 * \brief Get the latency of this timestamp compared to now
104 *
105 * \returns Difference between now and this timestamp
106 */
107 units::time::second_t GetLatency() const
108 {
109 return utils::GetCurrentTime() - this->time;
110 }
111 /**
112 * \brief Returns if this Timestamp is valid or not.
113 *
114 * \returns true when this is valid
115 */
116 constexpr bool IsValid() const
117 {
118 return this->valid;
119 }
120 };
121
122 /**
123 * \brief A collection of timestamps for a received signal.
124 */
125 class AllTimestamps final {
126 private:
127 Timestamp systemTimestamp{};
128 Timestamp canivoreTimestamp{};
129 Timestamp deviceTimestamp{};
130
131 public:
132 void Update(Timestamp const &newSystemTimestamp, Timestamp const &newCanivoreTimestamp, Timestamp const &newDeviceTimestamp)
133 {
134 systemTimestamp = newSystemTimestamp;
135 canivoreTimestamp = newCanivoreTimestamp;
136 deviceTimestamp = newDeviceTimestamp;
137 }
138
139 /**
140 * \brief Get the most accurate timestamp available.
141 *
142 * \details The timestamp sources from most to least accurate are:
143 *
144 * - Timestamp#TimestampSource#Device
145 * - Timestamp#TimestampSource#CANivore
146 * - Timestamp#TimestampSource#System
147 *
148 * Note that some of these sources may not be available.
149 *
150 * \returns The most accurate timestamp available
151 */
153 {
154 if (deviceTimestamp.IsValid()) {
155 return deviceTimestamp;
156 }
157 if (canivoreTimestamp.IsValid()) {
158 return canivoreTimestamp;
159 }
160 /* System timestamp is always available */
161 return systemTimestamp;
162 }
163
164 /**
165 * \brief Get the timestamp as reported by the Timestamp#TimestampSource#System source.
166 *
167 * \returns Timestamp as reported by the System
168 */
169 Timestamp const &GetSystemTimestamp() const { return systemTimestamp; }
170 /**
171 * \brief Get the timestamp as reported by the Timestamp#TimestampSource#CANivore source.
172 *
173 * \returns Timestamp as reported by the CANivore
174 */
175 Timestamp const &GetCANivoreTimestamp() const { return canivoreTimestamp; }
176 /**
177 * \brief Get the timestamp as reported by the Timestamp#TimestampSource#Device source.
178 *
179 * \returns Timestamp as reported by the device
180 */
181 Timestamp const &GetDeviceTimestamp() const { return deviceTimestamp; }
182 };
183
184}
185}
Cross the Road Electronics End User License Agreement This Software License or “Customer” and Cross The Road Electronics a Michigan based company with its principal place of business located at MI Terms Agreement this End User License Agreement that forms the entire agreement between You and the Company regarding the use of the Software CAN BUS a communication standard designed to allow hardware devices to communicate with each other Leone USA Content refers to content such as or other information that can be linked to or otherwise made available by regardless of the form of that content Device a hardware product or sold by Company FRC FIRST Robotics and or is stored with Devices This includes and is not limited to Device source
Definition CTRE_LICENSE.txt:27
A collection of timestamps for a received signal.
Definition Timestamp.hpp:125
Timestamp const & GetBestTimestamp() const
Get the most accurate timestamp available.
Definition Timestamp.hpp:152
Timestamp const & GetCANivoreTimestamp() const
Get the timestamp as reported by the Timestamp::TimestampSource::CANivore source.
Definition Timestamp.hpp:175
void Update(Timestamp const &newSystemTimestamp, Timestamp const &newCanivoreTimestamp, Timestamp const &newDeviceTimestamp)
Definition Timestamp.hpp:132
Timestamp const & GetDeviceTimestamp() const
Get the timestamp as reported by the Timestamp::TimestampSource::Device source.
Definition Timestamp.hpp:181
Timestamp const & GetSystemTimestamp() const
Get the timestamp as reported by the Timestamp::TimestampSource::System source.
Definition Timestamp.hpp:169
Information about the timestamp of a signal.
Definition Timestamp.hpp:17
constexpr units::time::second_t GetTime() const
Get the time in seconds as reported from this timestamp.
Definition Timestamp.hpp:89
constexpr TimestampSource GetSource() const
Get the source of this timestamp.
Definition Timestamp.hpp:98
constexpr bool IsValid() const
Returns if this Timestamp is valid or not.
Definition Timestamp.hpp:116
TimestampSource
Source of the timestamp.
Definition Timestamp.hpp:22
@ System
Timestamp as reported by the system.
units::time::second_t GetLatency() const
Get the latency of this timestamp compared to now.
Definition Timestamp.hpp:107
constexpr Timestamp(units::time::second_t time, TimestampSource source, bool valid=true)
Construct a new Timestamp for the given source.
Definition Timestamp.hpp:78
constexpr Timestamp()=default
Construct a new invalid Timestamp.
units::second_t GetCurrentTime()
Get the current timestamp.
Definition Utils.hpp:29
Definition motor_constants.h:14