blob: 8359cdbab5aa42847003e0b3d1868e73bc45a5e0 [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
Austin Schuhf6b94632019-02-02 22:11:27 -080012#include "hal/HAL.h"
13#include "hal/PDP.h"
14#include "hal/Ports.h"
Parker Schuhd3b7a8872018-02-19 16:42:27 -080015#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 */
James Kuszmaul26a6d2b2019-12-18 21:16:57 -080025PowerDistributionPanel::PowerDistributionPanel(int module) {
Parker Schuhd3b7a8872018-02-19 16:42:27 -080026 int32_t status = 0;
James Kuszmaul26a6d2b2019-12-18 21:16:57 -080027 m_handle = HAL_InitializePDP(module, &status);
Parker Schuhd3b7a8872018-02-19 16:42:27 -080028 if (status != 0) {
Parker Schuhd3b7a8872018-02-19 16:42:27 -080029 return;
30 }
31}
32
33/**
34 * Query the input voltage of the PDP.
35 *
36 * @return The voltage of the PDP in volts
37 */
38double PowerDistributionPanel::GetVoltage() const {
39 int32_t status = 0;
40
James Kuszmaul26a6d2b2019-12-18 21:16:57 -080041 double voltage = HAL_GetPDPVoltage(m_handle, &status);
Parker Schuhd3b7a8872018-02-19 16:42:27 -080042
43 if (status) {
44 WPI_LIB_FATAL_ERROR(Timeout, "");
45 }
46
47 return voltage;
48}
49
50/**
51 * Query the temperature of the PDP.
52 *
53 * @return The temperature of the PDP in degrees Celsius
54 */
55double PowerDistributionPanel::GetTemperature() const {
56 int32_t status = 0;
57
James Kuszmaul26a6d2b2019-12-18 21:16:57 -080058 double temperature = HAL_GetPDPTemperature(m_handle, &status);
Parker Schuhd3b7a8872018-02-19 16:42:27 -080059
60 if (status) {
61 WPI_LIB_FATAL_ERROR(Timeout, "");
62 }
63
64 return temperature;
65}
66
67/**
68 * Query the current of a single channel of the PDP.
69 *
70 * @return The current of one of the PDP channels (channels 0-15) in Amperes
71 */
72double PowerDistributionPanel::GetCurrent(int channel) const {
73 int32_t status = 0;
74
75 if (!CheckPDPChannel(channel)) {
76 std::stringstream buf;
77 buf << "PDP Channel " << channel;
78 WPI_LIB_FATAL_ERROR(ChannelIndexOutOfRange, buf.str());
79 }
80
James Kuszmaul26a6d2b2019-12-18 21:16:57 -080081 double current = HAL_GetPDPChannelCurrent(m_handle, channel, &status);
Parker Schuhd3b7a8872018-02-19 16:42:27 -080082
83 if (status) {
84 WPI_LIB_FATAL_ERROR(Timeout, "");
85 }
86
87 return current;
88}
89
90/**
91 * Query the total current of all monitored PDP channels (0-15).
92 *
93 * @return The the total current drawn from the PDP channels in Amperes
94 */
95double PowerDistributionPanel::GetTotalCurrent() const {
96 int32_t status = 0;
97
James Kuszmaul26a6d2b2019-12-18 21:16:57 -080098 double current = HAL_GetPDPTotalCurrent(m_handle, &status);
Parker Schuhd3b7a8872018-02-19 16:42:27 -080099
100 if (status) {
101 WPI_LIB_FATAL_ERROR(Timeout, "");
102 }
103
104 return current;
105}
106
107/**
108 * Query the total power drawn from the monitored PDP channels.
109 *
110 * @return The the total power drawn from the PDP channels in Watts
111 */
112double PowerDistributionPanel::GetTotalPower() const {
113 int32_t status = 0;
114
James Kuszmaul26a6d2b2019-12-18 21:16:57 -0800115 double power = HAL_GetPDPTotalPower(m_handle, &status);
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800116
117 if (status) {
118 WPI_LIB_FATAL_ERROR(Timeout, "");
119 }
120
121 return power;
122}
123
124/**
125 * Query the total energy drawn from the monitored PDP channels.
126 *
127 * @return The the total energy drawn from the PDP channels in Joules
128 */
129double PowerDistributionPanel::GetTotalEnergy() const {
130 int32_t status = 0;
131
James Kuszmaul26a6d2b2019-12-18 21:16:57 -0800132 double energy = HAL_GetPDPTotalEnergy(m_handle, &status);
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800133
134 if (status) {
135 WPI_LIB_FATAL_ERROR(Timeout, "");
136 }
137
138 return energy;
139}
140
141/**
142 * Reset the total energy drawn from the PDP.
143 *
144 * @see PowerDistributionPanel#GetTotalEnergy
145 */
146void PowerDistributionPanel::ResetTotalEnergy() {
147 int32_t status = 0;
148
James Kuszmaul26a6d2b2019-12-18 21:16:57 -0800149 HAL_ResetPDPTotalEnergy(m_handle, &status);
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800150
151 if (status) {
152 WPI_LIB_FATAL_ERROR(Timeout, "");
153 }
154}
155
156/**
157 * Remove all of the fault flags on the PDP.
158 */
159void PowerDistributionPanel::ClearStickyFaults() {
160 int32_t status = 0;
161
James Kuszmaul26a6d2b2019-12-18 21:16:57 -0800162 HAL_ClearPDPStickyFaults(m_handle, &status);
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800163
164 if (status) {
165 WPI_LIB_FATAL_ERROR(Timeout, "");
166 }
167}