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"
|
|
|
|
|
2023-05-24 08:07:23 -07:00
|
|
|
#if EFI_ENGINE_CONTROL
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2022-11-13 17:05:04 -08:00
|
|
|
// todo: fatal in July of 2023
|
2021-01-05 13:27:23 -08:00
|
|
|
// 14.7 = E0 gasoline AFR
|
2022-11-13 17:05:04 -08:00
|
|
|
engineConfiguration->stoichRatioPrimary = 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
|
2022-11-13 17:05:04 -08:00
|
|
|
// todo: fatal in July of 2023
|
|
|
|
engineConfiguration->stoichRatioSecondary = secondary = 9.0f;
|
2021-01-05 13:27:23 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-26 08:23:54 -08:00
|
|
|
float FuelComputer::getTargetLambda(int rpm, float load) const {
|
|
|
|
return interpolate3d(
|
|
|
|
config->lambdaTable,
|
|
|
|
config->lambdaLoadBins, load,
|
|
|
|
config->lambdaRpmBins, rpm
|
|
|
|
);
|
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;
|
|
|
|
}
|
|
|
|
}
|
2023-05-24 08:07:23 -07:00
|
|
|
|
|
|
|
#endif // EFI_ENGINE_CONTROL
|