blob: 90655c2dfa7c38cc11e04b28857edafe63a0183f [file] [log] [blame]
Parker Schuhd3b7a8872018-02-19 16:42:27 -08001/*----------------------------------------------------------------------------*/
2/* Copyright (c) FIRST 2014-2017. All Rights Reserved. */
3/* Open Source Software - may be modified and shared by FRC teams. The code */
4/* must be accompanied by the FIRST BSD license file in the root directory of */
5/* the project. */
6/*----------------------------------------------------------------------------*/
7
8#include "frc971/wpilib/ahal/PowerDistributionPanel.h"
9
10#include <sstream>
11
12#include "HAL/HAL.h"
13#include "HAL/PDP.h"
14#include "HAL/Ports.h"
15#include "frc971/wpilib/ahal/WPIErrors.h"
16
17using namespace frc;
18#define WPI_LIB_FATAL_ERROR(tag, msg)
19
20PowerDistributionPanel::PowerDistributionPanel() : PowerDistributionPanel(0) {}
21
22/**
23 * Initialize the PDP.
24 */
25PowerDistributionPanel::PowerDistributionPanel(int module) : m_module(module) {
26 int32_t status = 0;
27 HAL_InitializePDP(m_module, &status);
28 if (status != 0) {
29 // wpi_setErrorWithContextRange(status, 0, HAL_GetNumPDPModules(), module,
30 // HAL_GetErrorMessage(status));
31 m_module = -1;
32 return;
33 }
34}
35
36/**
37 * Query the input voltage of the PDP.
38 *
39 * @return The voltage of the PDP in volts
40 */
41double PowerDistributionPanel::GetVoltage() const {
42 int32_t status = 0;
43
44 double voltage = HAL_GetPDPVoltage(m_module, &status);
45
46 if (status) {
47 WPI_LIB_FATAL_ERROR(Timeout, "");
48 }
49
50 return voltage;
51}
52
53/**
54 * Query the temperature of the PDP.
55 *
56 * @return The temperature of the PDP in degrees Celsius
57 */
58double PowerDistributionPanel::GetTemperature() const {
59 int32_t status = 0;
60
61 double temperature = HAL_GetPDPTemperature(m_module, &status);
62
63 if (status) {
64 WPI_LIB_FATAL_ERROR(Timeout, "");
65 }
66
67 return temperature;
68}
69
70/**
71 * Query the current of a single channel of the PDP.
72 *
73 * @return The current of one of the PDP channels (channels 0-15) in Amperes
74 */
75double PowerDistributionPanel::GetCurrent(int channel) const {
76 int32_t status = 0;
77
78 if (!CheckPDPChannel(channel)) {
79 std::stringstream buf;
80 buf << "PDP Channel " << channel;
81 WPI_LIB_FATAL_ERROR(ChannelIndexOutOfRange, buf.str());
82 }
83
84 double current = HAL_GetPDPChannelCurrent(m_module, channel, &status);
85
86 if (status) {
87 WPI_LIB_FATAL_ERROR(Timeout, "");
88 }
89
90 return current;
91}
92
93/**
94 * Query the total current of all monitored PDP channels (0-15).
95 *
96 * @return The the total current drawn from the PDP channels in Amperes
97 */
98double PowerDistributionPanel::GetTotalCurrent() const {
99 int32_t status = 0;
100
101 double current = HAL_GetPDPTotalCurrent(m_module, &status);
102
103 if (status) {
104 WPI_LIB_FATAL_ERROR(Timeout, "");
105 }
106
107 return current;
108}
109
110/**
111 * Query the total power drawn from the monitored PDP channels.
112 *
113 * @return The the total power drawn from the PDP channels in Watts
114 */
115double PowerDistributionPanel::GetTotalPower() const {
116 int32_t status = 0;
117
118 double power = HAL_GetPDPTotalPower(m_module, &status);
119
120 if (status) {
121 WPI_LIB_FATAL_ERROR(Timeout, "");
122 }
123
124 return power;
125}
126
127/**
128 * Query the total energy drawn from the monitored PDP channels.
129 *
130 * @return The the total energy drawn from the PDP channels in Joules
131 */
132double PowerDistributionPanel::GetTotalEnergy() const {
133 int32_t status = 0;
134
135 double energy = HAL_GetPDPTotalEnergy(m_module, &status);
136
137 if (status) {
138 WPI_LIB_FATAL_ERROR(Timeout, "");
139 }
140
141 return energy;
142}
143
144/**
145 * Reset the total energy drawn from the PDP.
146 *
147 * @see PowerDistributionPanel#GetTotalEnergy
148 */
149void PowerDistributionPanel::ResetTotalEnergy() {
150 int32_t status = 0;
151
152 HAL_ResetPDPTotalEnergy(m_module, &status);
153
154 if (status) {
155 WPI_LIB_FATAL_ERROR(Timeout, "");
156 }
157}
158
159/**
160 * Remove all of the fault flags on the PDP.
161 */
162void PowerDistributionPanel::ClearStickyFaults() {
163 int32_t status = 0;
164
165 HAL_ClearPDPStickyFaults(m_module, &status);
166
167 if (status) {
168 WPI_LIB_FATAL_ERROR(Timeout, "");
169 }
170}