From 1d078c114b0f4cb49622d72a82de491e3fba6d8b Mon Sep 17 00:00:00 2001 From: rusefi Date: Thu, 11 Nov 2021 19:16:04 -0500 Subject: [PATCH] lua PID example --- firmware/config/engines/custom_engine.cpp | 29 ++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/firmware/config/engines/custom_engine.cpp b/firmware/config/engines/custom_engine.cpp index 7e33f82473..9c8dbf16fd 100644 --- a/firmware/config/engines/custom_engine.cpp +++ b/firmware/config/engines/custom_engine.cpp @@ -871,18 +871,41 @@ void proteusLuaDemo(DECLARE_CONFIG_PARAMETER_SIGNATURE) { // ETB disable PD11 auto script = R"( -startPwm(0, 800, 0.3) +startPwm(0, 800, 0.1) -- direction startPwm(1, 80, 1.0) -- disable startPwm(2, 80, 0.0) +pid = Pid.new() +pid:setP(2) +pid:setMinValue(0) +pid:setMaxValue(100) + function onTick() - analog1 = getAuxAnalog(0) + targetVoltage = getAuxAnalog(0) + + target = interpolate(1, 0, 3.5, 100, targetVoltage) +-- clamp 0 to 100 + target = math.max(0, target) + target = math.min(100, target) + + print('Target voltage: ' .. targetVoltage) + print('Decoded target: ' .. target) + + tps = getSensor("TPS1") + tps = (tps == nil and 'invalid TPS' or tps) + print('Tps ' .. tps) + + pid:setTarget(target) + output = pid:get(tps) + + print('pid output ' .. output) + print('') - position = interpolate(1, 0, 4, 100, analog1) end )"; + strncpy(config->luaScript, script, efi::size(config->luaScript)); #endif }