Advertisement
zeplintwo

Reactor DW20 Big Reactors control program

Feb 15th, 2015
489
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 18.08 KB | None | 0 0
  1. --[[
  2. Installer pastebin code http://pastebin.com/MuS68wCr
  3. Reactor pastebin code http://pastebin.com/YbHGQ2wY
  4. Button pastebin code http://pastebin.com/wGAt78pv
  5. Startup pastebin code http://pastebin.com/irim4gRD
  6.  
  7. Changes:
  8.    -2/16/2015
  9.    - pulling hot water capacity from reactor in active cooled mode
  10.    - verfied 3 peak speed for turbines default speed is 1820. Low is 900, High 2728
  11.    - user set-able reactor offset for cold start-up default % increase is 5
  12.    - build Histarics to help turbines stay in high output zones
  13.    - corrected some venting issues causing long start-up time is odd cases
  14.    - functions turbineWarmup and turbineRunning to help reduce code and help histarics implementation.
  15.  
  16.  
  17.    -2/15/2015
  18.    - adjusted hot water start to 20,000 mb
  19.    - set turnOnAt to 60 from 50
  20.    - set numCapacitors to 10 for house from 3
  21.    - function setVenting() for dumping water in reactor
  22.    - function setVentRuning() closing turbine before start-up
  23. ]]--
  24.  
  25. os.loadAPI("button")
  26.  
  27. p = peripheral.find("tile_blockcapacitorbank_name")
  28. m = peripheral.find("monitor")
  29. r = peripheral.find("BigReactors-Reactor")
  30. t = peripheral.find("BigReactors-Turbine")
  31.  
  32. local steamReactor = r.isActivelyCooled()
  33.  
  34. local numCapacitors = 27 -- number of capacitors in your capacitor bank
  35. local turnOnAt = 60 -- capacitor percentage to turn coils on in turbine or turn on the reactor
  36. local turnOffAt = 90 -- capacitor percentage to shut coils off on turbine or shut reactor off
  37. local reactorOffset = 5 -- used to help cold start-ups this is % of rods in during start-up
  38. local steamMin = 2000 -- Min um steam to start the program lower only to accommodate 1 rod reactors
  39. local targetSpeed = 1840 -- valid speeds peaks 900, (1840), 2728
  40.  
  41. local energy = 0
  42. local energyStored = 0
  43. local energyMax = 0
  44. local energyStoredPercent = 0
  45. local timerCode
  46. local mode = "Automatic"
  47. local RFProduction = 0
  48. local fuelUse = 0
  49. local coreTemp = 0
  50. local reactorOnline = false
  51. local rodLevel = 0
  52.  
  53. local turbineOnline = false
  54. local turbineRotorSpeed = 0
  55. local turbineRFGen = 0
  56. local turbineFluidRate = 0
  57. local turbineInductor = false
  58.  
  59. local OptFuelRodLevel = 0
  60.  
  61. if (steamReactor ~= true) then
  62.     local menuType = "Reactor"
  63. else
  64.     local menuType = "Turbine"
  65. end
  66.  
  67. function autoMenu()
  68.     m.setTextScale(1)
  69.     button.clearTable()
  70.     button.setTable("Automatic", autoMode, "", 3, 13, 6, 6)
  71.     button.setTable("Manual", manualMode, "", 15, 25, 6, 6)
  72.    
  73.     if steamReactor then
  74.        button.setTable("Reactor", reactorMenu, "", 5, 18, 19, 19)
  75.        button.setTable("Turbine", turbineMenu, "", 22, 35, 19, 19)
  76.     end
  77.     button.screen()
  78.     checkMode()
  79.     menuMode()
  80. end
  81.  
  82. function manualMenu()
  83.     m.setTextScale(1)
  84.     button.clearTable()
  85.     button.setTable("Automatic", autoMode, "", 3, 13, 6, 6)
  86.     button.setTable("Manual", manualMode, "", 15, 25, 6, 6)
  87.     button.setTable("Online", online, "", 3, 13, 8, 8)
  88.     button.setTable("Offline", offline, "", 15, 25, 8, 8)
  89.     button.setTable("0", setRods, 0, 11,14, 10, 10)
  90.     button.setTable("10", setRods, 10, 5,8, 12, 12)
  91.     button.setTable("20", setRods, 20, 11,14, 12, 12)
  92.     button.setTable("30", setRods, 30, 17,20, 12, 12)
  93.     button.setTable("40", setRods, 40, 5,8, 14, 14)
  94.     button.setTable("50", setRods, 50, 11,14, 14, 14)
  95.     button.setTable("60", setRods, 60, 17,20, 14, 14)
  96.     button.setTable("70", setRods, 70, 5,8, 16, 16)
  97.     button.setTable("80", setRods, 80, 11,14, 16, 16)
  98.     button.setTable("90", setRods, 90, 17,20, 16, 16)
  99.     button.setTable("+", rodPlus, "", 23, 25, 12, 12)
  100.     button.setTable("-", rodMinus, "", 23, 25, 16, 16)
  101.     if steamReactor then
  102.         button.setTable("Reactor", reactorMenu, "", 5, 18, 19, 19)
  103.         button.setTable("Turbine", turbineMenu, "", 22, 35, 19, 19)
  104.     end
  105.     button.screen()
  106.     checkMode()
  107.     reactorOnOff()
  108.     menuMode()
  109. end
  110.  
  111. function turbineAutoMenu()
  112.     m.setTextScale(1)
  113.     button.clearTable()
  114.     button.setTable("Automatic", autoMode, "", 3, 13, 6, 6)
  115.     button.setTable("Manual", manualMode, "", 15, 25, 6, 6)
  116.     button.setTable("Reactor", reactorMenu, "", 5, 18, 19, 19)
  117.     button.setTable("Turbine", turbineMenu, "", 22, 35, 19, 19)
  118.     button.screen()
  119.     checkMode()
  120.     menuMode()
  121. end
  122.  
  123. function turbineManualMenu()
  124.     m.setTextScale(1)
  125.     button.clearTable()
  126.     button.setTable("Automatic", autoMode, "", 3, 13, 6, 6)
  127.     button.setTable("Manual", manualMode, "", 15, 25, 6, 6)
  128.     button.setTable("Reactor", reactorMenu, "", 5, 18, 19, 19)
  129.     button.setTable("Turbine", turbineMenu, "", 22, 35, 19, 19)
  130.     button.setTable("Online", setTurbineOnline, "", 3, 13, 8, 8)
  131.     button.setTable("Offline", setTurbineOffline, "", 15, 25, 8, 8)
  132.     button.setTable("Coils On", coilsOn, "", 3, 13, 10, 10)
  133.     button.setTable("Coils Off", coilsOff, "", 15, 25, 10, 10)
  134.     button.screen()
  135.     checkMode()
  136.     turbineOnOff()
  137.     coilsOnOff()
  138.     menuMode()
  139. end
  140.  
  141. function reactorMenu()
  142.     menuType = "Reactor"
  143.     displayScreen()
  144. end
  145.  
  146. function turbineMenu()
  147.     menuType = "Turbine"
  148.     displayScreen()
  149. end
  150.  
  151. function online()
  152.     r.setActive(true)
  153. end
  154.  
  155. function offline()
  156.     r.setActive(false)
  157. end
  158.  
  159. function setTurbineOnline()
  160.     t.setActive(true)
  161. end
  162.  
  163. function setTurbineOffline()
  164.     t.setActive(false)
  165. end
  166.  
  167. function reactorOnOff()
  168.     button.setButton("Online", r.getActive())
  169.     button.setButton("Offline", not r.getActive())
  170. end
  171.  
  172. function turbineOnOff()
  173.     button.setButton("Online", t.getActive())
  174.     button.setButton("Offline", not t.getActive())
  175. end
  176.  
  177. function coilsOnOff()
  178.     button.setButton("Coils On", t.getInductorEngaged())
  179.     button.setButton("Coils Off", not t.getInductorEngaged())
  180. end
  181.  
  182. function coilsOn()
  183.     t.setInductorEngaged(true)
  184. end
  185.  
  186. function coilsOff()
  187.     t.setInductorEngaged(false)
  188. end
  189.  
  190. function menuMode()
  191.     if steamReactor then
  192.         if menuType == "Reactor" then
  193.             button.setButton("Reactor", true)
  194.             button.setButton("Turbine", false)
  195.         else
  196.             button.setButton("Reactor", false)
  197.             button.setButton("Turbine", true)
  198.         end
  199.     end
  200. end
  201.  
  202. function setRods(setLevel)
  203.     button.flash(tostring(setLevel))
  204.     r.setAllControlRodLevels(setLevel)
  205.     fuelRodLevel()
  206. end
  207.  
  208. function rodPlus()
  209.     button.flash("+")
  210.     r.setAllControlRodLevels(rodLevel+1)
  211.     fuelRodLevel()
  212. end
  213.  
  214. function rodMinus()
  215.     button.flash("-")
  216.     r.setAllControlRodLevels(rodLevel-1)
  217.     fuelRodLevel()
  218. end
  219.  
  220. function checkMode()
  221.     button.toggleButton(mode)
  222. end
  223.    
  224. function manualMode()
  225.     mode = "Manual"
  226.     manualMenu()
  227. end
  228.  
  229. function autoMode()
  230.     mode = "Automatic"
  231.     displayScreen()
  232. end
  233.  
  234. function comma_value(amount)
  235.     local formatted = amount
  236.     local swap = false
  237.     if formatted < 0 then
  238.         formatted = formatted*-1
  239.         swap = true
  240.     end
  241.     while true do
  242.         formatted, k = string.gsub(formatted, "^(%d+)(%d%d%d)", '%1,%2')
  243.         if k == 0 then
  244.             break
  245.         end
  246.     end
  247.     if swap then
  248.         formatted = "-"..formatted
  249.     end
  250.     return formatted
  251. end
  252.  
  253. function displayEn()
  254.     m.clear()
  255.     m.setCursorPos(1,1)
  256.     m.write("Energy Use: ")
  257.     if energy < 0 then
  258.         m.setTextColor(colors.red)
  259.     else
  260.         m.setTextColor(colors.green)
  261.     end
  262.     m.write(comma_value(math.floor(energy)).. "RF/t")
  263.     m.setTextColor(colors.white)
  264.     m.setCursorPos(1,2)
  265.     m.write("Energy Stored: "..energyStoredPercent.."%")
  266.     if menuType == "Reactor" then
  267.         m.setCursorPos(1,3)
  268.         m.write("Reactor is: ")
  269.         if reactorOnline then
  270.             m.setTextColor(colors.green)
  271.             m.write("Online")
  272.         else
  273.             m.setTextColor(colors.red)
  274.             m.write("Offline")
  275.         end
  276.         m.setTextColor(colors.white)
  277.         m.setCursorPos(22,1)
  278.         if steamReactor then
  279.             m.write("Steam: ")
  280.             m.setTextColor(colors.green)
  281.             m.write(comma_value(math.floor(RFProduction)).."MB/t")
  282.         else
  283.             m.write("RF Gen: ")
  284.             m.setTextColor(colors.green)
  285.             m.write(comma_value(math.floor(RFProduction)).."RF/t")
  286.         end
  287.         m.setTextColor(colors.white)
  288.         m.setCursorPos(22,2)
  289.         m.write("Core Temp: "..math.floor(coreTemp).."c")
  290.         m.setCursorPos(22,3)
  291.         m.write("Fuel Use: "..fuelUse.."MB/t")  
  292.     else
  293.         m.setCursorPos(1,3)
  294.         m.write("Turbine is: ")
  295.         if turbineOnline then
  296.             m.setTextColor(colors.green)
  297.             m.write("Online")
  298.         else
  299.             m.setTextColor(colors.red)
  300.             m.write("Offline")
  301.         end
  302.         m.setCursorPos(1,4)
  303.         m.setTextColor(colors.white)
  304.         m.write("Reactor is: ")
  305.         if reactorOnline then
  306.             m.setTextColor(colors.green)
  307.             m.write("Online")
  308.         else
  309.             m.setTextColor(colors.red)
  310.             m.write("Offline")
  311.         end
  312.         m.setTextColor(colors.white)
  313.         m.setCursorPos(22,1)
  314.         m.write("RFGen: ")
  315.         m.setTextColor(colors.green)
  316.         m.write(comma_value(math.floor(turbineRFGen)).."RF/t")
  317.         m.setTextColor(colors.white)
  318.         m.setCursorPos(22,2)
  319.         m.write("Rotor: "..comma_value(math.floor(turbineRotorSpeed)).." RPM")
  320.         m.setCursorPos(22,3)
  321.         m.write("Steam: "..comma_value(turbineFluidRate).."MB/t")  
  322.     end
  323. end
  324.  
  325. function checkEn()
  326.     local tempEnergy = 0
  327.     energyStored = p.getEnergyStored()
  328.     energyMax = p.getMaxEnergyStored()
  329.     energyStoredPercent = math.floor((energyStored/energyMax)*100)
  330.     RFProduction = r.getEnergyProducedLastTick()
  331.     fuelUse = r.getFuelConsumedLastTick()
  332.     fuelUse = math.floor(fuelUse*100)
  333.     fuelUse = fuelUse/100
  334.     coreTemp = r.getFuelTemperature()
  335.     reactorOnline = r.getActive()
  336.     tempEnergy = p.getEnergyStored()
  337.     sleep(0.1)
  338.     energy = (p.getEnergyStored()-tempEnergy)/2
  339.     energy = energy*numCapacitors
  340.     if steamReactor then
  341.         turbineOnline = t.getActive()
  342.         turbineRotorSpeed = t.getRotorSpeed()
  343.         turbineRFGen = t.getEnergyProducedLastTick()
  344.         turbineFluidRate = t.getFluidFlowRate()
  345.     end
  346. end
  347.  
  348. function fuelRodLevel()
  349.     rodLevel = r.getControlRodLevel(0)
  350.     m.setCursorPos(30,5)
  351.     m.write(tostring(rodLevel).."%")
  352.     m.setBackgroundColor(colors.white)
  353.     m.setCursorPos(28,6)
  354.     m.write("       ")
  355.     for i = 1,10 do
  356.         m.setCursorPos(28,i+6)
  357.         m.setBackgroundColor(colors.white)
  358.         m.write(" ")
  359.         m.setBackgroundColor(colors.yellow)
  360.         m.write(" ")
  361.         if rodLevel/10 >= i then
  362.             m.setBackgroundColor(colors.red)
  363.         else
  364.             m.setBackgroundColor(colors.yellow)
  365.         end
  366.         m.write("   ")
  367.         m.setBackgroundColor(colors.yellow)
  368.         m.write(" ")
  369.         m.setBackgroundColor(colors.white)
  370.         m.write(" ")
  371.     end
  372.     m.setCursorPos(28,17)
  373.     m.write("       ")
  374.     m.setBackgroundColor(colors.black)
  375. end
  376.  
  377. function turbineInductorDisplay()
  378.     turbineInductor = t.getInductorEngaged()
  379.     m.setCursorPos(30,5)
  380.     if turbineInductor then
  381.         m.write("On")
  382.     else
  383.         m.write("Off")
  384.     end
  385.     m.setBackgroundColor(colors.gray)
  386.     m.setCursorPos(28,6)
  387.     m.write("       ")
  388.     for i = 1,7 do
  389.         m.setCursorPos(28,i+6)
  390.         m.setBackgroundColor(colors.gray)
  391.         m.write(" ")
  392.         m.setBackgroundColor(colors.lightGray)
  393.         m.write(" ")
  394.         if i % 2 == 0 then
  395.             m.setBackgroundColor(colors.gray)
  396.         end
  397.         m.write(" ")
  398.         m.setBackgroundColor(colors.gray)
  399.         m.write(" ")
  400.         if i % 2 ~= 0 then
  401.             m.setBackgroundColor(colors.lightGray)
  402.         end
  403.         m.write(" ")
  404.         m.setBackgroundColor(colors.lightGray)
  405.         m.write(" ")
  406.         m.setBackgroundColor(colors.gray)
  407.         m.write(" ")
  408.     end
  409.     for i = 8,10 do
  410.         m.setCursorPos(28,i+6)
  411.         m.setBackgroundColor(colors.gray)
  412.         m.write(" ")
  413.         m.setBackgroundColor(colors.lightGray)
  414.         m.write(" ")
  415.         if turbineInductor then
  416.             m.setBackgroundColor(colors.red)
  417.         else
  418.             m.setBackgroundColor(colors.blue)
  419.         end
  420.         m.write(" ")
  421.         m.setBackgroundColor(colors.gray)
  422.         m.write(" ")
  423.         if turbineInductor then
  424.             m.setBackgroundColor(colors.red)
  425.         else
  426.             m.setBackgroundColor(colors.blue)
  427.         end
  428.         m.write(" ")
  429.         m.setBackgroundColor(colors.lightGray)
  430.         m.write(" ")
  431.         m.setBackgroundColor(colors.gray)
  432.         m.write(" ")
  433.     end
  434.     m.setCursorPos(28,17)
  435.     m.write("       ")
  436.     m.setBackgroundColor(colors.black)
  437. end
  438.  
  439. function getClick()
  440.     local event, side, x, y = os.pullEvent("monitor_touch")
  441.     button.checkxy(x,y)
  442. end
  443.  
  444. function setVenting()
  445.     t.setVentAll()
  446.     t.setActive(true)
  447. end
  448.  
  449. function setVentRuning()
  450.     t.setVentOverflow()
  451.     t.setActive(true)
  452. end
  453.  
  454. function turbineWarmup()
  455.     setTurbineOnline()
  456.     coilsOff()
  457. end
  458.  
  459. function turbineRunning()
  460.     setTurbineOnline()
  461.     coilsOn()
  462. end
  463.  
  464.    
  465. function findOptFuelRods()
  466.     m.clear()
  467.     r.setActive(false)
  468.     checkEn()
  469.     displayEn()
  470.     fuelRodLevel()
  471.     while r.getFuelTemperature() > 99 do
  472.         turbineWarmup()
  473.         for i= 1,3 do
  474.             checkEn()
  475.             displayEn()
  476.             fuelRodLevel()
  477.             m.setCursorPos(3,6)
  478.             m.write("Finding Optimal Rod Level")
  479.             m.setCursorPos(3,7)
  480.             m.write("Core Temp: "..r.getFuelTemperature())
  481.             m.setCursorPos(3,8)
  482.             m.write("Waiting for 99c")
  483.             sleep(1)
  484.         end
  485.     end
  486.     local reactorFluidCap = r.getCoolantAmountMax()
  487.     while r.getHotFluidAmount() > reactorFluidCap do
  488.         turbineWarmup()
  489.         for i = 1,3 do
  490.             checkEn()
  491.             displayEn()
  492.             fuelRodLevel()
  493.             setVentRuning()
  494.             m.setCursorPos(2,6)
  495.             m.write("Finding Optimal Rod Level, please wait...")
  496.             m.setCursorPos(2,7)
  497.             m.write("Fluid Amount: "..comma_value(r.getHotFluidAmount()).."mb")
  498.             m.setCursorPos(2,8)
  499.             m.write("Waiting for fluid levels.")
  500.             sleep(1)
  501.         end
  502.     end
  503.     setVentRuning()
  504.     r.setAllControlRodLevels(99)
  505.     r.setActive(true)
  506.     while r.getFuelTemperature() < 100 do
  507.         for i = 1,5 do
  508.             turbineWarmup()
  509.             checkEn()
  510.             displayEn()
  511.             fuelRodLevel()
  512.             setVentRuning()
  513.             m.setCursorPos(3,6)
  514.             m.write("Set all rod levels to 99")
  515.             m.setCursorPos(3,7)
  516.             m.write("Waiting 5 seconds...")
  517.             sleep(1)
  518.         end
  519.     end
  520.     for i = 1,5 do
  521.         turbineWarmup()
  522.         checkEn()
  523.         displayEn()
  524.         fuelRodLevel()
  525.         setVentRuning()
  526.         m.setCursorPos(3,6)
  527.         m.write("Set all rod levels to 99")
  528.         m.setCursorPos(3,7)
  529.         m.write("Waiting 5 seconds...")
  530.         sleep(1)
  531.     end
  532.     local tempMB = r.getEnergyProducedLastTick()
  533.     local tempRodLevels = math.floor(steamMin/tempMB)
  534.     tempRodLevels = 100-tempRodLevels+reactorOffset
  535.     if tempRodLevels < 0 then
  536.         if reactorOffset <0 then
  537.             reactorOffset = 5
  538.         end
  539.         tempRodLevels = (0 + reactorOffset)
  540.     end
  541.     r.setAllControlRodLevels(math.floor(tempRodLevels))
  542.     for i = 1,10 do
  543.         turbineWarmup()
  544.         checkEn()
  545.         displayEn()
  546.         fuelRodLevel()
  547.         setVentRuning()
  548.         m.setCursorPos(3,6)
  549.         m.write("Estimated Level: "..tempRodLevels)
  550.         m.setCursorPos(3,7)
  551.         m.write("Waiting 10 seconds...")
  552.         sleep(1)
  553.     end
  554.     tempMB = r.getEnergyProducedLastTick()
  555.     while tempMB > steamMin do
  556.         turbineWarmup()
  557.         tempRodLevels = tempRodLevels+1
  558.         r.setAllControlRodLevels(math.floor(tempRodLevels))
  559.         for i = 1,5 do
  560.             turbineWarmup()
  561.             checkEn()
  562.             displayEn()
  563.             fuelRodLevel()
  564.             setVentRuning()
  565.             m.setCursorPos(3,6)
  566.             m.write("Getting below "..steamMin.."mb/t")
  567.             m.setCursorPos(3,7)
  568.             m.write("Currently at: "..tempMB)
  569.             sleep(1)
  570.         end
  571.         tempMB = r.getEnergyProducedLastTick()
  572.     end
  573.     while tempMB < steamMin do
  574.         tempRodLevels = tempRodLevels -1
  575.         r.setAllControlRodLevels(math.floor(tempRodLevels))
  576.         for i = 1,5 do
  577.             turbineWarmup()
  578.             checkEn()
  579.             displayEn()
  580.             fuelRodLevel()
  581.             setVentRuning()
  582.             m.setCursorPos(3,6)
  583.             m.write("Getting Above "..steamMin.."mb/t")
  584.             m.setCursorPos(3,7)
  585.             m.write("Currently at: "..tempMB)
  586.             sleep(1)
  587.         end
  588.         tempMB = r.getEnergyProducedLastTick()
  589.     end
  590.     OptFuelRodLevel = tempRodLevels
  591. end
  592.    
  593. function autoReactor()
  594.     if not steamReactor then
  595.         r.setAllControlRodLevels(0)
  596.         if energyStoredPercent < turnOnAt then
  597.             if not reactorOnline then
  598.                 online()
  599.             end
  600.         end
  601.         if energyStoredPercent > turnOffAt then
  602.             if reactorOnline then
  603.                 offline()
  604.             end
  605.         end
  606.     else
  607.         r.setAllControlRodLevels(OptFuelRodLevel)
  608.         if energyStoredPercent < turnOnAt then
  609.             turbineRunning()
  610.         end
  611.         if energyStoredPercent > turnOffAt then
  612.             turbineWarmup()
  613.         end
  614.         if turbineRotorSpeed > targetSpeed then
  615.             offline()
  616.         else
  617.             online()
  618.         end
  619.     end
  620. end
  621.  
  622. function displayScreen()
  623.     checkEn()
  624.     displayEn()
  625.     if menuType == "Reactor" then
  626.         fuelRodLevel()
  627.         if mode == "Automatic" then
  628.             autoMenu()
  629.             autoReactor()
  630.         else
  631.             manualMenu()
  632.         end
  633.     else
  634.         turbineInductorDisplay()
  635.         if mode == "Automatic" then
  636.             turbineAutoMenu()
  637.             autoReactor()
  638.         else
  639.             turbineManualMenu()
  640.         end
  641.     end
  642.     timerCode = os.startTimer(1)
  643.     local event, side, x, y
  644.     repeat
  645.     event, side, x, y = os.pullEvent()
  646.     until event~= "timer" or timerCode == side
  647.     if event == "monitor_touch" then
  648.     button.checkxy(x,y)
  649.     end
  650. end
  651.  
  652. if steamReactor then
  653.    findOptFuelRods()
  654. end
  655.  
  656. while true do
  657.    displayScreen()
  658. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement