From 73b05d2e20efb4087dbd363284384073a3ef465a Mon Sep 17 00:00:00 2001 From: Matthew Kennedy Date: Sat, 13 Nov 2021 00:18:16 -0800 Subject: [PATCH] lua pid class (#3411) * lua pid * no luaaa stl * update luaaa * finality * luaaa * luaaa * = default * bad merge * gitmodules * lua * proteus demo script Co-authored-by: Matthew Kennedy --- firmware/config/engines/custom_engine.cpp | 8 +-- firmware/controllers/lua/lua_hooks.cpp | 64 +++++------------------ unit_tests/tests/lua/test_lua_hooks.cpp | 17 ++---- 3 files changed, 21 insertions(+), 68 deletions(-) diff --git a/firmware/config/engines/custom_engine.cpp b/firmware/config/engines/custom_engine.cpp index 21bff1ae2c..8591f2f212 100644 --- a/firmware/config/engines/custom_engine.cpp +++ b/firmware/config/engines/custom_engine.cpp @@ -905,10 +905,7 @@ startPwm(1, 80, 1.0) -- disable startPwm(2, 80, 0.0) -pid = Pid.new() -pid:setP(2) -pid:setMinValue(-100) -pid:setMaxValue(100) +pid = Pid.new(2, 0, 0, -100, 100) biasCurveIndex = findCurveIndex("bias") @@ -926,8 +923,7 @@ function onTick() tps = (tps == nil and 'invalid TPS' or tps) print('Tps ' .. tps) - pid:setTarget(target) - local output = pid:get(tps) + local output = pid:get(target, tps) local bias = curve(biasCurveIndex, target) print('bias ' .. bias) diff --git a/firmware/controllers/lua/lua_hooks.cpp b/firmware/controllers/lua/lua_hooks.cpp index 148fae9cf0..fa9f750b97 100644 --- a/firmware/controllers/lua/lua_hooks.cpp +++ b/firmware/controllers/lua/lua_hooks.cpp @@ -385,7 +385,7 @@ static int lua_canRxAdd(lua_State* l) { } #endif // EFI_CAN_SUPPORT -struct LuaSensor : public StoredValueSensor { +struct LuaSensor final : public StoredValueSensor { LuaSensor() : LuaSensor("Invalid") { } ~LuaSensor() { @@ -409,59 +409,36 @@ struct LuaSensor : public StoredValueSensor { void showInfo(const char*) const {} }; -struct LuaPid { - LuaPid - () -// todo (float kp, float ki, float kd, float min, float max) +struct LuaPid final { + LuaPid() = default; + + LuaPid(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.pFactor = kp; + m_params.iFactor = ki; + m_params.dFactor = kd; m_params.offset = 0; m_params.periodMs = 0; - m_params.minValue = 0; - m_params.maxValue = 0; + m_params.minValue = min; + m_params.maxValue = max; m_lastUpdate.reset(); } - float get(float input) { + float get(float target, 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(); } @@ -470,12 +447,9 @@ 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"); luaTimer .ctor() @@ -488,21 +462,11 @@ void configureRusefiLuaHooks(lua_State* l) { .fun("set", &LuaSensor::set) .fun("invalidate", &LuaSensor::invalidate); -// not enough Lua memory even to initialize Lua :( -#if defined(STM32F7) || defined(STM32H7) || EFI_UNIT_TEST LuaClass luaPid(l, "Pid"); luaPid - .ctor() + .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) - ; -#endif + .fun("reset", &LuaPid::reset); configureRusefiLuaUtilHooks(l); diff --git a/unit_tests/tests/lua/test_lua_hooks.cpp b/unit_tests/tests/lua/test_lua_hooks.cpp index 00ccaca9e3..09e55b5065 100644 --- a/unit_tests/tests/lua/test_lua_hooks.cpp +++ b/unit_tests/tests/lua/test_lua_hooks.cpp @@ -143,31 +143,24 @@ TEST(LuaHooks, LuaSensor) { static const char* pidTest = R"( function testFunc() - local pid = Pid.new() - pid:setP(0.5) - pid:setMinValue(-10) - pid:setMaxValue(10) - - pid:setTarget(3) + local pid = Pid.new(0.5, 0, 0, -10, 10) -- delta is -4, output -2 - if pid:get(7) ~= -2 then + if pid:get(3, 7) ~= -2 then return 1 end - pid:setTarget(4) -- delta is 6, output 3 - if pid:get(-2) ~= 3 then + if pid:get(4, -2) ~= 3 then return 2 end - pid:setTarget(0) -- test clamping - if pid:get(100) ~= -10 then + if pid:get(0, 100) ~= -10 then return 3 end - if pid:get(-100) ~= 10 then + if pid:get(0, -100) ~= 10 then return 4 end