2015-07-10 06:01:56 -07:00
|
|
|
/**
|
|
|
|
* @file speed_density.cpp
|
|
|
|
*
|
|
|
|
* See http://rusefi.com/wiki/index.php?title=Manual:Software:Fuel_Control#Speed_Density for details
|
|
|
|
*
|
|
|
|
* @date May 29, 2014
|
2018-01-20 17:55:31 -08:00
|
|
|
* @author Andrey Belomutskiy, (c) 2012-2018
|
2015-07-10 06:01:56 -07:00
|
|
|
*/
|
|
|
|
|
2018-09-16 19:26:57 -07:00
|
|
|
#include "global.h"
|
2015-07-10 06:01:56 -07:00
|
|
|
#include "speed_density.h"
|
|
|
|
#include "interpolation.h"
|
|
|
|
#include "rpm_calculator.h"
|
|
|
|
#include "engine_math.h"
|
|
|
|
#include "engine_state.h"
|
2019-01-12 22:53:58 -08:00
|
|
|
#include "maf2map.h"
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
#define rpmMin 500
|
|
|
|
#define rpmMax 8000
|
|
|
|
|
2016-10-11 18:03:00 -07:00
|
|
|
EXTERN_ENGINE;
|
|
|
|
|
2015-12-27 09:01:53 -08:00
|
|
|
fuel_Map3D_t veMap("VE");
|
|
|
|
fuel_Map3D_t ve2Map("VE2");
|
2016-07-01 20:01:22 -07:00
|
|
|
afr_Map3D_t afrMap("AFR", 1.0 / AFR_STORAGE_MULT);
|
2015-12-27 09:01:53 -08:00
|
|
|
baroCorr_Map3D_t baroCorrMap("baro");
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
#define tpMin 0
|
|
|
|
#define tpMax 100
|
|
|
|
// http://rusefi.com/math/t_charge.html
|
2017-05-15 20:33:22 -07:00
|
|
|
float getTCharge(int rpm, float tps, float coolantTemp, float airTemp DECLARE_ENGINE_PARAMETER_SUFFIX) {
|
2017-11-19 18:30:59 -08:00
|
|
|
if (cisnan(coolantTemp) || cisnan(airTemp)) {
|
2019-01-26 10:19:18 -08:00
|
|
|
warning(CUSTOM_ERR_NAN_TCHARGE, "t-getTCharge NaN");
|
2017-11-19 18:30:59 -08:00
|
|
|
return coolantTemp;
|
|
|
|
}
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2018-09-29 09:16:36 -07:00
|
|
|
float Tcharge_coff;
|
|
|
|
|
|
|
|
if (CONFIG(tChargeMode) == TCHARGE_MODE_AIR_INTERP) {
|
|
|
|
const floatms_t gramsPerMsToKgPerHour = (3600.0f * 1000.0f) / 1000.0f;
|
|
|
|
// We're actually using an 'old' airMass calculated for the previous cycle, but it's ok, we're not having any self-excitaton issues
|
2019-01-27 20:41:46 -08:00
|
|
|
floatms_t airMassForEngine = engine->engineState.airMass * CONFIG(specs.cylindersCount);
|
2018-09-29 09:16:36 -07:00
|
|
|
// airMass is in grams per 1 cycle for 1 cyl. Convert it to airFlow in kg/h for the engine.
|
|
|
|
// And if the engine is stopped (0 rpm), then airFlow is also zero (avoiding NaN division)
|
|
|
|
floatms_t airFlow = (rpm == 0) ? 0 : airMassForEngine * gramsPerMsToKgPerHour / getEngineCycleDuration(rpm PASS_ENGINE_PARAMETER_SUFFIX);
|
|
|
|
// just interpolate between user-specified min and max coefs, based on the max airFlow value
|
|
|
|
Tcharge_coff = interpolateClamped(0.0, CONFIG(tChargeAirCoefMin), CONFIG(tChargeAirFlowMax), CONFIG(tChargeAirCoefMax), airFlow);
|
|
|
|
// save it for console output (instead of MAF massAirFlow)
|
|
|
|
engine->engineState.airFlow = airFlow;
|
|
|
|
} else {
|
|
|
|
// TCHARGE_MODE_RPM_TPS
|
2019-01-27 20:41:46 -08:00
|
|
|
float minRpmKcurrentTPS = interpolateMsg("minRpm", tpMin, CONFIG(tChargeMinRpmMinTps), tpMax,
|
|
|
|
CONFIG(tChargeMinRpmMaxTps), tps);
|
|
|
|
float maxRpmKcurrentTPS = interpolateMsg("maxRpm", tpMin, CONFIG(tChargeMaxRpmMinTps), tpMax,
|
|
|
|
CONFIG(tChargeMaxRpmMaxTps), tps);
|
2018-09-29 09:16:36 -07:00
|
|
|
|
|
|
|
Tcharge_coff = interpolateMsg("Kcurr", rpmMin, minRpmKcurrentTPS, rpmMax, maxRpmKcurrentTPS, rpm);
|
|
|
|
}
|
|
|
|
|
2017-11-19 18:30:59 -08:00
|
|
|
if (cisnan(Tcharge_coff)) {
|
2018-09-10 19:00:13 -07:00
|
|
|
warning(CUSTOM_ERR_T2_CHARGE, "t2-getTCharge NaN");
|
2017-11-19 18:30:59 -08:00
|
|
|
return coolantTemp;
|
|
|
|
}
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2018-09-29 09:16:36 -07:00
|
|
|
// We use a robust interp. function for proper tcharge_coff clamping.
|
|
|
|
float Tcharge = interpolateClamped(0.0f, coolantTemp, 1.0f, airTemp, Tcharge_coff);
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2017-04-12 20:48:35 -07:00
|
|
|
if (cisnan(Tcharge)) {
|
|
|
|
// we can probably end up here while resetting engine state - interpolation would fail
|
2017-04-13 07:43:27 -07:00
|
|
|
warning(CUSTOM_ERR_TCHARGE_NOT_READY, "getTCharge NaN");
|
2017-04-12 20:48:35 -07:00
|
|
|
return coolantTemp;
|
|
|
|
}
|
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
return Tcharge;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* is J/g*K
|
|
|
|
*/
|
|
|
|
#define GAS_R 0.28705
|
|
|
|
|
2018-08-29 04:43:21 -07:00
|
|
|
/**
|
|
|
|
* @return air mass in grams
|
|
|
|
*/
|
2019-01-27 21:00:19 -08:00
|
|
|
static float getCycleAirMass(float volumetricEfficiency, float MAP, float tempK DECLARE_ENGINE_PARAMETER_SUFFIX) {
|
2015-12-02 17:10:06 -08:00
|
|
|
// todo: pre-calculate cylinder displacement to save one division
|
2019-01-27 20:41:46 -08:00
|
|
|
float cylinderDisplacement = CONFIG(specs.displacement);
|
2019-01-27 21:00:19 -08:00
|
|
|
return (cylinderDisplacement * volumetricEfficiency * MAP) / (GAS_R * tempK);
|
2015-12-02 17:10:06 -08:00
|
|
|
}
|
|
|
|
|
2019-01-27 21:00:19 -08:00
|
|
|
float getCylinderAirMass(float volumetricEfficiency, float MAP, float tempK DECLARE_ENGINE_PARAMETER_SUFFIX) {
|
|
|
|
return getCycleAirMass(volumetricEfficiency, MAP, tempK PASS_ENGINE_PARAMETER_SUFFIX)
|
|
|
|
/ CONFIG(specs.cylindersCount);
|
2017-11-06 18:48:25 -08:00
|
|
|
}
|
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
/**
|
|
|
|
* @return per cylinder injection time, in seconds
|
|
|
|
*/
|
2019-01-27 20:41:46 -08:00
|
|
|
float sdMath(float airMass, float AFR DECLARE_ENGINE_PARAMETER_SUFFIX) {
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* todo: pre-calculate gramm/second injector flow to save one multiplication
|
|
|
|
* open question if that's needed since that's just a multiplication
|
|
|
|
*/
|
2019-01-27 20:41:46 -08:00
|
|
|
float injectorFlowRate = cc_minute_to_gramm_second(CONFIG(injector.flow));
|
2015-07-10 06:01:56 -07:00
|
|
|
/**
|
|
|
|
* injection_pulse_duration = fuel_mass / injector_flow
|
|
|
|
* fuel_mass = air_mass / target_afr
|
|
|
|
*
|
|
|
|
* injection_pulse_duration = (air_mass / target_afr) / injector_flow
|
|
|
|
*/
|
|
|
|
return airMass / (AFR * injectorFlowRate);
|
|
|
|
}
|
|
|
|
|
|
|
|
EXTERN_ENGINE;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return per cylinder injection time, in Milliseconds
|
|
|
|
*/
|
2017-05-15 20:33:22 -07:00
|
|
|
floatms_t getSpeedDensityFuel(DECLARE_ENGINE_PARAMETER_SIGNATURE) {
|
2015-07-10 06:01:56 -07:00
|
|
|
/**
|
|
|
|
* most of the values are pre-calculated for performance reasons
|
|
|
|
*/
|
|
|
|
float tChargeK = ENGINE(engineState.tChargeK);
|
2017-04-13 05:01:59 -07:00
|
|
|
if (cisnan(tChargeK)) {
|
2017-04-13 07:43:27 -07:00
|
|
|
warning(CUSTOM_ERR_TCHARGE_NOT_READY2, "tChargeK not ready"); // this would happen before we have CLT reading for example
|
2017-04-13 05:01:59 -07:00
|
|
|
return 0;
|
|
|
|
}
|
2015-07-10 06:01:56 -07:00
|
|
|
float map = getMap();
|
2018-07-25 20:30:00 -07:00
|
|
|
efiAssert(CUSTOM_ERR_ASSERT, !cisnan(map), "NaN map", 0);
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2017-05-15 20:33:22 -07:00
|
|
|
float adjustedMap = map + engine->engineLoadAccelEnrichment.getEngineLoadEnrichment(PASS_ENGINE_PARAMETER_SIGNATURE);
|
2018-07-25 20:30:00 -07:00
|
|
|
efiAssert(CUSTOM_ERR_ASSERT, !cisnan(adjustedMap), "NaN adjustedMap", 0);
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2019-01-27 20:41:46 -08:00
|
|
|
float airMass = getCylinderAirMass(ENGINE(engineState.currentVE), adjustedMap, tChargeK PASS_ENGINE_PARAMETER_SUFFIX);
|
2018-11-10 17:53:28 -08:00
|
|
|
if (cisnan(airMass)) {
|
|
|
|
warning(CUSTOM_ERR_6685, "NaN airMass");
|
|
|
|
return 0;
|
|
|
|
}
|
2016-09-25 21:03:15 -07:00
|
|
|
#if EFI_PRINTF_FUEL_DETAILS || defined(__DOXYGEN__)
|
2018-01-23 09:05:14 -08:00
|
|
|
printf("map=%.2f adjustedMap=%.2f airMass=%.2f\t\n",
|
2016-09-25 21:03:15 -07:00
|
|
|
map, adjustedMap, engine->engineState.airMass);
|
|
|
|
#endif /*EFI_PRINTF_FUEL_DETAILS */
|
2015-12-02 17:10:06 -08:00
|
|
|
|
2017-04-12 20:25:58 -07:00
|
|
|
engine->engineState.airMass = airMass;
|
2019-01-27 20:41:46 -08:00
|
|
|
return sdMath(airMass, ENGINE(engineState.targetAFR) PASS_ENGINE_PARAMETER_SUFFIX) * 1000;
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static const baro_corr_table_t default_baro_corr = {
|
|
|
|
{1.141, 1.086, 1.039, 1},
|
|
|
|
{1.141, 1.086, 1.039, 1},
|
|
|
|
{1.141, 1.086, 1.039, 1},
|
|
|
|
{1.141, 1.086, 1.039, 1}
|
|
|
|
};
|
|
|
|
|
2017-05-15 20:33:22 -07:00
|
|
|
void setDefaultVETable(DECLARE_ENGINE_PARAMETER_SIGNATURE) {
|
2015-07-10 06:01:56 -07:00
|
|
|
setRpmTableBin(config->veRpmBins, FUEL_RPM_COUNT);
|
|
|
|
veMap.setAll(80);
|
|
|
|
|
|
|
|
// setRpmTableBin(engineConfiguration->ve2RpmBins, FUEL_RPM_COUNT);
|
2018-01-07 08:17:49 -08:00
|
|
|
// setLinearCurve(engineConfiguration->ve2LoadBins, FUEL_LOAD_COUNT, 10, 300, 1);
|
2015-07-10 06:01:56 -07:00
|
|
|
// ve2Map.setAll(0.81);
|
|
|
|
|
|
|
|
setRpmTableBin(config->afrRpmBins, FUEL_RPM_COUNT);
|
|
|
|
afrMap.setAll(14.7);
|
|
|
|
|
|
|
|
setRpmTableBin(engineConfiguration->baroCorrRpmBins, BARO_CORR_SIZE);
|
2018-01-07 08:17:49 -08:00
|
|
|
setLinearCurve(engineConfiguration->baroCorrPressureBins, BARO_CORR_SIZE, 75, 105, 1);
|
2015-07-10 06:01:56 -07:00
|
|
|
memcpy(engineConfiguration->baroCorrTable, default_baro_corr, sizeof(default_baro_corr));
|
|
|
|
}
|
|
|
|
|
2017-05-15 20:33:22 -07:00
|
|
|
void initSpeedDensity(DECLARE_ENGINE_PARAMETER_SIGNATURE) {
|
2015-07-10 06:01:56 -07:00
|
|
|
veMap.init(config->veTable, config->veLoadBins, config->veRpmBins);
|
|
|
|
// ve2Map.init(engineConfiguration->ve2Table, engineConfiguration->ve2LoadBins, engineConfiguration->ve2RpmBins);
|
|
|
|
afrMap.init(config->afrTable, config->afrLoadBins, config->afrRpmBins);
|
|
|
|
baroCorrMap.init(engineConfiguration->baroCorrTable, engineConfiguration->baroCorrPressureBins, engineConfiguration->baroCorrRpmBins);
|
2019-01-12 22:53:58 -08:00
|
|
|
initMaf2Map();
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|