Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local turbine = peripheral.wrap("back")
- local maxFlowRate
- local curentFlowRate
- local rotorSpeed
- local targetSpeed = 900
- local Kp = 5--0.05
- local Kd = 150
- local reacteur=peripheral.wrap("back")
- local lastError = 0
- local error = 0
- local commande
- xSize, ySize = term.getSize()
- local paramSizeWidth = 35
- local paramSizeHight = 6
- local textParamSize = 7
- local editableTextSize = 5
- local entredSpeed = ""
- local onParam = false
- local paramWindow
- local KpResult
- local KdResult
- local PID
- local etat = 1
- local db
- local edited = true
- local function loadSave()
- etat = db.etat
- if etat == nil then etat = 1 end
- targetSpeed = db.speed
- if targetSpeed == nil then targetSpeed = 900 end
- end
- local function save()
- db.etat = etat
- db.speed = targetSpeed
- end
- local function drawTerm()
- if onParam == false then
- term.setBackgroundColor(colors.black)
- term.clear()
- term.setCursorPos(1,1)
- term.setTextColor(colors.blue)
- write("Target Speed: ")
- term.setTextColor(colors.green)
- print(targetSpeed.." RPM")
- term.setTextColor(colors.blue)
- write("Rotor speed: ")
- if math.abs(error) <100 and math.abs(error) >20 then
- term.setTextColor(colors.orange)
- elseif math.abs(error) < 20 then
- term.setTextColor(colors.green)
- else
- term.setTextColor(colors.red)
- end
- print((math.floor(rotorSpeed*100)/100).." RPM\n")
- term.setTextColor(colors.blue)
- write("Commande fluid rate: ")
- term.setTextColor(colors.green)
- print(maxFlowRate.."mb/t")
- term.setTextColor(colors.blue)
- write("Curent fluid rate: ")
- if (maxFlowRate-curentFlowRate)<20 then
- term.setTextColor(colors.green)
- elseif (maxFlowRate-curentFlowRate)<100 then term.setTextColor(colors.orange)
- else term.setTextColor(colors.red) end
- print(curentFlowRate.."mb/t\n")
- term.setTextColor(colors.blue)
- write("Engaged: ")
- if turbine.getInductorEngaged() then
- term.setTextColor(colors.green)
- print("true")
- else
- term.setTextColor(colors.red)
- print("false")
- end
- term.setTextColor(colors.blue)
- write("Production: ")
- term.setTextColor(colors.green)
- print((math.floor(turbine.getEnergyProducedLastTick()/10)/100).."kRF/t\n")
- term.setTextColor(colors.blue)
- write("Kp Result: ")
- term.setTextColor(colors.green)
- print(math.floor(KpResult*100)/100)
- term.setTextColor(colors.blue)
- write("Kd Result: ")
- term.setTextColor(colors.green)
- print(math.floor(KdResult*100)/100)
- term.setTextColor(colors.blue)
- write("Increment: ")
- term.setTextColor(colors.green)
- print(math.floor(PID*100)/100)
- print("")
- print("")
- term.setTextColor(colors.white)
- if etat == 0 then term.setBackgroundColor(colors.green)
- else term.setBackgroundColor(colors.gray) end
- write(" Off ")
- if etat == 1 then term.setBackgroundColor(colors.green)
- else term.setBackgroundColor(colors.gray) end
- write(" 900 ")
- if etat == 2 then term.setBackgroundColor(colors.green)
- else term.setBackgroundColor(colors.gray) end
- write(" 1800 ")
- if etat == 3 then term.setBackgroundColor(colors.green)
- else term.setBackgroundColor(colors.gray) end
- write(" Manu ")
- if etat == 3 then
- term.setBackgroundColor(colors.black)
- write(" ")
- term.setBackgroundColor(colors.lightBlue)
- write(" Edit ")
- end
- term.setBackgroundColor(colors.black)
- end
- end
- local function drawCadre(hight,width,display)
- display.setTextColor(colors.red)
- display.setCursorPos(1,1)
- display.setBackgroundColor(colors.black)
- for i = 1, hight do
- display.setCursorPos(1,i)
- if i == 1 or i == hight then
- display.write("+")
- else
- display.write("|")
- end
- for j = 1, width-2 do
- if i == 1 or i == hight then
- display.write("-")
- else
- display.write(" ")
- end
- end
- if i == 1 or i == hight then
- display.write("+")
- else
- display.write("|")
- end
- end
- display.setTextColor(colors.white)
- end
- local function refresh()
- paramWindow.setBackgroundColor(colors.black)
- paramWindow.setCursorPos(1,1)
- paramWindow.clear()
- drawCadre(paramSizeHight,paramSizeWidth,paramWindow)
- paramWindow.setCursorPos(paramSizeWidth/2-textParamSize/2+textParamSize%2,2)
- paramWindow.setTextColor(colors.yellow)
- paramWindow.write("Vitesse")
- paramWindow.setTextColor(colors.white)
- paramWindow.setCursorPos(paramSizeWidth/2-editableTextSize/2+editableTextSize%2,3)
- paramWindow.setBackgroundColor(colors.gray)
- for i=0, editableTextSize-1 do
- paramWindow.write(" ")
- end
- paramWindow.setCursorPos(paramSizeWidth/2-editableTextSize/2+editableTextSize%2,3)
- paramWindow.setBackgroundColor(colors.gray)
- paramWindow.write(entredSpeed)
- paramWindow.setBackgroundColor(colors.black)
- paramWindow.setCursorPos(paramSizeWidth/2-string.len("Entrer ou Suppr")/2+string.len("Entrer ou Suppr")/2%2,5)
- paramWindow.setTextColor(colors.green)
- paramWindow.write("Entrer")
- paramWindow.setTextColor(colors.yellow)
- paramWindow.write(" ou ")
- paramWindow.setTextColor(colors.red)
- paramWindow.write("Suppr")
- term.setTextColor(colors.white)
- paramWindow.setTextColor(colors.white)
- end
- local function createWindow()
- paramWindow = window.create(term.current(),(xSize/2)-(paramSizeWidth/2), (ySize/2)-(paramSizeHight/2),paramSizeWidth,paramSizeHight,false)
- refresh()
- end
- local function loop()
- shell.run("pastebin get vhAGg4VB vault")
- os.loadAPI("vault")
- vault.setDir("/.save")
- db = vault.open("save.dat")
- loadSave()
- createWindow()
- while true do
- if turbine == nil then
- term.setBackgroundColor(colors.black)
- term.clear()
- term.setCursorPos(1,1)
- term.setTextColor(colors.red)
- print("Turbine is missing!")
- break
- end
- --------Refresh values-------------
- if rotorSpeed == 0 and targetSpeed == 0 then
- turbine.setActive(false)
- else turbine.setActive(true) end
- rotorSpeed = turbine.getRotorSpeed()
- maxFlowRate = turbine.getFluidFlowRateMax()
- curentFlowRate = turbine.getFluidFlowRate()
- lastError = error
- error = targetSpeed - rotorSpeed
- if rotorSpeed < targetSpeed and edited then
- turbine.setInductorEngaged(false)
- edited = false
- end
- if rotorSpeed >= targetSpeed then
- turbine.setInductorEngaged(true)
- edited = false
- end
- if error > 50 and edited == false then edited = true end
- KpResult = Kp * error
- KdResult = Kd * (error - lastError)
- PID = KpResult + KdResult
- commande = curentFlowRate + PID
- if commande<0 then commande = 0
- elseif commande>2000 then commande = 2000 end
- turbine.setFluidFlowRateMax(commande)
- --------Screen---------------------
- drawTerm()
- os.sleep(0.02)
- end
- end
- local function clickManager(x, y)
- if y == 15 then
- if x>=1 and x<=5 then
- etat = 0
- targetSpeed = 0
- edited = true
- save()
- elseif x>=6 and x<=10 then
- etat = 1
- targetSpeed = 900
- edited = true
- save()
- elseif x>=11 and x<=16 then
- etat = 2
- targetSpeed = 1800
- edited = true
- save()
- elseif x>=17 and x<=22 then
- etat = 3
- edited = true
- save()
- elseif etat == 3 and x>=27 and x<=32 then
- entredSpeed = ""
- onParam = true
- paramWindow.setVisible(true)
- edited = true
- save()
- end
- end
- end
- local function keyListener()
- while true do
- local event, param1, param2, param3 = os.pullEvent()
- if event == "char" then
- if string.len(entredSpeed) <4 and onParam then
- if string.byte(param1)<=57 and string.byte(param1)>=48 then
- entredSpeed = entredSpeed..param1
- end
- end
- refresh()
- elseif event == "key" then
- if param1 == 14 and onParam then
- entredSpeed = string.sub(entredSpeed, 1, string.len(entredSpeed)-1)
- refresh()
- elseif param1 == 28 and onParam then
- if string.len(entredSpeed)<1 then entredSpeed = "900"
- elseif tonumber(entredSpeed)>2100 then entredSpeed = "2100" end
- targetSpeed = tonumber(entredSpeed)
- paramWindow.setVisible(false)
- onParam = false
- edited = true
- save()
- elseif param1 == 211 and onParam then
- paramWindow.setVisible(false)
- onParam = false
- edited = true
- save()
- elseif param1 == 18 and onParam == false then
- entredSpeed = ""
- paramWindow.setVisible(true)
- onParam = true
- edited = true
- save()
- end
- elseif event == "mouse_click" then
- clickManager(param2, param3)
- end
- end
- end
- parallel.waitForAny(loop, keyListener)
Advertisement
Add Comment
Please, Sign In to add comment