From fcd6c3d5200dfb39136ff9b0bd9d94df6760b981 Mon Sep 17 00:00:00 2001 From: Matthew Kennedy Date: Sun, 1 Aug 2021 20:21:53 -0700 Subject: [PATCH] fix the mess (#3094) * test * s --- .../sensors/converters/flex_sensor.h | 26 ++++ firmware/init/sensor/init_flex.cpp | 25 +--- unit_tests/tests/sensor/test_flex_sensor.cpp | 132 ------------------ .../tests/sensor/test_frequency_sensor.cpp | 70 ++++++++++ unit_tests/tests/tests.mk | 2 +- 5 files changed, 98 insertions(+), 157 deletions(-) create mode 100644 firmware/controllers/sensors/converters/flex_sensor.h delete mode 100644 unit_tests/tests/sensor/test_flex_sensor.cpp create mode 100644 unit_tests/tests/sensor/test_frequency_sensor.cpp diff --git a/firmware/controllers/sensors/converters/flex_sensor.h b/firmware/controllers/sensors/converters/flex_sensor.h new file mode 100644 index 0000000000..a23655fd24 --- /dev/null +++ b/firmware/controllers/sensors/converters/flex_sensor.h @@ -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; +}; diff --git a/firmware/init/sensor/init_flex.cpp b/firmware/init/sensor/init_flex.cpp index bce06399ce..81ec147747 100644 --- a/firmware/init/sensor/init_flex.cpp +++ b/firmware/init/sensor/init_flex.cpp @@ -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; diff --git a/unit_tests/tests/sensor/test_flex_sensor.cpp b/unit_tests/tests/sensor/test_flex_sensor.cpp deleted file mode 100644 index 795093da5a..0000000000 --- a/unit_tests/tests/sensor/test_flex_sensor.cpp +++ /dev/null @@ -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 ð, 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); - } -} diff --git a/unit_tests/tests/sensor/test_frequency_sensor.cpp b/unit_tests/tests/sensor/test_frequency_sensor.cpp new file mode 100644 index 0000000000..aa9942fc88 --- /dev/null +++ b/unit_tests/tests/sensor/test_frequency_sensor.cpp @@ -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 ð, 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); + } +} diff --git a/unit_tests/tests/tests.mk b/unit_tests/tests/tests.mk index cb6cfbc582..90a5532627 100644 --- a/unit_tests/tests/tests.mk +++ b/unit_tests/tests/tests.mk @@ -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 \