From 6e7d72170f22af04f0128d9f80717cae2d0fcb2f Mon Sep 17 00:00:00 2001 From: Matthew Kennedy Date: Sun, 6 Sep 2020 16:06:32 -0700 Subject: [PATCH] Move ve logic (#1762) * move ve * fix build * fix rendering * duh * cleaning * correct scaling * put some back Co-authored-by: Matthew Kennedy --- firmware/console/status_loop.cpp | 9 +++--- firmware/controllers/algo/airmass/airmass.cpp | 16 +++++++++- .../algo/airmass/alphan_airmass.cpp | 2 +- .../controllers/algo/airmass/maf_airmass.cpp | 2 +- .../algo/airmass/speed_density_airmass.cpp | 4 ++- firmware/controllers/algo/engine2.cpp | 29 ++----------------- firmware/controllers/algo/engine_state.h | 2 +- firmware/controllers/algo/fuel_math.cpp | 4 ++- unit_tests/tests/test_fuel_map.cpp | 2 +- 9 files changed, 32 insertions(+), 38 deletions(-) diff --git a/firmware/console/status_loop.cpp b/firmware/console/status_loop.cpp index e110a3a4fe..d4e173ce2b 100644 --- a/firmware/console/status_loop.cpp +++ b/firmware/console/status_loop.cpp @@ -62,7 +62,6 @@ #include "cdm_ion_sense.h" #include "binary_logging.h" -extern afr_Map3D_t afrMap; extern bool main_loop_started; #if EFI_PROD_CODE @@ -606,12 +605,12 @@ void updateTunerStudioState(TunerStudioOutputChannels *tsOutputChannels DECLARE_ // 288 tsOutputChannels->injectionOffset = engine->engineState.injectionOffset; + // offset 112 + tsOutputChannels->veValue = engine->engineState.currentVe; + tsOutputChannels->currentTargetAfr = ENGINE(engineState.targetAFR); + if (hasMapSensor(PASS_ENGINE_PARAMETER_SIGNATURE)) { float mapValue = getMap(PASS_ENGINE_PARAMETER_SIGNATURE); - // // offset 112 - tsOutputChannels->veValue = engine->engineState.currentBaroCorrectedVE * PERCENT_MULT; - // todo: bug here? target afr could work based on real MAF? - tsOutputChannels->currentTargetAfr = afrMap.getValue(rpm, mapValue); // offset 40 tsOutputChannels->manifoldAirPressure = mapValue; } diff --git a/firmware/controllers/algo/airmass/airmass.cpp b/firmware/controllers/algo/airmass/airmass.cpp index 3ede8519e1..ebf701cc4f 100644 --- a/firmware/controllers/algo/airmass/airmass.cpp +++ b/firmware/controllers/algo/airmass/airmass.cpp @@ -1,4 +1,7 @@ #include "airmass.h" +#include "sensor.h" + +EXTERN_ENGINE; AirmassModelBase::AirmassModelBase(const ValueProvider3D& veTable) : m_veTable(&veTable) {} @@ -6,5 +9,16 @@ float AirmassModelBase::getVe(int rpm, float load) const { efiAssert(OBD_PCM_Processor_Fault, m_veTable != nullptr, "VE table null", 0); // TODO: allow override of the Y axis value based on a config field - return m_veTable->getValue(rpm, load); + float ve = m_veTable->getValue(rpm, load); + + auto tps = Sensor::get(SensorType::Tps1); + // get VE from the separate table for Idle + if (tps.Valid && CONFIG(useSeparateVeForIdle)) { + float idleVe = interpolate2d("idleVe", rpm, config->idleVeBins, config->idleVe); + // interpolate between idle table and normal (running) table using TPS threshold + ve = interpolateClamped(0.0f, idleVe, CONFIG(idlePidDeactivationTpsThreshold), ve, tps.Value); + } + + ENGINE(engineState.currentVe) = ve; + return ve * 0.01f; } diff --git a/firmware/controllers/algo/airmass/alphan_airmass.cpp b/firmware/controllers/algo/airmass/alphan_airmass.cpp index e9c081edb4..fb5869493f 100644 --- a/firmware/controllers/algo/airmass/alphan_airmass.cpp +++ b/firmware/controllers/algo/airmass/alphan_airmass.cpp @@ -14,7 +14,7 @@ AirmassResult AlphaNAirmass::getAirmass(int rpm) { // TODO: should this be barometric pressure and/or temperature compensated? float airmass = getAirmassImpl( - ve / 100.0f, + ve, 101.325f, // std atmosphere pressure 273.0f + 20.0f // std atmosphere pressure PASS_ENGINE_PARAMETER_SUFFIX diff --git a/firmware/controllers/algo/airmass/maf_airmass.cpp b/firmware/controllers/algo/airmass/maf_airmass.cpp index 7129b0746c..ce263d98a3 100644 --- a/firmware/controllers/algo/airmass/maf_airmass.cpp +++ b/firmware/controllers/algo/airmass/maf_airmass.cpp @@ -37,7 +37,7 @@ AirmassResult MafAirmass::getAirmassImpl(float massAirFlow, int rpm) const { float airChargeLoad = 100 * cylinderAirmass / ENGINE(standardAirCharge); //Correct air mass by VE table - float correctedAirmass = cylinderAirmass * getVe(rpm, airChargeLoad) / 100; + float correctedAirmass = cylinderAirmass * getVe(rpm, airChargeLoad); return { correctedAirmass, diff --git a/firmware/controllers/algo/airmass/speed_density_airmass.cpp b/firmware/controllers/algo/airmass/speed_density_airmass.cpp index 6bb8effbcc..34901d70a8 100644 --- a/firmware/controllers/algo/airmass/speed_density_airmass.cpp +++ b/firmware/controllers/algo/airmass/speed_density_airmass.cpp @@ -26,7 +26,9 @@ AirmassResult SpeedDensityAirmass::getAirmass(int rpm) { float adjustedMap = engine->engineState.sd.adjustedManifoldAirPressure = map + engine->engineState.sd.manifoldAirPressureAccelerationAdjustment; efiAssert(CUSTOM_ERR_ASSERT, !cisnan(adjustedMap), "NaN adjustedMap", {}); - float airMass = getAirmassImpl(ENGINE(engineState.currentBaroCorrectedVE), adjustedMap, tChargeK PASS_ENGINE_PARAMETER_SUFFIX); + float ve = getVe(rpm, adjustedMap); + + float airMass = getAirmassImpl(ve, adjustedMap, tChargeK PASS_ENGINE_PARAMETER_SUFFIX); if (cisnan(airMass)) { warning(CUSTOM_ERR_6685, "NaN airMass"); return {}; diff --git a/firmware/controllers/algo/engine2.cpp b/firmware/controllers/algo/engine2.cpp index 4a0019a365..a6190c8ca9 100644 --- a/firmware/controllers/algo/engine2.cpp +++ b/firmware/controllers/algo/engine2.cpp @@ -171,32 +171,8 @@ void EngineState::periodicFastCallback(DECLARE_ENGINE_PARAMETER_SIGNATURE) { baroCorrection = getBaroCorrection(PASS_ENGINE_PARAMETER_SIGNATURE); - multispark.count = getMultiSparkCount(rpm PASS_ENGINE_PARAMETER_SUFFIX); - - if (engineConfiguration->fuelAlgorithm == LM_SPEED_DENSITY) { - auto tps = Sensor::get(SensorType::Tps1); - updateTChargeK(rpm, tps.value_or(0) PASS_ENGINE_PARAMETER_SUFFIX); - float map = getMap(PASS_ENGINE_PARAMETER_SIGNATURE); - - /** - * *0.01 because of https://sourceforge.net/p/rusefi/tickets/153/ - */ - if (CONFIG(useTPSBasedVeTable)) { - // todo: should we have 'veTpsMap' fuel_Map3D_t variable here? - currentRawVE = interpolate3d(tps.value_or(50), CONFIG(ignitionTpsBins), IGN_TPS_COUNT, rpm, config->veRpmBins, FUEL_RPM_COUNT, veMap.pointers); - } else { - currentRawVE = veMap.getValue(rpm, map); - } - - // get VE from the separate table for Idle - if (tps.Valid && CONFIG(useSeparateVeForIdle)) { - float idleVe = interpolate2d("idleVe", rpm, config->idleVeBins, config->idleVe); - // interpolate between idle table and normal (running) table using TPS threshold - currentRawVE = interpolateClamped(0.0f, idleVe, CONFIG(idlePidDeactivationTpsThreshold), currentRawVE, tps.Value); - } - currentBaroCorrectedVE = baroCorrection * currentRawVE * PERCENT_DIV; - } - + auto tps = Sensor::get(SensorType::Tps1); + updateTChargeK(rpm, tps.value_or(0) PASS_ENGINE_PARAMETER_SUFFIX); ENGINE(injectionDuration) = getInjectionDuration(rpm PASS_ENGINE_PARAMETER_SUFFIX); float fuelLoad = getFuelingLoad(PASS_ENGINE_PARAMETER_SIGNATURE); @@ -204,6 +180,7 @@ void EngineState::periodicFastCallback(DECLARE_ENGINE_PARAMETER_SIGNATURE) { float ignitionLoad = getIgnitionLoad(PASS_ENGINE_PARAMETER_SIGNATURE); timingAdvance = getAdvance(rpm, ignitionLoad PASS_ENGINE_PARAMETER_SUFFIX); + multispark.count = getMultiSparkCount(rpm PASS_ENGINE_PARAMETER_SUFFIX); #endif // EFI_ENGINE_CONTROL } diff --git a/firmware/controllers/algo/engine_state.h b/firmware/controllers/algo/engine_state.h index 6299bfb3ea..6d64e074b5 100644 --- a/firmware/controllers/algo/engine_state.h +++ b/firmware/controllers/algo/engine_state.h @@ -59,7 +59,7 @@ public: efitick_t timeSinceLastTChargeK; - float currentRawVE = 0; + float currentVe = 0; int vssEventCounter = 0; diff --git a/firmware/controllers/algo/fuel_math.cpp b/firmware/controllers/algo/fuel_math.cpp index 3cce0ba9d6..c723dda606 100644 --- a/firmware/controllers/algo/fuel_math.cpp +++ b/firmware/controllers/algo/fuel_math.cpp @@ -137,11 +137,13 @@ floatms_t getRunningFuel(floatms_t baseFuel DECLARE_ENGINE_PARAMETER_SUFFIX) { float postCrankingFuelCorrection = ENGINE(engineState.DISPLAY_PREFIX(running).DISPLAY_FIELD(postCrankingFuelCorrection)); DISPLAY_TEXT(eol); + float baroCorrection = ENGINE(engineState.baroCorrection); + efiAssert(CUSTOM_ERR_ASSERT, !cisnan(iatCorrection), "NaN iatCorrection", 0); efiAssert(CUSTOM_ERR_ASSERT, !cisnan(cltCorrection), "NaN cltCorrection", 0); efiAssert(CUSTOM_ERR_ASSERT, !cisnan(postCrankingFuelCorrection), "NaN postCrankingFuelCorrection", 0); - floatms_t runningFuel = baseFuel * iatCorrection * cltCorrection * postCrankingFuelCorrection * ENGINE(engineState.running.pidCorrection); + floatms_t runningFuel = baseFuel * baroCorrection * iatCorrection * cltCorrection * postCrankingFuelCorrection * ENGINE(engineState.running.pidCorrection); efiAssert(CUSTOM_ERR_ASSERT, !cisnan(runningFuel), "NaN runningFuel", 0); DISPLAY_TEXT(eol); diff --git a/unit_tests/tests/test_fuel_map.cpp b/unit_tests/tests/test_fuel_map.cpp index 871b42ddee..ef8643802b 100644 --- a/unit_tests/tests/test_fuel_map.cpp +++ b/unit_tests/tests/test_fuel_map.cpp @@ -234,5 +234,5 @@ TEST(fuel, testTpsBasedVeDefect799) { engine->engineState.periodicFastCallback(PASS_ENGINE_PARAMETER_SIGNATURE); // value in the middle of the map as expected - ASSERT_EQ(107, engine->engineState.currentRawVE); + ASSERT_EQ(107, engine->engineState.currentVe); }