adjust PID update function return value
This commit is contained in:
10
main.lua
10
main.lua
@@ -864,13 +864,13 @@ function CreatePID(kp, ki, kd)
|
|||||||
minOutput = 0.0,
|
minOutput = 0.0,
|
||||||
maxOutput = 64.0,
|
maxOutput = 64.0,
|
||||||
update = function(self, setpoint, pv, dt)
|
update = function(self, setpoint, pv, dt)
|
||||||
|
if dt <= 0 then return 0.0 end
|
||||||
|
|
||||||
local error = setpoint - pv
|
local error = setpoint - pv
|
||||||
|
|
||||||
local P = self.kp * error
|
local P = self.kp * error
|
||||||
|
|
||||||
local D = 0
|
local D = self.kd * ((error - self.lastError) / dt)
|
||||||
if dt > 0 then D = self.kd * ((error - self.lastError) / dt) else D = 0 end
|
|
||||||
|
|
||||||
local potential_i = self.integral + (self.ki * error * dt)
|
local potential_i = self.integral + (self.ki * error * dt)
|
||||||
|
|
||||||
@@ -880,7 +880,7 @@ function CreatePID(kp, ki, kd)
|
|||||||
|
|
||||||
local clampedOutput = 0
|
local clampedOutput = 0
|
||||||
|
|
||||||
if self.minOutput <= potential_i and potential_i <= self.maxOutput then
|
if raw_output >= self.minOutput and raw_output <= self.maxOutput then
|
||||||
self.integral = potential_i
|
self.integral = potential_i
|
||||||
clampedOutput = raw_output
|
clampedOutput = raw_output
|
||||||
else
|
else
|
||||||
@@ -889,9 +889,7 @@ function CreatePID(kp, ki, kd)
|
|||||||
|
|
||||||
self.lastError = error
|
self.lastError = error
|
||||||
|
|
||||||
local normalizedOutput = (clampedOutput - self.minOutput) / (self.maxOutput - self.minOutput)
|
return clampedOutput
|
||||||
|
|
||||||
return normalizedOutput
|
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user