Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -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 "frc971/wpilib/ahal/PowerDistributionPanel.h" |
| 9 | |
| 10 | #include <sstream> |
| 11 | |
Austin Schuh | f6b9463 | 2019-02-02 22:11:27 -0800 | [diff] [blame] | 12 | #include "hal/HAL.h" |
| 13 | #include "hal/PDP.h" |
| 14 | #include "hal/Ports.h" |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 15 | #include "frc971/wpilib/ahal/WPIErrors.h" |
| 16 | |
| 17 | using namespace frc; |
| 18 | #define WPI_LIB_FATAL_ERROR(tag, msg) |
| 19 | |
| 20 | PowerDistributionPanel::PowerDistributionPanel() : PowerDistributionPanel(0) {} |
| 21 | |
| 22 | /** |
| 23 | * Initialize the PDP. |
| 24 | */ |
James Kuszmaul | 26a6d2b | 2019-12-18 21:16:57 -0800 | [diff] [blame^] | 25 | PowerDistributionPanel::PowerDistributionPanel(int module) { |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 26 | int32_t status = 0; |
James Kuszmaul | 26a6d2b | 2019-12-18 21:16:57 -0800 | [diff] [blame^] | 27 | m_handle = HAL_InitializePDP(module, &status); |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 28 | if (status != 0) { |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 29 | return; |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Query the input voltage of the PDP. |
| 35 | * |
| 36 | * @return The voltage of the PDP in volts |
| 37 | */ |
| 38 | double PowerDistributionPanel::GetVoltage() const { |
| 39 | int32_t status = 0; |
| 40 | |
James Kuszmaul | 26a6d2b | 2019-12-18 21:16:57 -0800 | [diff] [blame^] | 41 | double voltage = HAL_GetPDPVoltage(m_handle, &status); |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 42 | |
| 43 | if (status) { |
| 44 | WPI_LIB_FATAL_ERROR(Timeout, ""); |
| 45 | } |
| 46 | |
| 47 | return voltage; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Query the temperature of the PDP. |
| 52 | * |
| 53 | * @return The temperature of the PDP in degrees Celsius |
| 54 | */ |
| 55 | double PowerDistributionPanel::GetTemperature() const { |
| 56 | int32_t status = 0; |
| 57 | |
James Kuszmaul | 26a6d2b | 2019-12-18 21:16:57 -0800 | [diff] [blame^] | 58 | double temperature = HAL_GetPDPTemperature(m_handle, &status); |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 59 | |
| 60 | if (status) { |
| 61 | WPI_LIB_FATAL_ERROR(Timeout, ""); |
| 62 | } |
| 63 | |
| 64 | return temperature; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Query the current of a single channel of the PDP. |
| 69 | * |
| 70 | * @return The current of one of the PDP channels (channels 0-15) in Amperes |
| 71 | */ |
| 72 | double PowerDistributionPanel::GetCurrent(int channel) const { |
| 73 | int32_t status = 0; |
| 74 | |
| 75 | if (!CheckPDPChannel(channel)) { |
| 76 | std::stringstream buf; |
| 77 | buf << "PDP Channel " << channel; |
| 78 | WPI_LIB_FATAL_ERROR(ChannelIndexOutOfRange, buf.str()); |
| 79 | } |
| 80 | |
James Kuszmaul | 26a6d2b | 2019-12-18 21:16:57 -0800 | [diff] [blame^] | 81 | double current = HAL_GetPDPChannelCurrent(m_handle, channel, &status); |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 82 | |
| 83 | if (status) { |
| 84 | WPI_LIB_FATAL_ERROR(Timeout, ""); |
| 85 | } |
| 86 | |
| 87 | return current; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Query the total current of all monitored PDP channels (0-15). |
| 92 | * |
| 93 | * @return The the total current drawn from the PDP channels in Amperes |
| 94 | */ |
| 95 | double PowerDistributionPanel::GetTotalCurrent() const { |
| 96 | int32_t status = 0; |
| 97 | |
James Kuszmaul | 26a6d2b | 2019-12-18 21:16:57 -0800 | [diff] [blame^] | 98 | double current = HAL_GetPDPTotalCurrent(m_handle, &status); |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 99 | |
| 100 | if (status) { |
| 101 | WPI_LIB_FATAL_ERROR(Timeout, ""); |
| 102 | } |
| 103 | |
| 104 | return current; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Query the total power drawn from the monitored PDP channels. |
| 109 | * |
| 110 | * @return The the total power drawn from the PDP channels in Watts |
| 111 | */ |
| 112 | double PowerDistributionPanel::GetTotalPower() const { |
| 113 | int32_t status = 0; |
| 114 | |
James Kuszmaul | 26a6d2b | 2019-12-18 21:16:57 -0800 | [diff] [blame^] | 115 | double power = HAL_GetPDPTotalPower(m_handle, &status); |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 116 | |
| 117 | if (status) { |
| 118 | WPI_LIB_FATAL_ERROR(Timeout, ""); |
| 119 | } |
| 120 | |
| 121 | return power; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Query the total energy drawn from the monitored PDP channels. |
| 126 | * |
| 127 | * @return The the total energy drawn from the PDP channels in Joules |
| 128 | */ |
| 129 | double PowerDistributionPanel::GetTotalEnergy() const { |
| 130 | int32_t status = 0; |
| 131 | |
James Kuszmaul | 26a6d2b | 2019-12-18 21:16:57 -0800 | [diff] [blame^] | 132 | double energy = HAL_GetPDPTotalEnergy(m_handle, &status); |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 133 | |
| 134 | if (status) { |
| 135 | WPI_LIB_FATAL_ERROR(Timeout, ""); |
| 136 | } |
| 137 | |
| 138 | return energy; |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Reset the total energy drawn from the PDP. |
| 143 | * |
| 144 | * @see PowerDistributionPanel#GetTotalEnergy |
| 145 | */ |
| 146 | void PowerDistributionPanel::ResetTotalEnergy() { |
| 147 | int32_t status = 0; |
| 148 | |
James Kuszmaul | 26a6d2b | 2019-12-18 21:16:57 -0800 | [diff] [blame^] | 149 | HAL_ResetPDPTotalEnergy(m_handle, &status); |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 150 | |
| 151 | if (status) { |
| 152 | WPI_LIB_FATAL_ERROR(Timeout, ""); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Remove all of the fault flags on the PDP. |
| 158 | */ |
| 159 | void PowerDistributionPanel::ClearStickyFaults() { |
| 160 | int32_t status = 0; |
| 161 | |
James Kuszmaul | 26a6d2b | 2019-12-18 21:16:57 -0800 | [diff] [blame^] | 162 | HAL_ClearPDPStickyFaults(m_handle, &status); |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 163 | |
| 164 | if (status) { |
| 165 | WPI_LIB_FATAL_ERROR(Timeout, ""); |
| 166 | } |
| 167 | } |