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 <makenne@microsoft.com>
This commit is contained in:
Matthew Kennedy 2021-11-13 00:18:16 -08:00 committed by GitHub
parent a831bec797
commit 73b05d2e20
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 68 deletions

View File

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

View File

@ -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<Timer> 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> luaPid(l, "Pid");
luaPid
.ctor()
.ctor<float, float, float, float, float>()
.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);

View File

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