blob: 7d7d9164a895ea98b9b695482fdd3dd21d744648 [file] [log] [blame]
Brian Silverman41cdd3e2019-01-19 19:48:58 -08001/*----------------------------------------------------------------------------*/
James Kuszmaul4f3ad3c2019-12-01 16:35:21 -08002/* Copyright (c) 2014-2019 FIRST. All Rights Reserved. */
Brian Silverman41cdd3e2019-01-19 19:48:58 -08003/* 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 "frc/PowerDistributionPanel.h"
9
James Kuszmaul4b81d302019-12-14 20:53:14 -080010#include <hal/FRCUsageReporting.h>
Brian Silverman41cdd3e2019-01-19 19:48:58 -080011#include <hal/PDP.h>
12#include <hal/Ports.h>
13#include <wpi/SmallString.h>
14#include <wpi/raw_ostream.h>
15
16#include "frc/SensorUtil.h"
17#include "frc/WPIErrors.h"
18#include "frc/smartdashboard/SendableBuilder.h"
James Kuszmaul4f3ad3c2019-12-01 16:35:21 -080019#include "frc/smartdashboard/SendableRegistry.h"
Brian Silverman41cdd3e2019-01-19 19:48:58 -080020
21using namespace frc;
22
23PowerDistributionPanel::PowerDistributionPanel() : PowerDistributionPanel(0) {}
24
25/**
26 * Initialize the PDP.
27 */
28PowerDistributionPanel::PowerDistributionPanel(int module) {
29 int32_t status = 0;
30 m_handle = HAL_InitializePDP(module, &status);
31 if (status != 0) {
James Kuszmaul4b81d302019-12-14 20:53:14 -080032 wpi_setHALErrorWithRange(status, 0, HAL_GetNumPDPModules(), module);
Brian Silverman41cdd3e2019-01-19 19:48:58 -080033 return;
34 }
35
James Kuszmaul4b81d302019-12-14 20:53:14 -080036 HAL_Report(HALUsageReporting::kResourceType_PDP, module + 1);
James Kuszmaul4f3ad3c2019-12-01 16:35:21 -080037 SendableRegistry::GetInstance().AddLW(this, "PowerDistributionPanel", module);
Brian Silverman41cdd3e2019-01-19 19:48:58 -080038}
39
40double PowerDistributionPanel::GetVoltage() const {
41 int32_t status = 0;
42
43 double voltage = HAL_GetPDPVoltage(m_handle, &status);
44
45 if (status) {
46 wpi_setWPIErrorWithContext(Timeout, "");
47 }
48
49 return voltage;
50}
51
52double PowerDistributionPanel::GetTemperature() const {
53 int32_t status = 0;
54
55 double temperature = HAL_GetPDPTemperature(m_handle, &status);
56
57 if (status) {
58 wpi_setWPIErrorWithContext(Timeout, "");
59 }
60
61 return temperature;
62}
63
64double PowerDistributionPanel::GetCurrent(int channel) const {
65 int32_t status = 0;
66
67 if (!SensorUtil::CheckPDPChannel(channel)) {
68 wpi::SmallString<32> str;
69 wpi::raw_svector_ostream buf(str);
70 buf << "PDP Channel " << channel;
71 wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, buf.str());
72 }
73
74 double current = HAL_GetPDPChannelCurrent(m_handle, channel, &status);
75
76 if (status) {
77 wpi_setWPIErrorWithContext(Timeout, "");
78 }
79
80 return current;
81}
82
83double PowerDistributionPanel::GetTotalCurrent() const {
84 int32_t status = 0;
85
86 double current = HAL_GetPDPTotalCurrent(m_handle, &status);
87
88 if (status) {
89 wpi_setWPIErrorWithContext(Timeout, "");
90 }
91
92 return current;
93}
94
95double PowerDistributionPanel::GetTotalPower() const {
96 int32_t status = 0;
97
98 double power = HAL_GetPDPTotalPower(m_handle, &status);
99
100 if (status) {
101 wpi_setWPIErrorWithContext(Timeout, "");
102 }
103
104 return power;
105}
106
107double PowerDistributionPanel::GetTotalEnergy() const {
108 int32_t status = 0;
109
110 double energy = HAL_GetPDPTotalEnergy(m_handle, &status);
111
112 if (status) {
113 wpi_setWPIErrorWithContext(Timeout, "");
114 }
115
116 return energy;
117}
118
119void PowerDistributionPanel::ResetTotalEnergy() {
120 int32_t status = 0;
121
122 HAL_ResetPDPTotalEnergy(m_handle, &status);
123
124 if (status) {
125 wpi_setWPIErrorWithContext(Timeout, "");
126 }
127}
128
129void PowerDistributionPanel::ClearStickyFaults() {
130 int32_t status = 0;
131
132 HAL_ClearPDPStickyFaults(m_handle, &status);
133
134 if (status) {
135 wpi_setWPIErrorWithContext(Timeout, "");
136 }
137}
138
139void PowerDistributionPanel::InitSendable(SendableBuilder& builder) {
140 builder.SetSmartDashboardType("PowerDistributionPanel");
141 for (int i = 0; i < SensorUtil::kPDPChannels; ++i) {
142 builder.AddDoubleProperty("Chan" + wpi::Twine(i),
143 [=]() { return GetCurrent(i); }, nullptr);
144 }
145 builder.AddDoubleProperty("Voltage", [=]() { return GetVoltage(); }, nullptr);
146 builder.AddDoubleProperty("TotalCurrent", [=]() { return GetTotalCurrent(); },
147 nullptr);
148}