Advertisement
gpochy

dracrea

Jun 27th, 2016
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.83 KB | None | 0 0
  1. wantedField=50000000                    -- Force the field value to this approximate value
  2.  
  3. FluxMODEMVALUE = 0
  4. ValueChange = 10000 -- Change on the Flux Gate over time (Not exact, temperature and saturation divide this)
  5. MinEnergySat = 1000000 -- Minimum energy saturation level
  6. WantedRFt = 1200000
  7. totalFuel = 10368   -- Fully upgraded reactor
  8.  
  9. CapacitorNum = 5                            -- 5 Capacitors on top of computer
  10. CapacitorVal = 1000000                  -- Power holding capacity of the capacitors
  11.  
  12.  
  13. FIRavg = {}     -- last FIR to go     [Flux Input Rate]
  14. FIRLen = 20     -- length of averages
  15. FIRi = 0        -- location to do running average
  16. FIRVal = 200000 -- initial value for FIR
  17. FIRChg = false  -- has an up-down happened?
  18. FIRLow = 0
  19. FIRHigh = 0
  20. FIRLastDelta = 999999
  21.  
  22. for i=0, FIRLen-1 do
  23.     FIRavg[i] = FIRVal
  24. end
  25.  
  26. -----------
  27. function init()
  28.     inflowFluxGate = peripheral.wrap("bottom")
  29.     if inflowFluxGate ~= nil then
  30.         inflowFluxGate.open(1)
  31.         print("|# Input fluxgate:      "..inflowFluxGate.callRemote("flux_gate_"..FluxMODEMVALUE,"getSignalLowFlow"))
  32.     end
  33.  
  34.  
  35.    
  36.     maxFieldCharge = totalFuel * 96.45061728395062 * 100
  37.     maxEnergySaturation = (totalFuel * 96.45061728395062 * 1000)
  38.  
  39.     -- Capacitor on top
  40.     CapacitorMax = CapacitorVal*CapacitorNum    -- Power holding capacity of the capacitors
  41.  
  42.  
  43.  
  44.     capacitor_1 = peripheral.wrap("top")
  45.     tbl = peripheral.call("back","getReactorInfo")
  46.     FluxGateStatus = peripheral.call("right", "getSignalLowFlow")
  47.  
  48.     for k, v in pairs (tbl) do
  49.         --print(k,v)
  50.         if k == "temperature" then
  51.             temperatur = v
  52.         end
  53.         if k == "energySaturation" then
  54.             energysat = v
  55.         end
  56.         if k == "fieldStrength" then
  57.             feldst = v
  58.         end
  59.         if k == "generationRate" then
  60.             RFpt = v
  61.         end
  62.         if k == "fuelConversion" then
  63.             RestKraftstoff = v
  64.         end
  65.         if k == "fuelConversionRate" then
  66.             Kraftstoffverbrauch = v
  67.         end
  68.     end
  69.     energieladung = energysat * 0.0000001
  70.     fieldstr = feldst * 0.000001
  71.     kRFpt = RFpt / 1000
  72.     fuel = RestKraftstoff / 10368
  73.     FuelConv = fuel * 100
  74.     --------------------------------------------------
  75.     print("|# Teplota reaktoru   : "..math.ceil(temperatur))
  76.     print("|# Energy saturation %: "..math.ceil(energieladung))
  77.     print("|# Field strength %:    "..math.ceil(fieldstr))
  78.     print("|# Refuel level %:      "..math.ceil(FuelConv))
  79.     print("|# Energyproduction:    "..math.ceil(kRFpt).." kRF/t")
  80.     -------------------------------------------------
  81. end
  82.  
  83. function doFIRavg()
  84.     local val=0
  85.     for i=0, FIRLen-1 do
  86.         val = val+FIRavg[i]
  87.     end
  88.     return val/FIRLen
  89. end
  90.  
  91. function calcFieldDrain()
  92.         if FIRi >= FIRLen then
  93.             FIRi = 0
  94.         else
  95.             FIRi = FIRi+1
  96.         end
  97.        
  98.         FIRavg[FIRi]=feldst
  99.         local avg = doFIRavg()
  100.         print(avg.." "..wantedField)
  101.         local difference = math.abs(avg-wantedField)
  102.        
  103.         --local valuedelta = math.pow(avg-wantedField, 2)/(300*avg)
  104.        
  105.         local height = 40000
  106.        
  107.         local Offset = 78540000
  108.         local valuedelta = math.sin((avg-(wantedField+Offset))/(50000000))*height+height
  109.         if(valuedelta < 0) then
  110.             valuedelta = 1
  111.             print("Neg at: "..avg)
  112.         end
  113.        
  114.         --print("TTT: "..valuedelta)
  115.        
  116.        
  117.         if (avg > wantedField) then
  118.             if FIRVal-valuedelta < 10000 then
  119.                 FIRVal = 10000
  120.             else
  121.                 FIRVal = FIRVal-valuedelta
  122.             end
  123.             if(FIRChg) then
  124.                 FIRChg = false
  125.                 FIRHigh = FIRVal
  126.                
  127.                 -- Predict middle
  128.                 --FIRVal = (FIRHigh+FIRLow)/2
  129.             end
  130.         else
  131.             if (avg < wantedField) then
  132.                 if FIRVal+valuedelta > (900000-valuedelta) then
  133.                     FIRVal = 900000
  134.                 else
  135.                     FIRVal = FIRVal+valuedelta
  136.                 end
  137.             end
  138.             if(not FIRChg) then
  139.                 FIRChg = true
  140.                 FIRLow = FIRVal
  141.                
  142.                 -- Predict middle
  143.                 --FIRVal = (FIRHigh+FIRLow)/2
  144.             end
  145.            
  146.         end
  147.        
  148.         print("Low: "..FIRLow)
  149.         print("High: "..FIRHigh)
  150.         --print("Delta: "..math.abs(FIRHigh-FIRLow))
  151.        
  152.         fluxval = FIRVal
  153.         print("|#== FluxInputRate:     "..math.floor(fluxval).." kRF/t")
  154. end
  155.    
  156.  
  157. -- Set the shield input level
  158. function setLowLevel()
  159.     if inflowFluxGate ~= nil then
  160.         calcFieldDrain()
  161.         inflowFluxGate.callRemote("flux_gate_"..FluxMODEMVALUE,"setSignalLowFlow",fluxval)
  162.     end
  163. end
  164.  
  165.  
  166. function setLevel(level)
  167.     print("|#== FluxGate set to:   "..math.ceil(FluxGateStatus).." kRF/t")
  168.     peripheral.call("right", "setSignalLowFlow", FluxGateStatus)
  169.    
  170. end
  171.  
  172.  
  173. function limitLevel(FluxStatus)
  174.     -- Set the maximum value
  175.     if FluxGateStatus >= (WantedRFt*0.9-FluxGateStatus*0.3) then
  176.         FluxGateStatus = (WantedRFt*0.9-FluxGateStatus*0.3)
  177.     end
  178. end
  179.  
  180.  
  181. -- Start the reactor
  182. function startReactor()
  183.     peripheral.call("back", "chargeReactor")
  184.         print("|#==        Reactor charge !!        ==#|")
  185.     if feldst >= 50000000 then
  186.         peripheral.call("back", "activateReactor")
  187.             print("|#==        Reactor activate !       ==#|")
  188.     end
  189. end
  190.  
  191.  
  192. function testFieldStrength()
  193.     if feldst <= 15000000 then
  194.         peripheral.call("back", "stopReactor")
  195.         print("|#==     Reactor Emergency stop !    ==#|")
  196.         print("|#==     Field strength too low !     ==#|")
  197.     else
  198.         if feldst >= 20000000 then
  199.             peripheral.call("back", "activateReactor")
  200.             --print("|#==         Reactor active !        ==#|")
  201.         end
  202.     end
  203. end
  204.  
  205.  
  206. function calcNewValueChange()
  207.     local chg = ValueChange
  208.     local saturationchg = (energieladung/100)-0.3
  209.     local reactorTempx = 1-((temperatur-3000)/10000+0.2) -- reactor temperature accounting for +3000 heat
  210.    
  211.     print("|# Temperature Factor:  "..reactorTempx)
  212.     print("|# Saturation Factor:   "..saturationchg)
  213.     chg = chg*math.min(saturationchg,reactorTempx) -- Account for saturation and Temperature
  214.    
  215.     return chg
  216. end
  217.  
  218.  
  219. function testCapacitor()
  220.     -- Slow down the reactor to 1/8 of the requested power level
  221.     -- This is so that a slow-start is not required for later use
  222.     if capacitor_1 ~= nil then
  223.         local energyStored = capacitor_1.getEnergyStored()
  224.         if energyStored >= CapacitorMax then
  225.             FluxGateStatus = WantedRFt/8
  226.             setLevel(FluxGateStatus)
  227.         end
  228.     end
  229. end
  230.  
  231.  
  232. -- Run this program!
  233. function doRun()
  234.     if temperatur > 7777 then -- Temperatur check wenn mehr als 7777 Grad
  235.         FluxGateStatus = 200000
  236.     end
  237.  
  238.  
  239.     -- Set the energy level to be produced
  240.     if energysat > MinEnergySat then
  241.         limitLevel(FluxGateStatus)
  242.         FluxGateStatus = RFpt + calcNewValueChange()
  243.         setLevel(FluxGateStatus)
  244.     end
  245.  
  246.  
  247.  
  248.  
  249.     -- Energy Saturation settings
  250.     --if energysat < MinEnergySat then
  251.     --  if FluxGateStatus <= 10000 then
  252.     --      FluxGateStatus = 10000
  253.     --  end
  254.     --  FluxGateStatus = FluxGateStatus - ValueChange
  255.     --  setLevel(FluxGateStatus)
  256.     --end
  257.  
  258.  
  259.  
  260.     -- Stop the reactor when 90% of the fuel is gone
  261.     if FuelConv >= 90 then
  262.         peripheral.call("back", "stopReactor")
  263.  
  264.         print("|#==      Reactor refuel stop !      ==#|")
  265.         print("|#===========#| DracReact |#===========#|")
  266.         sleep(300)
  267.     end
  268.  
  269.     if temperatur <= 2000 then
  270.         startReactor()
  271.     else
  272.         testFieldStrength()
  273.     end
  274.  
  275.     setLowLevel(level)
  276.     testCapacitor()
  277. end
  278.  
  279. while true do
  280.     term.clear()
  281.     term.setCursorPos(1,1)
  282.     init()
  283.     doRun()
  284.     sleep(0.06)
  285. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement