Hi all,
by using Export.lua, I can get the TAS - TrueAirSpeed and I can control the throttle position by using
LoSetCommand(2004, throttlePosition) and I'm thinking to use TAS to control the throttle position by using PID algorithm, but I'm unfamiliar with this subject. By reading wikipedia I got some basic knowledge : http://en.wikipedia.org/wiki/PID_controller
Please see the attached image, the test shows it's not acceptable. The response speed is too slow, and overshoot is big, the stable error is large, and seems it's not even a stable control.
X is time in seconds, Y is KTAS, the target KTAS is 220.5
:doh::joystick:
this is my code:
local TargetTAS = 220.50 -- I want A10 to keep 220.50 KTAS
local LastTAS = 0.0 -- the previous sampled TAS
local TAS = 0.0 -- the current sampled TAS
local Thrust = -0.5 -- set the initial Thrust position to -0.5
local T0, T1 -- T0 is the last sample time, T1 is the current sample time
local lastDelta = 0 -- the TAS Delta ( the value of TAS - TargetTAS) of the last sample time,
local IntegralSum = 0 --Integral sum,
function AutoThrust()
if T0 == nil then T0 = T1 return end
local tDelta = T1 - T0 -- tDelta is the elapsed time between last sample time and the current sample time
local Kp = 2 -- PID value
local Ki = 1
local Kd = 1
local TASDelta = TargetTAS - TAS -- the speed delta between real and target speed
local PID_P = Kp * TASDelta -- calculate the P part
-- calculate the IntegralSum
IntegralSum = IntegralSum + TASDelta * tDelta
-- integral saturation
if math.abs(IntegralSum) > 1000 then
if IntegralSum > 0 then
IntegralSum = 1000
elseif IntegralSum < 0 then
IntegralSum = -1000
end
end
local PID_I = Ki * IntegralSum
-- the D part
local PID_D = Kd * (TASDelta - lastDelta) / tDelta
-- P+I+D
local Pout = PID_P + PID_I + PID_D
-- map the PID value to Thrust position value [ -1, 1] , -1 is the max throttle,
Thrust = Pout / -2000
--update some value
lastDelta = TASDelta
T0 = T1
textOutput = string.format("t=%f;Thrust=%f;TAS=%f;TASDelta=%f;Pout=%f;P=%f;I=%f;D=%f;\n", T1, Thrust, TAS, TASDelta, Pout, PID_P, PID_I, PID_D )
print( textOutput )
logFile:write( textOutput )
end
I played xplane for long time and I know the auto throttle works very well: fast response, no overshoot, and it's stable.
any suggestions on how to implement an automation throttle is appreciated :)
If any one has the interesting to have a try, please download export.lua, tcpServer.lua
run tcpServer first by execute:
lua tcpServer.luaand start a A10 instant mission, engage the auto pilot. the data will be recored to export.log, use GNU Octave script to plot the TAS data by execute
plotA10Data("export.log") note: extract OctaveScriptsForPlotData_m.zip to YourOctavePath\share\octave\3.6.0\m\
The Kp, Ki, Kd is hardcoded in the tcpServer.lua script but you can change it on the fly by using script tcpClient.lua, usage:
lua tcpClient.lua 5 2 1
5 2 1 is 3 numbers represent Kp Ki Kd Export.lua OctaveScriptsForPlotData_m.zip tcpServer.lua tcpClient.lua