blackrabt

BR Monitor 2 turbine

Mar 3rd, 2015
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 16.33 KB | None | 0 0
  1.  
  2.     --version 0.1.4
  3.   --fixed if true then statements to be cleaner
  4.   --supports 2 turbines
  5.   --detects if second turbine is present
  6.   --fixed stall on coroutine.yield()
  7.   --improved formatting
  8.   --checks for connections before attempting updateData()
  9. --now with more reliability!
  10.  
  11. function loadPeripherals()
  12. if peripheral.isPresent("left") then
  13.   local wireless = peripheral.wrap("left")
  14.   --wireless.open(4)
  15.   wireless.open(65535)
  16. end
  17. --if peripheral.isPresent("BigReactors-Reactor_1") == false then
  18. --  term.write("No Reactor Present!")
  19.   --shell.exit()
  20. --end
  21.   modem = peripheral.wrap("back")
  22.   monitor = peripheral.wrap("monitor_2")
  23.   if peripheral.isPresent("BigReactors-Reactor_1") then
  24.     reactor = peripheral.wrap("BigReactors-Reactor_1")
  25.     reactorPresent = true
  26.   end
  27.   if peripheral.isPresent("BigReactors-Turbine_0") then
  28.    turbine = peripheral.wrap("BigReactors-Turbine_0")
  29.    firstTurbinePresent = true
  30.   end
  31.   if peripheral.isPresent("BigReactors-Turbine_1") then
  32.     secondTurbinePresent = true
  33.     turbine2 = peripheral.wrap("BigReactors-Turbine_1")
  34.   end
  35.   term.clear()
  36.   monitor.setBackgroundColor(32768)
  37.   monitor.clear()
  38.   term.setCursorPos(3,1)
  39.   wirelessSide = "left"
  40.   modemSide = "top"
  41.   rednet.open(wirelessSide)
  42. end
  43.  
  44. function round2(num, places)
  45.   num = tostring(num)
  46.   local inc = false
  47.     if num == nil then
  48.       num = 1
  49.     end --testing this piece out to avoid errors when the reactor doesn't report fuel usage for a tick
  50.     local decimal = string.find(num, "%.")
  51.     if decimal == nil then
  52.       decimal = 1
  53.     end
  54.     if num:len() - decimal <= places then
  55.       return tonumber(num)
  56.     end --already rounded, nothing to do.
  57.     local digit = tonumber(num:sub(decimal + places + 1))
  58.     num = num:sub(1, decimal + places)
  59.     if digit <= 4 then
  60.       return tonumber(num)
  61.     end --no incrementation needed, return truncated number
  62.     local newNum = ""
  63.     for i=num:len(), 1, -1 do
  64.       digit = tonumber(num:sub(i))
  65.       if digit == 9 then
  66.         if i > 1 then
  67.           newNum = "0"..newNum
  68.         else
  69.           newNum = "10"..newNum
  70.         end
  71.       elseif digit == nil then
  72.         newNum = "."..newNum
  73.       else
  74.         if i > 1 then
  75.           newNum = num:sub(1,i-1)..(digit + 1)..newNum
  76.         else
  77.           newNum = (digit + 1)..newNum
  78.         end
  79.                     return tonumber(newNum) --No more 9s found, so we are done incrementing. Copy remaining digits, then return number.
  80.                   end
  81.                 end
  82.                 return tonumber(newNum)
  83.               end
  84.  
  85.               function updateData()
  86.   --declare some reactor variables sucka!
  87.   if reactorPresent then
  88.     fuelTempFloat = reactor.getFuelTemperature()
  89.     if fuelTempFloat == nil then
  90.       fuelTempFloat = 1
  91.     end
  92.     fuelTemp = math.floor(fuelTempFloat)
  93.     reactorIsConnected = reactor.getConnected()
  94.     reactorIsActive = reactor.getActive()
  95.     controlRodCount = reactor.getNumberOfControlRods()
  96.     reactorCasingTempFloat = reactor.getCasingTemperature()
  97.     if reactorCasingTempFloat == nil then
  98.       reactorCasingTempFloat = 0
  99.     end
  100.     reactorCasingTemp = math.floor(reactorCasingTempFloat) --use math.floor to keep it a whole integer otherwise it has too many sigfigs.
  101.     reactorFuelAmount = reactor.getFuelAmount()
  102.     reactorWasteAmount = reactor.getWasteAmount()
  103.     reactorCoolantType = reactor.getCoolantType()
  104.     controlRodDesiredLevel = 0
  105.     reactorFuelUsed = reactor.getFuelConsumedLastTick()
  106.     reactorSteamAmount = reactor.getHotFluidAmount()
  107.     reactorSteamMade = reactor.getHotFluidProducedLastTick()
  108.     controlRodLevelOne = reactor.getControlRodLevel(0) --remember that the index is one less than rod count. find a way to let this adjust for number of control rods automat
  109.     if controlRodLevelOne == nil then
  110.       controlRodLevelOne = 0
  111.     end
  112.     controlRodLevelTwo = reactor.getControlRodLevel(1)
  113.     if controlRodLevelTwo == nil then
  114.       controlRodLevelTwo = 0
  115.     end
  116.     controlRodLevelThree = reactor.getControlRodLevel(2)
  117.     if controlRodLevelThree == nil then
  118.       controlRodLevelThree = 0
  119.     end
  120.     controlRodLevelFour = reactor.getControlRodLevel(3)
  121.     if controlRodLevelFour == nil then
  122.       controlRodLevelFour = 0
  123.     end
  124.     controlRodAverage = ((controlRodLevelOne + controlRodLevelTwo + controlRodLevelThree + controlRodLevelFour)) / (4)
  125.   end
  126.   --turbine variables will go here if this turns out to be beneficial
  127.   if firstTurbinePresent then
  128.     idealRotorRPM = 1800
  129.     turbineIsConnected = turbine.getConnected()
  130.     turbineIsActive = turbine.getActive()
  131.     turbineRotorSpeedFloat = turbine.getRotorSpeed()
  132.     if turbineRotorSpeedFloat == nil then
  133.       turbineRotorSpeedFloat = 0
  134.     end
  135.     turbineRotorSpeed = math.floor(turbineRotorSpeedFloat)
  136.     turbineFluidRate = turbine.getFluidFlowRate()
  137.     turbineEngaged = turbine.getInductorEngaged()
  138.     turbineEnergyFloat = turbine.getEnergyProducedLastTick()
  139.     if turbineEnergyFloat == nil then
  140.       turbineEnergyFloat = 0
  141.     end
  142.     turbineEnergy = math.floor(turbineEnergyFloat)
  143.   end
  144.   --turbine 2
  145.   if secondTurbinePresent then
  146.     turbine2IsConnected = turbine2.getConnected(1)
  147.     idealRotorRPM2 = 1800
  148.     turbine2IsActive = turbine2.getActive()
  149.     turbine2RotorSpeedFloat = turbine2.getRotorSpeed()
  150.     if turbine2RotorSpeedFloat == nil then
  151.       turbine2RotorSpeedFloat = 0
  152.     end
  153.     turbine2RotorSpeed = math.floor(turbine2RotorSpeedFloat)
  154.     turbine2FluidRate = turbine2.getFluidFlowRate()
  155.     turbine2Engaged = turbine2.getInductorEngaged()
  156.     turbine2EnergyFloat = turbine2.getEnergyProducedLastTick()
  157.     if turbine2EnergyFloat == nil then
  158.       turbine2EnergyFloat = 0
  159.     end
  160.     turbine2Energy = math.floor(turbine2EnergyFloat)
  161.   end
  162. end
  163.  
  164. function tabletRequest()
  165.   wirelessSide = "left"
  166.   modemSide = "top"
  167.   local wireless = peripheral.wrap(wirelessSide)
  168.   local modem = peripheral.wrap(modemSide)
  169.   rednet.open(wirelessSide)
  170.   if wirelessSide == nil then
  171.     print("No Modem")
  172.     print("Will shutdown in 3 seconds")
  173.     sleep(3)
  174.   --os.shutdown()
  175.   else
  176.     print("Opened modem on ".. wirelessSide)
  177.   end
  178.   os.sleep(0.2)
  179.   id, message = rednet.receive()
  180.   term.setCursorPos(1,1)
  181.   print ("Device " .. id .. " sent a " .. message .. " ")
  182.   os.sleep(1)
  183.   term.setCursorPos(1,2)
  184.   print ("Sending reactor info...")
  185.   local reactor
  186.     local reactor = peripheral.wrap("BigReactors-Reactor_0")
  187.     if peripheral.wrap("back") then
  188.        term.setCursorPos(1,3)
  189.        print ("Found Reactor")
  190.     end
  191.     active = reactor.getActive(1)
  192.     if active then
  193.       term.setCursorPos(1,5)
  194.       print ("The Reactor is: ")
  195.       term.setTextColor(colors.green)
  196.       print ("ON")
  197.     else term.setTextColor(colors.white)
  198.       print ("The Reactor is: ")
  199.       term.setTextColor(colors.red)
  200.       print ("OFF")
  201.       rednet.send(id, "The Reactor is: OFF")
  202.       sleep(.1)
  203.       rednet.send(id, "The Reactor is: OFF")
  204.     --os.reboot()
  205.     --runProgram()
  206.     --exit() --stops whole program
  207.     --break --only in a loop, at the end
  208.     return --i think this did it
  209.   end
  210.   term.setTextColor(colors.white)
  211.   lvl = reactor.getControlRodLevel(0)
  212.   if lvl then
  213.     print ("Control rod level is " .. lvl .. "% closed")
  214.   end
  215.   turbineEnergy = turbine.getEnergyProducedLastTick()
  216.      tick = turbineEnergy  --reactor.getEnergyProducedLastTick(1)
  217.      if tick then
  218.        print (" " .. tick .. " RF/t ")
  219.      end
  220.      rednet.send(id, "The Reactor is: ON")
  221.      os.sleep(0.2)
  222.      rednet.send(id, "Control rod level is " .. lvl .. "% closed")
  223.      os.sleep(0.2)
  224.      rednet.send(id, " Producing " .. tick .. " RF/t ")
  225.      return
  226.   --os.reboot()
  227.   -- The Pocket computer program is at /bkc8M1Bi
  228.   -- Original code by Dukrobber
  229. -- Updates by blackrabt
  230. end
  231.  
  232. function createDisplayReactor()
  233.   --reactor display stuff
  234.   monitor.setBackgroundColor(32768) --black
  235.   monitor.setTextColor(512) --cyan
  236.   monitor.clear()
  237.   monitor.setCursorPos(4,1)
  238.   --set text color based on reactor status?
  239.   monitor.write("Reactor")
  240.   --term.redirect(monitor)
  241.   --paintutils.drawLine(3,3,7,3,1)
  242.   --term.redirect(term.native())
  243.   monitor.setCursorPos(2,2)
  244.   monitor.write("-----------") --___________
  245.   monitor.setCursorPos(3,3)
  246.   if reactorPresent then
  247.     if reactorIsConnected then
  248.       monitor.setTextColor(8192)
  249.       monitor.write("Connected")
  250.     end
  251.   --skip the rest of the reactor display sections in this event
  252.   monitor.setCursorPos(3,4)
  253.   if reactorIsActive then
  254.     monitor.setTextColor(32)
  255.     monitor.write("Active")
  256.   --if peripheral.isPresent("left") then
  257.   --  testPDA.print(reactorIsActive)
  258.   --  testPDA.print("test")
  259.   --end
  260.   monitor.setTextColor(512)
  261.   monitor.setCursorPos(3,5)
  262.  
  263.   monitor.setTextColor(16) --yellow
  264.   monitor.write("Casing: "..reactorCasingTemp.."C")
  265.   monitor.setCursorPos(3,6)
  266.   if fuelTemp > 170 and fuelTemp < 230 then
  267.     monitor.setTextColor(32)
  268.   end
  269.   if fuelTemp < 170 and fuelTemp > 100 then
  270.     monitor.setTextColor(16)
  271.   end
  272.   if fuelTemp < 100 then
  273.     monitor.setTextColor(16384)
  274.   end
  275.   if fuelTemp > 230 and fuelTemp < 300 then
  276.     monitor.setTextColor(16)
  277.   end
  278.   if fuelTemp > 3000 then
  279.     monitor.setTextColor(16384)
  280.     monitor.setCursorPos(11,6)
  281.     monitor.write("CAUTION!")
  282.     --while fuelTemp > 300 do --this makes a flashing caution but it halts the whole program
  283.       --monitor.setTextColor(16384)
  284.       --monitor.setCursorPos(3,11)
  285.       --monitor.write("CAUTION!")
  286.       --sleep(0.4)
  287.       --monitor.setTextColor(64)
  288.       --monitor.setCursorPos(3,11)
  289.       --monitor.write("CAUTION!")
  290.       --sleep(0.4)
  291.       --monitor.setTextColor(2)
  292.       --monitor.setCursorPos(3,11)
  293.       --monitor.write("CAUTION!")
  294.     --end
  295.   end
  296.   monitor.write("Core:   "..fuelTemp.."C")
  297.   monitor.setCursorPos(3,7)
  298.   monitor.setTextColor(256)
  299.   monitor.write("HFO:    "..reactorSteamMade.."mB/t")
  300.   monitor.setCursorPos(3,9)
  301.   controlRods = math.floor(controlRodAverage)
  302.   monitor.write("Avg. Control Rod: "..controlRodAverage.."% ")
  303.   monitor.setCursorPos(3,8)
  304.   --monitor.write("FBR:    "..reactorFuelUsed.."mB/t")
  305.   --  f = tostring(reactorFuelUsed)
  306.   --f = reactorFuelUsed
  307.   --fuelFormatted = (string.format("%.4f", reactor.getFuelConsumedLastTick()))
  308.   fuelFormatted = round2(reactorFuelUsed, 2)
  309.   -- monitor.write(fuelFormatted)
  310.   monitor.write("FBR:    "..fuelFormatted.."mB/t")
  311.   elseif reactorIsConnected == false then
  312.     monitor.setTextColor(4)
  313.     monitor.write("Disconnected")
  314.     return--skip the rest
  315.   end
  316.   elseif reactorIsActive == false then
  317.     monitor.setCursorPos(3,4)
  318.     monitor.setTextColor(16)
  319.     monitor.write("Inactive")
  320.     --return
  321.   end
  322. end
  323.  
  324. function createDisplayTurbine1()
  325.  
  326.     --turbine display arranged for a shorter and wider display
  327.   -- monitor.setTextColor(512) --cyan
  328.   -- monitor.setCursorPos(4,11)
  329.   -- monitor.write("Turbine Status    1           2")
  330.   -- monitor.setCursorPos(2,12)
  331.   -- monitor.write("-------------------------------")
  332.   -- --term.redirect(monitor)
  333.   -- --paintutils.drawLine(18,3,25,3,512)
  334.   -- --term.redirect(term.native())
  335.   -- monitor.setCursorPos(1,13)
  336.   -- monitor.setTextColor(64)
  337.   -- monitor.write("Network Status")
  338.  
  339.   -- monitor.setCursorPos(3,13)
  340.   -- if turbineIsConnected then
  341.   --   monitor.setTextColor(8192)
  342.   --   monitor.write("Connected") --should say Connected
  343.   -- end
  344.   -- if turbineIsConnected == false then
  345.   --   monitor.setTextColor(16384)
  346.   --   monitor.write("Disconnected")
  347.   -- end
  348.   -- monitor.write(turbineIsConnected)
  349.   -- monitor.setCursorPos(1,14)
  350.   -- monitor.setTextColor(64)
  351.   -- monitor.write("Device Status: ")
  352.  
  353.   -- monitor.setCursorPos(1,15)
  354.   -- monitor.setTextColor(64)
  355.   -- monitor.write("Coil Status: ")
  356.  
  357.   -- monitor.setCursorPos(1,16)
  358.   -- monitor.setTextColor(64)
  359.   -- monitor.write("Generation: ")
  360.  
  361.   -- monitor.setCursorPos(1,17)
  362.   -- monitor.setTextColor(64)
  363.   -- monitor.write("Rotor Speed: ")
  364.  
  365.   monitor.setTextColor(512) --cyan
  366.   monitor.setCursorPos(4,11)
  367.   monitor.write("Turbine 1")
  368.   monitor.setCursorPos(2,12)
  369.   monitor.write("-----------")
  370.   if firstTurbinePresent then
  371.     monitor.setCursorPos(3,13)
  372.     if turbineIsConnected then
  373.       monitor.setTextColor(8192)
  374.       monitor.write("Connected") --should say Connected
  375.     else
  376.       monitor.setTextColor(16384)
  377.       monitor.write("Disconnected")
  378.     end
  379.   end
  380.  
  381.   monitor.setCursorPos(3,14)
  382.   if turbineIsActive then
  383.     monitor.setTextColor(8192)
  384.     monitor.write("Active")
  385.   end
  386.   if turbineIsActive == false then
  387.     monitor.setTextColor(16)
  388.     monitor.write("Inactive")
  389.   end
  390.   monitor.setCursorPos(3,15)
  391.   if turbineEngaged then
  392.     monitor.setTextColor(32)
  393.     monitor.write("Coils Engaged")
  394.   end
  395.   if turbineEngaged == false then
  396.     monitor.setTextColor(16)
  397.     monitor.write("Not Engaged")
  398.   end
  399.   --monitor.setCursorPos(15,15)
  400.   --monitor.write("15,15")
  401.   monitor.setCursorPos(3,16)
  402.   if turbineEnergy > 1000 and turbineEnergy < 10000 then
  403.     monitor.setTextColor(16)
  404.   end
  405.   if turbineEnergy > 10000 then
  406.     monitor.setTextColor(32)
  407.   end
  408.   if turbineEnergy < 1000 then
  409.     monitor.setTextColor(16384)
  410.   end
  411.   monitor.write("Generating:  "..turbineEnergy.."RF")
  412.   monitor.setCursorPos(3,17)
  413.   if turbineRotorSpeed > 0 then
  414.  
  415.     monitor.setTextColor(32)
  416.     monitor.write("Rotor speed: "..turbineRotorSpeed.."RPM")
  417.   end
  418.   if turbineRotorSpeed <1 then
  419.     monitor.setTextColor(16384)
  420.     monitor.write("Rotor speed: "..turbineRotorSpeed.."RPM")
  421.   end
  422. end --ends createDisplayTurbine1() don't forget to update runProgram()
  423.     --TURBINE 2 GOES HERE
  424. function createDisplayTurbine2()
  425.   monitor.setTextColor(512) --cyan
  426.   monitor.setCursorPos(4,20)
  427.   monitor.write("Turbine 2")
  428.   monitor.setCursorPos(2,21)
  429.   monitor.write("-----------")
  430.  
  431.  
  432.   monitor.setCursorPos(3,22)
  433.   if secondTurbinePresent then  
  434.     if turbine2IsConnected then
  435.       monitor.setTextColor(8192)
  436.       monitor.write("Connected") --should say Connected
  437.     end
  438.   --monitor.write(turbineIsConnected)
  439.   monitor.setCursorPos(3,23)
  440.   if turbine2IsActive then
  441.     monitor.setTextColor(8192)
  442.     monitor.write("Active")
  443.   end
  444.   if turbine2IsActive == false then
  445.     monitor.setTextColor(16)
  446.     monitor.write("Inactive")
  447.   end
  448.   monitor.setCursorPos(3,24)
  449.   if turbineEngaged then
  450.     monitor.setTextColor(32)
  451.     monitor.write("Coils Engaged")
  452.   end
  453.   if turbine2Engaged == false then
  454.     monitor.setTextColor(16)
  455.     monitor.write("Not Engaged")
  456.   end
  457.   --monitor.setCursorPos(15,15)
  458.   --monitor.write("15,15")
  459.   monitor.setCursorPos(3,25)
  460.   if turbine2Energy > 1000 and turbine2Energy < 10000 then
  461.     monitor.setTextColor(16)
  462.   end
  463.   if turbine2Energy > 10000 then
  464.     monitor.setTextColor(32)
  465.   end
  466.   if turbine2Energy < 1000 then
  467.     monitor.setTextColor(16384)
  468.   end
  469.   monitor.write("Generating:  "..turbine2Energy.."RF")
  470.   monitor.setCursorPos(3,26)
  471.   if turbine2RotorSpeed > 0 then
  472.     monitor.setTextColor(32)
  473.     monitor.write("Rotor speed: "..turbine2RotorSpeed.."RPM")
  474.   end
  475.   if turbine2RotorSpeed <1 then
  476.     monitor.setTextColor(16384)
  477.     monitor.write("Rotor speed: "..turbine2RotorSpeed.."RPM")
  478.   end
  479.   else
  480.     if turbine2IsConnected == false then
  481.       monitor.setTextColor(16384)
  482.       monitor.write("Disconnected")
  483.     end
  484.     return
  485. end--end if statement
  486. end--end function
  487.  
  488.  
  489. function runProgram()
  490.   loadPeripherals()
  491.   updateData()
  492.   createDisplayReactor()
  493.   createDisplayTurbine1()
  494.   createDisplayTurbine2()
  495.   --coroutine.yield()
  496.   --tabletRequest()
  497.   sleep(1.5)
  498. end
  499.  
  500. -- function runAsCoroutine()
  501. --   runProgram()
  502. -- end
  503.  
  504. -- local theReactorCoroutine = coroutine.create(runProgram)
  505. -- local theTabletCoroutinr = coroutine.create(tabletRequest)
  506.  
  507. -- while true do
  508. --   coroutine.resume(theReactorCoroutine)
  509. --   if os.pullEvent(rednet.receive()) then
  510. --     coroutine.yield(theReactorCoroutine)
  511. --     coroutine.resume(theTabletCoroutinr)
  512. --     coroutine.resume(theReactorCoroutine)
  513. --   end
  514. -- end
  515.  
  516. --[[need to pcall(round2)?]]
  517. --this is supposed to run the peripheral loading in a protected call, and throw an error if there is a problem. It doesn't want to do that yet.
  518. --if pcall(loadPeripherals) then
  519. --  while true do
  520. --    runProgram()
  521. --  end
  522.   --runProgram()
  523. --else
  524.   --there was a problem
  525. --end
  526.  
  527. while true do
  528.   runProgram()
  529.   if rednet.receive(2) then --not nil then
  530.     tabletRequest()
  531.   end
  532. end
Advertisement
Add Comment
Please, Sign In to add comment