blob: fb428cf758e12c33224f67cd2bc6990f400ed846 [file] [log] [blame]
Brian Silverman26e4e522015-12-17 01:56:40 -05001/*----------------------------------------------------------------------------*/
2/* Copyright (c) FIRST 2014. All Rights Reserved.
3 */
4/* Open Source Software - may be modified and shared by FRC teams. The code */
5/* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib. */
6/*----------------------------------------------------------------------------*/
7
8#include "PowerDistributionPanel.h"
9#include "WPIErrors.h"
10#include "HAL/PDP.hpp"
11#include "LiveWindow/LiveWindow.h"
12
13#include <sstream>
14
15PowerDistributionPanel::PowerDistributionPanel() : PowerDistributionPanel(0) {}
16
17/**
18 * Initialize the PDP.
19 */
20PowerDistributionPanel::PowerDistributionPanel(uint8_t module)
21 : m_module(module) {
22 initializePDP(m_module);
23}
24
25/**
26 * Query the input voltage of the PDP
27 * @return The voltage of the PDP in volts
28 */
29double PowerDistributionPanel::GetVoltage() const {
30 int32_t status = 0;
31
32 double voltage = getPDPVoltage(m_module, &status);
33
34 if (status) {
35 wpi_setWPIErrorWithContext(Timeout, "");
36 }
37
38 return voltage;
39}
40
41/**
42 * Query the temperature of the PDP
43 * @return The temperature of the PDP in degrees Celsius
44 */
45double PowerDistributionPanel::GetTemperature() const {
46 int32_t status = 0;
47
48 double temperature = getPDPTemperature(m_module, &status);
49
50 if (status) {
51 wpi_setWPIErrorWithContext(Timeout, "");
52 }
53
54 return temperature;
55}
56
57/**
58 * Query the current of a single channel of the PDP
59 * @return The current of one of the PDP channels (channels 0-15) in Amperes
60 */
61double PowerDistributionPanel::GetCurrent(uint8_t channel) const {
62 int32_t status = 0;
63
64 if (!CheckPDPChannel(channel)) {
65 std::stringstream buf;
66 buf << "PDP Channel " << channel;
67 wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, buf.str());
68 }
69
70 double current = getPDPChannelCurrent(m_module, channel, &status);
71
72 if (status) {
73 wpi_setWPIErrorWithContext(Timeout, "");
74 }
75
76 return current;
77}
78
79/**
80 * Query the total current of all monitored PDP channels (0-15)
81 * @return The the total current drawn from the PDP channels in Amperes
82 */
83double PowerDistributionPanel::GetTotalCurrent() const {
84 int32_t status = 0;
85
86 double current = getPDPTotalCurrent(m_module, &status);
87
88 if (status) {
89 wpi_setWPIErrorWithContext(Timeout, "");
90 }
91
92 return current;
93}
94
95/**
96 * Query the total power drawn from the monitored PDP channels
97 * @return The the total power drawn from the PDP channels in Watts
98 */
99double PowerDistributionPanel::GetTotalPower() const {
100 int32_t status = 0;
101
102 double power = getPDPTotalPower(m_module, &status);
103
104 if (status) {
105 wpi_setWPIErrorWithContext(Timeout, "");
106 }
107
108 return power;
109}
110
111/**
112 * Query the total energy drawn from the monitored PDP channels
113 * @return The the total energy drawn from the PDP channels in Joules
114 */
115double PowerDistributionPanel::GetTotalEnergy() const {
116 int32_t status = 0;
117
118 double energy = getPDPTotalEnergy(m_module, &status);
119
120 if (status) {
121 wpi_setWPIErrorWithContext(Timeout, "");
122 }
123
124 return energy;
125}
126
127/**
128 * Reset the total energy drawn from the PDP
129 * @see PowerDistributionPanel#GetTotalEnergy
130 */
131void PowerDistributionPanel::ResetTotalEnergy() {
132 int32_t status = 0;
133
134 resetPDPTotalEnergy(m_module, &status);
135
136 if (status) {
137 wpi_setWPIErrorWithContext(Timeout, "");
138 }
139}
140
141/**
142 * Remove all of the fault flags on the PDP
143 */
144void PowerDistributionPanel::ClearStickyFaults() {
145 int32_t status = 0;
146
147 clearPDPStickyFaults(m_module, &status);
148
149 if (status) {
150 wpi_setWPIErrorWithContext(Timeout, "");
151 }
152}
153
154void PowerDistributionPanel::UpdateTable() {
155 if (m_table != nullptr) {
156 m_table->PutNumber("Chan0", GetCurrent(0));
157 m_table->PutNumber("Chan1", GetCurrent(1));
158 m_table->PutNumber("Chan2", GetCurrent(2));
159 m_table->PutNumber("Chan3", GetCurrent(3));
160 m_table->PutNumber("Chan4", GetCurrent(4));
161 m_table->PutNumber("Chan5", GetCurrent(5));
162 m_table->PutNumber("Chan6", GetCurrent(6));
163 m_table->PutNumber("Chan7", GetCurrent(7));
164 m_table->PutNumber("Chan8", GetCurrent(8));
165 m_table->PutNumber("Chan9", GetCurrent(9));
166 m_table->PutNumber("Chan10", GetCurrent(10));
167 m_table->PutNumber("Chan11", GetCurrent(11));
168 m_table->PutNumber("Chan12", GetCurrent(12));
169 m_table->PutNumber("Chan13", GetCurrent(13));
170 m_table->PutNumber("Chan14", GetCurrent(14));
171 m_table->PutNumber("Chan15", GetCurrent(15));
172 m_table->PutNumber("Voltage", GetVoltage());
173 m_table->PutNumber("TotalCurrent", GetTotalCurrent());
174 }
175}
176
177void PowerDistributionPanel::StartLiveWindowMode() {}
178
179void PowerDistributionPanel::StopLiveWindowMode() {}
180
181std::string PowerDistributionPanel::GetSmartDashboardType() const {
182 return "PowerDistributionPanel";
183}
184
185void PowerDistributionPanel::InitTable(std::shared_ptr<ITable> subTable) {
186 m_table = subTable;
187 UpdateTable();
188}
189
190std::shared_ptr<ITable> PowerDistributionPanel::GetTable() const { return m_table; }