Advertisement
Putnam

PI version of reactor efficiency

Oct 14th, 2014
569
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- keeps reactor working efficiently at all times; increases power load when necessary, goes idle at 90% power until below 10%
  2.  
  3. local function findPeripherals() --searches around for reactor so the computer doesn't have to be placed in a particular way
  4.   local reactor,terminal --also searches for a terminal glasses terminal
  5.   for k,v in pairs(rs.getSides()) do
  6.     if (peripheral.getType(v) == "BigReactors-Reactor") then
  7.       reactor = peripheral.wrap(v)
  8.       print('reactor found!')
  9.       break
  10.     elseif (peripheral.getType(v) == "openperipheral_glassesbridge") and (not terminal) then
  11.       terminal = peripheral.wrap(v)
  12.       print('terminal found!')
  13.     end
  14.   end
  15.   return reactor, terminal
  16. end
  17.  
  18.  
  19. local reactor,terminal=findPeripherals()
  20.  
  21. local curPower,oldPower,controlRodLevel,powerMode,secondsWorked=0,0,100,0,0
  22.  
  23. local integral=0
  24.  
  25. powerMode = reactor.getEnergyStored()<9000000
  26.  
  27. local function reactorActivities()
  28.   term.clear()
  29.   secondsWorked=secondsWorked+1
  30.   print('Yes, I am working. Total time: ' .. secondsWorked .. ' steps.')
  31.   oldPower=curPower
  32.   curPower=reactor.getEnergyStored()
  33.   if oldPower==0 then
  34.     oldPower=curPower+(target*interval)
  35.   end
  36.   if curPower>9000000 then
  37.     powerMode=false
  38.   elseif curPower<1000000 then
  39.     powerMode=true
  40.   end
  41.   if powerMode then
  42.     oldDeltaPower=deltaPower or 0
  43.     deltaPower = curPower-oldPower
  44.     local curError = deltaPower-(target*interval)
  45.     integral=integral+curError
  46.     print('Error: ' .. curError)
  47.     print('Integral: ' .. integral)
  48.     local deltaControlRodLevel=(Kp*curError)+(Ki*integral)
  49.     if deltaControlRodLevel>50 or deltaControlRodLevel<-50 then
  50.      if curError<target*10 and curError>-target*10 then
  51.       Kp=Kp/2
  52.      end
  53.      if integral>100000 or integral<100000 then
  54.       integral=0
  55.      end
  56.     end
  57.     if deltaControlRodLevel>-1 and deltaControlRodLevel<1 then
  58.      if curError>target*10 then
  59.       Kp=Kp*1.5
  60.      end
  61.     end
  62.     deltaControlRodLevel=deltaControlRodLevel<0 and math.ceil(deltaControlRodLevel) or deltaControlRodLevel>0 and math.floor(deltaControlRodLevel) or 0
  63.     print('Control rod level change: ' .. deltaControlRodLevel)
  64.     controlRodLevel = controlRodLevel+deltaControlRodLevel
  65.     if controlRodLevel>100 or controlRodLevel<0 then
  66.      integral=0
  67.     end
  68.     controlRodLevel = controlRodLevel>100 and 100 or controlRodLevel<0 and 0 or controlRodLevel
  69.     if curPower<10000 then
  70.      controlRodLevel=0
  71.      integral=0
  72.     end
  73.     print('Current control rod level: ' .. controlRodLevel)
  74.     reactor.setAllControlRodLevels(controlRodLevel)
  75.   else
  76.     reactor.setAllControlRodLevels(100)
  77.     print("Reactor's nearly full.")
  78.     print("Current status: off.")
  79.   end
  80. end
  81.  
  82. local function terminalActivities()
  83.   terminal.clear()
  84.   local wastePercent=reactor.getWasteAmount()/reactor.getFuelAmountMax()
  85.   local fuelPercent=(reactor.getWasteAmount()+reactor.getFuelAmount())/reactor.getFuelAmountMax()
  86.   terminal.addBox(5,5,100,10,0xcc0000,0.8)
  87.   terminal.addBox(5,5,math.floor(curPower/100000),10,0x00cc00,0.8)
  88.   terminal.addBox(105,5,10,100,0xcccccc,0.8)
  89.   terminal.addBox(105,5,10,controlRodLevel,0x303030,0.8)
  90.   terminal.addBox(5,20,100,10,0x303030,0.8)
  91.   terminal.addBox(5,20,math.min(math.floor(100*fuelPercent),100),10,0xcccc00,0.8)
  92.   terminal.addBox(5,20,math.min(math.floor(100*wastePercent),100),10,0x00cccc,0.8)
  93. end
  94.  
  95.  
  96. args = {...}
  97.  
  98. interval = args[1] or 2
  99. target = args[2] or 100
  100. Kp=0.001
  101. Ki=0.0001
  102.  
  103. interval=tonumber(interval)
  104. target=tonumber(target)
  105.  
  106. if interval<0.5 then
  107.     interval=0.5
  108. end
  109.  
  110. while reactor.getConnected() do
  111.   if interval then os.startTimer(interval) end
  112.   local event = {os.pullEvent()}
  113.   if event[1]=='timer' then
  114.    reactorActivities()
  115.    if terminal then
  116.     terminalActivities()
  117.    end
  118.   end
  119. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement