rusefi/firmware/controllers/algo/advance_map.cpp

331 lines
13 KiB
C++
Raw Normal View History

2015-07-10 06:01:56 -07:00
/**
* @file advance_map.cpp
*
* @date Mar 27, 2013
2020-01-07 21:02:40 -08:00
* @author Andrey Belomutskiy, (c) 2012-2020
2015-07-10 06:01:56 -07:00
*
* This file is part of rusEfi - see http://rusefi.com
*
* rusEfi is free software; you can redistribute it and/or modify it under the terms of
* the GNU General Public License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* rusEfi is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program.
* If not, see <http://www.gnu.org/licenses/>.
*/
2018-09-16 19:26:57 -07:00
#include "global.h"
#include "engine_configuration.h"
#include "engine.h"
2015-07-10 06:01:56 -07:00
#include "advance_map.h"
#include "interpolation.h"
#include "engine_math.h"
#include "tps.h"
#include "idle_thread.h"
#include "allsensors.h"
2015-07-10 06:01:56 -07:00
EXTERN_ENGINE
;
2015-07-10 06:01:56 -07:00
2015-12-27 09:01:53 -08:00
static ign_Map3D_t advanceMap("advance");
// This coeff in ctor parameter is sufficient for int16<->float conversion!
static ign_tps_Map3D_t advanceTpsMap("advanceTps", 1.0 / ADVANCE_TPS_STORAGE_MULT);
2015-12-27 09:01:53 -08:00
static ign_Map3D_t iatAdvanceCorrectionMap("iat corr");
2015-07-10 06:01:56 -07:00
// Init PID later (make it compatible with unit-tests)
static Pid idleTimingPid;
static bool shouldResetTimingPid = false;
static int minCrankingRpm = 0;
2019-06-10 12:45:18 -07:00
#if IGN_LOAD_COUNT == DEFAULT_IGN_LOAD_COUNT
2015-07-10 06:01:56 -07:00
static const float iatTimingRpmBins[IGN_LOAD_COUNT] = {880, 1260, 1640, 2020, 2400, 2780, 3000, 3380, 3760, 4140, 4520, 5000, 5700, 6500, 7200, 8000};
//880 1260 1640 2020 2400 2780 3000 3380 3760 4140 4520 5000 5700 6500 7200 8000
static const ignition_table_t defaultIatTiming = {
{ 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 2, 2, 2, 2, 2},
{ 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 2, 2, 2, 2, 2},
{ 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 2, 2, 2, 2, 2},
{ 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 2, 2, 2, 2, 2},
{3.5, 3.5, 3.5, 3.5, 3.5, 3.5, 3.5, 3.5, 3.5, 3.5, 3.5, 2, 2, 2, 2, 2},
{ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2},
{ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, -0.9, -0.9, -0.9, -0.9, -0.9, -0.9, -0.9, -0.9, -0.9, -0.9, -0.9, -0.9, -0.9, -0.9},
{ -3.3, -3.4, -4.9, -4.9, -4.9, -4.9, -4.4, -4.4, -4.4, -4.4, -4.4, -0.9, -0.9, -0.9, -0.9, -0.9},
{ -4.4, -4.9, -5.9, -5.9, -5.9, -5.9, -4.9, -4.9, -4.9, -4.9, -4.9, -2.4, -2.4, -2.4, -2.4, -2.4},
{ -4.4, -4.9, -5.9, -5.9, -5.9, -5.9, -4.9, -4.9, -4.9, -4.9, -4.9, -2.9, -2.9, -2.9, -2.9, -2.9},
{-4.4, -4.9, -5.9, -5.9, -5.9, -5.9, -4.9, -4.9, -4.9, -4.9, -4.9, -3.9, -3.9, -3.9, -3.9, -3.9},
{-4.4, -4.9, -5.9, -5.9, -5.9, -5.9, -4.9, -4.9, -4.9, -4.9, -4.9, -3.9, -3.9, -3.9, -3.9, -3.9},
{-4.4, -4.9, -5.9, -5.9, -5.9, -5.9, -4.9, -4.9, -4.9, -4.9, -4.9, -3.9, -3.9, -3.9, -3.9, -3.9},
};
2019-06-10 12:45:18 -07:00
#endif /* IGN_LOAD_COUNT == DEFAULT_IGN_LOAD_COUNT */
//Todo: There are some more conditions that needs to be true, and RPM range must be added to launchrpm?
//bool isLaunchCondition(int rpm DECLARE_ENGINE_PARAMETER_SUFFIX) {
// return CONFIG(launchControlEnabled) && rpm >= engineConfiguration->launchRpm;
//}
2015-07-10 06:01:56 -07:00
/**
* @return ignition timing angle advance before TDC
*/
2017-05-15 20:28:49 -07:00
static angle_t getRunningAdvance(int rpm, float engineLoad DECLARE_ENGINE_PARAMETER_SUFFIX) {
2017-12-03 10:22:29 -08:00
if (CONFIG(timingMode) == TM_FIXED)
return engineConfiguration->fixedTiming;
2019-05-07 16:32:08 -07:00
engine->m.beforeAdvance = getTimeNowLowerNt();
2015-07-10 06:01:56 -07:00
if (cisnan(engineLoad)) {
2016-11-02 20:01:48 -07:00
warning(CUSTOM_NAN_ENGINE_LOAD, "NaN engine load");
2015-07-10 06:01:56 -07:00
return NAN;
}
2018-07-25 20:30:00 -07:00
efiAssert(CUSTOM_ERR_ASSERT, !cisnan(engineLoad), "invalid el", NAN);
2019-05-07 16:32:08 -07:00
engine->m.beforeZeroTest = getTimeNowLowerNt();
engine->m.zeroTestTime = getTimeNowLowerNt() - engine->m.beforeZeroTest;
//See comment at line 70
2015-07-10 06:01:56 -07:00
// if (isLaunchCondition(rpm PASS_ENGINE_PARAMETER_SUFFIX)) {
// return engineConfiguration->launchTimingRetard;
// }
float advanceAngle;
if (CONFIG(useTPSAdvanceTable)) {
float tps = getTPS(PASS_ENGINE_PARAMETER_SIGNATURE);
advanceAngle = advanceTpsMap.getValue((float) rpm, tps);
} else {
advanceAngle = advanceMap.getValue((float) rpm, engineLoad);
}
// get advance from the separate table for Idle
if (CONFIG(useSeparateAdvanceForIdle)) {
float idleAdvance = interpolate2d("idleAdvance", rpm, config->idleAdvanceBins, config->idleAdvance);
// interpolate between idle table and normal (running) table using TPS threshold
float tps = getTPS(PASS_ENGINE_PARAMETER_SIGNATURE);
advanceAngle = interpolateClamped(0.0f, idleAdvance, CONFIG(idlePidDeactivationTpsThreshold), advanceAngle, tps);
}
2019-05-07 16:32:08 -07:00
engine->m.advanceLookupTime = getTimeNowLowerNt() - engine->m.beforeAdvance;
return advanceAngle;
}
angle_t getAdvanceCorrections(int rpm DECLARE_ENGINE_PARAMETER_SUFFIX) {
2016-05-17 21:03:11 -07:00
float iatCorrection;
2019-11-04 19:52:37 -08:00
if (!hasIatSensor()) {
2016-05-17 21:03:11 -07:00
iatCorrection = 0;
} else {
iatCorrection = iatAdvanceCorrectionMap.getValue((float) rpm, getIntakeAirTemperature());
2016-05-17 21:03:11 -07:00
}
// PID Ignition Advance angle correction
float pidTimingCorrection = 0.0f;
if (CONFIG(useIdleTimingPidControl)) {
int targetRpm = getTargetRpmForIdleCorrection(PASS_ENGINE_PARAMETER_SIGNATURE);
int rpmDelta = absI(rpm - targetRpm);
float tps = getTPS(PASS_ENGINE_PARAMETER_SIGNATURE);
if (tps >= CONFIG(idlePidDeactivationTpsThreshold)) {
// we are not in the idle mode anymore, so the 'reset' flag will help us when we return to the idle.
shouldResetTimingPid = true;
}
else if (rpmDelta > CONFIG(idleTimingPidDeadZone) && rpmDelta < CONFIG(idleTimingPidWorkZone) + CONFIG(idlePidFalloffDeltaRpm)) {
// We're now in the idle mode, and RPM is inside the Timing-PID regulator work zone!
// So if we need to reset the PID, let's do it now
if (shouldResetTimingPid) {
idleTimingPid.reset();
shouldResetTimingPid = false;
}
// get PID value (this is not an actual Advance Angle, but just a additive correction!)
2019-04-25 18:31:33 -07:00
percent_t timingRawCorr = idleTimingPid.getOutput(targetRpm, rpm,
/* is this the right dTime? this period is not exactly the period at which this code is invoked*/engineConfiguration->idleTimingPid.periodMs);
// tps idle-running falloff
pidTimingCorrection = interpolateClamped(0.0f, timingRawCorr, CONFIG(idlePidDeactivationTpsThreshold), 0.0f, tps);
// rpm falloff
pidTimingCorrection = interpolateClamped(0.0f, pidTimingCorrection, CONFIG(idlePidFalloffDeltaRpm), 0.0f, rpmDelta - CONFIG(idleTimingPidWorkZone));
} else {
shouldResetTimingPid = true;
}
} else {
shouldResetTimingPid = true;
}
2017-02-09 09:03:46 -08:00
if (engineConfiguration->debugMode == DBG_IGNITION_TIMING) {
2019-04-12 19:07:03 -07:00
#if EFI_TUNER_STUDIO
2017-01-05 18:12:06 -08:00
tsOutputChannels.debugFloatField1 = iatCorrection;
tsOutputChannels.debugFloatField2 = engine->engineState.cltTimingCorrection;
tsOutputChannels.debugFloatField3 = engine->fsioState.fsioTimingAdjustment;
tsOutputChannels.debugFloatField4 = pidTimingCorrection;
#endif /* EFI_TUNER_STUDIO */
2017-01-05 18:12:06 -08:00
}
return iatCorrection
+ engine->fsioState.fsioTimingAdjustment
+ engine->engineState.cltTimingCorrection
+ pidTimingCorrection
2019-06-13 21:57:24 -07:00
// todo: uncomment once we get usable knock - engine->knockCount
;
2015-07-10 06:01:56 -07:00
}
/**
* @return ignition timing angle advance before TDC for Cranking
*/
static angle_t getCrankingAdvance(int rpm, float engineLoad DECLARE_ENGINE_PARAMETER_SUFFIX) {
// get advance from the separate table for Cranking
if (CONFIG(useSeparateAdvanceForCranking)) {
return interpolate2d("crankingAdvance", rpm, CONFIG(crankingAdvanceBins), CONFIG(crankingAdvance));
}
// Interpolate the cranking timing angle to the earlier running angle for faster engine start
angle_t crankingToRunningTransitionAngle = getRunningAdvance(CONFIG(cranking.rpm), engineLoad PASS_ENGINE_PARAMETER_SUFFIX);
// interpolate not from zero, but starting from min. possible rpm detected
if (rpm < minCrankingRpm || minCrankingRpm == 0)
minCrankingRpm = rpm;
return interpolateClamped(minCrankingRpm, CONFIG(crankingTimingAngle), CONFIG(cranking.rpm), crankingToRunningTransitionAngle, rpm);
}
2017-05-15 20:28:49 -07:00
angle_t getAdvance(int rpm, float engineLoad DECLARE_ENGINE_PARAMETER_SUFFIX) {
2019-04-12 19:07:03 -07:00
#if EFI_ENGINE_CONTROL && EFI_SHAFT_POSITION_INPUT
2017-12-03 13:45:39 -08:00
if (cisnan(engineLoad)) {
return 0; // any error should already be reported
}
2015-07-10 06:01:56 -07:00
angle_t angle;
2017-07-06 05:43:15 -07:00
if (ENGINE(rpmCalculator).isCranking(PASS_ENGINE_PARAMETER_SIGNATURE)) {
angle = getCrankingAdvance(rpm, engineLoad PASS_ENGINE_PARAMETER_SUFFIX);
2018-09-10 19:00:13 -07:00
assertAngleRange(angle, "crAngle", CUSTOM_ERR_6680);
2018-09-16 19:11:59 -07:00
efiAssert(CUSTOM_ERR_ASSERT, !cisnan(angle), "cr_AngleN", 0);
2018-10-07 11:22:05 -07:00
if (CONFIG(useAdvanceCorrectionsForCranking)) {
angle_t correction = getAdvanceCorrections(rpm PASS_ENGINE_PARAMETER_SUFFIX);
if (!cisnan(correction)) { // correction could be NaN during settings update
angle += correction;
}
}
2018-09-29 11:11:25 -07:00
efiAssert(CUSTOM_ERR_ASSERT, !cisnan(angle), "cr_AngleN2", 0);
2015-07-10 06:01:56 -07:00
} else {
2017-12-03 10:22:29 -08:00
angle = getRunningAdvance(rpm, engineLoad PASS_ENGINE_PARAMETER_SUFFIX);
2018-09-16 19:11:59 -07:00
if (cisnan(angle)) {
warning(CUSTOM_ERR_6610, "NaN angle from table");
return 0;
}
2018-10-07 11:22:05 -07:00
angle_t correction = getAdvanceCorrections(rpm PASS_ENGINE_PARAMETER_SUFFIX);
if (!cisnan(correction)) { // correction could be NaN during settings update
angle += correction;
}
2018-09-29 11:11:25 -07:00
efiAssert(CUSTOM_ERR_ASSERT, !cisnan(angle), "AngleN3", 0);
2015-07-10 06:01:56 -07:00
}
2018-09-29 11:11:25 -07:00
efiAssert(CUSTOM_ERR_ASSERT, !cisnan(angle), "_AngleN4", 0);
2015-07-10 06:01:56 -07:00
angle -= engineConfiguration->ignitionOffset;
2018-09-29 11:11:25 -07:00
efiAssert(CUSTOM_ERR_ASSERT, !cisnan(angle), "_AngleN5", 0);
fixAngle(angle, "getAdvance", CUSTOM_ERR_ADCANCE_CALC_ANGLE);
2015-07-10 06:01:56 -07:00
return angle;
2019-01-31 14:55:23 -08:00
#else
return 0;
#endif
2015-07-10 06:01:56 -07:00
}
2017-05-15 20:28:49 -07:00
void setDefaultIatTimingCorrection(DECLARE_ENGINE_PARAMETER_SIGNATURE) {
setLinearCurve(config->ignitionIatCorrLoadBins, /*from*/CLT_CURVE_RANGE_FROM, 110, 1);
2019-06-10 12:45:18 -07:00
#if IGN_LOAD_COUNT == DEFAULT_IGN_LOAD_COUNT
2015-07-10 06:01:56 -07:00
memcpy(config->ignitionIatCorrRpmBins, iatTimingRpmBins, sizeof(iatTimingRpmBins));
copyTimingTable(defaultIatTiming, config->ignitionIatCorrTable);
2019-06-10 12:45:18 -07:00
#else
setLinearCurve(config->ignitionIatCorrLoadBins, /*from*/0, 6000, 1);
2019-06-10 12:45:18 -07:00
#endif /* IGN_LOAD_COUNT == DEFAULT_IGN_LOAD_COUNT */
2015-07-10 06:01:56 -07:00
}
2019-01-10 20:48:05 -08:00
void initTimingMap(DECLARE_ENGINE_PARAMETER_SIGNATURE) {
// We init both tables in RAM because here we're at a very early stage, with no config settings loaded.
2015-07-10 06:01:56 -07:00
advanceMap.init(config->ignitionTable, config->ignitionLoadBins,
config->ignitionRpmBins);
advanceTpsMap.init(CONFIG(ignitionTpsTable), CONFIG(ignitionTpsBins),
config->ignitionRpmBins);
2015-07-10 06:01:56 -07:00
iatAdvanceCorrectionMap.init(config->ignitionIatCorrTable, config->ignitionIatCorrLoadBins,
config->ignitionIatCorrRpmBins);
// init timing PID
idleTimingPid = Pid(&CONFIG(idleTimingPid));
2015-07-10 06:01:56 -07:00
}
2015-12-24 11:02:03 -08:00
/**
* @param octane gas octane number
* @param bore in mm
*/
float getTopAdvanceForBore(chamber_style_e style, int octane, double compression, double bore) {
int octaneCorrection;
if ( octane <= 90) {
octaneCorrection = -2;
} else if (octane < 94) {
octaneCorrection = -1;
} else {
octaneCorrection = 0;
}
int compressionCorrection;
if (compression <= 9) {
compressionCorrection = 2;
} else if (compression <= 10) {
compressionCorrection = 1;
} else if (compression <= 11) {
compressionCorrection = 0;
} else {
// compression ratio above 11
compressionCorrection = -2;
}
int base;
if (style == CS_OPEN) {
base = 33;
} else if (style == CS_CLOSED) {
base = 28;
} else {
// CS_SWIRL_TUMBLE
base = 22;
}
float boreCorrection = (bore - 4 * 25.4) / 25.4 * 6;
float result = base + octaneCorrection + compressionCorrection + boreCorrection;
return ((int)(result * 10)) / 10.0;
}
2015-12-31 10:02:19 -08:00
float getAdvanceForRpm(int rpm, float advanceMax) {
if (rpm >= 3000)
return advanceMax;
if (rpm < 600)
return 10;
2018-06-12 02:45:11 -07:00
return interpolateMsg("advance", 600, 10, 3000, advanceMax, rpm);
2015-12-31 10:02:19 -08:00
}
#define round10(x) efiRound(x, 0.1)
float getInitialAdvance(int rpm, float map, float advanceMax) {
2016-01-21 19:01:31 -08:00
map = minF(map, 100);
2015-12-31 10:02:19 -08:00
float advance = getAdvanceForRpm(rpm, advanceMax);
2016-01-21 19:01:31 -08:00
if (rpm >= 3000)
return round10(advance + 0.1 * (100 - map));
return round10(advance + 0.1 * (100 - map) * rpm / 3000);
2015-12-31 10:02:19 -08:00
}
2016-01-21 14:02:40 -08:00
/**
* this method builds a good-enough base timing advance map bases on a number of heuristics
*/
void buildTimingMap(float advanceMax DECLARE_CONFIG_PARAMETER_SUFFIX) {
2016-08-28 13:02:34 -07:00
if (engineConfiguration->fuelAlgorithm != LM_SPEED_DENSITY &&
engineConfiguration->fuelAlgorithm != LM_MAP) {
2016-11-02 20:01:48 -07:00
warning(CUSTOM_WRONG_ALGORITHM, "wrong algorithm for MAP-based timing");
2015-12-31 10:02:19 -08:00
return;
}
/**
* good enough (but do not trust us!) default timing map in case of MAP-based engine load
*/
for (int loadIndex = 0; loadIndex < IGN_LOAD_COUNT; loadIndex++) {
float load = config->ignitionLoadBins[loadIndex];
for (int rpmIndex = 0;rpmIndex<IGN_RPM_COUNT;rpmIndex++) {
2016-01-21 19:01:31 -08:00
float rpm = config->ignitionRpmBins[rpmIndex];
2015-12-31 10:02:19 -08:00
config->ignitionTable[loadIndex][rpmIndex] = getInitialAdvance(rpm, load, advanceMax);
}
}
}