SHARE
TWEET

program.lua

a guest Dec 14th, 2019 81 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Wrap peripherals
  2. interface = peripheral.wrap("bottom")
  3. monitor = peripheral.wrap("top")
  4.  
  5. -- Configurable variables
  6. emc_const = 73728
  7. emc_item_name = "projecte:fuel_block"
  8.  
  9. -- Variables used in code
  10. line = 1
  11. cycle = 1
  12.  
  13. filtered_emc = 0
  14. prev_femc = 0
  15. emc_rate = 0
  16.  
  17. -- Clears monitor and resets cursor
  18. function clearMonitor()
  19.     monitor.clear()
  20.     monitor.setCursorPos(1,1)
  21. end
  22.  
  23. -- Writes lines sequentially
  24. function writeLine(arg)
  25.     if arg == "reset" then
  26.         line = 1
  27.     else
  28.         monitor.setCursorPos(1,line)
  29.         monitor.write(arg)
  30.         line = line + 1
  31.     end
  32. end
  33.  
  34. -- Calculates total EMC given item name and EMC value
  35. function calculateEMC(item_name,emc_value)
  36.     local item = interface.findItem(item_name)
  37.     local emc_item_count = item.getMetadata().count
  38.     local emc_total = emc_value * emc_item_count
  39.     return emc_total
  40. end
  41.  
  42. -- Calculates the filtered value of EMC + rate
  43. -- First order IIR filter
  44. function filteredEMC(emc)
  45.     local alpha = 0.01
  46.     local neg_alpha = 1 - alpha
  47.     local cur_val = alpha * emc
  48.     local prev_val = neg_alpha * filtered_emc
  49.     prev_femc = filtered_emc
  50.     filtered_emc = cur_val + prev_val
  51.     emc_rate = filtered_emc - prev_femc
  52. end
  53.  
  54. -- Main program loop
  55. while true do    
  56.     -- Current emc value and rate
  57.     local emc = calculateEMC(emc_item_name,emc_const)
  58.    
  59.     -- Performs filter calculations
  60.     filteredEMC(emc)
  61.            
  62.     -- Writes data to monitor            
  63.     clearMonitor()
  64.     writeLine("EMC available:")
  65.     writeLine(emc)
  66.     writeLine("EMC/cycle:")
  67.     writeLine(emc_rate)
  68.     writeLine("reset")
  69.        
  70.     -- Waits before performing next cycle
  71.     os.sleep(1)
  72. 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