Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- keeps reactor working efficiently at all times; increases power load when necessary, goes idle at 90% power until below 10%
- local reactor=require('component').br_reactor
- local curPower,controlRodLevel,powerMode,tickCount=0,50,0,0
- local upTicks=0
- local integral=0
- local controlRodNumber=reactor.getNumberOfControlRods()
- powerMode = reactor.getEnergyStored()<9000000
- local function reactorActivities()
- tickCount=tickCount+1
- curPower = reactor.getEnergyStored()
- if curPower>9000000 then
- powerMode=false
- elseif curPower<1000000 and curPower>0 then
- powerMode=true
- end
- if powerMode then
- upTicks=upTicks+1
- reactor.setActive(true)
- local temperature = reactor.getFuelTemperature and reactor.getFuelTemperature() or reactor.getTemperature()
- oldError=curError or 0
- curError = temperature - 950 or 0
- integral=integral+curError or 0
- derivative = tickCount>1 and curError - oldError or 0
- local deltaControlRodLevel=(Kp*curError)+(Ki*integral)+(Kd*derivative)
- local numberOfControlRodsToAddOneTo=math.floor((deltaControlRodLevel%1)*controlRodNumber)
- deltaControlRodLevel=deltaControlRodLevel<0 and math.ceil(deltaControlRodLevel) or deltaControlRodLevel>0 and math.floor(deltaControlRodLevel) or 0
- controlRodLevel = controlRodLevel+deltaControlRodLevel
- if controlRodLevel>100 or controlRodLevel<0 then
- integral=0
- end
- controlRodLevel = controlRodLevel>100 and 100 or controlRodLevel<0 and 0 or controlRodLevel
- for i=0,controlRodNumber-1 do
- reactor.setControlRodLevel(i,controlRodLevel+(i<numberOfControlRodsToAddOneTo and 1 or 0))
- end
- else
- reactor.setActive(false)
- end
- end
- local args = {...}
- interval = 0.5
- Kp=0.5
- Ki=0.005*interval
- Kd=0.01*interval
- while reactor.getConnected() do
- pcall(reactorActivities)
- os.sleep(interval)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement