From 2b5b669807ecc7429bf9ce2c3e6de5cf0ce2b5ee Mon Sep 17 00:00:00 2001 From: Matthew Kennedy Date: Sun, 1 Aug 2021 12:58:39 -0700 Subject: [PATCH] refactor out frequency sensor (#3092) Co-authored-by: Matthew Kennedy --- firmware/controllers/sensors/flex_sensor.cpp | 42 ------------------- firmware/controllers/sensors/flex_sensor.h | 20 --------- .../controllers/sensors/frequency_sensor.cpp | 26 ++++++++++++ .../controllers/sensors/frequency_sensor.h | 15 +++++++ .../controllers/sensors/functional_sensor.h | 2 +- firmware/controllers/sensors/sensors.mk | 2 +- firmware/init/sensor/init_flex.cpp | 30 ++++++++++++- 7 files changed, 71 insertions(+), 66 deletions(-) delete mode 100644 firmware/controllers/sensors/flex_sensor.cpp delete mode 100644 firmware/controllers/sensors/flex_sensor.h create mode 100644 firmware/controllers/sensors/frequency_sensor.cpp create mode 100644 firmware/controllers/sensors/frequency_sensor.h diff --git a/firmware/controllers/sensors/flex_sensor.cpp b/firmware/controllers/sensors/flex_sensor.cpp deleted file mode 100644 index 74e02788f6..0000000000 --- a/firmware/controllers/sensors/flex_sensor.cpp +++ /dev/null @@ -1,42 +0,0 @@ -#include "pch.h" - -#include "digital_input_exti.h" -#include "flex_sensor.h" - -static void flexExtiCallback(void* arg) { - auto inst = reinterpret_cast(arg); - inst->onEdge(getTimeNowNt()); -} - -void FlexFuelSensor::init(brain_pin_e pin) { -#if EFI_PROD_CODE - // todo: refactor https://github.com/rusefi/rusefi/issues/2123 - efiExtiEnablePin("flex", pin, - PAL_EVENT_MODE_FALLING_EDGE, - flexExtiCallback, reinterpret_cast(this)); -#endif // EFI_PROD_CODE - - // 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); -} - -FlexFuelSensor::FlexFuelSensor() - // this long timeout is ok, ethanol content isn't going to change quickly - : StoredValueSensor(SensorType::FuelEthanolPercent, MS2NT(500)) -{ -} - -void FlexFuelSensor::onEdge(efitick_t nowNt) { - float frequency = 1 / m_edgeTimer.getElapsedSecondsAndReset(nowNt); - - // 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); - - setValidValue(m_filter.filter(flexPct), nowNt); - } - - // no explicit invalidation, we can tolerate a somewhat dodgy connection - // so long as we get a valid interval every now and then -} diff --git a/firmware/controllers/sensors/flex_sensor.h b/firmware/controllers/sensors/flex_sensor.h deleted file mode 100644 index 2bc244b650..0000000000 --- a/firmware/controllers/sensors/flex_sensor.h +++ /dev/null @@ -1,20 +0,0 @@ -#include "stored_value_sensor.h" -#include "timer.h" -#include "rusefi_hw_enums.h" -#include "biquad.h" - -class FlexFuelSensor : public StoredValueSensor { -public: - FlexFuelSensor(); - - void init(brain_pin_e pin); - - void onEdge(efitick_t nowNt); - - void showInfo(const char* /*sensorName*/) const override { } - -private: - Timer m_edgeTimer; - - Biquad m_filter; -}; diff --git a/firmware/controllers/sensors/frequency_sensor.cpp b/firmware/controllers/sensors/frequency_sensor.cpp new file mode 100644 index 0000000000..dbb0369a86 --- /dev/null +++ b/firmware/controllers/sensors/frequency_sensor.cpp @@ -0,0 +1,26 @@ +#include "pch.h" + +#include "frequency_sensor.h" + +#include "digital_input_exti.h" + +// Callback adapter since we can't pass a member function to a callback +static void freqSensorExtiCallback(void* arg) { + auto inst = reinterpret_cast(arg); + inst->onEdge(getTimeNowNt()); +} + +void FrequencySensor::init(brain_pin_e pin) { +#if EFI_PROD_CODE + // todo: refactor https://github.com/rusefi/rusefi/issues/2123 + efiExtiEnablePin("flex", pin, + PAL_EVENT_MODE_FALLING_EDGE, + freqSensorExtiCallback, reinterpret_cast(this)); +#endif // EFI_PROD_CODE +} + +void FrequencySensor::onEdge(efitick_t nowNt) { + float frequency = 1 / m_edgeTimer.getElapsedSecondsAndReset(nowNt); + + postRawValue(frequency, nowNt); +} diff --git a/firmware/controllers/sensors/frequency_sensor.h b/firmware/controllers/sensors/frequency_sensor.h new file mode 100644 index 0000000000..30021baee4 --- /dev/null +++ b/firmware/controllers/sensors/frequency_sensor.h @@ -0,0 +1,15 @@ +#include "functional_sensor.h" +#include "timer.h" + +class FrequencySensor : public FunctionalSensor { +public: + FrequencySensor(SensorType type, efitick_t timeoutPeriod) + : FunctionalSensor(type, timeoutPeriod) { } + + void init(brain_pin_e pin); + + void onEdge(efitick_t nowNt); + +private: + Timer m_edgeTimer; +}; diff --git a/firmware/controllers/sensors/functional_sensor.h b/firmware/controllers/sensors/functional_sensor.h index 14606944ba..aaef8b1c32 100644 --- a/firmware/controllers/sensors/functional_sensor.h +++ b/firmware/controllers/sensors/functional_sensor.h @@ -23,7 +23,7 @@ * Register an instance of the new class with an interface * that provides and posts raw values so the sensor can update. */ -class FunctionalSensor final : public StoredValueSensor { +class FunctionalSensor : public StoredValueSensor { public: FunctionalSensor(SensorType type, efitick_t timeoutPeriod) : StoredValueSensor(type, timeoutPeriod) { } diff --git a/firmware/controllers/sensors/sensors.mk b/firmware/controllers/sensors/sensors.mk index 524c22dec7..5209cf922d 100644 --- a/firmware/controllers/sensors/sensors.mk +++ b/firmware/controllers/sensors/sensors.mk @@ -13,7 +13,7 @@ CONTROLLERS_SENSORS_SRC_CPP = $(PROJECT_DIR)/controllers/sensors/thermistors.cp $(PROJECT_DIR)/controllers/sensors/redundant_sensor.cpp \ $(PROJECT_DIR)/controllers/sensors/redundant_ford_tps.cpp \ $(PROJECT_DIR)/controllers/sensors/AemXSeriesLambda.cpp \ - $(PROJECT_DIR)/cotnrollers/sensors/flex_sensor.cpp \ + $(PROJECT_DIR)/cotnrollers/sensors/frequency_sensor.cpp \ $(PROJECT_DIR)/controllers/sensors/software_knock.cpp \ $(PROJECT_DIR)/controllers/sensors/Lps25Sensor.cpp \ $(PROJECT_DIR)/controllers/sensors/converters/linear_func.cpp \ diff --git a/firmware/init/sensor/init_flex.cpp b/firmware/init/sensor/init_flex.cpp index d5362cf8d6..bce06399ce 100644 --- a/firmware/init/sensor/init_flex.cpp +++ b/firmware/init/sensor/init_flex.cpp @@ -1,9 +1,34 @@ #include "init.h" #include "pin_repository.h" #include "engine.h" -#include "flex_sensor.h" +#include "frequency_sensor.h" +#include "biquad.h" -static FlexFuelSensor flexSensor; +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; +}; + +static FrequencySensor flexSensor(SensorType::FuelEthanolPercent, MS2NT(500)); +static FlexConverter converter; // https://rusefi.com/forum/viewtopic.php?p=37452&sid=829804c90d5b2e1fecd1b900cf1b1811#p37452 @@ -15,6 +40,7 @@ void initFlexSensor(DECLARE_CONFIG_PARAMETER_SIGNATURE) { return; } + flexSensor.setFunction(converter); flexSensor.init(pin); flexSensor.Register(); }