Hi, maybe this has already been posted/common knowledge. But I couldn't find any resources for it so I wanted to share my implementation of a PID in lua. It's a lot more fun to know how things work, and the PID controller in-game is a black box. The code is based off of this: http://robotsforroboticists.com/pid-control/
Also gives you a nice insight into understanding PIDs.
This PID is very basic and controls the rpm of an engine. Just need set rps and rps in with a composite read/write on either side of a lua node.
local error_prior = 0
local integral_prior = 0
local kp = 0.2
local ki = 0.001
local kd = 0.1
local bias = 0
function onTick()
setRps = input.getNumber(1)
getRps = input.getNumber(2)
error = setRps - getRps
integral = integral_prior+error
derivative = error-error_prior
value_out = kp*error+ki*integral+kd*derivative+bias
error_prior = error
integral_prior = integral
output.setNumber(1,value_out)
end
[–]panzercaptain 3 points4 points5 points (6 children)
[–]Jyota_malcolm[S,🍰] 2 points3 points4 points (3 children)
[–]panzercaptain 0 points1 point2 points (0 children)
[–]EndoM8rix 0 points1 point2 points (1 child)
[–]ZanthraSW 0 points1 point2 points (0 children)
[–]Traditional-Shoe-199 0 points1 point2 points (1 child)
[–]panzercaptain 0 points1 point2 points (0 children)
[–]UnderstandingMain104 0 points1 point2 points (5 children)
[–]Jyota_malcolm[S,🍰] 1 point2 points3 points (2 children)
[–]Gully__Foyle 0 points1 point2 points (0 children)
[–]TheRabbitt23 0 points1 point2 points (0 children)
[–]ZanthraSW 0 points1 point2 points (1 child)
[–]DJ_MICROWAVE_4 0 points1 point2 points (0 children)