mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-07-02 02:51:42 +00:00
Move applicable integration tests to native build as unit tests (#1364)
This commit is contained in:
committed by
Peter Johnson
parent
e89d5eb692
commit
99e0f08a6f
@@ -14,14 +14,14 @@
|
||||
using namespace frc;
|
||||
|
||||
static const std::array<double, 10> values = {
|
||||
751.848, 766.366, 342.657, 234.252, 716.126,
|
||||
132.344, 445.697, 22.727, 421.125, 799.913};
|
||||
{751.848, 766.366, 342.657, 234.252, 716.126, 132.344, 445.697, 22.727,
|
||||
421.125, 799.913}};
|
||||
|
||||
static const std::array<double, 8> pushFrontOut = {
|
||||
799.913, 421.125, 22.727, 445.697, 132.344, 716.126, 234.252, 342.657};
|
||||
{799.913, 421.125, 22.727, 445.697, 132.344, 716.126, 234.252, 342.657}};
|
||||
|
||||
static const std::array<double, 8> pushBackOut = {
|
||||
342.657, 234.252, 716.126, 132.344, 445.697, 22.727, 421.125, 799.913};
|
||||
{342.657, 234.252, 716.126, 132.344, 445.697, 22.727, 421.125, 799.913}};
|
||||
|
||||
TEST(CircularBufferTest, PushFrontTest) {
|
||||
circular_buffer<double> queue(8);
|
||||
@@ -13,10 +13,20 @@
|
||||
#include <random>
|
||||
#include <thread>
|
||||
|
||||
#include "TestBench.h"
|
||||
#include "frc/Base.h"
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
/* Filter constants */
|
||||
static constexpr double kFilterStep = 0.005;
|
||||
static constexpr double kFilterTime = 2.0;
|
||||
static constexpr double kSinglePoleIIRTimeConstant = 0.015915;
|
||||
static constexpr double kSinglePoleIIRExpectedOutput = -3.2172003;
|
||||
static constexpr double kHighPassTimeConstant = 0.006631;
|
||||
static constexpr double kHighPassExpectedOutput = 10.074717;
|
||||
static constexpr int32_t kMovAvgTaps = 6;
|
||||
static constexpr double kMovAvgExpectedOutput = -10.191644;
|
||||
static constexpr double kPi = 3.14159265358979323846;
|
||||
|
||||
using namespace frc;
|
||||
|
||||
enum FilterNoiseTestType { TEST_SINGLE_POLE_IIR, TEST_MOVAVG };
|
||||
@@ -53,18 +63,18 @@ class NoiseGenerator : public PIDSource {
|
||||
|
||||
double PIDGet() override {
|
||||
m_noise = m_distr(m_gen);
|
||||
m_count += TestBench::kFilterStep;
|
||||
m_count += kFilterStep;
|
||||
return m_dataFunc(m_count) + m_noise;
|
||||
}
|
||||
|
||||
void Reset() { m_count = -TestBench::kFilterStep; }
|
||||
void Reset() { m_count = -kFilterStep; }
|
||||
|
||||
private:
|
||||
std::function<double(double)> m_dataFunc;
|
||||
double m_noise = 0.0;
|
||||
|
||||
// Make sure first call to PIDGet() uses m_count == 0
|
||||
double m_count = -TestBench::kFilterStep;
|
||||
double m_count = -kFilterStep;
|
||||
|
||||
std::random_device m_rd;
|
||||
std::mt19937 m_gen{m_rd()};
|
||||
@@ -79,7 +89,7 @@ class FilterNoiseTest : public testing::TestWithParam<FilterNoiseTestType> {
|
||||
std::unique_ptr<PIDSource> m_filter;
|
||||
std::shared_ptr<NoiseGenerator> m_noise;
|
||||
|
||||
static double GetData(double t) { return 100.0 * std::sin(2.0 * M_PI * t); }
|
||||
static double GetData(double t) { return 100.0 * std::sin(2.0 * kPi * t); }
|
||||
|
||||
void SetUp() override {
|
||||
m_noise = std::make_shared<NoiseGenerator>(GetData, kStdDev);
|
||||
@@ -88,15 +98,13 @@ class FilterNoiseTest : public testing::TestWithParam<FilterNoiseTestType> {
|
||||
case TEST_SINGLE_POLE_IIR: {
|
||||
m_filter = std::make_unique<LinearDigitalFilter>(
|
||||
LinearDigitalFilter::SinglePoleIIR(
|
||||
m_noise, TestBench::kSinglePoleIIRTimeConstant,
|
||||
TestBench::kFilterStep));
|
||||
m_noise, kSinglePoleIIRTimeConstant, kFilterStep));
|
||||
break;
|
||||
}
|
||||
|
||||
case TEST_MOVAVG: {
|
||||
m_filter = std::make_unique<LinearDigitalFilter>(
|
||||
LinearDigitalFilter::MovingAverage(m_noise,
|
||||
TestBench::kMovAvgTaps));
|
||||
LinearDigitalFilter::MovingAverage(m_noise, kMovAvgTaps));
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -112,7 +120,7 @@ TEST_P(FilterNoiseTest, NoiseReduce) {
|
||||
double filterError = 0.0;
|
||||
|
||||
m_noise->Reset();
|
||||
for (double t = 0; t < TestBench::kFilterTime; t += TestBench::kFilterStep) {
|
||||
for (double t = 0; t < kFilterTime; t += kFilterStep) {
|
||||
theoryData = GetData(t);
|
||||
filterError += std::abs(m_filter->PIDGet() - theoryData);
|
||||
noiseGenError += std::abs(m_noise->Get() - theoryData);
|
||||
@@ -13,10 +13,20 @@
|
||||
#include <random>
|
||||
#include <thread>
|
||||
|
||||
#include "TestBench.h"
|
||||
#include "frc/Base.h"
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
/* Filter constants */
|
||||
static constexpr double kFilterStep = 0.005;
|
||||
static constexpr double kFilterTime = 2.0;
|
||||
static constexpr double kSinglePoleIIRTimeConstant = 0.015915;
|
||||
static constexpr double kSinglePoleIIRExpectedOutput = -3.2172003;
|
||||
static constexpr double kHighPassTimeConstant = 0.006631;
|
||||
static constexpr double kHighPassExpectedOutput = 10.074717;
|
||||
static constexpr int32_t kMovAvgTaps = 6;
|
||||
static constexpr double kMovAvgExpectedOutput = -10.191644;
|
||||
static constexpr double kPi = 3.14159265358979323846;
|
||||
|
||||
using namespace frc;
|
||||
|
||||
enum FilterOutputTestType {
|
||||
@@ -52,17 +62,17 @@ class DataWrapper : public PIDSource {
|
||||
virtual void SetPIDSourceType(PIDSourceType pidSource) {}
|
||||
|
||||
virtual double PIDGet() {
|
||||
m_count += TestBench::kFilterStep;
|
||||
m_count += kFilterStep;
|
||||
return m_dataFunc(m_count);
|
||||
}
|
||||
|
||||
void Reset() { m_count = -TestBench::kFilterStep; }
|
||||
void Reset() { m_count = -kFilterStep; }
|
||||
|
||||
private:
|
||||
std::function<double(double)> m_dataFunc;
|
||||
|
||||
// Make sure first call to PIDGet() uses m_count == 0
|
||||
double m_count = -TestBench::kFilterStep;
|
||||
double m_count = -kFilterStep;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -75,7 +85,7 @@ class FilterOutputTest : public testing::TestWithParam<FilterOutputTestType> {
|
||||
double m_expectedOutput = 0.0;
|
||||
|
||||
static double GetData(double t) {
|
||||
return 100.0 * std::sin(2.0 * M_PI * t) + 20.0 * std::cos(50.0 * M_PI * t);
|
||||
return 100.0 * std::sin(2.0 * kPi * t) + 20.0 * std::cos(50.0 * kPi * t);
|
||||
}
|
||||
|
||||
static double GetPulseData(double t) {
|
||||
@@ -92,9 +102,8 @@ class FilterOutputTest : public testing::TestWithParam<FilterOutputTestType> {
|
||||
m_data = std::make_shared<DataWrapper>(GetData);
|
||||
m_filter = std::make_unique<LinearDigitalFilter>(
|
||||
LinearDigitalFilter::SinglePoleIIR(
|
||||
m_data, TestBench::kSinglePoleIIRTimeConstant,
|
||||
TestBench::kFilterStep));
|
||||
m_expectedOutput = TestBench::kSinglePoleIIRExpectedOutput;
|
||||
m_data, kSinglePoleIIRTimeConstant, kFilterStep));
|
||||
m_expectedOutput = kSinglePoleIIRExpectedOutput;
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -102,24 +111,23 @@ class FilterOutputTest : public testing::TestWithParam<FilterOutputTestType> {
|
||||
m_data = std::make_shared<DataWrapper>(GetData);
|
||||
m_filter =
|
||||
std::make_unique<LinearDigitalFilter>(LinearDigitalFilter::HighPass(
|
||||
m_data, TestBench::kHighPassTimeConstant,
|
||||
TestBench::kFilterStep));
|
||||
m_expectedOutput = TestBench::kHighPassExpectedOutput;
|
||||
m_data, kHighPassTimeConstant, kFilterStep));
|
||||
m_expectedOutput = kHighPassExpectedOutput;
|
||||
break;
|
||||
}
|
||||
|
||||
case TEST_MOVAVG: {
|
||||
m_data = std::make_shared<DataWrapper>(GetData);
|
||||
m_filter = std::make_unique<LinearDigitalFilter>(
|
||||
LinearDigitalFilter::MovingAverage(m_data, TestBench::kMovAvgTaps));
|
||||
m_expectedOutput = TestBench::kMovAvgExpectedOutput;
|
||||
LinearDigitalFilter::MovingAverage(m_data, kMovAvgTaps));
|
||||
m_expectedOutput = kMovAvgExpectedOutput;
|
||||
break;
|
||||
}
|
||||
|
||||
case TEST_PULSE: {
|
||||
m_data = std::make_shared<DataWrapper>(GetPulseData);
|
||||
m_filter = std::make_unique<LinearDigitalFilter>(
|
||||
LinearDigitalFilter::MovingAverage(m_data, TestBench::kMovAvgTaps));
|
||||
LinearDigitalFilter::MovingAverage(m_data, kMovAvgTaps));
|
||||
m_expectedOutput = 0.0;
|
||||
break;
|
||||
}
|
||||
@@ -134,8 +142,7 @@ TEST_P(FilterOutputTest, FilterOutput) {
|
||||
m_data->Reset();
|
||||
|
||||
double filterOutput = 0.0;
|
||||
for (double t = 0.0; t < TestBench::kFilterTime;
|
||||
t += TestBench::kFilterStep) {
|
||||
for (double t = 0.0; t < kFilterTime; t += kFilterStep) {
|
||||
filterOutput = m_filter->PIDGet();
|
||||
}
|
||||
|
||||
@@ -6,7 +6,6 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#include "MockSpeedController.h"
|
||||
#include "TestBench.h"
|
||||
#include "frc/RobotDrive.h"
|
||||
#include "frc/drive/DifferentialDrive.h"
|
||||
#include "frc/drive/MecanumDrive.h"
|
||||
@@ -11,7 +11,6 @@
|
||||
#include <vector>
|
||||
|
||||
#include "MockSpeedController.h"
|
||||
#include "TestBench.h"
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
using namespace frc;
|
||||
@@ -56,14 +56,4 @@ class TestBench {
|
||||
/* PCM channels */
|
||||
static const int32_t kSolenoidChannel1 = 0;
|
||||
static const int32_t kSolenoidChannel2 = 1;
|
||||
|
||||
/* Filter constants */
|
||||
static constexpr double kFilterStep = 0.005;
|
||||
static constexpr double kFilterTime = 2.0;
|
||||
static constexpr double kSinglePoleIIRTimeConstant = 0.015915;
|
||||
static constexpr double kSinglePoleIIRExpectedOutput = -3.2172003;
|
||||
static constexpr double kHighPassTimeConstant = 0.006631;
|
||||
static constexpr double kHighPassExpectedOutput = 10.074717;
|
||||
static constexpr int32_t kMovAvgTaps = 6;
|
||||
static constexpr double kMovAvgExpectedOutput = -10.191644;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user