mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-26 01:51:41 +00:00
[hal] [wpilib] Add initial support for the REV PDH (#3503)
This commit is contained in:
146
wpilibc/src/main/native/cpp/PowerDistribution.cpp
Normal file
146
wpilibc/src/main/native/cpp/PowerDistribution.cpp
Normal file
@@ -0,0 +1,146 @@
|
||||
// Copyright (c) FIRST and other WPILib contributors.
|
||||
// Open Source Software; you can modify and/or share it under the terms of
|
||||
// the WPILib BSD license file in the root directory of this project.
|
||||
|
||||
#include "frc/PowerDistribution.h"
|
||||
|
||||
#include <fmt/format.h>
|
||||
#include <hal/FRCUsageReporting.h>
|
||||
#include <hal/Ports.h>
|
||||
#include <hal/PowerDistribution.h>
|
||||
#include <wpi/StackTrace.h>
|
||||
#include <wpi/sendable/SendableBuilder.h>
|
||||
#include <wpi/sendable/SendableRegistry.h>
|
||||
|
||||
#include "frc/Errors.h"
|
||||
#include "frc/SensorUtil.h"
|
||||
|
||||
static_assert(static_cast<HAL_PowerDistributionType>(
|
||||
frc::PowerDistribution::ModuleType::kAutomatic) ==
|
||||
HAL_PowerDistributionType::HAL_PowerDistributionType_kAutomatic);
|
||||
static_assert(static_cast<HAL_PowerDistributionType>(
|
||||
frc::PowerDistribution::ModuleType::kCTRE) ==
|
||||
HAL_PowerDistributionType::HAL_PowerDistributionType_kCTRE);
|
||||
static_assert(static_cast<HAL_PowerDistributionType>(
|
||||
frc::PowerDistribution::ModuleType::kRev) ==
|
||||
HAL_PowerDistributionType::HAL_PowerDistributionType_kRev);
|
||||
static_assert(frc::PowerDistribution::kDefaultModule ==
|
||||
HAL_DEFAULT_POWER_DISTRIBUTION_MODULE);
|
||||
|
||||
using namespace frc;
|
||||
|
||||
PowerDistribution::PowerDistribution()
|
||||
: PowerDistribution(-1, ModuleType::kAutomatic) {}
|
||||
|
||||
PowerDistribution::PowerDistribution(int module, ModuleType moduleType) {
|
||||
auto stack = wpi::GetStackTrace(1);
|
||||
|
||||
int32_t status = 0;
|
||||
m_handle = HAL_InitializePowerDistribution(
|
||||
module, static_cast<HAL_PowerDistributionType>(moduleType), stack.c_str(),
|
||||
&status);
|
||||
FRC_CheckErrorStatus(status, "Module {}", module);
|
||||
m_module = HAL_GetPowerDistributionModuleNumber(m_handle, &status);
|
||||
FRC_ReportError(status, "Module {}", module);
|
||||
|
||||
HAL_Report(HALUsageReporting::kResourceType_PDP, m_module + 1);
|
||||
wpi::SendableRegistry::AddLW(this, "PowerDistribution", m_module);
|
||||
}
|
||||
|
||||
PowerDistribution::~PowerDistribution() {
|
||||
if (m_handle != HAL_kInvalidHandle) {
|
||||
HAL_CleanPowerDistribution(m_handle);
|
||||
m_handle = HAL_kInvalidHandle;
|
||||
}
|
||||
}
|
||||
|
||||
double PowerDistribution::GetVoltage() const {
|
||||
int32_t status = 0;
|
||||
double voltage = HAL_GetPowerDistributionVoltage(m_handle, &status);
|
||||
FRC_ReportError(status, "Module {}", m_module);
|
||||
return voltage;
|
||||
}
|
||||
|
||||
double PowerDistribution::GetTemperature() const {
|
||||
int32_t status = 0;
|
||||
double temperature = HAL_GetPowerDistributionTemperature(m_handle, &status);
|
||||
FRC_ReportError(status, "Module {}", m_module);
|
||||
return temperature;
|
||||
}
|
||||
|
||||
double PowerDistribution::GetCurrent(int channel) const {
|
||||
int32_t status = 0;
|
||||
double current =
|
||||
HAL_GetPowerDistributionChannelCurrent(m_handle, channel, &status);
|
||||
FRC_ReportError(status, "Module {} Channel {}", m_module, channel);
|
||||
|
||||
return current;
|
||||
}
|
||||
|
||||
double PowerDistribution::GetTotalCurrent() const {
|
||||
int32_t status = 0;
|
||||
double current = HAL_GetPowerDistributionTotalCurrent(m_handle, &status);
|
||||
FRC_ReportError(status, "Module {}", m_module);
|
||||
return current;
|
||||
}
|
||||
|
||||
double PowerDistribution::GetTotalPower() const {
|
||||
int32_t status = 0;
|
||||
double power = HAL_GetPowerDistributionTotalPower(m_handle, &status);
|
||||
FRC_ReportError(status, "Module {}", m_module);
|
||||
return power;
|
||||
}
|
||||
|
||||
double PowerDistribution::GetTotalEnergy() const {
|
||||
int32_t status = 0;
|
||||
double energy = HAL_GetPowerDistributionTotalEnergy(m_handle, &status);
|
||||
FRC_ReportError(status, "Module {}", m_module);
|
||||
return energy;
|
||||
}
|
||||
|
||||
void PowerDistribution::ResetTotalEnergy() {
|
||||
int32_t status = 0;
|
||||
HAL_ResetPowerDistributionTotalEnergy(m_handle, &status);
|
||||
FRC_ReportError(status, "Module {}", m_module);
|
||||
}
|
||||
|
||||
void PowerDistribution::ClearStickyFaults() {
|
||||
int32_t status = 0;
|
||||
HAL_ClearPowerDistributionStickyFaults(m_handle, &status);
|
||||
FRC_ReportError(status, "Module {}", m_module);
|
||||
}
|
||||
|
||||
int PowerDistribution::GetModule() const {
|
||||
return m_module;
|
||||
}
|
||||
|
||||
bool PowerDistribution::GetSwitchableChannel() const {
|
||||
int32_t status = 0;
|
||||
bool state = HAL_GetPowerDistributionSwitchableChannel(m_handle, &status);
|
||||
FRC_ReportError(status, "Module {}", m_module);
|
||||
return state;
|
||||
}
|
||||
|
||||
void PowerDistribution::SetSwitchableChannel(bool enabled) {
|
||||
int32_t status = 0;
|
||||
HAL_SetPowerDistributionSwitchableChannel(m_handle, enabled, &status);
|
||||
FRC_ReportError(status, "Module {}", m_module);
|
||||
}
|
||||
|
||||
void PowerDistribution::InitSendable(wpi::SendableBuilder& builder) {
|
||||
builder.SetSmartDashboardType("PowerDistribution");
|
||||
int32_t status = 0;
|
||||
int numChannels = HAL_GetPowerDistributionNumChannels(m_handle, &status);
|
||||
FRC_ReportError(status, "Module {}", m_module);
|
||||
for (int i = 0; i < numChannels; ++i) {
|
||||
builder.AddDoubleProperty(
|
||||
fmt::format("Chan{}", i), [=] { return GetCurrent(i); }, nullptr);
|
||||
}
|
||||
builder.AddDoubleProperty(
|
||||
"Voltage", [=] { return GetVoltage(); }, nullptr);
|
||||
builder.AddDoubleProperty(
|
||||
"TotalCurrent", [=] { return GetTotalCurrent(); }, nullptr);
|
||||
builder.AddBooleanProperty(
|
||||
"SwitchableChannel", [=] { return GetSwitchableChannel(); },
|
||||
[=](bool value) { SetSwitchableChannel(value); });
|
||||
}
|
||||
@@ -1,108 +0,0 @@
|
||||
// Copyright (c) FIRST and other WPILib contributors.
|
||||
// Open Source Software; you can modify and/or share it under the terms of
|
||||
// the WPILib BSD license file in the root directory of this project.
|
||||
|
||||
#include <fmt/format.h>
|
||||
#include <hal/FRCUsageReporting.h>
|
||||
#include <hal/Ports.h>
|
||||
#include <hal/PowerDistribution.h>
|
||||
#include <wpi/sendable/SendableBuilder.h>
|
||||
#include <wpi/sendable/SendableRegistry.h>
|
||||
|
||||
#include "frc/Errors.h"
|
||||
#include "frc/PowerDistribution.h"
|
||||
#include "frc/SensorUtil.h"
|
||||
|
||||
using namespace frc;
|
||||
|
||||
PowerDistribution::PowerDistribution() : PowerDistribution(0) {}
|
||||
|
||||
PowerDistribution::PowerDistribution(int module) : m_module(module) {
|
||||
int32_t status = 0;
|
||||
m_handle = HAL_InitializePowerDistribution(
|
||||
module, HAL_PowerDistributionType::HAL_PowerDistributionType_kAutomatic,
|
||||
&status);
|
||||
FRC_CheckErrorStatus(status, "Module {}", module);
|
||||
|
||||
HAL_Report(HALUsageReporting::kResourceType_PDP, module + 1);
|
||||
wpi::SendableRegistry::AddLW(this, "PowerDistribution", module);
|
||||
}
|
||||
|
||||
double PowerDistribution::GetVoltage() const {
|
||||
int32_t status = 0;
|
||||
double voltage = HAL_GetPowerDistributionVoltage(m_handle, &status);
|
||||
FRC_CheckErrorStatus(status, "Module {}", m_module);
|
||||
return voltage;
|
||||
}
|
||||
|
||||
double PowerDistribution::GetTemperature() const {
|
||||
int32_t status = 0;
|
||||
double temperature = HAL_GetPowerDistributionTemperature(m_handle, &status);
|
||||
FRC_CheckErrorStatus(status, "Module {}", m_module);
|
||||
return temperature;
|
||||
}
|
||||
|
||||
double PowerDistribution::GetCurrent(int channel) const {
|
||||
int32_t status = 0;
|
||||
|
||||
if (!HAL_CheckPowerDistributionChannel(m_handle, channel)) {
|
||||
FRC_ReportError(err::ChannelIndexOutOfRange, "Module {} Channel {}",
|
||||
m_module, channel);
|
||||
return 0;
|
||||
}
|
||||
|
||||
double current =
|
||||
HAL_GetPowerDistributionChannelCurrent(m_handle, channel, &status);
|
||||
FRC_CheckErrorStatus(status, "Module {} Channel {}", m_module, channel);
|
||||
|
||||
return current;
|
||||
}
|
||||
|
||||
double PowerDistribution::GetTotalCurrent() const {
|
||||
int32_t status = 0;
|
||||
double current = HAL_GetPowerDistributionTotalCurrent(m_handle, &status);
|
||||
FRC_CheckErrorStatus(status, "Module {}", m_module);
|
||||
return current;
|
||||
}
|
||||
|
||||
double PowerDistribution::GetTotalPower() const {
|
||||
int32_t status = 0;
|
||||
double power = HAL_GetPowerDistributionTotalPower(m_handle, &status);
|
||||
FRC_CheckErrorStatus(status, "Module {}", m_module);
|
||||
return power;
|
||||
}
|
||||
|
||||
double PowerDistribution::GetTotalEnergy() const {
|
||||
int32_t status = 0;
|
||||
double energy = HAL_GetPowerDistributionTotalEnergy(m_handle, &status);
|
||||
FRC_CheckErrorStatus(status, "Module {}", m_module);
|
||||
return energy;
|
||||
}
|
||||
|
||||
void PowerDistribution::ResetTotalEnergy() {
|
||||
int32_t status = 0;
|
||||
HAL_ResetPowerDistributionTotalEnergy(m_handle, &status);
|
||||
FRC_CheckErrorStatus(status, "Module {}", m_module);
|
||||
}
|
||||
|
||||
void PowerDistribution::ClearStickyFaults() {
|
||||
int32_t status = 0;
|
||||
HAL_ClearPowerDistributionStickyFaults(m_handle, &status);
|
||||
FRC_CheckErrorStatus(status, "Module {}", m_module);
|
||||
}
|
||||
|
||||
int PowerDistribution::GetModule() const {
|
||||
return m_module;
|
||||
}
|
||||
|
||||
void PowerDistribution::InitSendable(wpi::SendableBuilder& builder) {
|
||||
builder.SetSmartDashboardType("PowerDistribution");
|
||||
for (int i = 0; i < SensorUtil::kPDPChannels; ++i) {
|
||||
builder.AddDoubleProperty(
|
||||
fmt::format("Chan{}", i), [=] { return GetCurrent(i); }, nullptr);
|
||||
}
|
||||
builder.AddDoubleProperty(
|
||||
"Voltage", [=] { return GetVoltage(); }, nullptr);
|
||||
builder.AddDoubleProperty(
|
||||
"TotalCurrent", [=] { return GetTotalCurrent(); }, nullptr);
|
||||
}
|
||||
@@ -16,11 +16,8 @@ using namespace frc;
|
||||
const int SensorUtil::kDigitalChannels = HAL_GetNumDigitalChannels();
|
||||
const int SensorUtil::kAnalogInputs = HAL_GetNumAnalogInputs();
|
||||
const int SensorUtil::kAnalogOutputs = HAL_GetNumAnalogOutputs();
|
||||
const int SensorUtil::kSolenoidChannels = HAL_GetNumSolenoidChannels();
|
||||
const int SensorUtil::kSolenoidModules = HAL_GetNumCTREPCMModules();
|
||||
const int SensorUtil::kPwmChannels = HAL_GetNumPWMChannels();
|
||||
const int SensorUtil::kRelayChannels = HAL_GetNumRelayHeaders();
|
||||
const int SensorUtil::kPDPChannels = HAL_GetNumPDPChannels();
|
||||
|
||||
int SensorUtil::GetDefaultCTREPCMModule() {
|
||||
return 0;
|
||||
|
||||
@@ -92,12 +92,12 @@ void PowerDistributionSim::SetCurrent(int channel, double current) {
|
||||
HALSIM_SetPowerDistributionCurrent(m_index, channel, current);
|
||||
}
|
||||
|
||||
void PowerDistributionSim::GetAllCurrents(double* currents) const {
|
||||
HALSIM_GetPowerDistributionAllCurrents(m_index, currents);
|
||||
void PowerDistributionSim::GetAllCurrents(double* currents, int length) const {
|
||||
HALSIM_GetPowerDistributionAllCurrents(m_index, currents, length);
|
||||
}
|
||||
|
||||
void PowerDistributionSim::SetAllCurrents(const double* currents) {
|
||||
HALSIM_SetPowerDistributionAllCurrents(m_index, currents);
|
||||
void PowerDistributionSim::SetAllCurrents(const double* currents, int length) {
|
||||
HALSIM_SetPowerDistributionAllCurrents(m_index, currents, length);
|
||||
}
|
||||
|
||||
void PowerDistributionSim::ResetData() {
|
||||
|
||||
@@ -17,10 +17,13 @@ namespace frc {
|
||||
class PowerDistribution : public wpi::Sendable,
|
||||
public wpi::SendableHelper<PowerDistribution> {
|
||||
public:
|
||||
static constexpr int kDefaultModule = -1;
|
||||
enum class ModuleType { kAutomatic = 0, kCTRE = 1, kRev = 2 };
|
||||
|
||||
/**
|
||||
* Constructs a PowerDistribution.
|
||||
*
|
||||
* Uses the default CAN ID (0).
|
||||
* Uses the default CAN ID.
|
||||
*/
|
||||
PowerDistribution();
|
||||
|
||||
@@ -28,9 +31,11 @@ class PowerDistribution : public wpi::Sendable,
|
||||
* Constructs a PowerDistribution.
|
||||
*
|
||||
* @param module The CAN ID of the PDP
|
||||
* @param moduleType The type of module
|
||||
*/
|
||||
explicit PowerDistribution(int module);
|
||||
PowerDistribution(int module, ModuleType moduleType);
|
||||
|
||||
~PowerDistribution() override;
|
||||
PowerDistribution(PowerDistribution&&) = default;
|
||||
PowerDistribution& operator=(PowerDistribution&&) = default;
|
||||
|
||||
@@ -93,10 +98,14 @@ class PowerDistribution : public wpi::Sendable,
|
||||
*/
|
||||
int GetModule() const;
|
||||
|
||||
bool GetSwitchableChannel() const;
|
||||
|
||||
void SetSwitchableChannel(bool enabled);
|
||||
|
||||
void InitSendable(wpi::SendableBuilder& builder) override;
|
||||
|
||||
private:
|
||||
hal::Handle<HAL_PDPHandle> m_handle;
|
||||
hal::Handle<HAL_PowerDistributionHandle> m_handle;
|
||||
int m_module;
|
||||
};
|
||||
|
||||
|
||||
@@ -71,29 +71,11 @@ class SensorUtil final {
|
||||
*/
|
||||
static bool CheckAnalogOutputChannel(int channel);
|
||||
|
||||
/**
|
||||
* Verify that the power distribution channel number is within limits.
|
||||
*
|
||||
* @return PDP channel is valid
|
||||
*/
|
||||
static bool CheckPDPChannel(int channel);
|
||||
|
||||
/**
|
||||
* Verify that the PDP module number is within limits. module numbers are
|
||||
* 0-based
|
||||
*
|
||||
* @return PDP module is valid
|
||||
*/
|
||||
static bool CheckPDPModule(int module);
|
||||
|
||||
static const int kDigitalChannels;
|
||||
static const int kAnalogInputs;
|
||||
static const int kAnalogOutputs;
|
||||
static const int kSolenoidChannels;
|
||||
static const int kSolenoidModules;
|
||||
static const int kPwmChannels;
|
||||
static const int kRelayChannels;
|
||||
static const int kPDPChannels;
|
||||
};
|
||||
|
||||
} // namespace frc
|
||||
|
||||
@@ -142,7 +142,7 @@ class PowerDistributionSim {
|
||||
* array must be big enough to hold all the PowerDistribution
|
||||
* channels
|
||||
*/
|
||||
void GetAllCurrents(double* currents) const;
|
||||
void GetAllCurrents(double* currents, int length) const;
|
||||
|
||||
/**
|
||||
* Change the current in all of the PowerDistribution channels.
|
||||
@@ -151,7 +151,7 @@ class PowerDistributionSim {
|
||||
* array must be big enough to hold all the PowerDistribution
|
||||
* channels
|
||||
*/
|
||||
void SetAllCurrents(const double* currents);
|
||||
void SetAllCurrents(const double* currents, int length);
|
||||
|
||||
/**
|
||||
* Reset all PowerDistribution simulation data.
|
||||
|
||||
Reference in New Issue
Block a user