From 9b0f617664ad0c7ac70ac947d4e960c1038c964a Mon Sep 17 00:00:00 2001 From: Andrey Date: Sun, 7 Nov 2021 09:53:40 -0500 Subject: [PATCH] lua pid class #3411 --- firmware/controllers/lua/lua_hooks.cpp | 5 ++-- unit_tests/tests/lua/test_lua_hooks.cpp | 38 +++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/firmware/controllers/lua/lua_hooks.cpp b/firmware/controllers/lua/lua_hooks.cpp index b95c8f2ec7..562fce5604 100644 --- a/firmware/controllers/lua/lua_hooks.cpp +++ b/firmware/controllers/lua/lua_hooks.cpp @@ -484,10 +484,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() -/* .fun("get", &LuaPid::get) .fun("setTarget", &LuaPid::setTarget) .fun("setP", &LuaPid::setP) @@ -496,8 +497,8 @@ void configureRusefiLuaHooks(lua_State* l) { .fun("setMinValue", &LuaPid::setMinValue) .fun("setMaxValue", &LuaPid::setMaxValue) .fun("reset", &LuaPid::reset) -*/ ; +#endif configureRusefiLuaUtilHooks(l); diff --git a/unit_tests/tests/lua/test_lua_hooks.cpp b/unit_tests/tests/lua/test_lua_hooks.cpp index ab6b1da58d..00ccaca9e3 100644 --- a/unit_tests/tests/lua/test_lua_hooks.cpp +++ b/unit_tests/tests/lua/test_lua_hooks.cpp @@ -140,3 +140,41 @@ TEST(LuaHooks, LuaSensor) { // Ensure that the sensor got unregistered on teardown of the Lua interpreter EXPECT_FALSE(Sensor::hasSensor(SensorType::Clt)); } + +static const char* pidTest = R"( +function testFunc() + local pid = Pid.new() + pid:setP(0.5) + pid:setMinValue(-10) + pid:setMaxValue(10) + + pid:setTarget(3) + + -- delta is -4, output -2 + if pid:get(7) ~= -2 then + return 1 + end + + pid:setTarget(4) + -- delta is 6, output 3 + if pid:get(-2) ~= 3 then + return 2 + end + + pid:setTarget(0) + -- test clamping + if pid:get(100) ~= -10 then + return 3 + end + + if pid:get(-100) ~= 10 then + return 4 + end + + return 0 +end +)"; + +TEST(LuaHooks, LuaPid) { + EXPECT_EQ(testLuaReturnsNumber(pidTest), 0); +}