Advertisement
Putnam

Turbine(s) script for computercraft

Mar 10th, 2015
723
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.28 KB | None | 0 0
  1. -- Big Reactors/Turbine setup
  2. -- Yes, you need to connect the computer to both.
  3. -- Use networking cables.
  4.  
  5. local metaturbine={__index=function(self,key) return self.turbine[key] end}
  6.  
  7. local function findPeripherals() --searches around for reactor so the computer do esn't (AUTOCOMPLETE) have to be placed in a particular way
  8.   local reactor,turbines=nil,{}
  9.   turbines={}
  10.   for k,v in pairs(peripheral.getNames()) do
  11.     if (peripheral.getType(v) == "BigReactors-Reactor") then
  12.       reactor = peripheral.wrap(v)
  13.       print('reactor found!')
  14.     elseif (peripheral.getType(v) == "BigReactors-Turbine") then
  15.       local turbine={turbine=peripheral.wrap(v)}
  16.       turbine.turbineActivities=function(self,tickCount)
  17.           local RPM = self.turbine.getRotorSpeed()
  18.           self.turbineIntegral=self.turbineIntegral or 0
  19.           self.flowRate=self.turbine.getFluidFlowRate()
  20.           self.oldTurbineError=turbine.curTurbineError or 0
  21.           self.curTurbineError = (900-RPM)
  22.           self.turbineIntegral=(self.curTurbineError<100 and self.curTurbineError>-100) and self.turbineIntegral+self.curTurbineError or self.turbineIntegral or 0 --THIS IS SILLY
  23.           self.turbineDerivative = tickCount>1 and self.curTurbineError-self.oldTurbineError or 0
  24.           self.Kp=self.Kp or 0.5*interval
  25.           self.Ki=self.Ki or 0.005*interval
  26.           self.Kd=self.Kd or 0.05*interval
  27.           local deltaFlowRate=(self.Kp*self.curTurbineError)+(self.Ki*self.turbineIntegral)+(self.Kd*self.turbineDerivative)
  28.           deltaFlowRate=deltaFlowRate<0 and math.ceil(deltaFlowRate) or deltaFlowRate>0 and math.floor(deltaFlowRate) or 0
  29.           self.flowRate=math.min(self.flowRate+deltaFlowRate,self.turbine.getFluidFlowRateMaxMax())
  30.           if self.turbine.getFluidFlowRate()<(self.flowRate-100) then
  31.             self.flowRate = self.turbine.getFluidFlowRate()+50
  32.           end
  33.           self.turbine.setFluidFlowRateMax(self.flowRate)
  34.           return self.flowRate
  35.       end
  36.       table.insert(turbines,turbine)
  37.       print('turbine ' .. #turbines .. ' found!')
  38.     end
  39.   end
  40.   turbines.getConnected=function(self)
  41.     for _,turbineTable in pairs(self) do
  42.       if type(turbineTable)=='table' then
  43.         if not turbineTable.turbine.getConnected() then return false end
  44.       end
  45.     end
  46.     return true
  47.   end
  48.   turbines.getEnergyStored=function(self)
  49.     local sum=0
  50.     for _,turbineTable in pairs(self) do
  51.       if type(turbineTable)=='table' then
  52.         sum=sum+turbineTable.turbine.getEnergyStored()
  53.       end
  54.     end
  55.     return sum
  56.   end
  57.   return reactor, turbines
  58. end
  59.  
  60. local reactor,turbines=findPeripherals()
  61.    
  62. local curPower,controlRodLevel,tickCount=0,50,0
  63.  
  64. local turbineIntegral,reactorIntegral=0,0
  65.  
  66. local powerMode = turbines:getEnergyStored()<900000*#turbines
  67.  
  68. local idealMax,idealMin=100,0
  69.  
  70. local searchMode,idealAmount=false,nil
  71.  
  72. local searchConstant,cooldown
  73.  
  74. local function reactorActivities()
  75.   term.clear()
  76.   curPower=turbines:getEnergyStored()
  77.   if curPower>900000*#turbines then
  78.     powerMode=false
  79.   elseif curPower<100000*#turbines then
  80.     powerMode=true
  81.   end
  82.   if powerMode then
  83.       reactor.setActive(true)
  84.       local flowRate=0
  85.       for _,turbine in ipairs(turbines) do
  86.         if type(turbine)=='table' then
  87.           flowRate=flowRate+turbine:turbineActivities(tickCount)
  88.         end
  89.       end
  90.       --Begin reactor stuff
  91.       local hotFluidProduced=reactor.getHotFluidProducedLastTick()
  92.       if searchMode then
  93.         while not searchConstant do
  94.           if not oldTemp then
  95.             curTemp=reactor.getFuelTemperature()
  96.             os.sleep(interval)
  97.             oldTemp=curTemp
  98.           end
  99.           term.setCursorPos(1,1)
  100.           term.write('Determining average delta-T...')
  101.           if not alreadySetForTesting then
  102.             reactor.setAllControlRodLevels(reactor.getControlRodLevel(0)<50 and 100 or 0)
  103.           end
  104.           oldTemp=curTemp
  105.           curTemp=reactor.getFuelTemperature()
  106.           prevAverage=curAverage and curAverage or -400000000
  107.           curAverage=curAverage and (curAverage+math.abs(curTemp-oldTemp))/2 or math.abs(curTemp-oldTemp)
  108.           term.setCursorPos(1,2)
  109.           term.write('Current average: ' .. curAverage)
  110.           term.setCursorPos(1,3)
  111.           if math.abs(prevAverage-curAverage)<5 then
  112.             averageIsStableCount=averageIsStableCount+1
  113.             if averageIsStableCount>4 then
  114.               searchConstant=math.ceil(curAverage/500)
  115.             end
  116.           else
  117.             averageIsStableCount=0
  118.           end
  119.           term.write('Will go when counter hits 5: ' ..averageIsStableCount)
  120.           os.sleep(interval)
  121.         end
  122.         increased=increased or controlRodLevel and controlRodLevel>50
  123.         oldTemp=curTemp or 0
  124.         curTemp=reactor.getFuelTemperature()
  125.         deltaTemp=curTemp-oldTemp
  126.         local stabilized=math.abs(deltaTemp)<(searchConstant) or (increased and hotFluidProduced<flowRate)
  127.         if stabilized then
  128.           if hotFluidProduced<flowRate then
  129.             if idealAmount then
  130.               if cooldown==0 then
  131.                 idealAmount=idealAmount-1
  132.                 cooldown=math.floor((5/interval)+.5)
  133.               end
  134.             end
  135.             idealMax=controlRodLevel-1
  136.             increased=false
  137.           else
  138.             idealMin=controlRodLevel
  139.             increased=true
  140.           end
  141.           idealAmount=idealAmount or idealMax<=idealMin and idealMin+10
  142.           controlRodLevel=idealAmount and idealAmount or math.ceil((idealMax+idealMin)/2)
  143.         end
  144.         term.setCursorPos(1,2)
  145.         term.write('Control rod level set to '..controlRodLevel..'.')
  146.         term.setCursorPos(1,1)
  147.         if idealAmount then
  148.           term.write('Ideal amount set at ' .. idealAmount .. '.')
  149.           increased=true
  150.         else
  151.           term.write('Search mode! Ideal amount is somewhere...')
  152.           term.setCursorPos(1,4)
  153.           term.write('Max is ' .. idealMax)
  154.           term.setCursorPos(1,3)
  155.           term.write('Min is ' .. idealMin)
  156.         end
  157.       else
  158.         oldReactorError=curReactorError or 0
  159.         curReactorError=hotFluidProduced-flowRate
  160.         reactorIntegral=reactorIntegral+curReactorError
  161.         local reactorDerivative=tickCount>1 and curReactorError-oldReactorError or 0
  162.         RKp=RKp or 0.1
  163.         RKi=RKi or 0.0005*interval
  164.         RKd=RKd or 0.001
  165.         local deltaControlRodLevel=(RKp*curReactorError)+(RKi*reactorIntegral)+(RKd*reactorDerivative)
  166.         deltaControlRodLevel = deltaControlRodLevel<0 and math.ceil(deltaControlRodLevel) or deltaControlRodLevel>0 and math.floor(deltaControlRodLevel) or 0
  167.         controlRodLevel=math.min(100,math.max(0,controlRodLevel+deltaControlRodLevel))
  168.         term.setCursorPos(1,1)
  169.         term.write('Working! Been working for ' .. tickCount .. ' intervals.')
  170.         tickCount=tickCount+1
  171.         if curReactorError==0 then
  172.           searchMode=true
  173.           idealMin=math.floor((reactor.getControlRodLevel(0)/2)+.5)
  174.         end
  175.       end
  176.       cooldown=cooldown and (cooldown>0 and cooldown-1) or 0
  177.       reactor.setAllControlRodLevels(controlRodLevel)
  178.   else
  179.     reactor.setActive(false)
  180.    
  181.   end
  182. end
  183.  
  184. interval=0.5
  185.  
  186. while reactor.getConnected() do
  187.   os.sleep(interval)
  188.   reactorActivities()
  189. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement