Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 1 | /*----------------------------------------------------------------------------*/ |
James Kuszmaul | 4f3ad3c | 2019-12-01 16:35:21 -0800 | [diff] [blame] | 2 | /* Copyright (c) 2014-2019 FIRST. All Rights Reserved. */ |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 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 "frc/PowerDistributionPanel.h" |
| 9 | |
James Kuszmaul | 4b81d30 | 2019-12-14 20:53:14 -0800 | [diff] [blame] | 10 | #include <hal/FRCUsageReporting.h> |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 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 Kuszmaul | 4f3ad3c | 2019-12-01 16:35:21 -0800 | [diff] [blame] | 19 | #include "frc/smartdashboard/SendableRegistry.h" |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 20 | |
| 21 | using namespace frc; |
| 22 | |
| 23 | PowerDistributionPanel::PowerDistributionPanel() : PowerDistributionPanel(0) {} |
| 24 | |
| 25 | /** |
| 26 | * Initialize the PDP. |
| 27 | */ |
| 28 | PowerDistributionPanel::PowerDistributionPanel(int module) { |
| 29 | int32_t status = 0; |
| 30 | m_handle = HAL_InitializePDP(module, &status); |
| 31 | if (status != 0) { |
James Kuszmaul | 4b81d30 | 2019-12-14 20:53:14 -0800 | [diff] [blame] | 32 | wpi_setHALErrorWithRange(status, 0, HAL_GetNumPDPModules(), module); |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 33 | return; |
| 34 | } |
| 35 | |
James Kuszmaul | 4b81d30 | 2019-12-14 20:53:14 -0800 | [diff] [blame] | 36 | HAL_Report(HALUsageReporting::kResourceType_PDP, module + 1); |
James Kuszmaul | 4f3ad3c | 2019-12-01 16:35:21 -0800 | [diff] [blame] | 37 | SendableRegistry::GetInstance().AddLW(this, "PowerDistributionPanel", module); |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | double 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 | |
| 52 | double 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 | |
| 64 | double 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 | |
| 83 | double 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 | |
| 95 | double 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 | |
| 107 | double 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 | |
| 119 | void 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 | |
| 129 | void 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 | |
| 139 | void 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 | } |