CTRE Phoenix 6 C++ 26.50.0-alpha-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 * When used with CANivore, the only inaccuracy in this measurement is latency
40 * from CAN bus arbitration.
41 */
43 /**
44 * This timestamp source requires Phoenix Pro.
45 *
46 * Timestamp as reported by the device.
47 * This timestamp is captured when the device transmits the signal value.
48 * Because it is timestamped in the device, it is the most accurate timestamp source.
49 *
50 * This timestamp is synchronized to the CANivore clock, which is itself synchronized
51 * to the system monotonic clock. As a result, this timestamp source requires a CANivore.
52 *
53 * It can be assumed there is no latency between this timestamp and when the data was taken.
54 */
55 Device = 2,
56 };
57
58 private:
59 wpi::units::second_t time{};
61 bool valid{false};
62
63 public:
64 /**
65 * \brief Construct a new invalid Timestamp.
66 */
67 constexpr Timestamp() = default;
68
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 constexpr Timestamp(wpi::units::second_t time, TimestampSource source, bool valid = true) :
77 time{time},
78 source{source},
79 valid{valid}
80 {}
81
82 /**
83 * \brief Get the time in seconds as reported from this timestamp
84 *
85 * \returns Time in seconds
86 */
87 constexpr wpi::units::second_t GetTime() const
88 {
89 return this->time;
90 }
91 /**
92 * \brief Get the source of this timestamp
93 *
94 * \returns Source of this timestamp
95 */
96 constexpr TimestampSource GetSource() const
97 {
98 return this->source;
99 }
100 /**
101 * \brief Get the latency of this timestamp compared to now
102 *
103 * \returns Difference between now and this timestamp
104 */
105 wpi::units::second_t GetLatency() const
106 {
107 return utils::GetCurrentTime() - this->time;
108 }
109 /**
110 * \brief Returns if this Timestamp is valid or not.
111 *
112 * \returns true when this is valid
113 */
114 constexpr bool IsValid() const
115 {
116 return this->valid;
117 }
118 };
119
120 /**
121 * \brief A collection of timestamps for a received signal.
122 */
123 class AllTimestamps final {
124 private:
125 Timestamp systemTimestamp{};
126 Timestamp canivoreTimestamp{};
127 Timestamp deviceTimestamp{};
128
129 public:
130 void Update(Timestamp const &newSystemTimestamp, Timestamp const &newCanivoreTimestamp, Timestamp const &newDeviceTimestamp)
131 {
132 systemTimestamp = newSystemTimestamp;
133 canivoreTimestamp = newCanivoreTimestamp;
134 deviceTimestamp = newDeviceTimestamp;
135 }
136
137 /**
138 * \brief Get the most accurate timestamp available.
139 *
140 * \details The timestamp sources from most to least accurate are:
141 *
142 * - Timestamp#TimestampSource#Device
143 * - Timestamp#TimestampSource#CANivore
144 * - Timestamp#TimestampSource#System
145 *
146 * Note that some of these sources may not be available.
147 *
148 * \returns The most accurate timestamp available
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 /**
163 * \brief Get the timestamp as reported by the Timestamp#TimestampSource#System source.
164 *
165 * \returns Timestamp as reported by the System
166 */
167 Timestamp const &GetSystemTimestamp() const { return systemTimestamp; }
168 /**
169 * \brief Get the timestamp as reported by the Timestamp#TimestampSource#CANivore source.
170 *
171 * \returns Timestamp as reported by the CANivore
172 */
173 Timestamp const &GetCANivoreTimestamp() const { return canivoreTimestamp; }
174 /**
175 * \brief Get the timestamp as reported by the Timestamp#TimestampSource#Device source.
176 *
177 * \returns Timestamp as reported by the device
178 */
179 Timestamp const &GetDeviceTimestamp() const { return deviceTimestamp; }
180 };
181
182}
183}
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:123
Timestamp const & GetBestTimestamp() const
Get the most accurate timestamp available.
Definition Timestamp.hpp:150
Timestamp const & GetCANivoreTimestamp() const
Get the timestamp as reported by the Timestamp::TimestampSource::CANivore source.
Definition Timestamp.hpp:173
void Update(Timestamp const &newSystemTimestamp, Timestamp const &newCanivoreTimestamp, Timestamp const &newDeviceTimestamp)
Definition Timestamp.hpp:130
Timestamp const & GetDeviceTimestamp() const
Get the timestamp as reported by the Timestamp::TimestampSource::Device source.
Definition Timestamp.hpp:179
Timestamp const & GetSystemTimestamp() const
Get the timestamp as reported by the Timestamp::TimestampSource::System source.
Definition Timestamp.hpp:167
Information about the timestamp of a signal.
Definition Timestamp.hpp:17
constexpr TimestampSource GetSource() const
Get the source of this timestamp.
Definition Timestamp.hpp:96
constexpr bool IsValid() const
Returns if this Timestamp is valid or not.
Definition Timestamp.hpp:114
TimestampSource
Source of the timestamp.
Definition Timestamp.hpp:22
@ CANivore
Timestamp as reported by the CANivore.
Definition Timestamp.hpp:42
@ System
Timestamp as reported by the system.
Definition Timestamp.hpp:31
@ Device
This timestamp source requires Phoenix Pro.
Definition Timestamp.hpp:55
wpi::units::second_t GetLatency() const
Get the latency of this timestamp compared to now.
Definition Timestamp.hpp:105
constexpr Timestamp()=default
Construct a new invalid Timestamp.
constexpr Timestamp(wpi::units::second_t time, TimestampSource source, bool valid=true)
Construct a new Timestamp for the given source.
Definition Timestamp.hpp:76
constexpr wpi::units::second_t GetTime() const
Get the time in seconds as reported from this timestamp.
Definition Timestamp.hpp:87
wpi::units::second_t GetCurrentTime()
Get the current timestamp.
Definition Utils.hpp:27
Definition ExternalFeedbackConfigs.hpp:16
Definition motor_constants.h:14