refactor out frequency sensor (#3092)
Co-authored-by: Matthew Kennedy <makenne@microsoft.com>
This commit is contained in:
parent
e0eff6c66a
commit
263fd20dcb
|
@ -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<FlexFuelSensor*>(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<void*>(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
|
||||
}
|
|
@ -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;
|
||||
};
|
|
@ -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<FrequencySensor*>(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<void*>(this));
|
||||
#endif // EFI_PROD_CODE
|
||||
}
|
||||
|
||||
void FrequencySensor::onEdge(efitick_t nowNt) {
|
||||
float frequency = 1 / m_edgeTimer.getElapsedSecondsAndReset(nowNt);
|
||||
|
||||
postRawValue(frequency, nowNt);
|
||||
}
|
|
@ -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;
|
||||
};
|
|
@ -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) { }
|
||||
|
|
|
@ -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 \
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue