diff --git a/hal/src/main/java/edu/wpi/first/hal/PowerDistributionJNI.java b/hal/src/main/java/edu/wpi/first/hal/PowerDistributionJNI.java index b2ac61aa19..877dde3d1b 100644 --- a/hal/src/main/java/edu/wpi/first/hal/PowerDistributionJNI.java +++ b/hal/src/main/java/edu/wpi/first/hal/PowerDistributionJNI.java @@ -46,4 +46,14 @@ public class PowerDistributionJNI extends JNIWrapper { public static native boolean getSwitchableChannel(int handle); public static native void setSwitchableChannel(int handle, boolean enabled); + + public static native double getVoltageNoError(int handle); + + public static native double getChannelCurrentNoError(int handle, int channel); + + public static native double getTotalCurrentNoError(int handle); + + public static native boolean getSwitchableChannelNoError(int handle); + + public static native void setSwitchableChannelNoError(int handle, boolean enabled); } diff --git a/hal/src/main/native/cpp/jni/PowerDistributionJNI.cpp b/hal/src/main/native/cpp/jni/PowerDistributionJNI.cpp index b6084589a8..cac0739865 100644 --- a/hal/src/main/native/cpp/jni/PowerDistributionJNI.cpp +++ b/hal/src/main/native/cpp/jni/PowerDistributionJNI.cpp @@ -292,4 +292,74 @@ Java_edu_wpi_first_hal_PowerDistributionJNI_getSwitchableChannel return state; } +/* + * Class: edu_wpi_first_hal_PowerDistributionJNI + * Method: getVoltageNoError + * Signature: (I)D + */ +JNIEXPORT jdouble JNICALL +Java_edu_wpi_first_hal_PowerDistributionJNI_getVoltageNoError + (JNIEnv* env, jclass, jint handle) +{ + int32_t status = 0; + double voltage = HAL_GetPowerDistributionVoltage(handle, &status); + return voltage; +} + +/* + * Class: edu_wpi_first_hal_PowerDistributionJNI + * Method: getChannelCurrentNoError + * Signature: (II)D + */ +JNIEXPORT jdouble JNICALL +Java_edu_wpi_first_hal_PowerDistributionJNI_getChannelCurrentNoError + (JNIEnv* env, jclass, jint handle, jint channel) +{ + int32_t status = 0; + double current = + HAL_GetPowerDistributionChannelCurrent(handle, channel, &status); + return current; +} + +/* + * Class: edu_wpi_first_hal_PowerDistributionJNI + * Method: getTotalCurrentNoError + * Signature: (I)D + */ +JNIEXPORT jdouble JNICALL +Java_edu_wpi_first_hal_PowerDistributionJNI_getTotalCurrentNoError + (JNIEnv* env, jclass, jint handle) +{ + int32_t status = 0; + double current = HAL_GetPowerDistributionTotalCurrent(handle, &status); + return current; +} + +/* + * Class: edu_wpi_first_hal_PowerDistributionJNI + * Method: setSwitchableChannelNoError + * Signature: (IZ)V + */ +JNIEXPORT void JNICALL +Java_edu_wpi_first_hal_PowerDistributionJNI_setSwitchableChannelNoError + (JNIEnv* env, jclass, jint handle, jboolean enabled) +{ + int32_t status = 0; + HAL_SetPowerDistributionSwitchableChannel(handle, enabled, &status); +} + +/* + * Class: edu_wpi_first_hal_PowerDistributionJNI + * Method: getSwitchableChannelNoError + * Signature: (I)Z + */ +JNIEXPORT jboolean JNICALL +Java_edu_wpi_first_hal_PowerDistributionJNI_getSwitchableChannelNoError + (JNIEnv* env, jclass, jint handle) +{ + int32_t status = 0; + auto state = HAL_GetPowerDistributionSwitchableChannel(handle, &status); + return state; +} + } // extern "C" diff --git a/wpilibc/src/main/native/cpp/PowerDistribution.cpp b/wpilibc/src/main/native/cpp/PowerDistribution.cpp index 4c2acfcf5b..47fd96df07 100644 --- a/wpilibc/src/main/native/cpp/PowerDistribution.cpp +++ b/wpilibc/src/main/native/cpp/PowerDistribution.cpp @@ -132,15 +132,38 @@ void PowerDistribution::InitSendable(wpi::SendableBuilder& builder) { int32_t status = 0; int numChannels = HAL_GetPowerDistributionNumChannels(m_handle, &status); FRC_ReportError(status, "Module {}", m_module); + // Use manual reads to avoid printing errors for (int i = 0; i < numChannels; ++i) { builder.AddDoubleProperty( - fmt::format("Chan{}", i), [=] { return GetCurrent(i); }, nullptr); + fmt::format("Chan{}", i), + [=] { + int32_t lStatus = 0; + return HAL_GetPowerDistributionChannelCurrent(m_handle, i, &lStatus); + }, + nullptr); } builder.AddDoubleProperty( - "Voltage", [=] { return GetVoltage(); }, nullptr); + "Voltage", + [=] { + int32_t lStatus = 0; + return HAL_GetPowerDistributionVoltage(m_handle, &lStatus); + }, + nullptr); builder.AddDoubleProperty( - "TotalCurrent", [=] { return GetTotalCurrent(); }, nullptr); + "TotalCurrent", + [=] { + int32_t lStatus = 0; + return HAL_GetPowerDistributionTotalCurrent(m_handle, &lStatus); + }, + nullptr); builder.AddBooleanProperty( - "SwitchableChannel", [=] { return GetSwitchableChannel(); }, - [=](bool value) { SetSwitchableChannel(value); }); + "SwitchableChannel", + [=] { + int32_t lStatus = 0; + return HAL_GetPowerDistributionSwitchableChannel(m_handle, &lStatus); + }, + [=](bool value) { + int32_t lStatus = 0; + HAL_SetPowerDistributionSwitchableChannel(m_handle, value, &lStatus); + }); } diff --git a/wpilibj/src/main/java/edu/wpi/first/wpilibj/PowerDistribution.java b/wpilibj/src/main/java/edu/wpi/first/wpilibj/PowerDistribution.java index dd97efab55..54d5ec6302 100644 --- a/wpilibj/src/main/java/edu/wpi/first/wpilibj/PowerDistribution.java +++ b/wpilibj/src/main/java/edu/wpi/first/wpilibj/PowerDistribution.java @@ -160,11 +160,16 @@ public class PowerDistribution implements Sendable, AutoCloseable { int numChannels = getNumChannels(); for (int i = 0; i < numChannels; ++i) { final int chan = i; - builder.addDoubleProperty("Chan" + i, () -> getCurrent(chan), null); + builder.addDoubleProperty( + "Chan" + i, () -> PowerDistributionJNI.getChannelCurrentNoError(m_handle, chan), null); } - builder.addDoubleProperty("Voltage", this::getVoltage, null); - builder.addDoubleProperty("TotalCurrent", this::getTotalCurrent, null); + builder.addDoubleProperty( + "Voltage", () -> PowerDistributionJNI.getVoltageNoError(m_handle), null); + builder.addDoubleProperty( + "TotalCurrent", () -> PowerDistributionJNI.getTotalCurrent(m_handle), null); builder.addBooleanProperty( - "SwitchableChannel", this::getSwitchableChannel, this::setSwitchableChannel); + "SwitchableChannel", + () -> PowerDistributionJNI.getSwitchableChannelNoError(m_handle), + value -> PowerDistributionJNI.setSwitchableChannel(m_handle, value)); } }