Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wpimath] Fix Debouncer type-changing behavior #7870

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,7 @@ public Debouncer(double debounceTime, DebounceType type) {

resetTimer();

m_baseline =
switch (m_debounceType) {
case kBoth, kRising -> false;
case kFalling -> true;
};
m_baseline = m_debounceType == DebounceType.kFalling;
}

/**
Expand Down Expand Up @@ -114,6 +110,8 @@ public double getDebounceTime() {
*/
public void setDebounceType(DebounceType type) {
m_debounceType = type;

m_baseline = m_debounceType == DebounceType.kFalling;
}

/**
Expand Down
10 changes: 1 addition & 9 deletions wpimath/src/main/native/cpp/filter/Debouncer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,7 @@ using namespace frc;

Debouncer::Debouncer(units::second_t debounceTime, DebounceType type)
: m_debounceTime(debounceTime), m_debounceType(type) {
switch (type) {
case DebounceType::kBoth: // fall-through
case DebounceType::kRising:
m_baseline = false;
break;
case DebounceType::kFalling:
m_baseline = true;
break;
}
m_baseline = m_debounceType == DebounceType::kFalling;
ResetTimer();
}

Expand Down
6 changes: 5 additions & 1 deletion wpimath/src/main/native/include/frc/filter/Debouncer.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,11 @@ class WPILIB_DLLEXPORT Debouncer {
*
* @param type Which type of state change the debouncing will be performed on.
*/
constexpr void SetDebounceType(DebounceType type) { m_debounceType = type; }
constexpr void SetDebounceType(DebounceType type) {
m_debounceType = type;

m_baseline = m_debounceType == DebounceType::kFalling;
}

/**
* Get the debounce type.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,6 @@ void debounceParamsTest() {
debouncer.setDebounceType(Debouncer.DebounceType.kFalling);

assertSame(debouncer.getDebounceType(), Debouncer.DebounceType.kFalling);
assertTrue(debouncer.calculate(false));
}
}
2 changes: 2 additions & 0 deletions wpimath/src/test/native/cpp/filter/DebouncerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,6 @@ TEST_F(DebouncerTest, DebounceParams) {

EXPECT_TRUE(debouncer.GetDebounceType() ==
frc::Debouncer::DebounceType::kFalling);

EXPECT_TRUE(debouncer.Calculate(false));
}
Loading