blob: b3297ea98605e3f2ba5c8307b17cbbaa70147258 [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
10#include <hal/HAL.h>
11#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) {
32 wpi_setErrorWithContextRange(status, 0, HAL_GetNumPDPModules(), module,
33 HAL_GetErrorMessage(status));
34 return;
35 }
36
37 HAL_Report(HALUsageReporting::kResourceType_PDP, module);
James Kuszmaul4f3ad3c2019-12-01 16:35:21 -080038 SendableRegistry::GetInstance().AddLW(this, "PowerDistributionPanel", module);
Brian Silverman41cdd3e2019-01-19 19:48:58 -080039}
40
41double PowerDistributionPanel::GetVoltage() const {
42 int32_t status = 0;
43
44 double voltage = HAL_GetPDPVoltage(m_handle, &status);
45
46 if (status) {
47 wpi_setWPIErrorWithContext(Timeout, "");
48 }
49
50 return voltage;
51}
52
53double PowerDistributionPanel::GetTemperature() const {
54 int32_t status = 0;
55
56 double temperature = HAL_GetPDPTemperature(m_handle, &status);
57
58 if (status) {
59 wpi_setWPIErrorWithContext(Timeout, "");
60 }
61
62 return temperature;
63}
64
65double PowerDistributionPanel::GetCurrent(int channel) const {
66 int32_t status = 0;
67
68 if (!SensorUtil::CheckPDPChannel(channel)) {
69 wpi::SmallString<32> str;
70 wpi::raw_svector_ostream buf(str);
71 buf << "PDP Channel " << channel;
72 wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, buf.str());
73 }
74
75 double current = HAL_GetPDPChannelCurrent(m_handle, channel, &status);
76
77 if (status) {
78 wpi_setWPIErrorWithContext(Timeout, "");
79 }
80
81 return current;
82}
83
84double PowerDistributionPanel::GetTotalCurrent() const {
85 int32_t status = 0;
86
87 double current = HAL_GetPDPTotalCurrent(m_handle, &status);
88
89 if (status) {
90 wpi_setWPIErrorWithContext(Timeout, "");
91 }
92
93 return current;
94}
95
96double PowerDistributionPanel::GetTotalPower() const {
97 int32_t status = 0;
98
99 double power = HAL_GetPDPTotalPower(m_handle, &status);
100
101 if (status) {
102 wpi_setWPIErrorWithContext(Timeout, "");
103 }
104
105 return power;
106}
107
108double PowerDistributionPanel::GetTotalEnergy() const {
109 int32_t status = 0;
110
111 double energy = HAL_GetPDPTotalEnergy(m_handle, &status);
112
113 if (status) {
114 wpi_setWPIErrorWithContext(Timeout, "");
115 }
116
117 return energy;
118}
119
120void PowerDistributionPanel::ResetTotalEnergy() {
121 int32_t status = 0;
122
123 HAL_ResetPDPTotalEnergy(m_handle, &status);
124
125 if (status) {
126 wpi_setWPIErrorWithContext(Timeout, "");
127 }
128}
129
130void PowerDistributionPanel::ClearStickyFaults() {
131 int32_t status = 0;
132
133 HAL_ClearPDPStickyFaults(m_handle, &status);
134
135 if (status) {
136 wpi_setWPIErrorWithContext(Timeout, "");
137 }
138}
139
140void PowerDistributionPanel::InitSendable(SendableBuilder& builder) {
141 builder.SetSmartDashboardType("PowerDistributionPanel");
142 for (int i = 0; i < SensorUtil::kPDPChannels; ++i) {
143 builder.AddDoubleProperty("Chan" + wpi::Twine(i),
144 [=]() { return GetCurrent(i); }, nullptr);
145 }
146 builder.AddDoubleProperty("Voltage", [=]() { return GetVoltage(); }, nullptr);
147 builder.AddDoubleProperty("TotalCurrent", [=]() { return GetTotalCurrent(); },
148 nullptr);
149}