Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Super basic reactor and turbine control script
- turbine = peripheral.wrap("BiggerReactors_Turbine_0")
- reactor = peripheral.wrap("BiggerReactors_Reactor_0")
- burnValues = {}
- rfValues = {}
- numAverages = 1000
- -- reactor.fuelTank().burnedLastTick()
- --os.time()
- -- Basically we just want to PID power to 0 by way of control rod insertion
- -- First, let's see what we've got...
- -- Reactor:
- -- ambientTemperature
- -- connected
- -- active
- -- setAllControlRodLevels
- -- battery
- -- fuelTemperature
- -- controlRodCount
- -- setActive
- -- fuelTank
- -- coolantTank
- -- apiVersion
- -- getControlRod
- -- stackTemperature
- -- casingTemperature
- -- Actually, I might want to PID the casingTemperature to a certain level instead
- -- Just below steam generation so I can easily add more
- -- That should be 373.15k
- -- Turbine:
- -- connected
- -- rotor
- -- battery
- -- fluidTank
- -- setActive
- -- setCoilEngaged
- -- apiVersion
- -- active
- -- coilEngaged
- reactor.setActive(true)
- rpmPid = pid(0.01, 0, 0.05)
- function pid(p,i,d)
- return{p=p,i=i,d=d,E=0,D=0,I=0,
- run=function(s,sp,pv)
- local E,D,A
- E = sp-pv
- D = E-s.E
- A = math.abs(D-s.D)
- s.E = E
- s.D = D
- s.I = A<E and s.I +E*s.i or s.I*0.5
- return E*s.p +(A<E and s.I or 0) +D*s.d
- end
- }
- end
- while(true) do
- local burnSum = 0
- local rfSum = 0
- for k,v in ipairs(burnValues) do
- if(k < numAverages) then
- burnValues[k+1] = v
- burnSum = burnSum + v
- rfValues[k+1] = rfValues[k]
- rfSum = rfSum + rfValues[k+1]
- end
- end
- burnValues[0] = reactor.fuelTank().burnedLastTick()
- rfValues[0] = turbine.battery().producedLastTick()
- burnSum = burnSum + burnValues[0]
- rfSum = rfSum + rfValues[0]
- burnAverage = burnSum / numBurn * 20 * 60
- rfAverage = rfSum / numBurn
- print("Average burn/hour: " .. burnAverage)
- print("Average rf/tick: " .. rfAverage)
- -- If we wanna do this right we probably need to normalize
- local maxSteam = turbine.fluidTank().input().maxAmount()
- local steamPercent = turbine.fluidTank().input().amount()/maxSteam
- local deltaSteam = 0.5-steamPercent
- local targetRPM = 1800
- local rpmPidResult = rpmPid:run(targetRPM + targetRPM*deltaSteam, turbine.rotor().RPM())
- print(rpmPidResult)
- local targetTemp = 500
- local steamScalar = 1/1000
- -- We might need to adjust this up for when we need steam
- targetTemp = targetTemp - ((turbine.fluidTank().input().amount()-turbine.fluidTank().input().maxAmount()/2-(1800-turbine.rotor().RPM())) * steamScalar)
- print("Target temp: " .. targetTemp)
- local controlMult = 0.001
- -- Now try to keep the turbine at 1800 rpm
- if turbine.rotor().RPM() > 1800 and turbine.active() then
- turbine.setActive(false)
- elseif turbine.rotor().RPM() < 1800 and not turbine.active() then
- turbine.setActive(true)
- end
- -- And turn on the coils when we need power
- if turbine.battery().stored() == turbine.battery().capacity() and turbine.coilEngaged() then
- turbine.setCoilEngaged(false)
- elseif turbine.battery().stored() < turbine.battery().capacity() and not turbine.coilEngaged() then
- turbine.setCoilEngaged(true)
- end
- if turbine.coilEngaged() and not turbine.active() then
- turbine.setActive(true) -- And go ham if it's on
- end
- local controlRodChange = (reactor.casingTemperature()-targetTemp) * controlMult
- reactor.setAllControlRodLevels(reactor.getControlRod(0).level()+controlRodChange)
- print("Control rod change: " .. controlRodChange)
- print("RFT: " .. turbine.battery().producedLastTick())
- sleep(0.01)
- end
Advertisement
Add Comment
Please, Sign In to add comment