Stormworks: Build and Rescue

Stormworks: Build and Rescue

Not enough ratings
Jet engine throttle PID LUA microcontroller
   
Award
Favorite
Favorited
Unfavorite
Vehicles: Air
Microcontrollers: Modular, Microcontroller
Tags: v0.10.11
File Size
Posted
28.448 KB
18 Mar, 2020 @ 3:09am
1 Change Note ( view )

Subscribe to download
Jet engine throttle PID LUA microcontroller

Description
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