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