WPILibC++ 2024.3.2
PowerDistribution.h
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 <hal/Types.h>
10
11namespace frc {
12
13/**
14 * Class for getting voltage, current, temperature, power and energy from the
15 * CTRE Power Distribution Panel (PDP) or REV Power Distribution Hub (PDH).
16 */
18 public wpi::SendableHelper<PowerDistribution> {
19 public:
20 /// Default module number.
21 static constexpr int kDefaultModule = -1;
22
23 /**
24 * Power distribution module type.
25 */
26 enum class ModuleType {
27 /// CTRE (Cross The Road Electronics) CTRE Power Distribution Panel (PDP).
28 kCTRE = 1,
29 /// REV Power Distribution Hub (PDH).
30 kRev = 2
31 };
32
33 /**
34 * Constructs a PowerDistribution object.
35 *
36 * Detects the connected PDP/PDH using the default CAN ID (0 for CTRE and 1
37 * for REV).
38 */
40
41 /**
42 * Constructs a PowerDistribution object.
43 *
44 * @param module The CAN ID of the PDP/PDH
45 * @param moduleType The type of module
46 */
47 PowerDistribution(int module, ModuleType moduleType);
48
52
53 /**
54 * Query the input voltage of the PDP/PDH.
55 *
56 * @return The input voltage in volts
57 */
58 double GetVoltage() const;
59
60 /**
61 * Query the temperature of the PDP.
62 *
63 * Not supported on the Rev PDH and returns 0.
64 *
65 *
66 * @return The temperature in degrees Celsius
67 */
68 double GetTemperature() const;
69
70 /**
71 * Query the current of a single channel of the PDP/PDH.
72 *
73 * @param channel the channel to query (0-15 for PDP, 0-23 for PDH)
74 * @return The current of the channel in Amperes
75 */
76 double GetCurrent(int channel) const;
77
78 /**
79 * Query the total current of all monitored PDP/PDH channels.
80 *
81 * @return The total current drawn from all channels in Amperes
82 */
83 double GetTotalCurrent() const;
84
85 /**
86 * Query the total power drawn from all monitored PDP channels.
87 *
88 * Not supported on the Rev PDH and returns 0.
89 *
90 * @return The total power drawn in Watts
91 */
92 double GetTotalPower() const;
93
94 /**
95 * Query the total energy drawn from the monitored PDP channels.
96 *
97 * Not supported on the Rev PDH and returns 0.
98 *
99 * @return The total energy drawn in Joules
100 */
101 double GetTotalEnergy() const;
102
103 /**
104 * Reset the total energy drawn from the PDP.
105 *
106 * Not supported on the Rev PDH and does nothing.
107 *
108 * @see PowerDistribution#GetTotalEnergy
109 */
111
112 /**
113 * Remove all of the fault flags on the PDP/PDH.
114 */
116
117 /**
118 * Gets module number (CAN ID).
119 */
120 int GetModule() const;
121
122 /**
123 * Gets module type.
124 */
126
127 /**
128 * Gets whether the PDH switchable channel is turned on or off. Returns false
129 * with the CTRE PDP.
130 *
131 * @return The output state of the PDH switchable channel
132 */
134
135 /**
136 * Sets the PDH switchable channel on or off. Does nothing with the CTRE PDP.
137 *
138 * @param enabled Whether to turn the PDH switchable channel on or off
139 */
140 void SetSwitchableChannel(bool enabled);
141
142 struct Version {
145 uint32_t FirmwareFix;
148 uint32_t UniqueId;
149 };
150
152
153 struct Faults {
178 uint32_t Brownout : 1;
179 uint32_t CanWarning : 1;
180 uint32_t HardwareFault : 1;
181
182 /**
183 * Gets whether there is a breaker fault at a specified channel.
184 * @param channel Channel to check for faults.
185 * @return If there is a breaker fault.
186 * @throws A ChannelIndexOutOfRange error if the given int is outside of the
187 * range supported by the hardware.
188 */
189 bool GetBreakerFault(int channel) const;
190 };
191
192 /**
193 * Returns the power distribution faults.
194 *
195 * @return The power distribution faults.
196 */
198
224 uint32_t Brownout : 1;
225 uint32_t CanWarning : 1;
226 uint32_t CanBusOff : 1;
227 uint32_t HasReset : 1;
228
229 /**
230 * Gets whether there is a sticky breaker fault at the specified channel.
231 * @param channel Index to check for sticky faults.
232 * @return True if there is a sticky breaker fault at the channel, otherwise
233 * false.
234 * @throws A ChannelIndexOutOfRange error if the provided channel is outside
235 * of the range supported by the hardware.
236 */
237 bool GetBreakerFault(int channel) const;
238 };
239
240 /**
241 * Returns the power distribution sticky faults.
242 *
243 * @return The power distribution sticky faults.
244 */
246
247 void InitSendable(wpi::SendableBuilder& builder) override;
248
249 private:
251 int m_module;
252};
253
254} // namespace frc
Class for getting voltage, current, temperature, power and energy from the CTRE Power Distribution Pa...
Definition: PowerDistribution.h:18
void ClearStickyFaults()
Remove all of the fault flags on the PDP/PDH.
double GetTotalCurrent() const
Query the total current of all monitored PDP/PDH channels.
double GetTemperature() const
Query the temperature of the PDP.
ModuleType GetType() const
Gets module type.
void SetSwitchableChannel(bool enabled)
Sets the PDH switchable channel on or off.
StickyFaults GetStickyFaults() const
Returns the power distribution sticky faults.
double GetTotalPower() const
Query the total power drawn from all monitored PDP channels.
ModuleType
Power distribution module type.
Definition: PowerDistribution.h:26
Version GetVersion() const
PowerDistribution(PowerDistribution &&)=default
PowerDistribution()
Constructs a PowerDistribution object.
PowerDistribution & operator=(PowerDistribution &&)=default
double GetCurrent(int channel) const
Query the current of a single channel of the PDP/PDH.
PowerDistribution(int module, ModuleType moduleType)
Constructs a PowerDistribution object.
Faults GetFaults() const
Returns the power distribution faults.
double GetTotalEnergy() const
Query the total energy drawn from the monitored PDP channels.
~PowerDistribution() override
static constexpr int kDefaultModule
Default module number.
Definition: PowerDistribution.h:21
void ResetTotalEnergy()
Reset the total energy drawn from the PDP.
int GetModule() const
Gets module number (CAN ID).
bool GetSwitchableChannel() const
Gets whether the PDH switchable channel is turned on or off.
double GetVoltage() const
Query the input voltage of the PDP/PDH.
void InitSendable(wpi::SendableBuilder &builder) override
Initializes this Sendable object.
Helper class for building Sendable dashboard representations.
Definition: SendableBuilder.h:21
A helper class for use with objects that add themselves to SendableRegistry.
Definition: SendableHelper.h:19
Interface for Sendable objects.
Definition: Sendable.h:16
Definition: AprilTagPoseEstimator.h:15
Definition: PowerDistribution.h:153
uint32_t Channel4BreakerFault
Definition: PowerDistribution.h:158
uint32_t Channel18BreakerFault
Definition: PowerDistribution.h:172
uint32_t Channel2BreakerFault
Definition: PowerDistribution.h:156
uint32_t Brownout
Definition: PowerDistribution.h:178
uint32_t CanWarning
Definition: PowerDistribution.h:179
uint32_t Channel13BreakerFault
Definition: PowerDistribution.h:167
uint32_t Channel19BreakerFault
Definition: PowerDistribution.h:173
uint32_t Channel9BreakerFault
Definition: PowerDistribution.h:163
uint32_t Channel17BreakerFault
Definition: PowerDistribution.h:171
uint32_t Channel0BreakerFault
Definition: PowerDistribution.h:154
uint32_t Channel21BreakerFault
Definition: PowerDistribution.h:175
uint32_t Channel7BreakerFault
Definition: PowerDistribution.h:161
uint32_t Channel23BreakerFault
Definition: PowerDistribution.h:177
uint32_t Channel20BreakerFault
Definition: PowerDistribution.h:174
uint32_t Channel12BreakerFault
Definition: PowerDistribution.h:166
uint32_t Channel6BreakerFault
Definition: PowerDistribution.h:160
uint32_t Channel10BreakerFault
Definition: PowerDistribution.h:164
uint32_t Channel1BreakerFault
Definition: PowerDistribution.h:155
uint32_t Channel8BreakerFault
Definition: PowerDistribution.h:162
uint32_t HardwareFault
Definition: PowerDistribution.h:180
uint32_t Channel11BreakerFault
Definition: PowerDistribution.h:165
uint32_t Channel16BreakerFault
Definition: PowerDistribution.h:170
uint32_t Channel14BreakerFault
Definition: PowerDistribution.h:168
uint32_t Channel22BreakerFault
Definition: PowerDistribution.h:176
uint32_t Channel15BreakerFault
Definition: PowerDistribution.h:169
uint32_t Channel3BreakerFault
Definition: PowerDistribution.h:157
bool GetBreakerFault(int channel) const
Gets whether there is a breaker fault at a specified channel.
uint32_t Channel5BreakerFault
Definition: PowerDistribution.h:159
Definition: PowerDistribution.h:199
uint32_t Brownout
Definition: PowerDistribution.h:224
uint32_t Channel8BreakerFault
Definition: PowerDistribution.h:208
uint32_t Channel18BreakerFault
Definition: PowerDistribution.h:218
bool GetBreakerFault(int channel) const
Gets whether there is a sticky breaker fault at the specified channel.
uint32_t Channel23BreakerFault
Definition: PowerDistribution.h:223
uint32_t Channel20BreakerFault
Definition: PowerDistribution.h:220
uint32_t Channel1BreakerFault
Definition: PowerDistribution.h:201
uint32_t Channel4BreakerFault
Definition: PowerDistribution.h:204
uint32_t Channel12BreakerFault
Definition: PowerDistribution.h:212
uint32_t Channel22BreakerFault
Definition: PowerDistribution.h:222
uint32_t Channel10BreakerFault
Definition: PowerDistribution.h:210
uint32_t Channel7BreakerFault
Definition: PowerDistribution.h:207
uint32_t Channel19BreakerFault
Definition: PowerDistribution.h:219
uint32_t Channel17BreakerFault
Definition: PowerDistribution.h:217
uint32_t Channel6BreakerFault
Definition: PowerDistribution.h:206
uint32_t Channel14BreakerFault
Definition: PowerDistribution.h:214
uint32_t Channel0BreakerFault
Definition: PowerDistribution.h:200
uint32_t Channel11BreakerFault
Definition: PowerDistribution.h:211
uint32_t Channel15BreakerFault
Definition: PowerDistribution.h:215
uint32_t Channel2BreakerFault
Definition: PowerDistribution.h:202
uint32_t Channel16BreakerFault
Definition: PowerDistribution.h:216
uint32_t Channel13BreakerFault
Definition: PowerDistribution.h:213
uint32_t HasReset
Definition: PowerDistribution.h:227
uint32_t CanBusOff
Definition: PowerDistribution.h:226
uint32_t Channel21BreakerFault
Definition: PowerDistribution.h:221
uint32_t Channel9BreakerFault
Definition: PowerDistribution.h:209
uint32_t CanWarning
Definition: PowerDistribution.h:225
uint32_t Channel3BreakerFault
Definition: PowerDistribution.h:203
uint32_t Channel5BreakerFault
Definition: PowerDistribution.h:205
Definition: PowerDistribution.h:142
uint32_t HardwareMajor
Definition: PowerDistribution.h:147
uint32_t FirmwareMajor
Definition: PowerDistribution.h:143
uint32_t HardwareMinor
Definition: PowerDistribution.h:146
uint32_t FirmwareFix
Definition: PowerDistribution.h:145
uint32_t FirmwareMinor
Definition: PowerDistribution.h:144
uint32_t UniqueId
Definition: PowerDistribution.h:148