From 821276197bd21e14f4a3fbd7af6222f824947269 Mon Sep 17 00:00:00 2001 From: Andrey Date: Sun, 7 Nov 2021 09:49:47 -0500 Subject: [PATCH] lua pid class #3411 --- firmware/controllers/lua/lua_hooks.cpp | 80 ++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) diff --git a/firmware/controllers/lua/lua_hooks.cpp b/firmware/controllers/lua/lua_hooks.cpp index dfb527068f..b95c8f2ec7 100644 --- a/firmware/controllers/lua/lua_hooks.cpp +++ b/firmware/controllers/lua/lua_hooks.cpp @@ -405,6 +405,71 @@ struct LuaSensor : public StoredValueSensor { void showInfo(const char*) const {} }; +struct LuaPid { + LuaPid + () +// todo (float kp, float ki, float kd, float min, float max) + : m_pid(&m_params) + { + m_params.pFactor = 0; + m_params.iFactor = 0; + m_params.dFactor = 0; + + m_params.offset = 0; + m_params.periodMs = 0; + m_params.minValue = 0; + m_params.maxValue = 0; + + m_lastUpdate.reset(); + } + + float get(float input) { +#if EFI_UNIT_TEST + extern int timeNowUs; + // this is how we avoid zero dt + timeNowUs += 1000; +#endif + float dt = m_lastUpdate.getElapsedSecondsAndReset(getTimeNowNt()); + + return m_pid.getOutput(target, input, dt); + } + + void setTarget(float value) { + target = value; + } + + void setP(float value) { + m_params.pFactor = value; + } + + void setI(float value) { + m_params.iFactor = value; + } + + void setD(float value) { + m_params.dFactor = value; + } + + void setMinValue(float value) { + m_params.minValue = value; + } + + void setMaxValue(float value) { + m_params.maxValue = value; + } + + void reset() { + m_pid.reset(); + } + +private: + Pid m_pid; + Timer m_lastUpdate; + pid_s m_params; + // ugly as hell, a way to move forward while we wait for https://github.com/gengyong/luaaa/issues/7 + float target; +}; + void configureRusefiLuaHooks(lua_State* l) { LuaClass luaTimer(l, "Timer"); @@ -419,6 +484,21 @@ void configureRusefiLuaHooks(lua_State* l) { .fun("set", &LuaSensor::set) .fun("invalidate", &LuaSensor::invalidate); + LuaClass luaPid(l, "Pid"); + luaPid + .ctor() +/* + .fun("get", &LuaPid::get) + .fun("setTarget", &LuaPid::setTarget) + .fun("setP", &LuaPid::setP) + .fun("setI", &LuaPid::setI) + .fun("setD", &LuaPid::setD) + .fun("setMinValue", &LuaPid::setMinValue) + .fun("setMaxValue", &LuaPid::setMaxValue) + .fun("reset", &LuaPid::reset) +*/ + ; + configureRusefiLuaUtilHooks(l); lua_register(l, "readPin", lua_readpin);