diff --git a/firmware/console/status_loop.cpp b/firmware/console/status_loop.cpp index 33ddc71747..b8389d0ecb 100644 --- a/firmware/console/status_loop.cpp +++ b/firmware/console/status_loop.cpp @@ -241,7 +241,7 @@ static void printState(void) { // debugFloat(&logger, "table_spark", getAdvance(rpm, getMaf()), 2); - float engineLoad = getEngineLoadT(PASS_ENGINE_PARAMETER); + float engineLoad = getEngineLoadT(PASS_ENGINE_PARAMETER_F); float baseFuel = getBaseFuel(rpm PASS_ENGINE_PARAMETER); debugFloat(&logger, "fuel_base", baseFuel, 2); // debugFloat(&logger, "fuel_iat", getIatCorrection(getIntakeAirTemperature()), 2); @@ -249,7 +249,7 @@ static void printState(void) { debugFloat(&logger, "fuel_lag", engine->injectorLagMs, 2); debugFloat(&logger, "fuel", getFuelMs(rpm PASS_ENGINE_PARAMETER), 2); - debugFloat(&logger, "timing", getAdvance(rpm, engineLoad PASS_ENGINE_PARAMETER), 2); + debugFloat(&logger, "timing", engine->engineState.timingAdvance, 2); // float map = getMap(); @@ -412,7 +412,7 @@ static void showFuelInfo2(float rpm, float engineLoad) { #if EFI_ENGINE_CONTROL static void showFuelInfo(void) { - showFuelInfo2((float) getRpmE(engine), getEngineLoadT(PASS_ENGINE_PARAMETER)); + showFuelInfo2((float) getRpmE(engine), getEngineLoadT(PASS_ENGINE_PARAMETER_F)); } #endif @@ -541,7 +541,7 @@ void updateTunerStudioState(TunerStudioOutputChannels *tsOutputChannels DECLARE_ float coolant = getCoolantTemperature(PASS_ENGINE_PARAMETER_F); float intake = getIntakeAirTemperature(PASS_ENGINE_PARAMETER_F); - float engineLoad = getEngineLoadT(PASS_ENGINE_PARAMETER); + float engineLoad = getEngineLoadT(PASS_ENGINE_PARAMETER_F); float baseFuelMs = getBaseFuel(rpm PASS_ENGINE_PARAMETER); // header @@ -605,7 +605,7 @@ void updateTunerStudioState(TunerStudioOutputChannels *tsOutputChannels DECLARE_ tsOutputChannels->clutchUpState = engine->clutchUpState; tsOutputChannels->clutchDownState = engine->clutchDownState; tsOutputChannels->tCharge = getTCharge(rpm, tps, coolant, intake); - float timing = getAdvance(rpm, engineLoad PASS_ENGINE_PARAMETER); + float timing = engine->engineState.timingAdvance; tsOutputChannels->ignitionAdvance = timing > 360 ? timing - 720 : timing; tsOutputChannels->sparkDwell = ENGINE(engineState.sparkDwell); tsOutputChannels->baseFuel = baseFuelMs; diff --git a/firmware/controllers/algo/accel_enrichment.cpp b/firmware/controllers/algo/accel_enrichment.cpp index 2e081f5ec1..cca4b05fac 100644 --- a/firmware/controllers/algo/accel_enrichment.cpp +++ b/firmware/controllers/algo/accel_enrichment.cpp @@ -93,7 +93,7 @@ AccelEnrichmemnt::AccelEnrichmemnt() { //static msg_t DiffEnrichmentThread(int param) { // chRegSetThreadName("Diff Enrichment"); // while (TRUE) { -// instance.updateDiffEnrichment(engineConfiguration, getEngineLoadT(PASS_ENGINE_PARAMETER)); +// instance.updateDiffEnrichment(engineConfiguration, getEngineLoadT(PASS_ENGINE_PARAMETER_F)); // chThdSleepMilliseconds(100); // } //#if defined __GNUC__ diff --git a/firmware/controllers/algo/advance_map.cpp b/firmware/controllers/algo/advance_map.cpp index a58d560ba5..3c90a10f4c 100644 --- a/firmware/controllers/algo/advance_map.cpp +++ b/firmware/controllers/algo/advance_map.cpp @@ -1,5 +1,5 @@ /** - * @file advance_map.c + * @file advance_map.cpp * * @date Mar 27, 2013 * @author Andrey Belomutskiy, (c) 2012-2015 diff --git a/firmware/controllers/algo/engine.cpp b/firmware/controllers/algo/engine.cpp index 2dc332eab0..57d8bdd98a 100644 --- a/firmware/controllers/algo/engine.cpp +++ b/firmware/controllers/algo/engine.cpp @@ -16,6 +16,7 @@ #include "trigger_central.h" #include "fuel_math.h" #include "engine_math.h" +#include "advance_map.h" #if EFI_PROD_CODE #include "injector_central.h" @@ -159,14 +160,23 @@ void Engine::watchdog() { #endif } +/** + * The idea of this method is to execute all heavy calculations in a lower-priority thread, + * so that trigger event handler/IO scheduler tasks are faster. Th + */ void Engine::periodicFastCallback(DECLARE_ENGINE_PARAMETER_F) { int rpm = rpmCalculator.rpmValue; + float engineLoad = getEngineLoadT(PASS_ENGINE_PARAMETER_F); engineState.sparkDwell = getSparkDwellMsT(rpm PASS_ENGINE_PARAMETER); + // todo: move this field to engineState dwellAngle = engineState.sparkDwell / getOneDegreeTimeMs(rpm); engine->engineState.iatFuelCorrection = getIatCorrection(engine->engineState.iat PASS_ENGINE_PARAMETER); engine->engineState.cltFuelCorrection = getCltCorrection(engine->engineState.clt PASS_ENGINE_PARAMETER); + + engine->engineState.injectionAngle = getInjectionAngle(rpm PASS_ENGINE_PARAMETER); + engine->engineState.timingAdvance = getAdvance(rpm, engineLoad PASS_ENGINE_PARAMETER); } StartupFuelPumping::StartupFuelPumping() { diff --git a/firmware/controllers/algo/engine.h b/firmware/controllers/algo/engine.h index 43302d4fbd..d901bde3b0 100644 --- a/firmware/controllers/algo/engine.h +++ b/firmware/controllers/algo/engine.h @@ -95,6 +95,8 @@ public: float iatFuelCorrection; float cltFuelCorrection; float injectorLag; + + angle_t injectionAngle; }; class RpmCalculator; diff --git a/firmware/controllers/algo/fuel_math.cpp b/firmware/controllers/algo/fuel_math.cpp index 9af3e905bb..f49c03885f 100644 --- a/firmware/controllers/algo/fuel_math.cpp +++ b/firmware/controllers/algo/fuel_math.cpp @@ -126,8 +126,8 @@ floatms_t getFuelMs(int rpm DECLARE_ENGINE_PARAMETER_S) { } floatms_t getRunningFuel(floatms_t baseFuel, int rpm DECLARE_ENGINE_PARAMETER_S) { - float iatCorrection = getIatCorrection(engine->engineState.iat PASS_ENGINE_PARAMETER); - float cltCorrection = getCltCorrection(engine->engineState.clt PASS_ENGINE_PARAMETER); + float iatCorrection = ENGINE(engineState.iatFuelCorrection); + float cltCorrection = ENGINE(engineState.cltFuelCorrection); #if EFI_ACCEL_ENRICHMENT float accelEnrichment = getAccelEnrichment(); diff --git a/firmware/controllers/math/engine_math.cpp b/firmware/controllers/math/engine_math.cpp index b47cde1248..0172e2af0c 100644 --- a/firmware/controllers/math/engine_math.cpp +++ b/firmware/controllers/math/engine_math.cpp @@ -165,7 +165,7 @@ void FuelSchedule::addFuelEvents(OutputSignalList *sourceList, injection_mode_e * injection phase is scheduled by injection end, so we need to step the angle back * for the duration of the injection */ - float baseAngle = getInjectionAngle(engine->rpmCalculator.rpmValue PASS_ENGINE_PARAMETER) + float baseAngle = ENGINE(engineState.injectionAngle) + engineConfiguration->injectionAngle - MS2US(engine->fuelMs) / engine->rpmCalculator.oneDegreeUs; switch (mode) { diff --git a/firmware/controllers/trigger/main_trigger_callback.cpp b/firmware/controllers/trigger/main_trigger_callback.cpp index a41b2db596..3fc5331718 100644 --- a/firmware/controllers/trigger/main_trigger_callback.cpp +++ b/firmware/controllers/trigger/main_trigger_callback.cpp @@ -283,16 +283,14 @@ static void ignitionCalc(int rpm DECLARE_ENGINE_PARAMETER_S) { * Within one engine cycle all cylinders are fired with same timing advance. * todo: one day we can control cylinders individually? */ - float dwellMs = getSparkDwellMsT(rpm PASS_ENGINE_PARAMETER); + float dwellMs = ENGINE(engineState.sparkDwell); if (cisnan(dwellMs) || dwellMs < 0) { firmwareError("invalid dwell: %f at %d", dwellMs, rpm); return; } - float el = getEngineLoadT(PASS_ENGINE_PARAMETER_F); - engine->advance = -getAdvance(rpm, el PASS_ENGINE_PARAMETER); - - engine->dwellAngle = dwellMs / getOneDegreeTimeMs(rpm); + // todo: eliminate this field + engine->advance = -ENGINE(engineState.timingAdvance); } extern OutputSignalList runningInjectonSignals CCM_OPTIONAL; @@ -436,10 +434,10 @@ void MainTriggerCallback::init(Engine *engine) { static void showMainInfo(Engine *engine) { #if EFI_PROD_CODE int rpm = engine->rpmCalculator.rpm(PASS_ENGINE_PARAMETER_F); - float el = getEngineLoadT(PASS_ENGINE_PARAMETER); + float el = getEngineLoadT(PASS_ENGINE_PARAMETER_F); scheduleMsg(logger, "rpm %d engine_load %f", rpm, el); scheduleMsg(logger, "fuel %fms timing %f", getFuelMs(rpm PASS_ENGINE_PARAMETER), - getAdvance(rpm, el PASS_ENGINE_PARAMETER)); + engine->engineState.timingAdvance); #endif } diff --git a/unit_tests/test_fuel_map.cpp b/unit_tests/test_fuel_map.cpp index 69fafca20a..778643ba94 100644 --- a/unit_tests/test_fuel_map.cpp +++ b/unit_tests/test_fuel_map.cpp @@ -15,6 +15,7 @@ #include "trigger_decoder.h" #include "engine_test_helper.h" #include "efiGpio.h" +#include "advance_map.h" extern float testMafValue; @@ -70,6 +71,8 @@ void testFuelMap(void) { // because all the correction tables are zero printf("*************************************************** getRunningFuel 1\r\n"); + prepareTimingMap(PASS_ENGINE_PARAMETER_F); + eth.engine.periodicFastCallback(PASS_ENGINE_PARAMETER_F); float baseFuel = getBaseTableFuel(eth.engine.engineConfiguration, 5, getEngineLoadT(PASS_ENGINE_PARAMETER_F)); assertEqualsM("base fuel", 5.05, getRunningFuel(baseFuel, 5 PASS_ENGINE_PARAMETER)); @@ -99,7 +102,8 @@ void testFuelMap(void) { // 1005 * 2 for IAT correction printf("*************************************************** getRunningFuel 2\r\n"); - baseFuel = getBaseTableFuel(eth.engine.engineConfiguration, 5, getEngineLoadT(PASS_ENGINE_PARAMETER_F)); + eth.engine.periodicFastCallback(PASS_ENGINE_PARAMETER_F); + baseFuel = getBaseTableFuel(eth.engine.engineConfiguration, 5, getEngineLoadT(PASS_ENGINE_PARAMETER_F)); assertEqualsM("v1", 30150, getRunningFuel(baseFuel, 5 PASS_ENGINE_PARAMETER)); testMafValue = 0;