From 3cabdddf918e802ae7f92315f437305c154efd7b Mon Sep 17 00:00:00 2001 From: rusEfi Date: Tue, 10 Feb 2015 09:09:58 -0600 Subject: [PATCH] auto-sync --- firmware/config/engines/mazda_miata.cpp | 2 +- .../controllers/algo/auto_generated_enums.cpp | 4 ++-- firmware/controllers/algo/engine.cpp | 4 ++++ firmware/controllers/algo/engine.h | 6 ++++++ .../controllers/algo/engine_configuration.cpp | 2 +- firmware/controllers/algo/fuel_math.cpp | 4 ++-- firmware/controllers/algo/rusefi_enums.h | 2 +- firmware/controllers/map_averaging.cpp | 19 ++++++++++++++++--- firmware/controllers/math/engine_math.cpp | 2 +- firmware/controllers/math/speed_density.cpp | 4 ++-- .../controllers/trigger/rpm_calculator.cpp | 2 +- firmware/controllers/trigger/rpm_calculator.h | 1 + unit_tests/test_fuel_map.cpp | 1 + unit_tests/test_trigger_decoder.cpp | 2 ++ 14 files changed, 41 insertions(+), 14 deletions(-) diff --git a/firmware/config/engines/mazda_miata.cpp b/firmware/config/engines/mazda_miata.cpp index 12a93435dc..06350c15f2 100644 --- a/firmware/config/engines/mazda_miata.cpp +++ b/firmware/config/engines/mazda_miata.cpp @@ -239,7 +239,7 @@ void setFordEscortGt(engine_configuration_s *engineConfiguration) { setFrankenso0_1_joystick(engineConfiguration); engineConfiguration->specs.displacement = 1.839; - engineConfiguration->algorithm = LM_MAF; + engineConfiguration->algorithm = LM_PLAIN_MAF; boardConfiguration->tunerStudioSerialSpeed = 9600; setFuelLoadBin(engineConfiguration, 1.2, 4.4); diff --git a/firmware/controllers/algo/auto_generated_enums.cpp b/firmware/controllers/algo/auto_generated_enums.cpp index 58723473b1..283f92627b 100644 --- a/firmware/controllers/algo/auto_generated_enums.cpp +++ b/firmware/controllers/algo/auto_generated_enums.cpp @@ -174,8 +174,8 @@ case Force_4b_engine_load_mode: return "Force_4b_engine_load_mode"; case LM_ALPHA_N: return "LM_ALPHA_N"; -case LM_MAF: - return "LM_MAF"; +case LM_PLAIN_MAF: + return "LM_PLAIN_MAF"; case LM_MAP: return "LM_MAP"; case LM_SPEED_DENSITY: diff --git a/firmware/controllers/algo/engine.cpp b/firmware/controllers/algo/engine.cpp index 15ebd63b21..9def47e3ae 100644 --- a/firmware/controllers/algo/engine.cpp +++ b/firmware/controllers/algo/engine.cpp @@ -14,6 +14,7 @@ #include "engine_state.h" #include "efiGpio.h" #include "trigger_central.h" +#include "fuel_math.h" #if EFI_PROD_CODE #include "injector_central.h" @@ -34,6 +35,9 @@ EXTERN_ENGINE void Engine::updateSlowSensors() { engineState.iat = getIntakeAirTemperature(this); engineState.clt = getCoolantTemperature(this); + + Engine *engine = this; + injectorLagMs = getInjectorLag(getVBatt(engineConfiguration) PASS_ENGINE_PARAMETER); } void Engine::onTriggerEvent(uint64_t nowNt) { diff --git a/firmware/controllers/algo/engine.h b/firmware/controllers/algo/engine.h index b67a1aa9d5..cc20e7f4b3 100644 --- a/firmware/controllers/algo/engine.h +++ b/firmware/controllers/algo/engine.h @@ -128,6 +128,12 @@ public: EngineState engineState; uint64_t lastTriggerEventTimeNt; + /** + * this value depends on a slow-changing VBatt value, so + * we update it once in a while + */ + float injectorLagMs; + /** * This coefficient translates ADC value directly into voltage adjusted according to * voltage divider configuration. This is a future (?) performance optimization. diff --git a/firmware/controllers/algo/engine_configuration.cpp b/firmware/controllers/algo/engine_configuration.cpp index 54b26e3194..6412a1749c 100644 --- a/firmware/controllers/algo/engine_configuration.cpp +++ b/firmware/controllers/algo/engine_configuration.cpp @@ -261,7 +261,7 @@ void setDefaultConfiguration(engine_configuration_s *engineConfiguration, board_ engineConfiguration->overrideCrankingIgnition = true; engineConfiguration->analogChartFrequency = 20; - engineConfiguration->algorithm = LM_MAF; + engineConfiguration->algorithm = LM_PLAIN_MAF; engineConfiguration->vbattDividerCoeff = ((float) (15 + 65)) / 15; diff --git a/firmware/controllers/algo/fuel_math.cpp b/firmware/controllers/algo/fuel_math.cpp index 5f4be68692..219f23fd6f 100644 --- a/firmware/controllers/algo/fuel_math.cpp +++ b/firmware/controllers/algo/fuel_math.cpp @@ -89,8 +89,8 @@ float getFuelMs(int rpm DECLARE_ENGINE_PARAMETER_S) { } float getRunningFuel(float baseFuelMs, int rpm DECLARE_ENGINE_PARAMETER_S) { - float iatCorrection = getIatCorrection(getIntakeAirTemperature(engine) PASS_ENGINE_PARAMETER); - float cltCorrection = getCltCorrection(getCoolantTemperature(engine) PASS_ENGINE_PARAMETER); + float iatCorrection = getIatCorrection(engine->engineState.iat PASS_ENGINE_PARAMETER); + float cltCorrection = getCltCorrection(engine->engineState.clt PASS_ENGINE_PARAMETER); #if EFI_ACCEL_ENRICHMENT float accelEnrichment = getAccelEnrichment(); diff --git a/firmware/controllers/algo/rusefi_enums.h b/firmware/controllers/algo/rusefi_enums.h index 73e1169aec..1323918588 100644 --- a/firmware/controllers/algo/rusefi_enums.h +++ b/firmware/controllers/algo/rusefi_enums.h @@ -170,7 +170,7 @@ typedef enum { /** * raw Mass Air Flow sensor value algorithm. http://en.wikipedia.org/wiki/Mass_flow_sensor */ - LM_MAF = 0, + LM_PLAIN_MAF = 0, /** * Throttle Position Sensor value is used as engine load. http://en.wikipedia.org/wiki/Throttle_position_sensor */ diff --git a/firmware/controllers/map_averaging.cpp b/firmware/controllers/map_averaging.cpp index 4fdd0e4be4..58ca4d5771 100644 --- a/firmware/controllers/map_averaging.cpp +++ b/firmware/controllers/map_averaging.cpp @@ -51,7 +51,8 @@ static volatile int perRevolutionCounter = 0; static volatile int perRevolution = 0; /** - * In this lock-free imlementation 'readIndex' + * In this lock-free imlementation 'readIndex' is always pointing + * to the consistent copy of accumulator and counter pair */ static int readIndex = 0; static float accumulators[2]; @@ -121,6 +122,18 @@ void mapAveragingCallback(adcsample_t adcValue) { } #endif /* EFI_ANALOG_CHART */ + /** + * Local copy is now safe, but it's an overkill: we only + * have one writing thread anyway + */ + int readIndexLocal = readIndex; + int writeIndex = readIndexLocal ^ 1; + accumulators[writeIndex] = accumulators[readIndexLocal] + adcValue; + counters[writeIndex] = counters[readIndexLocal] + 1; + // this would commit the new pair of values + readIndex = writeIndex; + + // todo: migrate to the lock-free implementation chSysLockFromIsr() ; // with locking we would have a consistent state @@ -151,7 +164,7 @@ static void mapAveragingCallback(trigger_event_e ckpEventType, uint32_t index DE if (index != 0) return; - int rpm = getRpmE(engine); + int rpm = engine->rpmCalculator.rpmValue; if (!isValidRpm(rpm)) return; @@ -186,7 +199,7 @@ float getMapVoltage(void) { * @return Manifold Absolute Pressure, in kPa */ float getMap(void) { - if (getRpm() == 0) + if (!isValidRpm(engine->rpmCalculator.rpmValue)) return getRawMap(); // maybe return NaN in case of stopped engine? return getMapByVoltage(v_averagedMapValue); } diff --git a/firmware/controllers/math/engine_math.cpp b/firmware/controllers/math/engine_math.cpp index ed9379dbfd..160b23d62e 100644 --- a/firmware/controllers/math/engine_math.cpp +++ b/firmware/controllers/math/engine_math.cpp @@ -57,7 +57,7 @@ float getEngineLoadT(DECLARE_ENGINE_PARAMETER_F) { efiAssert(engine!=NULL, "engine 2NULL", NAN); efiAssert(engineConfiguration!=NULL, "engineConfiguration 2NULL", NAN); switch (engineConfiguration->algorithm) { - case LM_MAF: + case LM_PLAIN_MAF: return getMafT(engineConfiguration); case LM_SPEED_DENSITY: // SD engine load is used for timing lookup but not for fuel calculation diff --git a/firmware/controllers/math/speed_density.cpp b/firmware/controllers/math/speed_density.cpp index d84d194533..b7bfe1382a 100644 --- a/firmware/controllers/math/speed_density.cpp +++ b/firmware/controllers/math/speed_density.cpp @@ -69,8 +69,8 @@ float getSpeedDensityFuel(Engine *engine, int rpm) { engine_configuration_s *engineConfiguration = engine->engineConfiguration; float tps = getTPS(PASS_ENGINE_PARAMETER_F); - float coolantC = getCoolantTemperature(engine); - float intakeC = getIntakeAirTemperature(engine); + float coolantC = engine->engineState.clt; + float intakeC = engine->engineState.iat; float tChargeK = convertCelsiusToKelvin(getTCharge(rpm, tps, coolantC, intakeC)); float map = getMap(); float VE = veMap.getValue(map, engineConfiguration->veLoadBins, rpm, diff --git a/firmware/controllers/trigger/rpm_calculator.cpp b/firmware/controllers/trigger/rpm_calculator.cpp index da02a03512..f6a8b688fe 100644 --- a/firmware/controllers/trigger/rpm_calculator.cpp +++ b/firmware/controllers/trigger/rpm_calculator.cpp @@ -99,7 +99,7 @@ float RpmCalculator::getRpmAcceleration() { * * @return -1 in case of isNoisySignal(), current RPM otherwise */ -// todo: migrate to float return result or add a float verion? this would have with calculations +// todo: migrate to float return result or add a float version? this would have with calculations // todo: add a version which does not check time & saves time? need to profile int RpmCalculator::rpm(DECLARE_ENGINE_PARAMETER_F) { #if !EFI_PROD_CODE diff --git a/firmware/controllers/trigger/rpm_calculator.h b/firmware/controllers/trigger/rpm_calculator.h index f9a2896c8d..336ed7794e 100644 --- a/firmware/controllers/trigger/rpm_calculator.h +++ b/firmware/controllers/trigger/rpm_calculator.h @@ -48,6 +48,7 @@ public: float getRpmAcceleration(); /** * This is public because sometimes we cannot afford to call isRunning() and the value is good enough + * Zero if engine is not running */ volatile int rpmValue; int previousRpmValue; diff --git a/unit_tests/test_fuel_map.cpp b/unit_tests/test_fuel_map.cpp index 80427e4359..1f1772069c 100644 --- a/unit_tests/test_fuel_map.cpp +++ b/unit_tests/test_fuel_map.cpp @@ -55,6 +55,7 @@ void testFuelMap(void) { eth.engine.engineConfiguration->injector.battLagCorr[i] = 2 * i; } + eth.engine.updateSlowSensors(); // because all the correction tables are zero printf("*************************************************** getRunningFuel 1\r\n"); diff --git a/unit_tests/test_trigger_decoder.cpp b/unit_tests/test_trigger_decoder.cpp index 0d7ed1bf1d..35917d4a1e 100644 --- a/unit_tests/test_trigger_decoder.cpp +++ b/unit_tests/test_trigger_decoder.cpp @@ -373,6 +373,7 @@ static void testRpmCalculator(void) { engine_configuration_s *engineConfiguration = ð.persistentConfig.engineConfiguration; initThermistors(PASS_ENGINE_PARAMETER_F); + engine->updateSlowSensors(); engineConfiguration->trigger.customTotalToothCount = 8; engineConfiguration->globalFuelCorrection = 3; @@ -382,6 +383,7 @@ static void testRpmCalculator(void) { // engine.engineConfiguration = eth.engine.engineConfiguration; eth.engine.engineConfiguration->injector.lag = 0.0; + engine->updateSlowSensors(); timeNow = 0; assertEquals(0, eth.engine.rpmCalculator.rpm(PASS_ENGINE_PARAMETER_F));