diff --git a/firmware/controllers/sensors/frequency_sensor.cpp b/firmware/controllers/sensors/frequency_sensor.cpp index c9baf3f2ca..ede8ce6402 100644 --- a/firmware/controllers/sensors/frequency_sensor.cpp +++ b/firmware/controllers/sensors/frequency_sensor.cpp @@ -39,5 +39,7 @@ void FrequencySensor::deInit() { void FrequencySensor::onEdge(efitick_t nowNt) { float frequency = 1 / m_edgeTimer.getElapsedSecondsAndReset(nowNt); + frequency = m_filter.filter(frequency); + postRawValue(frequency, nowNt); } diff --git a/firmware/controllers/sensors/frequency_sensor.h b/firmware/controllers/sensors/frequency_sensor.h index 3179778ddd..ef984efeee 100644 --- a/firmware/controllers/sensors/frequency_sensor.h +++ b/firmware/controllers/sensors/frequency_sensor.h @@ -1,10 +1,14 @@ #include "functional_sensor.h" #include "timer.h" +#include "biquad.h" class FrequencySensor : public FunctionalSensor { public: - FrequencySensor(SensorType type, efitick_t timeoutPeriod) - : FunctionalSensor(type, timeoutPeriod) { } + FrequencySensor(SensorType type, efitick_t timeoutPeriod, float filterParameter) + : FunctionalSensor(type, timeoutPeriod) + { + m_filter.configureLowpass(1, filterParameter); + } void init(brain_pin_e pin); void deInit(); @@ -14,4 +18,6 @@ public: private: Timer m_edgeTimer; brain_pin_e m_pin = GPIO_UNASSIGNED; + + Biquad m_filter; }; diff --git a/firmware/init/sensor/init_flex.cpp b/firmware/init/sensor/init_flex.cpp index c54f674eef..3953509e07 100644 --- a/firmware/init/sensor/init_flex.cpp +++ b/firmware/init/sensor/init_flex.cpp @@ -4,7 +4,8 @@ #include "frequency_sensor.h" #include "flex_sensor.h" -static FrequencySensor flexSensor(SensorType::FuelEthanolPercent, MS2NT(500)); +// 0.01 means filter bandwidth of ~1hz with ~100hz sensor +static FrequencySensor flexSensor(SensorType::FuelEthanolPercent, MS2NT(500), 0.01f); static FlexConverter converter; // https://rusefi.com/forum/viewtopic.php?p=37452&sid=829804c90d5b2e1fecd1b900cf1b1811#p37452 diff --git a/firmware/init/sensor/init_turbocharger_speed_sensor.cpp b/firmware/init/sensor/init_turbocharger_speed_sensor.cpp index 1e1fbd86de..7fc3c15ed1 100644 --- a/firmware/init/sensor/init_turbocharger_speed_sensor.cpp +++ b/firmware/init/sensor/init_turbocharger_speed_sensor.cpp @@ -4,7 +4,8 @@ #include "frequency_sensor.h" #include "turbocharger_speed_converter.h" -static FrequencySensor turbochargerSpeedSensor(SensorType::TurbochargerSpeed, MS2NT(500)); +// Filter parameter of 0.01 filters over roughly 100 teeth +static FrequencySensor turbochargerSpeedSensor(SensorType::TurbochargerSpeed, MS2NT(500), 0.01f); static TurbochargerSpeedConverter turbochargerSpeedConverter; diff --git a/firmware/init/sensor/init_vehicle_speed_sensor.cpp b/firmware/init/sensor/init_vehicle_speed_sensor.cpp index 25e48c2784..2073b12ba6 100644 --- a/firmware/init/sensor/init_vehicle_speed_sensor.cpp +++ b/firmware/init/sensor/init_vehicle_speed_sensor.cpp @@ -4,7 +4,8 @@ #include "frequency_sensor.h" #include "vehicle_speed_converter.h" -static FrequencySensor vehicleSpeedSensor(SensorType::VehicleSpeed, MS2NT(500)); +// 0.05 filter parameter means averaging over ~20 sensor teeth +static FrequencySensor vehicleSpeedSensor(SensorType::VehicleSpeed, MS2NT(500), 0.05f); static VehicleSpeedConverter vehicleSpeedConverter; void initVehicleSpeedSensor() { diff --git a/unit_tests/tests/sensor/test_frequency_sensor.cpp b/unit_tests/tests/sensor/test_frequency_sensor.cpp index aea2442f1a..00e9e4eb92 100644 --- a/unit_tests/tests/sensor/test_frequency_sensor.cpp +++ b/unit_tests/tests/sensor/test_frequency_sensor.cpp @@ -15,7 +15,7 @@ static IdentityFunction identityFunc; class FrequencySensorTest : public ::testing::Test { public: FrequencySensorTest() - : dut(SensorType::FuelEthanolPercent, MS2NT(50)) + : dut(SensorType::FuelEthanolPercent, MS2NT(50), 0.5f) { } @@ -32,7 +32,7 @@ public: * (as Sensor works by falling edge) */ void generatePwm(EngineTestHelper ð, float freqHz) { - constexpr auto periods = 50; + constexpr auto periods = 1000; auto period = (1 / freqHz); std::cout << "PERIOD: " << period << std::endl; @@ -65,6 +65,6 @@ TEST_F(FrequencySensorTest, testValidWithPwm) { { auto s = Sensor::get(SensorType::FuelEthanolPercent); EXPECT_TRUE(s.Valid); - EXPECT_FLOAT_EQ(s.Value, 10); + EXPECT_NEAR(s.Value, 10, 1e-3); } }