mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-20 00:51:42 +00:00
[wpimath] Move debouncer to filters (#3838)
This commit is contained in:
58
wpimath/src/test/native/cpp/filter/DebouncerTest.cpp
Normal file
58
wpimath/src/test/native/cpp/filter/DebouncerTest.cpp
Normal file
@@ -0,0 +1,58 @@
|
||||
// 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 <wpi/timestamp.h>
|
||||
|
||||
#include "frc/filter/Debouncer.h"
|
||||
#include "gtest/gtest.h"
|
||||
#include "units/time.h"
|
||||
|
||||
static units::second_t now = 0_s;
|
||||
|
||||
class DebouncerTest : public ::testing::Test {
|
||||
protected:
|
||||
void SetUp() override {
|
||||
WPI_SetNowImpl([] { return units::microsecond_t{now}.to<uint64_t>(); });
|
||||
}
|
||||
|
||||
void TearDown() override { WPI_SetNowImpl(nullptr); }
|
||||
};
|
||||
|
||||
TEST_F(DebouncerTest, DebounceRising) {
|
||||
frc::Debouncer debouncer{20_ms};
|
||||
|
||||
debouncer.Calculate(false);
|
||||
EXPECT_FALSE(debouncer.Calculate(true));
|
||||
|
||||
now += 1_s;
|
||||
|
||||
EXPECT_TRUE(debouncer.Calculate(true));
|
||||
}
|
||||
|
||||
TEST_F(DebouncerTest, DebounceFalling) {
|
||||
frc::Debouncer debouncer{20_ms, frc::Debouncer::DebounceType::kFalling};
|
||||
|
||||
debouncer.Calculate(true);
|
||||
EXPECT_TRUE(debouncer.Calculate(false));
|
||||
|
||||
now += 1_s;
|
||||
|
||||
EXPECT_FALSE(debouncer.Calculate(false));
|
||||
}
|
||||
|
||||
TEST_F(DebouncerTest, DebounceBoth) {
|
||||
frc::Debouncer debouncer{20_ms, frc::Debouncer::DebounceType::kBoth};
|
||||
|
||||
debouncer.Calculate(false);
|
||||
EXPECT_FALSE(debouncer.Calculate(true));
|
||||
|
||||
now += 1_s;
|
||||
|
||||
EXPECT_TRUE(debouncer.Calculate(true));
|
||||
EXPECT_TRUE(debouncer.Calculate(false));
|
||||
|
||||
now += 1_s;
|
||||
|
||||
EXPECT_FALSE(debouncer.Calculate(false));
|
||||
}
|
||||
@@ -12,7 +12,16 @@
|
||||
|
||||
static units::second_t now = 0_s;
|
||||
|
||||
TEST(SlewRateLimiterTest, SlewRateLimit) {
|
||||
class SlewRateLimiterTest : public ::testing::Test {
|
||||
protected:
|
||||
void SetUp() override {
|
||||
WPI_SetNowImpl([] { return units::microsecond_t{now}.to<uint64_t>(); });
|
||||
}
|
||||
|
||||
void TearDown() override { WPI_SetNowImpl(nullptr); }
|
||||
};
|
||||
|
||||
TEST_F(SlewRateLimiterTest, SlewRateLimit) {
|
||||
WPI_SetNowImpl([] { return units::microsecond_t{now}.to<uint64_t>(); });
|
||||
|
||||
frc::SlewRateLimiter<units::meters> limiter(1_mps);
|
||||
@@ -22,9 +31,7 @@ TEST(SlewRateLimiterTest, SlewRateLimit) {
|
||||
EXPECT_LT(limiter.Calculate(2_m), 2_m);
|
||||
}
|
||||
|
||||
TEST(SlewRateLimiterTest, SlewRateNoLimit) {
|
||||
WPI_SetNowImpl([] { return units::microsecond_t{now}.to<uint64_t>(); });
|
||||
|
||||
TEST_F(SlewRateLimiterTest, SlewRateNoLimit) {
|
||||
frc::SlewRateLimiter<units::meters> limiter(1_mps);
|
||||
|
||||
now += 1_s;
|
||||
|
||||
Reference in New Issue
Block a user