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 function findPeripherals() --searches around for reactor so the computer doesn't have to be placed in a particular way
- local reactor,terminal --also searches for a terminal glasses terminal
- for k,v in pairs(rs.getSides()) do
- if (peripheral.getType(v) == "BigReactors-Reactor") then
- reactor = peripheral.wrap(v)
- print('reactor found!')
- break
- elseif (peripheral.getType(v) == "openperipheral_glassesbridge") and (not terminal) then
- terminal = peripheral.wrap(v)
- print('terminal found!')
- end
- end
- return reactor, terminal
- end
- local reactor,terminal=findPeripherals()
- local curPower,oldPower,controlRodLevel,powerMode,secondsWorked=0,0,100,0,0
- local integral=0
- powerMode = reactor.getEnergyStored()<9000000
- local function reactorActivities()
- term.clear()
- secondsWorked=secondsWorked+1
- print('Yes, I am working. Total time: ' .. secondsWorked .. ' steps.')
- oldPower=curPower
- curPower=reactor.getEnergyStored()
- if oldPower==0 then
- oldPower=curPower+(target*interval)
- end
- if curPower>9000000 then
- powerMode=false
- elseif curPower<1000000 then
- powerMode=true
- end
- if powerMode then
- oldDeltaPower=deltaPower or 0
- deltaPower = curPower-oldPower
- local curError = deltaPower-(target*interval)
- integral=integral+curError
- local derivative=(deltaPower-oldDeltaPower)/interval
- print('Error: ' .. curError)
- print('Integral: ' .. integral)
- print('Derivative: ' .. derivative)
- local deltaControlRodLevel=math.floor((Kp*curError)+(Ki*integral)+(Kd*derivative))
- print('Control rod level change: ' .. deltaControlRodLevel)
- 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
- if curPower<10000 then
- controlRodLevel=0
- integral=0
- end
- print('Current control rod level: ' .. controlRodLevel)
- reactor.setAllControlRodLevels(controlRodLevel)
- else
- reactor.setAllControlRodLevels(100)
- print("Reactor's nearly full.")
- print("Current status: off.")
- end
- end
- local function terminalActivities()
- terminal.clear()
- local wastePercent=reactor.getWasteAmount()/reactor.getFuelAmountMax()
- local fuelPercent=(reactor.getWasteAmount()+reactor.getFuelAmount())/reactor.getFuelAmountMax()
- terminal.addBox(5,5,100,10,0xcc0000,0.8)
- terminal.addBox(5,5,math.floor(curPower/100000),10,0x00cc00,0.8)
- terminal.addBox(105,5,10,100,0xcccccc,0.8)
- terminal.addBox(105,5,10,controlRodLevel,0x303030,0.8)
- terminal.addBox(5,20,100,10,0x303030,0.8)
- terminal.addBox(5,20,math.min(math.floor(100*fuelPercent),100),10,0xcccc00,0.8)
- terminal.addBox(5,20,math.min(math.floor(100*wastePercent),100),10,0x00cccc,0.8)
- end
- args = {...}
- interval = args[1] or 2
- target = args[2] or 100
- target = tonumber(target)
- Kp=args[3] or 0.001*(100/target)
- Ki=args[4] or 0.0001*(100/target)
- Kd=args[5] or 0.00001*(100/target)
- interval=tonumber(interval)
- if interval<0.5 then
- interval=0.5
- end
- while reactor.getConnected() do
- if interval then os.startTimer(interval) end
- local event = {os.pullEvent("timer")}
- reactorActivities()
- if terminal then
- terminalActivities()
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement