diff --git a/firmware/controllers/algo/advance_map.cpp b/firmware/controllers/algo/advance_map.cpp index 00c6dbda2a..0627b9a3c4 100644 --- a/firmware/controllers/algo/advance_map.cpp +++ b/firmware/controllers/algo/advance_map.cpp @@ -52,6 +52,18 @@ static angle_t getRunningAdvance(int rpm, float engineLoad) { config->ignitionRpmBins, rpm ); +#if EFI_ANTILAG_SYSTEM + if (engine->antilagController.isAntilagCondition) { + auto tps = Sensor::get(SensorType::Tps1); + engine->antilagController.timingALSCorrection = interpolate3d( + config->ALSTimingRetardTable, + config->alsIgnRetardLoadBins, tps.Value, + 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); diff --git a/firmware/controllers/algo/antilag_system.cpp b/firmware/controllers/algo/antilag_system.cpp index cce51355ab..e2db069a1f 100644 --- a/firmware/controllers/algo/antilag_system.cpp +++ b/firmware/controllers/algo/antilag_system.cpp @@ -14,7 +14,68 @@ #include "engine_state.h" #include "advance_map.h" -AntilagSystemBase::AntilagSystemBase() { +bool AntilagSystemBase::isInsideALSSwitchCondition() { + isALSSwitchActivated = engineConfiguration->antiLagActivationMode != SWITCH_INPUT_ANTILAG; + + if (isALSSwitchActivated) { + if (isBrainPinValid(engineConfiguration->ALSActivatePin)) { + ALSActivatePinState = engineConfiguration->ALSActivateInverted ^ efiReadPin(engineConfiguration->ALSActivatePin); + } + return ALSActivatePinState; + } else { + // ALWAYS_ACTIVE_ANTILAG + return true; + } +} + +bool AntilagSystemBase::isALSMinRPMCondition() const { + int rpm = Sensor::getOrZero(SensorType::Rpm); + + return engineConfiguration->ALSMinRPM < rpm; +} + +bool AntilagSystemBase::isALSMaxRPMCondition() const { + int rpm = Sensor::getOrZero(SensorType::Rpm); + + return engineConfiguration->ALSMaxRPM > rpm; +} + +bool AntilagSystemBase::isALSMinCLTCondition() const { + int clt = Sensor::getOrZero(SensorType::Clt); + + return engineConfiguration->ALSMinCLT < clt; +} + +bool AntilagSystemBase::isALSMaxCLTCondition() const { + int clt = Sensor::getOrZero(SensorType::Clt); + + return engineConfiguration->ALSMaxCLT > clt; +} + +bool AntilagSystemBase::isALSMaxTPSCondition() const { + int tps = Sensor::getOrZero(SensorType::Tps1); + + return engineConfiguration->ALSMaxTPS > tps; +} + +bool AntilagSystemBase::isAntilagConditionMet() { + + ALSMinRPMCondition = isALSMinRPMCondition(); + ALSMaxRPMCondition = isALSMaxRPMCondition(); + ALSMinCLTCondition = isALSMinCLTCondition(); + ALSMaxCLTCondition = isALSMaxCLTCondition(); + ALSMaxTPSCondition = isALSMaxTPSCondition(); + ALSSwitchCondition = isInsideALSSwitchCondition(); + + return ALSMinRPMCondition && ALSMaxRPMCondition && ALSMinCLTCondition && ALSMaxCLTCondition && ALSMaxTPSCondition && ALSSwitchCondition; +} + +void AntilagSystemBase::update() { + if (!engineConfiguration->antiLagEnabled) { + return; + } + + isAntilagCondition = isAntilagConditionMet(); } #endif /* EFI_ANTILAG_SYSTEM */ diff --git a/firmware/controllers/algo/antilag_system.h b/firmware/controllers/algo/antilag_system.h index 6a97cec713..cb394bccc3 100644 --- a/firmware/controllers/algo/antilag_system.h +++ b/firmware/controllers/algo/antilag_system.h @@ -14,8 +14,6 @@ void initAntilagSystem(); class AntilagSystemBase : public antilag_system_state_s { public: - AntilagSystemBase(); - void update(); bool isALSMinRPMCondition() const; @@ -23,7 +21,7 @@ public: bool isALSMinCLTCondition() const; bool isALSMaxCLTCondition() const; bool isALSMaxTPSCondition() const; - bool isAntilagConditionMet(int rpm); + bool isAntilagConditionMet(); bool isInsideALSSwitchCondition(); }; diff --git a/firmware/controllers/algo/fuel_math.cpp b/firmware/controllers/algo/fuel_math.cpp index 7cbd7c7c1e..5c8f2deefc 100644 --- a/firmware/controllers/algo/fuel_math.cpp +++ b/firmware/controllers/algo/fuel_math.cpp @@ -129,6 +129,8 @@ float getRunningFuel(float baseFuel) { float runningFuel = baseFuel * baroCorrection * iatCorrection * cltCorrection * postCrankingFuelCorrection; + runningFuel *= (1 + engine->antilagController.fuelALSCorrection); + #if EFI_LAUNCH_CONTROL runningFuel *= engine->launchController.getFuelCoefficient(); #endif @@ -391,6 +393,23 @@ float getBaroCorrection() { } } +auto tps = Sensor::get(SensorType::Tps1); + auto rpm = Sensor::get(SensorType::Rpm); +float getfuelALSCorrection(int rpm, float engineLoad) { +#if EFI_ANTILAG_SYSTEM + if (engine->antilagController.isAntilagCondition) { + auto AlsFuelAdd = interpolate3d( + config->ALSFuelAdjustment, + config->alsFuelAdjustmentLoadBins, tps.Value, + config->alsFuelAdjustmentrpmBins, rpm + ); + return AlsFuelAdd; + } else { + return 1; + } +#endif /* EFI_ANTILAG_SYSTEM */ +} + #if EFI_ENGINE_CONTROL /** * @return Duration of fuel injection while craning