From 92ea09b0a6dfb98ad91e2c3636f420d2540452f4 Mon Sep 17 00:00:00 2001 From: Matthew Kennedy Date: Sat, 2 Jan 2021 16:13:10 -0800 Subject: [PATCH] switch more map to read from sensor model (#2162) * hook up map * check for init * switch some consumers * that's the rest of them * test * tests Co-authored-by: Matthew Kennedy --- firmware/console/status_loop.cpp | 2 +- .../controllers/algo/airmass/speed_density_airmass.cpp | 9 ++++----- firmware/controllers/algo/fuel/fuel_computer.cpp | 3 ++- firmware/controllers/algo/fuel/injector_model.cpp | 9 +++++++-- firmware/controllers/algo/fuel_math.cpp | 7 +++++-- firmware/controllers/can/obd2.cpp | 2 +- firmware/controllers/core/fsio_impl.cpp | 2 +- firmware/controllers/gauges/lcd_controller.cpp | 2 +- firmware/controllers/limp_manager.cpp | 3 +-- firmware/controllers/math/engine_math.cpp | 2 +- unit_tests/tests/ignition_injection/test_fuelCut.cpp | 2 ++ .../tests/ignition_injection/test_injector_model.cpp | 2 +- unit_tests/tests/test_limp.cpp | 8 ++++---- 13 files changed, 31 insertions(+), 22 deletions(-) diff --git a/firmware/console/status_loop.cpp b/firmware/console/status_loop.cpp index bde1bd4b7e..7d5f400ceb 100644 --- a/firmware/console/status_loop.cpp +++ b/firmware/console/status_loop.cpp @@ -638,7 +638,7 @@ void updateTunerStudioState(TunerStudioOutputChannels *tsOutputChannels DECLARE_ tsOutputChannels->tpsAccelFuel = engine->engineState.tpsAccelEnrich; // engine load acceleration if (hasMapSensor(PASS_ENGINE_PARAMETER_SIGNATURE)) { - tsOutputChannels->engineLoadAccelExtra = engine->engineLoadAccelEnrichment.getEngineLoadEnrichment(PASS_ENGINE_PARAMETER_SIGNATURE) * 100 / getMap(PASS_ENGINE_PARAMETER_SIGNATURE); + tsOutputChannels->engineLoadAccelExtra = engine->engineLoadAccelEnrichment.getEngineLoadEnrichment(PASS_ENGINE_PARAMETER_SIGNATURE) * 100 / Sensor::get(SensorType::Map).value_or(0); } tsOutputChannels->engineLoadDelta = engine->engineLoadAccelEnrichment.getMaxDelta(); diff --git a/firmware/controllers/algo/airmass/speed_density_airmass.cpp b/firmware/controllers/algo/airmass/speed_density_airmass.cpp index 7a2b2b76b7..257890fbd9 100644 --- a/firmware/controllers/algo/airmass/speed_density_airmass.cpp +++ b/firmware/controllers/algo/airmass/speed_density_airmass.cpp @@ -1,7 +1,6 @@ #include "global.h" #include "engine.h" #include "speed_density_airmass.h" -#include "map.h" #include "perf_trace.h" EXTERN_ENGINE; @@ -18,15 +17,15 @@ AirmassResult SpeedDensityAirmass::getAirmass(int rpm) { return {}; } - float map = getMap(PASS_ENGINE_PARAMETER_SIGNATURE); - if (cisnan(map)) { + auto map = Sensor::get(SensorType::Map); + if (!map) { warning(CUSTOM_ERR_TCHARGE_NOT_READY2, "map not ready"); // this could happen during HW CI during configuration reset return {}; } engine->engineState.sd.manifoldAirPressureAccelerationAdjustment = engine->engineLoadAccelEnrichment.getEngineLoadEnrichment(PASS_ENGINE_PARAMETER_SIGNATURE); - float adjustedMap = engine->engineState.sd.adjustedManifoldAirPressure = map + engine->engineState.sd.manifoldAirPressureAccelerationAdjustment; + float adjustedMap = engine->engineState.sd.adjustedManifoldAirPressure = map.Value + engine->engineState.sd.manifoldAirPressureAccelerationAdjustment; efiAssert(CUSTOM_ERR_ASSERT, !cisnan(adjustedMap), "NaN adjustedMap", {}); float ve = getVe(rpm, adjustedMap); @@ -43,6 +42,6 @@ AirmassResult SpeedDensityAirmass::getAirmass(int rpm) { return { airMass, - map, // AFR/VE table Y axis + map.Value, // AFR/VE table Y axis }; } diff --git a/firmware/controllers/algo/fuel/fuel_computer.cpp b/firmware/controllers/algo/fuel/fuel_computer.cpp index 061263faf5..327c2f9f0b 100644 --- a/firmware/controllers/algo/fuel/fuel_computer.cpp +++ b/firmware/controllers/algo/fuel/fuel_computer.cpp @@ -45,7 +45,8 @@ float FuelComputer::getTargetLambdaLoadAxis(float defaultLoad) const { float getLoadOverride(float defaultLoad, afr_override_e overrideMode DECLARE_ENGINE_PARAMETER_SUFFIX) { switch(overrideMode) { case AFR_None: return defaultLoad; - case AFR_MAP: return getMap(PASS_ENGINE_PARAMETER_SIGNATURE); + // MAP default to 200kpa - failed MAP goes rich + case AFR_MAP: return Sensor::get(SensorType::Map).value_or(200); // TPS/pedal default to 100% - failed TPS goes rich case AFR_Tps: return Sensor::get(SensorType::Tps1).value_or(100); case AFR_AccPedal: return Sensor::get(SensorType::AcceleratorPedal).value_or(100); diff --git a/firmware/controllers/algo/fuel/injector_model.cpp b/firmware/controllers/algo/fuel/injector_model.cpp index e62e49b379..89407402ab 100644 --- a/firmware/controllers/algo/fuel/injector_model.cpp +++ b/firmware/controllers/algo/fuel/injector_model.cpp @@ -43,10 +43,15 @@ float InjectorModel::getInjectorFlowRatio() const { return 1.0f; } - float map = getMap(PASS_ENGINE_PARAMETER_SIGNATURE); + auto map = Sensor::get(SensorType::Map); + + // Map has failed, assume nominal pressure + if (!map) { + return 1.0f; + } // TODO: what to do when pressureDelta is less than 0? - float pressureDelta = absRailPressure.Value - map; + float pressureDelta = absRailPressure.Value - map.Value; float pressureRatio = pressureDelta / referencePressure; float flowRatio = sqrtf(pressureRatio); diff --git a/firmware/controllers/algo/fuel_math.cpp b/firmware/controllers/algo/fuel_math.cpp index 7e3815cf12..9fa34bc00b 100644 --- a/firmware/controllers/algo/fuel_math.cpp +++ b/firmware/controllers/algo/fuel_math.cpp @@ -406,8 +406,11 @@ float getFuelCutOffCorrection(efitick_t nowNt, int rpm DECLARE_ENGINE_PARAMETER_ return 1.0f; } - float map = getMap(PASS_ENGINE_PARAMETER_SIGNATURE); - + const auto [mapValid, map] = Sensor::get(SensorType::Map); + if (!mapValid) { + return 1.0f; + } + // gather events bool mapDeactivate = (map >= CONFIG(coastingFuelCutMap)); bool tpsDeactivate = (tpsPos >= CONFIG(coastingFuelCutTps)); diff --git a/firmware/controllers/can/obd2.cpp b/firmware/controllers/can/obd2.cpp index 9ee3789378..d1e39da6bb 100644 --- a/firmware/controllers/can/obd2.cpp +++ b/firmware/controllers/can/obd2.cpp @@ -133,7 +133,7 @@ static void handleGetDataRequest(const CANRxFrame& rx) { obdSendValue(_1_MODE, pid, 1, 128 * ENGINE(engineState.running.pidCorrection)); break; case PID_INTAKE_MAP: - obdSendValue(_1_MODE, pid, 1, getMap(PASS_ENGINE_PARAMETER_SIGNATURE)); + obdSendValue(_1_MODE, pid, 1, Sensor::get(SensorType::Map).value_or(0)); break; case PID_RPM: obdSendValue(_1_MODE, pid, 2, GET_RPM() * ODB_RPM_MULT); // rotation/min. (A*256+B)/4 diff --git a/firmware/controllers/core/fsio_impl.cpp b/firmware/controllers/core/fsio_impl.cpp index c5e4887f42..4005719a92 100644 --- a/firmware/controllers/core/fsio_impl.cpp +++ b/firmware/controllers/core/fsio_impl.cpp @@ -134,7 +134,7 @@ FsioResult getEngineValue(le_action_e action DECLARE_ENGINE_PARAMETER_SUFFIX) { case LE_METHOD_MAF: return getRealMaf(PASS_ENGINE_PARAMETER_SIGNATURE); case LE_METHOD_MAP: - return getMap(PASS_ENGINE_PARAMETER_SIGNATURE); + return Sensor::get(SensorType::Map).value_or(0); #if EFI_SHAFT_POSITION_INPUT case LE_METHOD_INTAKE_VVT: case LE_METHOD_EXHAUST_VVT: diff --git a/firmware/controllers/gauges/lcd_controller.cpp b/firmware/controllers/gauges/lcd_controller.cpp index 73bc72a9d3..e2ccc5b40a 100644 --- a/firmware/controllers/gauges/lcd_controller.cpp +++ b/firmware/controllers/gauges/lcd_controller.cpp @@ -228,7 +228,7 @@ static void showLine(lcd_line_e line, int /*screenY*/) { return; case LL_MAP: if (hasMapSensor(PASS_ENGINE_PARAMETER_SIGNATURE)) { - lcdPrintf("MAP %.2f", getMap(PASS_ENGINE_PARAMETER_SIGNATURE)); + lcdPrintf("MAP %.2f", Sensor::get(SensorType::Map).value_or(0)); } else { lcdPrintf("MAP: none"); } diff --git a/firmware/controllers/limp_manager.cpp b/firmware/controllers/limp_manager.cpp index 606359b178..069e6592ea 100644 --- a/firmware/controllers/limp_manager.cpp +++ b/firmware/controllers/limp_manager.cpp @@ -1,6 +1,5 @@ #include "limp_manager.h" #include "engine.h" -#include "map.h" #include "efilib.h" EXTERN_ENGINE; @@ -20,7 +19,7 @@ void LimpManager::updateState(int rpm) { // Limit fuel only on boost pressure (limiting spark bends valves) if (CONFIG(boostCutPressure) != 0) { - if (getMap(PASS_ENGINE_PARAMETER_SIGNATURE) > CONFIG(boostCutPressure)) { + if (Sensor::get(SensorType::Map).value_or(0) > CONFIG(boostCutPressure)) { limitFuel = true; } } diff --git a/firmware/controllers/math/engine_math.cpp b/firmware/controllers/math/engine_math.cpp index 2b81df57a5..7be0ef4740 100644 --- a/firmware/controllers/math/engine_math.cpp +++ b/firmware/controllers/math/engine_math.cpp @@ -66,7 +66,7 @@ float getEngineLoadT(DECLARE_ENGINE_PARAMETER_SIGNATURE) { efiAssert(CUSTOM_ERR_ASSERT, engineConfiguration!=NULL, "engineConfiguration 2NULL", NAN); switch (engineConfiguration->fuelAlgorithm) { case LM_SPEED_DENSITY: - return getMap(PASS_ENGINE_PARAMETER_SIGNATURE); + return Sensor::get(SensorType::Map).value_or(0); case LM_ALPHA_N: return Sensor::get(SensorType::Tps1).value_or(0); case LM_REAL_MAF: diff --git a/unit_tests/tests/ignition_injection/test_fuelCut.cpp b/unit_tests/tests/ignition_injection/test_fuelCut.cpp index 47396bcdd7..05a7397ce9 100644 --- a/unit_tests/tests/ignition_injection/test_fuelCut.cpp +++ b/unit_tests/tests/ignition_injection/test_fuelCut.cpp @@ -31,6 +31,8 @@ TEST(fuelCut, coasting) { // basic engine setup setupSimpleTestEngineWithMafAndTT_ONE_trigger(ð); + Sensor::setMockValue(SensorType::Map, 0); + // mock CLT - just above threshold ('hot engine') float hotClt = engineConfiguration->coastingFuelCutClt + 1; Sensor::setMockValue(SensorType::Clt, hotClt); diff --git a/unit_tests/tests/ignition_injection/test_injector_model.cpp b/unit_tests/tests/ignition_injection/test_injector_model.cpp index ed3481eb9e..45030d3012 100644 --- a/unit_tests/tests/ignition_injection/test_injector_model.cpp +++ b/unit_tests/tests/ignition_injection/test_injector_model.cpp @@ -109,7 +109,7 @@ TEST_P(FlowRateFixture, PressureRatio) { engineConfiguration->fuelReferencePressure = 400.0f; // MAP sensor always reads 35 kpa - engine->mockMapValue = fakeMap; + Sensor::setMockValue(SensorType::Map, fakeMap); // Should return the expected ratio EXPECT_FLOAT_EQ(expectedFlowRatio, dut.getInjectorFlowRatio()); diff --git a/unit_tests/tests/test_limp.cpp b/unit_tests/tests/test_limp.cpp index 128b418e5d..9b504a0d46 100644 --- a/unit_tests/tests/test_limp.cpp +++ b/unit_tests/tests/test_limp.cpp @@ -54,23 +54,23 @@ TEST(limp, boostCut) { INJECT_ENGINE_REFERENCE(&dut); // Below threshold, injection allowed - engine->mockMapValue = 80; + Sensor::setMockValue(SensorType::Map, 80); dut.updateState(1000); EXPECT_TRUE(dut.allowInjection()); // Above threshold, injection cut - engine->mockMapValue = 120; + Sensor::setMockValue(SensorType::Map, 120); dut.updateState(1000); EXPECT_FALSE(dut.allowInjection()); // Below threshold, should recover - engine->mockMapValue = 80; + Sensor::setMockValue(SensorType::Map, 80); dut.updateState(1000); EXPECT_TRUE(dut.allowInjection()); // SPECIAL CASE: threshold of 0 means never boost cut engineConfiguration->boostCutPressure = 0; - engine->mockMapValue = 500; + Sensor::setMockValue(SensorType::Map, 500); dut.updateState(1000); EXPECT_TRUE(dut.allowInjection()); }