From 8c6253f1987fd8e4bfcd9c0bfc07a38c629fc0da Mon Sep 17 00:00:00 2001 From: rusefi Date: Wed, 12 Apr 2017 23:25:58 -0400 Subject: [PATCH] better constraint validation & refacoting --- firmware/console/status_loop.cpp | 2 +- firmware/controllers/algo/engine.cpp | 3 +-- firmware/controllers/algo/fuel_math.cpp | 6 ++++-- firmware/controllers/algo/fuel_math.h | 2 +- firmware/controllers/math/speed_density.cpp | 9 +++++++-- unit_tests/test_fuel_map.cpp | 6 +++--- 6 files changed, 17 insertions(+), 11 deletions(-) diff --git a/firmware/console/status_loop.cpp b/firmware/console/status_loop.cpp index 3e3bf56312..9554649e32 100644 --- a/firmware/console/status_loop.cpp +++ b/firmware/console/status_loop.cpp @@ -494,7 +494,7 @@ void updateDevConsoleState(void) { static void showFuelInfo2(float rpm, float engineLoad) { - float baseFuelMs = getBaseTableFuel(engineConfiguration, (int) rpm, engineLoad); + float baseFuelMs = getBaseTableFuel((int) rpm, engineLoad); float magicAir = getAirMass(engineConfiguration, 1, 100, convertCelsiusToKelvin(20)); diff --git a/firmware/controllers/algo/engine.cpp b/firmware/controllers/algo/engine.cpp index 94700e14b9..329107ea6c 100644 --- a/firmware/controllers/algo/engine.cpp +++ b/firmware/controllers/algo/engine.cpp @@ -230,9 +230,8 @@ void EngineState::periodicFastCallback(DECLARE_ENGINE_PARAMETER_F) { currentVE = baroCorrection * veMap.getValue(rpm, map) * 0.01; targetAFR = afrMap.getValue(rpm, map); } else { - baseTableFuel = getBaseTableFuel(engineConfiguration, rpm, engineLoad); + baseTableFuel = getBaseTableFuel(rpm, engineLoad); } - } diff --git a/firmware/controllers/algo/fuel_math.cpp b/firmware/controllers/algo/fuel_math.cpp index 676c9765a1..1639715355 100644 --- a/firmware/controllers/algo/fuel_math.cpp +++ b/firmware/controllers/algo/fuel_math.cpp @@ -79,13 +79,15 @@ floatms_t getBaseFuel(int rpm DECLARE_ENGINE_PARAMETER_S) { floatms_t baseFuel; if (CONFIG(fuelAlgorithm) == LM_SPEED_DENSITY) { baseFuel = getSpeedDensityFuel(PASS_ENGINE_PARAMETER_F); + efiAssert(!cisnan(baseFuel), "NaN sd baseFuel", 0); } else if (engineConfiguration->fuelAlgorithm == LM_REAL_MAF) { float maf = getRealMaf(PASS_ENGINE_PARAMETER_F) + engine->engineLoadAccelEnrichment.getEngineLoadEnrichment(PASS_ENGINE_PARAMETER_F); baseFuel = getRealMafFuel(maf, rpm PASS_ENGINE_PARAMETER); + efiAssert(!cisnan(baseFuel), "NaN rm baseFuel", 0); } else { baseFuel = engine->engineState.baseTableFuel; + efiAssert(!cisnan(baseFuel), "NaN bt baseFuel", 0); } - efiAssert(!cisnan(baseFuel), "NaN baseFuel", 0); engine->engineState.baseFuel = baseFuel; return tpsAccelEnrich + baseFuel; @@ -215,7 +217,7 @@ float getIatFuelCorrection(float iat DECLARE_ENGINE_PARAMETER_S) { /** * @return Fuel injection duration injection as specified in the fuel map, in milliseconds */ -floatms_t getBaseTableFuel(engine_configuration_s *engineConfiguration, int rpm, float engineLoad) { +floatms_t getBaseTableFuel(int rpm, float engineLoad) { if (cisnan(engineLoad)) { warning(CUSTOM_NAN_ENGINE_LOAD_2, "NaN engine load"); return NAN; diff --git a/firmware/controllers/algo/fuel_math.h b/firmware/controllers/algo/fuel_math.h index 03673536b4..6de6894e71 100644 --- a/firmware/controllers/algo/fuel_math.h +++ b/firmware/controllers/algo/fuel_math.h @@ -24,7 +24,7 @@ floatms_t getRunningFuel(floatms_t baseFuel DECLARE_ENGINE_PARAMETER_S); floatms_t getRealMafFuel(float airMass, int rpm DECLARE_ENGINE_PARAMETER_S); -floatms_t getBaseTableFuel(engine_configuration_s *engineConfiguration, int rpm, float engineLoad); +floatms_t getBaseTableFuel(int rpm, float engineLoad); float getBaroCorrection(DECLARE_ENGINE_PARAMETER_F); int getNumberOfInjections(injection_mode_e mode DECLARE_ENGINE_PARAMETER_S); angle_t getinjectionOffset(float rpm DECLARE_ENGINE_PARAMETER_S); diff --git a/firmware/controllers/math/speed_density.cpp b/firmware/controllers/math/speed_density.cpp index 8ff01856f3..928b3cee09 100644 --- a/firmware/controllers/math/speed_density.cpp +++ b/firmware/controllers/math/speed_density.cpp @@ -80,17 +80,22 @@ floatms_t getSpeedDensityFuel(DECLARE_ENGINE_PARAMETER_F) { * most of the values are pre-calculated for performance reasons */ float tChargeK = ENGINE(engineState.tChargeK); + efiAssert(!cisnan(tChargeK), "NaN tChargeK", 0); float map = getMap(); + efiAssert(!cisnan(map), "NaN map", 0); float adjustedMap = map + engine->engineLoadAccelEnrichment.getEngineLoadEnrichment(PASS_ENGINE_PARAMETER_F); + efiAssert(!cisnan(adjustedMap), "NaN adjustedMap", 0); - engine->engineState.airMass = getAirMass(engineConfiguration, ENGINE(engineState.currentVE), adjustedMap, tChargeK); + float airMass = getAirMass(engineConfiguration, ENGINE(engineState.currentVE), adjustedMap, tChargeK); + efiAssert(!cisnan(airMass), "NaN airMass", 0); #if EFI_PRINTF_FUEL_DETAILS || defined(__DOXYGEN__) printf("map=%f adjustedMap=%f airMass=%f\t\n", map, adjustedMap, engine->engineState.airMass); #endif /*EFI_PRINTF_FUEL_DETAILS */ - return sdMath(engineConfiguration, engine->engineState.airMass, ENGINE(engineState.targetAFR)) * 1000; + engine->engineState.airMass = airMass; + return sdMath(engineConfiguration, airMass, ENGINE(engineState.targetAFR)) * 1000; } static const baro_corr_table_t default_baro_corr = { diff --git a/unit_tests/test_fuel_map.cpp b/unit_tests/test_fuel_map.cpp index 62c83343f7..7b45a46715 100644 --- a/unit_tests/test_fuel_map.cpp +++ b/unit_tests/test_fuel_map.cpp @@ -51,7 +51,7 @@ void testFuelMap(void) { for (int i = 0; i < FUEL_RPM_COUNT; i++) eth.engine.config->fuelRpmBins[i] = i; - assertEqualsM("base fuel table", 1005, getBaseTableFuel(eth.engine.engineConfiguration, 5, 5)); + assertEqualsM("base fuel table", 1005, getBaseTableFuel(5, 5)); printf("*************************************************** initThermistors\r\n"); @@ -70,7 +70,7 @@ void testFuelMap(void) { // because all the correction tables are zero printf("*************************************************** getRunningFuel 1\r\n"); eth.engine.periodicFastCallback(PASS_ENGINE_PARAMETER_F); - float baseFuel = getBaseTableFuel(eth.engine.engineConfiguration, 5, getEngineLoadT(PASS_ENGINE_PARAMETER_F)); + float baseFuel = getBaseTableFuel(5, getEngineLoadT(PASS_ENGINE_PARAMETER_F)); assertEqualsM("base fuel", 5.05, getRunningFuel(baseFuel PASS_ENGINE_PARAMETER)); printf("*************************************************** setting IAT table\r\n"); @@ -102,7 +102,7 @@ void testFuelMap(void) { // 1005 * 2 for IAT correction printf("*************************************************** getRunningFuel 2\r\n"); eth.engine.periodicFastCallback(PASS_ENGINE_PARAMETER_F); - baseFuel = getBaseTableFuel(eth.engine.engineConfiguration, 5, getEngineLoadT(PASS_ENGINE_PARAMETER_F)); + baseFuel = getBaseTableFuel(5, getEngineLoadT(PASS_ENGINE_PARAMETER_F)); assertEqualsM("v1", 30150, getRunningFuel(baseFuel PASS_ENGINE_PARAMETER)); testMafValue = 0;