2015-07-10 06:01:56 -07:00
|
|
|
/**
|
|
|
|
* @file fuel_math.cpp
|
|
|
|
* @brief Fuel amount calculation logic
|
|
|
|
*
|
|
|
|
* While engine running, fuel amount is an interpolated value from the fuel map by getRpm() and getEngineLoad()
|
|
|
|
* On top of the value from the fuel map we also apply
|
|
|
|
* <BR>1) getInjectorLag() correction to account for fuel injector lag
|
2017-03-06 22:37:07 -08:00
|
|
|
* <BR>2) getCltFuelCorrection() for warm-up
|
2015-07-10 06:01:56 -07:00
|
|
|
* <BR>3) getIatCorrection() to account for cold weather
|
|
|
|
*
|
|
|
|
* getCrankingFuel() depents only on getCoolantTemperature()
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @date May 27, 2013
|
2017-01-03 03:05:22 -08:00
|
|
|
* @author Andrey Belomutskiy, (c) 2012-2017
|
2015-07-10 06:01:56 -07:00
|
|
|
*
|
|
|
|
* This file is part of rusEfi - see http://rusefi.com
|
|
|
|
*
|
|
|
|
* rusEfi is free software; you can redistribute it and/or modify it under the terms of
|
|
|
|
* the GNU General Public License as published by the Free Software Foundation; either
|
|
|
|
* version 3 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* rusEfi is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
|
|
|
|
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with this program.
|
|
|
|
* If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "main.h"
|
|
|
|
#include "fuel_math.h"
|
|
|
|
#include "interpolation.h"
|
|
|
|
#include "engine_configuration.h"
|
|
|
|
#include "allsensors.h"
|
|
|
|
#include "engine_math.h"
|
|
|
|
#include "rpm_calculator.h"
|
|
|
|
#include "speed_density.h"
|
|
|
|
|
|
|
|
EXTERN_ENGINE
|
|
|
|
;
|
|
|
|
|
2016-08-26 16:02:56 -07:00
|
|
|
fuel_Map3D_t fuelMap("fuel");
|
2015-12-27 09:01:53 -08:00
|
|
|
static fuel_Map3D_t fuelPhaseMap("fl ph");
|
2015-07-10 06:01:56 -07:00
|
|
|
extern fuel_Map3D_t ve2Map;
|
2016-07-01 16:01:44 -07:00
|
|
|
extern afr_Map3D_t afrMap;
|
2015-07-10 06:01:56 -07:00
|
|
|
extern baroCorr_Map3D_t baroCorrMap;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return total duration of fuel injection per engine cycle, in milliseconds
|
|
|
|
*/
|
2017-05-15 20:28:49 -07:00
|
|
|
float getRealMafFuel(float airSpeed, int rpm DECLARE_ENGINE_PARAMETER_SUFFIX) {
|
2015-07-10 06:01:56 -07:00
|
|
|
if (rpm == 0)
|
|
|
|
return 0;
|
|
|
|
// duration of engine cycle, in hours
|
|
|
|
float engineCycleDurationHr = 1.0 / 60 / rpm;
|
|
|
|
|
|
|
|
float airMassKg = airSpeed * engineCycleDurationHr;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* todo: pre-calculate gramm/second injector flow to save one multiplication
|
|
|
|
* open question if that's needed since that's just a multiplication
|
|
|
|
*/
|
|
|
|
float injectorFlowRate = cc_minute_to_gramm_second(engineConfiguration->injector.flow);
|
|
|
|
|
2016-03-11 12:01:58 -08:00
|
|
|
float afr = afrMap.getValue(rpm, airSpeed);
|
2015-07-10 06:01:56 -07:00
|
|
|
float fuelMassGramm = airMassKg / afr * 1000;
|
|
|
|
|
|
|
|
return 1000 * fuelMassGramm / injectorFlowRate;
|
|
|
|
}
|
|
|
|
|
2015-12-26 09:03:13 -08:00
|
|
|
// todo: rename this method since it's now base+TPSaccel
|
2017-05-15 20:28:49 -07:00
|
|
|
floatms_t getBaseFuel(int rpm DECLARE_ENGINE_PARAMETER_SUFFIX) {
|
|
|
|
floatms_t tpsAccelEnrich = ENGINE(tpsAccelEnrichment.getTpsEnrichment(PASS_ENGINE_PARAMETER_SIGNATURE));
|
2017-04-12 15:38:18 -07:00
|
|
|
efiAssert(!cisnan(tpsAccelEnrich), "NaN tpsAccelEnrich", 0);
|
|
|
|
ENGINE(engineState.tpsAccelEnrich) = tpsAccelEnrich;
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2017-04-12 19:59:59 -07:00
|
|
|
floatms_t baseFuel;
|
2016-08-28 13:02:34 -07:00
|
|
|
if (CONFIG(fuelAlgorithm) == LM_SPEED_DENSITY) {
|
2017-05-15 20:28:49 -07:00
|
|
|
baseFuel = getSpeedDensityFuel(PASS_ENGINE_PARAMETER_SIGNATURE);
|
2017-04-12 20:25:58 -07:00
|
|
|
efiAssert(!cisnan(baseFuel), "NaN sd baseFuel", 0);
|
2016-08-28 13:02:34 -07:00
|
|
|
} else if (engineConfiguration->fuelAlgorithm == LM_REAL_MAF) {
|
2017-05-15 20:28:49 -07:00
|
|
|
float maf = getRealMaf(PASS_ENGINE_PARAMETER_SIGNATURE) + engine->engineLoadAccelEnrichment.getEngineLoadEnrichment(PASS_ENGINE_PARAMETER_SIGNATURE);
|
|
|
|
baseFuel = getRealMafFuel(maf, rpm PASS_ENGINE_PARAMETER_SUFFIX);
|
2017-04-12 20:25:58 -07:00
|
|
|
efiAssert(!cisnan(baseFuel), "NaN rm baseFuel", 0);
|
2015-07-10 06:01:56 -07:00
|
|
|
} else {
|
2017-04-12 19:59:59 -07:00
|
|
|
baseFuel = engine->engineState.baseTableFuel;
|
2017-04-12 20:25:58 -07:00
|
|
|
efiAssert(!cisnan(baseFuel), "NaN bt baseFuel", 0);
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
2017-04-12 19:59:59 -07:00
|
|
|
engine->engineState.baseFuel = baseFuel;
|
2015-12-02 17:10:06 -08:00
|
|
|
|
2017-04-12 19:59:59 -07:00
|
|
|
return tpsAccelEnrich + baseFuel;
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
2017-05-15 20:28:49 -07:00
|
|
|
angle_t getinjectionOffset(float rpm DECLARE_ENGINE_PARAMETER_SUFFIX) {
|
|
|
|
float engineLoad = getEngineLoadT(PASS_ENGINE_PARAMETER_SIGNATURE);
|
2016-08-28 14:02:14 -07:00
|
|
|
angle_t result = fuelPhaseMap.getValue(rpm, engineLoad) + CONFIG(extraInjectionOffset);
|
2016-12-05 19:01:54 -08:00
|
|
|
fixAngle(result, "inj offset");
|
2016-08-28 14:02:14 -07:00
|
|
|
return result;
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Number of injections into each cylinder per engine cycle
|
2017-03-06 22:28:26 -08:00
|
|
|
* @see getNumberOfSparks
|
2015-07-10 06:01:56 -07:00
|
|
|
*/
|
2017-05-15 20:28:49 -07:00
|
|
|
int getNumberOfInjections(injection_mode_e mode DECLARE_ENGINE_PARAMETER_SUFFIX) {
|
2015-07-10 06:01:56 -07:00
|
|
|
switch (mode) {
|
|
|
|
case IM_SIMULTANEOUS:
|
|
|
|
return engineConfiguration->specs.cylindersCount;
|
|
|
|
case IM_SEQUENTIAL:
|
|
|
|
return 1;
|
|
|
|
case IM_BATCH:
|
2016-07-23 18:04:30 -07:00
|
|
|
return 2;
|
2015-07-10 06:01:56 -07:00
|
|
|
default:
|
2016-10-10 13:02:39 -07:00
|
|
|
firmwareError(CUSTOM_ERR_INVALID_INJECTION_MODE, "Unexpected injection_mode_e %d", mode);
|
2015-07-10 06:01:56 -07:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-06 22:28:26 -08:00
|
|
|
/**
|
|
|
|
* @see getCoilDutyCycle
|
|
|
|
*/
|
2017-05-15 20:28:49 -07:00
|
|
|
percent_t getInjectorDutyCycle(int rpm DECLARE_ENGINE_PARAMETER_SUFFIX) {
|
|
|
|
floatms_t totalPerCycle = getInjectionDuration(rpm PASS_ENGINE_PARAMETER_SUFFIX) * getNumberOfInjections(engineConfiguration->injectionMode PASS_ENGINE_PARAMETER_SUFFIX);
|
|
|
|
floatms_t engineCycleDuration = getEngineCycleDuration(rpm PASS_ENGINE_PARAMETER_SUFFIX);
|
2016-07-23 18:04:30 -07:00
|
|
|
return 100 * totalPerCycle / engineCycleDuration;
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-08-23 19:01:55 -07:00
|
|
|
* @returns Length of each individual fuel injection, in milliseconds
|
2015-07-10 06:01:56 -07:00
|
|
|
*/
|
2017-05-15 20:28:49 -07:00
|
|
|
floatms_t getInjectionDuration(int rpm DECLARE_ENGINE_PARAMETER_SUFFIX) {
|
2015-07-10 06:01:56 -07:00
|
|
|
float theoreticalInjectionLength;
|
2017-04-12 16:58:29 -07:00
|
|
|
bool isCranking = isCrankingR(rpm);
|
|
|
|
int numberOfCylinders = getNumberOfInjections(isCranking ?
|
|
|
|
engineConfiguration->crankingInjectionMode :
|
2017-05-15 20:28:49 -07:00
|
|
|
engineConfiguration->injectionMode PASS_ENGINE_PARAMETER_SUFFIX);
|
2017-04-12 16:58:29 -07:00
|
|
|
if (numberOfCylinders == 0) {
|
2017-04-19 19:03:14 -07:00
|
|
|
warning(CUSTOM_CONFIG_NOT_READY, "config not ready");
|
2017-04-12 16:58:29 -07:00
|
|
|
return 0; // we can end up here during configuration reset
|
|
|
|
}
|
|
|
|
if (isCranking) {
|
2017-05-15 20:28:49 -07:00
|
|
|
theoreticalInjectionLength = getCrankingFuel(PASS_ENGINE_PARAMETER_SIGNATURE) / numberOfCylinders;
|
2017-04-12 17:43:08 -07:00
|
|
|
efiAssert(!cisnan(theoreticalInjectionLength), "NaN cranking theoreticalInjectionLength", 0);
|
2015-07-10 06:01:56 -07:00
|
|
|
} else {
|
2017-05-15 20:28:49 -07:00
|
|
|
floatms_t baseFuel = getBaseFuel(rpm PASS_ENGINE_PARAMETER_SUFFIX);
|
|
|
|
floatms_t fuelPerCycle = getRunningFuel(baseFuel PASS_ENGINE_PARAMETER_SUFFIX);
|
2017-04-12 15:38:18 -07:00
|
|
|
theoreticalInjectionLength = fuelPerCycle / numberOfCylinders;
|
2017-04-12 18:36:57 -07:00
|
|
|
efiAssert(!cisnan(theoreticalInjectionLength), "NaN fuelPerCycle", 0);
|
2016-09-25 21:03:15 -07:00
|
|
|
#if EFI_PRINTF_FUEL_DETAILS || defined(__DOXYGEN__)
|
|
|
|
printf("baseFuel=%f fuelPerCycle=%f theoreticalInjectionLength=%f\t\n",
|
|
|
|
baseFuel, fuelPerCycle, theoreticalInjectionLength);
|
|
|
|
#endif /*EFI_PRINTF_FUEL_DETAILS */
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
2017-04-19 04:58:22 -07:00
|
|
|
floatms_t injectorLag = ENGINE(engineState.injectorLag);
|
|
|
|
if (cisnan(injectorLag)) {
|
2017-05-24 20:42:27 -07:00
|
|
|
warning(CUSTOM_ERR_INJECTOR_LAG, "injectorLag not ready");
|
2017-04-19 04:58:22 -07:00
|
|
|
return 0; // we can end up here during configuration reset
|
|
|
|
}
|
|
|
|
return theoreticalInjectionLength + injectorLag;
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
2017-05-15 20:28:49 -07:00
|
|
|
floatms_t getRunningFuel(floatms_t baseFuel DECLARE_ENGINE_PARAMETER_SUFFIX) {
|
2015-07-10 06:01:56 -07:00
|
|
|
float iatCorrection = ENGINE(engineState.iatFuelCorrection);
|
|
|
|
float cltCorrection = ENGINE(engineState.cltFuelCorrection);
|
2017-04-12 19:59:59 -07:00
|
|
|
efiAssert(!cisnan(iatCorrection), "NaN iatCorrection", 0);
|
|
|
|
efiAssert(!cisnan(cltCorrection), "NaN cltCorrection", 0);
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2017-04-12 17:38:53 -07:00
|
|
|
floatms_t runningFuel = baseFuel * iatCorrection * cltCorrection + ENGINE(engineState.fuelPidCorrection);
|
2017-04-12 17:43:08 -07:00
|
|
|
efiAssert(!cisnan(runningFuel), "NaN runningFuel", 0);
|
2017-04-12 17:38:53 -07:00
|
|
|
ENGINE(engineState.runningFuel) = runningFuel;
|
2015-09-02 19:01:17 -07:00
|
|
|
|
2017-04-12 17:38:53 -07:00
|
|
|
return runningFuel;
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Injector lag correction
|
|
|
|
* @param vBatt Battery voltage.
|
|
|
|
* @return Time in ms for injection opening time based on current battery voltage
|
|
|
|
*/
|
2017-05-15 20:28:49 -07:00
|
|
|
floatms_t getInjectorLag(float vBatt DECLARE_ENGINE_PARAMETER_SUFFIX) {
|
2015-07-10 06:01:56 -07:00
|
|
|
if (cisnan(vBatt)) {
|
|
|
|
warning(OBD_System_Voltage_Malfunction, "vBatt=%f", vBatt);
|
2016-07-24 20:02:52 -07:00
|
|
|
return 0;
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
float vBattCorrection = interpolate2d(vBatt, engineConfiguration->injector.battLagCorrBins,
|
|
|
|
engineConfiguration->injector.battLagCorr, VBAT_INJECTOR_CURVE_SIZE);
|
2016-07-24 20:02:52 -07:00
|
|
|
return vBattCorrection;
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Initialize fuel map data structure
|
|
|
|
* @note this method has nothing to do with fuel map VALUES - it's job
|
|
|
|
* is to prepare the fuel map data structure for 3d interpolation
|
|
|
|
*/
|
2017-05-15 20:28:49 -07:00
|
|
|
void prepareFuelMap(DECLARE_ENGINE_PARAMETER_SIGNATURE) {
|
2015-07-10 06:01:56 -07:00
|
|
|
fuelMap.init(config->fuelTable, config->fuelLoadBins, config->fuelRpmBins);
|
|
|
|
fuelPhaseMap.init(config->injectionPhase, config->injPhaseLoadBins, config->injPhaseRpmBins);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Engine warm-up fuel correction.
|
|
|
|
*/
|
2017-05-15 20:28:49 -07:00
|
|
|
float getCltFuelCorrection(DECLARE_ENGINE_PARAMETER_SIGNATURE) {
|
2017-03-06 23:24:57 -08:00
|
|
|
if (cisnan(engine->sensors.clt))
|
2015-07-10 06:01:56 -07:00
|
|
|
return 1; // this error should be already reported somewhere else, let's just handle it
|
2017-03-06 23:24:57 -08:00
|
|
|
return interpolate2d(engine->sensors.clt, config->cltFuelCorrBins, config->cltFuelCorr, CLT_CURVE_SIZE) / PERCENT_MULT;
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
2017-05-15 20:28:49 -07:00
|
|
|
angle_t getCltTimingCorrection(DECLARE_ENGINE_PARAMETER_SIGNATURE) {
|
2017-03-06 23:24:57 -08:00
|
|
|
if (cisnan(engine->sensors.clt))
|
2017-01-05 18:12:06 -08:00
|
|
|
return 0; // this error should be already reported somewhere else, let's just handle it
|
2017-03-06 23:24:57 -08:00
|
|
|
return interpolate2d(engine->sensors.clt, engineConfiguration->cltTimingBins, engineConfiguration->cltTimingExtra, CLT_TIMING_CURVE_SIZE);
|
2017-01-05 18:12:06 -08:00
|
|
|
}
|
|
|
|
|
2017-05-15 20:28:49 -07:00
|
|
|
float getIatFuelCorrection(float iat DECLARE_ENGINE_PARAMETER_SUFFIX) {
|
2015-07-10 06:01:56 -07:00
|
|
|
if (cisnan(iat))
|
|
|
|
return 1; // this error should be already reported somewhere else, let's just handle it
|
|
|
|
return interpolate2d(iat, config->iatFuelCorrBins, config->iatFuelCorr, IAT_CURVE_SIZE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Fuel injection duration injection as specified in the fuel map, in milliseconds
|
|
|
|
*/
|
2017-04-12 20:25:58 -07:00
|
|
|
floatms_t getBaseTableFuel(int rpm, float engineLoad) {
|
2015-07-10 06:01:56 -07:00
|
|
|
if (cisnan(engineLoad)) {
|
2016-11-02 20:01:48 -07:00
|
|
|
warning(CUSTOM_NAN_ENGINE_LOAD_2, "NaN engine load");
|
2017-04-13 07:43:27 -07:00
|
|
|
return 0;
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
2017-04-13 05:28:41 -07:00
|
|
|
floatms_t result = fuelMap.getValue(rpm, engineLoad);
|
|
|
|
if (cisnan(result)) {
|
|
|
|
// result could be NaN in case of invalid table, like during initialization
|
|
|
|
result = 0;
|
2017-04-13 07:43:27 -07:00
|
|
|
warning(CUSTOM_ERR_FUEL_TABLE_NOT_READY, "baseFuel table not ready");
|
2017-04-13 05:28:41 -07:00
|
|
|
}
|
|
|
|
return result;
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
2017-05-15 20:28:49 -07:00
|
|
|
float getBaroCorrection(DECLARE_ENGINE_PARAMETER_SIGNATURE) {
|
|
|
|
if (hasBaroSensor(PASS_ENGINE_PARAMETER_SIGNATURE)) {
|
|
|
|
return baroCorrMap.getValue(getRpmE(engine), getBaroPressure(PASS_ENGINE_PARAMETER_SIGNATURE));
|
2015-07-10 06:01:56 -07:00
|
|
|
} else {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-02 19:01:17 -07:00
|
|
|
#if EFI_ENGINE_CONTROL || defined(__DOXYGEN__)
|
2015-07-10 06:01:56 -07:00
|
|
|
/**
|
|
|
|
* @return Duration of fuel injection while craning
|
|
|
|
*/
|
2017-05-15 20:28:49 -07:00
|
|
|
floatms_t getCrankingFuel(DECLARE_ENGINE_PARAMETER_SIGNATURE) {
|
|
|
|
return getCrankingFuel3(getCoolantTemperature(PASS_ENGINE_PARAMETER_SIGNATURE),
|
|
|
|
engine->rpmCalculator.getRevolutionCounterSinceStart() PASS_ENGINE_PARAMETER_SUFFIX);
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
floatms_t getCrankingFuel3(float coolantTemperature,
|
2017-05-15 20:28:49 -07:00
|
|
|
uint32_t revolutionCounterSinceStart DECLARE_ENGINE_PARAMETER_SUFFIX) {
|
2015-07-10 06:01:56 -07:00
|
|
|
// these magic constants are in Celsius
|
|
|
|
float baseCrankingFuel = engineConfiguration->cranking.baseFuel;
|
2017-05-07 11:09:40 -07:00
|
|
|
if (cisnan(coolantTemperature)) // todo: move this check down, below duration correction?
|
2015-07-10 06:01:56 -07:00
|
|
|
return baseCrankingFuel;
|
|
|
|
float durationCoef = interpolate2d(revolutionCounterSinceStart, config->crankingCycleBins,
|
|
|
|
config->crankingCycleCoef, CRANKING_CURVE_SIZE);
|
|
|
|
|
2015-08-18 22:01:23 -07:00
|
|
|
float coolantTempCoef = interpolate2d(coolantTemperature, config->crankingFuelBins,
|
|
|
|
config->crankingFuelCoef, CRANKING_CURVE_SIZE);
|
|
|
|
|
2017-05-15 20:28:49 -07:00
|
|
|
float tpsCoef = interpolate2d(getTPS(PASS_ENGINE_PARAMETER_SIGNATURE), engineConfiguration->crankingTpsBins,
|
2015-08-18 22:01:23 -07:00
|
|
|
engineConfiguration->crankingTpsCoef, CRANKING_CURVE_SIZE);
|
|
|
|
|
|
|
|
return baseCrankingFuel * durationCoef * coolantTempCoef * tpsCoef;
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|