Advertisement
1ng0

Draconic Reactor Control v2 with > 1M Rf/t

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