SHARE
TWEET

br_control.lua

a guest Feb 16th, 2019 92 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local component = require('component')
  2. local event = require('event')
  3. local os = require('os')
  4. local term = require('term')
  5.  
  6. local br = component.br_reactor
  7.  
  8. local MAX_REACTOR_BUFFER = 10000000
  9. local REACTOR_LO_BUFFER_TRIP = 0.1
  10. local REACTOR_HI_BUFFER_TRIP = 0.9
  11.  
  12. local trip_mode = 'automatic'
  13. local control_rod_pos = 0
  14. local should_exit = false
  15.  
  16. function on_key_up(name, kbd_address, char, code, player_name)
  17.   print('on_key_up')
  18.   local new_control_rod_pos
  19.  
  20.   if string.char(char) == 'i' then
  21.     new_control_rod_pos = control_rod_pos + 1
  22.   elseif string.char(char) == 'o' then
  23.     new_control_rod_pos = control_rod_pos - 1
  24.   elseif string.char(char) == 't' then
  25.     if trip_mode == 'automatic' then
  26.       trip_mode = 'override on'
  27.     elseif trip_mode == 'override on' then
  28.       trip_mode = 'override off'
  29.     else
  30.       trip_mode = 'automatic'
  31.     end
  32.   end
  33.  
  34.   if new_control_rod_pos > 100 then
  35.     new_control_rod_pos = 100
  36.   elseif new_control_rod_pos < 0 then
  37.     new_control_rod_pos = 0
  38.   end  
  39.  
  40.   br.setAllControlRodLevels(new_control_rod_pos)
  41.   control_rod_pos = new_control_rod_pos
  42. end
  43.  
  44. function close_on_interrupted(id)
  45.   return function(name)
  46.     event.cancel(id)
  47.     should_exit = true
  48.   end
  49. end
  50.  
  51. local key_up_listener = event.listen('key_up', on_key_up)
  52. event.listen('interrupted', close_on_interrupted(key_up_listener))
  53.  
  54. while br.getConnected() and not should_exit do
  55.   term.clear()
  56.  
  57.   print('trip mode:', trip_mode)
  58.   print('reactor active:', br.getActive())
  59.  
  60.   local energy_stored = br.getEnergyStored()
  61.   print('power buffer:', string.format('%d RF', energy_stored))
  62.  
  63.   print('fuel temp:', string.format('%f oC', br.getFuelTemperature()))
  64.   print('casing temp:', string.format('%f oC', br.getCasingTemperature()))
  65.  
  66.   print('energy output:', string.format('%f RF', br.getEnergyProducedLastTick()))
  67.  
  68.   -- specific burnup (burnup per unit power generated), in millibuckets per 1,000 RF
  69.   local specific_burnup = br.getFuelConsumedLastTick() / br.getEnergyProducedLastTick() * 1000
  70.   local sp_burnup_string
  71.  
  72.   if specific_burnup ~= specific_burnup then
  73.     -- sp burnup is nan; print placeholder
  74.     sp_burnup_string = '--------'
  75.   else
  76.     sp_burnup_string = string.format('%f mB/kRF', specific_burnup)
  77.   end
  78.  
  79.   print('sp. burnup:', sp_burnup_string)
  80.  
  81.   control_rod_pos = br.getControlRodLevel(0)
  82.  
  83.   for rod=0,br.getNumberOfControlRods()-1 do
  84.     print(string.format('rod %d pos:', rod), string.format('%d%%', br.getControlRodLevel(rod)))
  85.   end
  86.  
  87.   print()
  88.   print('t: trip mode')
  89.   print('i: control rods in 1%')
  90.   print('o: control rods out 1%')
  91.  
  92.   if trip_mode == 'override on' then
  93.     br.setActive(true)
  94.   elseif trip_mode == 'override off' then
  95.     br.setActive(false)
  96.   elseif trip_mode == 'automatic' then
  97.     if energy_stored / MAX_REACTOR_BUFFER < REACTOR_LO_BUFFER_TRIP then
  98.       br.setActive(true)
  99.     elseif energy_stored / MAX_REACTOR_BUFFER > REACTOR_HI_BUFFER_TRIP then
  100.       br.setActive(false)
  101.     end
  102.   end
  103.  
  104.   os.sleep(0.5)
  105. end
  106.  
  107. if should_exit then
  108.   print('interrupted; exiting.')
  109. else
  110.   print('reactor disconnected; exiting.')
  111. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top