CTRE Phoenix 6 C++ 24.50.0-alpha-2
HootReplay.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#include <string>
12#include <vector>
13
14namespace ctre {
15namespace phoenix6 {
16
17/**
18 * \brief Static class for controlling Phoenix 6 hoot log replay.
19 *
20 * This replays all signals in the given hoot log in simulation. Hoot logs can
21 * be created by a robot program using SignalLogger. Only one hoot log,
22 * corresponding to one CAN bus, may be replayed at a time.
23 *
24 * During replay, all transmits from the robot program are ignored. This includes
25 * features such as control requests, configs, and setting signal update frequency.
26 * Additionally, Tuner X is not functional during log replay.
27 */
29public:
30 /**
31 * \brief Loads the given file and starts signal log replay. Only one
32 * hoot log, corresponding to one CAN bus, may be replayed at a time.
33 *
34 * This must be called before constructing any devices or checking
35 * CAN bus status. The CANBus(std::string_view, char const *)
36 * constructor can be used when constructing devices to guarantee
37 * that this API is called first.
38 *
39 * \details When using relative paths, the file path is typically
40 * relative to the top-level folder of the robot project.
41 *
42 * \param filepath Path and name of the hoot file to load
43 * \returns Status of opening and reading the file for replay
44 */
45 static ctre::phoenix::StatusCode LoadFile(char const *filepath);
46 /**
47 * \brief Ends the hoot log replay. This stops the replay if it is running,
48 * closes the hoot log, and clears all signals read from the file.
49 */
50 static void CloseFile();
51 /**
52 * \brief Gets whether a valid hoot log file is currently loaded.
53 *
54 * \returns true if a valid hoot log file is loaded
55 */
56 static bool IsFileLoaded();
57
58 /**
59 * \brief Starts or resumes the hoot log replay.
60 *
61 * \returns Status of starting or resuming replay
62 */
64 /**
65 * \brief Pauses the hoot log replay. This maintains the current position
66 * in the log replay so it can be resumed later.
67 *
68 * \returns Status of pausing replay
69 */
71 /**
72 * \brief Stops the hoot log replay. This resets the current position in
73 * the log replay to the start.
74 *
75 * \returns Status of stopping replay
76 */
78 /**
79 * \brief Restarts the hoot log replay from the start of the log.
80 * This is equivalent to calling #Stop followed by #Play.
81 *
82 * \returns Status of restarting replay
83 */
85 {
86 auto retval = Stop();
87 if (retval.IsOK()) {
88 retval = Play();
89 }
90 return retval;
91 }
92
93 /**
94 * \brief Gets whether hoot log replay is actively playing.
95 *
96 * This API will return true in programs that do not support
97 * replay, making it safe to call without first checking if
98 * the program supports replay.
99 *
100 * \returns true if replay is playing back signals
101 */
102 static bool IsPlaying()
103 {
104 return WaitForPlaying(0_s);
105 }
106
107 /**
108 * \brief Waits until hoot log replay is actively playing.
109 *
110 * This API will immediately return true in programs that do
111 * not support replay, making it safe to call without first
112 * checking if the program supports replay
113 *
114 * Since this can block the calling thread, this should not
115 * be called with a non-zero timeout on the main thread.
116 *
117 * This can also be used with a timeout of 0 to perform
118 * a non-blocking check, which is equivalent to #IsPlaying.
119 *
120 * \param timeout Max time to wait for replay to start playing
121 * \returns true if replay is playing back signals
122 */
123 static bool WaitForPlaying(units::second_t timeout)
124 {
125 return WaitForPlayingImpl(timeout.value());
126 }
127
128 /**
129 * \brief Sets the speed of the hoot log replay. A speed of 1.0 corresponds
130 * to replaying the file in real time, and larger values increase the speed.
131 *
132 * - Minimum Value: 0.01
133 * - Maximum Value: 100.0
134 * - Default Value: 1.0
135 *
136 * \param speed Speed of the hoot log replay
137 */
138 static void SetSpeed(double speed);
139 /**
140 * \brief Advances the hoot log replay time by the given value. Replay must
141 * be paused or stopped before advancing its time.
142 *
143 * \param stepTimeSeconds The amount of time to advance
144 * \returns Status of advancing the replay time
145 */
146 static ctre::phoenix::StatusCode StepTiming(units::time::second_t stepTimeSeconds)
147 {
148 return StepTimingImpl(stepTimeSeconds.value());
149 }
150
151 /**
152 * \brief Stores information about a user signal from replay.
153 */
154 template <typename T>
155 struct SignalData {
156 /**
157 * \brief The name of the signal
158 */
159 std::string_view name;
160 /**
161 * \brief The units of the signal
162 */
163 std::string units;
164 /**
165 * \brief The timestamp of the signal
166 */
167 units::second_t timestamp;
168 /**
169 * \brief Status code response of getting the signal
170 */
172 /**
173 * \brief The value of the signal
174 */
176 };
177
178 /**
179 * \brief Gets a raw-bytes user signal.
180 *
181 * \param name Name of the signal
182 * \returns Structure with all information about the signal
183 */
184 static SignalData<std::vector<uint8_t>> GetRaw(std::string_view name)
185 {
186 return GetRawImpl(name).ToSignalData();
187 }
188 /**
189 * \brief Gets a boolean user signal.
190 *
191 * \param name Name of the signal
192 * \returns Structure with all information about the signal
193 */
194 static SignalData<bool> GetBoolean(std::string_view name)
195 {
196 return GetBooleanImpl(name).ToSignalData();
197 }
198 /**
199 * \brief Gets an integer user signal.
200 *
201 * \param name Name of the signal
202 * \returns Structure with all information about the signal
203 */
204 static SignalData<int64_t> GetInteger(std::string_view name)
205 {
206 return GetIntegerImpl(name).ToSignalData();
207 }
208 /**
209 * \brief Gets a float user signal.
210 *
211 * \param name Name of the signal
212 * \returns Structure with all information about the signal
213 */
214 static SignalData<float> GetFloat(std::string_view name)
215 {
216 return GetFloatImpl(name).ToSignalData();
217 }
218 /**
219 * \brief Gets a double user signal.
220 *
221 * \param name Name of the signal
222 * \returns Structure with all information about the signal
223 */
224 static SignalData<double> GetDouble(std::string_view name)
225 {
226 return GetDoubleImpl(name).ToSignalData();
227 }
228
229 /**
230 * \brief Gets a unit value user signal.
231 *
232 * \param name Name of the signal
233 * \returns Structure with all information about the signal
234 */
235 template <typename U, typename = std::enable_if_t<units::traits::is_unit_t_v<U>>>
236 static SignalData<U> GetValue(std::string_view name)
237 {
238 SignalData<double> doubleSig = GetDouble(name);
239 return {
240 doubleSig.name,
241 std::move(doubleSig.units),
242 doubleSig.timestamp,
243 doubleSig.status,
244 U{doubleSig.value}
245 };
246 }
247
248 /**
249 * \brief Gets a string user signal.
250 *
251 * \param name Name of the signal
252 * \returns Structure with all information about the signal
253 */
254 static SignalData<std::string> GetString(std::string_view name)
255 {
256 return GetStringImpl(name).ToSignalData();
257 }
258 /**
259 * \brief Get a boolean array user signal.
260 *
261 * \param name Name of the signal
262 * \returns Structure with all information about the signal
263 */
265 {
266 return GetBooleanArrayImpl(name).ToSignalData();
267 }
268 /**
269 * \brief Get an integer array user signal.
270 *
271 * \param name Name of the signal
272 * \returns Structure with all information about the signal
273 */
275 {
276 return GetIntegerArrayImpl(name).ToSignalData();
277 }
278 /**
279 * \brief Get a float array user signal.
280 *
281 * \param name Name of the signal
282 * \returns Structure with all information about the signal
283 */
284 static SignalData<std::vector<float>> GetFloatArray(std::string_view name)
285 {
286 return GetFloatArrayImpl(name).ToSignalData();
287 }
288 /**
289 * \brief Get a double array user signal.
290 *
291 * \param name Name of the signal
292 * \returns Structure with all information about the signal
293 */
294 static SignalData<std::vector<double>> GetDoubleArray(std::string_view name)
295 {
296 return GetDoubleArrayImpl(name).ToSignalData();
297 }
298
299private:
300 static bool WaitForPlayingImpl(double timeoutSeconds);
301 static ctre::phoenix::StatusCode StepTimingImpl(double stepTimeSeconds);
302
303 template <typename T>
304 struct UnitlessSignalData {
305 std::string_view name;
306 std::string units;
307 double timestampSec;
309 T value;
310
311 SignalData<T> ToSignalData() &&
312 {
313 return {
314 name,
315 std::move(units),
316 units::second_t{timestampSec},
317 status,
318 std::move(value)
319 };
320 }
321 };
322
323 static UnitlessSignalData<std::vector<uint8_t>> GetRawImpl(std::string_view name);
324 static UnitlessSignalData<bool> GetBooleanImpl(std::string_view name);
325 static UnitlessSignalData<int64_t> GetIntegerImpl(std::string_view name);
326 static UnitlessSignalData<float> GetFloatImpl(std::string_view name);
327 static UnitlessSignalData<double> GetDoubleImpl(std::string_view name);
328 static UnitlessSignalData<std::string> GetStringImpl(std::string_view name);
329 static UnitlessSignalData<std::vector<uint8_t>> GetBooleanArrayImpl(std::string_view name);
330 static UnitlessSignalData<std::vector<int64_t>> GetIntegerArrayImpl(std::string_view name);
331 static UnitlessSignalData<std::vector<float>> GetFloatArrayImpl(std::string_view name);
332 static UnitlessSignalData<std::vector<double>> GetDoubleArrayImpl(std::string_view name);
333};
334
335}
336}
Static class for controlling Phoenix 6 hoot log replay.
Definition: HootReplay.hpp:28
static ctre::phoenix::StatusCode Play()
Starts or resumes the hoot log replay.
static SignalData< float > GetFloat(std::string_view name)
Gets a float user signal.
Definition: HootReplay.hpp:214
static SignalData< std::string > GetString(std::string_view name)
Gets a string user signal.
Definition: HootReplay.hpp:254
static ctre::phoenix::StatusCode Stop()
Stops the hoot log replay.
static SignalData< std::vector< uint8_t > > GetRaw(std::string_view name)
Gets a raw-bytes user signal.
Definition: HootReplay.hpp:184
static bool IsFileLoaded()
Gets whether a valid hoot log file is currently loaded.
static void CloseFile()
Ends the hoot log replay.
static void SetSpeed(double speed)
Sets the speed of the hoot log replay.
static bool WaitForPlaying(units::second_t timeout)
Waits until hoot log replay is actively playing.
Definition: HootReplay.hpp:123
static SignalData< std::vector< double > > GetDoubleArray(std::string_view name)
Get a double array user signal.
Definition: HootReplay.hpp:294
static ctre::phoenix::StatusCode StepTiming(units::time::second_t stepTimeSeconds)
Advances the hoot log replay time by the given value.
Definition: HootReplay.hpp:146
static bool IsPlaying()
Gets whether hoot log replay is actively playing.
Definition: HootReplay.hpp:102
static SignalData< std::vector< uint8_t > > GetBooleanArray(std::string_view name)
Get a boolean array user signal.
Definition: HootReplay.hpp:264
static SignalData< std::vector< float > > GetFloatArray(std::string_view name)
Get a float array user signal.
Definition: HootReplay.hpp:284
static ctre::phoenix::StatusCode LoadFile(char const *filepath)
Loads the given file and starts signal log replay.
static SignalData< bool > GetBoolean(std::string_view name)
Gets a boolean user signal.
Definition: HootReplay.hpp:194
static SignalData< int64_t > GetInteger(std::string_view name)
Gets an integer user signal.
Definition: HootReplay.hpp:204
static SignalData< double > GetDouble(std::string_view name)
Gets a double user signal.
Definition: HootReplay.hpp:224
static SignalData< std::vector< int64_t > > GetIntegerArray(std::string_view name)
Get an integer array user signal.
Definition: HootReplay.hpp:274
static SignalData< U > GetValue(std::string_view name)
Gets a unit value user signal.
Definition: HootReplay.hpp:236
static ctre::phoenix::StatusCode Restart()
Restarts the hoot log replay from the start of the log.
Definition: HootReplay.hpp:84
static ctre::phoenix::StatusCode Pause()
Pauses the hoot log replay.
Status codes reported by APIs, including OK, warnings, and errors.
Definition: StatusCodes.h:27
Represents the state of one swerve module.
Definition: StatusCodes.h:18
Stores information about a user signal from replay.
Definition: HootReplay.hpp:155
ctre::phoenix::StatusCode status
Status code response of getting the signal.
Definition: HootReplay.hpp:171
std::string_view name
The name of the signal.
Definition: HootReplay.hpp:159
std::string units
The units of the signal.
Definition: HootReplay.hpp:163
units::second_t timestamp
The timestamp of the signal.
Definition: HootReplay.hpp:167
T value
The value of the signal.
Definition: HootReplay.hpp:175