From 697477fd73abd7a036048f75fed8250c7adf564e Mon Sep 17 00:00:00 2001 From: Steven Date: Sat, 9 Dec 2023 12:33:04 -0500 Subject: [PATCH 1/6] Add/Fix pdp.GetAllCurrents --- .../native/cpp/jni/PowerDistributionJNI.cpp | 9 +++--- .../src/main/native/cpp/PowerDistribution.cpp | 21 ++++++++++++-- .../native/include/frc/PowerDistribution.h | 14 +++++++++ .../test/native/cpp/simulation/PDPSimTest.cpp | 22 ++++++++++++++ .../wpi/first/wpilibj/PowerDistribution.java | 11 +++++++ .../first/wpilibj/PowerDistributionTest.java | 29 +++++++++++++++++++ 6 files changed, 99 insertions(+), 7 deletions(-) create mode 100644 wpilibj/src/test/java/edu/wpi/first/wpilibj/PowerDistributionTest.java diff --git a/hal/src/main/native/cpp/jni/PowerDistributionJNI.cpp b/hal/src/main/native/cpp/jni/PowerDistributionJNI.cpp index 9d85ea4a9c6..da60ac0e304 100644 --- a/hal/src/main/native/cpp/jni/PowerDistributionJNI.cpp +++ b/hal/src/main/native/cpp/jni/PowerDistributionJNI.cpp @@ -179,15 +179,16 @@ 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); + int32_t size = HAL_GetPowerDistributionNumChannels(handle, &status); //status won't change here + std::vector storage(size,0); + + HAL_GetPowerDistributionAllChannelCurrents(handle, storage.data(), size, &status); if (!CheckStatus(env, status, false)) { return; } - env->SetDoubleArrayRegion(jarr, 0, 16, storage); + env->SetDoubleArrayRegion(jarr, 0, size, storage.data()); } /* diff --git a/wpilibc/src/main/native/cpp/PowerDistribution.cpp b/wpilibc/src/main/native/cpp/PowerDistribution.cpp index ba4cb383c30..fcb75700c47 100644 --- a/wpilibc/src/main/native/cpp/PowerDistribution.cpp +++ b/wpilibc/src/main/native/cpp/PowerDistribution.cpp @@ -64,6 +64,13 @@ PowerDistribution::~PowerDistribution() { } } +int PowerDistribution::GetNumChannels() const { + int32_t status = 0; + int32_t size = HAL_GetPowerDistributionNumChannels(m_handle, &status); + FRC_ReportError(status, "Module {}", m_module); + return size; +} + double PowerDistribution::GetVoltage() const { int32_t status = 0; double voltage = HAL_GetPowerDistributionVoltage(m_handle, &status); @@ -87,6 +94,16 @@ double PowerDistribution::GetCurrent(int channel) const { return current; } +std::vector PowerDistribution::GetAllCurrents() const { + int32_t status = 0; + int32_t size = this->GetNumChannels(); + std::vector currents(size,0); + + HAL_GetPowerDistributionAllChannelCurrents(m_handle, currents.data(), size, &status); + FRC_ReportError(status, "Module {}", m_module); + return currents; +} + double PowerDistribution::GetTotalCurrent() const { int32_t status = 0; double current = HAL_GetPowerDistributionTotalCurrent(m_handle, &status); @@ -188,9 +205,7 @@ PowerDistribution::StickyFaults PowerDistribution::GetStickyFaults() const { void PowerDistribution::InitSendable(wpi::SendableBuilder& builder) { builder.SetSmartDashboardType("PowerDistribution"); - int32_t status = 0; - int numChannels = HAL_GetPowerDistributionNumChannels(m_handle, &status); - FRC_ReportError(status, "Module {}", m_module); + int numChannels = this->GetNumChannels(); // Use manual reads to avoid printing errors for (int i = 0; i < numChannels; ++i) { builder.AddDoubleProperty( diff --git a/wpilibc/src/main/native/include/frc/PowerDistribution.h b/wpilibc/src/main/native/include/frc/PowerDistribution.h index 763e6bfdc4b..b47f04504ef 100644 --- a/wpilibc/src/main/native/include/frc/PowerDistribution.h +++ b/wpilibc/src/main/native/include/frc/PowerDistribution.h @@ -40,6 +40,13 @@ class PowerDistribution : public wpi::Sendable, PowerDistribution(PowerDistribution&&) = default; PowerDistribution& operator=(PowerDistribution&&) = default; + /** + * Gets the number of channels for this power distribution object. + * + * @return Number of output channels (16 for PDP, 24 for PDH). + */ + int GetNumChannels() const; + /** * Query the input voltage of the PDP/PDH. * @@ -62,6 +69,13 @@ class PowerDistribution : public wpi::Sendable, */ double GetCurrent(int channel) const; + /** + * Query all currents of the PDP. + * + * @return The current of each channel in Amperes + */ + std::vector GetAllCurrents() const; + /** * Query the total current of all monitored PDP/PDH channels. * diff --git a/wpilibc/src/test/native/cpp/simulation/PDPSimTest.cpp b/wpilibc/src/test/native/cpp/simulation/PDPSimTest.cpp index ad058de734e..f80123b7b1c 100644 --- a/wpilibc/src/test/native/cpp/simulation/PDPSimTest.cpp +++ b/wpilibc/src/test/native/cpp/simulation/PDPSimTest.cpp @@ -79,4 +79,26 @@ TEST(PowerDistributionSimTest, SetCurrent) { EXPECT_TRUE(callback.GetLastValue()); } } + +TEST(PowerDistributionSimTest, GetAllCurrents) { + HAL_Initialize(500, 0); + PowerDistribution pdp{2, frc::PowerDistribution::ModuleType::kRev}; + PowerDistributionSim sim(pdp); + + //setup + for (int channel = 0; channel < pdp.GetNumChannels(); ++channel) { + const double kTestCurrent = 24 - channel; + sim.SetCurrent(channel, kTestCurrent); + } + + //run it + std::vector currents = pdp.GetAllCurrents(); + + //verify + for (int channel = 0; channel < pdp.GetNumChannels(); ++channel) { + const double kTestCurrent = 24 - channel; + EXPECT_EQ(kTestCurrent, currents[channel]); + } +} + } // namespace frc::sim 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 13954264e48..b1ff874237c 100644 --- a/wpilibj/src/main/java/edu/wpi/first/wpilibj/PowerDistribution.java +++ b/wpilibj/src/main/java/edu/wpi/first/wpilibj/PowerDistribution.java @@ -104,6 +104,17 @@ public double getCurrent(int channel) { return PowerDistributionJNI.getChannelCurrent(m_handle, channel); } + /** + * Query all currents of the PDP. + * + * @return The current of each channel in Amperes + */ + public double[] getAllCurrents() { + double[] currents = new double[getNumChannels()]; + PowerDistributionJNI.getAllCurrents(m_handle, currents); + return currents; + } + /** * Query the current of all monitored channels. * diff --git a/wpilibj/src/test/java/edu/wpi/first/wpilibj/PowerDistributionTest.java b/wpilibj/src/test/java/edu/wpi/first/wpilibj/PowerDistributionTest.java new file mode 100644 index 00000000000..a76323b6d39 --- /dev/null +++ b/wpilibj/src/test/java/edu/wpi/first/wpilibj/PowerDistributionTest.java @@ -0,0 +1,29 @@ +// 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.wpilibj; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import edu.wpi.first.hal.HAL; +import edu.wpi.first.wpilibj.PowerDistribution.ModuleType; +import edu.wpi.first.wpilibj.simulation.PDPSim; +import org.junit.jupiter.api.Test; + +class PowerDistributionTest { + @Test + void testGetAllCurrents() { + HAL.initialize(500, 0); + PowerDistribution pdp = new PowerDistribution(1, ModuleType.kRev); + PDPSim sim = new PDPSim(pdp); + + for (int i = 0; i < pdp.getNumChannels(); i++) { + sim.setCurrent(i, 24 - i); + } + double[] currents = pdp.getAllCurrents(); + for (int i = 0; i < pdp.getNumChannels(); i++) { + assertEquals(24 - i, currents[i]); + } + } +} From 74b391ff7e60b9855b49d1acac0ed84670ef313f Mon Sep 17 00:00:00 2001 From: Steven Date: Sat, 9 Dec 2023 13:03:44 -0500 Subject: [PATCH 2/6] fix format issues --- hal/src/main/native/cpp/jni/PowerDistributionJNI.cpp | 8 +++++--- wpilibc/src/main/native/cpp/PowerDistribution.cpp | 5 +++-- wpilibc/src/test/native/cpp/simulation/PDPSimTest.cpp | 10 +++++----- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/hal/src/main/native/cpp/jni/PowerDistributionJNI.cpp b/hal/src/main/native/cpp/jni/PowerDistributionJNI.cpp index da60ac0e304..f61c01c7ef9 100644 --- a/hal/src/main/native/cpp/jni/PowerDistributionJNI.cpp +++ b/hal/src/main/native/cpp/jni/PowerDistributionJNI.cpp @@ -180,10 +180,12 @@ Java_edu_wpi_first_hal_PowerDistributionJNI_getAllCurrents (JNIEnv* env, jclass, jint handle, jdoubleArray jarr) { int32_t status = 0; - int32_t size = HAL_GetPowerDistributionNumChannels(handle, &status); //status won't change here - std::vector storage(size,0); + // status won't change here + int32_t size = HAL_GetPowerDistributionNumChannels(handle, &status); + std::vector storage(size, 0); - HAL_GetPowerDistributionAllChannelCurrents(handle, storage.data(), size, &status); + HAL_GetPowerDistributionAllChannelCurrents(handle, storage.data(), size, + &status); if (!CheckStatus(env, status, false)) { return; } diff --git a/wpilibc/src/main/native/cpp/PowerDistribution.cpp b/wpilibc/src/main/native/cpp/PowerDistribution.cpp index fcb75700c47..115fc38e869 100644 --- a/wpilibc/src/main/native/cpp/PowerDistribution.cpp +++ b/wpilibc/src/main/native/cpp/PowerDistribution.cpp @@ -97,9 +97,10 @@ double PowerDistribution::GetCurrent(int channel) const { std::vector PowerDistribution::GetAllCurrents() const { int32_t status = 0; int32_t size = this->GetNumChannels(); - std::vector currents(size,0); + std::vector currents(size, 0); - HAL_GetPowerDistributionAllChannelCurrents(m_handle, currents.data(), size, &status); + HAL_GetPowerDistributionAllChannelCurrents(m_handle, currents.data(), size, + &status); FRC_ReportError(status, "Module {}", m_module); return currents; } diff --git a/wpilibc/src/test/native/cpp/simulation/PDPSimTest.cpp b/wpilibc/src/test/native/cpp/simulation/PDPSimTest.cpp index f80123b7b1c..fc61ebfe613 100644 --- a/wpilibc/src/test/native/cpp/simulation/PDPSimTest.cpp +++ b/wpilibc/src/test/native/cpp/simulation/PDPSimTest.cpp @@ -85,16 +85,16 @@ TEST(PowerDistributionSimTest, GetAllCurrents) { PowerDistribution pdp{2, frc::PowerDistribution::ModuleType::kRev}; PowerDistributionSim sim(pdp); - //setup + // setup for (int channel = 0; channel < pdp.GetNumChannels(); ++channel) { const double kTestCurrent = 24 - channel; sim.SetCurrent(channel, kTestCurrent); } - - //run it + + // run it std::vector currents = pdp.GetAllCurrents(); - - //verify + + // verify for (int channel = 0; channel < pdp.GetNumChannels(); ++channel) { const double kTestCurrent = 24 - channel; EXPECT_EQ(kTestCurrent, currents[channel]); From 908b11be1bd49e2be8e06e229b52979e30dbe841 Mon Sep 17 00:00:00 2001 From: Steven Date: Sat, 9 Dec 2023 13:21:32 -0500 Subject: [PATCH 3/6] cleanup suggested by PeterJohnson --- hal/src/main/native/cpp/jni/PowerDistributionJNI.cpp | 9 ++++----- wpilibc/src/main/native/cpp/PowerDistribution.cpp | 6 +++--- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/hal/src/main/native/cpp/jni/PowerDistributionJNI.cpp b/hal/src/main/native/cpp/jni/PowerDistributionJNI.cpp index f61c01c7ef9..d486556c98e 100644 --- a/hal/src/main/native/cpp/jni/PowerDistributionJNI.cpp +++ b/hal/src/main/native/cpp/jni/PowerDistributionJNI.cpp @@ -180,17 +180,16 @@ Java_edu_wpi_first_hal_PowerDistributionJNI_getAllCurrents (JNIEnv* env, jclass, jint handle, jdoubleArray jarr) { int32_t status = 0; - // status won't change here - int32_t size = HAL_GetPowerDistributionNumChannels(handle, &status); - std::vector storage(size, 0); + wpi::SmallVector storage; + storage.resize_for_overwrite(24); - HAL_GetPowerDistributionAllChannelCurrents(handle, storage.data(), size, + HAL_GetPowerDistributionAllChannelCurrents(handle, storage.data(), 24, &status); if (!CheckStatus(env, status, false)) { return; } - env->SetDoubleArrayRegion(jarr, 0, size, storage.data()); + env->SetDoubleArrayRegion(jarr, 0, 24, storage.data()); } /* diff --git a/wpilibc/src/main/native/cpp/PowerDistribution.cpp b/wpilibc/src/main/native/cpp/PowerDistribution.cpp index 115fc38e869..199258f19e6 100644 --- a/wpilibc/src/main/native/cpp/PowerDistribution.cpp +++ b/wpilibc/src/main/native/cpp/PowerDistribution.cpp @@ -96,8 +96,8 @@ double PowerDistribution::GetCurrent(int channel) const { std::vector PowerDistribution::GetAllCurrents() const { int32_t status = 0; - int32_t size = this->GetNumChannels(); - std::vector currents(size, 0); + int32_t size = GetNumChannels(); + std::vector currents(size); HAL_GetPowerDistributionAllChannelCurrents(m_handle, currents.data(), size, &status); @@ -206,7 +206,7 @@ PowerDistribution::StickyFaults PowerDistribution::GetStickyFaults() const { void PowerDistribution::InitSendable(wpi::SendableBuilder& builder) { builder.SetSmartDashboardType("PowerDistribution"); - int numChannels = this->GetNumChannels(); + int numChannels = GetNumChannels(); // Use manual reads to avoid printing errors for (int i = 0; i < numChannels; ++i) { builder.AddDoubleProperty( From ba2373f3a175dad6d36d6934328a23c56a60def6 Mon Sep 17 00:00:00 2001 From: Steven Date: Sat, 9 Dec 2023 13:36:55 -0500 Subject: [PATCH 4/6] another format issue --- wpilibc/src/main/native/include/frc/PowerDistribution.h | 1 + 1 file changed, 1 insertion(+) diff --git a/wpilibc/src/main/native/include/frc/PowerDistribution.h b/wpilibc/src/main/native/include/frc/PowerDistribution.h index b47f04504ef..8e2fe615811 100644 --- a/wpilibc/src/main/native/include/frc/PowerDistribution.h +++ b/wpilibc/src/main/native/include/frc/PowerDistribution.h @@ -4,6 +4,7 @@ #pragma once +#include #include #include #include From 914dc8a3a3b4762e2a4aaa0105f644ff7db9782d Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Sat, 9 Dec 2023 18:41:43 +0000 Subject: [PATCH 5/6] Formatting fixes --- wpilibc/src/main/native/include/frc/PowerDistribution.h | 1 + 1 file changed, 1 insertion(+) diff --git a/wpilibc/src/main/native/include/frc/PowerDistribution.h b/wpilibc/src/main/native/include/frc/PowerDistribution.h index 8e2fe615811..f8d69620b12 100644 --- a/wpilibc/src/main/native/include/frc/PowerDistribution.h +++ b/wpilibc/src/main/native/include/frc/PowerDistribution.h @@ -5,6 +5,7 @@ #pragma once #include + #include #include #include From dd2f92eb7f6456f6ea2b95a5b17e3535ea96c125 Mon Sep 17 00:00:00 2001 From: Steven Date: Sat, 9 Dec 2023 13:55:12 -0500 Subject: [PATCH 6/6] Size is needed for the JNI to return the right amount of channels --- hal/src/main/native/cpp/jni/PowerDistributionJNI.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/hal/src/main/native/cpp/jni/PowerDistributionJNI.cpp b/hal/src/main/native/cpp/jni/PowerDistributionJNI.cpp index d486556c98e..1d505283400 100644 --- a/hal/src/main/native/cpp/jni/PowerDistributionJNI.cpp +++ b/hal/src/main/native/cpp/jni/PowerDistributionJNI.cpp @@ -180,16 +180,17 @@ Java_edu_wpi_first_hal_PowerDistributionJNI_getAllCurrents (JNIEnv* env, jclass, jint handle, jdoubleArray jarr) { int32_t status = 0; + int32_t size = HAL_GetPowerDistributionNumChannels(handle, &status); wpi::SmallVector storage; - storage.resize_for_overwrite(24); + storage.resize_for_overwrite(size); - HAL_GetPowerDistributionAllChannelCurrents(handle, storage.data(), 24, + HAL_GetPowerDistributionAllChannelCurrents(handle, storage.data(), size, &status); if (!CheckStatus(env, status, false)) { return; } - env->SetDoubleArrayRegion(jarr, 0, 24, storage.data()); + env->SetDoubleArrayRegion(jarr, 0, size, storage.data()); } /*