diff --git a/glass/src/lib/native/cpp/hardware/PDP.cpp b/glass/src/lib/native/cpp/hardware/PowerDistribution.cpp similarity index 80% rename from glass/src/lib/native/cpp/hardware/PDP.cpp rename to glass/src/lib/native/cpp/hardware/PowerDistribution.cpp index af70116989..46e550b204 100644 --- a/glass/src/lib/native/cpp/hardware/PDP.cpp +++ b/glass/src/lib/native/cpp/hardware/PowerDistribution.cpp @@ -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 "glass/hardware/PDP.h" +#include "glass/hardware/PowerDistribution.h" #include #include @@ -15,7 +15,7 @@ using namespace glass; -static float DisplayChannel(PDPModel& pdp, int channel) { +static float DisplayChannel(PowerDistributionModel& pdp, int channel) { float width = 0; if (auto currentData = pdp.GetCurrentData(channel)) { ImGui::PushID(channel); @@ -34,9 +34,9 @@ static float DisplayChannel(PDPModel& pdp, int channel) { return width; } -void glass::DisplayPDP(PDPModel* model, int index) { +void glass::DisplayPowerDistribution(PowerDistributionModel* model, int index) { char name[128]; - std::snprintf(name, sizeof(name), "PDP[%d]", index); + std::snprintf(name, sizeof(name), "PowerDistribution[%d]", index); if (CollapsingHeader(name)) { // temperature if (auto tempData = model->GetTemperatureData()) { @@ -56,7 +56,7 @@ void glass::DisplayPDP(PDPModel* model, int index) { } } - // channel currents; show as two columns laid out like PDP + // channel currents; show as two columns laid out like PowerDistribution const int numChannels = model->GetNumChannels(); ImGui::Text("Channel Current (A)"); ImGui::Columns(2, "channels", false); @@ -79,12 +79,13 @@ void glass::DisplayPDP(PDPModel* model, int index) { } } -void glass::DisplayPDPs(PDPsModel* model, std::string_view noneMsg) { +void glass::DisplayPowerDistributions(PowerDistributionsModel* model, + std::string_view noneMsg) { bool hasAny = false; - model->ForEachPDP([&](PDPModel& pdp, int i) { + model->ForEachPowerDistribution([&](PowerDistributionModel& pdp, int i) { hasAny = true; PushID(i); - DisplayPDP(&pdp, i); + DisplayPowerDistribution(&pdp, i); PopID(); }); if (!hasAny && !noneMsg.empty()) { diff --git a/glass/src/lib/native/include/glass/hardware/PDP.h b/glass/src/lib/native/include/glass/hardware/PowerDistribution.h similarity index 62% rename from glass/src/lib/native/include/glass/hardware/PDP.h rename to glass/src/lib/native/include/glass/hardware/PowerDistribution.h index 8417ea35d6..0ef600637d 100644 --- a/glass/src/lib/native/include/glass/hardware/PDP.h +++ b/glass/src/lib/native/include/glass/hardware/PowerDistribution.h @@ -14,7 +14,7 @@ namespace glass { class DataSource; -class PDPModel : public Model { +class PowerDistributionModel : public Model { public: virtual int GetNumChannels() const = 0; @@ -27,13 +27,16 @@ class PDPModel : public Model { virtual void SetCurrent(int channel, double val) = 0; }; -class PDPsModel : public Model { +class PowerDistributionsModel : public Model { public: - virtual void ForEachPDP( - wpi::function_ref func) = 0; + virtual void ForEachPowerDistribution( + wpi::function_ref + func) = 0; }; -void DisplayPDP(PDPModel* model, int index); -void DisplayPDPs(PDPsModel* model, std::string_view noneMsg = "No PDPs"); +void DisplayPowerDistribution(PowerDistributionModel* model, int index); +void DisplayPowerDistributions( + PowerDistributionsModel* model, + std::string_view noneMsg = "No Power Distributions"); } // namespace glass diff --git a/hal/src/main/java/edu/wpi/first/hal/PDPJNI.java b/hal/src/main/java/edu/wpi/first/hal/PDPJNI.java deleted file mode 100644 index 26163871e9..0000000000 --- a/hal/src/main/java/edu/wpi/first/hal/PDPJNI.java +++ /dev/null @@ -1,32 +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. - -package edu.wpi.first.hal; - -@SuppressWarnings("AbbreviationAsWordInName") -public class PDPJNI extends JNIWrapper { - public static native int initializePDP(int module); - - public static native boolean checkPDPModule(int module); - - public static native boolean checkPDPChannel(int channel); - - public static native double getPDPTemperature(int handle); - - public static native double getPDPVoltage(int handle); - - public static native double getPDPChannelCurrent(byte channel, int handle); - - public static native void getPDPAllCurrents(int handle, double[] currents); - - public static native double getPDPTotalCurrent(int handle); - - public static native double getPDPTotalPower(int handle); - - public static native double getPDPTotalEnergy(int handle); - - public static native void resetPDPTotalEnergy(int handle); - - public static native void clearPDPStickyFaults(int handle); -} diff --git a/hal/src/main/java/edu/wpi/first/hal/PowerDistributionJNI.java b/hal/src/main/java/edu/wpi/first/hal/PowerDistributionJNI.java new file mode 100644 index 0000000000..8696d0d1e6 --- /dev/null +++ b/hal/src/main/java/edu/wpi/first/hal/PowerDistributionJNI.java @@ -0,0 +1,34 @@ +// 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. + +package edu.wpi.first.hal; + +@SuppressWarnings("AbbreviationAsWordInName") +public class PowerDistributionJNI extends JNIWrapper { + public static native int initialize(int module, int type); + + public static native boolean checkModule(int module, int type); + + public static native boolean checkChannel(int handle, int channel); + + public static native int getType(int handle); + + public static native double getTemperature(int handle); + + public static native double getVoltage(int handle); + + public static native double getChannelCurrent(byte channel, int handle); + + public static native void getAllCurrents(int handle, double[] currents); + + public static native double getTotalCurrent(int handle); + + public static native double getTotalPower(int handle); + + public static native double getTotalEnergy(int handle); + + public static native void resetTotalEnergy(int handle); + + public static native void clearStickyFaults(int handle); +} diff --git a/hal/src/main/java/edu/wpi/first/hal/simulation/PDPDataJNI.java b/hal/src/main/java/edu/wpi/first/hal/simulation/PowerDistributionDataJNI.java similarity index 96% rename from hal/src/main/java/edu/wpi/first/hal/simulation/PDPDataJNI.java rename to hal/src/main/java/edu/wpi/first/hal/simulation/PowerDistributionDataJNI.java index c84d843990..c17da137a1 100644 --- a/hal/src/main/java/edu/wpi/first/hal/simulation/PDPDataJNI.java +++ b/hal/src/main/java/edu/wpi/first/hal/simulation/PowerDistributionDataJNI.java @@ -6,7 +6,7 @@ package edu.wpi.first.hal.simulation; import edu.wpi.first.hal.JNIWrapper; -public class PDPDataJNI extends JNIWrapper { +public class PowerDistributionDataJNI extends JNIWrapper { public static native int registerInitializedCallback( int index, NotifyCallback callback, boolean initialNotify); diff --git a/hal/src/main/native/athena/CTREPCM.cpp b/hal/src/main/native/athena/CTREPCM.cpp index 84c06203c2..b000acea98 100644 --- a/hal/src/main/native/athena/CTREPCM.cpp +++ b/hal/src/main/native/athena/CTREPCM.cpp @@ -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. } diff --git a/hal/src/main/native/athena/PDP.cpp b/hal/src/main/native/athena/CTREPDP.cpp similarity index 76% rename from hal/src/main/native/athena/PDP.cpp rename to hal/src/main/native/athena/CTREPDP.cpp index 3771bcef9b..f61d6571ea 100644 --- a/hal/src/main/native/athena/PDP.cpp +++ b/hal/src/main/native/athena/CTREPDP.cpp @@ -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 #include @@ -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* pdpHandles; namespace hal::init { void InitializePDP() { - for (int i = 0; i < kNumPDPModules; i++) { - pdpHandles[i] = HAL_kInvalidHandle; - } + static IndexedHandleResource + 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" diff --git a/hal/src/main/native/include/hal/PDP.h b/hal/src/main/native/athena/CTREPDP.h similarity index 95% rename from hal/src/main/native/include/hal/PDP.h rename to hal/src/main/native/athena/CTREPDP.h index 610875ef5a..c3a9a5766d 100644 --- a/hal/src/main/native/include/hal/PDP.h +++ b/hal/src/main/native/athena/CTREPDP.h @@ -25,7 +25,8 @@ extern "C" { * @param module the module number to initialize * @return the created PDP */ -HAL_PDPHandle HAL_InitializePDP(int32_t module, int32_t* status); +HAL_PDPHandle HAL_InitializePDP(int32_t module, const char* allocationLocation, + int32_t* status); /** * Cleans a PDP module. diff --git a/hal/src/main/native/athena/PowerDistribution.cpp b/hal/src/main/native/athena/PowerDistribution.cpp new file mode 100644 index 0000000000..5936539984 --- /dev/null +++ b/hal/src/main/native/athena/PowerDistribution.cpp @@ -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_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" diff --git a/hal/src/main/native/athena/mockdata/PDPData.cpp b/hal/src/main/native/athena/mockdata/PDPData.cpp deleted file mode 100644 index 9861ad939a..0000000000 --- a/hal/src/main/native/athena/mockdata/PDPData.cpp +++ /dev/null @@ -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" diff --git a/hal/src/main/native/athena/mockdata/PowerDistributionData.cpp b/hal/src/main/native/athena/mockdata/PowerDistributionData.cpp new file mode 100644 index 0000000000..9d9b3b4902 --- /dev/null +++ b/hal/src/main/native/athena/mockdata/PowerDistributionData.cpp @@ -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" diff --git a/hal/src/main/native/cpp/jni/PDPJNI.cpp b/hal/src/main/native/cpp/jni/PDPJNI.cpp deleted file mode 100644 index 83838c3065..0000000000 --- a/hal/src/main/native/cpp/jni/PDPJNI.cpp +++ /dev/null @@ -1,190 +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 "HALUtil.h" -#include "edu_wpi_first_hal_PDPJNI.h" -#include "hal/PDP.h" -#include "hal/Ports.h" - -using namespace hal; - -extern "C" { - -/* - * Class: edu_wpi_first_hal_PDPJNI - * Method: initializePDP - * Signature: (I)I - */ -JNIEXPORT jint JNICALL -Java_edu_wpi_first_hal_PDPJNI_initializePDP - (JNIEnv* env, jclass, jint module) -{ - int32_t status = 0; - auto handle = HAL_InitializePDP(module, &status); - CheckStatusRange(env, status, 0, HAL_GetNumPDPModules(), module); - return static_cast(handle); -} - -/* - * Class: edu_wpi_first_hal_PDPJNI - * Method: checkPDPChannel - * Signature: (I)Z - */ -JNIEXPORT jboolean JNICALL -Java_edu_wpi_first_hal_PDPJNI_checkPDPChannel - (JNIEnv* env, jclass, jint channel) -{ - return HAL_CheckPDPChannel(channel); -} - -/* - * Class: edu_wpi_first_hal_PDPJNI - * Method: checkPDPModule - * Signature: (I)Z - */ -JNIEXPORT jboolean JNICALL -Java_edu_wpi_first_hal_PDPJNI_checkPDPModule - (JNIEnv* env, jclass, jint module) -{ - return HAL_CheckPDPModule(module); -} - -/* - * Class: edu_wpi_first_hal_PDPJNI - * Method: getPDPTemperature - * Signature: (I)D - */ -JNIEXPORT jdouble JNICALL -Java_edu_wpi_first_hal_PDPJNI_getPDPTemperature - (JNIEnv* env, jclass, jint handle) -{ - int32_t status = 0; - double temperature = HAL_GetPDPTemperature(handle, &status); - CheckStatus(env, status, false); - return temperature; -} - -/* - * Class: edu_wpi_first_hal_PDPJNI - * Method: getPDPVoltage - * Signature: (I)D - */ -JNIEXPORT jdouble JNICALL -Java_edu_wpi_first_hal_PDPJNI_getPDPVoltage - (JNIEnv* env, jclass, jint handle) -{ - int32_t status = 0; - double voltage = HAL_GetPDPVoltage(handle, &status); - CheckStatus(env, status, false); - return voltage; -} - -/* - * Class: edu_wpi_first_hal_PDPJNI - * Method: getPDPChannelCurrent - * Signature: (BI)D - */ -JNIEXPORT jdouble JNICALL -Java_edu_wpi_first_hal_PDPJNI_getPDPChannelCurrent - (JNIEnv* env, jclass, jbyte channel, jint handle) -{ - int32_t status = 0; - double current = HAL_GetPDPChannelCurrent(handle, channel, &status); - CheckStatus(env, status, false); - return current; -} - -/* - * Class: edu_wpi_first_hal_PDPJNI - * Method: getPDPAllCurrents - * Signature: (I[D)V - */ -JNIEXPORT void JNICALL -Java_edu_wpi_first_hal_PDPJNI_getPDPAllCurrents - (JNIEnv* env, jclass, jint handle, jdoubleArray jarr) -{ - double storage[16]; - int32_t status = 0; - HAL_GetPDPAllChannelCurrents(handle, storage, &status); - if (!CheckStatus(env, status, false)) { - return; - } - - env->SetDoubleArrayRegion(jarr, 0, 16, storage); -} - -/* - * Class: edu_wpi_first_hal_PDPJNI - * Method: getPDPTotalCurrent - * Signature: (I)D - */ -JNIEXPORT jdouble JNICALL -Java_edu_wpi_first_hal_PDPJNI_getPDPTotalCurrent - (JNIEnv* env, jclass, jint handle) -{ - int32_t status = 0; - double current = HAL_GetPDPTotalCurrent(handle, &status); - CheckStatus(env, status, false); - return current; -} - -/* - * Class: edu_wpi_first_hal_PDPJNI - * Method: getPDPTotalPower - * Signature: (I)D - */ -JNIEXPORT jdouble JNICALL -Java_edu_wpi_first_hal_PDPJNI_getPDPTotalPower - (JNIEnv* env, jclass, jint handle) -{ - int32_t status = 0; - double power = HAL_GetPDPTotalPower(handle, &status); - CheckStatus(env, status, false); - return power; -} - -/* - * Class: edu_wpi_first_hal_PDPJNI - * Method: getPDPTotalEnergy - * Signature: (I)D - */ -JNIEXPORT jdouble JNICALL -Java_edu_wpi_first_hal_PDPJNI_getPDPTotalEnergy - (JNIEnv* env, jclass, jint handle) -{ - int32_t status = 0; - double energy = HAL_GetPDPTotalEnergy(handle, &status); - CheckStatus(env, status, false); - return energy; -} - -/* - * Class: edu_wpi_first_hal_PDPJNI - * Method: resetPDPTotalEnergy - * Signature: (I)V - */ -JNIEXPORT void JNICALL -Java_edu_wpi_first_hal_PDPJNI_resetPDPTotalEnergy - (JNIEnv* env, jclass, jint handle) -{ - int32_t status = 0; - HAL_ResetPDPTotalEnergy(handle, &status); - CheckStatus(env, status, false); -} - -/* - * Class: edu_wpi_first_hal_PDPJNI - * Method: clearPDPStickyFaults - * Signature: (I)V - */ -JNIEXPORT void JNICALL -Java_edu_wpi_first_hal_PDPJNI_clearPDPStickyFaults - (JNIEnv* env, jclass, jint handle) -{ - int32_t status = 0; - HAL_ClearPDPStickyFaults(handle, &status); - CheckStatus(env, status, false); -} - -} // extern "C" diff --git a/hal/src/main/native/cpp/jni/PowerDistributionJNI.cpp b/hal/src/main/native/cpp/jni/PowerDistributionJNI.cpp new file mode 100644 index 0000000000..b9527330fd --- /dev/null +++ b/hal/src/main/native/cpp/jni/PowerDistributionJNI.cpp @@ -0,0 +1,209 @@ +// 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 "HALUtil.h" +#include "edu_wpi_first_hal_PowerDistributionJNI.h" +#include "hal/Ports.h" +#include "hal/PowerDistribution.h" + +using namespace hal; + +extern "C" { + +/* + * Class: edu_wpi_first_hal_PowerDistributionJNI + * Method: initialize + * Signature: (II)I + */ +JNIEXPORT jint JNICALL +Java_edu_wpi_first_hal_PowerDistributionJNI_initialize + (JNIEnv* env, jclass, jint module, jint type) +{ + int32_t status = 0; + auto handle = HAL_InitializePowerDistribution( + module, static_cast(type), &status); + CheckStatusForceThrow(env, status); + return static_cast(handle); +} + +/* + * Class: edu_wpi_first_hal_PowerDistributionJNI + * Method: checkChannel + * Signature: (II)Z + */ +JNIEXPORT jboolean JNICALL +Java_edu_wpi_first_hal_PowerDistributionJNI_checkChannel + (JNIEnv* env, jclass, jint handle, jint channel) +{ + return HAL_CheckPowerDistributionChannel(handle, channel); +} + +/* + * Class: edu_wpi_first_hal_PowerDistributionJNI + * Method: checkModule + * Signature: (II)Z + */ +JNIEXPORT jboolean JNICALL +Java_edu_wpi_first_hal_PowerDistributionJNI_checkModule + (JNIEnv* env, jclass, jint module, jint type) +{ + return HAL_CheckPowerDistributionModule( + module, static_cast(type)); +} + +/* + * Class: edu_wpi_first_hal_PowerDistributionJNI + * Method: getType + * Signature: (I)I + */ +JNIEXPORT jint JNICALL +Java_edu_wpi_first_hal_PowerDistributionJNI_getType + (JNIEnv* env, jclass, jint handle) +{ + int32_t status = 0; + auto result = HAL_GetPowerDistributionType(handle, &status); + CheckStatus(env, status); + return result; +} + +/* + * Class: edu_wpi_first_hal_PowerDistributionJNI + * Method: getTemperature + * Signature: (I)D + */ +JNIEXPORT jdouble JNICALL +Java_edu_wpi_first_hal_PowerDistributionJNI_getTemperature + (JNIEnv* env, jclass, jint handle) +{ + int32_t status = 0; + double temperature = HAL_GetPowerDistributionTemperature(handle, &status); + CheckStatus(env, status, false); + return temperature; +} + +/* + * Class: edu_wpi_first_hal_PowerDistributionJNI + * Method: getVoltage + * Signature: (I)D + */ +JNIEXPORT jdouble JNICALL +Java_edu_wpi_first_hal_PowerDistributionJNI_getVoltage + (JNIEnv* env, jclass, jint handle) +{ + int32_t status = 0; + double voltage = HAL_GetPowerDistributionVoltage(handle, &status); + CheckStatus(env, status, false); + return voltage; +} + +/* + * Class: edu_wpi_first_hal_PowerDistributionJNI + * Method: getChannelCurrent + * Signature: (BI)D + */ +JNIEXPORT jdouble JNICALL +Java_edu_wpi_first_hal_PowerDistributionJNI_getChannelCurrent + (JNIEnv* env, jclass, jbyte channel, jint handle) +{ + int32_t status = 0; + double current = + HAL_GetPowerDistributionChannelCurrent(handle, channel, &status); + CheckStatus(env, status, false); + return current; +} + +/* + * Class: edu_wpi_first_hal_PowerDistributionJNI + * Method: getAllCurrents + * Signature: (I[D)V + */ +JNIEXPORT void JNICALL +Java_edu_wpi_first_hal_PowerDistributionJNI_getAllCurrents + (JNIEnv* env, jclass, jint handle, jdoubleArray jarr) +{ + double storage[16]; + int32_t status = 0; + // TODO fix me + HAL_GetPowerDistributionAllChannelCurrents(handle, storage, 16, &status); + if (!CheckStatus(env, status, false)) { + return; + } + + env->SetDoubleArrayRegion(jarr, 0, 16, storage); +} + +/* + * Class: edu_wpi_first_hal_PowerDistributionJNI + * Method: getTotalCurrent + * Signature: (I)D + */ +JNIEXPORT jdouble JNICALL +Java_edu_wpi_first_hal_PowerDistributionJNI_getTotalCurrent + (JNIEnv* env, jclass, jint handle) +{ + int32_t status = 0; + double current = HAL_GetPowerDistributionTotalCurrent(handle, &status); + CheckStatus(env, status, false); + return current; +} + +/* + * Class: edu_wpi_first_hal_PowerDistributionJNI + * Method: getTotalPower + * Signature: (I)D + */ +JNIEXPORT jdouble JNICALL +Java_edu_wpi_first_hal_PowerDistributionJNI_getTotalPower + (JNIEnv* env, jclass, jint handle) +{ + int32_t status = 0; + double power = HAL_GetPowerDistributionTotalPower(handle, &status); + CheckStatus(env, status, false); + return power; +} + +/* + * Class: edu_wpi_first_hal_PowerDistributionJNI + * Method: getTotalEnergy + * Signature: (I)D + */ +JNIEXPORT jdouble JNICALL +Java_edu_wpi_first_hal_PowerDistributionJNI_getTotalEnergy + (JNIEnv* env, jclass, jint handle) +{ + int32_t status = 0; + double energy = HAL_GetPowerDistributionTotalEnergy(handle, &status); + CheckStatus(env, status, false); + return energy; +} + +/* + * Class: edu_wpi_first_hal_PowerDistributionJNI + * Method: resetTotalEnergy + * Signature: (I)V + */ +JNIEXPORT void JNICALL +Java_edu_wpi_first_hal_PowerDistributionJNI_resetTotalEnergy + (JNIEnv* env, jclass, jint handle) +{ + int32_t status = 0; + HAL_ResetPowerDistributionTotalEnergy(handle, &status); + CheckStatus(env, status, false); +} + +/* + * Class: edu_wpi_first_hal_PowerDistributionJNI + * Method: clearStickyFaults + * Signature: (I)V + */ +JNIEXPORT void JNICALL +Java_edu_wpi_first_hal_PowerDistributionJNI_clearStickyFaults + (JNIEnv* env, jclass, jint handle) +{ + int32_t status = 0; + HAL_ClearPowerDistributionStickyFaults(handle, &status); + CheckStatus(env, status, false); +} + +} // extern "C" diff --git a/hal/src/main/native/cpp/jni/simulation/PDPDataJNI.cpp b/hal/src/main/native/cpp/jni/simulation/PDPDataJNI.cpp deleted file mode 100644 index b86ab2b6b3..0000000000 --- a/hal/src/main/native/cpp/jni/simulation/PDPDataJNI.cpp +++ /dev/null @@ -1,229 +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 - -#include "CallbackStore.h" -#include "edu_wpi_first_hal_simulation_PDPDataJNI.h" -#include "hal/simulation/PDPData.h" - -using namespace hal; - -extern "C" { - -/* - * Class: edu_wpi_first_hal_simulation_PDPDataJNI - * Method: registerInitializedCallback - * Signature: (ILjava/lang/Object;Z)I - */ -JNIEXPORT jint JNICALL -Java_edu_wpi_first_hal_simulation_PDPDataJNI_registerInitializedCallback - (JNIEnv* env, jclass, jint index, jobject callback, jboolean initialNotify) -{ - return sim::AllocateCallback(env, index, callback, initialNotify, - &HALSIM_RegisterPDPInitializedCallback); -} - -/* - * Class: edu_wpi_first_hal_simulation_PDPDataJNI - * Method: cancelInitializedCallback - * Signature: (II)V - */ -JNIEXPORT void JNICALL -Java_edu_wpi_first_hal_simulation_PDPDataJNI_cancelInitializedCallback - (JNIEnv* env, jclass, jint index, jint handle) -{ - return sim::FreeCallback(env, handle, index, - &HALSIM_CancelPDPInitializedCallback); -} - -/* - * Class: edu_wpi_first_hal_simulation_PDPDataJNI - * Method: getInitialized - * Signature: (I)Z - */ -JNIEXPORT jboolean JNICALL -Java_edu_wpi_first_hal_simulation_PDPDataJNI_getInitialized - (JNIEnv*, jclass, jint index) -{ - return HALSIM_GetPDPInitialized(index); -} - -/* - * Class: edu_wpi_first_hal_simulation_PDPDataJNI - * Method: setInitialized - * Signature: (IZ)V - */ -JNIEXPORT void JNICALL -Java_edu_wpi_first_hal_simulation_PDPDataJNI_setInitialized - (JNIEnv*, jclass, jint index, jboolean value) -{ - HALSIM_SetPDPInitialized(index, value); -} - -/* - * Class: edu_wpi_first_hal_simulation_PDPDataJNI - * Method: registerTemperatureCallback - * Signature: (ILjava/lang/Object;Z)I - */ -JNIEXPORT jint JNICALL -Java_edu_wpi_first_hal_simulation_PDPDataJNI_registerTemperatureCallback - (JNIEnv* env, jclass, jint index, jobject callback, jboolean initialNotify) -{ - return sim::AllocateCallback(env, index, callback, initialNotify, - &HALSIM_RegisterPDPTemperatureCallback); -} - -/* - * Class: edu_wpi_first_hal_simulation_PDPDataJNI - * Method: cancelTemperatureCallback - * Signature: (II)V - */ -JNIEXPORT void JNICALL -Java_edu_wpi_first_hal_simulation_PDPDataJNI_cancelTemperatureCallback - (JNIEnv* env, jclass, jint index, jint handle) -{ - return sim::FreeCallback(env, handle, index, - &HALSIM_CancelPDPTemperatureCallback); -} - -/* - * Class: edu_wpi_first_hal_simulation_PDPDataJNI - * Method: getTemperature - * Signature: (I)D - */ -JNIEXPORT jdouble JNICALL -Java_edu_wpi_first_hal_simulation_PDPDataJNI_getTemperature - (JNIEnv*, jclass, jint index) -{ - return HALSIM_GetPDPTemperature(index); -} - -/* - * Class: edu_wpi_first_hal_simulation_PDPDataJNI - * Method: setTemperature - * Signature: (ID)V - */ -JNIEXPORT void JNICALL -Java_edu_wpi_first_hal_simulation_PDPDataJNI_setTemperature - (JNIEnv*, jclass, jint index, jdouble value) -{ - HALSIM_SetPDPTemperature(index, value); -} - -/* - * Class: edu_wpi_first_hal_simulation_PDPDataJNI - * Method: registerVoltageCallback - * Signature: (ILjava/lang/Object;Z)I - */ -JNIEXPORT jint JNICALL -Java_edu_wpi_first_hal_simulation_PDPDataJNI_registerVoltageCallback - (JNIEnv* env, jclass, jint index, jobject callback, jboolean initialNotify) -{ - return sim::AllocateCallback(env, index, callback, initialNotify, - &HALSIM_RegisterPDPVoltageCallback); -} - -/* - * Class: edu_wpi_first_hal_simulation_PDPDataJNI - * Method: cancelVoltageCallback - * Signature: (II)V - */ -JNIEXPORT void JNICALL -Java_edu_wpi_first_hal_simulation_PDPDataJNI_cancelVoltageCallback - (JNIEnv* env, jclass, jint index, jint handle) -{ - return sim::FreeCallback(env, handle, index, - &HALSIM_CancelPDPVoltageCallback); -} - -/* - * Class: edu_wpi_first_hal_simulation_PDPDataJNI - * Method: getVoltage - * Signature: (I)D - */ -JNIEXPORT jdouble JNICALL -Java_edu_wpi_first_hal_simulation_PDPDataJNI_getVoltage - (JNIEnv*, jclass, jint index) -{ - return HALSIM_GetPDPVoltage(index); -} - -/* - * Class: edu_wpi_first_hal_simulation_PDPDataJNI - * Method: setVoltage - * Signature: (ID)V - */ -JNIEXPORT void JNICALL -Java_edu_wpi_first_hal_simulation_PDPDataJNI_setVoltage - (JNIEnv*, jclass, jint index, jdouble value) -{ - HALSIM_SetPDPVoltage(index, value); -} - -/* - * Class: edu_wpi_first_hal_simulation_PDPDataJNI - * Method: registerCurrentCallback - * Signature: (IILjava/lang/Object;Z)I - */ -JNIEXPORT jint JNICALL -Java_edu_wpi_first_hal_simulation_PDPDataJNI_registerCurrentCallback - (JNIEnv* env, jclass, jint index, jint channel, jobject callback, - jboolean initialNotify) -{ - return sim::AllocateChannelCallback(env, index, channel, callback, - initialNotify, - &HALSIM_RegisterPDPCurrentCallback); -} - -/* - * Class: edu_wpi_first_hal_simulation_PDPDataJNI - * Method: cancelCurrentCallback - * Signature: (III)V - */ -JNIEXPORT void JNICALL -Java_edu_wpi_first_hal_simulation_PDPDataJNI_cancelCurrentCallback - (JNIEnv* env, jclass, jint index, jint channel, jint handle) -{ - return sim::FreeChannelCallback(env, handle, index, channel, - &HALSIM_CancelPDPCurrentCallback); -} - -/* - * Class: edu_wpi_first_hal_simulation_PDPDataJNI - * Method: getCurrent - * Signature: (II)D - */ -JNIEXPORT jdouble JNICALL -Java_edu_wpi_first_hal_simulation_PDPDataJNI_getCurrent - (JNIEnv*, jclass, jint index, jint channel) -{ - return HALSIM_GetPDPCurrent(index, channel); -} - -/* - * Class: edu_wpi_first_hal_simulation_PDPDataJNI - * Method: setCurrent - * Signature: (IID)V - */ -JNIEXPORT void JNICALL -Java_edu_wpi_first_hal_simulation_PDPDataJNI_setCurrent - (JNIEnv*, jclass, jint index, jint channel, jdouble value) -{ - HALSIM_SetPDPCurrent(index, channel, value); -} - -/* - * Class: edu_wpi_first_hal_simulation_PDPDataJNI - * Method: resetData - * Signature: (I)V - */ -JNIEXPORT void JNICALL -Java_edu_wpi_first_hal_simulation_PDPDataJNI_resetData - (JNIEnv*, jclass, jint index) -{ - HALSIM_ResetPDPData(index); -} - -} // extern "C" diff --git a/hal/src/main/native/cpp/jni/simulation/PowerDistributionDataJNI.cpp b/hal/src/main/native/cpp/jni/simulation/PowerDistributionDataJNI.cpp new file mode 100644 index 0000000000..517dc09b91 --- /dev/null +++ b/hal/src/main/native/cpp/jni/simulation/PowerDistributionDataJNI.cpp @@ -0,0 +1,233 @@ +// 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 + +#include "CallbackStore.h" +#include "edu_wpi_first_hal_simulation_PowerDistributionDataJNI.h" +#include "hal/simulation/PowerDistributionData.h" + +using namespace hal; + +extern "C" { + +/* + * Class: edu_wpi_first_hal_simulation_PowerDistributionDataJNI + * Method: registerInitializedCallback + * Signature: (ILjava/lang/Object;Z)I + */ +JNIEXPORT jint JNICALL +Java_edu_wpi_first_hal_simulation_PowerDistributionDataJNI_registerInitializedCallback + (JNIEnv* env, jclass, jint index, jobject callback, jboolean initialNotify) +{ + return sim::AllocateCallback( + env, index, callback, initialNotify, + &HALSIM_RegisterPowerDistributionInitializedCallback); +} + +/* + * Class: edu_wpi_first_hal_simulation_PowerDistributionDataJNI + * Method: cancelInitializedCallback + * Signature: (II)V + */ +JNIEXPORT void JNICALL +Java_edu_wpi_first_hal_simulation_PowerDistributionDataJNI_cancelInitializedCallback + (JNIEnv* env, jclass, jint index, jint handle) +{ + return sim::FreeCallback(env, handle, index, + &HALSIM_CancelPowerDistributionInitializedCallback); +} + +/* + * Class: edu_wpi_first_hal_simulation_PowerDistributionDataJNI + * Method: getInitialized + * Signature: (I)Z + */ +JNIEXPORT jboolean JNICALL +Java_edu_wpi_first_hal_simulation_PowerDistributionDataJNI_getInitialized + (JNIEnv*, jclass, jint index) +{ + return HALSIM_GetPowerDistributionInitialized(index); +} + +/* + * Class: edu_wpi_first_hal_simulation_PowerDistributionDataJNI + * Method: setInitialized + * Signature: (IZ)V + */ +JNIEXPORT void JNICALL +Java_edu_wpi_first_hal_simulation_PowerDistributionDataJNI_setInitialized + (JNIEnv*, jclass, jint index, jboolean value) +{ + HALSIM_SetPowerDistributionInitialized(index, value); +} + +/* + * Class: edu_wpi_first_hal_simulation_PowerDistributionDataJNI + * Method: registerTemperatureCallback + * Signature: (ILjava/lang/Object;Z)I + */ +JNIEXPORT jint JNICALL +Java_edu_wpi_first_hal_simulation_PowerDistributionDataJNI_registerTemperatureCallback + (JNIEnv* env, jclass, jint index, jobject callback, jboolean initialNotify) +{ + return sim::AllocateCallback( + env, index, callback, initialNotify, + &HALSIM_RegisterPowerDistributionTemperatureCallback); +} + +/* + * Class: edu_wpi_first_hal_simulation_PowerDistributionDataJNI + * Method: cancelTemperatureCallback + * Signature: (II)V + */ +JNIEXPORT void JNICALL +Java_edu_wpi_first_hal_simulation_PowerDistributionDataJNI_cancelTemperatureCallback + (JNIEnv* env, jclass, jint index, jint handle) +{ + return sim::FreeCallback(env, handle, index, + &HALSIM_CancelPowerDistributionTemperatureCallback); +} + +/* + * Class: edu_wpi_first_hal_simulation_PowerDistributionDataJNI + * Method: getTemperature + * Signature: (I)D + */ +JNIEXPORT jdouble JNICALL +Java_edu_wpi_first_hal_simulation_PowerDistributionDataJNI_getTemperature + (JNIEnv*, jclass, jint index) +{ + return HALSIM_GetPowerDistributionTemperature(index); +} + +/* + * Class: edu_wpi_first_hal_simulation_PowerDistributionDataJNI + * Method: setTemperature + * Signature: (ID)V + */ +JNIEXPORT void JNICALL +Java_edu_wpi_first_hal_simulation_PowerDistributionDataJNI_setTemperature + (JNIEnv*, jclass, jint index, jdouble value) +{ + HALSIM_SetPowerDistributionTemperature(index, value); +} + +/* + * Class: edu_wpi_first_hal_simulation_PowerDistributionDataJNI + * Method: registerVoltageCallback + * Signature: (ILjava/lang/Object;Z)I + */ +JNIEXPORT jint JNICALL +Java_edu_wpi_first_hal_simulation_PowerDistributionDataJNI_registerVoltageCallback + (JNIEnv* env, jclass, jint index, jobject callback, jboolean initialNotify) +{ + return sim::AllocateCallback( + env, index, callback, initialNotify, + &HALSIM_RegisterPowerDistributionVoltageCallback); +} + +/* + * Class: edu_wpi_first_hal_simulation_PowerDistributionDataJNI + * Method: cancelVoltageCallback + * Signature: (II)V + */ +JNIEXPORT void JNICALL +Java_edu_wpi_first_hal_simulation_PowerDistributionDataJNI_cancelVoltageCallback + (JNIEnv* env, jclass, jint index, jint handle) +{ + return sim::FreeCallback(env, handle, index, + &HALSIM_CancelPowerDistributionVoltageCallback); +} + +/* + * Class: edu_wpi_first_hal_simulation_PowerDistributionDataJNI + * Method: getVoltage + * Signature: (I)D + */ +JNIEXPORT jdouble JNICALL +Java_edu_wpi_first_hal_simulation_PowerDistributionDataJNI_getVoltage + (JNIEnv*, jclass, jint index) +{ + return HALSIM_GetPowerDistributionVoltage(index); +} + +/* + * Class: edu_wpi_first_hal_simulation_PowerDistributionDataJNI + * Method: setVoltage + * Signature: (ID)V + */ +JNIEXPORT void JNICALL +Java_edu_wpi_first_hal_simulation_PowerDistributionDataJNI_setVoltage + (JNIEnv*, jclass, jint index, jdouble value) +{ + HALSIM_SetPowerDistributionVoltage(index, value); +} + +/* + * Class: edu_wpi_first_hal_simulation_PowerDistributionDataJNI + * Method: registerCurrentCallback + * Signature: (IILjava/lang/Object;Z)I + */ +JNIEXPORT jint JNICALL +Java_edu_wpi_first_hal_simulation_PowerDistributionDataJNI_registerCurrentCallback + (JNIEnv* env, jclass, jint index, jint channel, jobject callback, + jboolean initialNotify) +{ + return sim::AllocateChannelCallback( + env, index, channel, callback, initialNotify, + &HALSIM_RegisterPowerDistributionCurrentCallback); +} + +/* + * Class: edu_wpi_first_hal_simulation_PowerDistributionDataJNI + * Method: cancelCurrentCallback + * Signature: (III)V + */ +JNIEXPORT void JNICALL +Java_edu_wpi_first_hal_simulation_PowerDistributionDataJNI_cancelCurrentCallback + (JNIEnv* env, jclass, jint index, jint channel, jint handle) +{ + return sim::FreeChannelCallback( + env, handle, index, channel, + &HALSIM_CancelPowerDistributionCurrentCallback); +} + +/* + * Class: edu_wpi_first_hal_simulation_PowerDistributionDataJNI + * Method: getCurrent + * Signature: (II)D + */ +JNIEXPORT jdouble JNICALL +Java_edu_wpi_first_hal_simulation_PowerDistributionDataJNI_getCurrent + (JNIEnv*, jclass, jint index, jint channel) +{ + return HALSIM_GetPowerDistributionCurrent(index, channel); +} + +/* + * Class: edu_wpi_first_hal_simulation_PowerDistributionDataJNI + * Method: setCurrent + * Signature: (IID)V + */ +JNIEXPORT void JNICALL +Java_edu_wpi_first_hal_simulation_PowerDistributionDataJNI_setCurrent + (JNIEnv*, jclass, jint index, jint channel, jdouble value) +{ + HALSIM_SetPowerDistributionCurrent(index, channel, value); +} + +/* + * Class: edu_wpi_first_hal_simulation_PowerDistributionDataJNI + * Method: resetData + * Signature: (I)V + */ +JNIEXPORT void JNICALL +Java_edu_wpi_first_hal_simulation_PowerDistributionDataJNI_resetData + (JNIEnv*, jclass, jint index) +{ + HALSIM_ResetPowerDistributionData(index); +} + +} // extern "C" diff --git a/hal/src/main/native/include/hal/HAL.h b/hal/src/main/native/include/hal/HAL.h index d75463cf71..0d6023729b 100644 --- a/hal/src/main/native/include/hal/HAL.h +++ b/hal/src/main/native/include/hal/HAL.h @@ -27,7 +27,6 @@ #include "hal/Interrupts.h" #include "hal/Main.h" #include "hal/Notifier.h" -#include "hal/PDP.h" #include "hal/PWM.h" #include "hal/Ports.h" #include "hal/Power.h" diff --git a/hal/src/main/native/include/hal/PowerDistribution.h b/hal/src/main/native/include/hal/PowerDistribution.h new file mode 100644 index 0000000000..e9ecf5e6e5 --- /dev/null +++ b/hal/src/main/native/include/hal/PowerDistribution.h @@ -0,0 +1,163 @@ +// 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 + +#include "hal/Types.h" + +/** + * @defgroup hal_pd Power Distribution Functions + * @ingroup hal_capi + * Functions to control Power Distribution devices. + * @{ + */ + +// clang-format off +/** + * The acceptable accelerometer ranges. + */ +HAL_ENUM(HAL_PowerDistributionType) { + HAL_PowerDistributionType_kAutomatic = 0, + HAL_PowerDistributionType_kCTRE = 1, + HAL_PowerDistributionType_kRev = 2, +}; +// clang-format on + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * Initializes a Power Distribution Panel. + * + * @param module the module number to initialize + * @param type the type of module to intialize + * @return the created PowerDistribution + */ +HAL_PowerDistributionHandle HAL_InitializePowerDistribution( + int32_t moduleNumber, HAL_PowerDistributionType type, int32_t* status); + +/** + * Cleans a PowerDistribution module. + * + * @param handle the module handle + */ +void HAL_CleanPowerDistribution(HAL_PowerDistributionHandle handle); + +/** + * Checks if a PowerDistribution channel is valid. + * + * @param handle the module handle + * @param channel the channel to check + * @return true if the channel is valid, otherwise false + */ +HAL_Bool HAL_CheckPowerDistributionChannel(HAL_PowerDistributionHandle handle, + int32_t channel); + +/** + * Checks if a PowerDistribution module is valid. + * + * @param channel the module to check + * @return true if the module is valid, otherwise false + */ +HAL_Bool HAL_CheckPowerDistributionModule(int32_t module, + HAL_PowerDistributionType type); + +/** + * Gets the type of PowerDistribution module. + * + * @return the type of module + */ +HAL_PowerDistributionType HAL_GetPowerDistributionType( + HAL_PowerDistributionHandle handle, int32_t* status); + +/** + * Gets the temperature of the PowerDistribution. + * + * @param handle the module handle + * @return the module temperature (celsius) + */ +double HAL_GetPowerDistributionTemperature(HAL_PowerDistributionHandle handle, + int32_t* status); + +/** + * Gets the PowerDistribution input voltage. + * + * @param handle the module handle + * @return the input voltage (volts) + */ +double HAL_GetPowerDistributionVoltage(HAL_PowerDistributionHandle handle, + int32_t* status); + +/** + * Gets the current of a specific PowerDistribution channel. + * + * @param module the module + * @param channel the channel + * @return the channel current (amps) + */ +double HAL_GetPowerDistributionChannelCurrent( + HAL_PowerDistributionHandle handle, int32_t channel, int32_t* status); + +/** + * Gets the current of all 16 channels on the PowerDistribution. + * + * The array must be large enough to hold all channels. + * + * @param handle the module handle + * @param currents the currents (output) + * @param currentsLength the length of the currents array + */ +void HAL_GetPowerDistributionAllChannelCurrents( + HAL_PowerDistributionHandle handle, double* currents, + int32_t currentsLength, int32_t* status); + +/** + * Gets the total current of the PowerDistribution. + * + * @param handle the module handle + * @return the total current (amps) + */ +double HAL_GetPowerDistributionTotalCurrent(HAL_PowerDistributionHandle handle, + int32_t* status); + +/** + * Gets the total power of the PowerDistribution. + * + * @param handle the module handle + * @return the total power (watts) + */ +double HAL_GetPowerDistributionTotalPower(HAL_PowerDistributionHandle handle, + int32_t* status); + +/** + * Gets the total energy of the PowerDistribution. + * + * @param handle the module handle + * @return the total energy (joules) + */ +double HAL_GetPowerDistributionTotalEnergy(HAL_PowerDistributionHandle handle, + int32_t* status); + +/** + * Resets the PowerDistribution accumulated energy. + * + * @param handle the module handle + */ +void HAL_ResetPowerDistributionTotalEnergy(HAL_PowerDistributionHandle handle, + int32_t* status); + +/** + * Clears any PowerDistribution sticky faults. + * + * @param handle the module handle + */ +void HAL_ClearPowerDistributionStickyFaults(HAL_PowerDistributionHandle handle, + int32_t* status); +#ifdef __cplusplus +} // extern "C" +#endif +/** @} */ diff --git a/hal/src/main/native/include/hal/Types.h b/hal/src/main/native/include/hal/Types.h index dd9303e368..ec9f739af2 100644 --- a/hal/src/main/native/include/hal/Types.h +++ b/hal/src/main/native/include/hal/Types.h @@ -62,6 +62,8 @@ typedef HAL_Handle HAL_AddressableLEDHandle; typedef HAL_CANHandle HAL_PDPHandle; +typedef HAL_Handle HAL_PowerDistributionHandle; + typedef HAL_Handle HAL_CTREPCMHandle; typedef int32_t HAL_Bool; diff --git a/hal/src/main/native/include/hal/handles/HandlesInternal.h b/hal/src/main/native/include/hal/handles/HandlesInternal.h index 4c0ea508da..c37a1ab229 100644 --- a/hal/src/main/native/include/hal/handles/HandlesInternal.h +++ b/hal/src/main/native/include/hal/handles/HandlesInternal.h @@ -67,6 +67,7 @@ enum class HAL_HandleEnum { DMA = 22, AddressableLED = 23, CTREPCM = 24, + CTREPDP = 25 }; /** diff --git a/hal/src/main/native/include/hal/simulation/PDPData.h b/hal/src/main/native/include/hal/simulation/PDPData.h deleted file mode 100644 index d29245ab49..0000000000 --- a/hal/src/main/native/include/hal/simulation/PDPData.h +++ /dev/null @@ -1,56 +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. - -#pragma once - -#include "hal/Types.h" -#include "hal/simulation/NotifyListener.h" - -#ifdef __cplusplus -extern "C" { -#endif - -void HALSIM_ResetPDPData(int32_t index); -int32_t HALSIM_RegisterPDPInitializedCallback(int32_t index, - HAL_NotifyCallback callback, - void* param, - HAL_Bool initialNotify); -void HALSIM_CancelPDPInitializedCallback(int32_t index, int32_t uid); -HAL_Bool HALSIM_GetPDPInitialized(int32_t index); -void HALSIM_SetPDPInitialized(int32_t index, HAL_Bool initialized); - -int32_t HALSIM_RegisterPDPTemperatureCallback(int32_t index, - HAL_NotifyCallback callback, - void* param, - HAL_Bool initialNotify); -void HALSIM_CancelPDPTemperatureCallback(int32_t index, int32_t uid); -double HALSIM_GetPDPTemperature(int32_t index); -void HALSIM_SetPDPTemperature(int32_t index, double temperature); - -int32_t HALSIM_RegisterPDPVoltageCallback(int32_t index, - HAL_NotifyCallback callback, - void* param, HAL_Bool initialNotify); -void HALSIM_CancelPDPVoltageCallback(int32_t index, int32_t uid); -double HALSIM_GetPDPVoltage(int32_t index); -void HALSIM_SetPDPVoltage(int32_t index, double voltage); - -int32_t HALSIM_RegisterPDPCurrentCallback(int32_t index, int32_t channel, - HAL_NotifyCallback callback, - void* param, HAL_Bool initialNotify); -void HALSIM_CancelPDPCurrentCallback(int32_t index, int32_t channel, - int32_t uid); -double HALSIM_GetPDPCurrent(int32_t index, int32_t channel); -void HALSIM_SetPDPCurrent(int32_t index, int32_t channel, double current); - -void HALSIM_GetPDPAllCurrents(int32_t index, double* currents); -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); - -#ifdef __cplusplus -} // extern "C" -#endif diff --git a/hal/src/main/native/include/hal/simulation/PowerDistributionData.h b/hal/src/main/native/include/hal/simulation/PowerDistributionData.h new file mode 100644 index 0000000000..c355bfd042 --- /dev/null +++ b/hal/src/main/native/include/hal/simulation/PowerDistributionData.h @@ -0,0 +1,59 @@ +// 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 "hal/Types.h" +#include "hal/simulation/NotifyListener.h" + +#ifdef __cplusplus +extern "C" { +#endif + +void HALSIM_ResetPowerDistributionData(int32_t index); +int32_t HALSIM_RegisterPowerDistributionInitializedCallback( + int32_t index, HAL_NotifyCallback callback, void* param, + HAL_Bool initialNotify); +void HALSIM_CancelPowerDistributionInitializedCallback(int32_t index, + int32_t uid); +HAL_Bool HALSIM_GetPowerDistributionInitialized(int32_t index); +void HALSIM_SetPowerDistributionInitialized(int32_t index, + HAL_Bool initialized); + +int32_t HALSIM_RegisterPowerDistributionTemperatureCallback( + int32_t index, HAL_NotifyCallback callback, void* param, + HAL_Bool initialNotify); +void HALSIM_CancelPowerDistributionTemperatureCallback(int32_t index, + int32_t uid); +double HALSIM_GetPowerDistributionTemperature(int32_t index); +void HALSIM_SetPowerDistributionTemperature(int32_t index, double temperature); + +int32_t HALSIM_RegisterPowerDistributionVoltageCallback( + int32_t index, HAL_NotifyCallback callback, void* param, + HAL_Bool initialNotify); +void HALSIM_CancelPowerDistributionVoltageCallback(int32_t index, int32_t uid); +double HALSIM_GetPowerDistributionVoltage(int32_t index); +void HALSIM_SetPowerDistributionVoltage(int32_t index, double voltage); + +int32_t HALSIM_RegisterPowerDistributionCurrentCallback( + int32_t index, int32_t channel, HAL_NotifyCallback callback, void* param, + HAL_Bool initialNotify); +void HALSIM_CancelPowerDistributionCurrentCallback(int32_t index, + int32_t channel, + int32_t uid); +double HALSIM_GetPowerDistributionCurrent(int32_t index, int32_t channel); +void HALSIM_SetPowerDistributionCurrent(int32_t index, int32_t channel, + double current); + +void HALSIM_GetPowerDistributionAllCurrents(int32_t index, double* currents); +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); + +#ifdef __cplusplus +} // extern "C" +#endif diff --git a/hal/src/main/native/sim/HAL.cpp b/hal/src/main/native/sim/HAL.cpp index 0df981ad2e..3b7b6314e5 100644 --- a/hal/src/main/native/sim/HAL.cpp +++ b/hal/src/main/native/sim/HAL.cpp @@ -74,7 +74,7 @@ void InitializeHAL() { InitializeEncoderData(); InitializeI2CData(); InitializeCTREPCMData(); - InitializePDPData(); + InitializePowerDistributionData(); InitializePWMData(); InitializeRelayData(); InitializeRoboRioData(); @@ -103,7 +103,7 @@ void InitializeHAL() { InitializeMain(); InitializeMockHooks(); InitializeNotifier(); - InitializePDP(); + InitializePowerDistribution(); InitializePorts(); InitializePower(); InitializeCTREPCM(); diff --git a/hal/src/main/native/sim/HALInitializer.h b/hal/src/main/native/sim/HALInitializer.h index ab5b818e73..3d96d8a18a 100644 --- a/hal/src/main/native/sim/HALInitializer.h +++ b/hal/src/main/native/sim/HALInitializer.h @@ -32,7 +32,7 @@ extern void InitializeDriverStationData(); extern void InitializeEncoderData(); extern void InitializeI2CData(); extern void InitializeCTREPCMData(); -extern void InitializePDPData(); +extern void InitializePowerDistributionData(); extern void InitializePWMData(); extern void InitializeRelayData(); extern void InitializeRoboRioData(); @@ -61,7 +61,7 @@ extern void InitializeInterrupts(); extern void InitializeMain(); extern void InitializeMockHooks(); extern void InitializeNotifier(); -extern void InitializePDP(); +extern void InitializePowerDistribution(); extern void InitializePorts(); extern void InitializePower(); extern void InitializeCTREPCM(); diff --git a/hal/src/main/native/sim/PDP.cpp b/hal/src/main/native/sim/PDP.cpp deleted file mode 100644 index f6c6bcb380..0000000000 --- a/hal/src/main/native/sim/PDP.cpp +++ /dev/null @@ -1,105 +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/PDP.h" - -#include - -#include "CANAPIInternal.h" -#include "HALInitializer.h" -#include "HALInternal.h" -#include "PortsInternal.h" -#include "hal/CANAPI.h" -#include "hal/Errors.h" -#include "mockdata/PDPDataInternal.h" - -using namespace hal; - -static constexpr HAL_CANManufacturer manufacturer = - HAL_CANManufacturer::HAL_CAN_Man_kCTRE; - -static constexpr HAL_CANDeviceType deviceType = - HAL_CANDeviceType::HAL_CAN_Dev_kPowerDistribution; - -namespace hal::init { -void InitializePDP() {} -} // namespace hal::init - -extern "C" { -HAL_PDPHandle HAL_InitializePDP(int32_t module, int32_t* status) { - if (!HAL_CheckPDPModule(module)) { - *status = PARAMETER_OUT_OF_RANGE; - hal::SetLastError(status, fmt::format("Invalid pdp module {}", module)); - return HAL_kInvalidHandle; - } - hal::init::CheckInit(); - SimPDPData[module].initialized = true; - auto handle = HAL_InitializeCAN(manufacturer, module, deviceType, status); - - if (*status != 0) { - HAL_CleanCAN(handle); - return HAL_kInvalidHandle; - } - - return handle; -} - -HAL_Bool HAL_CheckPDPModule(int32_t module) { - return module < kNumPDPModules && module >= 0; -} - -HAL_Bool HAL_CheckPDPChannel(int32_t channel) { - return channel < kNumPDPChannels && channel >= 0; -} - -void HAL_CleanPDP(HAL_PDPHandle handle) { - HAL_CleanCAN(handle); -} - -double HAL_GetPDPTemperature(HAL_PDPHandle handle, int32_t* status) { - auto module = hal::can::GetCANModuleFromHandle(handle, status); - if (*status != 0) { - return 0.0; - } - return SimPDPData[module].temperature; -} -double HAL_GetPDPVoltage(HAL_PDPHandle handle, int32_t* status) { - auto module = hal::can::GetCANModuleFromHandle(handle, status); - if (*status != 0) { - return 0.0; - } - return SimPDPData[module].voltage; -} -double HAL_GetPDPChannelCurrent(HAL_PDPHandle handle, int32_t channel, - int32_t* status) { - auto module = hal::can::GetCANModuleFromHandle(handle, status); - if (*status != 0) { - return 0.0; - } - return SimPDPData[module].current[channel]; -} -void HAL_GetPDPAllChannelCurrents(HAL_PDPHandle handle, double* currents, - int32_t* status) { - auto module = hal::can::GetCANModuleFromHandle(handle, status); - if (*status != 0) { - return; - } - - auto& data = SimPDPData[module]; - for (int i = 0; i < kNumPDPChannels; i++) { - currents[i] = data.current[i]; - } -} -double HAL_GetPDPTotalCurrent(HAL_PDPHandle handle, int32_t* status) { - return 0.0; -} -double HAL_GetPDPTotalPower(HAL_PDPHandle handle, int32_t* status) { - return 0.0; -} -double HAL_GetPDPTotalEnergy(HAL_PDPHandle handle, int32_t* status) { - return 0.0; -} -void HAL_ResetPDPTotalEnergy(HAL_PDPHandle handle, int32_t* status) {} -void HAL_ClearPDPStickyFaults(HAL_PDPHandle handle, int32_t* status) {} -} // extern "C" diff --git a/hal/src/main/native/sim/PowerDistribution.cpp b/hal/src/main/native/sim/PowerDistribution.cpp new file mode 100644 index 0000000000..3a37cc7d0b --- /dev/null +++ b/hal/src/main/native/sim/PowerDistribution.cpp @@ -0,0 +1,121 @@ +// 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 + +#include "CANAPIInternal.h" +#include "HALInitializer.h" +#include "HALInternal.h" +#include "PortsInternal.h" +#include "hal/CANAPI.h" +#include "hal/Errors.h" +#include "mockdata/PowerDistributionDataInternal.h" + +using namespace hal; + +static constexpr HAL_CANManufacturer manufacturer = + HAL_CANManufacturer::HAL_CAN_Man_kCTRE; + +static constexpr HAL_CANDeviceType deviceType = + HAL_CANDeviceType::HAL_CAN_Dev_kPowerDistribution; + +namespace hal::init { +void InitializePowerDistribution() {} +} // namespace hal::init + +extern "C" { +HAL_PowerDistributionHandle HAL_InitializePowerDistribution( + int32_t module, HAL_PowerDistributionType type, int32_t* status) { + if (!HAL_CheckPowerDistributionModule(module, type)) { + *status = PARAMETER_OUT_OF_RANGE; + hal::SetLastError(status, fmt::format("Invalid pdp module {}", module)); + return HAL_kInvalidHandle; + } + hal::init::CheckInit(); + SimPowerDistributionData[module].initialized = true; + auto handle = HAL_InitializeCAN(manufacturer, module, deviceType, status); + + if (*status != 0) { + HAL_CleanCAN(handle); + return HAL_kInvalidHandle; + } + + return handle; +} + +HAL_Bool HAL_CheckPowerDistributionModule(int32_t module, + HAL_PowerDistributionType type) { + return module < kNumPDPModules && module >= 0; +} + +HAL_Bool HAL_CheckPowerDistributionChannel(HAL_PowerDistributionHandle handle, + int32_t channel) { + return channel < kNumPDPChannels && channel >= 0; +} + +HAL_PowerDistributionType HAL_GetPowerDistributionType( + HAL_PowerDistributionHandle handle, int32_t* status) { + return HAL_PowerDistributionType::HAL_PowerDistributionType_kCTRE; +} + +void HAL_CleanPowerDistribution(HAL_PowerDistributionHandle handle) { + HAL_CleanCAN(handle); +} + +double HAL_GetPowerDistributionTemperature(HAL_PowerDistributionHandle handle, + int32_t* status) { + auto module = hal::can::GetCANModuleFromHandle(handle, status); + if (*status != 0) { + return 0.0; + } + return SimPowerDistributionData[module].temperature; +} +double HAL_GetPowerDistributionVoltage(HAL_PowerDistributionHandle handle, + int32_t* status) { + auto module = hal::can::GetCANModuleFromHandle(handle, status); + if (*status != 0) { + return 0.0; + } + return SimPowerDistributionData[module].voltage; +} +double HAL_GetPowerDistributionChannelCurrent( + HAL_PowerDistributionHandle handle, int32_t channel, int32_t* status) { + auto module = hal::can::GetCANModuleFromHandle(handle, status); + if (*status != 0) { + return 0.0; + } + return SimPowerDistributionData[module].current[channel]; +} +void HAL_GetPowerDistributionAllChannelCurrents( + HAL_PowerDistributionHandle handle, double* currents, + int32_t currentsLength, int32_t* status) { + auto module = hal::can::GetCANModuleFromHandle(handle, status); + if (*status != 0) { + return; + } + + auto& data = SimPowerDistributionData[module]; + for (int i = 0; i < kNumPDPChannels; i++) { + currents[i] = data.current[i]; + } +} +double HAL_GetPowerDistributionTotalCurrent(HAL_PowerDistributionHandle handle, + int32_t* status) { + return 0.0; +} +double HAL_GetPowerDistributionTotalPower(HAL_PowerDistributionHandle handle, + int32_t* status) { + return 0.0; +} +double HAL_GetPowerDistributionTotalEnergy(HAL_PowerDistributionHandle handle, + int32_t* status) { + return 0.0; +} +void HAL_ResetPowerDistributionTotalEnergy(HAL_PowerDistributionHandle handle, + int32_t* status) {} +void HAL_ClearPowerDistributionStickyFaults(HAL_PowerDistributionHandle handle, + int32_t* status) {} +} // extern "C" diff --git a/hal/src/main/native/sim/mockdata/PDPData.cpp b/hal/src/main/native/sim/mockdata/PDPData.cpp deleted file mode 100644 index 30152209a1..0000000000 --- a/hal/src/main/native/sim/mockdata/PDPData.cpp +++ /dev/null @@ -1,67 +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 "../PortsInternal.h" -#include "PDPDataInternal.h" - -using namespace hal; - -namespace hal::init { -void InitializePDPData() { - static PDPData spd[kNumPDPModules]; - ::hal::SimPDPData = spd; -} -} // namespace hal::init - -PDPData* hal::SimPDPData; -void PDPData::ResetData() { - initialized.Reset(false); - temperature.Reset(0.0); - voltage.Reset(12.0); - for (int i = 0; i < kNumPDPChannels; i++) { - current[i].Reset(0.0); - } -} - -extern "C" { -void HALSIM_ResetPDPData(int32_t index) { - SimPDPData[index].ResetData(); -} - -#define DEFINE_CAPI(TYPE, CAPINAME, LOWERNAME) \ - HAL_SIMDATAVALUE_DEFINE_CAPI(TYPE, HALSIM, PDP##CAPINAME, SimPDPData, \ - LOWERNAME) - -DEFINE_CAPI(HAL_Bool, Initialized, initialized) -DEFINE_CAPI(double, Temperature, temperature) -DEFINE_CAPI(double, Voltage, voltage) -HAL_SIMDATAVALUE_DEFINE_CAPI_CHANNEL(double, HALSIM, PDPCurrent, SimPDPData, - current) - -void HALSIM_GetPDPAllCurrents(int32_t index, double* currents) { - auto& data = SimPDPData[index].current; - for (int i = 0; i < kNumPDPChannels; i++) { - currents[i] = data[i]; - } -} - -void HALSIM_SetPDPAllCurrents(int32_t index, const double* currents) { - auto& data = SimPDPData[index].current; - for (int i = 0; i < kNumPDPChannels; i++) { - data[i] = currents[i]; - } -} - -#define REGISTER(NAME) \ - SimPDPData[index].NAME.RegisterCallback(callback, param, initialNotify) - -void HALSIM_RegisterPDPAllNonCurrentCallbacks(int32_t index, int32_t channel, - HAL_NotifyCallback callback, - void* param, - HAL_Bool initialNotify) { - REGISTER(initialized); - REGISTER(temperature); - REGISTER(voltage); -} -} // extern "C" diff --git a/hal/src/main/native/sim/mockdata/PowerDistributionData.cpp b/hal/src/main/native/sim/mockdata/PowerDistributionData.cpp new file mode 100644 index 0000000000..4234e49e87 --- /dev/null +++ b/hal/src/main/native/sim/mockdata/PowerDistributionData.cpp @@ -0,0 +1,68 @@ +// 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 "../PortsInternal.h" +#include "PowerDistributionDataInternal.h" + +using namespace hal; + +namespace hal::init { +void InitializePowerDistributionData() { + static PowerDistributionData spd[kNumPDPChannels]; + ::hal::SimPowerDistributionData = spd; +} +} // namespace hal::init + +PowerDistributionData* hal::SimPowerDistributionData; +void PowerDistributionData::ResetData() { + initialized.Reset(false); + temperature.Reset(0.0); + voltage.Reset(12.0); + for (int i = 0; i < kNumPDPChannels; i++) { + current[i].Reset(0.0); + } +} + +extern "C" { +void HALSIM_ResetPowerDistributionData(int32_t index) { + SimPowerDistributionData[index].ResetData(); +} + +#define DEFINE_CAPI(TYPE, CAPINAME, LOWERNAME) \ + HAL_SIMDATAVALUE_DEFINE_CAPI(TYPE, HALSIM, PowerDistribution##CAPINAME, \ + SimPowerDistributionData, LOWERNAME) + +DEFINE_CAPI(HAL_Bool, Initialized, initialized) +DEFINE_CAPI(double, Temperature, temperature) +DEFINE_CAPI(double, Voltage, voltage) +HAL_SIMDATAVALUE_DEFINE_CAPI_CHANNEL(double, HALSIM, PowerDistributionCurrent, + SimPowerDistributionData, current) + +void HALSIM_GetPowerDistributionAllCurrents(int32_t index, double* currents) { + auto& data = SimPowerDistributionData[index].current; + for (int i = 0; i < kNumPDPChannels; i++) { + currents[i] = data[i]; + } +} + +void HALSIM_SetPowerDistributionAllCurrents(int32_t index, + const double* currents) { + auto& data = SimPowerDistributionData[index].current; + for (int i = 0; i < kNumPDPChannels; i++) { + data[i] = currents[i]; + } +} + +#define REGISTER(NAME) \ + SimPowerDistributionData[index].NAME.RegisterCallback(callback, param, \ + initialNotify) + +void HALSIM_RegisterPowerDistributionAllNonCurrentCallbacks( + int32_t index, int32_t channel, HAL_NotifyCallback callback, void* param, + HAL_Bool initialNotify) { + REGISTER(initialized); + REGISTER(temperature); + REGISTER(voltage); +} +} // extern "C" diff --git a/hal/src/main/native/sim/mockdata/PDPDataInternal.h b/hal/src/main/native/sim/mockdata/PowerDistributionDataInternal.h similarity index 87% rename from hal/src/main/native/sim/mockdata/PDPDataInternal.h rename to hal/src/main/native/sim/mockdata/PowerDistributionDataInternal.h index 8ac1f699fb..49a1f87cc8 100644 --- a/hal/src/main/native/sim/mockdata/PDPDataInternal.h +++ b/hal/src/main/native/sim/mockdata/PowerDistributionDataInternal.h @@ -5,11 +5,11 @@ #pragma once #include "../PortsInternal.h" -#include "hal/simulation/PDPData.h" +#include "hal/simulation/PowerDistributionData.h" #include "hal/simulation/SimDataValue.h" namespace hal { -class PDPData { +class PowerDistributionData { HAL_SIMDATAVALUE_DEFINE_NAME(Initialized) HAL_SIMDATAVALUE_DEFINE_NAME(Temperature) HAL_SIMDATAVALUE_DEFINE_NAME(Voltage) @@ -29,5 +29,5 @@ class PDPData { virtual void ResetData(); }; -extern PDPData* SimPDPData; +extern PowerDistributionData* SimPowerDistributionData; } // namespace hal diff --git a/hal/src/test/native/cpp/mockdata/PDPDataTests.cpp b/hal/src/test/native/cpp/mockdata/PDPDataTests.cpp index 76de8f775d..6ac1e7f04c 100644 --- a/hal/src/test/native/cpp/mockdata/PDPDataTests.cpp +++ b/hal/src/test/native/cpp/mockdata/PDPDataTests.cpp @@ -4,9 +4,9 @@ #include "gtest/gtest.h" #include "hal/HAL.h" -#include "hal/PDP.h" +#include "hal/PowerDistribution.h" #include "hal/handles/HandlesInternal.h" -#include "hal/simulation/PDPData.h" +#include "hal/simulation/PowerDistributionData.h" namespace hal { @@ -23,7 +23,7 @@ TEST(PdpSimTests, TestPdpInitialization) { const int INDEX_TO_TEST = 1; int callbackParam = 0; - int callbackId = HALSIM_RegisterPDPInitializedCallback( + int callbackId = HALSIM_RegisterPowerDistributionInitializedCallback( INDEX_TO_TEST, &TestPdpInitializationCallback, &callbackParam, false); ASSERT_TRUE(0 != callbackId); @@ -31,7 +31,8 @@ TEST(PdpSimTests, TestPdpInitialization) { // Use out of range index gTestPdpCallbackName = "Unset"; - HAL_InitializePDP(INDEX_TO_TEST, &status); + HAL_InitializePowerDistribution(INDEX_TO_TEST, + HAL_PowerDistributionType_kCTRE, &status); EXPECT_EQ(0, status); EXPECT_STREQ("Initialized", gTestPdpCallbackName.c_str()); } diff --git a/simulation/halsim_gui/src/main/native/cpp/PDPSimGui.cpp b/simulation/halsim_gui/src/main/native/cpp/PDPSimGui.cpp deleted file mode 100644 index d1ce8bce5c..0000000000 --- a/simulation/halsim_gui/src/main/native/cpp/PDPSimGui.cpp +++ /dev/null @@ -1,124 +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 "PDPSimGui.h" - -#include - -#include -#include -#include -#include - -#include -#include - -#include "HALDataSource.h" -#include "HALSimGui.h" - -using namespace halsimgui; - -namespace { -HALSIMGUI_DATASOURCE_DOUBLE_INDEXED(PDPTemperature, "PDP Temp"); -HALSIMGUI_DATASOURCE_DOUBLE_INDEXED(PDPVoltage, "PDP Voltage"); -HALSIMGUI_DATASOURCE_DOUBLE_INDEXED2(PDPCurrent, "PDP Current"); - -class PDPSimModel : public glass::PDPModel { - public: - explicit PDPSimModel(int32_t index) - : m_index{index}, m_temp{index}, m_voltage{index} { - const int numChannels = HAL_GetNumPDPChannels(); - m_currents.reserve(numChannels); - for (int i = 0; i < numChannels; ++i) { - m_currents.emplace_back(std::make_unique(index, i)); - } - } - - void Update() override {} - - bool Exists() override { return HALSIM_GetPDPInitialized(m_index); } - - int GetNumChannels() const override { return m_currents.size(); } - - glass::DataSource* GetTemperatureData() override { return &m_temp; } - glass::DataSource* GetVoltageData() override { return &m_voltage; } - glass::DataSource* GetCurrentData(int channel) override { - return m_currents[channel].get(); - } - - void SetTemperature(double val) override { - HALSIM_SetPDPTemperature(m_index, val); - } - void SetVoltage(double val) override { HALSIM_SetPDPVoltage(m_index, val); } - void SetCurrent(int channel, double val) override { - HALSIM_SetPDPCurrent(m_index, channel, val); - } - - private: - int32_t m_index; - PDPTemperatureSource m_temp; - PDPVoltageSource m_voltage; - std::vector> m_currents; -}; - -class PDPsSimModel : public glass::PDPsModel { - public: - PDPsSimModel() : m_models(HAL_GetNumPDPModules()) {} - - void Update() override; - - bool Exists() override { return true; } - - void ForEachPDP( - wpi::function_ref func) override; - - private: - std::vector> m_models; -}; -} // namespace - -void PDPsSimModel::Update() { - for (int32_t i = 0, iend = static_cast(m_models.size()); i < iend; - ++i) { - auto& model = m_models[i]; - if (HALSIM_GetPDPInitialized(i)) { - if (!model) { - model = std::make_unique(i); - } - } else { - model.reset(); - } - } -} - -void PDPsSimModel::ForEachPDP( - wpi::function_ref func) { - for (int32_t i = 0, iend = static_cast(m_models.size()); i < iend; - ++i) { - if (auto model = m_models[i].get()) { - func(*model, i); - } - } -} - -static bool PDPsAnyInitialized() { - static const int32_t num = HAL_GetNumPDPModules(); - for (int32_t i = 0; i < num; ++i) { - if (HALSIM_GetPDPInitialized(i)) { - return true; - } - } - return false; -} - -void PDPSimGui::Initialize() { - HALSimGui::halProvider.Register( - "PDPs", PDPsAnyInitialized, - [] { return std::make_unique(); }, - [](glass::Window* win, glass::Model* model) { - win->SetDefaultPos(245, 155); - return glass::MakeFunctionView( - [=] { DisplayPDPs(static_cast(model)); }); - }); -} diff --git a/simulation/halsim_gui/src/main/native/cpp/PowerDistributionSimGui.cpp b/simulation/halsim_gui/src/main/native/cpp/PowerDistributionSimGui.cpp new file mode 100644 index 0000000000..660dd4a24c --- /dev/null +++ b/simulation/halsim_gui/src/main/native/cpp/PowerDistributionSimGui.cpp @@ -0,0 +1,136 @@ +// 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 "PowerDistributionSimGui.h" + +#include + +#include +#include +#include +#include + +#include +#include + +#include "HALDataSource.h" +#include "HALSimGui.h" + +using namespace halsimgui; + +namespace { +HALSIMGUI_DATASOURCE_DOUBLE_INDEXED(PowerDistributionTemperature, + "Power Distribution Temp"); +HALSIMGUI_DATASOURCE_DOUBLE_INDEXED(PowerDistributionVoltage, + "Power Distribution Voltage"); +HALSIMGUI_DATASOURCE_DOUBLE_INDEXED2(PowerDistributionCurrent, + "Power Distribution Current"); + +class PowerDistributionSimModel : public glass::PowerDistributionModel { + public: + explicit PowerDistributionSimModel(int32_t index) + : m_index{index}, m_temp{index}, m_voltage{index} { + const int numChannels = HAL_GetNumPDPChannels(); + m_currents.reserve(numChannels); + for (int i = 0; i < numChannels; ++i) { + m_currents.emplace_back( + std::make_unique(index, i)); + } + } + + void Update() override {} + + bool Exists() override { + return HALSIM_GetPowerDistributionInitialized(m_index); + } + + int GetNumChannels() const override { return m_currents.size(); } + + glass::DataSource* GetTemperatureData() override { return &m_temp; } + glass::DataSource* GetVoltageData() override { return &m_voltage; } + glass::DataSource* GetCurrentData(int channel) override { + return m_currents[channel].get(); + } + + void SetTemperature(double val) override { + HALSIM_SetPowerDistributionTemperature(m_index, val); + } + void SetVoltage(double val) override { + HALSIM_SetPowerDistributionVoltage(m_index, val); + } + void SetCurrent(int channel, double val) override { + HALSIM_SetPowerDistributionCurrent(m_index, channel, val); + } + + private: + int32_t m_index; + PowerDistributionTemperatureSource m_temp; + PowerDistributionVoltageSource m_voltage; + std::vector> m_currents; +}; + +class PowerDistributionsSimModel : public glass::PowerDistributionsModel { + public: + PowerDistributionsSimModel() : m_models(HAL_GetNumPDPModules()) {} + + void Update() override; + + bool Exists() override { return true; } + + void ForEachPowerDistribution( + wpi::function_ref + func) override; + + private: + std::vector> m_models; +}; +} // namespace + +void PowerDistributionsSimModel::Update() { + for (int32_t i = 0, iend = static_cast(m_models.size()); i < iend; + ++i) { + auto& model = m_models[i]; + if (HALSIM_GetPowerDistributionInitialized(i)) { + if (!model) { + model = std::make_unique(i); + } + } else { + model.reset(); + } + } +} + +void PowerDistributionsSimModel::ForEachPowerDistribution( + wpi::function_ref + func) { + for (int32_t i = 0, iend = static_cast(m_models.size()); i < iend; + ++i) { + if (auto model = m_models[i].get()) { + func(*model, i); + } + } +} + +static bool PowerDistributionsAnyInitialized() { + static const int32_t num = HAL_GetNumPDPModules(); + for (int32_t i = 0; i < num; ++i) { + if (HALSIM_GetPowerDistributionInitialized(i)) { + return true; + } + } + return false; +} + +void PowerDistributionSimGui::Initialize() { + HALSimGui::halProvider.Register( + "PowerDistributions", PowerDistributionsAnyInitialized, + [] { return std::make_unique(); }, + [](glass::Window* win, glass::Model* model) { + win->SetDefaultPos(245, 155); + return glass::MakeFunctionView([=] { + DisplayPowerDistributions( + static_cast(model)); + }); + }); +} diff --git a/simulation/halsim_gui/src/main/native/cpp/PDPSimGui.h b/simulation/halsim_gui/src/main/native/cpp/PowerDistributionSimGui.h similarity index 90% rename from simulation/halsim_gui/src/main/native/cpp/PDPSimGui.h rename to simulation/halsim_gui/src/main/native/cpp/PowerDistributionSimGui.h index a7da32e09f..10a78cc205 100644 --- a/simulation/halsim_gui/src/main/native/cpp/PDPSimGui.h +++ b/simulation/halsim_gui/src/main/native/cpp/PowerDistributionSimGui.h @@ -6,7 +6,7 @@ namespace halsimgui { -class PDPSimGui { +class PowerDistributionSimGui { public: static void Initialize(); }; diff --git a/simulation/halsim_gui/src/main/native/cpp/main.cpp b/simulation/halsim_gui/src/main/native/cpp/main.cpp index a0db3d0a2c..6df6f35e4e 100644 --- a/simulation/halsim_gui/src/main/native/cpp/main.cpp +++ b/simulation/halsim_gui/src/main/native/cpp/main.cpp @@ -24,8 +24,8 @@ #include "HALSimGui.h" #include "NetworkTablesSimGui.h" #include "PCMSimGui.h" -#include "PDPSimGui.h" #include "PWMSimGui.h" +#include "PowerDistributionSimGui.h" #include "RelaySimGui.h" #include "RoboRioSimGui.h" #include "SimDeviceGui.h" @@ -62,7 +62,7 @@ __declspec(dllexport) gui::AddInit(DIOSimGui::Initialize); gui::AddInit(NetworkTablesSimGui::Initialize); gui::AddInit(PCMSimGui::Initialize); - gui::AddInit(PDPSimGui::Initialize); + gui::AddInit(PowerDistributionSimGui::Initialize); gui::AddInit(PWMSimGui::Initialize); gui::AddInit(RelaySimGui::Initialize); gui::AddInit(RoboRioSimGui::Initialize); diff --git a/wpilibc/src/main/native/cpp/PowerDistributionPanel.cpp b/wpilibc/src/main/native/cpp/PowerDistributionPanel.cpp index f294eea0be..cca7916bb5 100644 --- a/wpilibc/src/main/native/cpp/PowerDistributionPanel.cpp +++ b/wpilibc/src/main/native/cpp/PowerDistributionPanel.cpp @@ -2,99 +2,101 @@ // 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/PowerDistributionPanel.h" - #include #include -#include #include +#include #include #include #include "frc/Errors.h" +#include "frc/PowerDistribution.h" #include "frc/SensorUtil.h" using namespace frc; -PowerDistributionPanel::PowerDistributionPanel() : PowerDistributionPanel(0) {} +PowerDistribution::PowerDistribution() : PowerDistribution(0) {} -PowerDistributionPanel::PowerDistributionPanel(int module) : m_module(module) { +PowerDistribution::PowerDistribution(int module) : m_module(module) { int32_t status = 0; - m_handle = HAL_InitializePDP(module, &status); + 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, "PowerDistributionPanel", module); + wpi::SendableRegistry::AddLW(this, "PowerDistribution", module); } -double PowerDistributionPanel::GetVoltage() const { +double PowerDistribution::GetVoltage() const { int32_t status = 0; - double voltage = HAL_GetPDPVoltage(m_handle, &status); + double voltage = HAL_GetPowerDistributionVoltage(m_handle, &status); FRC_CheckErrorStatus(status, "Module {}", m_module); return voltage; } -double PowerDistributionPanel::GetTemperature() const { +double PowerDistribution::GetTemperature() const { int32_t status = 0; - double temperature = HAL_GetPDPTemperature(m_handle, &status); + double temperature = HAL_GetPowerDistributionTemperature(m_handle, &status); FRC_CheckErrorStatus(status, "Module {}", m_module); return temperature; } -double PowerDistributionPanel::GetCurrent(int channel) const { +double PowerDistribution::GetCurrent(int channel) const { int32_t status = 0; - if (!SensorUtil::CheckPDPChannel(channel)) { + if (!HAL_CheckPowerDistributionChannel(m_handle, channel)) { FRC_ReportError(err::ChannelIndexOutOfRange, "Module {} Channel {}", m_module, channel); return 0; } - double current = HAL_GetPDPChannelCurrent(m_handle, channel, &status); + double current = + HAL_GetPowerDistributionChannelCurrent(m_handle, channel, &status); FRC_CheckErrorStatus(status, "Module {} Channel {}", m_module, channel); return current; } -double PowerDistributionPanel::GetTotalCurrent() const { +double PowerDistribution::GetTotalCurrent() const { int32_t status = 0; - double current = HAL_GetPDPTotalCurrent(m_handle, &status); + double current = HAL_GetPowerDistributionTotalCurrent(m_handle, &status); FRC_CheckErrorStatus(status, "Module {}", m_module); return current; } -double PowerDistributionPanel::GetTotalPower() const { +double PowerDistribution::GetTotalPower() const { int32_t status = 0; - double power = HAL_GetPDPTotalPower(m_handle, &status); + double power = HAL_GetPowerDistributionTotalPower(m_handle, &status); FRC_CheckErrorStatus(status, "Module {}", m_module); return power; } -double PowerDistributionPanel::GetTotalEnergy() const { +double PowerDistribution::GetTotalEnergy() const { int32_t status = 0; - double energy = HAL_GetPDPTotalEnergy(m_handle, &status); + double energy = HAL_GetPowerDistributionTotalEnergy(m_handle, &status); FRC_CheckErrorStatus(status, "Module {}", m_module); return energy; } -void PowerDistributionPanel::ResetTotalEnergy() { +void PowerDistribution::ResetTotalEnergy() { int32_t status = 0; - HAL_ResetPDPTotalEnergy(m_handle, &status); + HAL_ResetPowerDistributionTotalEnergy(m_handle, &status); FRC_CheckErrorStatus(status, "Module {}", m_module); } -void PowerDistributionPanel::ClearStickyFaults() { +void PowerDistribution::ClearStickyFaults() { int32_t status = 0; - HAL_ClearPDPStickyFaults(m_handle, &status); + HAL_ClearPowerDistributionStickyFaults(m_handle, &status); FRC_CheckErrorStatus(status, "Module {}", m_module); } -int PowerDistributionPanel::GetModule() const { +int PowerDistribution::GetModule() const { return m_module; } -void PowerDistributionPanel::InitSendable(wpi::SendableBuilder& builder) { - builder.SetSmartDashboardType("PowerDistributionPanel"); +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); diff --git a/wpilibc/src/main/native/cpp/SensorUtil.cpp b/wpilibc/src/main/native/cpp/SensorUtil.cpp index f1bdb818b9..c25ecfa36e 100644 --- a/wpilibc/src/main/native/cpp/SensorUtil.cpp +++ b/wpilibc/src/main/native/cpp/SensorUtil.cpp @@ -7,7 +7,6 @@ #include #include #include -#include #include #include #include @@ -46,11 +45,3 @@ bool SensorUtil::CheckAnalogInputChannel(int channel) { bool SensorUtil::CheckAnalogOutputChannel(int channel) { return HAL_CheckAnalogOutputChannel(channel); } - -bool SensorUtil::CheckPDPChannel(int channel) { - return HAL_CheckPDPChannel(channel); -} - -bool SensorUtil::CheckPDPModule(int module) { - return HAL_CheckPDPModule(module); -} diff --git a/wpilibc/src/main/native/cpp/simulation/PDPSim.cpp b/wpilibc/src/main/native/cpp/simulation/PDPSim.cpp deleted file mode 100644 index 788789700c..0000000000 --- a/wpilibc/src/main/native/cpp/simulation/PDPSim.cpp +++ /dev/null @@ -1,99 +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 "frc/simulation/PDPSim.h" - -#include -#include - -#include - -#include "frc/PowerDistributionPanel.h" - -using namespace frc; -using namespace frc::sim; - -PDPSim::PDPSim(int module) : m_index{module} {} - -PDPSim::PDPSim(const PowerDistributionPanel& pdp) : m_index{pdp.GetModule()} {} - -std::unique_ptr PDPSim::RegisterInitializedCallback( - NotifyCallback callback, bool initialNotify) { - auto store = std::make_unique( - m_index, -1, callback, &HALSIM_CancelPDPInitializedCallback); - store->SetUid(HALSIM_RegisterPDPInitializedCallback( - m_index, &CallbackStoreThunk, store.get(), initialNotify)); - return store; -} - -bool PDPSim::GetInitialized() const { - return HALSIM_GetPDPInitialized(m_index); -} - -void PDPSim::SetInitialized(bool initialized) { - HALSIM_SetPDPInitialized(m_index, initialized); -} - -std::unique_ptr PDPSim::RegisterTemperatureCallback( - NotifyCallback callback, bool initialNotify) { - auto store = std::make_unique( - m_index, -1, callback, &HALSIM_CancelPDPTemperatureCallback); - store->SetUid(HALSIM_RegisterPDPTemperatureCallback( - m_index, &CallbackStoreThunk, store.get(), initialNotify)); - return store; -} - -double PDPSim::GetTemperature() const { - return HALSIM_GetPDPTemperature(m_index); -} - -void PDPSim::SetTemperature(double temperature) { - HALSIM_SetPDPTemperature(m_index, temperature); -} - -std::unique_ptr PDPSim::RegisterVoltageCallback( - NotifyCallback callback, bool initialNotify) { - auto store = std::make_unique( - m_index, -1, callback, &HALSIM_CancelPDPVoltageCallback); - store->SetUid(HALSIM_RegisterPDPVoltageCallback(m_index, &CallbackStoreThunk, - store.get(), initialNotify)); - return store; -} - -double PDPSim::GetVoltage() const { - return HALSIM_GetPDPVoltage(m_index); -} - -void PDPSim::SetVoltage(double voltage) { - HALSIM_SetPDPVoltage(m_index, voltage); -} - -std::unique_ptr PDPSim::RegisterCurrentCallback( - int channel, NotifyCallback callback, bool initialNotify) { - auto store = std::make_unique( - m_index, channel, -1, callback, &HALSIM_CancelPDPCurrentCallback); - store->SetUid(HALSIM_RegisterPDPCurrentCallback( - m_index, channel, &CallbackStoreThunk, store.get(), initialNotify)); - return store; -} - -double PDPSim::GetCurrent(int channel) const { - return HALSIM_GetPDPCurrent(m_index, channel); -} - -void PDPSim::SetCurrent(int channel, double current) { - HALSIM_SetPDPCurrent(m_index, channel, current); -} - -void PDPSim::GetAllCurrents(double* currents) const { - HALSIM_GetPDPAllCurrents(m_index, currents); -} - -void PDPSim::SetAllCurrents(const double* currents) { - HALSIM_SetPDPAllCurrents(m_index, currents); -} - -void PDPSim::ResetData() { - HALSIM_ResetPDPData(m_index); -} diff --git a/wpilibc/src/main/native/cpp/simulation/PowerDistributionSim.cpp b/wpilibc/src/main/native/cpp/simulation/PowerDistributionSim.cpp new file mode 100644 index 0000000000..23b9bc2f2e --- /dev/null +++ b/wpilibc/src/main/native/cpp/simulation/PowerDistributionSim.cpp @@ -0,0 +1,105 @@ +// 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/simulation/PowerDistributionSim.h" + +#include +#include + +#include + +#include "frc/PowerDistribution.h" + +using namespace frc; +using namespace frc::sim; + +PowerDistributionSim::PowerDistributionSim(int module) : m_index{module} {} + +PowerDistributionSim::PowerDistributionSim(const PowerDistribution& pdp) + : m_index{pdp.GetModule()} {} + +std::unique_ptr +PowerDistributionSim::RegisterInitializedCallback(NotifyCallback callback, + bool initialNotify) { + auto store = std::make_unique( + m_index, -1, callback, + &HALSIM_CancelPowerDistributionInitializedCallback); + store->SetUid(HALSIM_RegisterPowerDistributionInitializedCallback( + m_index, &CallbackStoreThunk, store.get(), initialNotify)); + return store; +} + +bool PowerDistributionSim::GetInitialized() const { + return HALSIM_GetPowerDistributionInitialized(m_index); +} + +void PowerDistributionSim::SetInitialized(bool initialized) { + HALSIM_SetPowerDistributionInitialized(m_index, initialized); +} + +std::unique_ptr +PowerDistributionSim::RegisterTemperatureCallback(NotifyCallback callback, + bool initialNotify) { + auto store = std::make_unique( + m_index, -1, callback, + &HALSIM_CancelPowerDistributionTemperatureCallback); + store->SetUid(HALSIM_RegisterPowerDistributionTemperatureCallback( + m_index, &CallbackStoreThunk, store.get(), initialNotify)); + return store; +} + +double PowerDistributionSim::GetTemperature() const { + return HALSIM_GetPowerDistributionTemperature(m_index); +} + +void PowerDistributionSim::SetTemperature(double temperature) { + HALSIM_SetPowerDistributionTemperature(m_index, temperature); +} + +std::unique_ptr PowerDistributionSim::RegisterVoltageCallback( + NotifyCallback callback, bool initialNotify) { + auto store = std::make_unique( + m_index, -1, callback, &HALSIM_CancelPowerDistributionVoltageCallback); + store->SetUid(HALSIM_RegisterPowerDistributionVoltageCallback( + m_index, &CallbackStoreThunk, store.get(), initialNotify)); + return store; +} + +double PowerDistributionSim::GetVoltage() const { + return HALSIM_GetPowerDistributionVoltage(m_index); +} + +void PowerDistributionSim::SetVoltage(double voltage) { + HALSIM_SetPowerDistributionVoltage(m_index, voltage); +} + +std::unique_ptr PowerDistributionSim::RegisterCurrentCallback( + int channel, NotifyCallback callback, bool initialNotify) { + auto store = std::make_unique( + m_index, channel, -1, callback, + &HALSIM_CancelPowerDistributionCurrentCallback); + store->SetUid(HALSIM_RegisterPowerDistributionCurrentCallback( + m_index, channel, &CallbackStoreThunk, store.get(), initialNotify)); + return store; +} + +double PowerDistributionSim::GetCurrent(int channel) const { + return HALSIM_GetPowerDistributionCurrent(m_index, channel); +} + +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::SetAllCurrents(const double* currents) { + HALSIM_SetPowerDistributionAllCurrents(m_index, currents); +} + +void PowerDistributionSim::ResetData() { + HALSIM_ResetPowerDistributionData(m_index); +} diff --git a/wpilibc/src/main/native/include/frc/PowerDistributionPanel.h b/wpilibc/src/main/native/include/frc/PowerDistribution.h similarity index 81% rename from wpilibc/src/main/native/include/frc/PowerDistributionPanel.h rename to wpilibc/src/main/native/include/frc/PowerDistribution.h index b8466b2765..d1e3883258 100644 --- a/wpilibc/src/main/native/include/frc/PowerDistributionPanel.h +++ b/wpilibc/src/main/native/include/frc/PowerDistribution.h @@ -14,26 +14,25 @@ namespace frc { * Class for getting voltage, current, temperature, power and energy from the * CAN PDP. */ -class PowerDistributionPanel - : public wpi::Sendable, - public wpi::SendableHelper { +class PowerDistribution : public wpi::Sendable, + public wpi::SendableHelper { public: /** - * Constructs a PowerDistributionPanel. + * Constructs a PowerDistribution. * * Uses the default CAN ID (0). */ - PowerDistributionPanel(); + PowerDistribution(); /** - * Constructs a PowerDistributionPanel. + * Constructs a PowerDistribution. * * @param module The CAN ID of the PDP */ - explicit PowerDistributionPanel(int module); + explicit PowerDistribution(int module); - PowerDistributionPanel(PowerDistributionPanel&&) = default; - PowerDistributionPanel& operator=(PowerDistributionPanel&&) = default; + PowerDistribution(PowerDistribution&&) = default; + PowerDistribution& operator=(PowerDistribution&&) = default; /** * Query the input voltage of the PDP. @@ -80,7 +79,7 @@ class PowerDistributionPanel /** * Reset the total energy drawn from the PDP. * - * @see PowerDistributionPanel#GetTotalEnergy + * @see PowerDistribution#GetTotalEnergy */ void ResetTotalEnergy(); diff --git a/wpilibc/src/main/native/include/frc/shuffleboard/BuiltInWidgets.h b/wpilibc/src/main/native/include/frc/shuffleboard/BuiltInWidgets.h index 1b959ff4bf..f341108aab 100644 --- a/wpilibc/src/main/native/include/frc/shuffleboard/BuiltInWidgets.h +++ b/wpilibc/src/main/native/include/frc/shuffleboard/BuiltInWidgets.h @@ -160,8 +160,8 @@ enum class BuiltInWidgets { */ kVoltageView, /** - * Displays a PowerDistributionPanel.
Supported types:
  • - * PowerDistributionPanel
  • + * Displays a PowerDistribution.
    Supported types:
    • + * PowerDistribution
    • *
    *
    Custom properties: * @@ -170,7 +170,7 @@ enum class BuiltInWidgets { * *
    Whether or not to display the voltage and current draw
    */ - kPowerDistributionPanel, + kPowerDistribution, /** * Displays a SendableChooser with a dropdown combo box with a list of * options. diff --git a/wpilibc/src/main/native/include/frc/simulation/PDPSim.h b/wpilibc/src/main/native/include/frc/simulation/PowerDistributionSim.h similarity index 64% rename from wpilibc/src/main/native/include/frc/simulation/PDPSim.h rename to wpilibc/src/main/native/include/frc/simulation/PowerDistributionSim.h index 259af5a289..5b0e90bbbe 100644 --- a/wpilibc/src/main/native/include/frc/simulation/PDPSim.h +++ b/wpilibc/src/main/native/include/frc/simulation/PowerDistributionSim.h @@ -10,31 +10,31 @@ namespace frc { -class PowerDistributionPanel; +class PowerDistribution; namespace sim { /** - * Class to control a simulated Power Distribution Panel (PDP). + * Class to control a simulated Power Distribution Panel (PowerDistribution). */ -class PDPSim { +class PowerDistributionSim { public: /** - * Constructs from a PDP module number (CAN ID). + * Constructs from a PowerDistribution module number (CAN ID). * * @param module module number */ - explicit PDPSim(int module = 0); + explicit PowerDistributionSim(int module = 0); /** - * Constructs from a PowerDistributionPanel object. + * Constructs from a PowerDistribution object. * - * @param pdp PowerDistributionPanel to simulate + * @param pdp PowerDistribution to simulate */ - explicit PDPSim(const PowerDistributionPanel& pdp); + explicit PowerDistributionSim(const PowerDistribution& pdp); /** - * Register a callback to be run when the PDP is initialized. + * Register a callback to be run when the PowerDistribution is initialized. * * @param callback the callback * @param initialNotify whether to run the callback with the initial state @@ -44,21 +44,22 @@ class PDPSim { NotifyCallback callback, bool initialNotify); /** - * Check whether the PDP has been initialized. + * Check whether the PowerDistribution has been initialized. * * @return true if initialized */ bool GetInitialized() const; /** - * Define whether the PDP has been initialized. + * Define whether the PowerDistribution has been initialized. * * @param initialized whether this object is initialized */ void SetInitialized(bool initialized); /** - * Register a callback to be run whenever the PDP temperature changes. + * Register a callback to be run whenever the PowerDistribution temperature + * changes. * * @param callback the callback * @param initialNotify whether to call the callback with the initial state @@ -68,21 +69,22 @@ class PDPSim { NotifyCallback callback, bool initialNotify); /** - * Check the temperature of the PDP. + * Check the temperature of the PowerDistribution. * - * @return the PDP temperature + * @return the PowerDistribution temperature */ double GetTemperature() const; /** - * Define the PDP temperature. + * Define the PowerDistribution temperature. * - * @param temperature the new PDP temperature + * @param temperature the new PowerDistribution temperature */ void SetTemperature(double temperature); /** - * Register a callback to be run whenever the PDP voltage changes. + * Register a callback to be run whenever the PowerDistribution voltage + * changes. * * @param callback the callback * @param initialNotify whether to call the callback with the initial state @@ -92,16 +94,16 @@ class PDPSim { NotifyCallback callback, bool initialNotify); /** - * Check the PDP voltage. + * Check the PowerDistribution voltage. * - * @return the PDP voltage. + * @return the PowerDistribution voltage. */ double GetVoltage() const; /** - * Set the PDP voltage. + * Set the PowerDistribution voltage. * - * @param voltage the new PDP voltage + * @param voltage the new PowerDistribution voltage */ void SetVoltage(double voltage); @@ -118,7 +120,7 @@ class PDPSim { int channel, NotifyCallback callback, bool initialNotify); /** - * Read the current in one of the PDP channels. + * Read the current in one of the PowerDistribution channels. * * @param channel the channel to check * @return the current in the given channel @@ -134,23 +136,25 @@ class PDPSim { void SetCurrent(int channel, double current); /** - * Read the current of all of the PDP channels. + * Read the current of all of the PowerDistribution channels. * * @param currents output array; set to the current in each channel. The - * array must be big enough to hold all the PDP channels + * array must be big enough to hold all the PowerDistribution + * channels */ void GetAllCurrents(double* currents) const; /** - * Change the current in all of the PDP channels. + * Change the current in all of the PowerDistribution channels. * * @param currents array containing the current values for each channel. The - * array must be big enough to hold all the PDP channels + * array must be big enough to hold all the PowerDistribution + * channels */ void SetAllCurrents(const double* currents); /** - * Reset all PDP simulation data. + * Reset all PowerDistribution simulation data. */ void ResetData(); diff --git a/wpilibc/src/test/native/cpp/simulation/SimInitializationTest.cpp b/wpilibc/src/test/native/cpp/simulation/SimInitializationTest.cpp index f6d502c454..0d64876894 100644 --- a/wpilibc/src/test/native/cpp/simulation/SimInitializationTest.cpp +++ b/wpilibc/src/test/native/cpp/simulation/SimInitializationTest.cpp @@ -18,8 +18,8 @@ #include "frc/simulation/DriverStationSim.h" #include "frc/simulation/DutyCycleSim.h" #include "frc/simulation/EncoderSim.h" -#include "frc/simulation/PDPSim.h" #include "frc/simulation/PWMSim.h" +#include "frc/simulation/PowerDistributionSim.h" #include "frc/simulation/RelaySim.h" #include "frc/simulation/RoboRioSim.h" #include "frc/simulation/SPIAccelerometerSim.h" @@ -41,7 +41,7 @@ TEST(SimInitializationTests, TestAllInitialize) { EncoderSim esim = EncoderSim::CreateForIndex(0); (void)esim; CTREPCMSim pcmsim{0}; - PDPSim pdpsim{0}; + PowerDistributionSim pdpsim{0}; PWMSim pwmsim{0}; RelaySim rsim{0}; RoboRioSim rrsim; diff --git a/wpilibcExamples/src/main/cpp/examples/CANPDP/cpp/Robot.cpp b/wpilibcExamples/src/main/cpp/examples/CANPDP/cpp/Robot.cpp index d510e5f244..aed898fb5c 100644 --- a/wpilibcExamples/src/main/cpp/examples/CANPDP/cpp/Robot.cpp +++ b/wpilibcExamples/src/main/cpp/examples/CANPDP/cpp/Robot.cpp @@ -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 +#include #include #include @@ -31,7 +31,7 @@ class Robot : public frc::TimedRobot { private: // Object for dealing with the Power Distribution Panel (PDP). - frc::PowerDistributionPanel m_pdp; + frc::PowerDistribution m_pdp; }; #ifndef RUNNING_FRC_TESTS diff --git a/wpilibcIntegrationTests/src/main/native/cpp/PowerDistributionPanelTest.cpp b/wpilibcIntegrationTests/src/main/native/cpp/PowerDistributionPanelTest.cpp index 9460110a44..6bc6d053a9 100644 --- a/wpilibcIntegrationTests/src/main/native/cpp/PowerDistributionPanelTest.cpp +++ b/wpilibcIntegrationTests/src/main/native/cpp/PowerDistributionPanelTest.cpp @@ -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 "frc/PowerDistributionPanel.h" // NOLINT(build/include_order) +#include "frc/PowerDistribution.h" // NOLINT(build/include_order) #include #include @@ -14,13 +14,13 @@ static constexpr auto kMotorTime = 0.25_s; -class PowerDistributionPanelTest : public testing::Test { +class PowerDistributionTest : public testing::Test { protected: - frc::PowerDistributionPanel m_pdp; + frc::PowerDistribution m_pdp; frc::Talon m_talon{TestBench::kTalonChannel}; }; -TEST_F(PowerDistributionPanelTest, CheckRepeatedCalls) { +TEST_F(PowerDistributionTest, CheckRepeatedCalls) { auto numChannels = HAL_GetNumPDPChannels(); // 1 second for (int i = 0; i < 50; i++) { @@ -35,7 +35,7 @@ TEST_F(PowerDistributionPanelTest, CheckRepeatedCalls) { /** * Test if the current changes when the motor is driven using a talon */ -TEST_F(PowerDistributionPanelTest, CheckCurrentTalon) { +TEST_F(PowerDistributionTest, CheckCurrentTalon) { frc::Wait(kMotorTime); /* The Current should be 0 */ diff --git a/wpilibj/src/main/java/edu/wpi/first/wpilibj/PowerDistributionPanel.java b/wpilibj/src/main/java/edu/wpi/first/wpilibj/PowerDistribution.java similarity index 74% rename from wpilibj/src/main/java/edu/wpi/first/wpilibj/PowerDistributionPanel.java rename to wpilibj/src/main/java/edu/wpi/first/wpilibj/PowerDistribution.java index af31aeaaf1..af3f8e72c5 100644 --- a/wpilibj/src/main/java/edu/wpi/first/wpilibj/PowerDistributionPanel.java +++ b/wpilibj/src/main/java/edu/wpi/first/wpilibj/PowerDistribution.java @@ -6,7 +6,7 @@ package edu.wpi.first.wpilibj; import edu.wpi.first.hal.FRCNetComm.tResourceType; import edu.wpi.first.hal.HAL; -import edu.wpi.first.hal.PDPJNI; +import edu.wpi.first.hal.PowerDistributionJNI; import edu.wpi.first.util.sendable.Sendable; import edu.wpi.first.util.sendable.SendableBuilder; import edu.wpi.first.util.sendable.SendableRegistry; @@ -15,30 +15,29 @@ import edu.wpi.first.util.sendable.SendableRegistry; * Class for getting voltage, current, temperature, power and energy from the Power Distribution * Panel over CAN. */ -public class PowerDistributionPanel implements Sendable, AutoCloseable { +public class PowerDistribution implements Sendable, AutoCloseable { private final int m_handle; private final int m_module; /** - * Constructs a PowerDistributionPanel. + * Constructs a PowerDistribution. * * @param module The CAN ID of the PDP */ - public PowerDistributionPanel(int module) { - SensorUtil.checkPDPModule(module); - m_handle = PDPJNI.initializePDP(module); + public PowerDistribution(int module) { + m_handle = PowerDistributionJNI.initialize(module, 0); m_module = module; HAL.report(tResourceType.kResourceType_PDP, module + 1); - SendableRegistry.addLW(this, "PowerDistributionPanel", module); + SendableRegistry.addLW(this, "PowerDistribution", module); } /** - * Constructs a PowerDistributionPanel. + * Constructs a PowerDistribution. * *

    Uses the default CAN ID (0). */ - public PowerDistributionPanel() { + public PowerDistribution() { this(0); } @@ -53,7 +52,7 @@ public class PowerDistributionPanel implements Sendable, AutoCloseable { * @return The voltage of the PDP in volts */ public double getVoltage() { - return PDPJNI.getPDPVoltage(m_handle); + return PowerDistributionJNI.getVoltage(m_handle); } /** @@ -62,7 +61,7 @@ public class PowerDistributionPanel implements Sendable, AutoCloseable { * @return The temperature of the PDP in degrees Celsius */ public double getTemperature() { - return PDPJNI.getPDPTemperature(m_handle); + return PowerDistributionJNI.getTemperature(m_handle); } /** @@ -72,9 +71,7 @@ public class PowerDistributionPanel implements Sendable, AutoCloseable { * @return The current of one of the PDP channels (channels 0-15) in Amperes */ public double getCurrent(int channel) { - double current = PDPJNI.getPDPChannelCurrent((byte) channel, m_handle); - - SensorUtil.checkPDPChannel(channel); + double current = PowerDistributionJNI.getChannelCurrent((byte) channel, m_handle); return current; } @@ -85,7 +82,7 @@ public class PowerDistributionPanel implements Sendable, AutoCloseable { * @return The current of all the channels in Amperes */ public double getTotalCurrent() { - return PDPJNI.getPDPTotalCurrent(m_handle); + return PowerDistributionJNI.getTotalCurrent(m_handle); } /** @@ -94,7 +91,7 @@ public class PowerDistributionPanel implements Sendable, AutoCloseable { * @return the total power in Watts */ public double getTotalPower() { - return PDPJNI.getPDPTotalPower(m_handle); + return PowerDistributionJNI.getTotalPower(m_handle); } /** @@ -103,17 +100,17 @@ public class PowerDistributionPanel implements Sendable, AutoCloseable { * @return the total energy in Joules */ public double getTotalEnergy() { - return PDPJNI.getPDPTotalEnergy(m_handle); + return PowerDistributionJNI.getTotalEnergy(m_handle); } /** Reset the total energy to 0. */ public void resetTotalEnergy() { - PDPJNI.resetPDPTotalEnergy(m_handle); + PowerDistributionJNI.resetTotalEnergy(m_handle); } /** Clear all PDP sticky faults. */ public void clearStickyFaults() { - PDPJNI.clearPDPStickyFaults(m_handle); + PowerDistributionJNI.clearStickyFaults(m_handle); } /** @@ -127,7 +124,7 @@ public class PowerDistributionPanel implements Sendable, AutoCloseable { @Override public void initSendable(SendableBuilder builder) { - builder.setSmartDashboardType("PowerDistributionPanel"); + builder.setSmartDashboardType("PowerDistribution"); for (int i = 0; i < SensorUtil.kPDPChannels; ++i) { final int chan = i; builder.addDoubleProperty("Chan" + i, () -> getCurrent(chan), null); diff --git a/wpilibj/src/main/java/edu/wpi/first/wpilibj/SensorUtil.java b/wpilibj/src/main/java/edu/wpi/first/wpilibj/SensorUtil.java index 603b12b4aa..b7d73f5825 100644 --- a/wpilibj/src/main/java/edu/wpi/first/wpilibj/SensorUtil.java +++ b/wpilibj/src/main/java/edu/wpi/first/wpilibj/SensorUtil.java @@ -7,7 +7,6 @@ package edu.wpi.first.wpilibj; import edu.wpi.first.hal.AnalogJNI; import edu.wpi.first.hal.ConstantsJNI; import edu.wpi.first.hal.DIOJNI; -import edu.wpi.first.hal.PDPJNI; import edu.wpi.first.hal.PWMJNI; import edu.wpi.first.hal.PortsJNI; import edu.wpi.first.hal.RelayJNI; @@ -133,39 +132,6 @@ public final class SensorUtil { } } - /** - * Verify that the power distribution channel number is within limits. Channel numbers are - * 0-based. - * - * @param channel The channel number to check. - */ - public static void checkPDPChannel(final int channel) { - if (!PDPJNI.checkPDPChannel(channel)) { - StringBuilder buf = new StringBuilder(); - buf.append("Requested PDP channel is out of range. Minimum: 0, Maximum: ") - .append(kPDPChannels) - .append(", Requested: ") - .append(channel); - throw new IllegalArgumentException(buf.toString()); - } - } - - /** - * Verify that the PDP module number is within limits. module numbers are 0-based. - * - * @param module The module number to check. - */ - public static void checkPDPModule(final int module) { - if (!PDPJNI.checkPDPModule(module)) { - StringBuilder buf = new StringBuilder(); - buf.append("Requested PDP module is out of range. Minimum: 0, Maximum: ") - .append(kPDPModules) - .append(", Requested: ") - .append(module); - throw new IllegalArgumentException(buf.toString()); - } - } - /** * Get the number of the default solenoid module. * diff --git a/wpilibj/src/main/java/edu/wpi/first/wpilibj/shuffleboard/BuiltInWidgets.java b/wpilibj/src/main/java/edu/wpi/first/wpilibj/shuffleboard/BuiltInWidgets.java index f518a04830..ab231e8d09 100644 --- a/wpilibj/src/main/java/edu/wpi/first/wpilibj/shuffleboard/BuiltInWidgets.java +++ b/wpilibj/src/main/java/edu/wpi/first/wpilibj/shuffleboard/BuiltInWidgets.java @@ -188,11 +188,11 @@ public enum BuiltInWidgets implements WidgetType { */ kVoltageView("Voltage View"), /** - * Displays a {@link edu.wpi.first.wpilibj.PowerDistributionPanel PowerDistributionPanel}.
    + * Displays a {@link edu.wpi.first.wpilibj.PowerDistribution PowerDistribution}.
    * Supported types: * *

      - *
    • {@link edu.wpi.first.wpilibj.PowerDistributionPanel} + *
    • {@link edu.wpi.first.wpilibj.PowerDistribution} *
    * *
    @@ -204,7 +204,7 @@ public enum BuiltInWidgets implements WidgetType { * Whether or not to display the voltage and current draw * */ - kPowerDistributionPanel("PDP"), + kPowerDistribution("PDP"), /** * Displays a {@link edu.wpi.first.wpilibj.smartdashboard.SendableChooser SendableChooser} with a * dropdown combo box with a list of options.
    diff --git a/wpilibj/src/main/java/edu/wpi/first/wpilibj/simulation/PDPSim.java b/wpilibj/src/main/java/edu/wpi/first/wpilibj/simulation/PDPSim.java index b432b849f5..eef8ccf1ee 100644 --- a/wpilibj/src/main/java/edu/wpi/first/wpilibj/simulation/PDPSim.java +++ b/wpilibj/src/main/java/edu/wpi/first/wpilibj/simulation/PDPSim.java @@ -5,8 +5,8 @@ package edu.wpi.first.wpilibj.simulation; import edu.wpi.first.hal.simulation.NotifyCallback; -import edu.wpi.first.hal.simulation.PDPDataJNI; -import edu.wpi.first.wpilibj.PowerDistributionPanel; +import edu.wpi.first.hal.simulation.PowerDistributionDataJNI; +import edu.wpi.first.wpilibj.PowerDistribution; /** Class to control a simulated Power Distribution Panel (PDP). */ public class PDPSim { @@ -27,11 +27,11 @@ public class PDPSim { } /** - * Constructs from a PowerDistributionPanel object. + * Constructs from a PowerDistribution object. * - * @param pdp PowerDistributionPanel to simulate + * @param pdp PowerDistribution to simulate */ - public PDPSim(PowerDistributionPanel pdp) { + public PDPSim(PowerDistribution pdp) { m_index = pdp.getModule(); } @@ -44,8 +44,9 @@ public class PDPSim { * this object so GC doesn't cancel the callback. */ public CallbackStore registerInitializedCallback(NotifyCallback callback, boolean initialNotify) { - int uid = PDPDataJNI.registerInitializedCallback(m_index, callback, initialNotify); - return new CallbackStore(m_index, uid, PDPDataJNI::cancelInitializedCallback); + int uid = + PowerDistributionDataJNI.registerInitializedCallback(m_index, callback, initialNotify); + return new CallbackStore(m_index, uid, PowerDistributionDataJNI::cancelInitializedCallback); } /** @@ -54,7 +55,7 @@ public class PDPSim { * @return true if initialized */ public boolean getInitialized() { - return PDPDataJNI.getInitialized(m_index); + return PowerDistributionDataJNI.getInitialized(m_index); } /** @@ -63,7 +64,7 @@ public class PDPSim { * @param initialized whether this object is initialized */ public void setInitialized(boolean initialized) { - PDPDataJNI.setInitialized(m_index, initialized); + PowerDistributionDataJNI.setInitialized(m_index, initialized); } /** @@ -75,8 +76,9 @@ public class PDPSim { * this object so GC doesn't cancel the callback. */ public CallbackStore registerTemperatureCallback(NotifyCallback callback, boolean initialNotify) { - int uid = PDPDataJNI.registerTemperatureCallback(m_index, callback, initialNotify); - return new CallbackStore(m_index, uid, PDPDataJNI::cancelTemperatureCallback); + int uid = + PowerDistributionDataJNI.registerTemperatureCallback(m_index, callback, initialNotify); + return new CallbackStore(m_index, uid, PowerDistributionDataJNI::cancelTemperatureCallback); } /** @@ -85,7 +87,7 @@ public class PDPSim { * @return the PDP temperature */ public double getTemperature() { - return PDPDataJNI.getTemperature(m_index); + return PowerDistributionDataJNI.getTemperature(m_index); } /** @@ -94,7 +96,7 @@ public class PDPSim { * @param temperature the new PDP temperature */ public void setTemperature(double temperature) { - PDPDataJNI.setTemperature(m_index, temperature); + PowerDistributionDataJNI.setTemperature(m_index, temperature); } /** @@ -106,8 +108,8 @@ public class PDPSim { * this object so GC doesn't cancel the callback. */ public CallbackStore registerVoltageCallback(NotifyCallback callback, boolean initialNotify) { - int uid = PDPDataJNI.registerVoltageCallback(m_index, callback, initialNotify); - return new CallbackStore(m_index, uid, PDPDataJNI::cancelVoltageCallback); + int uid = PowerDistributionDataJNI.registerVoltageCallback(m_index, callback, initialNotify); + return new CallbackStore(m_index, uid, PowerDistributionDataJNI::cancelVoltageCallback); } /** @@ -116,7 +118,7 @@ public class PDPSim { * @return the PDP voltage. */ public double getVoltage() { - return PDPDataJNI.getVoltage(m_index); + return PowerDistributionDataJNI.getVoltage(m_index); } /** @@ -125,7 +127,7 @@ public class PDPSim { * @param voltage the new PDP voltage */ public void setVoltage(double voltage) { - PDPDataJNI.setVoltage(m_index, voltage); + PowerDistributionDataJNI.setVoltage(m_index, voltage); } /** @@ -139,8 +141,10 @@ public class PDPSim { */ public CallbackStore registerCurrentCallback( int channel, NotifyCallback callback, boolean initialNotify) { - int uid = PDPDataJNI.registerCurrentCallback(m_index, channel, callback, initialNotify); - return new CallbackStore(m_index, channel, uid, PDPDataJNI::cancelCurrentCallback); + int uid = + PowerDistributionDataJNI.registerCurrentCallback(m_index, channel, callback, initialNotify); + return new CallbackStore( + m_index, channel, uid, PowerDistributionDataJNI::cancelCurrentCallback); } /** @@ -150,7 +154,7 @@ public class PDPSim { * @return the current in the given channel */ public double getCurrent(int channel) { - return PDPDataJNI.getCurrent(m_index, channel); + return PowerDistributionDataJNI.getCurrent(m_index, channel); } /** @@ -160,11 +164,11 @@ public class PDPSim { * @param current the new current for the channel */ public void setCurrent(int channel, double current) { - PDPDataJNI.setCurrent(m_index, channel, current); + PowerDistributionDataJNI.setCurrent(m_index, channel, current); } /** Reset all PDP simulation data. */ public void resetData() { - PDPDataJNI.resetData(m_index); + PowerDistributionDataJNI.resetData(m_index); } } diff --git a/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/examples/canpdp/Robot.java b/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/examples/canpdp/Robot.java index 71b4852619..26ff212e37 100644 --- a/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/examples/canpdp/Robot.java +++ b/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/examples/canpdp/Robot.java @@ -4,7 +4,7 @@ package edu.wpi.first.wpilibj.examples.canpdp; -import edu.wpi.first.wpilibj.PowerDistributionPanel; +import edu.wpi.first.wpilibj.PowerDistribution; import edu.wpi.first.wpilibj.TimedRobot; import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard; @@ -15,7 +15,7 @@ import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard; public class Robot extends TimedRobot { private static final int kPDPId = 0; - private final PowerDistributionPanel m_pdp = new PowerDistributionPanel(kPDPId); + private final PowerDistribution m_pdp = new PowerDistribution(kPDPId); @Override public void robotPeriodic() { diff --git a/wpilibjIntegrationTests/src/main/java/edu/wpi/first/wpilibj/PDPTest.java b/wpilibjIntegrationTests/src/main/java/edu/wpi/first/wpilibj/PDPTest.java index e65bce0e23..548c2e0fa7 100644 --- a/wpilibjIntegrationTests/src/main/java/edu/wpi/first/wpilibj/PDPTest.java +++ b/wpilibjIntegrationTests/src/main/java/edu/wpi/first/wpilibj/PDPTest.java @@ -24,18 +24,18 @@ import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameters; -/** Test that covers the {@link PowerDistributionPanel}. */ +/** Test that covers the {@link PowerDistribution}. */ @RunWith(Parameterized.class) public class PDPTest extends AbstractComsSetup { private static final Logger logger = Logger.getLogger(PDPTest.class.getName()); - private static PowerDistributionPanel pdp; + private static PowerDistribution pdp; private static MotorEncoderFixture me; private final double m_expectedStoppedCurrentDraw; @BeforeClass public static void setUpBeforeClass() { - pdp = new PowerDistributionPanel(); + pdp = new PowerDistribution(); } @AfterClass diff --git a/wpilibjIntegrationTests/src/main/java/edu/wpi/first/wpilibj/test/TestBench.java b/wpilibjIntegrationTests/src/main/java/edu/wpi/first/wpilibj/test/TestBench.java index c71ca6bf0c..8e63dc90c8 100644 --- a/wpilibjIntegrationTests/src/main/java/edu/wpi/first/wpilibj/test/TestBench.java +++ b/wpilibjIntegrationTests/src/main/java/edu/wpi/first/wpilibj/test/TestBench.java @@ -41,7 +41,7 @@ public final class TestBench { public static final int kTiltServoChannel = 9; public static final int kPanServoChannel = 8; - /* PowerDistributionPanel channels */ + /* PowerDistribution channels */ public static final int kJaguarPDPChannel = 6; public static final int kVictorPDPChannel = 8; public static final int kTalonPDPChannel = 10;