Stormworks: Build and Rescue

Stormworks: Build and Rescue

評價次數不足
Jet engine throttle PID LUA microcontroller
   
獎勵
加入最愛
已加入最愛
移除最愛
Vehicles: Air
Microcontrollers: Modular, Microcontroller
標籤: v0.10.11
檔案大小
發佈於
28.448 KB
2020 年 3 月 18 日 上午 3:09
1 項更新註記 (檢視)

訂閱以下載
Jet engine throttle PID LUA microcontroller

描述
This microcontroller uses LUA PID code to control jet engine throttle.
Has many properties to adjust PID logic. Will prevent PID Integral overflow and stabilise output at all RPS ranges.
Has two inputs: throttle and RPS. Has two outputs: starter on/off and throttle.

I recommend looking into the code and messing around (default values are pretty good though). License MIT, so if you improve it you can upload it, no strings attached :)

Version 2020-03-18

---------------------------------------------------------------------------------------------------------

sumErr = 0
lastErr = 0

-- Tick function that will be executed every logic tick
function onTick()
-- Constants
dt = 1.0/60.0

-- Inputs
throttle = input.getNumber(1)
engineRPS = input.getNumber(2)
starterRPS = input.getNumber(3)
engineRPS_lim = input.getNumber(4)
engineRPS_hardlim = input.getNumber(5)
throttle_min = input.getNumber(6)
PID_p = input.getNumber(7)
PID_i = input.getNumber(8)
PID_d = input.getNumber(9)
PID_i_clamp_mult = input.getNumber(10)
PID_i_clamp_add = input.getNumber(11)

-- Outputs
starter = (throttle > throttle_min and engineRPS < starterRPS) -- check if starter should be on
engineThrottle = 0.0

-- PID
RPSfract = engineRPS/engineRPS_lim
err = throttle - RPSfract
deriv = (err - lastErr)*dt
sumErr = sumErr + err*PID_i
PID_i_clamp = math.abs(err)*PID_i_clamp_mult + PID_i_clamp_add
sumErr = math.min(PID_i_clamp, math.max(-PID_i_clamp, sumErr))
lastErr = err

engineThrottle = PID_p*err + sumErr + PID_d*deriv

-- Engine RPS limiter
if (engineRPS > engineRPS_lim and engineRPS_lim < engineRPS_hardlim) then
engineThrottle = (engineRPS_hardlim - engineRPS)/(engineRPS_hardlim - engineRPS_lim)
end

-- Output
output.setNumber(1, engineThrottle)
output.setBool(1, starter)
end