-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathturbinecontrol.lua
436 lines (408 loc) · 10.9 KB
/
turbinecontrol.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
--16
local t = nil
local state = {}
local socket = nil
local active = {
'shutdown',
'stop',
'coast',
'online'
}
local actFlow = nil
local targetFlow = 0
local function indexOf(tbl, val)
local index = {}
for k,v in pairs(tbl) do
index[v] = k
end
return index[val]
end
local options = {
{
name = 'state',
min = 1,
max = #active,
pos = 6
},
{
name = 'rpm',
min = 0,
max = 10000,
pos = 25
},
{
name = 'flow',
min = 0,
max = 2000,
pos = 37
}
}
function init()
if fs.exists('turbineState.tbl') and not fs.exists('var/turbineState.tbl') then
fs.move('turbineState.tbl', 'var/turbineState.tbl')
end
if fs.exists('var/turbineState.tbl') then
stateFile = fs.open('var/turbineState.tbl', 'r')
state = textutils.unserialise(stateFile.readAll())
stateFile.close()
state.cursor = 1
else
state = {
target = {
{
rpm = 0,
flow = 0
}
},
active = 1,
level = 1,
cursor = 1
}
writeState()
end
if state.portSide == nil then
setPortSide()
end
t = peripheral.wrap(state.portSide)
actFlow = t.getFluidFlowRateMax()
if fs.exists('websocket.lua') then
socket = require('websocket');
end
end
function setPortSide()
term.clear()
print('Which side is the port on?')
state.portSide = read()
writeState()
term.clear()
end
function setLevel(l)
state.level = tonumber(l)
if state.target[state.level] == nil then
state.target[state.level] = {
rpm = 0,
flow = 0
}
end
end
function sendMethods()
if socket then
socket.methods({
{
type='dropdown',
key='preset',
name='Preset',
options={1,2,3,4,5,6,7,8,9},
value=state.level,
fn = function(value)
setLevel(value)
writeState()
sendMethods()
end
},
{
type='radio',
key= 'state',
name = 'Power',
options = active,
value = active[state.active],
fn = function(value)
state.active = indexOf(active, value)
writeState()
return value
end
},
{
type ='number',
key ='speed',
name = 'RPM',
min = 0,
max = 10000,
value = state.target[state.level]['rpm'],
fn = function(value)
if value == nil or value < 0 then
value = 0
end
if value > 10000 then
value = 10000
end
state.target[state.level]['rpm'] = value
writeState()
return value
end
},
{
type ='number',
key ='flow',
name = 'Steam',
min = 0,
max = 2000,
value = state.target[state.level]['flow'],
fn = function(value)
if value == nil or value < 0 then
value = 0
end
if value > 2000 then
value = 2000
end
state.target[state.level]['flow'] = value
writeState()
return value
end
}
})
end
end
function sendInfo()
if socket then
local rf = t.getEnergyProducedLastTick()
local flow = t.getFluidFlowRate()
local efficiency = 0
if rf > 0 and flow > 0 then
efficiency = rf / flow
end
socket.info({
{
key = 'RPM',
value = t.getRotorSpeed(),
type = 'number'
},
{
key = 'RF',
value = rf,
type= 'number'
},
{
key = 'Efficiency',
value = efficiency,
type = 'number'
},
{
key = 'Low Flow',
value = flow < actFlow,
type = 'warning'
},
{
key = 'Storing energy',
value = t.getEnergyStored() > 10000,
type = 'warning'
}
})
end
end
function writeState()
stateFile = fs.open('var/turbineState.tbl', 'w')
stateFile.write(textutils.serialise(state))
stateFile.close()
end
function keyListener()
while true do
local event, key = os.pullEvent('key')
if key <= keys.nine and key >= keys.one then
setLevel(key - keys.one + 1)
end
if key == keys.up then
increase()
end
if key == keys.down then
decrease()
end
if key == keys.left then
back()
end
if key == keys.right then
forward()
end
if key == keys.enter and socket then
if not socket.isConnected() then
socket.connect('turbine', true)
else
socket.disconnect()
end
end
writeState()
sendMethods()
display()
end
end
function increase()
local opt = options[state.cursor]
if opt.name == 'state' then
local amt = state.active + 1
if amt <= opt.max then
state.active = amt
end
else
local amt = state.target[state.level][opt.name] + 1
if amt <= opt.max then
state.target[state.level][opt.name] = amt
end
end
end
function decrease()
local opt = options[state.cursor]
if opt.name == 'state' then
local amt = state.active - 1
if amt >= opt.min then
state.active = amt
end
else
local amt = state.target[state.level][opt.name] - 1
if amt >= opt.min then
state.target[state.level][opt.name] = amt
end
end
end
function back()
local crs = state.cursor - 1
if crs < 1 then
crs = table.getn(options)
end
state.cursor = crs
end
function forward()
local crs = state.cursor + 1
if crs > table.getn(options) then
crs = 1
end
state.cursor = crs
end
function displayLevel()
for i = 1,9 do
term.setCursorPos(5*i, 1)
if i == state.level then
term.setTextColor(colors.black)
term.setBackgroundColor(colors.white)
end
term.write(' ')
term.setCursorPos(5*i, 2)
term.write(string.format(' %d ', i))
term.setCursorPos(5*i, 3)
term.write(' ')
term.setTextColor(colors.white)
term.setBackgroundColor(colors.black)
end
end
function displayInfo()
local rf = t.getEnergyProducedLastTick()
local rpm = t.getRotorSpeed()
local flow = t.getFluidFlowRate()
local storage = t.getEnergyStored()
local bladeEfficiency = t.getBladeEfficiency()
local efficiency = 0
if rf > 0 and flow > 0 then
efficiency = rf / flow
end
term.setCursorPos(6, 5)
term.write(string.format("RPM: %1.2f", rpm))
term.setCursorPos(6, 7)
term.write(string.format("RF: %1.2f", rf))
term.setCursorPos(6, 9)
term.write(string.format("Flow: %d", actFlow))
term.setCursorPos(6, 11)
term.write(string.format("RF/mB: %1.2f", efficiency))
term.setCursorPos(6, 13)
term.write(string.format("Target flow: %d", targetFlow))
if storage > 2 * rf then
term.setCursorPos(26, 5)
term.write("WARNING! Storing energy!")
end
term.setCursorPos(26, 7)
if socket then
if socket.isConnected() then
term.write('Websocket ID: '..(socket.id() or ''))
else
term.write('Websocket available')
term.setCursorPos(26, 9)
term.write('Press Enter to connect')
end
end
end
function displayOptions()
for i = 1, table.getn(options) do
local op = options[i]
local value = ""
if i == state.cursor then
term.setTextColor(colors.black)
term.setBackgroundColor(colors.white)
end
term.setCursorPos(op.pos, 16)
if op.name == 'state' then
value = active[state.active]
else
value = string.format("%d", state.target[state.level][op.name])
end
term.write(string.format("%s:", op.name))
term.setTextColor(colors.white)
term.setBackgroundColor(colors.black)
term.write(string.format(" %s", value))
end
end
function display()
term.clear()
displayLevel()
displayInfo()
displayOptions()
sendInfo()
end
function flowCalc(target, targetRPM, rpm)
local maxFlow = math.max(t.getNumberOfBlades() * 25, target)
local rampFactor = 0.025 * t.getNumberOfBlades() + 0.1
return math.max(0, math.min(maxFlow, target + math.floor((targetRPM - rpm) * rampFactor)))
end
function control()
while true do
local rpm = t.getRotorSpeed()
local targetRPM = state.target[state.level].rpm
t.setActive(true)
if state.active == 1 then -- Shutdown mode
targetFlow = 0
t.setInductorEngaged(true)
end
if state.active == 2 then -- Stop mode (no output)
t.setInductorEngaged(false)
targetFlow = flowCalc(0, targetRPM, rpm)
end
if state.active == 3 then -- Coast mode (idle and output)
targetFlow = flowCalc(0, targetRPM, rpm)
if rpm < (targetRPM - 1) then
t.setInductorEngaged(false)
end
if rpm >= targetRPM then
t.setInductorEngaged(true)
end
end
if state.active == 4 then -- Online mode
targetFlow = flowCalc(state.target[state.level].flow, targetRPM, rpm)
if rpm < (targetRPM - 1) then
t.setInductorEngaged(false)
end
if rpm >= targetRPM then
t.setInductorEngaged(true)
end
end
display()
sleep(0.1)
end
end
function flowUpdate()
while true do
if actFlow < targetFlow then
actFlow = actFlow + 1
elseif actFlow > targetFlow then
actFlow = actFlow - 1
end
t.setFluidFlowRateMax(actFlow)
sleep(0.15)
end
end
init()
local loops = {
keyListener,
control,
flowUpdate
}
if socket then
table.insert(loops, socket.runtime)
end
parallel.waitForAll(unpack(loops))