From 0978722225e133270b3ecaaf695947ff35afcb23 Mon Sep 17 00:00:00 2001 From: Matthew Kennedy Date: Thu, 23 Sep 2021 20:50:47 -0700 Subject: [PATCH] lua hook for timing add (#3260) * load script * test * needs more rams * embiggen stack * init system lua from lua thread * de-embiggen stack * infra * hook * system needs a little more * semicolon Co-authored-by: Matthew Kennedy --- firmware/controllers/algo/engine2.cpp | 2 +- firmware/controllers/algo/engine_state.h | 6 ++++++ firmware/controllers/lua/lua.cpp | 3 +++ firmware/controllers/lua/lua_hooks.cpp | 8 ++++++++ 4 files changed, 18 insertions(+), 1 deletion(-) diff --git a/firmware/controllers/algo/engine2.cpp b/firmware/controllers/algo/engine2.cpp index 1cfeac0f55..3937e0f6c7 100644 --- a/firmware/controllers/algo/engine2.cpp +++ b/firmware/controllers/algo/engine2.cpp @@ -163,7 +163,7 @@ void EngineState::periodicFastCallback(DECLARE_ENGINE_PARAMETER_SIGNATURE) { injectionOffset = getInjectionOffset(rpm, fuelLoad PASS_ENGINE_PARAMETER_SUFFIX); float ignitionLoad = getIgnitionLoad(PASS_ENGINE_PARAMETER_SIGNATURE); - timingAdvance = getAdvance(rpm, ignitionLoad PASS_ENGINE_PARAMETER_SUFFIX); + timingAdvance = getAdvance(rpm, ignitionLoad PASS_ENGINE_PARAMETER_SUFFIX) + luaAdjustments.ignitionTimingAdd; // TODO: calculate me from a table! trailingSparkAngle = CONFIG(trailingSparkAngle); diff --git a/firmware/controllers/algo/engine_state.h b/firmware/controllers/algo/engine_state.h index 07dd6252f2..372ccd8ec9 100644 --- a/firmware/controllers/algo/engine_state.h +++ b/firmware/controllers/algo/engine_state.h @@ -13,6 +13,10 @@ #include "pid.h" #include "engine_state_generated.h" +struct LuaAdjustments { + float ignitionTimingAdd = 0; +}; + class EngineState : public engine_state2_s { public: EngineState(); @@ -86,4 +90,6 @@ public: float targetLambda = 0.0f; float stoichiometricRatio = 0.0f; + + LuaAdjustments luaAdjustments; }; diff --git a/firmware/controllers/lua/lua.cpp b/firmware/controllers/lua/lua.cpp index 6407a63f17..92b34d7aea 100644 --- a/firmware/controllers/lua/lua.cpp +++ b/firmware/controllers/lua/lua.cpp @@ -344,6 +344,9 @@ void LuaThread::ThreadTask() { while (!chThdShouldTerminateX()) { bool wasOk = runOneLua(myAlloc<0>, config->luaScript); + // Reset any lua adjustments the script made + ENGINE(engineState).luaAdjustments = {}; + if (!wasOk) { // Something went wrong executing the script, spin // until reset invoked (maybe the user fixed the script) diff --git a/firmware/controllers/lua/lua_hooks.cpp b/firmware/controllers/lua/lua_hooks.cpp index fc2b36075c..bff02804ff 100644 --- a/firmware/controllers/lua/lua_hooks.cpp +++ b/firmware/controllers/lua/lua_hooks.cpp @@ -307,6 +307,12 @@ static int lua_stopEngine(lua_State*) { return 0; } + +static int lua_setTimingAdd(lua_State* l) { + ENGINE(engineState).luaAdjustments.ignitionTimingAdd = luaL_checknumber(l, 1); + + return 0; +} #endif // EFI_UNIT_TEST void configureRusefiLuaHooks(lua_State* l) { @@ -338,5 +344,7 @@ void configureRusefiLuaHooks(lua_State* l) { lua_register(l, "setAirmass", lua_setAirmass); lua_register(l, "stopEngine", lua_stopEngine); + + lua_register(l, "setTimingAdd", lua_setTimingAdd); #endif }