Advertisement
Guest User

Turbine Controler

a guest
Aug 14th, 2016
1,131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.73 KB | None | 0 0
  1. local com = require "component"
  2.  
  3. --component wrapping
  4. local gpu = com.gpu
  5. local capacitorBank = com.capacitor_bank
  6. local R = com.br_reactor
  7.  
  8. local insertionHigh = 95 -- high control rod level
  9. local insertionLow = 64 -- low control rod level
  10. local hotPoint 400 -- point where reactor gets forced shut down from over heating
  11. R.setAllControlRodLevels(insertionLow)
  12. --used varriables
  13. local addList = {} --Stores list of turbine address
  14. --Populates addList tabel with turbine address
  15. local a = 1
  16. for k, v in com.list("br_turbine") do
  17.     addList[a]  = k
  18.     a = a + 1
  19. end
  20.  
  21. local allData = {}
  22.     allData["turbine"] = {}
  23.     allData["reactor"] = {}
  24.     allData["capacitor"] = {}
  25.  
  26. local function rpm(num)
  27.     allData["turbine"][num]={}
  28.     local turbineRPM = com.invoke(addList[num], "getRotorSpeed")
  29.     if turbineRPM < 1798 then --keeps the turbines spinning at 1800 rpm even if steam flow rate is very low
  30.         com.invoke(addList[num], "setInductorEngaged", false)
  31.         allData["turbine"][num]["state"] = "Inactive"
  32.     else
  33.         com.invoke(addList[num], "setInductorEngaged", true)
  34.         allData["turbine"][num]["state"] = "Active"
  35.     end
  36.     allData["turbine"][num]["RPM"] = turbineRPM
  37. end
  38.  
  39. local function steam(num)
  40.     allData["turbine"][num]["FluidRate"] = com.invoke(addList[num], "getFluidFlowRate")
  41. end
  42.  
  43. local function reactorHeat()
  44.     local heat = R.getCasingTemperature()
  45.     local capEnergy = capacitorBank.getEnergyStored()
  46. --simple control if the energy level of my capacitors reaches below 80% it adjusts the rector control rods to be less inserted
  47. --if the stored power is above 93% then it adjusts the control rods to be more inserted
  48.     allData["capacitor"] = capEnergy
  49.     capEnergy = capEnergy/1000000000
  50.     if capEnergy <.8 then
  51.             R.setActive(true)
  52.             R.setAllControlRodLevels(insertionLow)
  53.     elseif capEnergy > 0.93 then
  54.             R.setAllControlRodLevels(insertionHigh)
  55.     end
  56.     --keeps reactor from getting to hot if the steam is backed up
  57.     if heat > hotPoint then
  58.         R.setActive(false)
  59.     end
  60.     allData["reactor"]["temp"] = heat
  61.     if R.getActive() == true then
  62.         allData["reactor"]["state"] = "Active"
  63.     else
  64.         allData["reactor"]["state"] = "Inactive"
  65.     end
  66.     allData["reactor"]["FluidRate"] = R.getHotFluidProducedLastTick()
  67.     allData["reactor"]["controlRodLevel"] = R.getControlRodLevel(1)
  68. end
  69.  
  70. local function turbine()
  71.     for i = 1, (a-1) do
  72.         rpm(i)
  73.         steam(i)
  74.     end
  75. end
  76.  
  77. -- prints all contents of a passed tabel
  78. function print_r ( t )  
  79.     local print_r_cache={}
  80.     local function sub_print_r(t,indent)
  81.         if (print_r_cache[tostring(t)]) then
  82.             print(indent.."*"..tostring(t))
  83.         else
  84.             print_r_cache[tostring(t)]=true
  85.             if (type(t)=="table") then
  86.                 for pos,val in pairs(t) do
  87.                     if (type(val)=="table") then
  88.                         print(indent.."["..pos.."] => "..tostring(t).." {")
  89.                         sub_print_r(val,indent..string.rep(" ",string.len(pos)+8))
  90.                         print(indent..string.rep(" ",string.len(pos)+6).."}")
  91.                     elseif (type(val)=="string") then
  92.                         print(indent.."["..pos..'] => "'..val..'"')
  93.                     else
  94.                         print(indent.."["..pos.."] => "..tostring(val))
  95.                     end
  96.                 end
  97.             else
  98.                 print(indent..tostring(t))
  99.             end
  100.         end
  101.     end
  102.     if (type(t)=="table") then
  103.         print(tostring(t).." {")
  104.         sub_print_r(t,"  ")
  105.         print("}")
  106.     else
  107.         sub_print_r(t,"  ")
  108.     end
  109.     print()
  110. end
  111.  
  112. local function Data()
  113.     print_r(allData)
  114.     allData = {}
  115.     allData["turbine"] = {}
  116.     allData["reactor"] = {}
  117.     allData["capacitor"] = {}
  118. end
  119.  
  120. while true do
  121.     turbine()
  122.     reactorHeat()
  123.     Data()
  124.     os.sleep(1)
  125. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement