newPIDStandard
Definition
-- @/lua/common/controlSystems.lua:108
--Usage:
--local myPID = newPIDStandard(1, 0.5, 0.1, 0, 1)
--local control = myPID:get(processVariable, setPoint, dt)
--This PID uses derivative calculation based on the process variable rather than the error to avoid spikes when changing the setpoint
function newPIDStandard(kP, tI, tD, minOutput, maxOutput, integralInCoef, integralOutCoef, minIntegral, maxIntegral, errorDeadzone)
local data = {
kP = kP,
tICoef = tI > 0 and 1 / tI or 0, --integral time - try to eliminate past errors within this time, pre-calculate the 1 / tI for optimization purposes
tD = tD, -- derivative time - try to predict error this time in the future
error = 0,
integral = 0,
output = 0,
integralInCoef = integralInCoef or 1,
integralOutCoef = integralOutCoef or 1,
lastProcessVariable = 0,
minOutput = minOutput or -math.huge,
maxOutput = maxOutput or math.huge,
errorDeadzone = errorDeadzone or 0.0,
get = PIDStandard.getMethod
}
data.maxIntegral = maxIntegral or data.maxOutput
data.minIntegral = minIntegral or -data.maxIntegral
setmetatable(data, PIDStandard)
return data
end
Callers
@/lua/common/controlSystems.lua
--Usage:
--local myPID = newPIDStandard(1, 0.5, 0.1, 0, 1)
--local control = myPID:get(processVariable, setPoint, dt)
@/lua/vehicle/extensions/cruiseControl.lua
local throttleSmooth = newTemporalSmoothing(200, 200)
local speedPID = newPIDStandard(0.3, 2, 0.0, 0, 1, 1, 1, 0, 1)
--speedPID:setDebug(true)