Advertisement
Guest User

ptm.lua

a guest
Dec 16th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.65 KB | None | 0 0
  1. print("Plasma turbine monitor v.0.4")
  2.  
  3. local event = require("event")
  4. local cmp = require("component")
  5. local sides = require("sides")
  6. local computer = require("computer")
  7.  
  8. -- Config file parameters
  9. local durability = 1536000
  10. local durabilityMin = 1000
  11. local rotorTime = 0
  12. local nominalOut = 28643
  13.  
  14. -- Varibles
  15. local exitPending = false
  16. local eu = 0
  17. local plasma = 0
  18. local rotor_start_time = 0
  19.  
  20. -- Constants
  21. local eu_min = 10000000
  22. local eu_max = 100000000
  23. local plasma_min = 64
  24. local plasma_max = 128
  25. local durabilityPeriod = 20
  26. local sideFluidOut = sides.south -- transposer side to get plasma
  27. local sideEnableFlow = sides.west -- red out to enable main plasma flow
  28. local sideTank = sides.west -- adapter side to tank
  29.  
  30. local slow_timer = -1
  31. local warm_timer = -1
  32. local dur_timer = -1
  33.  
  34. local tank = cmp.tank_controller
  35. local trans = cmp.transposer
  36. local bat = cmp.gt_batterybuffer
  37. local red = cmp.redstone
  38.  
  39. --load cells
  40.  
  41. local function readConfig()
  42.   local f = io.open("ptm.cfg", "r")
  43.   if f then
  44.     line = f:read() if line ~= nil then durability = tonumber(line) end
  45.     line = f:read() if line ~= nil then durabilityMin = tonumber(line) end
  46.     line = f:read() if line ~= nil then rotorTime = tonumber(line) end
  47.     line = f:read() if line ~= nil then nominalOut = tonumber(line) end
  48.     f:close()
  49.   end
  50. end
  51.  
  52. local function saveConfig()
  53.   local f = io.open("ptm.cfg", "w")
  54.   if f then
  55.     f:write(durability.."\n")
  56.     f:write(durabilityMin.."\n")
  57.     f:write(rotorTime.."\n")
  58.     f:write(nominalOut.."\n")
  59.     f:close()
  60.   end
  61. end
  62.  
  63. local function hysteresisOFF(state, value, vmin, vmax)
  64.   local r = state
  65.   if (not state) then
  66.     if (value < vmin) then r = true end
  67.   else
  68.     if (value > vmax) then r = false end
  69.   end
  70.   return r
  71. end
  72.  
  73. local function hysteresisON(state, value, vmin, vmax)
  74.   local r = state
  75.   if (state) then
  76.     if (value < vmin) then r = false end
  77.   else
  78.     if (value > vmax) then r = true end
  79.   end
  80.   return r
  81. end
  82.  
  83. local function getEUStored()
  84.   local eu = bat.getEUStored() + bat.getBatteryCharge(1)
  85.   return eu
  86. end
  87.  
  88. local function getPlasmaStored()
  89.   local sns = tank.getSensorInformation()
  90.   local value = tonumber(string.sub(sns[4],1,-2))
  91.   return value
  92. end
  93.  
  94. -- State machine definitions
  95.  
  96. local stStandBy = 1
  97. local stWarmingUp = 2
  98. local stRun = 3
  99. local stSlowingDown = 4
  100. local stShutDown = 5
  101.  
  102. local m_state = stStandBy
  103. local m_state_pending = stStandBy
  104. local m_entry = {}
  105. local m_in_state = {}
  106. local m_exit = {}
  107.  
  108. local function state2str(value)
  109.   local s = "undef"
  110.   if value == stStandBy then
  111.     s = "stStandBy"
  112.   end
  113.   if value == stWarmingUp then
  114.     s = "stWarmingUp"
  115.   end
  116.   if value == stRun then
  117.     s = "stRun"
  118.   end
  119.   if value == stSlowingDown then
  120.     s = "stSlowingDown"
  121.   end
  122.   if value == stShutDown then
  123.     s = "stShutDown"
  124.   end
  125.   return s.."("..value..")"
  126. end
  127.  
  128. local function assignState(value, on_entry, in_state, on_exit)
  129.   m_entry[value] = on_entry
  130.   m_in_state[value] = in_state
  131.   m_exit[value] = on_exit
  132. end
  133.  
  134. local function switchState()
  135.   if m_exit[m_state] ~= nul then
  136.     print("exit from "..state2str(m_state))
  137.     m_exit[m_state]()
  138.   end
  139.  
  140.   print("assign "..state2str(m_state_pending))
  141.   m_state = m_state_pending
  142.  
  143.   if m_entry[m_state] ~= nul then
  144.     print("enter in "..state2str(m_state))
  145.     m_entry[m_state]()
  146.   end
  147. end
  148.  
  149. local function s_standby()
  150.   if exitPending then
  151.     m_state_pending = stShutDown
  152.   else
  153.     if eu < eu_min and plasma > plasma_min then
  154.       local r = trans.transferFluid(sideFluidOut, sides.up, 1000)
  155.       print("fluid: ", r)
  156.       if r then
  157.          m_state_pending = stWarmingUp
  158.       else
  159.         print("# Cannot load plasma to sping up")
  160.       end
  161.     end
  162.   end
  163. end
  164.  
  165. local function sin_warming()
  166.   warm_timer = event.timer(51, handle_warming_timer, 1)
  167. end
  168.  
  169. function handle_warming_timer()
  170.   warm_timer = -1
  171.   print("handle_warming_timer()")
  172.   m_state_pending = stRun
  173. end
  174.  
  175. local function s_warming()
  176.   if exitPending then
  177.     m_state_pending = stShutDown
  178.   end
  179. end
  180.  
  181. local function sout_warming()
  182.   if warm_timer ~= -1 then
  183.     event.cancel(warm_timer)
  184.   end
  185. end
  186.  
  187. local function sin_run()
  188.   print("Enable main plasma flow ...")
  189.   red.setOutput(sideEnableFlow, 255) -- enable main plasma flow
  190.   dur_timer = event.timer(durabilityPeriod, handle_dur_timer, math.huge)
  191.   rotor_start_time = computer.uptime()
  192. end
  193.  
  194. local function s_run()
  195.   if exitPending or eu > eu_max or plasma < plasma_min or durability <= durabilityMin then
  196.     m_state_pending = stSlowingDown
  197.   end
  198. end
  199.  
  200. function handle_dur_timer()
  201.   durability = durability - durabilityPeriod
  202.   print("Calculate durability ... "..durability)
  203. end
  204.  
  205. local function sin_slowing()
  206.   print("Disable main plasma flow ...")
  207.   red.setOutput(sideEnableFlow, 0) -- disable main plasma flow
  208.   slow_timer = event.timer(60, handle_slowing_timer, 1)
  209. end
  210.  
  211. function handle_slowing_timer()
  212.   print("handle_slowing_timer()")
  213.   if exitPending or durability <= durabilityMin then
  214.     m_state_pending = stShutDown
  215.   else
  216.     m_state_pending = stStandBy
  217.   end
  218. end
  219.  
  220. function sout_slowing()
  221.   rotorTime = rotorTime + (computer.uptime() - rotor_start_time)
  222.   print("Rotor time: "..rotorTime)
  223.   event.cancel(dur_timer)
  224.   dur_timer = -1
  225. end
  226.  
  227. function sin_shutdown()
  228.   if durability <= durabilityMin then
  229.     print("# Replace rotor !!! Durability is too low: "..durability)
  230.   end
  231. end
  232.  
  233. assignState(stStandBy,     nul,          s_standby, nul)
  234. assignState(stWarmingUp,   sin_warming,  s_warming, sout_warming)
  235. assignState(stRun,         sin_run,      s_run,     nul)
  236. assignState(stSlowingDown, sin_slowing,  nul,       sout_slowing)
  237. assignState(stShutDown,    sin_shutdown, nul,       nul)
  238.  
  239. -- Main
  240.  
  241. function handleKeyDown(A, keybAddr, ch, code, pName)
  242.   if (ch == 32) then
  243.     print("Exit pending ...")
  244.     exitPending = true
  245.     return
  246.   end
  247.   --print(A, keybAddr, ch, code, pName)
  248. end
  249.  
  250. local function Main()
  251.   local cnt = 4
  252.   local s = ""
  253.   local t = 0
  254.   local eu_old = 0
  255.   local de = 0
  256.   while m_state ~= stShutDown do
  257.     cnt = cnt + 1
  258.     if cnt > 5 then
  259.       cnt = 0
  260.       eu = getEUStored()
  261.       --plasma = getPlasmaStored()
  262.       plasma = tank.getTankLevel(sideTank)
  263.       if m_in_state[m_state] ~= nul then m_in_state[m_state]() end
  264.      
  265.       t = computer.uptime()
  266.       de = eu - eu_old
  267.       eu_old = eu
  268.  
  269.       s = plasma < plasma_min and "LOW" or ""
  270.       print("t: "..t.."  st: "..m_state.."  eu: "..eu.."  de: "..de.."  pls: "..plasma..s)
  271.     end
  272.     if m_state ~= m_state_pending then switchState() end
  273.  
  274.     os.sleep(2)
  275.     --event.debug_tick()
  276.   end
  277. end
  278.  
  279. red.setOutput(sideEnableFlow, 0)
  280. readConfig()
  281. if durability > durabilityMin then
  282.   event.listen("key_down", handleKeyDown)
  283.   Main()
  284.   event.ignore("key_down", handleKeyDown)
  285.   if dur_timer ~= -1 then
  286.     event.cancel(dur_timer)
  287.   end
  288.   red.setOutput(sideEnableFlow, 0) -- disable main flow
  289.   saveConfig()
  290. else
  291.   sin_repair()
  292. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement