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