CTRE Phoenix 6 C++ 26.50.0-alpha-1
Loading...
Searching...
No Matches
StatusSignalCollection.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 <vector>
11
12namespace ctre {
13namespace phoenix6 {
14
15 /**
16 * \brief Class to manage bulk refreshing device status signals.
17 *
18 * This class does not own copies of the provided status signals. As a result,
19 * the usage of this class cannot outlive the lifetime of the status signals.
20 */
22 protected:
23 /** Signals stored by this collection */
24 std::vector<BaseStatusSignal *> _signals;
25
26 public:
27 /**
28 * \brief Creates a new collection of status signals, optionally
29 * adding the provided signals to the collection.
30 *
31 * This collection does not own copies of the provided status signals.
32 * As a result, the usage of this collection cannot outlive the lifetime
33 * of the status signals.
34 *
35 * \param signals Signals to add, passed as a comma-separated list of signal references
36 */
37 template <std::derived_from<BaseStatusSignal>... Signals>
39 _signals{(&signals)...}
40 {}
41 /**
42 * \brief Creates a new collection of status signals, adding the
43 * provided span of signals to the collection.
44 *
45 * This collection does not own copies of the provided status signals.
46 * As a result, the usage of this collection cannot outlive the lifetime
47 * of the status signals.
48 *
49 * \param signals Signals to add, passed as a span of signal pointers
50 */
51 StatusSignalCollection(std::span<BaseStatusSignal *const> signals) :
52 _signals(signals.begin(), signals.end())
53 {}
54
55 /**
56 * \brief Creates a new collection of status signals, reserving
57 * capacity for the specified number of signals.
58 *
59 * \param signalCount Number of signals to reserve space for
60 */
61 StatusSignalCollection(size_t signalCount)
62 {
63 _signals.reserve(signalCount);
64 }
65
66 /**
67 * \brief Adds the provided signals to the collection.
68 *
69 * This collection does not own copies of the provided status signals.
70 * As a result, the usage of this collection cannot outlive the lifetime
71 * of the status signals.
72 *
73 * \param signals Signals to add, passed as a comma-separated list of signal references
74 */
75 template <std::derived_from<BaseStatusSignal>... Signals>
76 void AddSignals(Signals &... signals)
77 {
78 return AddSignals(std::array<BaseStatusSignal *, sizeof...(Signals)>{(&signals)...});
79 }
80
81 /**
82 * \brief Adds the provided signals to the collection.
83 *
84 * This collection does not own copies of the provided status signals.
85 * As a result, the usage of this collection cannot outlive the lifetime
86 * of the status signals.
87 *
88 * \param signals Signals to add, passed as a span of signal pointers
89 */
90 void AddSignals(std::span<BaseStatusSignal *const> signals)
91 {
92 _signals.insert(_signals.end(), signals.begin(), signals.end());
93 }
94
95 /**
96 * \brief Waits for new data on all signals up to timeout.
97 * This API is typically used with CANivore Bus signals as they will be synced using the
98 * CANivore Timesync feature and arrive simultaneously. Signals on a non-CANivore bus cannot
99 * be synced and may require a significantly longer blocking call to receive all signals.
100 *
101 * Note that CANivore Timesync requires Phoenix Pro.
102 *
103 * This can also be used with a timeout of zero to refresh many signals at once, which
104 * is faster than calling Refresh() on every signal. This is equivalent to calling #RefreshAll.
105 *
106 * If a signal arrives multiple times while waiting, such as when *not* using CANivore
107 * Timesync, the newest signal data is fetched. Additionally, if this function times out,
108 * the newest signal data is fetched for all signals (when possible). We recommend checking
109 * the individual status codes using BaseStatusSignal#GetStatus() when this happens.
110 *
111 * \param timeout Maximum time to wait for all the signals to arrive.
112 * Pass zero to refresh all signals without blocking.
113 * \param reportError Whether to report any errors to the Driver Station/stderr
114 * \return InvalidNetwork if signals are on different CAN bus networks,
115 * RxTimeout if it took longer than timeoutSeconds to receive all the signals,
116 * An OK status code means that all signals arrived within timeoutSeconds and they are all OK.
117 *
118 * Any other value represents the StatusCode of the first failed signal.
119 * Call GetStatus() on each signal to determine which ones failed.
120 */
121 ctre::phoenix::StatusCode WaitForAll(wpi::units::second_t timeout, bool reportError = true)
122 {
123 return BaseStatusSignal::WaitForAll(timeout, reportError, _signals);
124 }
125
126 /**
127 * \brief Performs a non-blocking refresh on all signals.
128 *
129 * This provides a performance improvement over separately calling Refresh() on each signal.
130 *
131 * \param reportError Whether to report any errors to the Driver Station/stderr
132 * \return InvalidNetwork if signals are on different CAN bus networks.
133 * An OK status code means that all signals are OK.
134 *
135 * Any other value represents the StatusCode of the first failed signal.
136 * Call GetStatus() on each signal to determine which ones failed.
137 */
138 ctre::phoenix::StatusCode RefreshAll(bool reportError = true)
139 {
140 return BaseStatusSignal::RefreshAll(reportError, _signals);
141 }
142
143 /**
144 * \brief Sets the update frequency of all status signals to the provided common frequency.
145 *
146 * A frequency of 0 Hz will turn off the signal. Otherwise, the minimum supported signal frequency
147 * is 4 Hz, and the maximum is 1000 Hz.
148 *
149 * If other StatusSignals in the same status frame have been set to an update frequency,
150 * the fastest requested update frequency will be applied to the frame.
151 *
152 * This will wait up to 0.100 seconds (100ms) for each signal.
153 *
154 * \param frequency Rate to publish the signal in Hz.
155 * \returns Status code of the first failed update frequency set call, or OK if all succeeded
156 */
158 {
160 }
161
162 /**
163 * \brief Checks if all signals have an OK error code.
164 *
165 * \returns True if all signals are OK, false otherwise
166 */
167 bool IsAllGood() const
168 {
170 }
171 };
172
173}
174}
Class that provides operations to retrieve information about a status signal.
Definition StatusSignal.hpp:38
static bool IsAllGood(Signals const &... signals)
Checks if all signals have an OK error code.
Definition StatusSignal.hpp:494
static ctre::phoenix::StatusCode SetUpdateFrequencyForAll(wpi::units::hertz_t frequencyHz, Signals &... signals)
Sets the update frequency of all specified status signals to the provided common frequency.
Definition StatusSignal.hpp:531
static ctre::phoenix::StatusCode RefreshAll(Signals &... signals)
Performs a non-blocking refresh on all provided signals.
Definition StatusSignal.hpp:429
static ctre::phoenix::StatusCode WaitForAll(wpi::units::second_t timeoutSeconds, Signals &... signals)
Waits for new data on all provided signals up to timeout.
Definition StatusSignal.hpp:312
void AddSignals(std::span< BaseStatusSignal *const > signals)
Adds the provided signals to the collection.
Definition StatusSignalCollection.hpp:90
ctre::phoenix::StatusCode RefreshAll(bool reportError=true)
Performs a non-blocking refresh on all signals.
Definition StatusSignalCollection.hpp:138
StatusSignalCollection(size_t signalCount)
Creates a new collection of status signals, reserving capacity for the specified number of signals.
Definition StatusSignalCollection.hpp:61
bool IsAllGood() const
Checks if all signals have an OK error code.
Definition StatusSignalCollection.hpp:167
void AddSignals(Signals &... signals)
Adds the provided signals to the collection.
Definition StatusSignalCollection.hpp:76
ctre::phoenix::StatusCode SetUpdateFrequencyForAll(wpi::units::hertz_t frequency)
Sets the update frequency of all status signals to the provided common frequency.
Definition StatusSignalCollection.hpp:157
std::vector< BaseStatusSignal * > _signals
Signals stored by this collection.
Definition StatusSignalCollection.hpp:24
ctre::phoenix::StatusCode WaitForAll(wpi::units::second_t timeout, bool reportError=true)
Waits for new data on all signals up to timeout.
Definition StatusSignalCollection.hpp:121
StatusSignalCollection(Signals &... signals)
Creates a new collection of status signals, optionally adding the provided signals to the collection.
Definition StatusSignalCollection.hpp:38
StatusSignalCollection(std::span< BaseStatusSignal *const > signals)
Creates a new collection of status signals, adding the provided span of signals to the collection.
Definition StatusSignalCollection.hpp:51
Status codes reported by APIs, including OK, warnings, and errors.
Definition StatusCodes.h:28
Definition SpnEnums.hpp:16
Definition ExternalFeedbackConfigs.hpp:16
Definition motor_constants.h:14