| Line | Branch | Decision | Exec | Source |
|---|---|---|---|---|
| 1 | #include "pch.h" | |||
| 2 | ||||
| 3 | #include "closed_loop_fuel.h" | |||
| 4 | #include "tunerstudio.h" | |||
| 5 | ||||
| 6 | #if EFI_ENGINE_CONTROL | |||
| 7 | ||||
| 8 | 4696 | SensorType ShortTermFuelTrim::getSensorForBankIndex(size_t index) { | ||
| 9 |
2/3✓ Branch 0 taken 2348 times.
✓ Branch 1 taken 2348 times.
✗ Branch 2 not taken.
|
4696 | switch (index) { | |
| 10 |
1/1✓ Decision 'true' taken 2348 times.
|
2348 | case 0: return SensorType::Lambda1; | |
| 11 |
1/1✓ Decision 'true' taken 2348 times.
|
2348 | case 1: return SensorType::Lambda2; | |
| 12 | ✗ | default: return SensorType::Invalid; | ||
| 13 | } | |||
| 14 | } | |||
| 15 | ||||
| 16 | 12 | size_t ShortTermFuelTrim::computeStftBin(float rpm, float load, stft_s& cfg) { | ||
| 17 | // Low RPM -> idle | |||
| 18 |
2/2✓ Branch 2 taken 3 times.
✓ Branch 3 taken 9 times.
|
2/2✓ Decision 'true' taken 3 times.
✓ Decision 'false' taken 9 times.
|
12 | if (idleDeadband.lt(rpm, cfg.maxIdleRegionRpm)) |
| 19 | { | |||
| 20 | 3 | return 0; | ||
| 21 | } | |||
| 22 | ||||
| 23 | // Low load -> overrun | |||
| 24 |
2/2✓ Branch 1 taken 3 times.
✓ Branch 2 taken 6 times.
|
2/2✓ Decision 'true' taken 3 times.
✓ Decision 'false' taken 6 times.
|
9 | if (overrunDeadband.lt(load, cfg.maxOverrunLoad)) |
| 25 | { | |||
| 26 | 3 | return 1; | ||
| 27 | } | |||
| 28 | ||||
| 29 | // High load -> power | |||
| 30 |
2/2✓ Branch 1 taken 3 times.
✓ Branch 2 taken 3 times.
|
2/2✓ Decision 'true' taken 3 times.
✓ Decision 'false' taken 3 times.
|
6 | if (loadDeadband.gt(load, cfg.minPowerLoad)) |
| 31 | { | |||
| 32 | 3 | return 2; | ||
| 33 | } | |||
| 34 | ||||
| 35 | // Default -> normal "in the middle" cell | |||
| 36 | 3 | return 3; | ||
| 37 | } | |||
| 38 | ||||
| 39 | 1101 | stft_state_e ShortTermFuelTrim::getCorrectionState() { | ||
| 40 | 1101 | const auto& cfg = engineConfiguration->stft; | ||
| 41 | ||||
| 42 | // User disable bit | |||
| 43 |
1/2✓ Branch 0 taken 1101 times.
✗ Branch 1 not taken.
|
1/2✓ Decision 'true' taken 1101 times.
✗ Decision 'false' not taken.
|
1101 | if (!engineConfiguration->fuelClosedLoopCorrectionEnabled) { |
| 44 | 1101 | return stftDisabledSettings; | ||
| 45 | } | |||
| 46 | ||||
| 47 | // Don't correct if tuning seems to be happening | |||
| 48 | ✗ | if (checkIfTuningVeNow()) { | ||
| 49 | ✗ | return stftDisabledTuning; | ||
| 50 | } | |||
| 51 | ||||
| 52 | // Don't correct if not running | |||
| 53 | ✗ | if (!engine->rpmCalculator.isRunning()) { | ||
| 54 | ✗ | return stftDisabledRPM; | ||
| 55 | } | |||
| 56 | ||||
| 57 | // Startup delay - allow O2 sensor to warm up, etc | |||
| 58 | ✗ | if (cfg.startupDelay > engine->fuelComputer.running.timeSinceCrankingInSecs) { | ||
| 59 | ✗ | return stftDisabledCrankingDelay; | ||
| 60 | } | |||
| 61 | ||||
| 62 | // Check that the engine is hot enough (and clt not failed) | |||
| 63 | ✗ | auto clt = Sensor::get(SensorType::Clt); | ||
| 64 | ✗ | if (!clt.Valid || clt.Value < cfg.minClt) { | ||
| 65 | ✗ | return stftDisabledClt; | ||
| 66 | } | |||
| 67 | ||||
| 68 | // If all was well, then we're enabled! | |||
| 69 | ✗ | return stftEnabled; | ||
| 70 | } | |||
| 71 | ||||
| 72 | 3 | stft_state_e ShortTermFuelTrim::getLearningState(SensorType sensor) { | ||
| 73 | 3 | const auto& cfg = engineConfiguration->stft; | ||
| 74 | ||||
| 75 | // TODO: add check for stftLearningDisabledSettings | |||
| 76 | ||||
| 77 | // Pause (but don't reset) correction if the AFR is off scale. | |||
| 78 | // It's probably a transient and poorly tuned transient correction | |||
| 79 | // TODO: use getStoichiometricRatio() instead of STOICH_RATIO | |||
| 80 | 3 | auto afr = Sensor::getOrZero(sensor) * STOICH_RATIO; | ||
| 81 |
7/8✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 1 time.
✓ Branch 6 taken 1 time.
✓ Branch 7 taken 1 time.
✓ Branch 8 taken 2 times.
✓ Branch 9 taken 1 time.
|
2/2✓ Decision 'true' taken 2 times.
✓ Decision 'false' taken 1 time.
|
3 | if (!afr || afr < cfg.minAfr || afr > cfg.maxAfr) { |
| 82 | 2 | return stftDisabledAfrOurOfRange; | ||
| 83 | } | |||
| 84 | ||||
| 85 | // Pause correction if DFCO was active recently | |||
| 86 | 1 | auto timeSinceDfco = engine->module<DfcoController>()->getTimeSinceCut(); | ||
| 87 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
|
1/2✗ Decision 'true' not taken.
✓ Decision 'false' taken 1 time.
|
1 | if (timeSinceDfco < engineConfiguration->noFuelTrimAfterDfcoTime) { |
| 88 | ✗ | return stftDisabledDFCO; | ||
| 89 | } | |||
| 90 | ||||
| 91 | // Pause correction if Accel enrichment was active recently | |||
| 92 | 1 | auto timeSinceAccel = engine->module<TpsAccelEnrichment>()->getTimeSinceAcell(); | ||
| 93 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
|
1/2✗ Decision 'true' not taken.
✓ Decision 'false' taken 1 time.
|
1 | if (timeSinceAccel < engineConfiguration->noFuelTrimAfterAccelTime) { |
| 94 | ✗ | return stftDisabledTpsAccel; | ||
| 95 | } | |||
| 96 | ||||
| 97 | // Pause if some other cut was active recently | |||
| 98 | 1 | auto timeSinceFuelCut = engine->module<LimpManager>()->getTimeSinceAnyCut(); | ||
| 99 | // TODO: should duration this be configurable? | |||
| 100 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
|
1/2✗ Decision 'true' not taken.
✓ Decision 'false' taken 1 time.
|
1 | if (timeSinceFuelCut < 2) { |
| 101 | ✗ | return stftDisabledFuelCut; | ||
| 102 | } | |||
| 103 | ||||
| 104 | 1 | return stftEnabled; | ||
| 105 | } | |||
| 106 | ||||
| 107 | 587 | void ShortTermFuelTrim::init(stft_s *stftCfg) { | ||
| 108 |
2/2✓ Branch 0 taken 1174 times.
✓ Branch 1 taken 587 times.
|
2/2✓ Decision 'true' taken 1174 times.
✓ Decision 'false' taken 587 times.
|
1761 | for (size_t bank = 0; bank < FT_BANK_COUNT; bank++) { |
| 109 |
2/2✓ Branch 0 taken 4696 times.
✓ Branch 1 taken 1174 times.
|
2/2✓ Decision 'true' taken 4696 times.
✓ Decision 'false' taken 1174 times.
|
5870 | for (size_t bin = 0; bin < STFT_CELL_COUNT; bin++) { |
| 110 | 4696 | auto& cell = banks[bank].cells[bin]; | ||
| 111 | 4696 | SensorType sensor = getSensorForBankIndex(bank); | ||
| 112 | ||||
| 113 | 4696 | cell.configure(&stftCfg->cellCfgs[bin], sensor); | ||
| 114 | } | |||
| 115 | } | |||
| 116 | 587 | } | ||
| 117 | ||||
| 118 | 1101 | ClosedLoopFuelResult ShortTermFuelTrim::getCorrection(float rpm, float fuelLoad) { | ||
| 119 |
1/1✓ Branch 1 taken 1101 times.
|
1101 | stftCorrectionState = getCorrectionState(); | |
| 120 |
1/2✓ Branch 0 taken 1101 times.
✗ Branch 1 not taken.
|
1/2✓ Decision 'true' taken 1101 times.
✗ Decision 'false' not taken.
|
1101 | if (stftCorrectionState != stftEnabled) { |
| 121 | // Learning is also prohibited | |||
| 122 |
2/2✓ Branch 0 taken 2202 times.
✓ Branch 1 taken 1101 times.
|
2/2✓ Decision 'true' taken 2202 times.
✓ Decision 'false' taken 1101 times.
|
3303 | for (size_t bank = 0; bank < FT_BANK_COUNT; bank++) { |
| 123 | 2202 | stftLearningState[bank] = stftCorrectionState; | ||
| 124 | } | |||
| 125 | 1101 | return {}; | ||
| 126 | } | |||
| 127 | ||||
| 128 | ✗ | stftCorrectionBinIdx = computeStftBin(rpm, fuelLoad, engineConfiguration->stft); | ||
| 129 | ||||
| 130 | ✗ | ClosedLoopFuelResult result; | ||
| 131 | ||||
| 132 | ✗ | for (size_t bank = 0; bank < FT_BANK_COUNT; bank++) { | ||
| 133 | ✗ | auto& cell = banks[bank].cells[stftCorrectionBinIdx]; | ||
| 134 | ||||
| 135 | ✗ | SensorType sensor = getSensorForBankIndex(bank); | ||
| 136 | ||||
| 137 | ✗ | stftLearningState[bank] = getLearningState(sensor); | ||
| 138 | ✗ | if (stftLearningState[bank] == stftEnabled) { | ||
| 139 | ✗ | stftInputError[bank] = cell.getLambdaError(); | ||
| 140 | ✗ | cell.update(engineConfiguration->stft.deadband * 0.01f, engineConfiguration->stftIgnoreErrorMagnitude); | ||
| 141 | ✗ | stftLearningBinIdx = stftCorrectionBinIdx; | ||
| 142 | } | |||
| 143 | ||||
| 144 | ✗ | result.banks[bank] = cell.getAdjustment(); | ||
| 145 | } | |||
| 146 | ||||
| 147 | ✗ | return result; | ||
| 148 | } | |||
| 149 | ||||
| 150 | 1086 | void ShortTermFuelTrim::onSlowCallback() { | ||
| 151 | // Do some magic math here? | |||
| 152 | 1086 | } | ||
| 153 | ||||
| 154 | 1002 | bool ShortTermFuelTrim::needsDelayedShutoff() { | ||
| 155 | 1002 | return false; | ||
| 156 | } | |||
| 157 | ||||
| 158 | 585 | void initStft(void) | ||
| 159 | { | |||
| 160 | 585 | engine->module<ShortTermFuelTrim>()->init(&engineConfiguration->stft); | ||
| 161 | 585 | } | ||
| 162 | ||||
| 163 | /* TODO: move out of here */ | |||
| 164 | 17 | bool checkIfTuningVeNow() { | ||
| 165 | #if EFI_TUNER_STUDIO | |||
| 166 | 17 | const bool result = isTuningVeNow(); | ||
| 167 | #else | |||
| 168 | const bool result = false; | |||
| 169 | #endif /* EFI_TUNER_STUDIO */ | |||
| 170 | 17 | engine->outputChannels.isTuningNow = result; | ||
| 171 | 17 | return result; | ||
| 172 | } | |||
| 173 | ||||
| 174 | #endif // EFI_ENGINE_CONTROL | |||
| 175 |