WPILibC++ 2027.0.0-alpha-4
Loading...
Searching...
No Matches
PowerDistribution.hpp
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
7#include <vector>
8
10#include "wpi/hal/Types.hpp"
13
14namespace wpi {
15
16/**
17 * Class for getting voltage, current, temperature, power and energy from the
18 * CTRE Power Distribution Panel (PDP) or REV Power Distribution Hub (PDH).
19 */
21 public wpi::util::SendableHelper<PowerDistribution> {
22 public:
23 /// Default module number.
24 static constexpr int kDefaultModule = -1;
25
26 /**
27 * Power distribution module type.
28 */
29 enum class ModuleType {
30 /// CTRE (Cross The Road Electronics) CTRE Power Distribution Panel (PDP).
31 kCTRE = 1,
32 /// REV Power Distribution Hub (PDH).
33 kRev = 2
34 };
35
36 /**
37 * Constructs a PowerDistribution object.
38 *
39 * Detects the connected PDP/PDH using the default CAN ID (0 for CTRE and 1
40 * for REV).
41 *
42 * @param busId The bus ID.
43 */
44 explicit PowerDistribution(int busId);
45
46 /**
47 * Constructs a PowerDistribution object.
48 *
49 * @param busId The bus ID.
50 * @param module The CAN ID of the PDP/PDH
51 * @param moduleType The type of module
52 */
53 PowerDistribution(int busId, int module, ModuleType moduleType);
54
57
58 ~PowerDistribution() override = default;
59
60 /**
61 * Gets the number of channels for this power distribution object.
62 *
63 * @return Number of output channels (16 for PDP, 24 for PDH).
64 */
65 int GetNumChannels() const;
66
67 /**
68 * Query the input voltage of the PDP/PDH.
69 *
70 * @return The input voltage in volts
71 */
72 double GetVoltage() const;
73
74 /**
75 * Query the temperature of the PDP.
76 *
77 * Not supported on the Rev PDH and returns 0.
78 *
79 *
80 * @return The temperature in degrees Celsius
81 */
82 double GetTemperature() const;
83
84 /**
85 * Query the current of a single channel of the PDP/PDH.
86 *
87 * @param channel the channel to query (0-15 for PDP, 0-23 for PDH)
88 * @return The current of the channel in Amperes
89 */
90 double GetCurrent(int channel) const;
91
92 /**
93 * Query all currents of the PDP.
94 *
95 * @return The current of each channel in Amperes
96 */
97 std::vector<double> GetAllCurrents() const;
98
99 /**
100 * Query the total current of all monitored PDP/PDH channels.
101 *
102 * @return The total current drawn from all channels in Amperes
103 */
104 double GetTotalCurrent() const;
105
106 /**
107 * Query the total power drawn from all monitored PDP channels.
108 *
109 * Not supported on the Rev PDH and returns 0.
110 *
111 * @return The total power drawn in Watts
112 */
113 double GetTotalPower() const;
114
115 /**
116 * Query the total energy drawn from the monitored PDP channels.
117 *
118 * Not supported on the Rev PDH and returns 0.
119 *
120 * @return The total energy drawn in Joules
121 */
122 double GetTotalEnergy() const;
123
124 /**
125 * Reset the total energy drawn from the PDP.
126 *
127 * Not supported on the Rev PDH and does nothing.
128 *
129 * @see PowerDistribution#GetTotalEnergy
130 */
132
133 /**
134 * Remove all of the fault flags on the PDP/PDH.
135 */
137
138 /**
139 * Gets module number (CAN ID).
140 */
141 int GetModule() const;
142
143 /**
144 * Gets module type.
145 */
147
148 /**
149 * Gets whether the PDH switchable channel is turned on or off. Returns false
150 * with the CTRE PDP.
151 *
152 * @return The output state of the PDH switchable channel
153 */
155
156 /**
157 * Sets the PDH switchable channel on or off. Does nothing with the CTRE PDP.
158 *
159 * @param enabled Whether to turn the PDH switchable channel on or off
160 */
161 void SetSwitchableChannel(bool enabled);
162
163 /** Version and device data received from a PowerDistribution device */
164 struct Version {
165 /** Firmware major version number. */
167 /** Firmware minor version number. */
169 /** Firmware fix version number. */
170 uint32_t FirmwareFix;
171 /** Hardware minor version number. */
173 /** Hardware major version number. */
175 /** Unique ID. */
176 uint32_t UniqueId;
177 };
178
180
181 /**
182 * Faults for a PowerDistribution device. These faults are only active while
183 * the condition is active.
184 */
185 struct Faults {
186 /** Breaker fault on channel 0. */
188 /** Breaker fault on channel 1. */
190 /** Breaker fault on channel 2. */
192 /** Breaker fault on channel 3. */
194 /** Breaker fault on channel 4. */
196 /** Breaker fault on channel 5. */
198 /** Breaker fault on channel 6. */
200 /** Breaker fault on channel 7. */
202 /** Breaker fault on channel 8. */
204 /** Breaker fault on channel 9. */
206 /** Breaker fault on channel 10. */
208 /** Breaker fault on channel 12. */
210 /** Breaker fault on channel 13. */
212 /** Breaker fault on channel 14. */
214 /** Breaker fault on channel 15. */
216 /** Breaker fault on channel 16. */
218 /** Breaker fault on channel 17. */
220 /** Breaker fault on channel 18. */
222 /** Breaker fault on channel 19. */
224 /** Breaker fault on channel 20. */
226 /** Breaker fault on channel 21. */
228 /** Breaker fault on channel 22. */
230 /** Breaker fault on channel 23. */
232 /** Breaker fault on channel 24. */
234 /** The input voltage is below the minimum voltage. */
235 uint32_t Brownout : 1;
236 /** A warning was raised by the device's CAN controller. */
237 uint32_t CanWarning : 1;
238 /** The hardware on the device has malfunctioned. */
239 uint32_t HardwareFault : 1;
240
241 /**
242 * Gets whether there is a breaker fault at a specified channel.
243 * @param channel Channel to check for faults.
244 * @return If there is a breaker fault.
245 * @throws A ChannelIndexOutOfRange error if the given int is outside of the
246 * range supported by the hardware.
247 */
248 bool GetBreakerFault(int channel) const;
249 };
250
251 /**
252 * Returns the power distribution faults.
253 *
254 * On a CTRE PDP, this will return an object with no faults active.
255 *
256 * @return The power distribution faults.
257 */
259
260 /**
261 * Sticky faults for a PowerDistribution device. These faults will remain
262 * active until they are reset by the user.
263 */
265 /** Breaker fault on channel 0. */
267 /** Breaker fault on channel 1. */
269 /** Breaker fault on channel 2. */
271 /** Breaker fault on channel 3. */
273 /** Breaker fault on channel 4. */
275 /** Breaker fault on channel 5. */
277 /** Breaker fault on channel 6. */
279 /** Breaker fault on channel 7. */
281 /** Breaker fault on channel 8. */
283 /** Breaker fault on channel 9. */
285 /** Breaker fault on channel 10. */
287 /** Breaker fault on channel 12. */
289 /** Breaker fault on channel 13. */
291 /** Breaker fault on channel 14. */
293 /** Breaker fault on channel 15. */
295 /** Breaker fault on channel 16. */
297 /** Breaker fault on channel 17. */
299 /** Breaker fault on channel 18. */
301 /** Breaker fault on channel 19. */
303 /** Breaker fault on channel 20. */
305 /** Breaker fault on channel 21. */
307 /** Breaker fault on channel 22. */
309 /** Breaker fault on channel 23. */
311 /** Breaker fault on channel 24. */
313 /** The input voltage is below the minimum voltage. */
314 uint32_t Brownout : 1;
315 /** A warning was raised by the device's CAN controller. */
316 uint32_t CanWarning : 1;
317 /** The device's CAN controller experienced a "Bus Off" event. */
318 uint32_t CanBusOff : 1;
319 /** The hardware on the device has malfunctioned. */
320 uint32_t HardwareFault : 1;
321 /** The firmware on the device has malfunctioned. */
322 uint32_t FirmwareFault : 1;
323 /** The device has rebooted. */
324 uint32_t HasReset : 1;
325
326 /**
327 * Gets whether there is a sticky breaker fault at the specified channel.
328 * @param channel Index to check for sticky faults.
329 * @return True if there is a sticky breaker fault at the channel, otherwise
330 * false.
331 * @throws A ChannelIndexOutOfRange error if the provided channel is outside
332 * of the range supported by the hardware.
333 */
334 bool GetBreakerFault(int channel) const;
335 };
336
337 /**
338 * Returns the power distribution sticky faults.
339 *
340 * On a CTRE PDP, this will return an object with no faults active.
341 *
342 * @return The power distribution sticky faults.
343 */
345
347
348 private:
350 m_handle;
351 int m_module;
352};
353
354} // namespace wpi
Faults GetFaults() const
Returns the power distribution faults.
void ClearStickyFaults()
Remove all of the fault flags on the PDP/PDH.
static constexpr int kDefaultModule
Default module number.
Definition PowerDistribution.hpp:24
~PowerDistribution() override=default
void ResetTotalEnergy()
Reset the total energy drawn from the PDP.
double GetTotalEnergy() const
Query the total energy drawn from the monitored PDP channels.
double GetTemperature() const
Query the temperature of the PDP.
double GetCurrent(int channel) const
Query the current of a single channel of the PDP/PDH.
double GetVoltage() const
Query the input voltage of the PDP/PDH.
ModuleType
Power distribution module type.
Definition PowerDistribution.hpp:29
@ kCTRE
CTRE (Cross The Road Electronics) CTRE Power Distribution Panel (PDP).
Definition PowerDistribution.hpp:31
@ kRev
REV Power Distribution Hub (PDH).
Definition PowerDistribution.hpp:33
bool GetSwitchableChannel() const
Gets whether the PDH switchable channel is turned on or off.
int GetNumChannels() const
Gets the number of channels for this power distribution object.
PowerDistribution & operator=(PowerDistribution &&)=default
double GetTotalCurrent() const
Query the total current of all monitored PDP/PDH channels.
ModuleType GetType() const
Gets module type.
int GetModule() const
Gets module number (CAN ID).
PowerDistribution(int busId, int module, ModuleType moduleType)
Constructs a PowerDistribution object.
PowerDistribution(int busId)
Constructs a PowerDistribution object.
StickyFaults GetStickyFaults() const
Returns the power distribution sticky faults.
void InitSendable(wpi::util::SendableBuilder &builder) override
Initializes this Sendable object.
std::vector< double > GetAllCurrents() const
Query all currents of the PDP.
double GetTotalPower() const
Query the total power drawn from all monitored PDP channels.
PowerDistribution(PowerDistribution &&)=default
Version GetVersion() const
void SetSwitchableChannel(bool enabled)
Sets the PDH switchable channel on or off.
A move-only C++ wrapper around a HAL handle.
Definition Types.hpp:16
Helper class for building Sendable dashboard representations.
Definition SendableBuilder.hpp:21
A helper class for use with objects that add themselves to SendableRegistry.
Definition SendableHelper.hpp:21
Interface for Sendable objects.
Definition Sendable.hpp:16
Definition CvSource.hpp:15
Faults for a PowerDistribution device.
Definition PowerDistribution.hpp:185
uint32_t Channel19BreakerFault
Breaker fault on channel 20.
Definition PowerDistribution.hpp:225
uint32_t Channel13BreakerFault
Breaker fault on channel 14.
Definition PowerDistribution.hpp:213
uint32_t HardwareFault
The hardware on the device has malfunctioned.
Definition PowerDistribution.hpp:239
uint32_t Channel20BreakerFault
Breaker fault on channel 21.
Definition PowerDistribution.hpp:227
uint32_t Channel18BreakerFault
Breaker fault on channel 19.
Definition PowerDistribution.hpp:223
uint32_t Channel9BreakerFault
Breaker fault on channel 9.
Definition PowerDistribution.hpp:205
uint32_t Channel12BreakerFault
Breaker fault on channel 13.
Definition PowerDistribution.hpp:211
uint32_t Channel2BreakerFault
Breaker fault on channel 2.
Definition PowerDistribution.hpp:191
uint32_t Channel4BreakerFault
Breaker fault on channel 4.
Definition PowerDistribution.hpp:195
uint32_t Channel11BreakerFault
Breaker fault on channel 12.
Definition PowerDistribution.hpp:209
uint32_t Channel14BreakerFault
Breaker fault on channel 15.
Definition PowerDistribution.hpp:215
uint32_t Channel23BreakerFault
Breaker fault on channel 24.
Definition PowerDistribution.hpp:233
uint32_t Brownout
The input voltage is below the minimum voltage.
Definition PowerDistribution.hpp:235
uint32_t Channel10BreakerFault
Breaker fault on channel 10.
Definition PowerDistribution.hpp:207
uint32_t Channel17BreakerFault
Breaker fault on channel 18.
Definition PowerDistribution.hpp:221
uint32_t Channel0BreakerFault
Breaker fault on channel 0.
Definition PowerDistribution.hpp:187
uint32_t Channel15BreakerFault
Breaker fault on channel 16.
Definition PowerDistribution.hpp:217
bool GetBreakerFault(int channel) const
Gets whether there is a breaker fault at a specified channel.
uint32_t Channel21BreakerFault
Breaker fault on channel 22.
Definition PowerDistribution.hpp:229
uint32_t Channel16BreakerFault
Breaker fault on channel 17.
Definition PowerDistribution.hpp:219
uint32_t CanWarning
A warning was raised by the device's CAN controller.
Definition PowerDistribution.hpp:237
uint32_t Channel8BreakerFault
Breaker fault on channel 8.
Definition PowerDistribution.hpp:203
uint32_t Channel7BreakerFault
Breaker fault on channel 7.
Definition PowerDistribution.hpp:201
uint32_t Channel3BreakerFault
Breaker fault on channel 3.
Definition PowerDistribution.hpp:193
uint32_t Channel1BreakerFault
Breaker fault on channel 1.
Definition PowerDistribution.hpp:189
uint32_t Channel6BreakerFault
Breaker fault on channel 6.
Definition PowerDistribution.hpp:199
uint32_t Channel22BreakerFault
Breaker fault on channel 23.
Definition PowerDistribution.hpp:231
uint32_t Channel5BreakerFault
Breaker fault on channel 5.
Definition PowerDistribution.hpp:197
Sticky faults for a PowerDistribution device.
Definition PowerDistribution.hpp:264
uint32_t Channel15BreakerFault
Breaker fault on channel 16.
Definition PowerDistribution.hpp:296
uint32_t Channel1BreakerFault
Breaker fault on channel 1.
Definition PowerDistribution.hpp:268
uint32_t Channel3BreakerFault
Breaker fault on channel 3.
Definition PowerDistribution.hpp:272
uint32_t Channel4BreakerFault
Breaker fault on channel 4.
Definition PowerDistribution.hpp:274
uint32_t Channel5BreakerFault
Breaker fault on channel 5.
Definition PowerDistribution.hpp:276
uint32_t Channel14BreakerFault
Breaker fault on channel 15.
Definition PowerDistribution.hpp:294
uint32_t Channel2BreakerFault
Breaker fault on channel 2.
Definition PowerDistribution.hpp:270
uint32_t Channel8BreakerFault
Breaker fault on channel 8.
Definition PowerDistribution.hpp:282
uint32_t HasReset
The device has rebooted.
Definition PowerDistribution.hpp:324
uint32_t Channel0BreakerFault
Breaker fault on channel 0.
Definition PowerDistribution.hpp:266
uint32_t Channel23BreakerFault
Breaker fault on channel 24.
Definition PowerDistribution.hpp:312
uint32_t Channel10BreakerFault
Breaker fault on channel 10.
Definition PowerDistribution.hpp:286
uint32_t Channel16BreakerFault
Breaker fault on channel 17.
Definition PowerDistribution.hpp:298
uint32_t Channel22BreakerFault
Breaker fault on channel 23.
Definition PowerDistribution.hpp:310
uint32_t CanWarning
A warning was raised by the device's CAN controller.
Definition PowerDistribution.hpp:316
uint32_t Channel6BreakerFault
Breaker fault on channel 6.
Definition PowerDistribution.hpp:278
uint32_t Channel13BreakerFault
Breaker fault on channel 14.
Definition PowerDistribution.hpp:292
uint32_t FirmwareFault
The firmware on the device has malfunctioned.
Definition PowerDistribution.hpp:322
uint32_t Channel19BreakerFault
Breaker fault on channel 20.
Definition PowerDistribution.hpp:304
uint32_t Channel17BreakerFault
Breaker fault on channel 18.
Definition PowerDistribution.hpp:300
uint32_t CanBusOff
The device's CAN controller experienced a "Bus Off" event.
Definition PowerDistribution.hpp:318
uint32_t Brownout
The input voltage is below the minimum voltage.
Definition PowerDistribution.hpp:314
uint32_t Channel7BreakerFault
Breaker fault on channel 7.
Definition PowerDistribution.hpp:280
uint32_t Channel12BreakerFault
Breaker fault on channel 13.
Definition PowerDistribution.hpp:290
uint32_t Channel21BreakerFault
Breaker fault on channel 22.
Definition PowerDistribution.hpp:308
uint32_t Channel11BreakerFault
Breaker fault on channel 12.
Definition PowerDistribution.hpp:288
uint32_t Channel20BreakerFault
Breaker fault on channel 21.
Definition PowerDistribution.hpp:306
uint32_t Channel9BreakerFault
Breaker fault on channel 9.
Definition PowerDistribution.hpp:284
bool GetBreakerFault(int channel) const
Gets whether there is a sticky breaker fault at the specified channel.
uint32_t Channel18BreakerFault
Breaker fault on channel 19.
Definition PowerDistribution.hpp:302
uint32_t HardwareFault
The hardware on the device has malfunctioned.
Definition PowerDistribution.hpp:320
Version and device data received from a PowerDistribution device.
Definition PowerDistribution.hpp:164
uint32_t UniqueId
Unique ID.
Definition PowerDistribution.hpp:176
uint32_t FirmwareFix
Firmware fix version number.
Definition PowerDistribution.hpp:170
uint32_t FirmwareMajor
Firmware major version number.
Definition PowerDistribution.hpp:166
uint32_t FirmwareMinor
Firmware minor version number.
Definition PowerDistribution.hpp:168
uint32_t HardwareMajor
Hardware major version number.
Definition PowerDistribution.hpp:174
uint32_t HardwareMinor
Hardware minor version number.
Definition PowerDistribution.hpp:172