diff --git a/firmware/controllers/math/closed_loop_fuel.cpp b/firmware/controllers/math/closed_loop_fuel.cpp index 4276d0473b..8349036ba4 100644 --- a/firmware/controllers/math/closed_loop_fuel.cpp +++ b/firmware/controllers/math/closed_loop_fuel.cpp @@ -66,13 +66,13 @@ static bool shouldCorrect(DECLARE_ENGINE_PARAMETER_SIGNATURE) { return true; } -static bool shouldUpdateCorrection(DECLARE_ENGINE_PARAMETER_SIGNATURE) { +bool shouldUpdateCorrection(DECLARE_ENGINE_PARAMETER_SIGNATURE) { const auto& cfg = CONFIG(stft); // Pause (but don't reset) correction if the AFR is off scale. // It's probably a transient and poorly tuned transient correction - auto afr = Sensor::get(SensorType::Lambda); - if (!afr || afr.value_or(0) < (cfg.minAfr * 0.1f) || afr.value_or(0) > (cfg.maxAfr * 0.1f)) { + auto afr = Sensor::get(SensorType::Lambda).value_or(0) * 14.7f; + if (!afr || afr < (cfg.minAfr * 0.1f) || afr > (cfg.maxAfr * 0.1f)) { return false; } diff --git a/firmware/controllers/math/closed_loop_fuel.h b/firmware/controllers/math/closed_loop_fuel.h index aff0d2c426..efe894f9fc 100644 --- a/firmware/controllers/math/closed_loop_fuel.h +++ b/firmware/controllers/math/closed_loop_fuel.h @@ -5,3 +5,4 @@ float fuelClosedLoopCorrection(DECLARE_ENGINE_PARAMETER_SIGNATURE); size_t computeStftBin(int rpm, float load, stft_s& cfg); +bool shouldUpdateCorrection(DECLARE_ENGINE_PARAMETER_SIGNATURE); diff --git a/unit_tests/tests/test_stft.cpp b/unit_tests/tests/test_stft.cpp index 0f442bde64..f9237fa0d1 100644 --- a/unit_tests/tests/test_stft.cpp +++ b/unit_tests/tests/test_stft.cpp @@ -1,4 +1,5 @@ +#include "engine_test_helper.h" #include "closed_loop_fuel_cell.h" #include "closed_loop_fuel.h" @@ -79,3 +80,19 @@ TEST(ClosedLoopFuel, CellSelection) { EXPECT_EQ(3, computeStftBin(4000, 50, cfg)); EXPECT_EQ(3, computeStftBin(10000, 50, cfg)); } + +TEST(ClosedLoopFuel, afrLimits) { + WITH_ENGINE_TEST_HELPER(TEST_ENGINE); + + engineConfiguration->stft.minAfr = 100; // 10.0 AFR + engineConfiguration->stft.maxAfr = 180; // 18.0 AFR + + Sensor::setMockValue(SensorType::Lambda, 0.1f); + EXPECT_FALSE(shouldUpdateCorrection(PASS_ENGINE_PARAMETER_SIGNATURE)); + + Sensor::setMockValue(SensorType::Lambda, 1.0f); + EXPECT_TRUE(shouldUpdateCorrection(PASS_ENGINE_PARAMETER_SIGNATURE)); + + Sensor::setMockValue(SensorType::Lambda, 2.0f); + EXPECT_FALSE(shouldUpdateCorrection(PASS_ENGINE_PARAMETER_SIGNATURE)); +}