diff --git a/firmware/controllers/lua/examples/custom-launch-control.txt b/firmware/controllers/lua/examples/custom-launch-control.txt index 35788844f7..1a396bbf93 100644 --- a/firmware/controllers/lua/examples/custom-launch-control.txt +++ b/firmware/controllers/lua/examples/custom-launch-control.txt @@ -23,17 +23,23 @@ function onTick() desiredBoost = curve(desiredBoostCurve, launchStrength) initialTorqueReduction = table3d(initialTorqueReductionTable, desiredRPM, desiredBoost) + pid:setOffset(initialTorqueReduction) print ("Running LC " ..desiredRPM .." boost=" + desiredBoost ..' t=' ..initialTorqueReduction) elseif launchButtonState == 1 then + rpm = getSensor("RPM") + -- handle nil RPM, todo: change firmware to avoid nil RPM + rpm = (rpm == nil and 0 or rpm) + print ("Running LC " ..desiredRPM .." boost=" + desiredBoost) else print "Not running LC" + setTimingAdd(0) end end diff --git a/firmware/controllers/lua/lua_hooks.cpp b/firmware/controllers/lua/lua_hooks.cpp index f65d560996..fa760a613d 100644 --- a/firmware/controllers/lua/lua_hooks.cpp +++ b/firmware/controllers/lua/lua_hooks.cpp @@ -395,6 +395,11 @@ struct LuaPid final { return m_pid.getOutput(target, input, dt); } + void setOffset(float offset) { + m_params.offset = offset; + reset(); + } + void reset() { m_pid.reset(); } @@ -422,6 +427,7 @@ void configureRusefiLuaHooks(lua_State* l) { luaPid .ctor() .fun("get", &LuaPid::get) + .fun("setOffset", &LuaPid::setOffset) .fun("reset", &LuaPid::reset); configureRusefiLuaUtilHooks(l); diff --git a/unit_tests/tests/lua/test_lua_basic.cpp b/unit_tests/tests/lua/test_lua_basic.cpp index 5a8cf57c1d..59c2a91747 100644 --- a/unit_tests/tests/lua/test_lua_basic.cpp +++ b/unit_tests/tests/lua/test_lua_basic.cpp @@ -90,6 +90,16 @@ TEST(LuaBasic, ExpectNumOrNilReturnsNumber) { )").value_or(0)); } +TEST(LuaPid, Offset) { + EXPECT_FLOAT_EQ(18.0f, testLuaReturnsNumberOrNil(R"( + function testFunc() +pid = Pid.new(2, 0, 0, -100, 100) +pid:setOffset(20) + return pid:get(20, 21) + end + )").value_or(0)); +} + TEST(LuaBasic, ExpectNumOrNilReturnsNothing) { // Returning nothing is generally functionally equivalent to returning nil EXPECT_EQ(testLuaReturnsNumberOrNil(R"(