Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local tempMesurer = 100
- local targetTemp = 300
- local Kp = 0.0050 --0.05
- local insert = 100
- local Ki = 0.000015
- local Kd = 0.12
- reacteur=peripheral.wrap("back")
- local lastError = 0
- local error = 0
- local sommeError = 0
- local first = true
- local KpResult
- local KdResult
- local KiResult
- local commande
- xSize, ySize = term.getSize()
- local paramSizeWidth = 35
- local paramSizeHight = 6
- local textParamSize = 11
- 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 ingotDurationS = 0
- function secondsToClock(seconds)
- if seconds <= 0 then
- return "0";
- else
- hours = string.format("%02.d", math.floor(seconds/3600));
- mins = string.format("%02.d", math.floor(seconds/60 - (tonumber(hours)*60)));
- secs = string.format("%02.d", math.floor(seconds - tonumber(hours)*3600 - tonumber(mins) *60));
- return mins.."min "..secs.."s"
- end
- end
- local function loadSave()
- etat = db.etat
- if etat == nil then etat = 1 end
- targetTemp = db.temp
- if targetTemp == nil then targetTemp = 300 end
- end
- local function save()
- db.etat = etat
- db.temp = targetTemp
- end
- local function drawTerm()
- if onParam == false then
- term.setBackgroundColor(colors.black)
- term.clear()
- term.setCursorPos(1,1)
- term.setTextColor(colors.blue)
- write("Target Temp: ")
- term.setTextColor(colors.green)
- print(targetTemp.."°C")
- term.setTextColor(colors.blue)
- write("Current Temp: ")
- 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(tempMesurer*100)/100).."°C\n")
- term.setTextColor(colors.blue)
- write("Fluid rate: ")
- term.setTextColor(colors.green)
- print(reacteur.getHotFluidProducedLastTick().."mb/t")
- term.setTextColor(colors.blue)
- write("Rod level: ")
- term.setTextColor(colors.green)
- print((math.floor(insert*100)/100).."%")
- term.setTextColor(colors.blue)
- write("Fuel usage: ")
- term.setTextColor(colors.green)
- print((math.floor(reacteur.getFuelConsumedLastTick()*100)/100).."mb/t")
- term.setTextColor(colors.blue)
- write("Ingot duration: ")
- term.setTextColor(colors.green)
- print(secondsToClock(ingotDurationS).."\n")
- term.setTextColor(colors.blue)
- write("Kp Result: ")
- term.setTextColor(colors.green)
- print(math.floor(KpResult*100)/100)
- term.setTextColor(colors.blue)
- write("Ki Result: ")
- term.setTextColor(colors.green)
- print(math.floor(KiResult*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(commande*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(" 300 ")
- if etat == 2 then term.setBackgroundColor(colors.green)
- else term.setBackgroundColor(colors.gray) end
- write(" 1000 ")
- 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("Temperature")
- 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
- -------------------Refresh value -----------------
- tempMesurer = reacteur.getFuelTemperature()
- lastError = error
- error = tempMesurer - targetTemp
- sommeError = sommeError + error
- KpResult = Kp * error
- KdResult = Kd * (error - lastError)
- KiResult = Ki * sommeError
- commande = KpResult + KdResult + KiResult
- insert=insert+commande
- if insert<0 then insert = 0
- elseif insert>100 then insert = 100 end
- reacteur.setAllControlRodLevels(insert)
- ingotDurationS = 1000/(reacteur.getFuelConsumedLastTick()*20)
- -------------------Screen----------------------
- drawTerm()
- os.sleep(0.04)
- end
- end
- local function clickManager(x, y)
- if y == 15 then
- if x>=1 and x<=5 then
- etat = 0
- targetTemp = 0
- edited = true
- save()
- elseif x>=6 and x<=10 then
- etat = 1
- targetTemp = 300
- edited = true
- save()
- elseif x>=11 and x<=16 then
- etat = 2
- targetTemp = 1000
- 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 = "300"
- elseif tonumber(entredSpeed)>1700 then entredSpeed = "1700" end
- targetTemp = 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.waitForAll(loop, keyListener)
Advertisement
Add Comment
Please, Sign In to add comment