Advertisement
Putnam

PID version of reactor control

Oct 14th, 2014
544
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.     local derivative=(deltaPower-oldDeltaPower)/interval
  47.     print('Error: ' .. curError)
  48.     print('Integral: ' .. integral)
  49.     print('Derivative: ' .. derivative)
  50.     local deltaControlRodLevel=math.floor((Kp*curError)+(Ki*integral)+(Kd*derivative))
  51.     print('Control rod level change: ' .. deltaControlRodLevel)
  52.     controlRodLevel = controlRodLevel+deltaControlRodLevel
  53.     if controlRodLevel>100 or controlRodLevel<0 then
  54.      integral=0
  55.     end
  56.     controlRodLevel = controlRodLevel>100 and 100 or controlRodLevel<0 and 0 or controlRodLevel
  57.     if curPower<10000 then
  58.      controlRodLevel=0
  59.      integral=0
  60.     end
  61.     print('Current control rod level: ' .. controlRodLevel)
  62.     reactor.setAllControlRodLevels(controlRodLevel)
  63.   else
  64.     reactor.setAllControlRodLevels(100)
  65.     print("Reactor's nearly full.")
  66.     print("Current status: off.")
  67.   end
  68. end
  69.  
  70. local function terminalActivities()
  71.   terminal.clear()
  72.   local wastePercent=reactor.getWasteAmount()/reactor.getFuelAmountMax()
  73.   local fuelPercent=(reactor.getWasteAmount()+reactor.getFuelAmount())/reactor.getFuelAmountMax()
  74.   terminal.addBox(5,5,100,10,0xcc0000,0.8)
  75.   terminal.addBox(5,5,math.floor(curPower/100000),10,0x00cc00,0.8)
  76.   terminal.addBox(105,5,10,100,0xcccccc,0.8)
  77.   terminal.addBox(105,5,10,controlRodLevel,0x303030,0.8)
  78.   terminal.addBox(5,20,100,10,0x303030,0.8)
  79.   terminal.addBox(5,20,math.min(math.floor(100*fuelPercent),100),10,0xcccc00,0.8)
  80.   terminal.addBox(5,20,math.min(math.floor(100*wastePercent),100),10,0x00cccc,0.8)
  81. end
  82.  
  83.  
  84. args = {...}
  85.  
  86. interval = args[1] or 2
  87. target = args[2] or 100
  88. target = tonumber(target)
  89. Kp=args[3] or 0.001*(100/target)
  90. Ki=args[4] or 0.0001*(100/target)
  91. Kd=args[5] or 0.00001*(100/target)
  92.  
  93. interval=tonumber(interval)
  94.  
  95. if interval<0.5 then
  96.     interval=0.5
  97. end
  98.  
  99. while reactor.getConnected() do
  100.   if interval then os.startTimer(interval) end
  101.   local event = {os.pullEvent("timer")}
  102.   reactorActivities()
  103.   if terminal then
  104.     terminalActivities()
  105.   end
  106. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement