mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-07-02 02:51:42 +00:00
[wpimath] ApplyDeadband: add a scale param (#3865)
Also templates it in C++ so it can work with both doubles and units.
This commit is contained in:
@@ -10,7 +10,7 @@ import org.junit.jupiter.api.Test;
|
||||
|
||||
class MathUtilTest {
|
||||
@Test
|
||||
void testApplyDeadband() {
|
||||
void testApplyDeadbandUnityScale() {
|
||||
// < 0
|
||||
assertEquals(-1.0, MathUtil.applyDeadband(-1.0, 0.02));
|
||||
assertEquals((-0.03 + 0.02) / (1.0 - 0.02), MathUtil.applyDeadband(-0.03, 0.02));
|
||||
@@ -27,6 +27,27 @@ class MathUtilTest {
|
||||
assertEquals(1.0, MathUtil.applyDeadband(1.0, 0.02));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testApplyDeadbandArbitraryScale() {
|
||||
// < 0
|
||||
assertEquals(-2.5, MathUtil.applyDeadband(-2.5, 0.02, 2.5));
|
||||
assertEquals(0.0, MathUtil.applyDeadband(-0.02, 0.02, 2.5));
|
||||
assertEquals(0.0, MathUtil.applyDeadband(-0.01, 0.02, 2.5));
|
||||
|
||||
// == 0
|
||||
assertEquals(0.0, MathUtil.applyDeadband(0.0, 0.02, 2.5));
|
||||
|
||||
// > 0
|
||||
assertEquals(0.0, MathUtil.applyDeadband(0.01, 0.02, 2.5));
|
||||
assertEquals(0.0, MathUtil.applyDeadband(0.02, 0.02, 2.5));
|
||||
assertEquals(2.5, MathUtil.applyDeadband(2.5, 0.02, 2.5));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testApplyDeadbandLargeMaxMagnitude() {
|
||||
assertEquals(80.0, MathUtil.applyDeadband(100.0, 20, Double.POSITIVE_INFINITY));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testInputModulus() {
|
||||
// These tests check error wrapping. That is, the result of wrapping the
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
// 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 <limits>
|
||||
|
||||
#include "frc/MathUtil.h"
|
||||
#include "gtest/gtest.h"
|
||||
#include "units/angle.h"
|
||||
@@ -10,7 +12,7 @@
|
||||
|
||||
#define EXPECT_UNITS_NEAR(a, b, c) EXPECT_NEAR((a).value(), (b).value(), c)
|
||||
|
||||
TEST(MathUtilTest, ApplyDeadband) {
|
||||
TEST(MathUtilTest, ApplyDeadbandUnityScale) {
|
||||
// < 0
|
||||
EXPECT_DOUBLE_EQ(-1.0, frc::ApplyDeadband(-1.0, 0.02));
|
||||
EXPECT_DOUBLE_EQ((-0.03 + 0.02) / (1.0 - 0.02),
|
||||
@@ -29,6 +31,33 @@ TEST(MathUtilTest, ApplyDeadband) {
|
||||
EXPECT_DOUBLE_EQ(1.0, frc::ApplyDeadband(1.0, 0.02));
|
||||
}
|
||||
|
||||
TEST(MathUtilTest, ApplyDeadbandArbitraryScale) {
|
||||
// < 0
|
||||
EXPECT_DOUBLE_EQ(-2.5, frc::ApplyDeadband(-2.5, 0.02, 2.5));
|
||||
EXPECT_DOUBLE_EQ(0.0, frc::ApplyDeadband(-0.02, 0.02, 2.5));
|
||||
EXPECT_DOUBLE_EQ(0.0, frc::ApplyDeadband(-0.01, 0.02, 2.5));
|
||||
|
||||
// == 0
|
||||
EXPECT_DOUBLE_EQ(0.0, frc::ApplyDeadband(0.0, 0.02, 2.5));
|
||||
|
||||
// > 0
|
||||
EXPECT_DOUBLE_EQ(0.0, frc::ApplyDeadband(0.01, 0.02, 2.5));
|
||||
EXPECT_DOUBLE_EQ(0.0, frc::ApplyDeadband(0.02, 0.02, 2.5));
|
||||
EXPECT_DOUBLE_EQ(2.5, frc::ApplyDeadband(2.5, 0.02, 2.5));
|
||||
}
|
||||
|
||||
TEST(MathUtilTest, ApplyDeadbandUnits) {
|
||||
// < 0
|
||||
EXPECT_DOUBLE_EQ(
|
||||
-20, frc::ApplyDeadband<units::radian_t>(-20_rad, 1_rad, 20_rad).value());
|
||||
}
|
||||
|
||||
TEST(MathUtilTest, ApplyDeadbandLargeMaxMagnitude) {
|
||||
EXPECT_DOUBLE_EQ(
|
||||
80.0,
|
||||
frc::ApplyDeadband(100.0, 20.0, std::numeric_limits<double>::infinity()));
|
||||
}
|
||||
|
||||
TEST(MathUtilTest, InputModulus) {
|
||||
// These tests check error wrapping. That is, the result of wrapping the
|
||||
// result of an angle reference minus the measurement.
|
||||
|
||||
Reference in New Issue
Block a user