lua pid class #3411

This commit is contained in:
Andrey 2021-11-07 09:53:40 -05:00
parent c98bfecf11
commit 49d4dea65e
2 changed files with 41 additions and 2 deletions

View File

@ -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> 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);

View File

@ -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);
}