Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Big Reactors/Turbine setup
- -- Yes, you need to connect the computer to both.
- -- Use networking cables.
- local metaturbine={__index=function(self,key) return self.turbine[key] end}
- local function findPeripherals() --searches around for reactor so the computer do esn't (AUTOCOMPLETE) have to be placed in a particular way
- local reactor,turbines=nil,{}
- turbines={}
- for k,v in pairs(peripheral.getNames()) do
- if (peripheral.getType(v) == "BigReactors-Reactor") then
- reactor = peripheral.wrap(v)
- print('reactor found!')
- elseif (peripheral.getType(v) == "BigReactors-Turbine") then
- local turbine={turbine=peripheral.wrap(v)}
- turbine.turbineActivities=function(self,tickCount)
- local RPM = self.turbine.getRotorSpeed()
- self.turbineIntegral=self.turbineIntegral or 0
- self.flowRate=self.turbine.getFluidFlowRate()
- self.oldTurbineError=turbine.curTurbineError or 0
- self.curTurbineError = (900-RPM)
- self.turbineIntegral=(self.curTurbineError<100 and self.curTurbineError>-100) and self.turbineIntegral+self.curTurbineError or self.turbineIntegral or 0 --THIS IS SILLY
- self.turbineDerivative = tickCount>1 and self.curTurbineError-self.oldTurbineError or 0
- self.Kp=self.Kp or 0.5*interval
- self.Ki=self.Ki or 0.005*interval
- self.Kd=self.Kd or 0.05*interval
- local deltaFlowRate=(self.Kp*self.curTurbineError)+(self.Ki*self.turbineIntegral)+(self.Kd*self.turbineDerivative)
- deltaFlowRate=deltaFlowRate<0 and math.ceil(deltaFlowRate) or deltaFlowRate>0 and math.floor(deltaFlowRate) or 0
- self.flowRate=math.min(self.flowRate+deltaFlowRate,self.turbine.getFluidFlowRateMaxMax())
- if self.turbine.getFluidFlowRate()<(self.flowRate-100) then
- self.flowRate = self.turbine.getFluidFlowRate()+50
- end
- self.turbine.setFluidFlowRateMax(self.flowRate)
- return self.flowRate
- end
- table.insert(turbines,turbine)
- print('turbine ' .. #turbines .. ' found!')
- end
- end
- turbines.getConnected=function(self)
- for _,turbineTable in pairs(self) do
- if type(turbineTable)=='table' then
- if not turbineTable.turbine.getConnected() then return false end
- end
- end
- return true
- end
- turbines.getEnergyStored=function(self)
- local sum=0
- for _,turbineTable in pairs(self) do
- if type(turbineTable)=='table' then
- sum=sum+turbineTable.turbine.getEnergyStored()
- end
- end
- return sum
- end
- return reactor, turbines
- end
- local reactor,turbines=findPeripherals()
- local curPower,controlRodLevel,tickCount=0,50,0
- local turbineIntegral,reactorIntegral=0,0
- local powerMode = turbines:getEnergyStored()<900000*#turbines
- local idealMax,idealMin=100,0
- local searchMode,idealAmount=false,nil
- local searchConstant,cooldown
- local function reactorActivities()
- term.clear()
- curPower=turbines:getEnergyStored()
- if curPower>900000*#turbines then
- powerMode=false
- elseif curPower<100000*#turbines then
- powerMode=true
- end
- if powerMode then
- reactor.setActive(true)
- local flowRate=0
- for _,turbine in ipairs(turbines) do
- if type(turbine)=='table' then
- flowRate=flowRate+turbine:turbineActivities(tickCount)
- end
- end
- --Begin reactor stuff
- local hotFluidProduced=reactor.getHotFluidProducedLastTick()
- if searchMode then
- while not searchConstant do
- if not oldTemp then
- curTemp=reactor.getFuelTemperature()
- os.sleep(interval)
- oldTemp=curTemp
- end
- term.setCursorPos(1,1)
- term.write('Determining average delta-T...')
- if not alreadySetForTesting then
- reactor.setAllControlRodLevels(reactor.getControlRodLevel(0)<50 and 100 or 0)
- end
- oldTemp=curTemp
- curTemp=reactor.getFuelTemperature()
- prevAverage=curAverage and curAverage or -400000000
- curAverage=curAverage and (curAverage+math.abs(curTemp-oldTemp))/2 or math.abs(curTemp-oldTemp)
- term.setCursorPos(1,2)
- term.write('Current average: ' .. curAverage)
- term.setCursorPos(1,3)
- if math.abs(prevAverage-curAverage)<5 then
- averageIsStableCount=averageIsStableCount+1
- if averageIsStableCount>4 then
- searchConstant=math.ceil(curAverage/500)
- end
- else
- averageIsStableCount=0
- end
- term.write('Will go when counter hits 5: ' ..averageIsStableCount)
- os.sleep(interval)
- end
- increased=increased or controlRodLevel and controlRodLevel>50
- oldTemp=curTemp or 0
- curTemp=reactor.getFuelTemperature()
- deltaTemp=curTemp-oldTemp
- local stabilized=math.abs(deltaTemp)<(searchConstant) or (increased and hotFluidProduced<flowRate)
- if stabilized then
- if hotFluidProduced<flowRate then
- if idealAmount then
- if cooldown==0 then
- idealAmount=idealAmount-1
- cooldown=math.floor((5/interval)+.5)
- end
- end
- idealMax=controlRodLevel-1
- increased=false
- else
- idealMin=controlRodLevel
- increased=true
- end
- idealAmount=idealAmount or idealMax<=idealMin and idealMin+10
- controlRodLevel=idealAmount and idealAmount or math.ceil((idealMax+idealMin)/2)
- end
- term.setCursorPos(1,2)
- term.write('Control rod level set to '..controlRodLevel..'.')
- term.setCursorPos(1,1)
- if idealAmount then
- term.write('Ideal amount set at ' .. idealAmount .. '.')
- increased=true
- else
- term.write('Search mode! Ideal amount is somewhere...')
- term.setCursorPos(1,4)
- term.write('Max is ' .. idealMax)
- term.setCursorPos(1,3)
- term.write('Min is ' .. idealMin)
- end
- else
- oldReactorError=curReactorError or 0
- curReactorError=hotFluidProduced-flowRate
- reactorIntegral=reactorIntegral+curReactorError
- local reactorDerivative=tickCount>1 and curReactorError-oldReactorError or 0
- RKp=RKp or 0.1
- RKi=RKi or 0.0005*interval
- RKd=RKd or 0.001
- local deltaControlRodLevel=(RKp*curReactorError)+(RKi*reactorIntegral)+(RKd*reactorDerivative)
- deltaControlRodLevel = deltaControlRodLevel<0 and math.ceil(deltaControlRodLevel) or deltaControlRodLevel>0 and math.floor(deltaControlRodLevel) or 0
- controlRodLevel=math.min(100,math.max(0,controlRodLevel+deltaControlRodLevel))
- term.setCursorPos(1,1)
- term.write('Working! Been working for ' .. tickCount .. ' intervals.')
- tickCount=tickCount+1
- if curReactorError==0 then
- searchMode=true
- idealMin=math.floor((reactor.getControlRodLevel(0)/2)+.5)
- end
- end
- cooldown=cooldown and (cooldown>0 and cooldown-1) or 0
- reactor.setAllControlRodLevels(controlRodLevel)
- else
- reactor.setActive(false)
- end
- end
- interval=0.5
- while reactor.getConnected() do
- os.sleep(interval)
- reactorActivities()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement