From a61043e5703893636124eaa920f8e3f179ee15db Mon Sep 17 00:00:00 2001 From: Andrey Date: Tue, 16 Nov 2021 13:15:12 -0500 Subject: [PATCH] proportional spark cut #3427 --- firmware/controllers/algo/engine.h | 1 + firmware/controllers/algo/launch_control.cpp | 15 +++++++++++++++ firmware/controllers/algo/launch_control.h | 14 ++++++++++++++ firmware/controllers/lua/lua_hooks.cpp | 6 ++++++ 4 files changed, 36 insertions(+) diff --git a/firmware/controllers/algo/engine.h b/firmware/controllers/algo/engine.h index ac3bda259c..463e4a1d6a 100644 --- a/firmware/controllers/algo/engine.h +++ b/firmware/controllers/algo/engine.h @@ -115,6 +115,7 @@ public: GearControllerBase *gearController; LaunchControlBase launchController; + SoftSparkLimiter softSparkLimiter; efitick_t mostRecentSparkEvent; efitick_t mostRecentTimeBetweenSparkEvents; diff --git a/firmware/controllers/algo/launch_control.cpp b/firmware/controllers/algo/launch_control.cpp index 53cc6d6697..768c793842 100644 --- a/firmware/controllers/algo/launch_control.cpp +++ b/firmware/controllers/algo/launch_control.cpp @@ -137,6 +137,21 @@ bool LaunchControlBase::isLaunchFuelRpmRetardCondition() const { return isLaunchRpmRetardCondition() && engineConfiguration->launchFuelCutEnable; } +void SoftSparkLimiter::setTargetSkipRatio(float targetSkipRatio) { + this->targetSkipRatio = targetSkipRatio; +} + +bool SoftSparkLimiter::shouldSkip() { + if (targetSkipRatio == 0 || wasJustSkipped) { + wasJustSkipped = false; + return false; + } + + float r = static_cast (rand()) / static_cast (RAND_MAX); + wasJustSkipped = r < 2 * targetSkipRatio; + return wasJustSkipped; +} + void initLaunchControl() { engine->launchController.inject(); } diff --git a/firmware/controllers/algo/launch_control.h b/firmware/controllers/algo/launch_control.h index dc70fba076..b42b569cdc 100644 --- a/firmware/controllers/algo/launch_control.h +++ b/firmware/controllers/algo/launch_control.h @@ -35,3 +35,17 @@ private: Timer m_launchTimer; }; + + +class SoftSparkLimiter { +public: + /** + * targetSkipRatio of '0' means 'do not skip', would always return false + */ + void setTargetSkipRatio(float targetSkipRatio); + + bool shouldSkip(); +private: + bool wasJustSkipped = false; + float targetSkipRatio = 0; +}; diff --git a/firmware/controllers/lua/lua_hooks.cpp b/firmware/controllers/lua/lua_hooks.cpp index 018006a927..06ad1adc09 100644 --- a/firmware/controllers/lua/lua_hooks.cpp +++ b/firmware/controllers/lua/lua_hooks.cpp @@ -486,6 +486,12 @@ void configureRusefiLuaHooks(lua_State* l) { return 1; }); + lua_register(l, "setSparkSkipRatio", [](lua_State* l) { + auto targetSkipRatio = luaL_checknumber(l, 1); + engine->softSparkLimiter.setTargetSkipRatio(targetSkipRatio); + return 1; + }); + #if !EFI_UNIT_TEST lua_register(l, "startPwm", lua_startPwm); lua_register(l, "setPwmDuty", lua_setPwmDuty);