edit PIDs to output normalized data and changed how UpdatGlobalThrust works

This commit is contained in:
2026-06-29 23:34:18 -05:00
parent cb7b9490bf
commit 61c8f88d40

View File

@@ -860,12 +860,34 @@ function CreatePID(kp, ki, kd)
ki = ki,
kd = kd,
integral = 0, lastError = 0,
minOutput = 0,
maxOutput = 256,
update = function(self, setpoint, pv, dt)
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
return (self.kp * error) + (self.ki * self.integral) + (self.kd * derivative)
local normalizedOutput = (clampedOutput - self.minOutput) / (self.maxOutput - self.minOutput)
return normalizedOutput
end
}
end
@@ -956,30 +978,37 @@ function UpdateGlobalThrust()
-- 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 == "up" then
desiredThrust = desiredThrust + pitchRateOutput
else
desiredThrust = desiredThrust - pitchRateOutput
end
end
if thruster.affectVectors.angular.roll ~= nil and thruster.primary_roll_thruster then
if thruster.affectVectors.angular.roll == "port" then
desiredThrust = desiredThrust + rollRateOutput
else
desiredThrust = desiredThrust - rollRateOutput
end
end
if thruster.affectVectors.angular.yaw ~= nil and thruster.primary_yaw_thruster then
if thruster.affectVectors.angular.yaw == "port" then
desiredThrust = desiredThrust + yawRateOutput
else
desiredThrust = desiredThrust - yawRateOutput
end
end
if thruster.affectVectors.lateral.y == "up" then
desiredThrust = desiredThrust + throttleOutput
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
SetThrusterPower(thruster, normalizedThrust)
SetThrusterPower(thruster, desiredThrust)
end
end