edit PIDs to output normalized data and changed how UpdatGlobalThrust works
This commit is contained in:
59
main.lua
59
main.lua
@@ -860,12 +860,34 @@ function CreatePID(kp, ki, kd)
|
|||||||
ki = ki,
|
ki = ki,
|
||||||
kd = kd,
|
kd = kd,
|
||||||
integral = 0, lastError = 0,
|
integral = 0, lastError = 0,
|
||||||
|
|
||||||
|
minOutput = 0,
|
||||||
|
maxOutput = 256,
|
||||||
update = function(self, setpoint, pv, dt)
|
update = function(self, setpoint, pv, dt)
|
||||||
|
|
||||||
local error = setpoint - pv
|
local error = setpoint - pv
|
||||||
self.integral = self.integral + (error * dt)
|
|
||||||
local derivative = (error - self.lastError) / dt
|
local P = self.kp * error
|
||||||
|
if dt > 0 then local D = self.kd * ((error - self.lastError) / dt) else D = 0 end
|
||||||
|
|
||||||
|
local potential_i = self.integral + (self.ki * error * dt)
|
||||||
|
|
||||||
|
local raw_output = P + potential_i + D
|
||||||
|
|
||||||
|
local clampedOutput = 0
|
||||||
|
|
||||||
|
if self.minOutput <= potential_i <= self.maxOutput then
|
||||||
|
self.integral = potential_i
|
||||||
|
clampedOutput = raw_output
|
||||||
|
else
|
||||||
|
clampedOutput = math.max(self.minOutput, math.min(raw_output, self.maxOutput))
|
||||||
|
end
|
||||||
|
|
||||||
self.lastError = error
|
self.lastError = error
|
||||||
return (self.kp * error) + (self.ki * self.integral) + (self.kd * derivative)
|
|
||||||
|
local normalizedOutput = (clampedOutput - self.minOutput) / (self.maxOutput - self.minOutput)
|
||||||
|
|
||||||
|
return normalizedOutput
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
@@ -956,30 +978,37 @@ function UpdateGlobalThrust()
|
|||||||
|
|
||||||
-- Calculate desired thrust based on affectVectors and PID outputs
|
-- Calculate desired thrust based on affectVectors and PID outputs
|
||||||
if thruster.affectVectors.angular.pitch ~= nil and thruster.primary_pitch_thruster then
|
if thruster.affectVectors.angular.pitch ~= nil and thruster.primary_pitch_thruster then
|
||||||
desiredThrust = desiredThrust + pitchRateOutput
|
if thruster.affectVectors.angular.pitch == "up" then
|
||||||
|
desiredThrust = desiredThrust + pitchRateOutput
|
||||||
|
else
|
||||||
|
desiredThrust = desiredThrust - pitchRateOutput
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if thruster.affectVectors.angular.roll ~= nil and thruster.primary_roll_thruster then
|
if thruster.affectVectors.angular.roll ~= nil and thruster.primary_roll_thruster then
|
||||||
desiredThrust = desiredThrust + rollRateOutput
|
if thruster.affectVectors.angular.roll == "port" then
|
||||||
|
desiredThrust = desiredThrust + rollRateOutput
|
||||||
|
else
|
||||||
|
desiredThrust = desiredThrust - rollRateOutput
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
if thruster.affectVectors.angular.yaw ~= nil and thruster.primary_yaw_thruster then
|
if thruster.affectVectors.angular.yaw ~= nil and thruster.primary_yaw_thruster then
|
||||||
desiredThrust = desiredThrust + yawRateOutput
|
if thruster.affectVectors.angular.yaw == "port" then
|
||||||
|
desiredThrust = desiredThrust + yawRateOutput
|
||||||
|
else
|
||||||
|
desiredThrust = desiredThrust - yawRateOutput
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if thruster.affectVectors.lateral.y == "up" then
|
if thruster.affectVectors.lateral.y == "up" then
|
||||||
desiredThrust = desiredThrust + throttleOutput
|
desiredThrust = desiredThrust + throttleOutput
|
||||||
end
|
end
|
||||||
|
|
||||||
if desiredThrust > maxThrust then maxThrust = desiredThrust end
|
|
||||||
if desiredThrust < minThrust then minThrust = desiredThrust end
|
|
||||||
|
|
||||||
-- Normalize the desired thrust to be between 0 and 1
|
|
||||||
local normalizedThrust = normalize(desiredThrust, 0, 50)
|
|
||||||
|
|
||||||
if normalizedThrust < 0 then normalizedThrust = 0 end
|
|
||||||
|
|
||||||
if Config.Debug then print("DEBUG: "..thruster.name..": desiredThrust "..desiredThrust..", normalizedThrust "..normalizedThrust) end
|
if Config.Debug then print("DEBUG: "..thruster.name..": desiredThrust "..desiredThrust..", normalizedThrust "..normalizedThrust) end
|
||||||
|
|
||||||
SetThrusterPower(thruster, normalizedThrust)
|
SetThrusterPower(thruster, desiredThrust)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user