From 0ad595c33c52828394e64ec2a080eb6c2eacce80 Mon Sep 17 00:00:00 2001 From: crueter Date: Mon, 3 Mar 2025 19:21:38 -0500 Subject: [PATCH] [wpimath] Add Debouncer type and time setters (#7839) Signed-off-by: swurl Co-authored-by: Tyler Veness --- .../edu/wpi/first/math/filter/Debouncer.java | 42 ++++++++++++++++++- .../native/include/frc/filter/Debouncer.h | 32 ++++++++++++++ .../wpi/first/math/filter/DebouncerTest.java | 18 ++++++++ .../test/native/cpp/filter/DebouncerTest.cpp | 17 ++++++++ 4 files changed, 107 insertions(+), 2 deletions(-) diff --git a/wpimath/src/main/java/edu/wpi/first/math/filter/Debouncer.java b/wpimath/src/main/java/edu/wpi/first/math/filter/Debouncer.java index 9a281e83f7..a19de59f0b 100644 --- a/wpimath/src/main/java/edu/wpi/first/math/filter/Debouncer.java +++ b/wpimath/src/main/java/edu/wpi/first/math/filter/Debouncer.java @@ -21,8 +21,8 @@ public class Debouncer { kBoth } - private final double m_debounceTimeSeconds; - private final DebounceType m_debounceType; + private double m_debounceTimeSeconds; + private DebounceType m_debounceType; private boolean m_baseline; private double m_prevTimeSeconds; @@ -86,4 +86,42 @@ public class Debouncer { return m_baseline; } } + + /** + * Sets the time to debounce. + * + * @param time The number of seconds the value must change from baseline for the filtered value to + * change. + */ + public void setDebounceTime(double time) { + m_debounceTimeSeconds = time; + } + + /** + * Gets the time to debounce. + * + * @return The number of seconds the value must change from baseline for the filtered value to + * change. + */ + public double getDebounceTime() { + return m_debounceTimeSeconds; + } + + /** + * Sets the debounce type. + * + * @param type Which type of state change the debouncing will be performed on. + */ + public void setDebounceType(DebounceType type) { + m_debounceType = type; + } + + /** + * Gets the debounce type. + * + * @return Which type of state change the debouncing will be performed on. + */ + public DebounceType getDebounceType() { + return m_debounceType; + } } diff --git a/wpimath/src/main/native/include/frc/filter/Debouncer.h b/wpimath/src/main/native/include/frc/filter/Debouncer.h index 9ab3e0e168..45ed5df8e6 100644 --- a/wpimath/src/main/native/include/frc/filter/Debouncer.h +++ b/wpimath/src/main/native/include/frc/filter/Debouncer.h @@ -48,6 +48,38 @@ class WPILIB_DLLEXPORT Debouncer { */ bool Calculate(bool input); + /** + * Sets the time to debounce. + * + * @param time The number of seconds the value must change from baseline + * for the filtered value to change. + */ + constexpr void SetDebounceTime(units::second_t time) { + m_debounceTime = time; + } + + /** + * Gets the time to debounce. + * + * @return The number of seconds the value must change from baseline + * for the filtered value to change. + */ + constexpr units::second_t GetDebounceTime() const { return m_debounceTime; } + + /** + * Set the debounce type. + * + * @param type Which type of state change the debouncing will be performed on. + */ + constexpr void SetDebounceType(DebounceType type) { m_debounceType = type; } + + /** + * Get the debounce type. + * + * @return Which type of state change the debouncing will be performed on. + */ + constexpr DebounceType GetDebounceType() const { return m_debounceType; } + private: units::second_t m_debounceTime; bool m_baseline; diff --git a/wpimath/src/test/java/edu/wpi/first/math/filter/DebouncerTest.java b/wpimath/src/test/java/edu/wpi/first/math/filter/DebouncerTest.java index b6334de009..8cf8fd8812 100644 --- a/wpimath/src/test/java/edu/wpi/first/math/filter/DebouncerTest.java +++ b/wpimath/src/test/java/edu/wpi/first/math/filter/DebouncerTest.java @@ -4,7 +4,9 @@ package edu.wpi.first.math.filter; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertTrue; import edu.wpi.first.util.WPIUtilJNI; @@ -67,4 +69,20 @@ class DebouncerTest { assertFalse(debouncer.calculate(false)); } + + @Test + void debounceParamsTest() { + var debouncer = new Debouncer(0.02, Debouncer.DebounceType.kBoth); + + assertEquals(debouncer.getDebounceTime(), 0.02); + assertSame(debouncer.getDebounceType(), Debouncer.DebounceType.kBoth); + + debouncer.setDebounceTime(0.1); + + assertEquals(debouncer.getDebounceTime(), 0.1); + + debouncer.setDebounceType(Debouncer.DebounceType.kFalling); + + assertSame(debouncer.getDebounceType(), Debouncer.DebounceType.kFalling); + } } diff --git a/wpimath/src/test/native/cpp/filter/DebouncerTest.cpp b/wpimath/src/test/native/cpp/filter/DebouncerTest.cpp index d232739b7a..e1a990964b 100644 --- a/wpimath/src/test/native/cpp/filter/DebouncerTest.cpp +++ b/wpimath/src/test/native/cpp/filter/DebouncerTest.cpp @@ -56,3 +56,20 @@ TEST_F(DebouncerTest, DebounceBoth) { EXPECT_FALSE(debouncer.Calculate(false)); } + +TEST_F(DebouncerTest, DebounceParams) { + frc::Debouncer debouncer{20_ms, frc::Debouncer::DebounceType::kBoth}; + + EXPECT_TRUE(debouncer.GetDebounceTime() == 20_ms); + EXPECT_TRUE(debouncer.GetDebounceType() == + frc::Debouncer::DebounceType::kBoth); + + debouncer.SetDebounceTime(100_ms); + + EXPECT_TRUE(debouncer.GetDebounceTime() == 100_ms); + + debouncer.SetDebounceType(frc::Debouncer::DebounceType::kFalling); + + EXPECT_TRUE(debouncer.GetDebounceType() == + frc::Debouncer::DebounceType::kFalling); +}