Advertisement
Guest User

rfmanager.lua

a guest
Jan 22nd, 2018
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.50 KB | None | 0 0
  1. local component = require("component")
  2. local computer = require("computer")
  3. local math = require("math")
  4. local term = require("term")
  5. local event = require("event")
  6.  
  7. local running = true
  8.  
  9.  
  10. --Addresses of Engine Control Units
  11. local ecuAddress =
  12. {
  13.   "cd1ae864-e5aa-42b0-abfc-d7134e5da40f",
  14.   "b12af19b-3357-43c6-99a5-287a0f21256d",
  15.   "fa2cbaa6-776c-4f75-baaa-e740e64fb765",
  16.   "27a756ad-8b67-4551-85d0-8a654b96f5e1"
  17. }
  18.  
  19. --Address of RF Battery (will find automatically by default)
  20. local batAddress = component.list("RFBattery")()
  21.  
  22. --Maximum power output of one generation unit in RF
  23. local maxOutput = 129052  
  24.  
  25. --Create ECU Proxy Table (contains component proxy, engine state, and RF output values per engine state)
  26. local ecu = {}
  27. for i = 1, #ecuAddress do
  28.   ecu[component.proxy(ecuAddress[i])] = {curState = 0, stateOpt = {["0"] = 0, ["1"] = maxOutput / 16, ["2"] = maxOutput / 4, ["3"] = maxOutput / 2, ["4"] = maxOutput / 1}}
  29. end
  30.  
  31. --Create RF Battery Proxy Table (contains component proxy, Last RF Value, Last Tick, Current RF Value, and Storage Percent)    
  32. local bat =
  33. {
  34.   proxy = component.proxy(batAddress),
  35.   lastRF = 0,
  36.   curRF = 0,
  37.   curTick = 0,
  38.   lastTick = 0,
  39.   io = 0,
  40.   pRF = 0
  41. }
  42.  
  43. --Sync ECU State and stored state to full power
  44. for proxy, _ in pairs(ecu) do
  45.   proxy.setECU(4)
  46.   ecu[proxy].curState = 4
  47. end
  48.  
  49. --Shutdown function
  50. function shutDown()
  51.   running = false
  52.   term.clear()
  53.   return false
  54. end
  55.  
  56. --Press any key to stop
  57. event.listen("key_down", shutDown)
  58.  
  59. --Function to get current tick since startup
  60. function tick()
  61.   local t = (computer.uptime() * 20)
  62.   return t
  63. end
  64.  
  65. --Function to set ECU state and update current state
  66. function ecuCon(proxy, state)
  67.   proxy.setECU(state)
  68.   ecu[proxy].curState = state  
  69. end
  70.  
  71. --Returns I/O rate of RF to/from RF Battery
  72. function updateBat()
  73.   bat.curTick = tick()
  74.   --For more accuracy we take a reading every 5 seconds
  75.   if (bat.curTick - bat.lastTick) >= 100 then
  76.     bat.lastRF = bat.curRF
  77.     bat.curRF = bat.proxy.getFullStoredEnergy()
  78.     bat.io = math.floor((bat.curRF - bat.lastRF) / (bat.curTick - bat.lastTick))
  79.     bat.lastTick = bat.curTick
  80.   end
  81.   return bat.io
  82. end
  83.  
  84.  
  85. for proxy, _ in pairs(ecu) do
  86.   for state,output in pairs(ecu[proxy].stateOpt) do
  87.     print("Output: " .. output)  
  88.   end  
  89. end
  90.  
  91.  
  92. for proxy, _ in pairs(ecu) do
  93.   print(ecu[proxy].curState)
  94. end
  95.  
  96. --[[
  97. while running do
  98.   term.clear()
  99.  
  100.   local deltaRF = updateBat()
  101.   print("Stored RF:  " .. bat.curRF)
  102.   print("Delta RF:  " .. deltaRF)
  103.   for proxy, _ in pairs(ecu) do
  104.     ecuCon(proxy, 4)
  105.   end    
  106.   os.sleep(0.1)
  107. end
  108. ]]--
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement