rusefi/firmware/controllers/algo/advance_map.cpp

278 lines
10 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/>.
*/
#include "pch.h"
2015-07-10 06:01:56 -07:00
#include "advance_map.h"
#include "idle_thread.h"
2020-03-24 16:55:12 -07:00
#include "launch_control.h"
#include "gppwm_channel.h"
2015-07-10 06:01:56 -07:00
#if EFI_ENGINE_CONTROL
2023-03-01 14:08:37 -08:00
// TODO: wow move this into engineState at least for context not to leak from test to test!
2021-05-23 10:54:49 -07:00
// todo: reset this between cranking attempts?! #2735
2024-09-24 23:52:14 -07:00
float minCrankingRpm = 0;
static Map3D<TRACTION_CONTROL_ETB_DROP_SIZE, TRACTION_CONTROL_ETB_DROP_SIZE, int8_t, uint16_t, uint8_t> tcTimingDropTable{"tct"};
static Map3D<TRACTION_CONTROL_ETB_DROP_SIZE, TRACTION_CONTROL_ETB_DROP_SIZE, int8_t, uint16_t, uint8_t> tcSparkSkipTable{"tcs"};
2024-02-03 14:02:30 -08:00
2023-11-04 21:18:31 -07:00
#if EFI_ENGINE_CONTROL && EFI_SHAFT_POSITION_INPUT
2015-07-10 06:01:56 -07:00
/**
* @return ignition timing angle advance before TDC
*/
2024-09-24 23:52:14 -07:00
angle_t getRunningAdvance(float rpm, float engineLoad) {
2024-07-22 12:15:40 -07:00
if (std::isnan(engineLoad)) {
warning(ObdCode::CUSTOM_NAN_ENGINE_LOAD, "NaN engine load");
2015-07-10 06:01:56 -07:00
return NAN;
}
2020-03-24 16:55:12 -07:00
2024-07-22 16:00:59 -07:00
efiAssert(ObdCode::CUSTOM_ERR_ASSERT, !std::isnan(engineLoad), "invalid el", NAN);
2015-07-10 06:01:56 -07:00
// compute base ignition angle from main table
float advanceAngle = interpolate3d(
config->ignitionTable,
config->ignitionLoadBins, engineLoad,
config->ignitionRpmBins, rpm
);
2020-03-24 16:55:12 -07:00
2024-02-03 14:02:30 -08:00
float vehicleSpeed = Sensor::getOrZero(SensorType::VehicleSpeed);
float wheelSlip = Sensor::getOrZero(SensorType::WheelSlipRatio);
engine->ignitionState.tractionAdvanceDrop = tcTimingDropTable.getValue(wheelSlip, vehicleSpeed);
engine->engineState.tractionControlSparkSkip = tcSparkSkipTable.getValue(wheelSlip, vehicleSpeed);
engine->engineState.updateSparkSkip();
2024-02-03 14:02:30 -08:00
advanceAngle += engine->ignitionState.tractionAdvanceDrop;
#if EFI_ANTILAG_SYSTEM
if (engine->antilagController.isAntilagCondition) {
float throttleIntent = Sensor::getOrZero(SensorType::DriverThrottleIntent);
engine->antilagController.timingALSCorrection = interpolate3d(
config->ALSTimingRetardTable,
2023-02-06 14:14:21 -08:00
config->alsIgnRetardLoadBins, throttleIntent,
config->alsIgnRetardrpmBins, rpm
);
advanceAngle += engine->antilagController.timingALSCorrection;
}
#endif /* EFI_ANTILAG_SYSTEM */
// Add any adjustments if configured
for (size_t i = 0; i < efi::size(config->ignBlends); i++) {
auto result = calculateBlend(config->ignBlends[i], rpm, engineLoad);
engine->outputChannels.ignBlendParameter[i] = result.BlendParameter;
engine->outputChannels.ignBlendBias[i] = result.Bias;
engine->outputChannels.ignBlendOutput[i] = result.Value;
advanceAngle += result.Value;
}
// get advance from the separate table for Idle
#if EFI_IDLE_CONTROL
if (engineConfiguration->useSeparateAdvanceForIdle &&
engine->module<IdleController>()->isIdlingOrTaper()) {
float idleAdvance = interpolate2d(rpm, config->idleAdvanceBins, config->idleAdvance);
auto tps = Sensor::get(SensorType::DriverThrottleIntent);
if (tps) {
// interpolate between idle table and normal (running) table using TPS threshold
advanceAngle = interpolateClamped(0.0f, idleAdvance, engineConfiguration->idlePidDeactivationTpsThreshold, advanceAngle, tps.Value);
}
}
#endif
2020-03-24 16:55:12 -07:00
#if EFI_LAUNCH_CONTROL
2024-06-11 11:49:20 -07:00
if (engineConfiguration->launchControlEnabled && engineConfiguration->enableLaunchRetard) {
const float launchAngle = engineConfiguration->launchTimingRetard;
if (engine->launchController.isPreLaunchCondition) {
const int launchRpm = engineConfiguration->launchRpm;
const int smoothRetardStartRpm = (launchRpm - engineConfiguration->launchRpmWindow);
const int smoothRetardEndRpm = (launchRpm - engineConfiguration->launchCorrectionsEndRpm);
if (smoothRetardStartRpm <= rpm) {
if (engineConfiguration->launchSmoothRetard && (rpm <= smoothRetardEndRpm)) {
// https://github.com/rusefi/rusefi/issues/5611#issuecomment-2130431696
return interpolateClamped(smoothRetardStartRpm, advanceAngle, smoothRetardEndRpm, launchAngle, rpm);
} else {
return launchAngle;
}
}
} else if (engine->launchController.isLaunchCondition) {
return launchAngle;
}
2020-03-24 16:55:12 -07:00
}
if (engineConfiguration->torqueReductionEnabled
&& engine->shiftTorqueReductionController.isFlatShiftConditionSatisfied
) {
return engineConfiguration->torqueReductionIgnitionRetard;
}
2020-03-24 16:55:12 -07:00
#endif /* EFI_LAUNCH_CONTROL */
return advanceAngle;
}
angle_t getAdvanceCorrections(float engineLoad) {
auto iat = Sensor::get(SensorType::Iat);
if (!iat) {
engine->ignitionState.timingIatCorrection = 0;
2016-05-17 21:03:11 -07:00
} else {
engine->ignitionState.timingIatCorrection = interpolate3d(
config->ignitionIatCorrTable,
config->ignitionIatCorrLoadBins, engineLoad,
config->ignitionIatCorrTempBins, iat.Value
);
2016-05-17 21:03:11 -07:00
}
2023-11-04 10:05:19 -07:00
#if EFI_IDLE_CONTROL
float instantRpm = engine->triggerCentral.instantRpm.getInstantRpm();
engine->ignitionState.timingPidCorrection = engine->module<IdleController>()->getIdleTimingAdjustment(instantRpm);
2023-11-04 10:05:19 -07:00
#endif // EFI_IDLE_CONTROL
2019-04-12 19:07:03 -07:00
#if EFI_TUNER_STUDIO
engine->outputChannels.multiSparkCounter = engine->engineState.multispark.count;
#endif /* EFI_TUNER_STUDIO */
return engine->ignitionState.timingIatCorrection
+ engine->ignitionState.cltTimingCorrection
+ engine->ignitionState.timingPidCorrection;
2015-07-10 06:01:56 -07:00
}
/**
* @return ignition timing angle advance before TDC for Cranking
*/
2024-09-24 23:52:14 -07:00
angle_t getCrankingAdvance(float rpm, float engineLoad) {
// get advance from the separate table for Cranking
if (engineConfiguration->useSeparateAdvanceForCranking) {
return interpolate2d(rpm, config->crankingAdvanceBins, config->crankingAdvance);
}
// Interpolate the cranking timing angle to the earlier running angle for faster engine start
angle_t crankingToRunningTransitionAngle = getRunningAdvance(engineConfiguration->cranking.rpm, engineLoad);
// interpolate not from zero, but starting from min. possible rpm detected
if (rpm < minCrankingRpm || minCrankingRpm == 0)
minCrankingRpm = rpm;
return interpolateClamped(minCrankingRpm, engineConfiguration->crankingTimingAngle, engineConfiguration->cranking.rpm, crankingToRunningTransitionAngle, rpm);
}
2023-11-04 10:05:19 -07:00
#endif // EFI_ENGINE_CONTROL && EFI_SHAFT_POSITION_INPUT
2024-09-24 23:52:14 -07:00
angle_t getAdvance(float rpm, float engineLoad) {
2019-04-12 19:07:03 -07:00
#if EFI_ENGINE_CONTROL && EFI_SHAFT_POSITION_INPUT
2024-07-22 12:15:40 -07:00
if (std::isnan(engineLoad)) {
2017-12-03 13:45:39 -08:00
return 0; // any error should already be reported
}
if (engineConfiguration->timingMode == TM_FIXED) {
// fixed timing is the simple: cranking/running does not matter, no corrections!
return engineConfiguration->fixedTiming;
}
2015-07-10 06:01:56 -07:00
angle_t angle;
bool isCranking = engine->rpmCalculator.isCranking();
if (isCranking) {
angle = getCrankingAdvance(rpm, engineLoad);
assertAngleRange(angle, "crAngle", ObdCode::CUSTOM_ERR_ANGLE_CR);
2024-07-22 12:15:40 -07:00
efiAssert(ObdCode::CUSTOM_ERR_ASSERT, !std::isnan(angle), "cr_AngleN", 0);
2015-07-10 06:01:56 -07:00
} else {
angle = getRunningAdvance(rpm, engineLoad);
2024-07-22 12:15:40 -07:00
if (std::isnan(angle)) {
warning(ObdCode::CUSTOM_ERR_6610, "NaN angle from table");
2018-09-16 19:11:59 -07:00
return 0;
}
}
// Allow if we're either not cranking OR allowed to correct in cranking
bool allowCorrections = !isCranking || engineConfiguration->useAdvanceCorrectionsForCranking;
if (allowCorrections) {
angle_t correction = getAdvanceCorrections(engineLoad);
2024-07-22 12:15:40 -07:00
if (!std::isnan(correction)) { // correction could be NaN during settings update
2018-10-07 11:22:05 -07:00
angle += correction;
}
2015-07-10 06:01:56 -07:00
}
2024-07-22 12:15:40 -07:00
efiAssert(ObdCode::CUSTOM_ERR_ASSERT, !std::isnan(angle), "_AngleN5", 0);
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
}
2024-09-24 23:52:14 -07:00
angle_t getWrappedAdvance(const float rpm, const float engineLoad) {
angle_t angle = getAdvance(rpm, engineLoad) * engine->ignitionState.luaTimingMult + engine->ignitionState.luaTimingAdd;
wrapAngle(angle, "getWrappedAdvance", ObdCode::CUSTOM_ERR_ADCANCE_CALC_ANGLE);
return angle;
}
2024-09-24 23:52:14 -07:00
angle_t getCylinderIgnitionTrim(size_t cylinderNumber, float rpm, float ignitionLoad) {
return interpolate3d(
config->ignTrims[cylinderNumber].table,
config->ignTrimLoadBins, ignitionLoad,
config->ignTrimRpmBins, rpm
);
}
2024-09-24 23:52:14 -07:00
size_t getMultiSparkCount(float rpm) {
// Compute multispark (if enabled)
if (engineConfiguration->multisparkEnable
&& rpm <= engineConfiguration->multisparkMaxRpm
&& engineConfiguration->multisparkMaxExtraSparkCount > 0) {
// For zero RPM, disable multispark. We don't yet know the engine speed, so multispark may not be safe.
if (rpm == 0) {
return 0;
}
floatus_t multiDelay = 1000.0f * engineConfiguration->multisparkSparkDuration;
floatus_t multiDwell = 1000.0f * engineConfiguration->multisparkDwell;
// dwell times are below 10 seconds here so we use 32 bit type for performance reasons
engine->engineState.multispark.delay = (uint32_t)USF2NT(multiDelay);
engine->engineState.multispark.dwell = (uint32_t)USF2NT(multiDwell);
constexpr float usPerDegreeAt1Rpm = 60e6 / 360;
floatus_t usPerDegree = usPerDegreeAt1Rpm / rpm;
// How long is there for sparks? The user configured an angle, convert to time.
floatus_t additionalSparksUs = usPerDegree * engineConfiguration->multisparkMaxSparkingAngle;
// How long does one spark take?
floatus_t oneSparkTime = multiDelay + multiDwell;
// How many sparks can we fit in the alloted time?
float sparksFitInTime = additionalSparksUs / oneSparkTime;
// Take the floor (convert to uint8_t) - we want to undershoot, not overshoot
uint32_t floored = sparksFitInTime;
// Allow no more than the maximum number of extra sparks
return minI(floored, engineConfiguration->multisparkMaxExtraSparkCount);
} else {
return 0;
}
}
2024-02-03 14:02:30 -08:00
void initIgnitionAdvanceControl() {
2024-03-04 14:58:24 -08:00
tcTimingDropTable.initTable(engineConfiguration->tractionControlTimingDrop, engineConfiguration->tractionControlSlipBins, engineConfiguration->tractionControlSpeedBins);
tcSparkSkipTable.initTable(engineConfiguration->tractionControlIgnitionSkip, engineConfiguration->tractionControlSlipBins, engineConfiguration->tractionControlSpeedBins);
2024-02-03 14:02:30 -08:00
}
#endif // EFI_ENGINE_CONTROL