From 9ecb0a4e553974d9c6c53b19904bfe1f4dbfa555 Mon Sep 17 00:00:00 2001 From: Matthew Kennedy Date: Wed, 3 Jun 2020 18:12:12 -0700 Subject: [PATCH] refactor (#1481) --- firmware/console/status_loop.cpp | 2 +- firmware/controllers/algo/fuel_math.cpp | 18 +++++++- firmware/controllers/algo/fuel_math.h | 1 + .../controllers/math/config_engine_specs.h | 40 ------------------ firmware/controllers/math/engine_math.cpp | 1 - firmware/controllers/math/speed_density.cpp | 42 +++++-------------- firmware/controllers/math/speed_density.h | 3 +- unit_tests/tests/test_fuel_map.cpp | 4 +- unit_tests/tests/test_speed_density.cpp | 3 +- 9 files changed, 33 insertions(+), 81 deletions(-) delete mode 100644 firmware/controllers/math/config_engine_specs.h diff --git a/firmware/console/status_loop.cpp b/firmware/console/status_loop.cpp index bc984b6001..1291616611 100644 --- a/firmware/console/status_loop.cpp +++ b/firmware/console/status_loop.cpp @@ -289,7 +289,7 @@ static void showFuelInfo2(float rpm, float engineLoad) { float magicAir = getCylinderAirMass(1, 100, convertCelsiusToKelvin(20) PASS_ENGINE_PARAMETER_SUFFIX); - scheduleMsg(&logger, "SD magic fuel %.2f", sdMath(magicAir, 14.7 PASS_ENGINE_PARAMETER_SUFFIX)); + scheduleMsg(&logger, "SD magic fuel %.2f", getInjectionDurationForAirmass(magicAir, 14.7 PASS_ENGINE_PARAMETER_SUFFIX)); scheduleMsg(&logger, "inj flow %.2fcc/min displacement %.2fL", engineConfiguration->injector.flow, engineConfiguration->specs.displacement); diff --git a/firmware/controllers/algo/fuel_math.cpp b/firmware/controllers/algo/fuel_math.cpp index d73a3c48f0..065c44e3b4 100644 --- a/firmware/controllers/algo/fuel_math.cpp +++ b/firmware/controllers/algo/fuel_math.cpp @@ -169,13 +169,27 @@ float getRealMafFuel(float airSpeed, int rpm DECLARE_ENGINE_PARAMETER_SUFFIX) { //Correct air mass by VE table float corrCylAirmass = cylinderAirmass * veMap.getValue(rpm, airChargeLoad) / 100; - float fuelMassGram = corrCylAirmass / afrMap.getValue(rpm, airSpeed); - float pulseWidthSeconds = fuelMassGram / cc_minute_to_gramm_second(engineConfiguration->injector.flow); + float afr = afrMap.getValue(rpm, airChargeLoad); + float pulseWidthSeconds = getInjectionDurationForAirmass(corrCylAirmass, afr PASS_ENGINE_PARAMETER_SUFFIX); // Convert to ms return 1000 * pulseWidthSeconds; } +constexpr float convertToGramsPerSecond(float ccPerMinute) { + float ccPerSecond = ccPerMinute / 60; + return ccPerSecond * 0.72f; // 0.72g/cc fuel density +} + +/** + * @return per cylinder injection time, in seconds + */ +float getInjectionDurationForAirmass(float airMass, float afr DECLARE_ENGINE_PARAMETER_SUFFIX) { + float gPerSec = convertToGramsPerSecond(CONFIG(injector.flow)); + + return airMass / (afr * gPerSec); +} + /** * per-cylinder fuel amount * todo: rename this method since it's now base+TPSaccel diff --git a/firmware/controllers/algo/fuel_math.h b/firmware/controllers/algo/fuel_math.h index 6fe04eb355..69bf083838 100644 --- a/firmware/controllers/algo/fuel_math.h +++ b/firmware/controllers/algo/fuel_math.h @@ -41,3 +41,4 @@ float getStandardAirCharge(DECLARE_ENGINE_PARAMETER_SIGNATURE); // convert injection duration (Ms/Nt) to fuel rate (L/h) float getFuelRate(floatms_t totalInjDuration, efitick_t timePeriod DECLARE_ENGINE_PARAMETER_SUFFIX); +float getInjectionDurationForAirmass(float airMass, float afr DECLARE_ENGINE_PARAMETER_SUFFIX); diff --git a/firmware/controllers/math/config_engine_specs.h b/firmware/controllers/math/config_engine_specs.h deleted file mode 100644 index 892e0d84d8..0000000000 --- a/firmware/controllers/math/config_engine_specs.h +++ /dev/null @@ -1,40 +0,0 @@ -/* - * @file config_engine_specs.h - * - * @date Jan 28, 2019 - * @author Andrey Belomutskiy, (c) 2012-2020 - */ - -#pragma once - - -#include "global.h" - -/** - * All these scary macro are about three goals: - * 1) performance optimization: real ECU hardware firmware should access configuration via direct global variable access without any pointers - * 2) design enforcement: unit tests should access configuration via pointers, NOT via global variables - * 3) design enforcement: user code should take special considerations in order to access specific group of configuration and not have access to complete configuration by default - */ - -#if EFI_UNIT_TEST -#define DECLARE_GLOBAL_SIGNATURE Engine *engine, engine_configuration_s *___engineConfiguration, persistent_config_s *config -#define DECLARE_GLOBAL_SUFFIX , DECLARE_GLOBAL_SIGNATURE -#define PASS_GLOBAL_SIGNATURE engine, ___engineConfiguration, config -#define PASS_GLOBAL_SUFFIX , PASS_GLOBAL_SIGNATURE -#define CONFIG_ACCESS_FOR_CONFIG_HEADER_ONLY(x) ___engineConfiguration->x -#else /* EFI_UNIT_TEST */ -#define DECLARE_GLOBAL_SIGNATURE void -// Use this version of the macro as the suffix if method has other parameters -#define DECLARE_GLOBAL_SUFFIX -// Pass this if only magic references are needed -#define PASS_GLOBAL_SIGNATURE -// Pass this after some other parameters are passed -#define PASS_GLOBAL_SUFFIX -#define CONFIG_ACCESS_FOR_CONFIG_HEADER_ONLY(x) persistentState.persistentConfiguration.engineConfiguration.x -#endif /* EFI_UNIT_TEST */ - -#define get_specs_displacement CONFIG_ACCESS_FOR_CONFIG_HEADER_ONLY(specs.displacement) -#define get_specs_cylindersCount CONFIG_ACCESS_FOR_CONFIG_HEADER_ONLY(specs.cylindersCount) -#define get_injector_flow CONFIG_ACCESS_FOR_CONFIG_HEADER_ONLY(injector.flow) - diff --git a/firmware/controllers/math/engine_math.cpp b/firmware/controllers/math/engine_math.cpp index 3ff7dba4f9..26d52bc49c 100644 --- a/firmware/controllers/math/engine_math.cpp +++ b/firmware/controllers/math/engine_math.cpp @@ -29,7 +29,6 @@ #include "efi_gpio.h" #include "fuel_math.h" #include "advance_map.h" -#include "config_engine_specs.h" EXTERN_ENGINE; #if EFI_UNIT_TEST diff --git a/firmware/controllers/math/speed_density.cpp b/firmware/controllers/math/speed_density.cpp index 75d221fbdf..50f34b5ad4 100644 --- a/firmware/controllers/math/speed_density.cpp +++ b/firmware/controllers/math/speed_density.cpp @@ -8,12 +8,11 @@ */ #include "global.h" -#include "globalaccess.h" #include "speed_density.h" +#include "fuel_math.h" #include "interpolation.h" #include "engine.h" #include "engine_math.h" -#include "config_engine_specs.h" #include "perf_trace.h" #include "sensor.h" @@ -120,40 +119,19 @@ temperature_t getTCharge(int rpm, float tps DECLARE_ENGINE_PARAMETER_SUFFIX) { /** * @return air mass in grams */ -static float getCycleAirMass(float volumetricEfficiency, float MAP, float tempK DECLARE_GLOBAL_SUFFIX) { - return (get_specs_displacement * volumetricEfficiency * MAP) / (GAS_R * tempK); +static float getCycleAirMass(float volumetricEfficiency, float MAP, float tempK DECLARE_ENGINE_PARAMETER_SUFFIX) { + return (CONFIG(specs.displacement) * volumetricEfficiency * MAP) / (GAS_R * tempK); } -float getCylinderAirMass(float volumetricEfficiency, float MAP, float tempK DECLARE_GLOBAL_SUFFIX) { - return getCycleAirMass(volumetricEfficiency, MAP, tempK PASS_GLOBAL_SUFFIX) - / get_specs_cylindersCount; +float getCylinderAirMass(float volumetricEfficiency, float MAP, float tempK DECLARE_ENGINE_PARAMETER_SUFFIX) { + return getCycleAirMass(volumetricEfficiency, MAP, tempK PASS_ENGINE_PARAMETER_SUFFIX) + / CONFIG(specs.cylindersCount); } -/** - * @return per cylinder injection time, in seconds - */ -float sdMath(float airMass, float AFR DECLARE_GLOBAL_SUFFIX) { - - /** - * todo: pre-calculate gramm/second injector flow to save one multiplication - * open question if that's needed since that's just a multiplication - */ - float injectorFlowRate = cc_minute_to_gramm_second(get_injector_flow); - /** - * injection_pulse_duration = fuel_mass / injector_flow - * fuel_mass = air_mass / target_afr - * - * injection_pulse_duration = (air_mass / target_afr) / injector_flow - */ - return airMass / (AFR * injectorFlowRate); -} - -EXTERN_ENGINE; - /** * @return per cylinder injection time, in Milliseconds */ -floatms_t getSpeedDensityFuel(float map DECLARE_GLOBAL_SUFFIX) { +floatms_t getSpeedDensityFuel(float map DECLARE_ENGINE_PARAMETER_SUFFIX) { ScopePerf perf(PE::GetSpeedDensityFuel); /** @@ -166,12 +144,12 @@ floatms_t getSpeedDensityFuel(float map DECLARE_GLOBAL_SUFFIX) { } efiAssert(CUSTOM_ERR_ASSERT, !cisnan(map), "NaN map", 0); - engine->engineState.sd.manifoldAirPressureAccelerationAdjustment = engine->engineLoadAccelEnrichment.getEngineLoadEnrichment(PASS_GLOBAL_SIGNATURE); + engine->engineState.sd.manifoldAirPressureAccelerationAdjustment = engine->engineLoadAccelEnrichment.getEngineLoadEnrichment(PASS_ENGINE_PARAMETER_SIGNATURE); float adjustedMap = engine->engineState.sd.adjustedManifoldAirPressure = map + engine->engineState.sd.manifoldAirPressureAccelerationAdjustment; efiAssert(CUSTOM_ERR_ASSERT, !cisnan(adjustedMap), "NaN adjustedMap", 0); - float airMass = getCylinderAirMass(ENGINE(engineState.currentBaroCorrectedVE), adjustedMap, tChargeK PASS_GLOBAL_SUFFIX); + float airMass = getCylinderAirMass(ENGINE(engineState.currentBaroCorrectedVE), adjustedMap, tChargeK PASS_ENGINE_PARAMETER_SUFFIX); if (cisnan(airMass)) { warning(CUSTOM_ERR_6685, "NaN airMass"); return 0; @@ -182,7 +160,7 @@ floatms_t getSpeedDensityFuel(float map DECLARE_GLOBAL_SUFFIX) { #endif /*EFI_PRINTF_FUEL_DETAILS */ engine->engineState.sd.airMassInOneCylinder = airMass; - return sdMath(airMass, ENGINE(engineState.targetAFR) PASS_GLOBAL_SUFFIX) * 1000; + return getInjectionDurationForAirmass(airMass, ENGINE(engineState.targetAFR) PASS_ENGINE_PARAMETER_SUFFIX) * 1000; } void setDefaultVETable(DECLARE_ENGINE_PARAMETER_SIGNATURE) { diff --git a/firmware/controllers/math/speed_density.h b/firmware/controllers/math/speed_density.h index 8d581aa2b5..51f21b65e8 100644 --- a/firmware/controllers/math/speed_density.h +++ b/firmware/controllers/math/speed_density.h @@ -7,12 +7,13 @@ #pragma once +#include "engine.h" + #define gramm_second_to_cc_minute(gs) ((gs) / 0.0119997981) #define cc_minute_to_gramm_second(ccm) ((ccm) * 0.0119997981) temperature_t getTCharge(int rpm, float tps DECLARE_ENGINE_PARAMETER_SUFFIX); float getCylinderAirMass(float volumetricEfficiency, float MAP, float tempK DECLARE_ENGINE_PARAMETER_SUFFIX); -float sdMath(float airMass, float AFR DECLARE_ENGINE_PARAMETER_SUFFIX); void setDefaultVETable(DECLARE_ENGINE_PARAMETER_SIGNATURE); void initSpeedDensity(DECLARE_ENGINE_PARAMETER_SIGNATURE); diff --git a/unit_tests/tests/test_fuel_map.cpp b/unit_tests/tests/test_fuel_map.cpp index e602473ecf..9fb293db9a 100644 --- a/unit_tests/tests/test_fuel_map.cpp +++ b/unit_tests/tests/test_fuel_map.cpp @@ -16,7 +16,6 @@ #include "sensor.h" TEST(misc, testMafFuelMath) { - printf("====================================================================================== testMafFuelMath\r\n"); WITH_ENGINE_TEST_HELPER(FORD_ASPIRE_1996); extern fuel_Map3D_t veMap; veMap.setAll(75); @@ -26,8 +25,7 @@ TEST(misc, testMafFuelMath) { setAfrMap(config->afrTable, 13); - float fuelMs = getRealMafFuel(300, 6000 PASS_ENGINE_PARAMETER_SUFFIX); - assertEqualsM("fuelMs", 0.75 * 13.3550, fuelMs); + EXPECT_NEAR(0.75 * 13.3547, getRealMafFuel(300, 6000 PASS_ENGINE_PARAMETER_SUFFIX), EPS4D); } TEST(misc, testFuelMap) { diff --git a/unit_tests/tests/test_speed_density.cpp b/unit_tests/tests/test_speed_density.cpp index d87ad61919..d8e0faf5ee 100644 --- a/unit_tests/tests/test_speed_density.cpp +++ b/unit_tests/tests/test_speed_density.cpp @@ -7,6 +7,7 @@ #include "engine_test_helper.h" #include "speed_density.h" +#include "fuel_math.h" TEST(big, testSpeedDensity) { printf("*************************************************** testSpeedDensity\r\n"); @@ -29,5 +30,5 @@ TEST(big, testSpeedDensity) { ASSERT_FLOAT_EQ(0.9371106624, airMass); // 0.01414 sec or 14.14 ms - ASSERT_FLOAT_EQ(0.014137065038, sdMath(airMass, 12.5 PASS_ENGINE_PARAMETER_SUFFIX)); + EXPECT_NEAR(0.014137, getInjectionDurationForAirmass(airMass, 12.5 PASS_ENGINE_PARAMETER_SUFFIX), EPS4D); }