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
|
|
|
*/
|
|
|
|
|
|
|
|
#include "main.h"
|
|
|
|
#include "speed_density.h"
|
|
|
|
#include "interpolation.h"
|
|
|
|
#include "rpm_calculator.h"
|
|
|
|
#include "engine_math.h"
|
|
|
|
#include "engine_state.h"
|
|
|
|
|
|
|
|
#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)) {
|
2018-09-10 19:00:13 -07:00
|
|
|
warning(CUSTOM_ERR_START_STACK, "t-getTCharge NaN");
|
2017-11-19 18:30:59 -08:00
|
|
|
return coolantTemp;
|
|
|
|
}
|
2018-06-12 02:45:11 -07:00
|
|
|
float minRpmKcurrentTPS = interpolateMsg("minRpm", tpMin, engineConfiguration->tChargeMinRpmMinTps, tpMax,
|
2016-10-11 18:03:00 -07:00
|
|
|
engineConfiguration->tChargeMinRpmMaxTps, tps);
|
2018-06-12 02:45:11 -07:00
|
|
|
float maxRpmKcurrentTPS = interpolateMsg("maxRpm", tpMin, engineConfiguration->tChargeMaxRpmMinTps, tpMax,
|
2016-10-11 18:03:00 -07:00
|
|
|
engineConfiguration->tChargeMaxRpmMaxTps, tps);
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2018-06-12 02:45:11 -07:00
|
|
|
float 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
|
|
|
|
|
|
|
float Tcharge = coolantTemp * (1 - Tcharge_coff) + airTemp * Tcharge_coff;
|
|
|
|
|
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
|
|
|
|
*/
|
2017-11-06 18:48:25 -08:00
|
|
|
float getCycleAirMass(engine_configuration_s *engineConfiguration, float VE, float MAP, float tempK) {
|
2015-12-02 17:10:06 -08:00
|
|
|
// todo: pre-calculate cylinder displacement to save one division
|
2017-11-06 18:48:25 -08:00
|
|
|
float cylinderDisplacement = engineConfiguration->specs.displacement;
|
2015-12-02 17:10:06 -08:00
|
|
|
return (cylinderDisplacement * VE * MAP) / (GAS_R * tempK);
|
|
|
|
}
|
|
|
|
|
2017-11-06 18:48:25 -08:00
|
|
|
float getCylinderAirMass(engine_configuration_s *engineConfiguration, float VE, float MAP, float tempK) {
|
|
|
|
return getCycleAirMass(engineConfiguration, VE, MAP, tempK) / engineConfiguration->specs.cylindersCount;
|
|
|
|
}
|
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
/**
|
|
|
|
* @return per cylinder injection time, in seconds
|
|
|
|
*/
|
2015-12-02 17:10:06 -08:00
|
|
|
float sdMath(engine_configuration_s *engineConfiguration, float airMass, float AFR) {
|
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
|
|
|
|
*/
|
|
|
|
float injectorFlowRate = cc_minute_to_gramm_second(engineConfiguration->injector.flow);
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
2017-11-06 18:48:25 -08:00
|
|
|
float airMass = getCylinderAirMass(engineConfiguration, ENGINE(engineState.currentVE), adjustedMap, tChargeK);
|
2018-07-25 20:30:00 -07:00
|
|
|
efiAssert(CUSTOM_ERR_ASSERT, !cisnan(airMass), "NaN airMass", 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;
|
|
|
|
return sdMath(engineConfiguration, airMass, ENGINE(engineState.targetAFR)) * 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);
|
|
|
|
}
|