From 9ce3f1413786177ce548bf4a2ebe3c9312ea1f6b Mon Sep 17 00:00:00 2001 From: rusefillc Date: Thu, 10 Feb 2022 21:01:15 -0500 Subject: [PATCH] Lua CAN relay performance #3911 --- firmware/controllers/lua/lua.cpp | 11 +++++++---- firmware/tunerstudio/rusefi.input | 2 ++ 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/firmware/controllers/lua/lua.cpp b/firmware/controllers/lua/lua.cpp index a2bdb56495..a0e070f4fc 100644 --- a/firmware/controllers/lua/lua.cpp +++ b/firmware/controllers/lua/lua.cpp @@ -125,7 +125,7 @@ static void* myAlloc(void* /*ud*/, void* ptr, size_t /*osize*/, size_t nsize) { } #endif // EFI_PROD_CODE -static int luaTickPeriodMs; +static int luaTickPeriodUs; static int lua_setTickRate(lua_State* l) { float freq = luaL_checknumber(l, 1); @@ -133,7 +133,7 @@ static int lua_setTickRate(lua_State* l) { // Limit to 1..100 hz freq = clampF(1, freq, 100); - luaTickPeriodMs = 1000.0f / freq; + luaTickPeriodUs = 1000000.0f / freq; return 0; } @@ -305,13 +305,14 @@ static bool runOneLua(lua_Alloc alloc, const char* script) { } // Reset default tick rate - luaTickPeriodMs = 100; + luaTickPeriodUs = MS2US(100); if (!loadScript(ls, script)) { return false; } while (!needsReset && !chThdShouldTerminateX()) { + efitick_t beforeNt = getTimeNowNt(); #if EFI_CAN_SUPPORT // First, process any pending can RX messages doLuaCanRx(ls); @@ -322,7 +323,9 @@ static bool runOneLua(lua_Alloc alloc, const char* script) { invokeTick(ls); - chThdSleepMilliseconds(luaTickPeriodMs); + chThdSleep(TIME_US2I(luaTickPeriodUs)); + engine->outputChannels.luaLastCycleDuration = (getTimeNowNt() - beforeNt); + engine->outputChannels.luaInvocationCounter++; } #if EFI_CAN_SUPPORT diff --git a/firmware/tunerstudio/rusefi.input b/firmware/tunerstudio/rusefi.input index aa600cb1d8..1dff71f747 100644 --- a/firmware/tunerstudio/rusefi.input +++ b/firmware/tunerstudio/rusefi.input @@ -987,6 +987,8 @@ triggerSecondaryFallGauge = triggerSecondaryFall,"triggerSecondaryFall", "", -10 triggerSecondaryRiseGauge = triggerSecondaryRise,"triggerSecondaryRise", "", -10000.0,10000.0, -10000.0,10000.0, -10000.0,10000.0, 3,3 triggerVvtFallGauge = triggerVvtFall,"triggerVvtFall", "", -10000.0,10000.0, -10000.0,10000.0, -10000.0,10000.0, 3,3 triggerVvtRiseGauge = triggerVvtRise,"triggerVvtRise", "", -10000.0,10000.0, -10000.0,10000.0, -10000.0,10000.0, 3,3 +luaInvocationCounterGauge = luaInvocationCounter,"luaInvocationCounter", "count", 0.0,0.0, 0.0,0.0, 0.0,0.0, 0,0 +luaLastCycleDurationGauge = luaLastCycleDuration,"luaLastCycleDuration", "nt", 0.0,0.0, 0.0,0.0, 0.0,0.0, 0,0 gaugeCategory = PIDs alternatorStatus_iTermGauge = alternatorStatus_iTerm,"alternatorStatus_ iTerm", "", -10000.0,10000.0, -10000.0,10000.0, -10000.0,10000.0, 3,3