Dimencia

Untitled

Jan 5th, 2023
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.49 KB | None | 0 0
  1. -- Super basic reactor and turbine control script
  2.  
  3. turbine = peripheral.wrap("BiggerReactors_Turbine_0")
  4. reactor = peripheral.wrap("BiggerReactors_Reactor_0")
  5. burnValues = {}
  6. rfValues = {}
  7. numAverages = 1000
  8.  
  9. -- reactor.fuelTank().burnedLastTick()
  10.  
  11. --os.time()
  12.  
  13. -- Basically we just want to PID power to 0 by way of control rod insertion
  14. -- First, let's see what we've got...
  15. -- Reactor:
  16. -- ambientTemperature
  17. -- connected
  18. -- active
  19. -- setAllControlRodLevels
  20. -- battery
  21. -- fuelTemperature
  22. -- controlRodCount
  23. -- setActive
  24. -- fuelTank
  25. -- coolantTank
  26. -- apiVersion
  27. -- getControlRod
  28. -- stackTemperature
  29. -- casingTemperature
  30.  
  31. -- Actually, I might want to PID the casingTemperature to a certain level instead
  32. -- Just below steam generation so I can easily add more
  33. -- That should be 373.15k
  34.  
  35. -- Turbine:
  36. -- connected
  37. -- rotor
  38. -- battery
  39. -- fluidTank
  40. -- setActive
  41. -- setCoilEngaged
  42. -- apiVersion
  43. -- active
  44. -- coilEngaged
  45.  
  46. reactor.setActive(true)
  47. rpmPid = pid(0.01, 0, 0.05)
  48.  
  49. function pid(p,i,d)
  50. return{p=p,i=i,d=d,E=0,D=0,I=0,
  51. run=function(s,sp,pv)
  52. local E,D,A
  53. E = sp-pv
  54. D = E-s.E
  55. A = math.abs(D-s.D)
  56. s.E = E
  57. s.D = D
  58. s.I = A<E and s.I +E*s.i or s.I*0.5
  59. return E*s.p +(A<E and s.I or 0) +D*s.d
  60. end
  61. }
  62. end
  63.  
  64.  
  65. while(true) do
  66. local burnSum = 0
  67. local rfSum = 0
  68. for k,v in ipairs(burnValues) do
  69. if(k < numAverages) then
  70. burnValues[k+1] = v
  71. burnSum = burnSum + v
  72. rfValues[k+1] = rfValues[k]
  73. rfSum = rfSum + rfValues[k+1]
  74. end
  75. end
  76.  
  77. burnValues[0] = reactor.fuelTank().burnedLastTick()
  78. rfValues[0] = turbine.battery().producedLastTick()
  79. burnSum = burnSum + burnValues[0]
  80. rfSum = rfSum + rfValues[0]
  81. burnAverage = burnSum / numBurn * 20 * 60
  82. rfAverage = rfSum / numBurn
  83. print("Average burn/hour: " .. burnAverage)
  84. print("Average rf/tick: " .. rfAverage)
  85.  
  86. -- If we wanna do this right we probably need to normalize
  87. local maxSteam = turbine.fluidTank().input().maxAmount()
  88. local steamPercent = turbine.fluidTank().input().amount()/maxSteam
  89. local deltaSteam = 0.5-steamPercent
  90.  
  91. local targetRPM = 1800
  92. local rpmPidResult = rpmPid:run(targetRPM + targetRPM*deltaSteam, turbine.rotor().RPM())
  93. print(rpmPidResult)
  94.  
  95.  
  96.  
  97. local targetTemp = 500
  98. local steamScalar = 1/1000
  99. -- We might need to adjust this up for when we need steam
  100. targetTemp = targetTemp - ((turbine.fluidTank().input().amount()-turbine.fluidTank().input().maxAmount()/2-(1800-turbine.rotor().RPM())) * steamScalar)
  101. print("Target temp: " .. targetTemp)
  102.  
  103. local controlMult = 0.001
  104.  
  105.  
  106. -- Now try to keep the turbine at 1800 rpm
  107. if turbine.rotor().RPM() > 1800 and turbine.active() then
  108. turbine.setActive(false)
  109. elseif turbine.rotor().RPM() < 1800 and not turbine.active() then
  110. turbine.setActive(true)
  111. end
  112.  
  113. -- And turn on the coils when we need power
  114. if turbine.battery().stored() == turbine.battery().capacity() and turbine.coilEngaged() then
  115. turbine.setCoilEngaged(false)
  116. elseif turbine.battery().stored() < turbine.battery().capacity() and not turbine.coilEngaged() then
  117. turbine.setCoilEngaged(true)
  118. end
  119.  
  120. if turbine.coilEngaged() and not turbine.active() then
  121. turbine.setActive(true) -- And go ham if it's on
  122. end
  123.  
  124. local controlRodChange = (reactor.casingTemperature()-targetTemp) * controlMult
  125. reactor.setAllControlRodLevels(reactor.getControlRod(0).level()+controlRodChange)
  126. print("Control rod change: " .. controlRodChange)
  127.  
  128. print("RFT: " .. turbine.battery().producedLastTick())
  129.  
  130. sleep(0.01)
  131. end
Advertisement
Add Comment
Please, Sign In to add comment