mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-21 01:01:43 +00:00
[wpimath] Move debouncer to filters (#3838)
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
// 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.math.filter;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import edu.wpi.first.util.WPIUtilJNI;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class DebouncerTest {
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
WPIUtilJNI.enableMockTime();
|
||||
WPIUtilJNI.setMockTime(0L);
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void tearDown() {
|
||||
WPIUtilJNI.setMockTime(0L);
|
||||
WPIUtilJNI.disableMockTime();
|
||||
}
|
||||
|
||||
@Test
|
||||
void debounceRisingTest() {
|
||||
var debouncer = new Debouncer(0.02, Debouncer.DebounceType.kRising);
|
||||
|
||||
debouncer.calculate(false);
|
||||
assertFalse(debouncer.calculate(true));
|
||||
|
||||
WPIUtilJNI.setMockTime(1000000L);
|
||||
|
||||
assertTrue(debouncer.calculate(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
void debounceFallingTest() {
|
||||
var debouncer = new Debouncer(0.02, Debouncer.DebounceType.kFalling);
|
||||
|
||||
debouncer.calculate(true);
|
||||
assertTrue(debouncer.calculate(false));
|
||||
|
||||
WPIUtilJNI.setMockTime(1000000L);
|
||||
|
||||
assertFalse(debouncer.calculate(false));
|
||||
|
||||
WPIUtilJNI.setMockTime(0L);
|
||||
}
|
||||
|
||||
@Test
|
||||
void debounceBothTest() {
|
||||
var debouncer = new Debouncer(0.02, Debouncer.DebounceType.kBoth);
|
||||
|
||||
debouncer.calculate(false);
|
||||
assertFalse(debouncer.calculate(true));
|
||||
|
||||
WPIUtilJNI.setMockTime(1000000L);
|
||||
|
||||
assertTrue(debouncer.calculate(true));
|
||||
assertTrue(debouncer.calculate(false));
|
||||
|
||||
WPIUtilJNI.setMockTime(2000000L);
|
||||
|
||||
assertFalse(debouncer.calculate(false));
|
||||
}
|
||||
}
|
||||
@@ -8,28 +8,34 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import edu.wpi.first.util.WPIUtilJNI;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class SlewRateLimiterTest {
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
WPIUtilJNI.enableMockTime();
|
||||
WPIUtilJNI.setMockTime(0L);
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void tearDown() {
|
||||
WPIUtilJNI.setMockTime(0L);
|
||||
WPIUtilJNI.disableMockTime();
|
||||
}
|
||||
|
||||
@Test
|
||||
void slewRateLimitTest() {
|
||||
WPIUtilJNI.enableMockTime();
|
||||
|
||||
var limiter = new SlewRateLimiter(1);
|
||||
WPIUtilJNI.setMockTime(1000000L);
|
||||
assertTrue(limiter.calculate(2) < 2);
|
||||
|
||||
WPIUtilJNI.setMockTime(0L);
|
||||
}
|
||||
|
||||
@Test
|
||||
void slewRateNoLimitTest() {
|
||||
WPIUtilJNI.enableMockTime();
|
||||
|
||||
var limiter = new SlewRateLimiter(1);
|
||||
WPIUtilJNI.setMockTime(1000000L);
|
||||
assertEquals(limiter.calculate(0.5), 0.5);
|
||||
|
||||
WPIUtilJNI.setMockTime(0L);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user