blob: 39087fbb284d1f268860ba7a57bcf8f1d0bb87de [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"
Austin Schuh9950f682021-11-06 15:27:58 -070013#include "hal/PowerDistribution.h"
Austin Schuhf6b94632019-02-02 22:11:27 -080014#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;
Austin Schuh9950f682021-11-06 15:27:58 -070027 m_handle = HAL_InitializePowerDistribution(
28 module, HAL_PowerDistributionType::HAL_PowerDistributionType_kCTRE,
29 nullptr, &status);
Parker Schuhd3b7a8872018-02-19 16:42:27 -080030 if (status != 0) {
Parker Schuhd3b7a8872018-02-19 16:42:27 -080031 return;
32 }
33}
34
35/**
36 * Query the input voltage of the PDP.
37 *
38 * @return The voltage of the PDP in volts
39 */
40double PowerDistributionPanel::GetVoltage() const {
41 int32_t status = 0;
42
Austin Schuh9950f682021-11-06 15:27:58 -070043 double voltage = HAL_GetPowerDistributionVoltage(m_handle, &status);
Parker Schuhd3b7a8872018-02-19 16:42:27 -080044
45 if (status) {
46 WPI_LIB_FATAL_ERROR(Timeout, "");
47 }
48
49 return voltage;
50}
51
52/**
53 * Query the temperature of the PDP.
54 *
55 * @return The temperature of the PDP in degrees Celsius
56 */
57double PowerDistributionPanel::GetTemperature() const {
58 int32_t status = 0;
59
Austin Schuh9950f682021-11-06 15:27:58 -070060 double temperature = HAL_GetPowerDistributionTemperature(m_handle, &status);
Parker Schuhd3b7a8872018-02-19 16:42:27 -080061
62 if (status) {
63 WPI_LIB_FATAL_ERROR(Timeout, "");
64 }
65
66 return temperature;
67}
68
69/**
70 * Query the current of a single channel of the PDP.
71 *
72 * @return The current of one of the PDP channels (channels 0-15) in Amperes
73 */
74double PowerDistributionPanel::GetCurrent(int channel) const {
75 int32_t status = 0;
76
Austin Schuh9950f682021-11-06 15:27:58 -070077 if (!CheckPDPChannel(
78 channel,
79 HAL_PowerDistributionType::HAL_PowerDistributionType_kCTRE)) {
Parker Schuhd3b7a8872018-02-19 16:42:27 -080080 std::stringstream buf;
81 buf << "PDP Channel " << channel;
82 WPI_LIB_FATAL_ERROR(ChannelIndexOutOfRange, buf.str());
83 }
84
Austin Schuh9950f682021-11-06 15:27:58 -070085 double current =
86 HAL_GetPowerDistributionChannelCurrent(m_handle, channel, &status);
Parker Schuhd3b7a8872018-02-19 16:42:27 -080087
88 if (status) {
89 WPI_LIB_FATAL_ERROR(Timeout, "");
90 }
91
92 return current;
93}
94
95/**
96 * Query the total current of all monitored PDP channels (0-15).
97 *
98 * @return The the total current drawn from the PDP channels in Amperes
99 */
100double PowerDistributionPanel::GetTotalCurrent() const {
101 int32_t status = 0;
102
Austin Schuh9950f682021-11-06 15:27:58 -0700103 double current = HAL_GetPowerDistributionTotalCurrent(m_handle, &status);
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800104
105 if (status) {
106 WPI_LIB_FATAL_ERROR(Timeout, "");
107 }
108
109 return current;
110}
111
112/**
113 * Query the total power drawn from the monitored PDP channels.
114 *
115 * @return The the total power drawn from the PDP channels in Watts
116 */
117double PowerDistributionPanel::GetTotalPower() const {
118 int32_t status = 0;
119
Austin Schuh9950f682021-11-06 15:27:58 -0700120 double power = HAL_GetPowerDistributionTotalPower(m_handle, &status);
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800121
122 if (status) {
123 WPI_LIB_FATAL_ERROR(Timeout, "");
124 }
125
126 return power;
127}
128
129/**
130 * Query the total energy drawn from the monitored PDP channels.
131 *
132 * @return The the total energy drawn from the PDP channels in Joules
133 */
134double PowerDistributionPanel::GetTotalEnergy() const {
135 int32_t status = 0;
136
Austin Schuh9950f682021-11-06 15:27:58 -0700137 double energy = HAL_GetPowerDistributionTotalEnergy(m_handle, &status);
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800138
139 if (status) {
140 WPI_LIB_FATAL_ERROR(Timeout, "");
141 }
142
143 return energy;
144}
145
146/**
147 * Reset the total energy drawn from the PDP.
148 *
149 * @see PowerDistributionPanel#GetTotalEnergy
150 */
151void PowerDistributionPanel::ResetTotalEnergy() {
152 int32_t status = 0;
153
Austin Schuh9950f682021-11-06 15:27:58 -0700154 HAL_ResetPowerDistributionTotalEnergy(m_handle, &status);
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800155
156 if (status) {
157 WPI_LIB_FATAL_ERROR(Timeout, "");
158 }
159}
160
161/**
162 * Remove all of the fault flags on the PDP.
163 */
164void PowerDistributionPanel::ClearStickyFaults() {
165 int32_t status = 0;
166
Austin Schuh9950f682021-11-06 15:27:58 -0700167 HAL_ClearPowerDistributionStickyFaults(m_handle, &status);
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800168
169 if (status) {
170 WPI_LIB_FATAL_ERROR(Timeout, "");
171 }
172}