Advertisement
bob558

Self-Regulating Big Reactor v4

Oct 26th, 2016
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.31 KB | None | 0 0
  1. -- SELF-REGULATING BIG REACTOR: ITERATION FOUR
  2. -- Determines the most efficient control rod setting for actively-cooled reactor to produce 2000 mB/t of steam
  3. -- Brings a Big Reactor / Turbine pair up to running speed of 1830 RPM from cold
  4. -- When reactor is at running speed, will produce power until your power storage is full
  5. -- When batteries are full, keeps the turbine spinning at or above 1830 RPM until power is needed again
  6. -- Utilizes redstone signal to bottom of computer to signify low / high energy in batteries (I use a Power Monitor from EnderIO)
  7. -- No monitor necessary, as there are no controls or displays - it simply detects your setup and keeps it running smoothly.
  8. -- This program will function with any reactor / turbine pair capable of steady operation at 1800-1830 RPM. No other requirements.
  9.  
  10.  
  11. -- Create optional display which only appears if a monitor is wrapped. A monitor will never be necessary for this program.
  12.  
  13. reactor = peripheral.find("BigReactors-Reactor")
  14. turbine = peripheral.find("BigReactors-Turbine")
  15.            
  16. local function round(someNumber) -- simple rounding function to obtain integers.
  17.     result = math.floor(someNumber + 0.5)
  18.     return result
  19. end
  20.  
  21. local function passiveMaintain()
  22.     print("Engaging Passive Maintain Mode.")
  23.     reactor.setActive(true)
  24.     while true do
  25.         sleep(1)
  26.         local i = round(reactor.getEnergyStored() / 100000) -- converts the reactor's stored energy buffer to an integer between 1-100.
  27.         local energyProd = reactor.getEnergyProducedLastTick()
  28.         reactor.setAllControlRodLevels(i)
  29.         term.clear()
  30.         term.setCursorPos(1,1)
  31.             print("Passively Cooled Reactor Detected. Rod Insertion Control Engaged.")
  32.         term.setCursorPos(1,2)
  33.             print("Control Rods set at " ..i.. "%.")
  34.         term.setCursorPos(1,3)
  35.             print("Output: " ..energyProd.. " RF/t.")
  36.         end
  37.  end
  38.  
  39. local roughGuess = 0 -- floating variable for rough guesses of the optimal rod insertion. (via the adjustRods() function)
  40. local nextGuess = 0 -- floating variable for more accurate guesses of optimal control rod insertion.
  41.  
  42. local function tooLow() -- runs when the roughGuess variable ends up sending too little steam to the turbine.
  43.     while reactor.getHotFluidProducedLastTick() <= 2000 do
  44.         nextGuess = roughGuess - 1
  45.             print("Low Output. Trying " ..nextGuess.. "%.")
  46.         reactor.setActive(false)
  47.         sleep(2) -- small sleep with the reactor off to let its steam tank empty
  48.         reactor.setAllControlRodLevels(nextGuess)
  49.         reactor.setActive(true)
  50.         sleep(4) -- small sleep to let the reactor build up to full steam production
  51.         local finalProd = reactor.getHotFluidProducedLastTick()
  52.             print("Steam Production: " ..finalProd.. " mB/t.")
  53.         roughGuess = roughGuess - 1
  54.         end
  55. end
  56.  
  57. local function tooHigh() -- runs when the roughGuess variable ends up sending too much steam to the turbine.
  58.     while reactor.getHotFluidProducedLastTick() >= 2000 do
  59.         nextGuess = roughGuess + 1
  60.             print("High Output. Trying " ..nextGuess.. "%.")
  61.         reactor.setActive(false)
  62.         sleep(2) -- small sleep with the reactor off to let its steam tank empty
  63.         reactor.setAllControlRodLevels(nextGuess)
  64.         reactor.setActive(true)
  65.         sleep(4) -- small sleep to let the reactor build up to full steam production
  66.         local finalProd = reactor.getHotFluidProducedLastTick()
  67.             print("Steam Production: " ..finalProd.. " mB/t.")
  68.         roughGuess = roughGuess + 1
  69.     end
  70. end
  71.  
  72. local function adjustRods() --main function for determining optimal control rod insertion level
  73.     turbine.setActive(true) -- makes sure the turbine is draining steam from the reactor
  74.     turbine.setInductorEngaged(true) -- makes sure the turbine does not spin too quickly while active
  75.     reactor.setActive(false)
  76.     if reactor.getFuelTemperature() >= 100 then -- brings reactor under boiling temp to increase reading accuracy
  77.             print("Please wait while reactor cools down...")
  78.         while reactor.getFuelTemperature() >= 100 do
  79.             sleep(1)
  80.         end
  81.     end
  82.     reactor.setAllControlRodLevels(99) -- sets rods to 99% in order to better predict steam output incrementally
  83.     reactor.setActive(true)
  84.         print("Reactor below 100C. Control Rods set to 99%.")
  85.         print("Waiting for measurement...")
  86.     while reactor.getFuelTemperature() <= 100 do
  87.         sleep(1) -- sleeps to make sure the reactor is fully powered up before measuring steam output
  88.     end
  89.     sleep(5)
  90.     local steamProd = reactor.getHotFluidProducedLastTick() -- first measurement of steam production, at 99% rod insertion
  91.         print("Steam Production: " ..steamProd.. " mB/t.")
  92.     local middleman = 2000 / steamProd -- to determine roughly how much steam is produced per % insertion
  93.     local inverseLevel = round(middleman) -- gets the inverse integer for the roughGuess variable
  94.         print("Rounding to " ..inverseLevel)
  95.     if inverseLevel < 1 then
  96.         inverseLevel = 1
  97.         print("Steam output too low! Setting inverseLevel to 1.")
  98.     roughGuess = 100 - inverseLevel -- first guess of what the optimal control rod insertion level might be
  99.         print("First guess for optimal control rod level is " ..roughGuess.. "%.")
  100.     reactor.setAllControlRodLevels(roughGuess)
  101.         print("Setting all control rods to " ..roughGuess.. "%.")
  102.     sleep(3) -- once again, a sleep to make sure the steam production reading is stable and accurate
  103.     local finalProd = reactor.getHotFluidProducedLastTick()
  104.         print("Steam Production: " ..finalProd.. " mB/t.")
  105.     tooLow() -- this order of functions (low, high, low) ensures that the final result is just over 2000 mB/t of steam.
  106.     tooHigh()
  107.     tooLow()
  108.         print("Optimum Rod Insertion: " ..nextGuess.. "%.")
  109. end
  110.  
  111. local function charge() -- spins turbine up to full-speed and then produces power until redstone signal to computer turns off
  112.     reactor.setActive(true)
  113.     turbine.setInductorEngaged(false)
  114.   while turbine.getRotorSpeed() < 1830 do
  115.     local rotorSpeed = round(turbine.getRotorSpeed())
  116.     term.clear()
  117.     term.setCursorPos(1,1)
  118.         print("Spinning Up Turbine: " ..rotorSpeed.. " RPM")
  119.     sleep(1)
  120.   end
  121.     turbine.setInductorEngaged(true)
  122.   while rs.getInput("bottom") do
  123.     local rotorSpeed = round(turbine.getRotorSpeed())
  124.     local energyRate = round(turbine.getEnergyProducedLastTick())
  125.     term.clear()
  126.     term.setCursorPos(1,1)
  127.         print("Inductors Engaged!")
  128.     term.setCursorPos(1,2)
  129.         print("Turbine Speed: " ..rotorSpeed.. " RPM")
  130.     term.setCursorPos(1,3)
  131.         print("Output: " ..energyRate.. " RF/t")
  132.     sleep(1)
  133.   end
  134. end
  135.  
  136. local function activeMaintain() -- no power produced, but keeps the turbine spinning full-speed at a negligible expense of yellorium
  137.     while not rs.getInput("bottom") do
  138.         sleep(1)
  139.         while turbine.getRotorSpeed() <= 1830 do
  140.             if turbine.getInductorEngaged() == true then
  141.                 turbine.setInductorEngaged(false)
  142.             end
  143.             if reactor.getActive() == false then
  144.                 reactor.setActive(true)
  145.             end
  146.             local rotorSpeed = round(turbine.getRotorSpeed())
  147.             term.clear()
  148.             term.setCursorPos(1,1)
  149.                 print("Turbine Velocity Low.")
  150.             term.setCursorPos(1,2)
  151.                 print("Spooling: " ..rotorSpeed.. " RPM")
  152.             sleep(1)
  153.         end
  154.        
  155.         if turbine.getRotorSpeed() >= 1830 and reactor.getActive() == true then
  156.             reactor.setActive(false)
  157.         end
  158.        
  159.         local rotorSpeed = round(turbine.getRotorSpeed())
  160.         term.clear()
  161.         term.setCursorPos(1,1)
  162.             print("Turbine Velocity Stable.")
  163.         term.setCursorPos(1,2)
  164.             print("Sleeping at " ..rotorSpeed.. " RPM")
  165.         end
  166.     end
  167. end
  168.  
  169. -- EXECUTION --
  170.  
  171. print("Detecting reactor: " ..tostring(reactor.getConnected()))
  172. print("Reactor Passively Cooled: " ..tostring(reactor.isActivelyCooled()))
  173.  
  174. if reactor.isActivelyCooled() == false then
  175.     passiveMaintain()
  176. end
  177.  
  178. adjustRods()
  179.  
  180. while true do
  181.     sleep(1)
  182.     if rs.getInput("bottom") then
  183.         charge()
  184.     end
  185.     if not rs.getInput("bottom") then
  186.         activeMaintain()
  187.     end
  188. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement