diff --git a/firmware/controllers/algo/engine.cpp b/firmware/controllers/algo/engine.cpp index 3280d7f2c7..dc73fe9657 100644 --- a/firmware/controllers/algo/engine.cpp +++ b/firmware/controllers/algo/engine.cpp @@ -156,7 +156,9 @@ void EngineState::periodicFastCallback(DECLARE_ENGINE_PARAMETER_F) { sparkDwell = getSparkDwell(rpm PASS_ENGINE_PARAMETER); dwellAngle = sparkDwell / getOneDegreeTimeMs(rpm); + // todo: move this into slow callback, no reason for IAT corr to be here iatFuelCorrection = getIatCorrection(iat PASS_ENGINE_PARAMETER); + // todo: move this into slow callback, no reason for CLT corr to be here if (boardConfiguration->useWarmupPidAfr && clt < engineConfiguration->warmupAfrThreshold) { if (rpm < 200) { cltFuelCorrection = 1; @@ -377,7 +379,7 @@ void Engine::periodicFastCallback(DECLARE_ENGINE_PARAMETER_F) { engineState.periodicFastCallback(PASS_ENGINE_PARAMETER_F); engine->m.beforeFuelCalc = GET_TIMESTAMP(); - ENGINE(fuelMs) = getFuelMs(rpm PASS_ENGINE_PARAMETER) * engineConfiguration->globalFuelCorrection; + ENGINE(fuelMs) = getInjectionDuration(rpm PASS_ENGINE_PARAMETER) * engineConfiguration->globalFuelCorrection; engine->m.fuelCalcTime = GET_TIMESTAMP() - engine->m.beforeFuelCalc; prepareFuelSchedule(PASS_ENGINE_PARAMETER_F); diff --git a/firmware/controllers/algo/fuel_math.cpp b/firmware/controllers/algo/fuel_math.cpp index c81f5aa607..23f3196d13 100644 --- a/firmware/controllers/algo/fuel_math.cpp +++ b/firmware/controllers/algo/fuel_math.cpp @@ -109,7 +109,7 @@ int getNumberOfInjections(injection_mode_e mode DECLARE_ENGINE_PARAMETER_S) { } percent_t getInjectorDutyCycle(int rpm DECLARE_ENGINE_PARAMETER_S) { - floatms_t totalPerCycle = getFuelMs(rpm PASS_ENGINE_PARAMETER) * getNumberOfInjections(engineConfiguration->injectionMode PASS_ENGINE_PARAMETER); + floatms_t totalPerCycle = getInjectionDuration(rpm PASS_ENGINE_PARAMETER) * getNumberOfInjections(engineConfiguration->injectionMode PASS_ENGINE_PARAMETER); floatms_t engineCycleDuration = getCrankshaftRevolutionTimeMs(rpm) * (engineConfiguration->operationMode == TWO_STROKE ? 1 : 2); return 100 * totalPerCycle / engineCycleDuration; } @@ -117,14 +117,14 @@ percent_t getInjectorDutyCycle(int rpm DECLARE_ENGINE_PARAMETER_S) { /** * @returns Length of each individual fuel injection, in milliseconds */ -floatms_t getFuelMs(int rpm DECLARE_ENGINE_PARAMETER_S) { +floatms_t getInjectionDuration(int rpm DECLARE_ENGINE_PARAMETER_S) { float theoreticalInjectionLength; if (isCrankingR(rpm)) { theoreticalInjectionLength = getCrankingFuel(PASS_ENGINE_PARAMETER_F) / getNumberOfInjections(engineConfiguration->crankingInjectionMode PASS_ENGINE_PARAMETER); } else { - float baseFuel = getBaseFuel(rpm PASS_ENGINE_PARAMETER); - float fuelPerCycle = getRunningFuel(baseFuel, rpm PASS_ENGINE_PARAMETER); + floatms_t baseFuel = getBaseFuel(rpm PASS_ENGINE_PARAMETER); + floatms_t fuelPerCycle = getRunningFuel(baseFuel, rpm PASS_ENGINE_PARAMETER); theoreticalInjectionLength = fuelPerCycle / getNumberOfInjections(engineConfiguration->injectionMode PASS_ENGINE_PARAMETER); } diff --git a/firmware/controllers/algo/fuel_math.h b/firmware/controllers/algo/fuel_math.h index 93dc137b07..bc8f3e946e 100644 --- a/firmware/controllers/algo/fuel_math.h +++ b/firmware/controllers/algo/fuel_math.h @@ -33,7 +33,7 @@ floatms_t getInjectorLag(float vBatt DECLARE_ENGINE_PARAMETER_S); float getCltCorrection(float clt DECLARE_ENGINE_PARAMETER_S); floatms_t getCrankingFuel(DECLARE_ENGINE_PARAMETER_F); floatms_t getCrankingFuel3(float coolantTemperature, uint32_t revolutionCounterSinceStart DECLARE_ENGINE_PARAMETER_S); -floatms_t getFuelMs(int rpm DECLARE_ENGINE_PARAMETER_S); +floatms_t getInjectionDuration(int rpm DECLARE_ENGINE_PARAMETER_S); percent_t getInjectorDutyCycle(int rpm DECLARE_ENGINE_PARAMETER_S); #endif /* FUEL_MAP_H_ */ diff --git a/firmware/controllers/core/table_helper.cpp b/firmware/controllers/core/table_helper.cpp index acf6bc70ad..1ed0970b6e 100644 --- a/firmware/controllers/core/table_helper.cpp +++ b/firmware/controllers/core/table_helper.cpp @@ -10,9 +10,9 @@ #include "efilib.h" #include "interpolation.h" -void setTableBin2(float array[], int size, float l, float r, float precision) { +void setTableBin2(float array[], int size, float from, float to, float precision) { for (int i = 0; i < size; i++) { - float value = interpolateMsg("setTable", 0, l, size - 1, r, i); + float value = interpolateMsg("setTable", 0, from, size - 1, to, i); /** * rounded values look nicer, also we want to avoid precision mismatch with Tuner Studio */ diff --git a/firmware/controllers/core/table_helper.h b/firmware/controllers/core/table_helper.h index aad2c841e1..500c3eee3f 100644 --- a/firmware/controllers/core/table_helper.h +++ b/firmware/controllers/core/table_helper.h @@ -147,8 +147,8 @@ typedef Map3D baroCorr_Map3D_t; void setRpmBin(float array[], int size, float idleRpm, float topRpm); -void setTableBin(float array[], int size, float l, float r); -void setTableBin2(float array[], int size, float l, float r, float precision); +void setTableBin(float array[], int size, float from, float to); +void setTableBin2(float array[], int size, float from, float to, float precision); void setRpmTableBin(float array[], int size); #endif /* TABLE_HELPER_H_ */ diff --git a/firmware/controllers/trigger/main_trigger_callback.cpp b/firmware/controllers/trigger/main_trigger_callback.cpp index cb8df7e7be..fd330604bf 100644 --- a/firmware/controllers/trigger/main_trigger_callback.cpp +++ b/firmware/controllers/trigger/main_trigger_callback.cpp @@ -207,7 +207,7 @@ static ALWAYS_INLINE void handleFuel(bool limitedFuel, uint32_t eventIndex, int ENGINE(tpsAccelEnrichment.onNewValue(getTPS(PASS_ENGINE_PARAMETER_F) PASS_ENGINE_PARAMETER)); ENGINE(engineLoadAccelEnrichment.onEngineCycle(PASS_ENGINE_PARAMETER_F)); - ENGINE(fuelMs) = getFuelMs(rpm PASS_ENGINE_PARAMETER) * CONFIG(globalFuelCorrection); + ENGINE(fuelMs) = getInjectionDuration(rpm PASS_ENGINE_PARAMETER) * CONFIG(globalFuelCorrection); for (int injEventIndex = 0; injEventIndex < injectionEvents->size; injEventIndex++) { InjectionEvent *event = &injectionEvents->elements[injEventIndex]; @@ -542,7 +542,7 @@ static void showMainInfo(Engine *engine) { int rpm = engine->rpmCalculator.getRpm(PASS_ENGINE_PARAMETER_F); 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), engine->engineState.timingAdvance); + scheduleMsg(logger, "fuel %fms timing %f", getInjectionDuration(rpm PASS_ENGINE_PARAMETER), engine->engineState.timingAdvance); #endif } diff --git a/firmware/development/rfi_perftest.cpp b/firmware/development/rfi_perftest.cpp index 02e6e42d9d..f8a776707d 100644 --- a/firmware/development/rfi_perftest.cpp +++ b/firmware/development/rfi_perftest.cpp @@ -83,7 +83,7 @@ static void testRusefiMethods(const int count) { // start = currentTimeMillis(); // for (int i = 0; i < count; i++) -// tempi += getFuelMs(1200, NULL); // todo +// tempi += getInjectionDuration(1200, NULL); // todo // time = currentTimeMillis() - start; // if (tempi != 0) // scheduleMsg(logger, "Finished %d iterations of getFuelMs in %dms", count, time); diff --git a/unit_tests/engine_test_helper.cpp b/unit_tests/engine_test_helper.cpp index 3d52bd30e0..503dea33a0 100644 --- a/unit_tests/engine_test_helper.cpp +++ b/unit_tests/engine_test_helper.cpp @@ -12,6 +12,7 @@ #include "fuel_math.h" #include "accel_enrichment.h" #include "thermistors.h" +#include "advance_map.h" extern int timeNow; @@ -52,16 +53,21 @@ EngineTestHelper::EngineTestHelper(engine_type_e engineType) : engine (&persiste initThermistors(NULL PASS_ENGINE_PARAMETER); // this is needed to have valid CLT and IAT. engine->updateSlowSensors(PASS_ENGINE_PARAMETER_F); + prepareTimingMap(PASS_ENGINE_PARAMETER_F); +} + +void EngineTestHelper::fireTriggerEvents2(int count, int duration) { + for (int i = 0; i < count; i++) { + timeNow += duration; + board_configuration_s * boardConfiguration = &engine.engineConfiguration->bc; + engine.triggerCentral.handleShaftSignal(SHAFT_PRIMARY_RISING, &engine, engine.engineConfiguration, &persistentConfig, boardConfiguration); + timeNow += duration; + engine.triggerCentral.handleShaftSignal(SHAFT_PRIMARY_FALLING, &engine, engine.engineConfiguration, &persistentConfig, boardConfiguration); + } } void EngineTestHelper::fireTriggerEvents(int count) { - for (int i = 0; i < count; i++) { - timeNow += 5000; // 5ms - board_configuration_s * boardConfiguration = &engine.engineConfiguration->bc; - engine.triggerCentral.handleShaftSignal(SHAFT_PRIMARY_RISING, &engine, engine.engineConfiguration, &persistentConfig, boardConfiguration); - timeNow += 5000; - engine.triggerCentral.handleShaftSignal(SHAFT_PRIMARY_FALLING, &engine, engine.engineConfiguration, &persistentConfig, boardConfiguration); - } + fireTriggerEvents2(count, 5000); // 5ms } void EngineTestHelper::initTriggerShapeAndRpmCalculator() { diff --git a/unit_tests/engine_test_helper.h b/unit_tests/engine_test_helper.h index 41ab5d281f..ea44b716ac 100644 --- a/unit_tests/engine_test_helper.h +++ b/unit_tests/engine_test_helper.h @@ -16,6 +16,7 @@ public: EngineTestHelper(engine_type_e engineType); void initTriggerShapeAndRpmCalculator(); void fireTriggerEvents(int count); + void fireTriggerEvents2(int count, int duration); persistent_config_s persistentConfig; engine_configuration2_s ec2; diff --git a/unit_tests/test_fuel_map.cpp b/unit_tests/test_fuel_map.cpp index 7048c47101..76a29d126c 100644 --- a/unit_tests/test_fuel_map.cpp +++ b/unit_tests/test_fuel_map.cpp @@ -68,7 +68,6 @@ 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)); diff --git a/unit_tests/test_trigger_decoder.cpp b/unit_tests/test_trigger_decoder.cpp index 28ccbde479..b6b1fb023b 100644 --- a/unit_tests/test_trigger_decoder.cpp +++ b/unit_tests/test_trigger_decoder.cpp @@ -321,9 +321,6 @@ void testRpmCalculator(void) { triggerCallbackInstance.init(ð.engine); eth.engine.triggerCentral.addEventListener(mainTriggerCallback, "main loop", ð.engine); -// engine.rpmCalculator = ð.rpmState; - prepareTimingMap(PASS_ENGINE_PARAMETER_F); - assertEqualsM("queue size/0", 0, schedulingQueue.size()); debugSignalExecutor = true; @@ -588,5 +585,17 @@ void testFuelSchedulerBug299(void) { assertEqualsM("CLT", 70, engine->engineState.clt); + engineConfiguration->trigger.type = TT_ONE; + incrementGlobalConfigurationVersion(); + + eth.initTriggerShapeAndRpmCalculator(); + + assertEqualsM("RPM=0", 0, eth.engine.rpmCalculator.getRpm(PASS_ENGINE_PARAMETER_F)); + + eth.fireTriggerEvents2(2, MS2US(50)); + + assertEqualsM("RPM", 1200, eth.engine.rpmCalculator.getRpm(PASS_ENGINE_PARAMETER_F)); + + assertEqualsM("fuel", 4.07, engine->fuelMs); }