From d24b5d5e93a51678483cda5e1c5e302afce90df4 Mon Sep 17 00:00:00 2001 From: Matthew Kennedy Date: Sun, 31 May 2020 13:59:05 -0700 Subject: [PATCH] Integrate stft (#1475) * code changes * missed a spot * update UI * gauge name * adjust gauge scaling Co-authored-by: Matthew Kennedy --- firmware/console/binary/tunerstudio_outputs.h | 2 +- .../console/binary_log/binary_logging.cpp | 2 +- firmware/console/status_loop.cpp | 2 +- firmware/controllers/algo/engine2.cpp | 3 + firmware/controllers/algo/fuel_math.cpp | 2 +- .../engine_cycle/main_trigger_callback.cpp | 31 ---------- firmware/integration/rusefi_config.txt | 2 +- firmware/tunerstudio/rusefi.input | 61 +++++++++++++------ 8 files changed, 50 insertions(+), 55 deletions(-) diff --git a/firmware/console/binary/tunerstudio_outputs.h b/firmware/console/binary/tunerstudio_outputs.h index 9f9a515a81..1bc4dbc202 100644 --- a/firmware/console/binary/tunerstudio_outputs.h +++ b/firmware/console/binary/tunerstudio_outputs.h @@ -127,7 +127,7 @@ typedef struct { scaled_percent iatCorrection; // 64 scaled_percent cltCorrection; // 66 scaled_percent baroCorrection; // 68 - scaled_ms fuelPidCorrection; // 70 + scaled_percent shortTermFuelTrim; // 70 // Wall model AE scaled_ms wallFuelAmount; // 72 diff --git a/firmware/console/binary_log/binary_logging.cpp b/firmware/console/binary_log/binary_logging.cpp index 658325666c..fd909cbb7b 100644 --- a/firmware/console/binary_log/binary_logging.cpp +++ b/firmware/console/binary_log/binary_logging.cpp @@ -27,7 +27,7 @@ static const LogField fields[] = { {tsOutputChannels.veValue, GAUGE_NAME_FUEL_VE, "%", 1}, {tsOutputChannels.tCharge, "tCharge", "C", 1}, {tsOutputChannels.injectorLagMs, GAUGE_NAME_INJECTOR_LAG, "ms", 3}, - {tsOutputChannels.fuelPidCorrection, GAUGE_NAME_FUEL_PID_CORR, "ms", 3}, + {tsOutputChannels.shortTermFuelTrim, GAUGE_NAME_FUEL_PID_CORR, "%", 3}, {tsOutputChannels.wallFuelCorrection, GAUGE_NAME_FUEL_WALL_CORRECTION, "ms", 3}, {tsOutputChannels.tpsAccelFuel, GAUGE_NAME_FUEL_TPS_EXTRA, "ms", 3}, {tsOutputChannels.ignitionAdvance, GAUGE_NAME_TIMING_ADVANCE, "deg", 1}, diff --git a/firmware/console/status_loop.cpp b/firmware/console/status_loop.cpp index 728e31835d..2ed994334b 100644 --- a/firmware/console/status_loop.cpp +++ b/firmware/console/status_loop.cpp @@ -582,7 +582,7 @@ void updateTunerStudioState(TunerStudioOutputChannels *tsOutputChannels DECLARE_ // 120 tsOutputChannels->firmwareVersion = getRusEfiVersion(); // 268 - tsOutputChannels->fuelPidCorrection = ENGINE(engineState.running.pidCorrection); + tsOutputChannels->shortTermFuelTrim = 100.0f * (ENGINE(engineState.running.pidCorrection) - 1.0f); // 276 tsOutputChannels->accelerationX = engine->sensors.accelerometer.x; // 278 diff --git a/firmware/controllers/algo/engine2.cpp b/firmware/controllers/algo/engine2.cpp index 8db251a9f1..1b2bd890b6 100644 --- a/firmware/controllers/algo/engine2.cpp +++ b/firmware/controllers/algo/engine2.cpp @@ -16,6 +16,7 @@ #include "advance_map.h" #include "aux_valves.h" #include "perf_trace.h" +#include "closed_loop_fuel.h" #include "sensor.h" #if EFI_PROD_CODE @@ -146,6 +147,8 @@ void EngineState::periodicFastCallback(DECLARE_ENGINE_PARAMETER_SIGNATURE) { // todo: move this into slow callback, no reason for CLT corr to be here running.coolantTemperatureCoefficient = getCltFuelCorrection(PASS_ENGINE_PARAMETER_SIGNATURE); + running.pidCorrection = fuelClosedLoopCorrection(PASS_ENGINE_PARAMETER_SIGNATURE); + // update fuel consumption states fuelConsumption.update(nowNt PASS_ENGINE_PARAMETER_SUFFIX); diff --git a/firmware/controllers/algo/fuel_math.cpp b/firmware/controllers/algo/fuel_math.cpp index 4c13d6d1a0..d73a3c48f0 100644 --- a/firmware/controllers/algo/fuel_math.cpp +++ b/firmware/controllers/algo/fuel_math.cpp @@ -125,7 +125,7 @@ floatms_t getRunningFuel(floatms_t baseFuel DECLARE_ENGINE_PARAMETER_SUFFIX) { 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 * iatCorrection * cltCorrection * postCrankingFuelCorrection * ENGINE(engineState.running.pidCorrection); efiAssert(CUSTOM_ERR_ASSERT, !cisnan(runningFuel), "NaN runningFuel", 0); DISPLAY_TEXT(eol); diff --git a/firmware/controllers/engine_cycle/main_trigger_callback.cpp b/firmware/controllers/engine_cycle/main_trigger_callback.cpp index 35f7ecffba..1457bd835d 100644 --- a/firmware/controllers/engine_cycle/main_trigger_callback.cpp +++ b/firmware/controllers/engine_cycle/main_trigger_callback.cpp @@ -58,9 +58,6 @@ static const char *prevOutputName = nullptr; static InjectionEvent primeInjEvent; static Logging *logger; -#if ! EFI_UNIT_TEST -static Pid fuelPid(&persistentState.persistentConfiguration.engineConfiguration.fuelClosedLoopPid); -#endif // todo: figure out if this even helps? //#if defined __GNUC__ @@ -284,30 +281,6 @@ void handleFuelInjectionEvent(int injEventIndex, InjectionEvent *event, #endif /* EFI_DEFAILED_LOGGING */ } -static void fuelClosedLoopCorrection(DECLARE_ENGINE_PARAMETER_SIGNATURE) { -#if ! EFI_UNIT_TEST - if (GET_RPM_VALUE < CONFIG(fuelClosedLoopRpmThreshold) || - Sensor::get(SensorType::Clt).value_or(0) < CONFIG(fuelClosedLoopCltThreshold) || - Sensor::get(SensorType::Tps1).value_or(100) > CONFIG(fuelClosedLoopTpsThreshold) || - ENGINE(sensors.currentAfr) < CONFIG(fuelClosedLoopAfrLowThreshold) || - ENGINE(sensors.currentAfr) > engineConfiguration->fuelClosedLoopAfrHighThreshold) { - engine->engineState.running.pidCorrection = 0; - fuelPid.reset(); - return; - } - - engine->engineState.running.pidCorrection = fuelPid.getOutput(ENGINE(engineState.targetAFR), ENGINE(sensors.currentAfr), NOT_TIME_BASED_PID); - if (engineConfiguration->debugMode == DBG_FUEL_PID_CORRECTION) { -#if EFI_TUNER_STUDIO - tsOutputChannels.debugFloatField1 = engine->engineState.running.pidCorrection; - fuelPid.postState(&tsOutputChannels); -#endif /* EFI_TUNER_STUDIO */ - } - -#endif -} - - static ALWAYS_INLINE void handleFuel(const bool limitedFuel, uint32_t trgEventIndex, int rpm, efitick_t nowNt DECLARE_ENGINE_PARAMETER_SUFFIX) { ScopePerf perf(PE::HandleFuel); @@ -442,10 +415,6 @@ static void mainTriggerCallback(trigger_event_e ckpSignalType, uint32_t trgEvent // we need this to apply new 'triggerIndexByAngle' values engine->periodicFastCallback(PASS_ENGINE_PARAMETER_SIGNATURE); } - - if (CONFIG(fuelClosedLoopCorrectionEnabled)) { - fuelClosedLoopCorrection(PASS_ENGINE_PARAMETER_SIGNATURE); - } } efiAssertVoid(CUSTOM_IGN_MATH_STATE, !CONFIG(useOnlyRisingEdgeForTrigger) || CONFIG(ignMathCalculateAtIndex) % 2 == 0, "invalid ignMathCalculateAtIndex"); diff --git a/firmware/integration/rusefi_config.txt b/firmware/integration/rusefi_config.txt index 877c7749ec..8a7fa00a12 100644 --- a/firmware/integration/rusefi_config.txt +++ b/firmware/integration/rusefi_config.txt @@ -1484,7 +1484,7 @@ end_struct #define GAUGE_NAME_FUEL_RUNNING "fuel: running" #define GAUGE_NAME_FUEL_LAST_INJECTION "fuel: Last injection" #define GAUGE_NAME_FUEL_BASE "fuel: base" -#define GAUGE_NAME_FUEL_PID_CORR "fuel: Short-term closed loop correction" +#define GAUGE_NAME_FUEL_PID_CORR "fuel: Short-term fuel trim" #define GAUGE_NAME_FUEL_WALL_AMOUNT "fuel: wall amount" #define GAUGE_NAME_FUEL_WALL_CORRECTION "fuel: wall corr ms" diff --git a/firmware/tunerstudio/rusefi.input b/firmware/tunerstudio/rusefi.input index dd6d04b021..7c8af2ccf5 100644 --- a/firmware/tunerstudio/rusefi.input +++ b/firmware/tunerstudio/rusefi.input @@ -264,7 +264,7 @@ enable2ndByteCanID = false iatCorrection = scalar, S16, 64, "%",{1/@@PACK_MULT_PERCENT@@}, 0 cltCorrection = scalar, S16, 66, "%",{1/@@PACK_MULT_PERCENT@@}, 0 baroCorrection = scalar, S16, 68, "%",{1/@@PACK_MULT_PERCENT@@}, 0 - fuelPidCorrection=scalar, S16, 70, "ms",{1/@@PACK_MULT_MS@@}, 0 + fuelPidCorrection=scalar, S16, 70, "ms",{1/@@PACK_MULT_PERCENT@@}, 0 ; Wall model AE wallFuelAmount = scalar, U16, 72, "ms",{1/@@PACK_MULT_MS@@}, 0 @@ -999,7 +999,7 @@ gaugeCategory = Fueling injectorLagMsGauge = injectorLagMs, @@GAUGE_NAME_INJECTOR_LAG@@, "mSec", 0, 25.5, 1.0, 1.2, 20, 25, 3, 1 fuelRunningGauge = fuelRunning, @@GAUGE_NAME_FUEL_RUNNING@@, "mSec", 0, 25.5, 1.0, 1.2, 20, 25, 3, 1 baseFuelGauge = baseFuel, @@GAUGE_NAME_FUEL_BASE@@, "mSec", 0, 25.5, 1.0, 1.2, 20, 25, 3, 1 - fuelPidCorrectionGauge = fuelPidCorrection, @@GAUGE_NAME_FUEL_PID_CORR@@, "mSec", -11, 11, 1.0, 1.2, 20, 25, 3, 1 + fuelPidCorrectionGauge = fuelPidCorrection, @@GAUGE_NAME_FUEL_PID_CORR@@, "%", -10, 10, -8, -5, 5, 8, 3, 1 gaugeCategory = Throttle Body (incl. ETB) pedalPositionGauge = throttlePedalPosition, @@GAUGE_NAME_THROTTLE_PEDAL@@, "%", 0, 120, 0, 0, 100, 100, 1, 1 @@ -1271,7 +1271,7 @@ menuDialog = main # Targets & closed loop subMenu = afrTableTbl, "Target AFR", 0, {isInjectionEnabled == 1 && (fuelAlgorithm == LM_SPEED_DENSITY || fuelAlgorithm == LM_REAL_MAF)} - subMenu = fuelClosedLoopDialog, "Closed loop correction", 0, {isInjectionEnabled == 1} + subMenu = fuelClosedLoopDialog, "Closed loop fuel correction", 0, {isInjectionEnabled == 1} subMenu = coastingFuelCutControl, "Deceleration fuel cutoff (DFCO)", 0, {isInjectionEnabled == 1} subMenu = std_separator @@ -2433,23 +2433,46 @@ cmd_set_engine_type_default = "w\x00\x31\x00\x00" field = "SPI3sck mode", spi3SckMode, {is_enabled_spi_3 == 1} field = "LIS302DLCsPin", LIS302DLCsPin - dialog = fuelClosedLoopDialog, "Fuel short-term closed-loop correction" - field = "#This is just the cherry on the cake once you have good VE table" - field = "Enabled", fuelClosedLoopCorrectionEnabled - field = "!No correction below these value" - field = "CLT - low threshod", fuelClosedLoopCltThreshold, {fuelClosedLoopCorrectionEnabled == 1} - field = "RPM - low threshod", fuelClosedLoopRpmThreshold, {fuelClosedLoopCorrectionEnabled == 1} - field = "EGO - low threshod", fuelClosedLoopAfrLowThreshold, {fuelClosedLoopCorrectionEnabled == 1} - field = "!No correction above these value" - field = "TPS - high threshod", fuelClosedLoopTpsThreshold, {fuelClosedLoopCorrectionEnabled == 1} - field = "EGO - high threshod", fuelClosedLoopAfrHighThreshold, {fuelClosedLoopCorrectionEnabled == 1} - field = "!PID settings" - field = "P factor", fuelClosedLoopPid_pFactor, {fuelClosedLoopCorrectionEnabled == 1} - field = "I factor", fuelClosedLoopPid_iFactor, {fuelClosedLoopCorrectionEnabled == 1} - field = "D factor", fuelClosedLoopPid_dFactor, {fuelClosedLoopCorrectionEnabled == 1} - field = "min", fuelClosedLoopPid_minValue, {fuelClosedLoopCorrectionEnabled == 1} - field = "max", fuelClosedLoopPid_maxValue, {fuelClosedLoopCorrectionEnabled == 1} + dialog = stftPartitioning, "Region Configuration" + field = "Idle region RPM", stft_maxIdleRegionRpm + field = "Overrun region load", stft_maxOverrunLoad + field = "Power region load", stft_minPowerLoad + dialog = stftPartitionSettingsMain, "Main Region", xAxis + field = "Time Const", stft_cellCfgs4_timeConstant + field = "Max add", stft_cellCfgs4_maxAdd + field = "Max remove", stft_cellCfgs4_maxRemove + + dialog = stftPartitionSettingsIdle, "Idle Region", xAxis + field = "Time Const", stft_cellCfgs1_timeConstant + field = "Max add", stft_cellCfgs1_maxAdd + field = "Max remove", stft_cellCfgs1_maxRemove + + dialog = stftPartitionSettingsPower, "Power Region", xAxis + field = "Time Const", stft_cellCfgs3_timeConstant + field = "Max add", stft_cellCfgs3_maxAdd + field = "Max remove", stft_cellCfgs3_maxRemove + + dialog = stftPartitionSettingsOverrun, "Overrun Region", xAxis + field = "Time Const", stft_cellCfgs2_timeConstant + field = "Max add", stft_cellCfgs2_maxAdd + field = "Max remove", stft_cellCfgs2_maxRemove + + dialog = fuelClosedLoopDialog, "Short-term fuel trim" + field = "Enabled", fuelClosedLoopCorrectionEnabled + + field = "Startup delay" stft_startupDelay, {fuelClosedLoopCorrectionEnabled == 1} + field = "Minimum CLT for correction", stft_minClt, {fuelClosedLoopCorrectionEnabled == 1} + field = "Minimum AFR for correction", stft_minAfr, {fuelClosedLoopCorrectionEnabled == 1} + field = "Maximum AFR for correction", stft_maxAfr, {fuelClosedLoopCorrectionEnabled == 1} + field = "Adjustment deadband", stft_deadband, {fuelClosedLoopCorrectionEnabled == 1} + field = "Ignore error magnitude", stftIgnoreErrorMagnitude, {fuelClosedLoopCorrectionEnabled == 1} + + panel = stftPartitioning, {fuelClosedLoopCorrectionEnabled == 1} + panel = stftPartitionSettingsMain, {fuelClosedLoopCorrectionEnabled == 1} + panel = stftPartitionSettingsIdle, {fuelClosedLoopCorrectionEnabled == 1} + panel = stftPartitionSettingsPower, {fuelClosedLoopCorrectionEnabled == 1} + panel = stftPartitionSettingsOverrun, {fuelClosedLoopCorrectionEnabled == 1} dialog = auxPidDialog, "Aux PID" field = "Enabled", activateAuxPid1