From a58ce665816c93c6120be9a34312ebebd57375cf Mon Sep 17 00:00:00 2001 From: Matthew Kennedy Date: Fri, 13 Sep 2024 02:52:10 -0400 Subject: [PATCH] AC state change uses a timer --- firmware/controllers/actuators/ac_control.h | 2 ++ firmware/controllers/actuators/ac_control.txt | 3 --- firmware/controllers/actuators/idle_thread.cpp | 2 +- firmware/controllers/algo/engine.cpp | 2 +- firmware/controllers/lua/lua_hooks.cpp | 2 +- 5 files changed, 5 insertions(+), 6 deletions(-) diff --git a/firmware/controllers/actuators/ac_control.h b/firmware/controllers/actuators/ac_control.h index d643e589fd..005d20d54d 100644 --- a/firmware/controllers/actuators/ac_control.h +++ b/firmware/controllers/actuators/ac_control.h @@ -13,6 +13,8 @@ public: virtual bool isAcEnabled() const; + Timer timeSinceStateChange; + private: bool getAcState(); diff --git a/firmware/controllers/actuators/ac_control.txt b/firmware/controllers/actuators/ac_control.txt index bc639f18d0..2c4eb4632e 100644 --- a/firmware/controllers/actuators/ac_control.txt +++ b/firmware/controllers/actuators/ac_control.txt @@ -15,7 +15,4 @@ bit acCompressorState;AC compressor on bit acPressureTooLow;AC pressure too low bit acPressureTooHigh;AC pressure too high -! todo: extract some helper which would contain boolean state and most recent toggle time? -int acSwitchLastChangeTimeMs; - end_struct diff --git a/firmware/controllers/actuators/idle_thread.cpp b/firmware/controllers/actuators/idle_thread.cpp index b5612dc54b..8f77e5f79b 100644 --- a/firmware/controllers/actuators/idle_thread.cpp +++ b/firmware/controllers/actuators/idle_thread.cpp @@ -254,7 +254,7 @@ float IdleController::getClosedLoop(IIdleController::Phase phase, float tpsPos, return 0; } - bool acToggleJustTouched = (US2MS(nowUs) - engine->module().unmock().acSwitchLastChangeTimeMs) < 500/*ms*/; + bool acToggleJustTouched = engine->module().unmock().timeSinceStateChange.getElapsedSeconds() < 0.5f /*second*/; // check if within the dead zone isInDeadZone = !acToggleJustTouched && absI(rpm - targetRpm) <= engineConfiguration->idlePidRpmDeadZone; if (isInDeadZone) { diff --git a/firmware/controllers/algo/engine.cpp b/firmware/controllers/algo/engine.cpp index c7fb0a1e6c..592d54d18a 100644 --- a/firmware/controllers/algo/engine.cpp +++ b/firmware/controllers/algo/engine.cpp @@ -281,7 +281,7 @@ extern bool kAcRequestState; } AcController & acController = engine->module().unmock(); if (engine->acButtonSwitchedState.update(currentState)) { - acController.acSwitchLastChangeTimeMs = US2MS(getTimeNowUs()); + acController.timeSinceStateChange.reset(); } } diff --git a/firmware/controllers/lua/lua_hooks.cpp b/firmware/controllers/lua/lua_hooks.cpp index 599052d8e4..a4bf3c0d50 100644 --- a/firmware/controllers/lua/lua_hooks.cpp +++ b/firmware/controllers/lua/lua_hooks.cpp @@ -963,7 +963,7 @@ void configureRusefiLuaHooks(lua_State* lState) { return 0; }); lua_register(lState, "getTimeSinceAcToggleMs", [](lua_State* l) { - int result = US2MS(getTimeNowUs()) - engine->module().unmock().acSwitchLastChangeTimeMs; + float result = engine->module().unmock().timeSinceStateChange.getElapsedSeconds() * 1000; lua_pushnumber(l, result); return 1; });