mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-20 00:51:42 +00:00
[wpiutil] Improve wpi::circular_buffer iterators (#3410)
The implementation of wpi::circular_buffer has been effectively replaced with a dynamically sized copy of wpi::static_circular_buffer with a resize() member function.
This commit is contained in:
@@ -5,7 +5,6 @@
|
||||
#include "frc/LinearFilter.h" // NOLINT(build/include_order)
|
||||
|
||||
#include <cmath>
|
||||
#include <memory>
|
||||
#include <random>
|
||||
|
||||
#include <wpi/numbers>
|
||||
@@ -14,26 +13,12 @@
|
||||
#include "units/time.h"
|
||||
|
||||
// Filter constants
|
||||
static constexpr units::second_t kFilterStep = 0.005_s;
|
||||
static constexpr units::second_t kFilterTime = 2.0_s;
|
||||
static constexpr auto kFilterStep = 5_ms;
|
||||
static constexpr auto kFilterTime = 2_s;
|
||||
static constexpr double kSinglePoleIIRTimeConstant = 0.015915;
|
||||
static constexpr int32_t kMovAvgTaps = 6;
|
||||
|
||||
enum LinearFilterNoiseTestType { TEST_SINGLE_POLE_IIR, TEST_MOVAVG };
|
||||
|
||||
std::ostream& operator<<(std::ostream& os,
|
||||
const LinearFilterNoiseTestType& type) {
|
||||
switch (type) {
|
||||
case TEST_SINGLE_POLE_IIR:
|
||||
os << "LinearFilter SinglePoleIIR";
|
||||
break;
|
||||
case TEST_MOVAVG:
|
||||
os << "LinearFilter MovingAverage";
|
||||
break;
|
||||
}
|
||||
|
||||
return os;
|
||||
}
|
||||
enum LinearFilterNoiseTestType { kTestSinglePoleIIR, kTestMovAvg };
|
||||
|
||||
static double GetData(double t) {
|
||||
return 100.0 * std::sin(2.0 * wpi::numbers::pi * t);
|
||||
@@ -42,24 +27,17 @@ static double GetData(double t) {
|
||||
class LinearFilterNoiseTest
|
||||
: public testing::TestWithParam<LinearFilterNoiseTestType> {
|
||||
protected:
|
||||
std::unique_ptr<frc::LinearFilter<double>> m_filter;
|
||||
|
||||
void SetUp() override {
|
||||
frc::LinearFilter<double> m_filter = [=] {
|
||||
switch (GetParam()) {
|
||||
case TEST_SINGLE_POLE_IIR: {
|
||||
m_filter = std::make_unique<frc::LinearFilter<double>>(
|
||||
frc::LinearFilter<double>::SinglePoleIIR(kSinglePoleIIRTimeConstant,
|
||||
kFilterStep));
|
||||
case kTestSinglePoleIIR:
|
||||
return frc::LinearFilter<double>::SinglePoleIIR(
|
||||
kSinglePoleIIRTimeConstant, kFilterStep);
|
||||
break;
|
||||
}
|
||||
|
||||
case TEST_MOVAVG: {
|
||||
m_filter = std::make_unique<frc::LinearFilter<double>>(
|
||||
frc::LinearFilter<double>::MovingAverage(kMovAvgTaps));
|
||||
default:
|
||||
return frc::LinearFilter<double>::MovingAverage(kMovAvgTaps);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}();
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -76,7 +54,7 @@ TEST_P(LinearFilterNoiseTest, NoiseReduce) {
|
||||
for (auto t = 0_s; t < kFilterTime; t += kFilterStep) {
|
||||
double theory = GetData(t.to<double>());
|
||||
double noise = distr(gen);
|
||||
filterError += std::abs(m_filter->Calculate(theory + noise) - theory);
|
||||
filterError += std::abs(m_filter.Calculate(theory + noise) - theory);
|
||||
noiseGenError += std::abs(noise - theory);
|
||||
}
|
||||
|
||||
@@ -88,4 +66,4 @@ TEST_P(LinearFilterNoiseTest, NoiseReduce) {
|
||||
}
|
||||
|
||||
INSTANTIATE_TEST_SUITE_P(Test, LinearFilterNoiseTest,
|
||||
testing::Values(TEST_SINGLE_POLE_IIR, TEST_MOVAVG));
|
||||
testing::Values(kTestSinglePoleIIR, kTestMovAvg));
|
||||
|
||||
@@ -15,8 +15,8 @@
|
||||
#include "units/time.h"
|
||||
|
||||
// Filter constants
|
||||
static constexpr units::second_t kFilterStep = 0.005_s;
|
||||
static constexpr units::second_t kFilterTime = 2.0_s;
|
||||
static constexpr auto kFilterStep = 5_ms;
|
||||
static constexpr auto kFilterTime = 2_s;
|
||||
static constexpr double kSinglePoleIIRTimeConstant = 0.015915;
|
||||
static constexpr double kSinglePoleIIRExpectedOutput = -3.2172003;
|
||||
static constexpr double kHighPassTimeConstant = 0.006631;
|
||||
@@ -25,32 +25,12 @@ static constexpr int32_t kMovAvgTaps = 6;
|
||||
static constexpr double kMovAvgExpectedOutput = -10.191644;
|
||||
|
||||
enum LinearFilterOutputTestType {
|
||||
TEST_SINGLE_POLE_IIR,
|
||||
TEST_HIGH_PASS,
|
||||
TEST_MOVAVG,
|
||||
TEST_PULSE
|
||||
kTestSinglePoleIIR,
|
||||
kTestHighPass,
|
||||
kTestMovAvg,
|
||||
kTestPulse
|
||||
};
|
||||
|
||||
std::ostream& operator<<(std::ostream& os,
|
||||
const LinearFilterOutputTestType& type) {
|
||||
switch (type) {
|
||||
case TEST_SINGLE_POLE_IIR:
|
||||
os << "LinearFilter SinglePoleIIR";
|
||||
break;
|
||||
case TEST_HIGH_PASS:
|
||||
os << "LinearFilter HighPass";
|
||||
break;
|
||||
case TEST_MOVAVG:
|
||||
os << "LinearFilter MovingAverage";
|
||||
break;
|
||||
case TEST_PULSE:
|
||||
os << "LinearFilter Pulse";
|
||||
break;
|
||||
}
|
||||
|
||||
return os;
|
||||
}
|
||||
|
||||
static double GetData(double t) {
|
||||
return 100.0 * std::sin(2.0 * wpi::numbers::pi * t) +
|
||||
20.0 * std::cos(50.0 * wpi::numbers::pi * t);
|
||||
@@ -70,41 +50,48 @@ static double GetPulseData(double t) {
|
||||
class LinearFilterOutputTest
|
||||
: public testing::TestWithParam<LinearFilterOutputTestType> {
|
||||
protected:
|
||||
std::unique_ptr<frc::LinearFilter<double>> m_filter;
|
||||
frc::LinearFilter<double> m_filter = [=] {
|
||||
switch (GetParam()) {
|
||||
case kTestSinglePoleIIR:
|
||||
return frc::LinearFilter<double>::SinglePoleIIR(
|
||||
kSinglePoleIIRTimeConstant, kFilterStep);
|
||||
break;
|
||||
case kTestHighPass:
|
||||
return frc::LinearFilter<double>::HighPass(kHighPassTimeConstant,
|
||||
kFilterStep);
|
||||
break;
|
||||
case kTestMovAvg:
|
||||
return frc::LinearFilter<double>::MovingAverage(kMovAvgTaps);
|
||||
break;
|
||||
default:
|
||||
return frc::LinearFilter<double>::MovingAverage(kMovAvgTaps);
|
||||
break;
|
||||
}
|
||||
}();
|
||||
std::function<double(double)> m_data;
|
||||
double m_expectedOutput = 0.0;
|
||||
|
||||
void SetUp() override {
|
||||
LinearFilterOutputTest() {
|
||||
switch (GetParam()) {
|
||||
case TEST_SINGLE_POLE_IIR: {
|
||||
m_filter = std::make_unique<frc::LinearFilter<double>>(
|
||||
frc::LinearFilter<double>::SinglePoleIIR(kSinglePoleIIRTimeConstant,
|
||||
kFilterStep));
|
||||
case kTestSinglePoleIIR: {
|
||||
m_data = GetData;
|
||||
m_expectedOutput = kSinglePoleIIRExpectedOutput;
|
||||
break;
|
||||
}
|
||||
|
||||
case TEST_HIGH_PASS: {
|
||||
m_filter = std::make_unique<frc::LinearFilter<double>>(
|
||||
frc::LinearFilter<double>::HighPass(kHighPassTimeConstant,
|
||||
kFilterStep));
|
||||
case kTestHighPass: {
|
||||
m_data = GetData;
|
||||
m_expectedOutput = kHighPassExpectedOutput;
|
||||
break;
|
||||
}
|
||||
|
||||
case TEST_MOVAVG: {
|
||||
m_filter = std::make_unique<frc::LinearFilter<double>>(
|
||||
frc::LinearFilter<double>::MovingAverage(kMovAvgTaps));
|
||||
case kTestMovAvg: {
|
||||
m_data = GetData;
|
||||
m_expectedOutput = kMovAvgExpectedOutput;
|
||||
break;
|
||||
}
|
||||
|
||||
case TEST_PULSE: {
|
||||
m_filter = std::make_unique<frc::LinearFilter<double>>(
|
||||
frc::LinearFilter<double>::MovingAverage(kMovAvgTaps));
|
||||
case kTestPulse: {
|
||||
m_data = GetPulseData;
|
||||
m_expectedOutput = 0.0;
|
||||
break;
|
||||
@@ -119,7 +106,7 @@ class LinearFilterOutputTest
|
||||
TEST_P(LinearFilterOutputTest, Output) {
|
||||
double filterOutput = 0.0;
|
||||
for (auto t = 0_s; t < kFilterTime; t += kFilterStep) {
|
||||
filterOutput = m_filter->Calculate(m_data(t.to<double>()));
|
||||
filterOutput = m_filter.Calculate(m_data(t.to<double>()));
|
||||
}
|
||||
|
||||
RecordProperty("LinearFilterOutput", filterOutput);
|
||||
@@ -129,5 +116,5 @@ TEST_P(LinearFilterOutputTest, Output) {
|
||||
}
|
||||
|
||||
INSTANTIATE_TEST_SUITE_P(Test, LinearFilterOutputTest,
|
||||
testing::Values(TEST_SINGLE_POLE_IIR, TEST_HIGH_PASS,
|
||||
TEST_MOVAVG, TEST_PULSE));
|
||||
testing::Values(kTestSinglePoleIIR, kTestHighPass,
|
||||
kTestMovAvg, kTestPulse));
|
||||
|
||||
Reference in New Issue
Block a user