CTRE Phoenix Pro C++ 23.0.12
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 phoenixpro {
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 is not currently implemented in this version of 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 */
74 Timestamp(units::time::second_t time, TimestampSource source) :
75 time{time},
77 valid{true}
78 {
79 }
80 /**
81 * \brief Construct a new invalid Timestamp.
82 */
84 time{},
85 source{},
86 valid{false}
87 {
88 }
89
90 /**
91 * \brief Get the time in seconds as reported from this timestamp
92 *
93 * \returns Time in seconds
94 */
95 units::time::second_t GetTime() const
96 {
97 return this->time;
98 }
99 /**
100 * \brief Get the source of this timestamp
101 *
102 * \returns Source of this timestamp
103 */
105 {
106 return this->source;
107 }
108 /**
109 * \brief Get the latency of this timestamp compared to now
110 *
111 * \returns Difference between now and this timestamp
112 */
113 units::time::second_t GetLatency() const
114 {
115 return units::time::second_t{GetCurrentTimeSeconds()} - this->time;
116 }
117 /**
118 * \brief Returns if this Timestamp is valid or not.
119 *
120 * \returns true when this is valid
121 */
122 bool IsValid() const
123 {
124 return this->valid;
125 }
126 };
127
128 /**
129 * \brief A collection of timestamps for a received signal.
130 */
132 private:
133 Timestamp systemTimestamp{};
134 Timestamp canivoreTimestamp{};
135
136 public:
137 void Update(const Timestamp& newSystemTimestamp, const Timestamp& newCanivoreTimestamp, const Timestamp& newDeviceTimestamp)
138 {
139 systemTimestamp = newSystemTimestamp;
140 canivoreTimestamp = newCanivoreTimestamp;
141 }
142 /**
143 * \brief Get the best timestamp available.
144 *
145 * \returns Best available timestamp
146 */
148 {
149 if (canivoreTimestamp.IsValid()) return canivoreTimestamp;
150 /* System timestamp is always available */
151 return systemTimestamp;
152 }
153 /**
154 * \brief Get the timestamp as reported by the system.
155 *
156 * \returns Timestamp#TimestampSource#System timestamp
157 */
158 const Timestamp& GetSystemTimestamp() const { return systemTimestamp; }
159 /**
160 * \brief Get the timestamp as reported by the CANivore.
161 *
162 * \returns Timestamp#TimestampSource#CANivore timestamp
163 */
164 const Timestamp& GetCANivoreTimestamp() const { return canivoreTimestamp; }
165 };
166
167}
168}
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 means this End User License Agreement that forms the entire agreement between You and the Company regarding the use of the Software 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 means a hardware product or sold by Company Software means a collection of software supplied by Company that interacts with Devices and or simulates the behavior of and or is stored with Devices This includes and is not limited to Device source
Definition: CTRE_LICENSE.txt:23
A collection of timestamps for a received signal.
Definition: Timestamp.hpp:131
const Timestamp & GetSystemTimestamp() const
Get the timestamp as reported by the system.
Definition: Timestamp.hpp:158
void Update(const Timestamp &newSystemTimestamp, const Timestamp &newCanivoreTimestamp, const Timestamp &newDeviceTimestamp)
Definition: Timestamp.hpp:137
const Timestamp & GetBestTimestamp() const
Get the best timestamp available.
Definition: Timestamp.hpp:147
const Timestamp & GetCANivoreTimestamp() const
Get the timestamp as reported by the CANivore.
Definition: Timestamp.hpp:164
Information about the timestamp of a signal.
Definition: Timestamp.hpp:20
Timestamp(units::time::second_t time, TimestampSource source)
Construct a new Timestamp for the given source.
Definition: Timestamp.hpp:74
Timestamp()
Construct a new invalid Timestamp.
Definition: Timestamp.hpp:83
TimestampSource GetSource() const
Get the source of this timestamp.
Definition: Timestamp.hpp:104
units::time::second_t GetLatency() const
Get the latency of this timestamp compared to now.
Definition: Timestamp.hpp:113
units::time::second_t GetTime() const
Get the time in seconds as reported from this timestamp.
Definition: Timestamp.hpp:95
bool IsValid() const
Returns if this Timestamp is valid or not.
Definition: Timestamp.hpp:122
TimestampSource
Source of the timestamp.
Definition: Timestamp.hpp:25
CTREXPORT double GetCurrentTimeSeconds()
Get the current timestamp in seconds.
Definition: string_util.hpp:14