rusefi/firmware/controllers/algo/fuel_math.cpp

229 lines
8.2 KiB
C++
Raw Normal View History

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
* <BR>2) getCltCorrection() for warm-up
* <BR>3) getIatCorrection() to account for cold weather
*
* getCrankingFuel() depents only on getCoolantTemperature()
*
*
* @date May 27, 2013
2015-12-31 13:02:30 -08:00
* @author Andrey Belomutskiy, (c) 2012-2016
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
*/
float getRealMafFuel(float airSpeed, int rpm DECLARE_ENGINE_PARAMETER_S) {
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
2015-07-10 06:01:56 -07:00
floatms_t getBaseFuel(int rpm DECLARE_ENGINE_PARAMETER_S) {
2015-12-26 09:03:13 -08:00
ENGINE(engineState.tpsAccelEnrich) = ENGINE(tpsAccelEnrichment.getTpsEnrichment(PASS_ENGINE_PARAMETER_F));
2015-07-10 06:01:56 -07:00
2016-08-28 13:02:34 -07:00
if (CONFIG(fuelAlgorithm) == LM_SPEED_DENSITY) {
2015-12-02 17:10:06 -08:00
engine->engineState.baseFuel = getSpeedDensityFuel(rpm PASS_ENGINE_PARAMETER);
2016-08-28 13:02:34 -07:00
} else if (engineConfiguration->fuelAlgorithm == LM_REAL_MAF) {
2016-01-02 13:01:27 -08:00
float maf = getRealMaf(PASS_ENGINE_PARAMETER_F) + engine->engineLoadAccelEnrichment.getEngineLoadEnrichment(PASS_ENGINE_PARAMETER_F);
2015-12-02 17:10:06 -08:00
engine->engineState.baseFuel = getRealMafFuel(maf, rpm PASS_ENGINE_PARAMETER);
2015-07-10 06:01:56 -07:00
} else {
2015-12-02 17:10:06 -08:00
engine->engineState.baseFuel = engine->engineState.baseTableFuel;
2015-07-10 06:01:56 -07:00
}
2015-12-02 17:10:06 -08:00
2015-12-26 09:03:13 -08:00
return ENGINE(engineState.tpsAccelEnrich) + ENGINE(engineState.baseFuel);
2015-07-10 06:01:56 -07:00
}
2016-07-06 20:02:59 -07:00
float getinjectionOffset(float rpm DECLARE_ENGINE_PARAMETER_S) {
2015-07-10 06:01:56 -07:00
float engineLoad = getEngineLoadT(PASS_ENGINE_PARAMETER_F);
2016-03-11 12:01:58 -08:00
return fuelPhaseMap.getValue(rpm, engineLoad);
2015-07-10 06:01:56 -07:00
}
/**
* Number of injections into each cylinder per engine cycle
*/
2016-07-23 18:04:30 -07:00
int getNumberOfInjections(injection_mode_e mode DECLARE_ENGINE_PARAMETER_S) {
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:
firmwareError("Unexpected getFuelMultiplier %d", mode);
return 1;
}
}
percent_t getInjectorDutyCycle(int rpm DECLARE_ENGINE_PARAMETER_S) {
2016-08-26 15:02:39 -07:00
floatms_t totalPerCycle = getInjectionDuration(rpm PASS_ENGINE_PARAMETER) * getNumberOfInjections(engineConfiguration->injectionMode PASS_ENGINE_PARAMETER);
2016-07-23 18:04:30 -07:00
floatms_t engineCycleDuration = getCrankshaftRevolutionTimeMs(rpm) * (engineConfiguration->operationMode == TWO_STROKE ? 1 : 2);
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
*/
2016-08-26 15:02:39 -07:00
floatms_t getInjectionDuration(int rpm DECLARE_ENGINE_PARAMETER_S) {
2015-07-10 06:01:56 -07:00
float theoreticalInjectionLength;
if (isCrankingR(rpm)) {
theoreticalInjectionLength = getCrankingFuel(PASS_ENGINE_PARAMETER_F)
/ getNumberOfInjections(engineConfiguration->crankingInjectionMode PASS_ENGINE_PARAMETER);
} else {
2016-08-26 15:02:39 -07:00
floatms_t baseFuel = getBaseFuel(rpm PASS_ENGINE_PARAMETER);
floatms_t fuelPerCycle = getRunningFuel(baseFuel, rpm PASS_ENGINE_PARAMETER);
2015-07-10 06:01:56 -07:00
theoreticalInjectionLength = fuelPerCycle
/ getNumberOfInjections(engineConfiguration->injectionMode PASS_ENGINE_PARAMETER);
}
2016-02-06 09:02:24 -08:00
return theoreticalInjectionLength + ENGINE(engineState.injectorLag);
2015-07-10 06:01:56 -07:00
}
floatms_t getRunningFuel(floatms_t baseFuel, int rpm DECLARE_ENGINE_PARAMETER_S) {
float iatCorrection = ENGINE(engineState.iatFuelCorrection);
float cltCorrection = ENGINE(engineState.cltFuelCorrection);
2015-12-26 09:03:13 -08:00
ENGINE(engineState.runningFuel) = baseFuel * iatCorrection * cltCorrection;
2015-09-02 19:01:17 -07:00
2015-12-26 09:03:13 -08:00
return ENGINE(engineState.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
*/
floatms_t getInjectorLag(float vBatt DECLARE_ENGINE_PARAMETER_S) {
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
*/
void prepareFuelMap(DECLARE_ENGINE_PARAMETER_F) {
fuelMap.init(config->fuelTable, config->fuelLoadBins, config->fuelRpmBins);
fuelPhaseMap.init(config->injectionPhase, config->injPhaseLoadBins, config->injPhaseRpmBins);
}
/**
* @brief Engine warm-up fuel correction.
*/
float getCltCorrection(float clt DECLARE_ENGINE_PARAMETER_S) {
if (cisnan(clt))
return 1; // this error should be already reported somewhere else, let's just handle it
2016-02-14 10:02:00 -08:00
return interpolate2d(clt, config->cltFuelCorrBins, config->cltFuelCorr, CLT_CURVE_SIZE) / PERCENT_MULT;
2015-07-10 06:01:56 -07:00
}
float getIatCorrection(float iat DECLARE_ENGINE_PARAMETER_S) {
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
*/
floatms_t getBaseTableFuel(engine_configuration_s *engineConfiguration, int rpm, float engineLoad) {
if (cisnan(engineLoad)) {
2016-07-13 18:03:05 -07:00
warning(CUSTOM_OBD_2, "NaN engine load");
2015-07-10 06:01:56 -07:00
return NAN;
}
2016-03-11 12:01:58 -08:00
return fuelMap.getValue(rpm, engineLoad);
2015-07-10 06:01:56 -07:00
}
float getBaroCorrection(DECLARE_ENGINE_PARAMETER_F) {
if (hasBaroSensor(PASS_ENGINE_PARAMETER_F)) {
2016-03-11 12:01:58 -08:00
return baroCorrMap.getValue(getRpmE(engine), getBaroPressure(PASS_ENGINE_PARAMETER_F));
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
*/
floatms_t getCrankingFuel(DECLARE_ENGINE_PARAMETER_F) {
return getCrankingFuel3(getCoolantTemperature(PASS_ENGINE_PARAMETER_F),
engine->rpmCalculator.getRevolutionCounterSinceStart() PASS_ENGINE_PARAMETER);
}
#endif
floatms_t getCrankingFuel3(float coolantTemperature,
uint32_t revolutionCounterSinceStart DECLARE_ENGINE_PARAMETER_S) {
// these magic constants are in Celsius
float baseCrankingFuel = engineConfiguration->cranking.baseFuel;
if (cisnan(coolantTemperature))
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);
2015-08-19 05:02:11 -07:00
float tpsCoef = interpolate2d(getTPS(PASS_ENGINE_PARAMETER_F), 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
}