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