PID
The PID class simulates a PID controller. PID is an acronym for proportional, integral, derivative. PIDs are input feedback loops that try to reach a specific goal by measuring the difference between the input and the desired value, and then returning a new desired input.
A common example is a car's cruise control, which would give a PID the current speed and the desired speed, and the PID controller would return the desired throttle input to reach the desired speed.
Original code based upon the Arduino PID Library.
Properties
POnE
PID.POnE:
boolean
POnE stands for "Proportional on Error".
Set to true
by default.
true
: The PID applies the proportional calculation on the error.false
: The PID applies the proportional calculation on the measurement.
Setting this value to false
may help the PID move smoother and help
eliminate overshoot.
local pid = PID.new(...)
pid.POnE = true|false
Functions
new
PID.
new
(
min:
number
,
--
Minimum value the PID can output
max:
number
,
--
Maximum value the PID can output
kp:
number
,
--
Proportional coefficient
ki:
number
,
--
Integral coefficient
kd:
number
--
Derivative coefficient
) →
PID
Constructs a new PID.
local pid = PID.new(0, 1, 0.1, 0, 0)
Reset
PID:
Reset
(
) →
(
)
Resets the PID to a zero start state.
Calculate
PID:
Calculate
(
setpoint:
number
,
--
The desired point to reach
input:
number
--
The current inputted value
) →
output:
number
Calculates the new output based on the setpoint and input. For example, if the PID was being used for a car's throttle control where the throttle can be in the range of [0, 1], then the PID calculation might look like the following:
local cruisePID = PID.new(0, 1, ...)
local desiredSpeed = 50
RunService.Heartbeat:Connect(function()
local throttle = cruisePID:Calculate(desiredSpeed, car.CurrentSpeed)
car:SetThrottle(throttle)
end)
Debug
Creates a folder that contains attributes that can be used to tune the PID during runtime within the explorer.
Studio Only
This will only create the folder in Studio. In a real game server, this function will do nothing.
Destroy
PID:
Destroy
(
) →
(
)
Destroys the PID. This is only necessary if calling PID:Debug
.