2020-08-10 21:40:19 -07:00
|
|
|
#include "fuel_computer.h"
|
2020-09-08 14:15:18 -07:00
|
|
|
#include "map.h"
|
2020-08-10 21:40:19 -07:00
|
|
|
|
|
|
|
EXTERN_ENGINE;
|
|
|
|
|
|
|
|
mass_t FuelComputerBase::getCycleFuel(mass_t airmass, int rpm, float load) const {
|
2020-09-08 14:15:18 -07:00
|
|
|
load = getTargetLambdaLoadAxis(load);
|
|
|
|
|
2020-08-10 21:40:19 -07:00
|
|
|
float stoich = getStoichiometricRatio();
|
|
|
|
float lambda = getTargetLambda(rpm, load);
|
|
|
|
float afr = stoich * lambda;
|
|
|
|
|
2020-09-07 07:15:42 -07:00
|
|
|
ENGINE(engineState.currentAfrLoad) = load;
|
2020-10-26 15:15:17 -07:00
|
|
|
ENGINE(engineState.targetLambda) = lambda;
|
2020-08-10 21:40:19 -07:00
|
|
|
ENGINE(engineState.targetAFR) = afr;
|
2020-10-31 14:52:07 -07:00
|
|
|
ENGINE(engineState.stoichiometricRatio) = stoich;
|
2020-08-10 21:40:19 -07:00
|
|
|
|
|
|
|
return airmass / afr;
|
|
|
|
}
|
|
|
|
|
2020-10-26 15:15:17 -07:00
|
|
|
FuelComputer::FuelComputer(const ValueProvider3D& lambdaTable) : m_lambdaTable(&lambdaTable) {}
|
2020-08-10 21:40:19 -07:00
|
|
|
|
|
|
|
float FuelComputer::getStoichiometricRatio() const {
|
2021-01-05 13:27:23 -08:00
|
|
|
float primary = (float)CONFIG(stoichRatioPrimary) / PACK_MULT_AFR_CFG;
|
2020-08-21 16:43:30 -07:00
|
|
|
|
|
|
|
// Config compatibility: this field may be zero on ECUs with old defaults
|
2021-01-05 13:27:23 -08:00
|
|
|
if (primary < 5) {
|
|
|
|
// 14.7 = E0 gasoline AFR
|
2021-05-19 23:39:20 -07:00
|
|
|
primary = STOICH_RATIO;
|
2020-08-21 16:43:30 -07:00
|
|
|
}
|
|
|
|
|
2021-01-05 13:27:23 -08:00
|
|
|
// Without an ethanol/flex sensor, return primary configured stoich ratio
|
|
|
|
if (!Sensor::hasSensor(SensorType::FuelEthanolPercent)) {
|
|
|
|
return primary;
|
|
|
|
}
|
|
|
|
|
|
|
|
float secondary = (float)CONFIG(stoichRatioSecondary) / PACK_MULT_AFR_CFG;
|
|
|
|
|
|
|
|
// Config compatibility: this field may be zero on ECUs with old defaults
|
|
|
|
if (secondary < 5) {
|
|
|
|
// 9.0 = E100 ethanol AFR
|
|
|
|
secondary = 9.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto flex = Sensor::get(SensorType::FuelEthanolPercent);
|
|
|
|
|
|
|
|
// TODO: what do do if flex sensor fails?
|
|
|
|
|
|
|
|
// Linear interpolate between primary and secondary stoich ratios
|
|
|
|
return interpolateClamped(0, primary, 100, secondary, flex.Value);
|
2020-08-10 21:40:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
float FuelComputer::getTargetLambda(int rpm, float load) const {
|
2020-10-26 15:15:17 -07:00
|
|
|
efiAssert(OBD_PCM_Processor_Fault, m_lambdaTable != nullptr, "AFR table null", 0);
|
2020-08-10 21:40:19 -07:00
|
|
|
|
2020-10-26 15:15:17 -07:00
|
|
|
return m_lambdaTable->getValue(rpm, load);
|
2020-09-08 14:15:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
float FuelComputer::getTargetLambdaLoadAxis(float defaultLoad) const {
|
2020-10-09 14:16:49 -07:00
|
|
|
return getLoadOverride(defaultLoad, CONFIG(afrOverrideMode) PASS_ENGINE_PARAMETER_SUFFIX);
|
|
|
|
}
|
|
|
|
|
|
|
|
float getLoadOverride(float defaultLoad, afr_override_e overrideMode DECLARE_ENGINE_PARAMETER_SUFFIX) {
|
|
|
|
switch(overrideMode) {
|
2020-09-08 14:15:18 -07:00
|
|
|
case AFR_None: return defaultLoad;
|
2021-01-02 16:13:10 -08:00
|
|
|
// MAP default to 200kpa - failed MAP goes rich
|
|
|
|
case AFR_MAP: return Sensor::get(SensorType::Map).value_or(200);
|
2020-09-08 14:15:18 -07:00
|
|
|
// TPS/pedal default to 100% - failed TPS goes rich
|
|
|
|
case AFR_Tps: return Sensor::get(SensorType::Tps1).value_or(100);
|
|
|
|
case AFR_AccPedal: return Sensor::get(SensorType::AcceleratorPedal).value_or(100);
|
|
|
|
case AFR_CylFilling: return 100 * ENGINE(engineState.sd.airMassInOneCylinder) / ENGINE(standardAirCharge);
|
|
|
|
default: return 0;
|
|
|
|
}
|
|
|
|
}
|