Advertisement
kkeith29

turbine_control

Feb 15th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.64 KB | None | 0 0
  1. local component = require('component')
  2. local event     = require('event')
  3. local keyboard  = require('keyboard')
  4. local term      = require('term')
  5. local reactor   = require('reactor')
  6. local turbine   = require('turbine')
  7.  
  8. local STATUS_STARTUP   = 1
  9. local STATUS_ACTIVE    = 2
  10. local STATUS_COOL_DOWN = 3
  11. local STATUS_INACTIVE  = 4
  12.  
  13. local STEAM_FLOW_RATE = 2000
  14. local ROTOR_SPEED = 1800
  15.  
  16. function round( num,idp )
  17.     local mult = 10^(idp or 0)
  18.     return math.floor(num * mult + 0.5) / mult
  19. end
  20.  
  21. local function write_xy( row,col,s,... )
  22.     term.setCursor( col,row )
  23.     term.write( s:format(...) )
  24. end
  25.  
  26. local loop = true
  27. local status = STATUS_INACTIVE
  28. local evt  = {}
  29. local curr_control_rod = 1
  30. local last_steam_produced = 0
  31. local control_rod_step = 0.1
  32. local control_rod_lock = false
  33.  
  34. local r = reactor.new('e74e3a')
  35. local turbines = {}
  36. turbines[1] = turbine.new('49b302')
  37. turbines[2] = turbine.new('b28bd5')
  38.  
  39. local steam_needed = STEAM_FLOW_RATE * #turbines
  40.  
  41. function evt.key_down( _,address,char,code,player )
  42.     if code == keyboard.keys.s then
  43.         if status == STATUS_INACTIVE then
  44.             status = STATUS_STARTUP
  45.         elseif status == STATUS_ACTIVE then
  46.             status = STATUS_COOL_DOWN
  47.         end
  48.     elseif code == keyboard.keys.l then
  49.         control_rod_lock = not control_rod_lock
  50.     elseif code == keyboard.keys.q then
  51.         loop = false
  52.         return
  53.     end
  54. end
  55.  
  56. event.listen( 'key_down',evt.key_down )
  57.  
  58. term.clear()
  59.  
  60. --turn everything off to normalize state
  61. if r:is_running() then
  62.     r:all_rods_level(100)
  63.     r:stop()
  64. end
  65. for t_idx,t in ipairs(turbines) do
  66.     t:disengage_inductor()
  67.     t:stop()
  68. end
  69.  
  70. write_xy(1,1,'Status: Inactive ')
  71.  
  72. local STATUS_STEAM_INCREASE = 1
  73. local STATUS_STEAM_DECREASE = 2
  74. local STATUS_STEAM_STABLE   = 3
  75. local steam_poll = nil
  76. local steam_status = nil
  77. local last_control_rod = 1
  78.  
  79. while loop do
  80.     if status == STATUS_STARTUP then
  81.         write_xy(1,1,'Status: Starting Up')
  82.         os.sleep(1)
  83.         for t_idx,_ in ipairs(turbines) do
  84.             turbines[t_idx]:start()
  85.         end
  86.         r:start()
  87.         status = STATUS_ACTIVE
  88.     elseif status == STATUS_ACTIVE then
  89.         write_xy(1,1,'Status: Active     ')
  90.         --handle steam production
  91.         local steam_produced = r:hot_fluid_produced()
  92.         local rod_level = r:rod_level(1)
  93. --[[
  94.         if steam_produced < ( steam_needed - 1 ) or steam_produced > ( steam_needed + 1 ) then
  95.             local steam_diff = 0
  96.             local increase_rod_level = true
  97.             if steam_produced > last_steam_produced then
  98.                 --steam output increasing
  99.                 steam_diff = steam_produced - last_steam_produced
  100.                 increase_rod_level = true
  101.             elseif steam_produced < last_steam_produced then
  102.                 --steam outout decreasing
  103.                 steam_diff = last_steam_produced - steam_produced
  104.                 increase_rod_level = false
  105.             end
  106.             local control_rod_step = 1
  107.             if steam_diff > 200 then
  108.                 --rising quickly, move control rod more
  109.                 control_rod_step = control_rod_step * 3 --kind of pointless, but left in for more control
  110.             elseif steam_diff >= 100 then
  111.                 control_rod_step = control_rod_step * 1
  112.             elseif control_rod_step >= 25 then
  113.                 control_rod_step = control_rod_step * 0.3
  114.             else
  115.                 control_rod_step = control_rod_step * 0.1
  116.             end
  117.             if increase_rod_level then
  118.                 r:increase_rod_level( control_rod_step )
  119.                 rod_level = rod_level + control_rod_step
  120.             else
  121.                 r:decrease_rod_level( control_rod_step )
  122.                 rod_level = rod_level - control_rod_step
  123.             end
  124.         end
  125. --]]
  126. --[[
  127.         if steam_produced < ( steam_needed - 100 ) then
  128.             r:decrease_rod_level(0.8)
  129.         elseif steam_produced > ( steam_needed + 100 ) then
  130.             r:increase_rod_level(0.8)
  131.         else
  132.             --refine control to individual rods
  133.             if steam_produced < ( steam_needed - 1 ) then
  134.                 local next_control_rod = ( last_control_rod + 1 )
  135.                 if next_control_rod > ( r:rod_count() - 1 ) then
  136.                     next_control_rod = 0
  137.                 end
  138.                 r:decrease_single_rod_level( next_control_rod,control_rod_step )
  139.                 last_control_rod = next_control_rod
  140.             elseif steam_produced > ( steam_needed + 1 ) then
  141.                 local next_control_rod = ( last_control_rod - 1 )
  142.                 if next_control_rod < 0 then
  143.                     next_control_rod = ( r:rod_count() - 1 )
  144.                 end
  145.                 r:increase_single_rod_level( next_control_rod,control_rod_step )
  146.                 last_control_rod = next_control_rod
  147.             end
  148.         end
  149. --]]
  150. --[[
  151.         if steam_produced < ( steam_needed - 100 ) then
  152.             r:decrease_rod_level(0.8)
  153.         elseif steam_produced > ( steam_needed + 100 ) then
  154.             r:increase_rod_level(0.8)
  155.         else
  156.             --refine control to individual rods
  157.             if steam_produced < ( steam_needed - 1 ) then
  158.                 local next_control_rod = ( last_control_rod + 1 )
  159.                 if next_control_rod > ( r:rod_count() - 1 ) then
  160.                     next_control_rod = 0
  161.                 end
  162.                 r:decrease_single_rod_level( next_control_rod,control_rod_step )
  163.                 last_control_rod = next_control_rod
  164.                 steam_status = STATUS_STEAM_DECREASE
  165.             elseif steam_produced > ( steam_needed + 1 ) then
  166.                 local next_control_rod = ( last_control_rod - 1 )
  167.                 if next_control_rod < 0 then
  168.                     next_control_rod = ( r:rod_count() - 1 )
  169.                 end
  170.                 r:increase_single_rod_level( next_control_rod,control_rod_step )
  171.                 last_control_rod = next_control_rod
  172.                 steam_status = STATUS_STEAM_INCREASE
  173.             else
  174.                 steam_status = STATUS_STEAM_STABLE
  175.             end
  176.         end
  177. --]]
  178.         if not control_rod_lock then
  179.             if steam_poll == nil or steam_poll == 20 then
  180.                 if steam_produced > ( steam_needed + 10 ) then --if producing too much steam, lower rods quickly to prevent build up
  181.                     r:increase_rod_level(1)
  182.                 elseif steam_produced < ( steam_needed - 100 ) then --if producing too little steam, raise rods fairly quickly to build output
  183.                     r:decrease_rod_level(0.8)
  184.                 elseif steam_produced < ( steam_needed - 1 ) then --if producing under 100mb from target output, slow rod rise speed to prevent overshooting
  185.                     local steam_diff = steam_needed - steam_produced
  186.                     local rod_step = 0.2
  187.                     if steam_diff <= 10 then
  188.                         rod_step = 0.05
  189.                     end
  190.                     local rod_idx = r:highest_rod()
  191.                     r:decrease_single_rod_level( rod_idx,rod_step )
  192.                 elseif steam_produced > ( steam_needed + 1 ) and steam_produced <= ( steam_needed + 10 ) then --if producing just over target, lower rods slowly to match goal
  193.                     local steam_diff = steam_produced - steam_needed
  194.                     local rod_step = 0.1
  195.                     local rod_idx = r:lowest_rod()
  196.                     r:increase_single_rod_level( rod_idx,rod_step )
  197.                 end
  198.                 steam_poll = 0
  199.             else
  200.                 steam_poll = steam_poll + 1
  201.             end
  202.         end
  203. --[[
  204.         if steam_produced > ( steam_needed + 10 ) then
  205.             r:increase_rod_level(0.7)
  206.         elseif steam_produced < ( steam_needed - 100 ) then
  207.             r:decrease_rod_level(0.3)
  208.         elseif steam_produced < ( steam_needed - 1 ) then
  209.             local steam_diff = steam_needed - steam_produced
  210.             local rod_step = 0.1
  211. --[[
  212.             if steam_diff <= 100 then
  213.                 rod_step = 0.1
  214.             end
  215. --]]--[[
  216.             r:decrease_single_rod_level( curr_control_rod,rod_step )
  217.             curr_control_rod = curr_control_rod + 1
  218.             if curr_control_rod > r:rod_count() then
  219.                 curr_control_rod = 1
  220.             end
  221.         elseif steam_produced > steam_needed and steam_produced <= ( steam_needed + 10 ) then
  222.             local steam_diff = steam_produced - steam_needed
  223.             local rod_step = 0.1
  224. --[[
  225.             if steam_diff <= 100 then
  226.                 rod_step = 0.1
  227.             end
  228. --]]--[[
  229.             r:increase_single_rod_level( curr_control_rod,rod_step )
  230.             curr_control_rod = curr_control_rod - 1
  231.             if curr_control_rod < 1 then
  232.                 curr_control_rod = r:rod_count()
  233.             end
  234.         end
  235. --]]
  236. --[[
  237.             --refine control to individual rods
  238.             if steam_produced < ( steam_needed - 1 ) then
  239.                 local next_control_rod = ( last_control_rod + 1 )
  240.                 if next_control_rod > ( r:rod_count() - 1 ) then
  241.                     next_control_rod = 0
  242.                 end
  243.                 local steam_diff = steam_needed - steam_produced
  244.                 local rod_step = 0.3
  245.                 if steam_diff <= 10 then
  246.                     rod_step = 0.1
  247.                 end
  248.                 r:decrease_single_rod_level( next_control_rod,rod_step )
  249.                 last_control_rod = next_control_rod
  250.             elseif steam_produced > ( steam_needed + 1 ) then
  251.                 local next_control_rod = ( last_control_rod - 1 )
  252.                 if next_control_rod < 0 then
  253.                     next_control_rod = ( r:rod_count() - 1 )
  254.                 end
  255.                 local steam_diff = steam_produced - steam_needed
  256.                 local rod_step = 0.3
  257.                 if steam_diff <= 10 then
  258.                     rod_step = 0.1
  259.                 end
  260.                 r:increase_single_rod_level( next_control_rod,rod_step )
  261.                 last_control_rod = next_control_rod
  262.             end
  263.         end
  264. --]]
  265.         last_steam_produced = steam_produced
  266.         --local rod_levels = ''
  267.         --for i=1,#r.rod_levels do
  268.         --    rod_levels = rod_levels .. '|' .. round(r.rod_levels[i],2)
  269.         --end
  270.         --write_xy(8,1,rod_levels .. '                   ')
  271.         local rod_line = 'Rod Level: %g%%'
  272.         if control_rod_lock then
  273.             rod_line = rod_line .. ' [locked]'
  274.         else
  275.             rod_line = rod_line .. '         '
  276.         end
  277.         write_xy(2,1,rod_line,rod_level);
  278.         write_xy(3,1,'Fluid Produced: %g mb          ',steam_produced)
  279.         local line_adjust = 0
  280.         for t_idx,_ in ipairs(turbines) do
  281.             local inductor_engaged = turbines[t_idx]:is_inductor_engaged()
  282.             local rotor_speed = turbines[t_idx]:rotor_speed()
  283.             if not inductor_engaged and rotor_speed >= ( ROTOR_SPEED + 5 ) then
  284.                 turbines[t_idx]:engage_inductor()
  285.             elseif inductor_engaged and rotor_speed <= ( ROTOR_SPEED - 5 ) then
  286.                 turbines[t_idx]:disengage_inductor()
  287.             end
  288.             write_xy(4 + line_adjust,1,'Turbine #' .. t_idx)
  289.             write_xy(5 + line_adjust,5,'Rotor Speed: %g RPM      ',rotor_speed)
  290.             write_xy(6 + line_adjust,5,'      Power: %g RF         ',turbines[t_idx]:energy_produced())
  291.             line_adjust = line_adjust + 4
  292.         end
  293.     elseif status == STATUS_COOL_DOWN then
  294.         term.clear()
  295.         write_xy(1,1,'Status: Cool Down  ')
  296.         r:stop()
  297.         r:all_rods_level(100)
  298.         local fluid_amount = r:hot_fluid_amount()
  299.         if fluid_amount == 0 then
  300.             for t_idx,_ in ipairs(turbines) do
  301.                 turbines[t_idx]:stop()
  302.             end
  303.             status = STATUS_INACTIVE
  304.         end
  305.     elseif status == STATUS_INACTIVE then
  306.         write_xy(1,1,'Status: Inactive ')
  307.     end
  308. --[[
  309.     local curr_energy = r1.getEnergyStored()
  310.     local curr_percentage = ( ( curr_energy / 10000000 ) * 100 )
  311.     write_xy( 2,1,'Energy %%: %g',curr_percentage )
  312.     if last_energy ~= nil then
  313.         if curr_energy < last_energy then
  314.             write_xy( 3,1,'   Usage: -%g RF      ',( last_energy - curr_energy ) )
  315.         elseif curr_energy > last_energy then
  316.             write_xy( 3,1,'   Usage: +%g RF      ',( curr_energy - last_energy ) )
  317.         end
  318.     end
  319.     last_energy = curr_energy
  320.     if curr_percentage <= 15 then
  321.         r1.setActive(true)
  322.         write_xy(1,1,'  Status: On ')
  323.     elseif curr_percentage >= 90 then
  324.         r1.setActive(false)
  325.         write_xy(1,1,'  Status: Off')
  326.     end
  327. --]]
  328.     --r:save_state('last')
  329.     os.sleep(0)
  330. end
  331.  
  332. term.clear()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement