mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-07-05 03:21:42 +00:00
[wpimath] Move SlewRateLimiter from wpilib to wpimath (#3399)
Timer was replaced with wpi::Now() to avoid a dependency on other wpilib classes.
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
// 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.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import edu.wpi.first.wpiutil.WPIUtilJNI;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class SlewRateLimiterTest {
|
||||
@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);
|
||||
}
|
||||
}
|
||||
33
wpimath/src/test/native/cpp/SlewRateLimiterTest.cpp
Normal file
33
wpimath/src/test/native/cpp/SlewRateLimiterTest.cpp
Normal file
@@ -0,0 +1,33 @@
|
||||
// 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/SlewRateLimiter.h"
|
||||
#include "gtest/gtest.h"
|
||||
#include "units/length.h"
|
||||
#include "units/time.h"
|
||||
#include "units/velocity.h"
|
||||
|
||||
static units::second_t now = 0_s;
|
||||
|
||||
TEST(SlewRateLimiterTest, SlewRateLimitTest) {
|
||||
WPI_SetNowImpl([] { return units::microsecond_t{now}.to<uint64_t>(); });
|
||||
|
||||
frc::SlewRateLimiter<units::meters> limiter(1_mps);
|
||||
|
||||
now += 1_s;
|
||||
|
||||
EXPECT_LT(limiter.Calculate(2_m), 2_m);
|
||||
}
|
||||
|
||||
TEST(SlewRateLimiterTest, SlewRateNoLimitTest) {
|
||||
WPI_SetNowImpl([] { return units::microsecond_t{now}.to<uint64_t>(); });
|
||||
|
||||
frc::SlewRateLimiter<units::meters> limiter(1_mps);
|
||||
|
||||
now += 1_s;
|
||||
|
||||
EXPECT_EQ(limiter.Calculate(0.5_m), 0.5_m);
|
||||
}
|
||||
Reference in New Issue
Block a user