2021-08-03 19:05:01 -07:00
|
|
|
#include "pch.h"
|
|
|
|
|
2022-09-03 08:07:26 -07:00
|
|
|
#include "engine_configuration.h"
|
|
|
|
#include "sensor.h"
|
|
|
|
#include "error_handling.h"
|
|
|
|
#include "efi_interpolation.h"
|
|
|
|
#include "table_helper.h"
|
|
|
|
#include "fuel_math.h"
|
2020-08-10 21:40:19 -07:00
|
|
|
#include "fuel_computer.h"
|
|
|
|
|
2022-09-02 10:10:21 -07:00
|
|
|
mass_t FuelComputerBase::getCycleFuel(mass_t airmass, int rpm, float load) {
|
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;
|
|
|
|
|
2022-11-05 17:23:09 -07:00
|
|
|
afrTableYAxis = load;
|
2022-09-02 11:38:03 -07:00
|
|
|
targetLambda = lambda;
|
|
|
|
targetAFR = afr;
|
|
|
|
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-11-17 00:54:21 -08:00
|
|
|
float primary = engineConfiguration->stoichRatioPrimary;
|
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;
|
|
|
|
}
|
|
|
|
|
2021-11-17 00:54:21 -08:00
|
|
|
float secondary = engineConfiguration->stoichRatioSecondary;
|
2021-01-05 13:27:23 -08:00
|
|
|
|
|
|
|
// 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 {
|
2021-11-17 00:54:21 -08:00
|
|
|
return getLoadOverride(defaultLoad, engineConfiguration->afrOverrideMode);
|
2020-10-09 14:16:49 -07:00
|
|
|
}
|
|
|
|
|
2022-09-03 08:07:26 -07:00
|
|
|
float IFuelComputer::getLoadOverride(float defaultLoad, load_override_e overrideMode) const {
|
2020-10-09 14:16:49 -07:00
|
|
|
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);
|
2022-11-05 17:23:09 -07:00
|
|
|
case AFR_CylFilling: return 100 * sdAirMassInOneCylinder / getStandardAirCharge();
|
2020-09-08 14:15:18 -07:00
|
|
|
default: return 0;
|
|
|
|
}
|
|
|
|
}
|