2021-08-04 20:31:17 -07:00
|
|
|
// 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 "hal/PowerDistribution.h"
|
|
|
|
|
|
|
|
|
|
#include "CTREPDP.h"
|
|
|
|
|
#include "HALInternal.h"
|
|
|
|
|
#include "PortsInternal.h"
|
2021-08-14 11:44:56 -07:00
|
|
|
#include "REVPDH.h"
|
2021-08-04 20:31:17 -07:00
|
|
|
#include "hal/Errors.h"
|
|
|
|
|
#include "hal/handles/HandlesInternal.h"
|
|
|
|
|
|
|
|
|
|
using namespace hal;
|
|
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
|
|
|
|
|
|
HAL_PowerDistributionHandle HAL_InitializePowerDistribution(
|
2021-08-14 11:44:56 -07:00
|
|
|
int32_t moduleNumber, HAL_PowerDistributionType type,
|
|
|
|
|
const char* allocationLocation, int32_t* status) {
|
2021-08-04 20:31:17 -07:00
|
|
|
if (type == HAL_PowerDistributionType::HAL_PowerDistributionType_kAutomatic) {
|
|
|
|
|
type = HAL_PowerDistributionType::HAL_PowerDistributionType_kCTRE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (type == HAL_PowerDistributionType::HAL_PowerDistributionType_kCTRE) {
|
2021-08-14 11:44:56 -07:00
|
|
|
if (moduleNumber == HAL_DEFAULT_POWER_DISTRIBUTION_MODULE) {
|
|
|
|
|
moduleNumber = 0;
|
|
|
|
|
}
|
2021-08-04 20:31:17 -07:00
|
|
|
return static_cast<HAL_PowerDistributionHandle>(
|
2021-08-14 11:44:56 -07:00
|
|
|
HAL_InitializePDP(moduleNumber, allocationLocation, status)); // TODO
|
2021-08-04 20:31:17 -07:00
|
|
|
} else {
|
2021-08-14 11:44:56 -07:00
|
|
|
if (moduleNumber == HAL_DEFAULT_POWER_DISTRIBUTION_MODULE) {
|
|
|
|
|
moduleNumber = 1;
|
|
|
|
|
}
|
|
|
|
|
return static_cast<HAL_PowerDistributionHandle>(
|
|
|
|
|
HAL_REV_InitializePDH(moduleNumber, allocationLocation, status));
|
2021-08-04 20:31:17 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#define IsCtre(handle) ::hal::isHandleType(handle, HAL_HandleEnum::CTREPDP)
|
|
|
|
|
|
|
|
|
|
void HAL_CleanPowerDistribution(HAL_PowerDistributionHandle handle) {
|
|
|
|
|
if (IsCtre(handle)) {
|
|
|
|
|
HAL_CleanPDP(handle);
|
|
|
|
|
} else {
|
2021-08-14 11:44:56 -07:00
|
|
|
HAL_REV_FreePDH(handle);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int32_t HAL_GetPowerDistributionModuleNumber(HAL_PowerDistributionHandle handle,
|
|
|
|
|
int32_t* status) {
|
|
|
|
|
if (IsCtre(handle)) {
|
|
|
|
|
return HAL_GetPDPModuleNumber(handle, status);
|
|
|
|
|
} else {
|
|
|
|
|
return HAL_REV_GetPDHModuleNumber(handle, status);
|
2021-08-04 20:31:17 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
HAL_Bool HAL_CheckPowerDistributionChannel(HAL_PowerDistributionHandle handle,
|
|
|
|
|
int32_t channel) {
|
|
|
|
|
if (IsCtre(handle)) {
|
|
|
|
|
return HAL_CheckPDPChannel(channel);
|
|
|
|
|
} else {
|
2021-08-14 11:44:56 -07:00
|
|
|
return HAL_REV_CheckPDHChannelNumber(channel);
|
2021-08-04 20:31:17 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
HAL_Bool HAL_CheckPowerDistributionModule(int32_t module,
|
|
|
|
|
HAL_PowerDistributionType type) {
|
|
|
|
|
if (type == HAL_PowerDistributionType::HAL_PowerDistributionType_kCTRE) {
|
|
|
|
|
return HAL_CheckPDPModule(module);
|
|
|
|
|
} else {
|
2021-08-14 11:44:56 -07:00
|
|
|
return HAL_REV_CheckPDHModuleNumber(module);
|
2021-08-04 20:31:17 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
HAL_PowerDistributionType HAL_GetPowerDistributionType(
|
|
|
|
|
HAL_PowerDistributionHandle handle, int32_t* status) {
|
|
|
|
|
return IsCtre(handle)
|
|
|
|
|
? HAL_PowerDistributionType::HAL_PowerDistributionType_kCTRE
|
|
|
|
|
: HAL_PowerDistributionType::HAL_PowerDistributionType_kRev;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-14 11:44:56 -07:00
|
|
|
int32_t HAL_GetPowerDistributionNumChannels(HAL_PowerDistributionHandle handle,
|
|
|
|
|
int32_t* status) {
|
|
|
|
|
if (IsCtre(handle)) {
|
|
|
|
|
return kNumCTREPDPChannels;
|
|
|
|
|
} else {
|
|
|
|
|
return kNumREVPDHChannels;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-04 20:31:17 -07:00
|
|
|
double HAL_GetPowerDistributionTemperature(HAL_PowerDistributionHandle handle,
|
|
|
|
|
int32_t* status) {
|
|
|
|
|
if (IsCtre(handle)) {
|
|
|
|
|
return HAL_GetPDPTemperature(handle, status);
|
|
|
|
|
} else {
|
2021-08-14 11:44:56 -07:00
|
|
|
// Not supported
|
|
|
|
|
return 0;
|
2021-08-04 20:31:17 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
double HAL_GetPowerDistributionVoltage(HAL_PowerDistributionHandle handle,
|
|
|
|
|
int32_t* status) {
|
|
|
|
|
if (IsCtre(handle)) {
|
|
|
|
|
return HAL_GetPDPVoltage(handle, status);
|
|
|
|
|
} else {
|
2021-08-14 11:44:56 -07:00
|
|
|
return HAL_REV_GetPDHSupplyVoltage(handle, status);
|
2021-08-04 20:31:17 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
double HAL_GetPowerDistributionChannelCurrent(
|
|
|
|
|
HAL_PowerDistributionHandle handle, int32_t channel, int32_t* status) {
|
|
|
|
|
if (IsCtre(handle)) {
|
|
|
|
|
return HAL_GetPDPChannelCurrent(handle, channel, status);
|
|
|
|
|
} else {
|
2021-08-14 11:44:56 -07:00
|
|
|
return HAL_REV_GetPDHChannelCurrent(handle, channel, status);
|
2021-08-04 20:31:17 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HAL_GetPowerDistributionAllChannelCurrents(
|
|
|
|
|
HAL_PowerDistributionHandle handle, double* currents,
|
|
|
|
|
int32_t currentsLength, int32_t* status) {
|
|
|
|
|
if (IsCtre(handle)) {
|
2021-08-14 11:44:56 -07:00
|
|
|
if (currentsLength < kNumCTREPDPChannels) {
|
2021-08-04 20:31:17 -07:00
|
|
|
*status = PARAMETER_OUT_OF_RANGE;
|
|
|
|
|
SetLastError(status, "Output array not large enough");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
return HAL_GetPDPAllChannelCurrents(handle, currents, status);
|
|
|
|
|
} else {
|
2021-08-14 11:44:56 -07:00
|
|
|
if (currentsLength < kNumREVPDHChannels) {
|
|
|
|
|
*status = PARAMETER_OUT_OF_RANGE;
|
|
|
|
|
SetLastError(status, "Output array not large enough");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
return HAL_REV_GetPDHAllChannelCurrents(handle, currents, status);
|
2021-08-04 20:31:17 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
double HAL_GetPowerDistributionTotalCurrent(HAL_PowerDistributionHandle handle,
|
|
|
|
|
int32_t* status) {
|
|
|
|
|
if (IsCtre(handle)) {
|
|
|
|
|
return HAL_GetPDPTotalCurrent(handle, status);
|
|
|
|
|
} else {
|
2021-08-14 11:44:56 -07:00
|
|
|
return HAL_REV_GetPDHTotalCurrent(handle, status);
|
2021-08-04 20:31:17 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
double HAL_GetPowerDistributionTotalPower(HAL_PowerDistributionHandle handle,
|
|
|
|
|
int32_t* status) {
|
|
|
|
|
if (IsCtre(handle)) {
|
|
|
|
|
return HAL_GetPDPTotalPower(handle, status);
|
|
|
|
|
} else {
|
2021-08-14 11:44:56 -07:00
|
|
|
// Not currently supported
|
2021-08-04 20:31:17 -07:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
double HAL_GetPowerDistributionTotalEnergy(HAL_PowerDistributionHandle handle,
|
|
|
|
|
int32_t* status) {
|
|
|
|
|
if (IsCtre(handle)) {
|
|
|
|
|
return HAL_GetPDPTotalEnergy(handle, status);
|
|
|
|
|
} else {
|
2021-08-14 11:44:56 -07:00
|
|
|
// Not currently supported
|
2021-08-04 20:31:17 -07:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HAL_ResetPowerDistributionTotalEnergy(HAL_PowerDistributionHandle handle,
|
|
|
|
|
int32_t* status) {
|
|
|
|
|
if (IsCtre(handle)) {
|
|
|
|
|
HAL_ResetPDPTotalEnergy(handle, status);
|
|
|
|
|
} else {
|
2021-08-14 11:44:56 -07:00
|
|
|
// Not supported
|
2021-08-04 20:31:17 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HAL_ClearPowerDistributionStickyFaults(HAL_PowerDistributionHandle handle,
|
|
|
|
|
int32_t* status) {
|
|
|
|
|
if (IsCtre(handle)) {
|
|
|
|
|
HAL_ClearPDPStickyFaults(handle, status);
|
|
|
|
|
} else {
|
2021-08-14 11:44:56 -07:00
|
|
|
HAL_REV_ClearPDHFaults(handle, status);
|
2021-08-04 20:31:17 -07:00
|
|
|
}
|
|
|
|
|
}
|
2021-08-14 11:44:56 -07:00
|
|
|
|
|
|
|
|
void HAL_SetPowerDistributionSwitchableChannel(
|
|
|
|
|
HAL_PowerDistributionHandle handle, HAL_Bool enabled, int32_t* status) {
|
|
|
|
|
if (IsCtre(handle)) {
|
|
|
|
|
// No-op on CTRE
|
|
|
|
|
return;
|
|
|
|
|
} else {
|
|
|
|
|
HAL_REV_SetPDHSwitchableChannel(handle, enabled, status);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
HAL_Bool HAL_GetPowerDistributionSwitchableChannel(
|
|
|
|
|
HAL_PowerDistributionHandle handle, int32_t* status) {
|
|
|
|
|
if (IsCtre(handle)) {
|
|
|
|
|
// No-op on CTRE
|
|
|
|
|
return false;
|
|
|
|
|
} else {
|
|
|
|
|
return HAL_REV_GetPDHSwitchableChannelState(handle, status);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-04 20:31:17 -07:00
|
|
|
} // extern "C"
|