Advertisement
Guest User

powermanager.lua

a guest
Jan 22nd, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.86 KB | None | 0 0
  1. --Load APIs
  2. local component = require("component")
  3. local math = require("math")
  4. local term = require("term")
  5. local event = require("event")
  6.  
  7. --Enter Engine Control Unit address(es) below (ex:  {"address1", "address2"} or {"address3"}
  8. local ecuAddress = {"ba3fb1a8-0804-460d-9d4c-08f0a53339d7", "18d07964-e76c-48c3-83a8-b68d478270e1"}
  9.  
  10. --Enter ONE ME Controller Address below (ex:  {"address1"}
  11. local meAddress = {"bc4cc21f-9a05-406d-8620-ab427b5dbaed"}
  12.  
  13. --Enter the maximum power output of ONE engine in watts
  14. local powMax = 67108864
  15.  
  16. --Enter the number of watts in one AE Energy Unit.  Use network tool
  17. --to guesstimate this number.
  18. wattAE = 1040
  19.  
  20.  
  21.  
  22. --Do not edit below this line
  23. running = true
  24. local numECU = #ecuAddress
  25. local me = component.proxy(meAddress[1])
  26. local ecu = {}
  27. curGen = 0
  28. totalGen = 0
  29. powNeed = 0
  30.  
  31. function shutDown()
  32.   running = false
  33.   term.clear()
  34.   return false
  35. end
  36.  
  37.  
  38. event.listen("key_down", shutDown)
  39.  
  40.  
  41. --Create a table of ECU Proxies
  42. for i = 1, numECU do
  43.   ecu[component.proxy(ecuAddress[i])] = {curState = 0, stateOpt = {powMax / 16, powMax / 4, powMax / 2, powMax}}
  44. end
  45.  
  46. --Sync ECU power setting and saved states
  47. for proxy, _ in pairs(ecu) do
  48.   proxy.setECU(4)
  49.   ecu[proxy].curState = 4
  50. end
  51.  
  52. --Main Loop
  53.  
  54.  
  55.  
  56. while running do
  57.  
  58. term.clear()
  59.  
  60. eUse = math.floor(me.getAvgPowerUsage() * wattAE)
  61. powNeed = math.abs((powNeed - eUse))
  62.  
  63. for proxy, _ in pairs(ecu) do
  64.   if powNeed > 0 then
  65.     if powNeed > powMax then
  66.       proxy.setECU(4)
  67.       ecu[proxy].curState = 4
  68.     else
  69.       for state, output in ipairs(ecu[proxy].stateOpt) do
  70.         if (output - powNeed) > 0 then
  71.           proxy.setECU(state)
  72.           ecu[proxy].curState = state
  73.           break
  74.         end
  75.       end
  76.     end
  77.     powNeed = (powNeed - ecu[proxy].stateOpt[ecu[proxy].curState])
  78.   else
  79.     proxy.setECU(0)
  80.     ecu[proxy].curState = 0
  81.   end
  82.   print(ecu[proxy].curState)
  83. end        
  84.  
  85.  
  86. os.sleep(1)
  87.  
  88.  
  89. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement