17#include <initializer_list>
22#include <units/base.h>
23#include <units/frequency.h>
24#include <units/time.h>
64 std::function<
void()> checkFirmVersFunction
99 double timeoutSeconds);
126 double timeoutSeconds);
137 template <
typename Arr>
140 if (signals.size() < 1)
150 const std::string &network = signals[0]->deviceIdentifier.network;
152 for (
auto signal : signals)
155 if (network != signal->deviceIdentifier.network)
168 for (
auto signal : signals)
170 signal->_checkFirmVersFunction();
174 ctre::phoenix::StatusCode retval = Status_WaitForAll(signals.data(), signals.size(), network.c_str(), timeoutSeconds.to<
double>());
177 if (
false == retval.
IsOK())
190 template <
typename... Signals>
192 std::conjunction<std::is_base_of<BaseStatusSignal, std::remove_reference_t<Signals>>...>
198 template <
typename... Signals>
232 template <
typename... Signals,
typename = std::enable_if_t<is_all_status_signal_v<Signals...>>>
235 return WaitForAll(timeoutSeconds,
269 constexpr char kLocation[] =
"ctre::phoenix6::BaseStatusSignal::WaitForAll";
270 return WaitForAllImpl(kLocation, timeoutSeconds, signals);
304 constexpr char kLocation[] =
"ctre::phoenix6::BaseStatusSignal::WaitForAll";
305 return WaitForAllImpl(kLocation, timeoutSeconds, signals);
321 template <
typename... Signals,
typename = std::enable_if_t<is_all_status_signal_v<Signals...>>>
324 return RefreshAll(std::array<
BaseStatusSignal *,
sizeof...(Signals)>{(&signals)...});
342 constexpr char kLocation[] =
"ctre::phoenix6::BaseStatusSignal::RefreshAll";
343 return WaitForAllImpl(kLocation, 0_s, signals);
362 constexpr char kLocation[] =
"ctre::phoenix6::BaseStatusSignal::RefreshAll";
363 return WaitForAllImpl(kLocation, 0_s, signals);
388 template <
typename U,
typename U_PER_SEC>
391 static_assert(units::traits::is_unit_t_v<U>,
"signal must be a unit signal");
392 static_assert(units::traits::is_unit_t_v<U_PER_SEC>,
"signalSlope must be a unit signal");
393 using lhs =
typename units::traits::unit_t_traits<U>::unit_type;
394 using rhs =
typename units::traits::unit_t_traits<U_PER_SEC>::unit_type;
395 static_assert(units::traits::is_convertible_unit_v<lhs, units::compound_unit<rhs, units::seconds>>,
396 "Compensation can only be performed on a signal with its derivative");
398 const U nonCompensatedSignal = signal.
GetValue();
399 const U_PER_SEC changeInSignal = signalSlope.
GetValue();
401 if (maxLatencySeconds > 0_s && latency > maxLatencySeconds) {
402 latency = maxLatencySeconds;
404 return nonCompensatedSignal + (changeInSignal * latency);
413 template <
typename... Signals,
typename = std::enable_if_t<is_all_status_signal_v<Signals...>>>
416 return IsAllGood(std::array<
BaseStatusSignal *,
sizeof...(Signals)>{(&signals)...});
424 static bool IsAllGood(
const std::vector<BaseStatusSignal *> &signals)
426 for (
auto signal : signals)
428 if (!signal->GetStatus().IsOK()) {
441 static bool IsAllGood(
const std::array<BaseStatusSignal *, N> &signals)
443 for (
auto signal : signals)
445 if (!signal->GetStatus().IsOK()) {
467 template <
typename... Signals,
typename = std::enable_if_t<is_all_status_signal_v<Signals...>>>
470 return SetUpdateFrequencyForAll(frequencyHz, std::array<
BaseStatusSignal *,
sizeof...(Signals)>{(&signals)...});
489 return Status_SetUpdateFrequencyForAll(signals.data(), signals.size(), frequencyHz.to<
double>(), 0.100);
509 return Status_SetUpdateFrequencyForAll(signals.data(), signals.size(), frequencyHz.to<
double>(), 0.100);
527 return Status_SetUpdateFrequency(
528 this->deviceIdentifier.
network.c_str(),
529 this->deviceIdentifier.deviceHash,
531 frequencyHz.to<
double>(),
532 timeoutSeconds.to<
double>()
547 return units::frequency::hertz_t{
548 Status_GetAppliedUpdateFrequency(
549 this->deviceIdentifier.
network.c_str(),
550 this->deviceIdentifier.deviceHash,
561 const std::string &
GetName()
const {
return signalName; }
567 const std::string &
GetUnits()
const {
return units; }
612 auto const ×tamp = GetAllTimestamps().GetSystemTimestamp();
613 if (timestamp.IsValid())
616 if (_lastTimestamp != timestamp.GetTime())
618 _lastTimestamp = timestamp.GetTime();
629 template <
typename L>
655 template <
typename T>
658 std::map<uint16_t, std::string> _unitStrings;
661 void RefreshValue(
bool block, units::time::second_t timeout,
bool ReportOnError)
663 _checkFirmVersFunction();
665 this->error = Status_Get(
666 *
this, this->deviceIdentifier.network.c_str(),
667 block, timeout.to<
double>()
670 if (ReportOnError && !(this->error.IsOK()))
672 std::stringstream location;
673 location << this->deviceIdentifier.ToString() <<
" Status Signal " << this->signalName;
683 std::function<
void()> checkFirmVersFunction,
684 std::string signalName
695 std::function<
void()> checkFirmVersFunction,
696 std::function<std::map<uint16_t, std::string>()> mapFiller,
697 std::string signalName
700 _unitStrings{mapFiller()},
703 for (
auto &unitString : _unitStrings) {
704 unitString.second = Status_GetUnits(unitString.first);
710 BaseStatusSignal{error},
719 if (_unitsKey != unitsKey) {
720 auto const it = _unitStrings.find(unitsKey);
721 if (it != _unitStrings.end()) {
723 _unitsKey = unitsKey;
734 if constexpr(units::traits::is_unit_t_v<T>)
736 os << data.GetValue().value() <<
" " << data.GetUnits();
740 os << data.GetValue() <<
" " << data.GetUnits();
746 std::stringstream ss;
761 if constexpr(units::traits::is_unit_t_v<T>)
763 return units::make_unit<T>(this->baseValue);
767 return static_cast<T
>(this->baseValue);
788 RefreshValue(
false, 0_s, ReportOnError);
805 RefreshValue(
true, timeoutSec, ReportOnError);
820 toRet.
value = GetValue();
821 toRet.
status = GetStatus();
822 toRet.
units = GetUnits();
823 toRet.
timestamp = GetTimestamp().GetTime();
834 return [
this]() {
return Refresh().GetValue(); };
CTREXPORT void c_ctre_phoenix_report_error(int isError, int32_t errorCode, int isLVCode, const char *details, const char *location, const char *callStack)
A collection of timestamps for a received signal.
Definition Timestamp.hpp:133
Class that provides operations to retrieve information about a status signal.
Definition StatusSignal.hpp:41
std::string signalName
Definition StatusSignal.hpp:51
units::time::second_t _lastTimestamp
Definition StatusSignal.hpp:55
const AllTimestamps & GetAllTimestamps() const
Get the timestamps of this signals.
Definition StatusSignal.hpp:579
AllTimestamps timestamps
Definition StatusSignal.hpp:48
ctre::phoenix::StatusCode error
Definition StatusSignal.hpp:50
static ctre::phoenix::StatusCode WaitForAll(units::time::second_t timeoutSeconds, const std::vector< BaseStatusSignal * > &signals)
Waits for new data on all provided signals up to timeout.
Definition StatusSignal.hpp:266
static ctre::phoenix::StatusCode Status_WaitForAll(BaseStatusSignal *const *signals, size_t count, const char *network, double timeoutSeconds)
Wait for multiple signals to arrive.
uint16_t spn
Definition StatusSignal.hpp:46
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:468
static std::string Status_GetUnits(uint32_t signal)
Gets signal units.
static bool IsAllGood(Signals &... signals)
Checks if all signals have an OK error code.
Definition StatusSignal.hpp:414
static ctre::phoenix::StatusCode RefreshAll(const std::array< BaseStatusSignal *, N > &signals)
Performs a non-blocking refresh on all provided signals.
Definition StatusSignal.hpp:359
static ctre::phoenix::StatusCode RefreshAll(Signals &... signals)
Performs a non-blocking refresh on all provided signals.
Definition StatusSignal.hpp:322
std::string units
Definition StatusSignal.hpp:47
static bool IsAllGood(const std::vector< BaseStatusSignal * > &signals)
Checks if all signals have an OK error code.
Definition StatusSignal.hpp:424
static double Status_GetAppliedUpdateFrequency(const char *canbus, uint32_t deviceHash, uint16_t spn)
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:525
ctre::phoenix::StatusCode GetStatus() const
Get the error code from when we last received this signal.
Definition StatusSignal.hpp:599
static ctre::phoenix::StatusCode WaitForAllImpl(const char *location, units::time::second_t timeoutSeconds, const Arr &signals)
Implementation of the WaitForAll API.
Definition StatusSignal.hpp:138
static ctre::phoenix::StatusCode SetUpdateFrequencyForAll(units::frequency::hertz_t frequencyHz, const std::array< BaseStatusSignal *, N > &signals)
Sets the update frequency of all specified status signals to the provided common frequency.
Definition StatusSignal.hpp:507
static U GetLatencyCompensatedValue(StatusSignal< U > &signal, StatusSignal< U_PER_SEC > &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:389
virtual void UpdateUnits(uint16_t spn)=0
std::function< void()> _checkFirmVersFunction
Definition StatusSignal.hpp:53
bool HasUpdated()
Check whether the signal has been updated since the last check.
Definition StatusSignal.hpp:608
static ctre::phoenix::StatusCode SetUpdateFrequencyForAll(units::frequency::hertz_t frequencyHz, const std::vector< BaseStatusSignal * > &signals)
Sets the update frequency of all specified status signals to the provided common frequency.
Definition StatusSignal.hpp:487
BaseStatusSignal(ctre::phoenix::StatusCode error)
Definition StatusSignal.hpp:75
const std::string & GetUnits() const
Gets the units for this signal.
Definition StatusSignal.hpp:567
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:233
static ctre::phoenix::StatusCode Status_SetUpdateFrequency(const char *canbus, uint32_t deviceHash, uint16_t spn, double frequencyHz, double timeoutSeconds)
units::frequency::hertz_t GetAppliedUpdateFrequency() const
Gets the rate at which the device will publish this signal.
Definition StatusSignal.hpp:545
static bool IsAllGood(const std::array< BaseStatusSignal *, N > &signals)
Checks if all signals have an OK error code.
Definition StatusSignal.hpp:441
double baseValue
Definition StatusSignal.hpp:49
const std::string & GetName() const
Gets the name of this signal.
Definition StatusSignal.hpp:561
virtual ~BaseStatusSignal()=0
double GetValueAsDouble() const
Gets the value of this signal as a double.
Definition StatusSignal.hpp:573
hardware::DeviceIdentifier deviceIdentifier
Definition StatusSignal.hpp:45
static ctre::phoenix::StatusCode Status_SetUpdateFrequencyForAll(BaseStatusSignal *const *signals, size_t count, double frequencyHz, double timeoutSeconds)
static ctre::phoenix::StatusCode WaitForAll(units::time::second_t timeoutSeconds, const std::array< BaseStatusSignal *, N > &signals)
Waits for new data on all provided signals up to timeout.
Definition StatusSignal.hpp:301
const Timestamp & GetTimestamp() const
Get the most accurate timestamp available for this signal.
Definition StatusSignal.hpp:593
BaseStatusSignal(hardware::DeviceIdentifier deviceIdentifier, uint16_t spn, std::string signalName, std::function< void()> checkFirmVersFunction)
Definition StatusSignal.hpp:60
static ctre::phoenix::StatusCode RefreshAll(const std::vector< BaseStatusSignal * > &signals)
Performs a non-blocking refresh on all provided signals.
Definition StatusSignal.hpp:339
static ctre::phoenix::StatusCode Status_Get(BaseStatusSignal &signal, const char *network, bool bWaitForUpdate, double timeoutSeconds)
Get signal update.
Represents a status signal with data of type T, and operations available to retrieve information abou...
Definition StatusSignal.hpp:657
friend std::ostream & operator<<(std::ostream &os, const StatusSignal< T > &data)
Definition StatusSignal.hpp:729
std::string ToString() const
Definition StatusSignal.hpp:744
T GetValue() const
Gets the cached value from this status signal.
Definition StatusSignal.hpp:759
void UpdateUnits(uint16_t unitsKey) override
Definition StatusSignal.hpp:717
StatusSignal< T > & Refresh(bool ReportOnError=true)
Refreshes the value of this status signal.
Definition StatusSignal.hpp:786
SignalMeasurement< T > GetDataCopy() const
Get a basic data-only container with this information, to be used for things such as data logging.
Definition StatusSignal.hpp:817
std::function< T()> AsSupplier()
Returns a lambda that calls Refresh and GetValue on this object.
Definition StatusSignal.hpp:832
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:803
Information about the timestamp of a signal.
Definition Timestamp.hpp:20
units::time::second_t GetLatency() const
Get the latency of this timestamp compared to now.
Definition Timestamp.hpp:115
Definition DeviceIdentifier.hpp:19
std::string network
Definition DeviceIdentifier.hpp:21
Parent class for all devices.
Definition ParentDevice.hpp:29
Status codes reported by APIs, including OK, warnings, and errors.
Definition StatusCodes.h:27
constexpr const char * GetDescription() const
Gets the description of this StatusCode.
Definition StatusCodes.h:1066
static constexpr int InvalidParamValue
An invalid argument was passed into the function/VI, such as a null pointer.
Definition StatusCodes.h:368
constexpr bool IsOK() const
Definition StatusCodes.h:859
constexpr bool IsError() const
Definition StatusCodes.h:851
static constexpr int InvalidNetwork
InvalidNetwork.
Definition StatusCodes.h:636
static constexpr int SigNotUpdated
No new response to update signal.
Definition StatusCodes.h:418
Definition StatusCodes.h:18
Type trait to verify that all types passed in are subclasses of BaseStatusSignal.
Definition StatusSignal.hpp:193
Information from a single measurement of a status signal.
Definition StatusSignal.hpp:631
std::string units
Units that correspond to this point.
Definition StatusSignal.hpp:647
units::time::second_t timestamp
Timestamp of when the data point was taken.
Definition StatusSignal.hpp:639
L value
The value of the signal.
Definition StatusSignal.hpp:635
ctre::phoenix::StatusCode status
Status code response of getting the data.
Definition StatusSignal.hpp:643