fix the mess (#3094)

* test

* s
This commit is contained in:
Matthew Kennedy 2021-08-01 20:21:53 -07:00 committed by GitHub
parent 6f96887d74
commit fcd6c3d520
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 98 additions and 157 deletions

View File

@ -0,0 +1,26 @@
#include "sensor_converter_func.h"
#include "biquad.h"
class FlexConverter : public SensorConverter {
public:
FlexConverter() {
// Update rate is 50-150hz, so this actually filters at 0.5-1.5hz -3db depending on E%, which is ok
m_filter.configureLowpass(100, 1);
}
SensorResult convert(float frequency) const override {
// Sensor should only report 50-150hz, significantly outside that range indicates a problem
// it changes to 200hz+ to indicate methanol "contamination"
if (frequency > 45 && frequency < 155) {
float flexPct = clampF(0, frequency - 50, 100);
return m_filter.filter(flexPct);
} else {
return unexpected;
}
}
private:
mutable Biquad m_filter;
};

View File

@ -2,30 +2,7 @@
#include "pin_repository.h"
#include "engine.h"
#include "frequency_sensor.h"
#include "biquad.h"
class FlexConverter : public SensorConverter {
public:
FlexConverter() {
// Update rate is 50-150hz, so this actually filters at 0.5-1.5hz -3db depending on E%, which is ok
m_filter.configureLowpass(100, 1);
}
SensorResult convert(float frequency) const override {
// Sensor should only report 50-150hz, significantly outside that range indicates a problem
// it changes to 200hz+ to indicate methanol "contamination"
if (frequency > 45 && frequency < 155) {
float flexPct = clampF(0, frequency - 50, 100);
return m_filter.filter(flexPct);
} else {
return unexpected;
}
}
private:
mutable Biquad m_filter;
};
#include "flex_sensor.h"
static FrequencySensor flexSensor(SensorType::FuelEthanolPercent, MS2NT(500));
static FlexConverter converter;

View File

@ -1,132 +0,0 @@
#include "pch.h"
#include "flex_sensor.h"
class FlexSensorForTests : public ::testing::Test
{
protected:
void SetUp () override {
// If somehow prodcode will be unwrapped for test it MAYBE! will fire with error.
// At least we must init FlexSensor somehow
dut.init (GPIO_INVALID);
Sensor::resetRegistry (); // IDK why i must use this. Just copypaste.
}
void TearDown () override {
Sensor::resetRegistry ();
}
/*
* This method must simulate some amount periods of square-wave
* and fire callback on every falling edge.
* (as Sensor works by falling edge)
*/
void WithEngineHelperGenerateInputPWM (EngineTestHelper &eth, float freqHz) {
auto const PERIODS_TO_SIMULATE = 50;
auto period = (1 / freqHz);
std::cout << "PERIOD: " << period << std::endl;
for (auto i = PERIODS_TO_SIMULATE; i > 0; i--)
{
eth.moveTimeForwardSec (period);
dut.onEdge (getTimeNowNt ());
}
}
// todo: some function to convert output value to frequency?
float ConvertExpectedValueToFrequency (float expectedValue) {
return VALID_LOW_FREQ;
}
const float INVALID_LOW_FREQ = 44.0f;
const float VALID_LOW_FREQ = 46.0f;
const float INVALID_HIGH_FREQ = 156.0f;
const float VALID_HIGH_FREQ = 154.0f;
const float VALID_FREQ = 100.0f;
FlexFuelSensor dut;
};
/*
* Sensor must take PWM input on "valid" frequency and generate any input.
*/
TEST_F(FlexSensorForTests, testCreationAndGeneratingAnyValidResult)
{
ASSERT_TRUE(dut.Register ());
WITH_ENGINE_TEST_HELPER(TEST_ENGINE);
// Should be invalid - not set yet
{
auto s = Sensor::get (SensorType::FuelEthanolPercent);
EXPECT_FALSE(s.Valid);
}
WithEngineHelperGenerateInputPWM (eth, VALID_FREQ);
// Should be valid
{
auto s = Sensor::get (SensorType::FuelEthanolPercent);
EXPECT_TRUE(s.Valid);
}
}
/*
* Sensor must take PWM input on "invalid" frequency and generate no input.
*/
TEST_F(FlexSensorForTests, testInvalidFrequency)
{
ASSERT_TRUE(dut.Register ());
WITH_ENGINE_TEST_HELPER(TEST_ENGINE);
// Should be invalid - not set yet
{
auto s = Sensor::get (SensorType::FuelEthanolPercent);
EXPECT_FALSE(s.Valid);
}
WithEngineHelperGenerateInputPWM (eth, INVALID_LOW_FREQ);
// Should be invalid - too low freq
{
auto s = Sensor::get (SensorType::FuelEthanolPercent);
EXPECT_FALSE(s.Valid);
}
WithEngineHelperGenerateInputPWM (eth, INVALID_HIGH_FREQ);
// Should be invalid - too high freq
{
auto s = Sensor::get (SensorType::FuelEthanolPercent);
EXPECT_FALSE(s.Valid);
}
}
/*
* Sensor must take PWM input on frequency with "known output value"
* and generate similiar output.
*/
TEST_F(FlexSensorForTests, testFrequencyConversion)
{
ASSERT_TRUE(dut.Register ());
WITH_ENGINE_TEST_HELPER(TEST_ENGINE);
// Should be invalid - not set yet
{
auto s = Sensor::get (SensorType::FuelEthanolPercent);
EXPECT_FALSE(s.Valid);
}
float expectedValue = 75; // Any between 0 and 100
float freq = ConvertExpectedValueToFrequency (expectedValue);
WithEngineHelperGenerateInputPWM (eth, freq);
// Should be invalid - too low freq
{
auto s = Sensor::get (SensorType::FuelEthanolPercent);
EXPECT_TRUE(s.Valid);
EXPECT_FLOAT_EQ(s.Value, expectedValue);
}
}

View File

@ -0,0 +1,70 @@
#include "pch.h"
#include "frequency_sensor.h"
#include "sensor_type.h"
// Fake converter just passes value straight though
struct IdentityFunction : public SensorConverter {
SensorResult convert(float raw) const {
return raw;
}
};
static IdentityFunction identityFunc;
class FrequencySensorTest : public ::testing::Test {
public:
FrequencySensorTest()
: dut(SensorType::FuelEthanolPercent, MS2NT(50))
{
}
void SetUp() override {
// If somehow prodcode will be unwrapped for test it MAYBE! will fire with error.
// At least we must init FlexSensor somehow
dut.init(GPIO_INVALID);
dut.setFunction(identityFunc);
}
/*
* This method must simulate some amount periods of square-wave
* and fire callback on every falling edge.
* (as Sensor works by falling edge)
*/
void generatePwm(EngineTestHelper &eth, float freqHz) {
constexpr auto periods = 50;
auto period = (1 / freqHz);
std::cout << "PERIOD: " << period << std::endl;
for (auto i = 0; i < periods; i++) {
eth.moveTimeForwardSec(period);
dut.onEdge(getTimeNowNt());
}
}
FrequencySensor dut;
};
/*
* Sensor must take PWM input on "valid" frequency and generate any input.
*/
TEST_F(FrequencySensorTest, testValidWithPwm) {
ASSERT_TRUE(dut.Register());
WITH_ENGINE_TEST_HELPER(TEST_ENGINE);
// Should be invalid - not set yet
{
auto s = Sensor::get(SensorType::FuelEthanolPercent);
EXPECT_FALSE(s.Valid);
}
generatePwm(eth, 10);
// Should be valid, correct frequency
{
auto s = Sensor::get(SensorType::FuelEthanolPercent);
EXPECT_TRUE(s.Valid);
EXPECT_FLOAT_EQ(s.Value, 10);
}
}

View File

@ -84,5 +84,5 @@ TESTS_SRC_CPP = \
tests/test_gpio.cpp \
tests/test_limp.cpp \
tests/trigger/test_all_triggers.cpp \
tests/sensor/test_flex_sensor.cpp \
tests/sensor/test_frequency_sensor.cpp \