SHOW:
|
|
- or go back to the newest paste.
1 | -- keeps reactor working efficiently at all times; increases power load when necessary, goes idle at 90% power until below 10% | |
2 | ||
3 | local function findPeripherals() --searches around for reactor so the computer doesn't have to be placed in a particular way | |
4 | local reactor,terminal --also searches for a terminal glasses terminal | |
5 | for k,v in pairs(rs.getSides()) do | |
6 | if (peripheral.getType(v) == "BigReactors-Reactor") then | |
7 | reactor = peripheral.wrap(v) | |
8 | print('reactor found!') | |
9 | break | |
10 | elseif (peripheral.getType(v) == "openperipheral_glassesbridge") and (not terminal) then | |
11 | terminal = peripheral.wrap(v) | |
12 | print('terminal found!') | |
13 | end | |
14 | end | |
15 | return reactor, terminal | |
16 | end | |
17 | ||
18 | ||
19 | local reactor,terminal=findPeripherals() | |
20 | ||
21 | local curPower,oldPower,controlRodLevel,powerMode,secondsWorked=0,0,100,0,0 | |
22 | ||
23 | local integral=0 | |
24 | ||
25 | powerMode = reactor.getEnergyStored()<9000000 | |
26 | ||
27 | local function reactorActivities() | |
28 | term.clear() | |
29 | secondsWorked=secondsWorked+1 | |
30 | print('Yes, I am working. Total time: ' .. secondsWorked .. ' steps.') | |
31 | oldPower=curPower | |
32 | curPower=reactor.getEnergyStored() | |
33 | if oldPower==0 then | |
34 | oldPower=curPower+(target*interval) | |
35 | end | |
36 | if curPower>9000000 then | |
37 | powerMode=false | |
38 | elseif curPower<1000000 then | |
39 | powerMode=true | |
40 | end | |
41 | if powerMode then | |
42 | oldDeltaPower=deltaPower or 0 | |
43 | deltaPower = curPower-oldPower | |
44 | local curError = deltaPower-(target*interval) | |
45 | integral=integral+curError | |
46 | local derivative=(deltaPower-oldDeltaPower)/interval | |
47 | print('Error: ' .. curError) | |
48 | - | local deltaControlRodLevel=(Kp*curError)+(Ki*integral) |
48 | + | |
49 | - | if deltaControlRodLevel>50 or deltaControlRodLevel<-50 then |
49 | + | print('Derivative: ' .. derivative) |
50 | - | if curError<target*10 and curError>-target*10 then |
50 | + | local deltaControlRodLevel=math.floor((Kp*curError)+(Ki*integral)+(Kd*derivative)) |
51 | - | Kp=Kp/2 |
51 | + | |
52 | - | end |
52 | + | |
53 | - | if integral>100000 or integral<100000 then |
53 | + | |
54 | - | integral=0 |
54 | + | |
55 | - | end |
55 | + | |
56 | - | end |
56 | + | |
57 | - | if deltaControlRodLevel>-1 and deltaControlRodLevel<1 then |
57 | + | |
58 | - | if curError>target*10 then |
58 | + | |
59 | - | Kp=Kp*1.5 |
59 | + | |
60 | - | end |
60 | + | |
61 | - | end |
61 | + | |
62 | - | deltaControlRodLevel=deltaControlRodLevel<0 and math.ceil(deltaControlRodLevel) or deltaControlRodLevel>0 and math.floor(deltaControlRodLevel) or 0 |
62 | + | |
63 | else | |
64 | reactor.setAllControlRodLevels(100) | |
65 | print("Reactor's nearly full.") | |
66 | print("Current status: off.") | |
67 | end | |
68 | end | |
69 | ||
70 | local function terminalActivities() | |
71 | terminal.clear() | |
72 | local wastePercent=reactor.getWasteAmount()/reactor.getFuelAmountMax() | |
73 | local fuelPercent=(reactor.getWasteAmount()+reactor.getFuelAmount())/reactor.getFuelAmountMax() | |
74 | terminal.addBox(5,5,100,10,0xcc0000,0.8) | |
75 | terminal.addBox(5,5,math.floor(curPower/100000),10,0x00cc00,0.8) | |
76 | terminal.addBox(105,5,10,100,0xcccccc,0.8) | |
77 | terminal.addBox(105,5,10,controlRodLevel,0x303030,0.8) | |
78 | terminal.addBox(5,20,100,10,0x303030,0.8) | |
79 | terminal.addBox(5,20,math.min(math.floor(100*fuelPercent),100),10,0xcccc00,0.8) | |
80 | terminal.addBox(5,20,math.min(math.floor(100*wastePercent),100),10,0x00cccc,0.8) | |
81 | end | |
82 | ||
83 | ||
84 | args = {...} | |
85 | ||
86 | interval = args[1] or 2 | |
87 | target = args[2] or 100 | |
88 | target = tonumber(target) | |
89 | Kp=args[3] or 0.001*(100/target) | |
90 | Ki=args[4] or 0.0001*(100/target) | |
91 | Kd=args[5] or 0.00001*(100/target) | |
92 | ||
93 | interval=tonumber(interval) | |
94 | ||
95 | if interval<0.5 then | |
96 | interval=0.5 | |
97 | end | |
98 | ||
99 | while reactor.getConnected() do | |
100 | - | Kp=0.001 |
100 | + | |
101 | - | Ki=0.0001 |
101 | + | local event = {os.pullEvent("timer")} |
102 | reactorActivities() | |
103 | if terminal then | |
104 | - | target=tonumber(target) |
104 | + | |
105 | end | |
106 | end |