Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 1 | // Copyright (c) FIRST and other WPILib contributors. |
| 2 | // Open Source Software; you can modify and/or share it under the terms of |
| 3 | // the WPILib BSD license file in the root directory of this project. |
| 4 | |
| 5 | #include "frc/PowerDistribution.h" |
| 6 | |
| 7 | #include <fmt/format.h> |
| 8 | #include <hal/FRCUsageReporting.h> |
| 9 | #include <hal/Ports.h> |
| 10 | #include <hal/PowerDistribution.h> |
| 11 | #include <wpi/StackTrace.h> |
| 12 | #include <wpi/sendable/SendableBuilder.h> |
| 13 | #include <wpi/sendable/SendableRegistry.h> |
| 14 | |
| 15 | #include "frc/Errors.h" |
| 16 | #include "frc/SensorUtil.h" |
| 17 | |
| 18 | static_assert(static_cast<HAL_PowerDistributionType>( |
| 19 | frc::PowerDistribution::ModuleType::kAutomatic) == |
| 20 | HAL_PowerDistributionType::HAL_PowerDistributionType_kAutomatic); |
| 21 | static_assert(static_cast<HAL_PowerDistributionType>( |
| 22 | frc::PowerDistribution::ModuleType::kCTRE) == |
| 23 | HAL_PowerDistributionType::HAL_PowerDistributionType_kCTRE); |
| 24 | static_assert(static_cast<HAL_PowerDistributionType>( |
| 25 | frc::PowerDistribution::ModuleType::kRev) == |
| 26 | HAL_PowerDistributionType::HAL_PowerDistributionType_kRev); |
| 27 | static_assert(frc::PowerDistribution::kDefaultModule == |
| 28 | HAL_DEFAULT_POWER_DISTRIBUTION_MODULE); |
| 29 | |
| 30 | using namespace frc; |
| 31 | |
| 32 | PowerDistribution::PowerDistribution() |
| 33 | : PowerDistribution(-1, ModuleType::kAutomatic) {} |
| 34 | |
| 35 | PowerDistribution::PowerDistribution(int module, ModuleType moduleType) { |
| 36 | auto stack = wpi::GetStackTrace(1); |
| 37 | |
| 38 | int32_t status = 0; |
| 39 | m_handle = HAL_InitializePowerDistribution( |
| 40 | module, static_cast<HAL_PowerDistributionType>(moduleType), stack.c_str(), |
| 41 | &status); |
| 42 | FRC_CheckErrorStatus(status, "Module {}", module); |
| 43 | m_module = HAL_GetPowerDistributionModuleNumber(m_handle, &status); |
| 44 | FRC_ReportError(status, "Module {}", module); |
| 45 | |
| 46 | HAL_Report(HALUsageReporting::kResourceType_PDP, m_module + 1); |
| 47 | wpi::SendableRegistry::AddLW(this, "PowerDistribution", m_module); |
| 48 | } |
| 49 | |
| 50 | PowerDistribution::~PowerDistribution() { |
| 51 | if (m_handle != HAL_kInvalidHandle) { |
| 52 | HAL_CleanPowerDistribution(m_handle); |
| 53 | m_handle = HAL_kInvalidHandle; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | double PowerDistribution::GetVoltage() const { |
| 58 | int32_t status = 0; |
| 59 | double voltage = HAL_GetPowerDistributionVoltage(m_handle, &status); |
| 60 | FRC_ReportError(status, "Module {}", m_module); |
| 61 | return voltage; |
| 62 | } |
| 63 | |
| 64 | double PowerDistribution::GetTemperature() const { |
| 65 | int32_t status = 0; |
| 66 | double temperature = HAL_GetPowerDistributionTemperature(m_handle, &status); |
| 67 | FRC_ReportError(status, "Module {}", m_module); |
| 68 | return temperature; |
| 69 | } |
| 70 | |
| 71 | double PowerDistribution::GetCurrent(int channel) const { |
| 72 | int32_t status = 0; |
| 73 | double current = |
| 74 | HAL_GetPowerDistributionChannelCurrent(m_handle, channel, &status); |
| 75 | FRC_ReportError(status, "Module {} Channel {}", m_module, channel); |
| 76 | |
| 77 | return current; |
| 78 | } |
| 79 | |
| 80 | double PowerDistribution::GetTotalCurrent() const { |
| 81 | int32_t status = 0; |
| 82 | double current = HAL_GetPowerDistributionTotalCurrent(m_handle, &status); |
| 83 | FRC_ReportError(status, "Module {}", m_module); |
| 84 | return current; |
| 85 | } |
| 86 | |
| 87 | double PowerDistribution::GetTotalPower() const { |
| 88 | int32_t status = 0; |
| 89 | double power = HAL_GetPowerDistributionTotalPower(m_handle, &status); |
| 90 | FRC_ReportError(status, "Module {}", m_module); |
| 91 | return power; |
| 92 | } |
| 93 | |
| 94 | double PowerDistribution::GetTotalEnergy() const { |
| 95 | int32_t status = 0; |
| 96 | double energy = HAL_GetPowerDistributionTotalEnergy(m_handle, &status); |
| 97 | FRC_ReportError(status, "Module {}", m_module); |
| 98 | return energy; |
| 99 | } |
| 100 | |
| 101 | void PowerDistribution::ResetTotalEnergy() { |
| 102 | int32_t status = 0; |
| 103 | HAL_ResetPowerDistributionTotalEnergy(m_handle, &status); |
| 104 | FRC_ReportError(status, "Module {}", m_module); |
| 105 | } |
| 106 | |
| 107 | void PowerDistribution::ClearStickyFaults() { |
| 108 | int32_t status = 0; |
| 109 | HAL_ClearPowerDistributionStickyFaults(m_handle, &status); |
| 110 | FRC_ReportError(status, "Module {}", m_module); |
| 111 | } |
| 112 | |
| 113 | int PowerDistribution::GetModule() const { |
| 114 | return m_module; |
| 115 | } |
| 116 | |
| 117 | bool PowerDistribution::GetSwitchableChannel() const { |
| 118 | int32_t status = 0; |
| 119 | bool state = HAL_GetPowerDistributionSwitchableChannel(m_handle, &status); |
| 120 | FRC_ReportError(status, "Module {}", m_module); |
| 121 | return state; |
| 122 | } |
| 123 | |
| 124 | void PowerDistribution::SetSwitchableChannel(bool enabled) { |
| 125 | int32_t status = 0; |
| 126 | HAL_SetPowerDistributionSwitchableChannel(m_handle, enabled, &status); |
| 127 | FRC_ReportError(status, "Module {}", m_module); |
| 128 | } |
| 129 | |
| 130 | void PowerDistribution::InitSendable(wpi::SendableBuilder& builder) { |
| 131 | builder.SetSmartDashboardType("PowerDistribution"); |
| 132 | int32_t status = 0; |
| 133 | int numChannels = HAL_GetPowerDistributionNumChannels(m_handle, &status); |
| 134 | FRC_ReportError(status, "Module {}", m_module); |
| 135 | for (int i = 0; i < numChannels; ++i) { |
| 136 | builder.AddDoubleProperty( |
| 137 | fmt::format("Chan{}", i), [=] { return GetCurrent(i); }, nullptr); |
| 138 | } |
| 139 | builder.AddDoubleProperty( |
| 140 | "Voltage", [=] { return GetVoltage(); }, nullptr); |
| 141 | builder.AddDoubleProperty( |
| 142 | "TotalCurrent", [=] { return GetTotalCurrent(); }, nullptr); |
| 143 | builder.AddBooleanProperty( |
| 144 | "SwitchableChannel", [=] { return GetSwitchableChannel(); }, |
| 145 | [=](bool value) { SetSwitchableChannel(value); }); |
| 146 | } |