[hal] Rename PowerDistributionPanel to PowerDistribution (#3466)

Makes HAL more generic for the PDP, to enable the Rev PDH in the future.
This commit is contained in:
Thad House
2021-08-04 20:31:17 -07:00
committed by GitHub
parent 2014115bca
commit 1ac73a247e
50 changed files with 1612 additions and 1177 deletions

View File

@@ -183,7 +183,7 @@ HAL_CTREPCMHandle HAL_InitializeCTREPCM(int32_t module,
pcm->previousAllocation);
} else {
hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for CTRE PCM", 0,
kNumAccumulators, module);
kNumCTREPCMModules, module);
}
return HAL_kInvalidHandle; // failed to allocate. Pass error back.
}

View File

@@ -2,7 +2,7 @@
// 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/PDP.h"
#include "CTREPDP.h"
#include <fmt/format.h>
#include <wpi/mutex.h>
@@ -12,6 +12,7 @@
#include "PortsInternal.h"
#include "hal/CANAPI.h"
#include "hal/Errors.h"
#include "hal/handles/IndexedHandleResource.h"
using namespace hal;
@@ -102,20 +103,29 @@ union PdpStatusEnergy {
} bits;
};
static wpi::mutex pdpHandleMutex;
static HAL_PDPHandle pdpHandles[kNumPDPModules];
namespace {
struct PDP {
HAL_CANHandle canHandle;
std::string previousAllocation;
};
} // namespace
static IndexedHandleResource<HAL_PDPHandle, PDP, kNumPDPModules,
HAL_HandleEnum::CTREPDP>* pdpHandles;
namespace hal::init {
void InitializePDP() {
for (int i = 0; i < kNumPDPModules; i++) {
pdpHandles[i] = HAL_kInvalidHandle;
}
static IndexedHandleResource<HAL_PDPHandle, PDP, kNumPDPModules,
HAL_HandleEnum::CTREPDP>
pH;
pdpHandles = &pH;
}
} // namespace hal::init
extern "C" {
HAL_PDPHandle HAL_InitializePDP(int32_t module, int32_t* status) {
HAL_PDPHandle HAL_InitializePDP(int32_t module, const char* allocationLocation,
int32_t* status) {
hal::init::CheckInit();
if (!HAL_CheckPDPModule(module)) {
*status = PARAMETER_OUT_OF_RANGE;
@@ -123,34 +133,37 @@ HAL_PDPHandle HAL_InitializePDP(int32_t module, int32_t* status) {
return HAL_kInvalidHandle;
}
std::scoped_lock lock(pdpHandleMutex);
if (pdpHandles[module] != HAL_kInvalidHandle) {
*status = 0;
return pdpHandles[module];
}
auto handle = HAL_InitializeCAN(manufacturer, module, deviceType, status);
HAL_PDPHandle handle;
auto pdp = pdpHandles->Allocate(module, &handle, status);
if (*status != 0) {
HAL_CleanCAN(handle);
if (pdp) {
hal::SetLastErrorPreviouslyAllocated(status, "CTRE PDP", module,
pdp->previousAllocation);
} else {
hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for CTRE PDP", 0,
kNumPDPModules, module);
}
return HAL_kInvalidHandle; // failed to allocate. Pass error back.
}
pdp->canHandle = HAL_InitializeCAN(manufacturer, module, deviceType, status);
if (*status != 0) {
pdpHandles->Free(handle);
return HAL_kInvalidHandle;
}
pdpHandles[module] = handle;
pdp->previousAllocation = allocationLocation ? allocationLocation : "";
return handle;
}
void HAL_CleanPDP(HAL_PDPHandle handle) {
HAL_CleanCAN(handle);
for (int i = 0; i < kNumPDPModules; i++) {
if (pdpHandles[i] == handle) {
pdpHandles[i] = HAL_kInvalidHandle;
return;
}
auto pdp = pdpHandles->Get(handle);
if (pdp) {
HAL_CleanCAN(pdp->canHandle);
}
pdpHandles->Free(handle);
}
HAL_Bool HAL_CheckPDPModule(int32_t module) {
@@ -162,11 +175,17 @@ HAL_Bool HAL_CheckPDPChannel(int32_t channel) {
}
double HAL_GetPDPTemperature(HAL_PDPHandle handle, int32_t* status) {
auto pdp = pdpHandles->Get(handle);
if (pdp == nullptr) {
*status = HAL_HANDLE_ERROR;
return 0;
}
PdpStatus3 pdpStatus;
int32_t length = 0;
uint64_t receivedTimestamp = 0;
HAL_ReadCANPacketTimeout(handle, Status3, pdpStatus.data, &length,
HAL_ReadCANPacketTimeout(pdp->canHandle, Status3, pdpStatus.data, &length,
&receivedTimestamp, TimeoutMs, status);
if (*status != 0) {
@@ -177,11 +196,17 @@ double HAL_GetPDPTemperature(HAL_PDPHandle handle, int32_t* status) {
}
double HAL_GetPDPVoltage(HAL_PDPHandle handle, int32_t* status) {
auto pdp = pdpHandles->Get(handle);
if (pdp == nullptr) {
*status = HAL_HANDLE_ERROR;
return 0;
}
PdpStatus3 pdpStatus;
int32_t length = 0;
uint64_t receivedTimestamp = 0;
HAL_ReadCANPacketTimeout(handle, Status3, pdpStatus.data, &length,
HAL_ReadCANPacketTimeout(pdp->canHandle, Status3, pdpStatus.data, &length,
&receivedTimestamp, TimeoutMs, status);
if (*status != 0) {
@@ -199,6 +224,12 @@ double HAL_GetPDPChannelCurrent(HAL_PDPHandle handle, int32_t channel,
return 0;
}
auto pdp = pdpHandles->Get(handle);
if (pdp == nullptr) {
*status = HAL_HANDLE_ERROR;
return 0;
}
int32_t length = 0;
uint64_t receivedTimestamp = 0;
@@ -206,7 +237,7 @@ double HAL_GetPDPChannelCurrent(HAL_PDPHandle handle, int32_t channel,
if (channel <= 5) {
PdpStatus1 pdpStatus;
HAL_ReadCANPacketTimeout(handle, Status1, pdpStatus.data, &length,
HAL_ReadCANPacketTimeout(pdp->canHandle, Status1, pdpStatus.data, &length,
&receivedTimestamp, TimeoutMs, status);
if (*status != 0) {
return 0;
@@ -239,7 +270,7 @@ double HAL_GetPDPChannelCurrent(HAL_PDPHandle handle, int32_t channel,
}
} else if (channel <= 11) {
PdpStatus2 pdpStatus;
HAL_ReadCANPacketTimeout(handle, Status2, pdpStatus.data, &length,
HAL_ReadCANPacketTimeout(pdp->canHandle, Status2, pdpStatus.data, &length,
&receivedTimestamp, TimeoutMs, status);
if (*status != 0) {
return 0;
@@ -272,7 +303,7 @@ double HAL_GetPDPChannelCurrent(HAL_PDPHandle handle, int32_t channel,
}
} else {
PdpStatus3 pdpStatus;
HAL_ReadCANPacketTimeout(handle, Status3, pdpStatus.data, &length,
HAL_ReadCANPacketTimeout(pdp->canHandle, Status3, pdpStatus.data, &length,
&receivedTimestamp, TimeoutMs, status);
if (*status != 0) {
return 0;
@@ -303,22 +334,28 @@ double HAL_GetPDPChannelCurrent(HAL_PDPHandle handle, int32_t channel,
void HAL_GetPDPAllChannelCurrents(HAL_PDPHandle handle, double* currents,
int32_t* status) {
auto pdp = pdpHandles->Get(handle);
if (pdp == nullptr) {
*status = HAL_HANDLE_ERROR;
return;
}
int32_t length = 0;
uint64_t receivedTimestamp = 0;
PdpStatus1 pdpStatus;
HAL_ReadCANPacketTimeout(handle, Status1, pdpStatus.data, &length,
HAL_ReadCANPacketTimeout(pdp->canHandle, Status1, pdpStatus.data, &length,
&receivedTimestamp, TimeoutMs, status);
if (*status != 0) {
return;
}
PdpStatus2 pdpStatus2;
HAL_ReadCANPacketTimeout(handle, Status2, pdpStatus2.data, &length,
HAL_ReadCANPacketTimeout(pdp->canHandle, Status2, pdpStatus2.data, &length,
&receivedTimestamp, TimeoutMs, status);
if (*status != 0) {
return;
}
PdpStatus3 pdpStatus3;
HAL_ReadCANPacketTimeout(handle, Status3, pdpStatus3.data, &length,
HAL_ReadCANPacketTimeout(pdp->canHandle, Status3, pdpStatus3.data, &length,
&receivedTimestamp, TimeoutMs, status);
if (*status != 0) {
return;
@@ -377,12 +414,18 @@ void HAL_GetPDPAllChannelCurrents(HAL_PDPHandle handle, double* currents,
}
double HAL_GetPDPTotalCurrent(HAL_PDPHandle handle, int32_t* status) {
auto pdp = pdpHandles->Get(handle);
if (pdp == nullptr) {
*status = HAL_HANDLE_ERROR;
return 0;
}
PdpStatusEnergy pdpStatus;
int32_t length = 0;
uint64_t receivedTimestamp = 0;
HAL_ReadCANPacketTimeout(handle, StatusEnergy, pdpStatus.data, &length,
&receivedTimestamp, TimeoutMs, status);
HAL_ReadCANPacketTimeout(pdp->canHandle, StatusEnergy, pdpStatus.data,
&length, &receivedTimestamp, TimeoutMs, status);
if (*status != 0) {
return 0;
}
@@ -395,12 +438,18 @@ double HAL_GetPDPTotalCurrent(HAL_PDPHandle handle, int32_t* status) {
}
double HAL_GetPDPTotalPower(HAL_PDPHandle handle, int32_t* status) {
auto pdp = pdpHandles->Get(handle);
if (pdp == nullptr) {
*status = HAL_HANDLE_ERROR;
return 0;
}
PdpStatusEnergy pdpStatus;
int32_t length = 0;
uint64_t receivedTimestamp = 0;
HAL_ReadCANPacketTimeout(handle, StatusEnergy, pdpStatus.data, &length,
&receivedTimestamp, TimeoutMs, status);
HAL_ReadCANPacketTimeout(pdp->canHandle, StatusEnergy, pdpStatus.data,
&length, &receivedTimestamp, TimeoutMs, status);
if (*status != 0) {
return 0;
}
@@ -415,12 +464,18 @@ double HAL_GetPDPTotalPower(HAL_PDPHandle handle, int32_t* status) {
}
double HAL_GetPDPTotalEnergy(HAL_PDPHandle handle, int32_t* status) {
auto pdp = pdpHandles->Get(handle);
if (pdp == nullptr) {
*status = HAL_HANDLE_ERROR;
return 0;
}
PdpStatusEnergy pdpStatus;
int32_t length = 0;
uint64_t receivedTimestamp = 0;
HAL_ReadCANPacketTimeout(handle, StatusEnergy, pdpStatus.data, &length,
&receivedTimestamp, TimeoutMs, status);
HAL_ReadCANPacketTimeout(pdp->canHandle, StatusEnergy, pdpStatus.data,
&length, &receivedTimestamp, TimeoutMs, status);
if (*status != 0) {
return 0;
}
@@ -443,13 +498,25 @@ double HAL_GetPDPTotalEnergy(HAL_PDPHandle handle, int32_t* status) {
}
void HAL_ResetPDPTotalEnergy(HAL_PDPHandle handle, int32_t* status) {
auto pdp = pdpHandles->Get(handle);
if (pdp == nullptr) {
*status = HAL_HANDLE_ERROR;
return;
}
uint8_t pdpControl[] = {0x40}; /* only bit set is ResetEnergy */
HAL_WriteCANPacket(handle, pdpControl, 1, Control1, status);
HAL_WriteCANPacket(pdp->canHandle, pdpControl, 1, Control1, status);
}
void HAL_ClearPDPStickyFaults(HAL_PDPHandle handle, int32_t* status) {
auto pdp = pdpHandles->Get(handle);
if (pdp == nullptr) {
*status = HAL_HANDLE_ERROR;
return;
}
uint8_t pdpControl[] = {0x80}; /* only bit set is ClearStickyFaults */
HAL_WriteCANPacket(handle, pdpControl, 1, Control1, status);
HAL_WriteCANPacket(pdp->canHandle, pdpControl, 1, Control1, status);
}
} // extern "C"

View File

@@ -0,0 +1,131 @@
// 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.
#pragma once
#include <stdint.h>
#include "hal/Types.h"
/**
* @defgroup hal_pdp PDP Functions
* @ingroup hal_capi
* Functions to control the Power Distribution Panel.
* @{
*/
#ifdef __cplusplus
extern "C" {
#endif
/**
* Initializes a Power Distribution Panel.
*
* @param module the module number to initialize
* @return the created PDP
*/
HAL_PDPHandle HAL_InitializePDP(int32_t module, const char* allocationLocation,
int32_t* status);
/**
* Cleans a PDP module.
*
* @param handle the module handle
*/
void HAL_CleanPDP(HAL_PDPHandle handle);
/**
* Checks if a PDP channel is valid.
*
* @param channel the channel to check
* @return true if the channel is valid, otherwise false
*/
HAL_Bool HAL_CheckPDPChannel(int32_t channel);
/**
* Checks if a PDP module is valid.
*
* @param channel the module to check
* @return true if the module is valid, otherwise false
*/
HAL_Bool HAL_CheckPDPModule(int32_t module);
/**
* Gets the temperature of the PDP.
*
* @param handle the module handle
* @return the module temperature (celsius)
*/
double HAL_GetPDPTemperature(HAL_PDPHandle handle, int32_t* status);
/**
* Gets the PDP input voltage.
*
* @param handle the module handle
* @return the input voltage (volts)
*/
double HAL_GetPDPVoltage(HAL_PDPHandle handle, int32_t* status);
/**
* Gets the current of a specific PDP channel.
*
* @param module the module
* @param channel the channel
* @return the channel current (amps)
*/
double HAL_GetPDPChannelCurrent(HAL_PDPHandle handle, int32_t channel,
int32_t* status);
/**
* Gets the current of all 16 channels on the PDP.
*
* The array must be large enough to hold all channels.
*
* @param handle the module handle
* @param current the currents (output)
*/
void HAL_GetPDPAllChannelCurrents(HAL_PDPHandle handle, double* currents,
int32_t* status);
/**
* Gets the total current of the PDP.
*
* @param handle the module handle
* @return the total current (amps)
*/
double HAL_GetPDPTotalCurrent(HAL_PDPHandle handle, int32_t* status);
/**
* Gets the total power of the PDP.
*
* @param handle the module handle
* @return the total power (watts)
*/
double HAL_GetPDPTotalPower(HAL_PDPHandle handle, int32_t* status);
/**
* Gets the total energy of the PDP.
*
* @param handle the module handle
* @return the total energy (joules)
*/
double HAL_GetPDPTotalEnergy(HAL_PDPHandle handle, int32_t* status);
/**
* Resets the PDP accumulated energy.
*
* @param handle the module handle
*/
void HAL_ResetPDPTotalEnergy(HAL_PDPHandle handle, int32_t* status);
/**
* Clears any PDP sticky faults.
*
* @param handle the module handle
*/
void HAL_ClearPDPStickyFaults(HAL_PDPHandle handle, int32_t* status);
#ifdef __cplusplus
} // extern "C"
#endif
/** @} */

View File

@@ -0,0 +1,171 @@
// 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"
#include "hal/Errors.h"
#include "hal/handles/HandlesInternal.h"
using namespace hal;
extern "C" {
HAL_PowerDistributionHandle HAL_InitializePowerDistribution(
int32_t moduleNumber, HAL_PowerDistributionType type, int32_t* status) {
if (type == HAL_PowerDistributionType::HAL_PowerDistributionType_kAutomatic) {
type = HAL_PowerDistributionType::HAL_PowerDistributionType_kCTRE;
}
if (type == HAL_PowerDistributionType::HAL_PowerDistributionType_kCTRE) {
return static_cast<HAL_PowerDistributionHandle>(
HAL_InitializePDP(moduleNumber, nullptr, status)); // TODO
} else {
*status = PARAMETER_OUT_OF_RANGE;
SetLastError(status, "Rev Power not currently supported");
return HAL_kInvalidHandle;
}
}
#define IsCtre(handle) ::hal::isHandleType(handle, HAL_HandleEnum::CTREPDP)
void HAL_CleanPowerDistribution(HAL_PowerDistributionHandle handle) {
if (IsCtre(handle)) {
HAL_CleanPDP(handle);
} else {
// TODO
}
}
HAL_Bool HAL_CheckPowerDistributionChannel(HAL_PowerDistributionHandle handle,
int32_t channel) {
if (IsCtre(handle)) {
return HAL_CheckPDPChannel(channel);
} else {
return false;
// TODO
}
}
HAL_Bool HAL_CheckPowerDistributionModule(int32_t module,
HAL_PowerDistributionType type) {
if (type == HAL_PowerDistributionType::HAL_PowerDistributionType_kCTRE) {
return HAL_CheckPDPModule(module);
} else {
return false;
// TODO
}
}
HAL_PowerDistributionType HAL_GetPowerDistributionType(
HAL_PowerDistributionHandle handle, int32_t* status) {
return IsCtre(handle)
? HAL_PowerDistributionType::HAL_PowerDistributionType_kCTRE
: HAL_PowerDistributionType::HAL_PowerDistributionType_kRev;
}
double HAL_GetPowerDistributionTemperature(HAL_PowerDistributionHandle handle,
int32_t* status) {
if (IsCtre(handle)) {
return HAL_GetPDPTemperature(handle, status);
} else {
*status = PARAMETER_OUT_OF_RANGE;
SetLastError(status, "Rev Power not currently supported");
return false;
}
}
double HAL_GetPowerDistributionVoltage(HAL_PowerDistributionHandle handle,
int32_t* status) {
if (IsCtre(handle)) {
return HAL_GetPDPVoltage(handle, status);
} else {
*status = PARAMETER_OUT_OF_RANGE;
SetLastError(status, "Rev Power not currently supported");
return false;
}
}
double HAL_GetPowerDistributionChannelCurrent(
HAL_PowerDistributionHandle handle, int32_t channel, int32_t* status) {
if (IsCtre(handle)) {
return HAL_GetPDPChannelCurrent(handle, channel, status);
} else {
*status = PARAMETER_OUT_OF_RANGE;
SetLastError(status, "Rev Power not currently supported");
return 0;
}
}
void HAL_GetPowerDistributionAllChannelCurrents(
HAL_PowerDistributionHandle handle, double* currents,
int32_t currentsLength, int32_t* status) {
if (IsCtre(handle)) {
if (currentsLength < kNumPDPChannels) {
*status = PARAMETER_OUT_OF_RANGE;
SetLastError(status, "Output array not large enough");
return;
}
return HAL_GetPDPAllChannelCurrents(handle, currents, status);
} else {
*status = PARAMETER_OUT_OF_RANGE;
SetLastError(status, "Rev Power not currently supported");
}
}
double HAL_GetPowerDistributionTotalCurrent(HAL_PowerDistributionHandle handle,
int32_t* status) {
if (IsCtre(handle)) {
return HAL_GetPDPTotalCurrent(handle, status);
} else {
*status = PARAMETER_OUT_OF_RANGE;
SetLastError(status, "Rev Power not currently supported");
return 0;
}
}
double HAL_GetPowerDistributionTotalPower(HAL_PowerDistributionHandle handle,
int32_t* status) {
if (IsCtre(handle)) {
return HAL_GetPDPTotalPower(handle, status);
} else {
*status = PARAMETER_OUT_OF_RANGE;
SetLastError(status, "Rev Power not currently supported");
return 0;
}
}
double HAL_GetPowerDistributionTotalEnergy(HAL_PowerDistributionHandle handle,
int32_t* status) {
if (IsCtre(handle)) {
return HAL_GetPDPTotalEnergy(handle, status);
} else {
*status = PARAMETER_OUT_OF_RANGE;
SetLastError(status, "Rev Power not currently supported");
return 0;
}
}
void HAL_ResetPowerDistributionTotalEnergy(HAL_PowerDistributionHandle handle,
int32_t* status) {
if (IsCtre(handle)) {
HAL_ResetPDPTotalEnergy(handle, status);
} else {
*status = PARAMETER_OUT_OF_RANGE;
SetLastError(status, "Rev Power not currently supported");
}
}
void HAL_ClearPowerDistributionStickyFaults(HAL_PowerDistributionHandle handle,
int32_t* status) {
if (IsCtre(handle)) {
HAL_ClearPDPStickyFaults(handle, status);
} else {
*status = PARAMETER_OUT_OF_RANGE;
SetLastError(status, "Rev Power not currently supported");
}
}
} // extern "C"

View File

@@ -1,33 +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 "hal/simulation/PDPData.h"
#include "../PortsInternal.h"
#include "hal/simulation/SimDataValue.h"
extern "C" {
void HALSIM_ResetPDPData(int32_t index) {}
#define DEFINE_CAPI(TYPE, CAPINAME, RETURN) \
HAL_SIMDATAVALUE_STUB_CAPI(TYPE, HALSIM, PDP##CAPINAME, RETURN)
DEFINE_CAPI(HAL_Bool, Initialized, false)
DEFINE_CAPI(double, Temperature, 0)
DEFINE_CAPI(double, Voltage, 0)
HAL_SIMDATAVALUE_STUB_CAPI_CHANNEL(double, HALSIM, PDPCurrent, 0)
void HALSIM_GetPDPAllCurrents(int32_t index, double* currents) {
for (int i = 0; i < hal::kNumPDPChannels; i++) {
currents[i] = 0;
}
}
void HALSIM_SetPDPAllCurrents(int32_t index, const double* currents) {}
void HALSIM_RegisterPDPAllNonCurrentCallbacks(int32_t index, int32_t channel,
HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {}
} // extern "C"

View File

@@ -0,0 +1,33 @@
// 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/simulation/PowerDistributionData.h"
#include "../PortsInternal.h"
#include "hal/simulation/SimDataValue.h"
extern "C" {
void HALSIM_ResetPowerDistributionData(int32_t index) {}
#define DEFINE_CAPI(TYPE, CAPINAME, RETURN) \
HAL_SIMDATAVALUE_STUB_CAPI(TYPE, HALSIM, PowerDistribution##CAPINAME, RETURN)
DEFINE_CAPI(HAL_Bool, Initialized, false)
DEFINE_CAPI(double, Temperature, 0)
DEFINE_CAPI(double, Voltage, 0)
HAL_SIMDATAVALUE_STUB_CAPI_CHANNEL(double, HALSIM, PowerDistributionCurrent, 0)
void HALSIM_GetPowerDistributionAllCurrents(int32_t index, double* currents) {
for (int i = 0; i < hal::kNumPDPChannels; i++) {
currents[i] = 0;
}
}
void HALSIM_SetPowerDistributionAllCurrents(int32_t index,
const double* currents) {}
void HALSIM_RegisterPowerDistributionAllNonCurrentCallbacks(
int32_t index, int32_t channel, HAL_NotifyCallback callback, void* param,
HAL_Bool initialNotify) {}
} // extern "C"