Brian Silverman | f7f267a | 2017-02-04 16:16:08 -0800 | [diff] [blame^] | 1 | /*----------------------------------------------------------------------------*/ |
| 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 "PowerDistributionPanel.h" |
| 9 | |
| 10 | #include <sstream> |
| 11 | |
| 12 | #include "HAL/HAL.h" |
| 13 | #include "HAL/PDP.h" |
| 14 | #include "HAL/Ports.h" |
| 15 | #include "LiveWindow/LiveWindow.h" |
| 16 | #include "WPIErrors.h" |
| 17 | |
| 18 | using namespace frc; |
| 19 | |
| 20 | PowerDistributionPanel::PowerDistributionPanel() : PowerDistributionPanel(0) {} |
| 21 | |
| 22 | /** |
| 23 | * Initialize the PDP. |
| 24 | */ |
| 25 | PowerDistributionPanel::PowerDistributionPanel(int module) : m_module(module) { |
| 26 | int32_t status = 0; |
| 27 | HAL_InitializePDP(m_module, &status); |
| 28 | if (status != 0) { |
| 29 | wpi_setErrorWithContextRange(status, 0, HAL_GetNumPDPModules(), module, |
| 30 | HAL_GetErrorMessage(status)); |
| 31 | m_module = -1; |
| 32 | return; |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Query the input voltage of the PDP. |
| 38 | * |
| 39 | * @return The voltage of the PDP in volts |
| 40 | */ |
| 41 | double PowerDistributionPanel::GetVoltage() const { |
| 42 | if (StatusIsFatal()) return 0; |
| 43 | int32_t status = 0; |
| 44 | |
| 45 | double voltage = HAL_GetPDPVoltage(m_module, &status); |
| 46 | |
| 47 | if (status) { |
| 48 | wpi_setWPIErrorWithContext(Timeout, ""); |
| 49 | } |
| 50 | |
| 51 | return voltage; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Query the temperature of the PDP. |
| 56 | * |
| 57 | * @return The temperature of the PDP in degrees Celsius |
| 58 | */ |
| 59 | double PowerDistributionPanel::GetTemperature() const { |
| 60 | if (StatusIsFatal()) return 0; |
| 61 | int32_t status = 0; |
| 62 | |
| 63 | double temperature = HAL_GetPDPTemperature(m_module, &status); |
| 64 | |
| 65 | if (status) { |
| 66 | wpi_setWPIErrorWithContext(Timeout, ""); |
| 67 | } |
| 68 | |
| 69 | return temperature; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Query the current of a single channel of the PDP. |
| 74 | * |
| 75 | * @return The current of one of the PDP channels (channels 0-15) in Amperes |
| 76 | */ |
| 77 | double PowerDistributionPanel::GetCurrent(int channel) const { |
| 78 | if (StatusIsFatal()) return 0; |
| 79 | int32_t status = 0; |
| 80 | |
| 81 | if (!CheckPDPChannel(channel)) { |
| 82 | std::stringstream buf; |
| 83 | buf << "PDP Channel " << channel; |
| 84 | wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, buf.str()); |
| 85 | } |
| 86 | |
| 87 | double current = HAL_GetPDPChannelCurrent(m_module, channel, &status); |
| 88 | |
| 89 | if (status) { |
| 90 | wpi_setWPIErrorWithContext(Timeout, ""); |
| 91 | } |
| 92 | |
| 93 | return current; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Query the total current of all monitored PDP channels (0-15). |
| 98 | * |
| 99 | * @return The the total current drawn from the PDP channels in Amperes |
| 100 | */ |
| 101 | double PowerDistributionPanel::GetTotalCurrent() const { |
| 102 | if (StatusIsFatal()) return 0; |
| 103 | int32_t status = 0; |
| 104 | |
| 105 | double current = HAL_GetPDPTotalCurrent(m_module, &status); |
| 106 | |
| 107 | if (status) { |
| 108 | wpi_setWPIErrorWithContext(Timeout, ""); |
| 109 | } |
| 110 | |
| 111 | return current; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Query the total power drawn from the monitored PDP channels. |
| 116 | * |
| 117 | * @return The the total power drawn from the PDP channels in Watts |
| 118 | */ |
| 119 | double PowerDistributionPanel::GetTotalPower() const { |
| 120 | if (StatusIsFatal()) return 0; |
| 121 | int32_t status = 0; |
| 122 | |
| 123 | double power = HAL_GetPDPTotalPower(m_module, &status); |
| 124 | |
| 125 | if (status) { |
| 126 | wpi_setWPIErrorWithContext(Timeout, ""); |
| 127 | } |
| 128 | |
| 129 | return power; |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Query the total energy drawn from the monitored PDP channels. |
| 134 | * |
| 135 | * @return The the total energy drawn from the PDP channels in Joules |
| 136 | */ |
| 137 | double PowerDistributionPanel::GetTotalEnergy() const { |
| 138 | if (StatusIsFatal()) return 0; |
| 139 | int32_t status = 0; |
| 140 | |
| 141 | double energy = HAL_GetPDPTotalEnergy(m_module, &status); |
| 142 | |
| 143 | if (status) { |
| 144 | wpi_setWPIErrorWithContext(Timeout, ""); |
| 145 | } |
| 146 | |
| 147 | return energy; |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Reset the total energy drawn from the PDP. |
| 152 | * |
| 153 | * @see PowerDistributionPanel#GetTotalEnergy |
| 154 | */ |
| 155 | void PowerDistributionPanel::ResetTotalEnergy() { |
| 156 | if (StatusIsFatal()) return; |
| 157 | int32_t status = 0; |
| 158 | |
| 159 | HAL_ResetPDPTotalEnergy(m_module, &status); |
| 160 | |
| 161 | if (status) { |
| 162 | wpi_setWPIErrorWithContext(Timeout, ""); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Remove all of the fault flags on the PDP. |
| 168 | */ |
| 169 | void PowerDistributionPanel::ClearStickyFaults() { |
| 170 | if (StatusIsFatal()) return; |
| 171 | int32_t status = 0; |
| 172 | |
| 173 | HAL_ClearPDPStickyFaults(m_module, &status); |
| 174 | |
| 175 | if (status) { |
| 176 | wpi_setWPIErrorWithContext(Timeout, ""); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | void PowerDistributionPanel::UpdateTable() { |
| 181 | if (m_table != nullptr) { |
| 182 | m_table->PutNumber("Chan0", GetCurrent(0)); |
| 183 | m_table->PutNumber("Chan1", GetCurrent(1)); |
| 184 | m_table->PutNumber("Chan2", GetCurrent(2)); |
| 185 | m_table->PutNumber("Chan3", GetCurrent(3)); |
| 186 | m_table->PutNumber("Chan4", GetCurrent(4)); |
| 187 | m_table->PutNumber("Chan5", GetCurrent(5)); |
| 188 | m_table->PutNumber("Chan6", GetCurrent(6)); |
| 189 | m_table->PutNumber("Chan7", GetCurrent(7)); |
| 190 | m_table->PutNumber("Chan8", GetCurrent(8)); |
| 191 | m_table->PutNumber("Chan9", GetCurrent(9)); |
| 192 | m_table->PutNumber("Chan10", GetCurrent(10)); |
| 193 | m_table->PutNumber("Chan11", GetCurrent(11)); |
| 194 | m_table->PutNumber("Chan12", GetCurrent(12)); |
| 195 | m_table->PutNumber("Chan13", GetCurrent(13)); |
| 196 | m_table->PutNumber("Chan14", GetCurrent(14)); |
| 197 | m_table->PutNumber("Chan15", GetCurrent(15)); |
| 198 | m_table->PutNumber("Voltage", GetVoltage()); |
| 199 | m_table->PutNumber("TotalCurrent", GetTotalCurrent()); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | void PowerDistributionPanel::StartLiveWindowMode() {} |
| 204 | |
| 205 | void PowerDistributionPanel::StopLiveWindowMode() {} |
| 206 | |
| 207 | std::string PowerDistributionPanel::GetSmartDashboardType() const { |
| 208 | return "PowerDistributionPanel"; |
| 209 | } |
| 210 | |
| 211 | void PowerDistributionPanel::InitTable(std::shared_ptr<ITable> subTable) { |
| 212 | m_table = subTable; |
| 213 | UpdateTable(); |
| 214 | } |
| 215 | |
| 216 | std::shared_ptr<ITable> PowerDistributionPanel::GetTable() const { |
| 217 | return m_table; |
| 218 | } |