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
- print('Error: ' .. curError)
- print('Integral: ' .. integral)
- local deltaControlRodLevel=(Kp*curError)+(Ki*integral)
- if deltaControlRodLevel>50 or deltaControlRodLevel<-50 then
- if curError<target*10 and curError>-target*10 then
- Kp=Kp/2
- end
- if integral>100000 or integral<100000 then
- integral=0
- end
- end
- if deltaControlRodLevel>-1 and deltaControlRodLevel<1 then
- if curError>target*10 then
- Kp=Kp*1.5
- end
- end
- deltaControlRodLevel=deltaControlRodLevel<0 and math.ceil(deltaControlRodLevel) or deltaControlRodLevel>0 and math.floor(deltaControlRodLevel) or 0
- 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
- Kp=0.001
- Ki=0.0001
- interval=tonumber(interval)
- target=tonumber(target)
- if interval<0.5 then
- interval=0.5
- end
- while reactor.getConnected() do
- if interval then os.startTimer(interval) end
- local event = {os.pullEvent()}
- if event[1]=='timer' then
- reactorActivities()
- if terminal then
- terminalActivities()
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement