rusefi/firmware/controllers/algo/launch_control.cpp

216 lines
7.0 KiB
C++
Raw Normal View History

/*
* @file launch_control.cpp
*
* @date 10. sep. 2019
* Author: Ola Ruud
2024-03-09 05:39:58 -08:00
* Rework: Patryk Chmura
*/
#include "pch.h"
#if EFI_LAUNCH_CONTROL
#include "boost_control.h"
#include "launch_control.h"
#include "advance_map.h"
#include "engine_state.h"
#include "advance_map.h"
2021-11-25 19:40:19 -08:00
#include "tinymt32.h"
/**
* We can have active condition from switch or from clutch.
* In case we are dependent on VSS we just return true.
*/
2021-11-15 17:09:03 -08:00
bool LaunchControlBase::isInsideSwitchCondition() {
isSwitchActivated = engineConfiguration->launchActivationMode == SWITCH_INPUT_LAUNCH;
isClutchActivated = engineConfiguration->launchActivationMode == CLUTCH_INPUT_LAUNCH;
isBrakePedalActivated = engineConfiguration->launchActivationMode == STOP_INPUT_LAUNCH;
if (isSwitchActivated) {
2021-11-16 13:45:14 -08:00
#if !EFI_SIMULATOR
if (isBrainPinValid(engineConfiguration->launchActivatePin)) {
launchActivatePinState = engineConfiguration->launchActivateInverted ^ efiReadPin(engineConfiguration->launchActivatePin);
}
2021-11-16 13:31:35 -08:00
#endif // EFI_PROD_CODE
2021-11-15 17:09:03 -08:00
return launchActivatePinState;
} else if (isClutchActivated) {
return getClutchDownState();
} else if (isBrakePedalActivated) {
return getBrakePedalState();
} else {
// ALWAYS_ACTIVE_LAUNCH
return true;
}
}
/**
* Returns True when Vehicle speed ALLOWS launch control
*/
bool LaunchControlBase::isInsideSpeedCondition() const {
if (engineConfiguration->launchSpeedThreshold == 0) {
return true; // allow launch, speed does not matter
}
2021-10-05 16:59:07 -07:00
int speed = Sensor::getOrZero(SensorType::VehicleSpeed);
return engineConfiguration->launchSpeedThreshold > speed;
}
/**
2021-11-16 13:46:54 -08:00
* Returns false if TPS is invalid or TPS > preset threshold
*/
bool LaunchControlBase::isInsideTpsCondition() const {
auto tps = Sensor::get(SensorType::DriverThrottleIntent);
// Disallow launch without valid TPS
if (!tps.Valid) {
return false;
}
// todo: should this be 'launchTpsThreshold <= tps.Value' so that nicely calibrated TPS of zero does not prevent launch?
return engineConfiguration->launchTpsThreshold < tps.Value;
}
2024-09-25 00:20:18 -07:00
LaunchCondition LaunchControlBase::calculateRPMLaunchCondition(const float rpm) {
if ((engineConfiguration->launchActivationMode == SWITCH_INPUT_LAUNCH)
&& (engineConfiguration->torqueReductionActivationMode == LAUNCH_BUTTON)
&& engineConfiguration->torqueReductionEnabled
&& (engineConfiguration->torqueReductionArmingRpm <= rpm)
) {
// We need perform Shift Torque Reduction stuff (see
// https://github.com/rusefi/rusefi/issues/5608#issuecomment-2391500472 and
// https://github.com/rusefi/rusefi/issues/5608#issuecomment-2391772899 for details)
return LaunchCondition::NotMet;
}
const int launchRpm = engineConfiguration->launchRpm;
const int preLaunchRpm = launchRpm - engineConfiguration->launchRpmWindow;
if (rpm < preLaunchRpm) {
return LaunchCondition::NotMet;
} else if (launchRpm <= rpm) {
return LaunchCondition::Launch;
} else {
return LaunchCondition::PreLaunch;
}
}
2024-09-25 00:20:18 -07:00
LaunchCondition LaunchControlBase::calculateLaunchCondition(const float rpm) {
const LaunchCondition currentRpmLaunchCondition = calculateRPMLaunchCondition(rpm);
activateSwitchCondition = isInsideSwitchCondition();
rpmLaunchCondition = (currentRpmLaunchCondition == LaunchCondition::Launch);
rpmPreLaunchCondition = (currentRpmLaunchCondition == LaunchCondition::PreLaunch);
speedCondition = isInsideSpeedCondition();
tpsCondition = isInsideTpsCondition();
if(speedCondition && activateSwitchCondition && tpsCondition) {
return currentRpmLaunchCondition;
} else {
return LaunchCondition::NotMet;
}
}
LaunchControlBase::LaunchControlBase() {
launchActivatePinState = false;
isPreLaunchCondition = false;
isLaunchCondition = false;
}
float LaunchControlBase::getFuelCoefficient() const {
2022-11-13 17:23:19 -08:00
return 1 + (isLaunchCondition && engineConfiguration->launchControlEnabled ? engineConfiguration->launchFuelAdderPercent / 100.0 : 0);
}
void LaunchControlBase::update() {
if (!engineConfiguration->launchControlEnabled) {
return;
}
2024-09-25 00:20:18 -07:00
const float rpm = Sensor::getOrZero(SensorType::Rpm);
const LaunchCondition launchCondition = calculateLaunchCondition(rpm);
isLaunchCondition = (launchCondition == LaunchCondition::Launch);
isPreLaunchCondition = (launchCondition == LaunchCondition::PreLaunch);
//and still recalculate in case user changed the values
2024-05-22 13:15:30 -07:00
retardThresholdRpm = engineConfiguration->launchRpm;
sparkSkipRatio = calculateSparkSkipRatio(rpm);
}
2021-11-15 16:48:55 -08:00
bool LaunchControlBase::isLaunchRpmRetardCondition() const {
return isLaunchCondition && engineConfiguration->launchControlEnabled && (retardThresholdRpm < Sensor::getOrZero(SensorType::Rpm));
2021-11-15 16:48:55 -08:00
}
bool LaunchControlBase::isLaunchSparkRpmRetardCondition() const {
return isLaunchRpmRetardCondition() && engineConfiguration->launchSparkCutEnable;
}
bool LaunchControlBase::isLaunchFuelRpmRetardCondition() const {
return isLaunchRpmRetardCondition() && engineConfiguration->launchFuelCutEnable;
}
2024-09-25 21:34:00 -07:00
float LaunchControlBase::calculateSparkSkipRatio(const float rpm) const {
float result = 0.0f;
if (engineConfiguration->launchControlEnabled && engineConfiguration->launchSparkCutEnable) {
if (isLaunchCondition) {
result = 1.0f;
} else if (isPreLaunchCondition) {
const int launchRpm = engineConfiguration->launchRpm;
const int sparkSkipStartRpm = launchRpm - engineConfiguration->launchRpmWindow;
if (sparkSkipStartRpm <= rpm) {
const float initialIgnitionCutRatio = engineConfiguration->initialIgnitionCutPercent / 100.0f;
const int sparkSkipEndRpm = launchRpm - engineConfiguration->launchCorrectionsEndRpm;
const float finalIgnitionCutRatio = engineConfiguration->finalIgnitionCutPercentBeforeLaunch / 100.0f;
result = interpolateClamped(sparkSkipStartRpm, initialIgnitionCutRatio, sparkSkipEndRpm, finalIgnitionCutRatio, rpm);
}
}
}
return result;
}
SoftSparkLimiter::SoftSparkLimiter(const bool p_allowHardCut)
: allowHardCut(p_allowHardCut) {
2023-05-25 15:52:19 -07:00
#if EFI_UNIT_TEST
initLaunchControl();
#endif // EFI_UNIT_TEST
2023-05-25 09:41:11 -07:00
}
void SoftSparkLimiter::updateTargetSkipRatio(
const float luaSparkSkip,
const float tractionControlSparkSkip,
const float launchOrShiftTorqueReductionControllerSparkSkipRatio
) {
targetSkipRatio = luaSparkSkip;
if (engineConfiguration->useHardSkipInTraction) {
if (allowHardCut) {
targetSkipRatio += tractionControlSparkSkip;
}
} else if (!allowHardCut) {
targetSkipRatio += tractionControlSparkSkip;
}
if (allowHardCut) {
/*
* We are applying launch controller spark skip ratio only for hard skip limiter (see
* https://github.com/rusefi/rusefi/issues/6566#issuecomment-2153149902).
*/
targetSkipRatio += launchOrShiftTorqueReductionControllerSparkSkipRatio;
}
}
2021-11-25 19:40:19 -08:00
static tinymt32_t tinymt;
2021-11-16 10:15:12 -08:00
bool SoftSparkLimiter::shouldSkip() {
2023-05-25 12:17:07 -07:00
if (targetSkipRatio == 0 || (!allowHardCut && wasJustSkipped)) {
2021-11-16 10:15:12 -08:00
wasJustSkipped = false;
return false;
}
2021-11-25 19:40:19 -08:00
float r = tinymt32_generate_float(&tinymt);
2023-05-25 12:17:07 -07:00
wasJustSkipped = r < (allowHardCut ? 1 : 2) * targetSkipRatio;
2021-11-16 10:15:12 -08:00
return wasJustSkipped;
}
void initLaunchControl() {
2021-11-25 19:40:19 -08:00
tinymt32_init(&tinymt, 1345135);
}
#endif /* EFI_LAUNCH_CONTROL */