CTRE Phoenix 6 C++ 26.1.3
Loading...
Searching...
No Matches
StatusSignal.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
14#include <array>
15#include <functional>
16#include <map>
17#include <ostream>
18#include <span>
19#include <sstream>
20#include <string>
21#include <units/frequency.h>
22#include <units/math.h>
23#include <units/time.h>
24
25namespace ctre {
26namespace phoenix6 {
27
28 namespace hardware {
29 class ParentDevice;
30 }
31
32 template <typename T>
33 class StatusSignal;
34
35 /**
36 * \brief Class that provides operations to
37 * retrieve information about a status signal.
38 */
40 private:
41 hardware::DeviceIdentifier deviceIdentifier;
42 uint16_t spn;
43 std::string name;
44 std::function<void()> _checkFirmVersFunction;
45
46 std::map<uint16_t, std::string> _unitStrings{};
47 uint16_t _unitsKey;
48
49 units::time::second_t _lastTimestamp{0_s};
50
51 protected:
52 std::string units;
53 double baseValue = 0;
56
58 hardware::DeviceIdentifier deviceIdentifier,
59 uint16_t spn,
60 std::string signalName,
61 std::function<void()> checkFirmVersFunction
62 ) :
63 deviceIdentifier{std::move(deviceIdentifier)},
64 spn{spn},
65 name{std::move(signalName)},
66 _checkFirmVersFunction{std::move(checkFirmVersFunction)},
67 _unitsKey{spn},
69 {
70 }
71
73 hardware::DeviceIdentifier deviceIdentifier,
74 uint16_t spn,
75 std::string signalName,
76 std::function<void()> checkFirmVersFunction,
77 std::function<std::map<uint16_t, std::string>()> const &unitsGenerator
78 ) :
79 deviceIdentifier{std::move(deviceIdentifier)},
80 spn{spn},
81 name{std::move(signalName)},
82 _checkFirmVersFunction{std::move(checkFirmVersFunction)},
83 _unitStrings{unitsGenerator()},
84 _unitsKey{spn},
86 {
87 for (auto &unitString : _unitStrings) {
88 unitString.second = Status_GetUnits(unitString.first);
89 }
90 }
91
92 /* Constructor for an invalid BaseStatusSignal */
94 deviceIdentifier{hardware::DeviceIdentifier{}},
95 spn{0},
96 name{"Invalid"},
97 _checkFirmVersFunction{[] {}},
98 _unitsKey{spn},
99 error{error}
100 {
101 }
102
103 static std::string Status_GetUnits(uint32_t signal);
104
106 BaseStatusSignal &signal,
107 char const *network,
108 bool bWaitForUpdate,
109 double timeoutSeconds);
111 std::span<BaseStatusSignal* const> signals,
112 char const *network,
113 double timeoutSeconds);
114
116 BaseStatusSignal &signal,
117 double frequencyHz,
118 double timeoutSeconds);
120 std::span<BaseStatusSignal* const> signals,
121 double frequencyHz,
122 double timeoutSeconds);
123 static double Status_GetAppliedUpdateFrequency(char const *canbus, uint32_t deviceHash, uint16_t spn);
124
126 char const *location,
127 units::time::second_t timeoutSeconds,
128 std::span<BaseStatusSignal* const> signals);
129
130 void RefreshValue(bool waitForUpdate, units::time::second_t timeout, bool ReportOnError);
131 void UpdateUnits(uint16_t unitsKey);
132
133 public:
134 virtual ~BaseStatusSignal() = 0; // Declare virtual destructor to make this class abstract
135
136 /**
137 * \brief Gets the name of this signal.
138 *
139 * \returns Name of this signal
140 */
141 std::string const &GetName() const { return name; }
142 /**
143 * \brief Gets the units for this signal.
144 *
145 * \returns String representation of units for this signal
146 */
147 std::string const &GetUnits() const { return units; }
148 /**
149 * \brief Gets the value of this signal as a double.
150 *
151 * \return Value of this signal as a double instead of the generic type
152 */
153 double GetValueAsDouble() const { return baseValue; }
154 /**
155 * \brief Gets the timestamps of this signals.
156 *
157 * \returns All timestamps for this signal
158 */
159 AllTimestamps const &GetAllTimestamps() const { return timestamps; }
160 /**
161 * \brief Gets the most accurate timestamp available for this signal.
162 *
163 * \details The timestamp sources from most to least accurate are:
164 *
165 * - Timestamp#TimestampSource#Device
166 * - Timestamp#TimestampSource#CANivore
167 * - Timestamp#TimestampSource#System
168 *
169 * Note that some of these sources may not be available.
170 *
171 * \returns The most accurate timestamp available for this signal
172 */
173 Timestamp const &GetTimestamp() const { return timestamps.GetBestTimestamp(); }
174 /**
175 * \brief Gets the error code from when we last received this signal.
176 *
177 * \returns Last cached Error Code
178 */
179 ctre::phoenix::StatusCode GetStatus() const { return error; }
180
181 /**
182 * \brief Checks whether the signal has been updated since the last check.
183 *
184 * Note that the signal must be refreshed before calling this routine.
185 *
186 * \returns true if the signal has updated since the previous call of this routine
187 */
189 {
190 bool retval = false;
191 /* did we receive an update */
192 auto const &timestamp = GetAllTimestamps().GetSystemTimestamp();
193 if (timestamp.IsValid()) {
194 /* if the update timestamp is new, then a new frame was sent */
195 if (_lastTimestamp != timestamp.GetTime()) {
196 _lastTimestamp = timestamp.GetTime();
197 retval = true;
198 }
199 }
200 return retval;
201 }
202
203 /**
204 * \brief Sets the rate at which the device will publish this signal.
205 *
206 * A frequency of 0 Hz will turn off the signal. Otherwise, the minimum supported signal
207 * frequency is 4 Hz, and the maximum is 1000 Hz. Additionally, some update frequencies are
208 * not supported and will be promoted up to the next highest supported frequency.
209 *
210 * If other StatusSignals in the same status frame have been set to an update frequency,
211 * the fastest requested update frequency will be applied to the frame.
212 *
213 * \param frequencyHz Rate to publish the signal in Hz.
214 * \param timeoutSeconds Maximum amount of time to wait when performing the action
215 * \returns Status code of setting the update frequency
216 */
217 ctre::phoenix::StatusCode SetUpdateFrequency(units::frequency::hertz_t frequencyHz, units::time::second_t timeoutSeconds = 100_ms)
218 {
219 return Status_SetUpdateFrequency(
220 *this,
221 frequencyHz.to<double>(),
222 timeoutSeconds.to<double>()
223 );
224 }
225
226 /**
227 * \brief Gets the rate at which the device will publish this signal.
228 *
229 * This is typically the last value passed into #SetUpdateFrequency. The returned value
230 * may be higher if another StatusSignal in the same status frame has been set to a higher
231 * update frequency.
232 *
233 * \returns Applied update frequency of the signal in Hz
234 */
235 units::frequency::hertz_t GetAppliedUpdateFrequency() const
236 {
237 return units::frequency::hertz_t{
238 Status_GetAppliedUpdateFrequency(
239 this->deviceIdentifier.network.c_str(),
240 this->deviceIdentifier.deviceHash,
241 this->spn
242 )
243 };
244 }
245
246 /**
247 * \brief Performs latency compensation on signal using the signalSlope and signal's latency to determine
248 * the magnitude of compensation. The caller must refresh these StatusSignals beforehand;
249 * this function only does the math required for latency compensation.
250 *
251 * \details Example usage:
252 * \code
253 * units::turn_t compensatedTurns = BaseStatusSignal::GetLatencyCompensatedValue(fx.GetPosition(), fx.GetVelocity());
254 * \endcode
255 *
256 * \tparam U Type of signal's underlying type. This is the type that the function will return.
257 * \tparam U_PER_SEC Type of signalSlope's underlying type. This must be the derivative of U.
258 * \param signal Signal to be latency compensated. Caller must make sure this signal is up to date
259 * either by calling \c Refresh() or \c WaitForUpdate().
260 * \param signalSlope Derivative of signal that informs compensation magnitude. Caller must make sure this
261 * signal is up to date either by calling \c Refresh() or \c WaitForUpdate().
262 * \param maxLatencySeconds The maximum amount of latency to compensate for in seconds. A negative or zero
263 * value disables the max latency cap. This is used to cap the contribution of
264 * latency compensation for stale signals, such as after the device has been
265 * disconnected from the CAN bus.
266 * \returns Latency compensated value from the signal StatusSignal.
267 */
268 template <typename U, typename U_PER_SEC>
269 requires units::traits::is_unit_t_v<U> && units::traits::is_unit_t_v<U_PER_SEC> &&
270 units::traits::is_convertible_unit_v<
271 typename units::traits::unit_t_traits<U>::unit_type,
272 units::compound_unit<typename units::traits::unit_t_traits<U_PER_SEC>::unit_type, units::seconds>
273 >
274 static U GetLatencyCompensatedValue(StatusSignal<U> const &signal, StatusSignal<U_PER_SEC> const &signalSlope, units::time::second_t maxLatencySeconds = 0.300_s)
275 {
276 U const nonCompensatedSignal = signal.GetValue();
277 U_PER_SEC const changeInSignal = signalSlope.GetValue();
278 units::second_t latency = signal.GetTimestamp().GetLatency();
279 if (maxLatencySeconds > 0_s && latency > maxLatencySeconds) {
280 latency = maxLatencySeconds;
281 }
282 return nonCompensatedSignal + (changeInSignal * latency);
283 }
284
285 /**
286 * \brief Waits for new data on all provided signals up to timeout.
287 * This API is typically used with CANivore Bus signals as they will be synced using the
288 * CANivore Timesync feature and arrive simultaneously. Signals on a roboRIO bus cannot
289 * be synced and may require a significantly longer blocking call to receive all signals.
290 *
291 * Note that CANivore Timesync requires Phoenix Pro.
292 *
293 * This can also be used with a timeout of zero to refresh many signals at once, which
294 * is faster than calling Refresh() on every signal. This is equivalent to calling #RefreshAll.
295 *
296 * If a signal arrives multiple times while waiting, such as when *not* using CANivore
297 * Timesync, the newest signal data is fetched. Additionally, if this function times out,
298 * the newest signal data is fetched for all signals (when possible). We recommend checking
299 * the individual status codes using GetStatus() when this happens.
300 *
301 * \param timeoutSeconds Maximum time to wait for all the signals to arrive.
302 * Pass zero to refresh all signals without blocking.
303 * \param signals Signals to wait on, passed as a comma-separated list of signal references.
304 * \return An InvalidParamValue if signals array is empty,
305 * InvalidNetwork if signals are on different CAN bus networks,
306 * RxTimeout if it took longer than timeoutSeconds to receive all the signals,
307 * MultiSignalNotSupported if using the roboRIO bus with more than one signal and a non-zero timeout.
308 * An OK status code means that all signals arrived within timeoutSeconds and they are all OK.
309 *
310 * Any other value represents the StatusCode of the first failed signal.
311 * Call GetStatus() on each signal to determine which ones failed.
312 */
313 template <std::derived_from<BaseStatusSignal>... Signals>
314 static ctre::phoenix::StatusCode WaitForAll(units::time::second_t timeoutSeconds, Signals &... signals)
315 {
316 return WaitForAll(
317 timeoutSeconds,
318 std::array<BaseStatusSignal *, sizeof...(Signals)>{(&signals)...}
319 );
320 }
321 /**
322 * \brief Waits for new data on all provided signals up to timeout.
323 * This API is typically used with CANivore Bus signals as they will be synced using the
324 * CANivore Timesync feature and arrive simultaneously. Signals on a roboRIO bus cannot
325 * be synced and may require a significantly longer blocking call to receive all signals.
326 *
327 * Note that CANivore Timesync requires Phoenix Pro.
328 *
329 * This can also be used with a timeout of zero to refresh many signals at once, which
330 * is faster than calling Refresh() on every signal. This is equivalent to calling #RefreshAll.
331 *
332 * If a signal arrives multiple times while waiting, such as when *not* using CANivore
333 * Timesync, the newest signal data is fetched. Additionally, if this function times out,
334 * the newest signal data is fetched for all signals (when possible). We recommend checking
335 * the individual status codes using GetStatus() when this happens.
336 *
337 * \param timeoutSeconds Maximum time to wait for all the signals to arrive.
338 * Pass zero to refresh all signals without blocking.
339 * \param signals Signals to wait on, passed as a span of signal pointers.
340 * \return An InvalidParamValue if signals array is empty,
341 * InvalidNetwork if signals are on different CAN bus networks,
342 * RxTimeout if it took longer than timeoutSeconds to receive all the signals,
343 * MultiSignalNotSupported if using the roboRIO bus with more than one signal and a non-zero timeout.
344 * An OK status code means that all signals arrived within timeoutSeconds and they are all OK.
345 *
346 * Any other value represents the StatusCode of the first failed signal.
347 * Call GetStatus() on each signal to determine which ones failed.
348 */
349 static ctre::phoenix::StatusCode WaitForAll(units::time::second_t timeoutSeconds, std::span<BaseStatusSignal* const> signals)
350 {
351 /* static string for location */
352 constexpr char kLocation[] = "ctre::phoenix6::BaseStatusSignal::WaitForAll";
353 return WaitForAllImpl(kLocation, timeoutSeconds, signals);
354 }
355
356 /**
357 * \brief Performs a non-blocking refresh on all provided signals.
358 *
359 * This provides a performance improvement over separately calling Refresh() on each signal.
360 *
361 * \param signals Signals to refresh, passed as a comma-separated list of signal references.
362 * \return An InvalidParamValue if signals array is empty,
363 * InvalidNetwork if signals are on different CAN bus networks.
364 * An OK status code means that all signals are OK.
365 *
366 * Any other value represents the StatusCode of the first failed signal.
367 * Call GetStatus() on each signal to determine which ones failed.
368 */
369 template <std::derived_from<BaseStatusSignal>... Signals>
370 static ctre::phoenix::StatusCode RefreshAll(Signals &... signals)
371 {
372 return RefreshAll(std::array<BaseStatusSignal *, sizeof...(Signals)>{(&signals)...});
373 }
374 /**
375 * \brief Performs a non-blocking refresh on all provided signals.
376 *
377 * This provides a performance improvement over separately calling Refresh() on each signal.
378 *
379 * \param signals Signals to refresh, passed as a span of signal pointers.
380 * \return An InvalidParamValue if signals array is empty,
381 * InvalidNetwork if signals are on different CAN bus networks.
382 * An OK status code means that all signals are OK.
383 *
384 * Any other value represents the StatusCode of the first failed signal.
385 * Call GetStatus() on each signal to determine which ones failed.
386 */
387 static ctre::phoenix::StatusCode RefreshAll(std::span<BaseStatusSignal* const> signals)
388 {
389 /* static string for location */
390 constexpr char kLocation[] = "ctre::phoenix6::BaseStatusSignal::RefreshAll";
391 return WaitForAllImpl(kLocation, 0_s, signals);
392 }
393
394 /**
395 * \brief Checks if all signals have an OK error code.
396 *
397 * \param signals Signals to check error code of, passed as a comma-separated list of signal references.
398 * \returns True if all signals are OK, false otherwise
399 */
400 template <std::derived_from<BaseStatusSignal>... Signals>
401 static bool IsAllGood(Signals const &... signals)
402 {
403 return IsAllGood(std::array<BaseStatusSignal const *, sizeof...(Signals)>{(&signals)...});
404 }
405 /**
406 * \brief Checks if all signals have an OK error code.
407 *
408 * \param signals Signals to check error code of, passed as a span of signal pointers.
409 * \returns True if all signals are OK, false otherwise
410 */
411 static bool IsAllGood(std::span<BaseStatusSignal const *const> signals)
412 {
413 for (auto signal : signals) {
414 if (!signal->GetStatus().IsOK()) {
415 return false;
416 }
417 }
418 return true;
419 }
420
421 /**
422 * \brief Sets the update frequency of all specified status signals to the provided common frequency.
423 *
424 * A frequency of 0 Hz will turn off the signal. Otherwise, the minimum supported signal frequency
425 * is 4 Hz, and the maximum is 1000 Hz. Additionally, some update frequencies are not supported and
426 * will be promoted up to the next highest supported frequency.
427 *
428 * If other StatusSignals in the same status frame have been set to an update frequency,
429 * the fastest requested update frequency will be applied to the frame.
430 *
431 * This will wait up to 0.100 seconds (100ms) for each signal.
432 *
433 * \param frequencyHz Rate to publish the signal in Hz.
434 * \param signals Signals to apply the update frequency to, passed as a comma-separated list of signal references.
435 * \returns Status code of the first failed update frequency set call, or OK if all succeeded
436 */
437 template <std::derived_from<BaseStatusSignal>... Signals>
438 static ctre::phoenix::StatusCode SetUpdateFrequencyForAll(units::frequency::hertz_t frequencyHz, Signals &... signals)
439 {
440 return SetUpdateFrequencyForAll(frequencyHz, std::array<BaseStatusSignal *, sizeof...(Signals)>{(&signals)...});
441 }
442 /**
443 * \brief Sets the update frequency of all specified status signals to the provided common frequency.
444 *
445 * A frequency of 0 Hz will turn off the signal. Otherwise, the minimum supported signal frequency
446 * is 4 Hz, and the maximum is 1000 Hz. Additionally, some update frequencies are not supported and
447 * will be promoted up to the next highest supported frequency.
448 *
449 * If other StatusSignals in the same status frame have been set to an update frequency,
450 * the fastest requested update frequency will be applied to the frame.
451 *
452 * This will wait up to 0.100 seconds (100ms) for each signal.
453 *
454 * \param frequencyHz Rate to publish the signal in Hz.
455 * \param signals Signals to apply the update frequency to, passed as a span of signal pointers.
456 * \returns Status code of the first failed update frequency set call, or OK if all succeeded
457 */
458 static ctre::phoenix::StatusCode SetUpdateFrequencyForAll(units::frequency::hertz_t frequencyHz, std::span<BaseStatusSignal* const> signals)
459 {
460 return Status_SetUpdateFrequencyForAll(signals, frequencyHz.to<double>(), 0.100);
461 }
462 };
463
464 /**
465 * \brief Represents a status signal with data of type T,
466 * and operations available to retrieve information about
467 * the signal.
468 */
469 template <typename T>
472
474 hardware::DeviceIdentifier deviceIdentifier,
475 uint16_t spn,
476 std::string signalName,
477 std::function<void()> checkFirmVersFunction
478 ) :
480 std::move(deviceIdentifier),
481 spn, std::move(signalName),
482 std::move(checkFirmVersFunction)
483 }
484 {
485 }
486
488 hardware::DeviceIdentifier deviceIdentifier,
489 uint16_t spn,
490 std::string signalName,
491 std::function<void()> checkFirmVersFunction,
492 std::function<std::map<uint16_t, std::string>()> const &unitsGenerator
493 ) :
495 std::move(deviceIdentifier),
496 spn, std::move(signalName),
497 std::move(checkFirmVersFunction),
498 unitsGenerator
499 }
500 {
501 }
502
503 /* Constructor for an invalid StatusSignal */
504 StatusSignal(ctre::phoenix::StatusCode error) :
505 BaseStatusSignal{error}
506 {
507 }
508
509 public:
510 /**
511 * \brief Gets the cached value from this status signal.
512 *
513 * \details Gets the cached value. To make sure the value is up-to-date
514 * call \c Refresh() or \c WaitForUpdate()
515 *
516 * \returns Cached value
517 */
518 T GetValue() const
519 {
520 if constexpr(units::traits::is_unit_t_v<T>) {
521 return units::make_unit<T>(this->baseValue);
522 } else {
523 return static_cast<T>(this->baseValue);
524 }
525 }
526
527 /**
528 * \brief Refreshes the value of this status signal.
529 *
530 * If the user application caches this StatusSignal object
531 * instead of periodically fetching it from the hardware device,
532 * this function must be called to fetch fresh data.
533 *
534 * \details This performs a non-blocking refresh operation. If
535 * you want to wait until you receive the signal, call
536 * \c WaitForUpdate() instead.
537 *
538 * \param ReportOnError Whether to report any errors to the Driver Station/stderr.
539 *
540 * \returns Reference to itself
541 */
542 StatusSignal<T> &Refresh(bool ReportOnError = true)
543 {
544 RefreshValue(false, 0_s, ReportOnError); // Don't block and error if signal is older than a default timeout
545 return *this;
546 }
547 /**
548 * \brief Waits up to timeoutSec to get the up-to-date status signal value.
549 *
550 * \details This performs a blocking refresh operation. If
551 * you want to non-blocking refresh the signal, call
552 * \c Refresh() instead.
553 *
554 * \param timeoutSec Maximum time to wait for the signal to update
555 * \param ReportOnError Whether to report any errors to the Driver Station/stderr.
556 *
557 * \returns Reference to itself
558 */
559 StatusSignal<T> &WaitForUpdate(units::time::second_t timeoutSec, bool ReportOnError = true)
560 {
561 RefreshValue(true, timeoutSec, ReportOnError);
562 return *this;
563 }
564
565 /**
566 * \brief Checks whether the signal is near a target value within the
567 * given tolerance.
568 *
569 * \param target The target value of the signal
570 * \param tolerance The error tolerance between the target and measured values
571 * \returns Whether the signal is near the target value
572 */
573 bool IsNear(T target, T tolerance) const
574 requires (std::same_as<T, double>)
575 {
576 return fabs(GetValue() - target) <= tolerance;
577 }
578
579 /**
580 * \brief Checks whether the signal is near a target value within the
581 * given tolerance.
582 *
583 * \param target The target value of the signal
584 * \param tolerance The error tolerance between the target and measured values
585 * \returns Whether the signal is near the target value
586 */
587 bool IsNear(T target, T tolerance) const
588 requires (units::traits::is_unit_t_v<T>)
589 {
590 return units::math::abs(GetValue() - target) <= tolerance;
591 }
592
593 /**
594 * \brief Get a basic data-only container with a copy of the current signal data.
595 *
596 * If looking for Phoenix 6 logging features, see the SignalLogger class instead.
597 *
598 * \returns Basic structure with all relevant information
599 */
601 {
603 toRet.name = GetName();
604 toRet.value = GetValue();
605 toRet.timestamp = GetTimestamp().GetTime();
606 toRet.units = GetUnits();
607 toRet.status = GetStatus();
608 return toRet;
609 }
610
611 /**
612 * \brief Returns a lambda that calls #Refresh and #GetValue on this object. This is useful for command-based programming.
613 *
614 * \returns std::function<T()> that calls #Refresh and returns this signal's value.
615 */
616 std::function<T()> AsSupplier()
617 {
618 return [this]() { return Refresh().GetValue(); };
619 }
620
621 friend std::ostream &operator<<(std::ostream &os, StatusSignal<T> const &data)
622 {
623 /* Units may contain UTF-8 characters */
625
626 if constexpr(units::traits::is_unit_t_v<T>) {
627 os << data.GetValue().value() << " " << data.GetUnits();
628 } else {
629 os << data.GetValue() << " " << data.GetUnits();
630 }
631 return os;
632 }
633 std::string ToString() const
634 {
635 std::stringstream ss;
636 ss << *this;
637 return ss.str();
638 }
639 };
640
641}
642}
A collection of timestamps for a received signal.
Definition Timestamp.hpp:125
Class that provides operations to retrieve information about a status signal.
Definition StatusSignal.hpp:39
AllTimestamps timestamps
Definition StatusSignal.hpp:54
AllTimestamps const & GetAllTimestamps() const
Gets the timestamps of this signals.
Definition StatusSignal.hpp:159
ctre::phoenix::StatusCode error
Definition StatusSignal.hpp:55
static std::string Status_GetUnits(uint32_t signal)
static double Status_GetAppliedUpdateFrequency(char const *canbus, uint32_t deviceHash, uint16_t spn)
static ctre::phoenix::StatusCode RefreshAll(std::span< BaseStatusSignal *const > signals)
Performs a non-blocking refresh on all provided signals.
Definition StatusSignal.hpp:387
std::string units
Definition StatusSignal.hpp:52
static ctre::phoenix::StatusCode SetUpdateFrequencyForAll(units::frequency::hertz_t frequencyHz, std::span< BaseStatusSignal *const > signals)
Sets the update frequency of all specified status signals to the provided common frequency.
Definition StatusSignal.hpp:458
std::string const & GetName() const
Gets the name of this signal.
Definition StatusSignal.hpp:141
BaseStatusSignal(hardware::DeviceIdentifier deviceIdentifier, uint16_t spn, std::string signalName, std::function< void()> checkFirmVersFunction, std::function< std::map< uint16_t, std::string >()> const &unitsGenerator)
Definition StatusSignal.hpp:72
ctre::phoenix::StatusCode SetUpdateFrequency(units::frequency::hertz_t frequencyHz, units::time::second_t timeoutSeconds=100_ms)
Sets the rate at which the device will publish this signal.
Definition StatusSignal.hpp:217
ctre::phoenix::StatusCode GetStatus() const
Gets the error code from when we last received this signal.
Definition StatusSignal.hpp:179
void RefreshValue(bool waitForUpdate, units::time::second_t timeout, bool ReportOnError)
static ctre::phoenix::StatusCode WaitForAll(units::time::second_t timeoutSeconds, std::span< BaseStatusSignal *const > signals)
Waits for new data on all provided signals up to timeout.
Definition StatusSignal.hpp:349
static ctre::phoenix::StatusCode WaitForAll(units::time::second_t timeoutSeconds, Signals &... signals)
Waits for new data on all provided signals up to timeout.
Definition StatusSignal.hpp:314
static ctre::phoenix::StatusCode Status_Get(BaseStatusSignal &signal, char const *network, bool bWaitForUpdate, double timeoutSeconds)
static bool IsAllGood(Signals const &... signals)
Checks if all signals have an OK error code.
Definition StatusSignal.hpp:401
bool HasUpdated()
Checks whether the signal has been updated since the last check.
Definition StatusSignal.hpp:188
BaseStatusSignal(ctre::phoenix::StatusCode error)
Definition StatusSignal.hpp:93
units::frequency::hertz_t GetAppliedUpdateFrequency() const
Gets the rate at which the device will publish this signal.
Definition StatusSignal.hpp:235
double baseValue
Definition StatusSignal.hpp:53
std::string const & GetUnits() const
Gets the units for this signal.
Definition StatusSignal.hpp:147
static U GetLatencyCompensatedValue(StatusSignal< U > const &signal, StatusSignal< U_PER_SEC > const &signalSlope, units::time::second_t maxLatencySeconds=0.300_s)
Performs latency compensation on signal using the signalSlope and signal's latency to determine the m...
Definition StatusSignal.hpp:274
static ctre::phoenix::StatusCode Status_WaitForAll(std::span< BaseStatusSignal *const > signals, char const *network, double timeoutSeconds)
static ctre::phoenix::StatusCode RefreshAll(Signals &... signals)
Performs a non-blocking refresh on all provided signals.
Definition StatusSignal.hpp:370
static ctre::phoenix::StatusCode Status_SetUpdateFrequency(BaseStatusSignal &signal, double frequencyHz, double timeoutSeconds)
static ctre::phoenix::StatusCode SetUpdateFrequencyForAll(units::frequency::hertz_t frequencyHz, Signals &... signals)
Sets the update frequency of all specified status signals to the provided common frequency.
Definition StatusSignal.hpp:438
double GetValueAsDouble() const
Gets the value of this signal as a double.
Definition StatusSignal.hpp:153
static bool IsAllGood(std::span< BaseStatusSignal const *const > signals)
Checks if all signals have an OK error code.
Definition StatusSignal.hpp:411
Timestamp const & GetTimestamp() const
Gets the most accurate timestamp available for this signal.
Definition StatusSignal.hpp:173
BaseStatusSignal(hardware::DeviceIdentifier deviceIdentifier, uint16_t spn, std::string signalName, std::function< void()> checkFirmVersFunction)
Definition StatusSignal.hpp:57
void UpdateUnits(uint16_t unitsKey)
static ctre::phoenix::StatusCode Status_SetUpdateFrequencyForAll(std::span< BaseStatusSignal *const > signals, double frequencyHz, double timeoutSeconds)
static ctre::phoenix::StatusCode WaitForAllImpl(char const *location, units::time::second_t timeoutSeconds, std::span< BaseStatusSignal *const > signals)
Represents a status signal with data of type T, and operations available to retrieve information abou...
Definition StatusSignal.hpp:470
std::string ToString() const
Definition StatusSignal.hpp:633
T GetValue() const
Gets the cached value from this status signal.
Definition StatusSignal.hpp:518
friend std::ostream & operator<<(std::ostream &os, StatusSignal< T > const &data)
Definition StatusSignal.hpp:621
StatusSignal< T > & Refresh(bool ReportOnError=true)
Refreshes the value of this status signal.
Definition StatusSignal.hpp:542
SignalMeasurement< T > GetDataCopy() const
Get a basic data-only container with a copy of the current signal data.
Definition StatusSignal.hpp:600
bool IsNear(T target, T tolerance) const
Checks whether the signal is near a target value within the given tolerance.
Definition StatusSignal.hpp:587
std::function< T()> AsSupplier()
Returns a lambda that calls Refresh and GetValue on this object.
Definition StatusSignal.hpp:616
StatusSignal< T > & WaitForUpdate(units::time::second_t timeoutSec, bool ReportOnError=true)
Waits up to timeoutSec to get the up-to-date status signal value.
Definition StatusSignal.hpp:559
bool IsNear(T target, T tolerance) const
Checks whether the signal is near a target value within the given tolerance.
Definition StatusSignal.hpp:573
Information about the timestamp of a signal.
Definition Timestamp.hpp:17
units::time::second_t GetLatency() const
Get the latency of this timestamp compared to now.
Definition Timestamp.hpp:107
Definition DeviceIdentifier.hpp:16
std::string network
Definition DeviceIdentifier.hpp:18
Parent class for all devices.
Definition ParentDevice.hpp:23
Status codes reported by APIs, including OK, warnings, and errors.
Definition StatusCodes.h:28
static constexpr int SigNotUpdated
No new response to update signal.
Definition StatusCodes.h:419
CTREXPORT void EnableConsoleUTF8Output()
Enables UTF-8 console output.
Definition motor_constants.h:14
Information from a single measurement of a status signal.
Definition SignalMeasurement.hpp:20
T value
The value of the signal.
Definition SignalMeasurement.hpp:28
std::string units
The units of the signal measurement.
Definition SignalMeasurement.hpp:36
ctre::phoenix::StatusCode status
Status code response of getting the data.
Definition SignalMeasurement.hpp:40
units::time::second_t timestamp
Timestamp of when the data point was taken.
Definition SignalMeasurement.hpp:32
std::string_view name
The name of the signal.
Definition SignalMeasurement.hpp:24