From 26e5f99dcf551622399fa4d5818c496897a3c9c2 Mon Sep 17 00:00:00 2001 From: Andrey Date: Fri, 5 Jul 2024 16:57:17 -0400 Subject: [PATCH] only:extracting lua_pid --- firmware/controllers/lua/lua_hooks.cpp | 100 +----------------------- firmware/controllers/lua/lua_pid.h | 102 +++++++++++++++++++++++++ 2 files changed, 103 insertions(+), 99 deletions(-) create mode 100644 firmware/controllers/lua/lua_pid.h diff --git a/firmware/controllers/lua/lua_hooks.cpp b/firmware/controllers/lua/lua_hooks.cpp index abd805067d..7a89a489a8 100644 --- a/firmware/controllers/lua/lua_hooks.cpp +++ b/firmware/controllers/lua/lua_hooks.cpp @@ -10,6 +10,7 @@ #include "value_lookup.h" #include "can_filter.h" #include "tunerstudio.h" +#include "lua_pid.h" #if EFI_PROD_CODE && HW_HELLEN #include "hellen_meta.h" @@ -522,105 +523,6 @@ private: bool m_isRedundant = false; }; -struct LuaPid final { - LuaPid() = default; - - LuaPid(float kp, float ki, float kd, float min, float max) - : m_pid(&m_params) - { - m_params.pFactor = kp; - m_params.iFactor = ki; - m_params.dFactor = kd; - - m_params.offset = 0; - m_params.periodMs = 0; - m_params.minValue = min; - m_params.maxValue = max; - - m_lastUpdate.reset(); - } - - float get(float target, float input) { -#if EFI_UNIT_TEST - // this is how we avoid zero dt - advanceTimeUs(1000); -#endif - - float dt = m_lastUpdate.getElapsedSecondsAndReset(getTimeNowNt()); - - return m_pid.getOutput(target, input, dt); - } - - void setOffset(float offset) { - m_params.offset = offset; - reset(); - } - - void reset() { - m_pid.reset(); - } - -private: - Pid m_pid; - Timer m_lastUpdate; - pid_s m_params; -}; - -// todo: use templates and reduce duplication between LuaPid and LuaIndustrialPid? -struct LuaIndustrialPid final { - LuaIndustrialPid() = default; - - LuaIndustrialPid(float kp, float ki, float kd, float min, float max) - : m_pid(&m_params) - { - m_params.pFactor = kp; - m_params.iFactor = ki; - m_params.dFactor = kd; - - m_params.offset = 0; - m_params.periodMs = 0; - m_params.minValue = min; - m_params.maxValue = max; - - m_lastUpdate.reset(); - } - - float get(float target, float input) { -#if EFI_UNIT_TEST - // this is how we avoid zero dt - advanceTimeUs(1000); -#endif - - float dt = m_lastUpdate.getElapsedSecondsAndReset(getTimeNowNt()); - - return m_pid.getOutput(target, input, dt); - } - - void setOffset(float offset) { - m_params.offset = offset; - reset(); - } - - void setDerivativeFilterLoss(float derivativeFilterLoss) { - m_pid.derivativeFilterLoss = derivativeFilterLoss; - reset(); - } - - void setAntiwindupFreq(float antiwindupFreq) { - m_pid.antiwindupFreq = antiwindupFreq; - reset(); - } - - void reset() { - m_pid.reset(); - } - -private: - PidIndustrial m_pid; - Timer m_lastUpdate; - pid_s m_params; -}; - static bool isFunction(lua_State* l, int idx) { return lua_type(l, idx) == LUA_TFUNCTION; } diff --git a/firmware/controllers/lua/lua_pid.h b/firmware/controllers/lua/lua_pid.h new file mode 100644 index 0000000000..1de769715f --- /dev/null +++ b/firmware/controllers/lua/lua_pid.h @@ -0,0 +1,102 @@ +#pragma once + +#include "pch.h" + +struct LuaPid final { + LuaPid() = default; + + LuaPid(float kp, float ki, float kd, float min, float max) + : m_pid(&m_params) + { + m_params.pFactor = kp; + m_params.iFactor = ki; + m_params.dFactor = kd; + + m_params.offset = 0; + m_params.periodMs = 0; + m_params.minValue = min; + m_params.maxValue = max; + + m_lastUpdate.reset(); + } + + float get(float target, float input) { +#if EFI_UNIT_TEST + // this is how we avoid zero dt + advanceTimeUs(1000); +#endif + + float dt = m_lastUpdate.getElapsedSecondsAndReset(getTimeNowNt()); + + return m_pid.getOutput(target, input, dt); + } + + void setOffset(float offset) { + m_params.offset = offset; + reset(); + } + + void reset() { + m_pid.reset(); + } + +private: + Pid m_pid; + Timer m_lastUpdate; + pid_s m_params; +}; + +// todo: use templates and reduce duplication between LuaPid and LuaIndustrialPid? +struct LuaIndustrialPid final { + LuaIndustrialPid() = default; + + LuaIndustrialPid(float kp, float ki, float kd, float min, float max) + : m_pid(&m_params) + { + m_params.pFactor = kp; + m_params.iFactor = ki; + m_params.dFactor = kd; + + m_params.offset = 0; + m_params.periodMs = 0; + m_params.minValue = min; + m_params.maxValue = max; + + m_lastUpdate.reset(); + } + + float get(float target, float input) { +#if EFI_UNIT_TEST + // this is how we avoid zero dt + advanceTimeUs(1000); +#endif + + float dt = m_lastUpdate.getElapsedSecondsAndReset(getTimeNowNt()); + + return m_pid.getOutput(target, input, dt); + } + + void setOffset(float offset) { + m_params.offset = offset; + reset(); + } + + void setDerivativeFilterLoss(float derivativeFilterLoss) { + m_pid.derivativeFilterLoss = derivativeFilterLoss; + reset(); + } + + void setAntiwindupFreq(float antiwindupFreq) { + m_pid.antiwindupFreq = antiwindupFreq; + reset(); + } + + void reset() { + m_pid.reset(); + } + +private: + PidIndustrial m_pid; + Timer m_lastUpdate; + pid_s m_params; +};