Broken_Fre

Minecraft Colled Reactor (BigReactor) Control

Feb 16th, 2017
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.67 KB | None | 0 0
  1. local tempMesurer = 100
  2. local targetTemp = 300
  3. local Kp = 0.0050 --0.05
  4. local insert = 100
  5. local Ki = 0.000015
  6.  
  7. local Kd = 0.12
  8. reacteur=peripheral.wrap("back")
  9. local lastError = 0
  10. local error = 0
  11. local sommeError = 0
  12. local first = true
  13. local KpResult
  14. local KdResult
  15. local KiResult
  16. local commande
  17.  
  18. xSize, ySize = term.getSize()
  19. local paramSizeWidth = 35
  20. local paramSizeHight = 6
  21. local textParamSize = 11
  22. local editableTextSize = 5
  23. local entredSpeed = ""
  24. local onParam = false
  25. local paramWindow
  26. local KpResult
  27. local KdResult
  28. local PID
  29. local etat = 1
  30. local db
  31. local edited = true
  32. local ingotDurationS = 0
  33.  
  34.  
  35.  
  36. function secondsToClock(seconds)
  37.  
  38.  
  39.   if seconds <= 0 then
  40.     return "0";
  41.   else
  42.     hours = string.format("%02.d", math.floor(seconds/3600));
  43.     mins = string.format("%02.d", math.floor(seconds/60 - (tonumber(hours)*60)));
  44.     secs = string.format("%02.d", math.floor(seconds - tonumber(hours)*3600 - tonumber(mins) *60));
  45.     return mins.."min "..secs.."s"
  46.   end
  47. end
  48.  
  49. local function loadSave()
  50.   etat = db.etat
  51.   if etat == nil then etat = 1 end
  52.   targetTemp = db.temp
  53.   if targetTemp == nil then targetTemp = 300 end
  54.  
  55. end
  56.  
  57. local function save()
  58.   db.etat = etat
  59.   db.temp = targetTemp
  60. end
  61.  
  62.  
  63. local function drawTerm()
  64.   if onParam == false then
  65.     term.setBackgroundColor(colors.black)
  66.     term.clear()
  67.     term.setCursorPos(1,1)
  68.     term.setTextColor(colors.blue)
  69.     write("Target Temp: ")
  70.     term.setTextColor(colors.green)
  71.     print(targetTemp.."°C")
  72.     term.setTextColor(colors.blue)
  73.     write("Current Temp: ")
  74.     if math.abs(error) <100 and math.abs(error) >20 then
  75.       term.setTextColor(colors.orange)
  76.     elseif math.abs(error) < 20 then
  77.       term.setTextColor(colors.green)
  78.     else
  79.       term.setTextColor(colors.red)
  80.     end
  81.     print((math.floor(tempMesurer*100)/100).."°C\n")
  82.     term.setTextColor(colors.blue)
  83.     write("Fluid rate: ")
  84.     term.setTextColor(colors.green)
  85.     print(reacteur.getHotFluidProducedLastTick().."mb/t")
  86.     term.setTextColor(colors.blue)
  87.     write("Rod level: ")
  88.     term.setTextColor(colors.green)
  89.     print((math.floor(insert*100)/100).."%")
  90.     term.setTextColor(colors.blue)
  91.     write("Fuel usage: ")
  92.     term.setTextColor(colors.green)
  93.     print((math.floor(reacteur.getFuelConsumedLastTick()*100)/100).."mb/t")
  94.     term.setTextColor(colors.blue)
  95.     write("Ingot duration: ")
  96.     term.setTextColor(colors.green)
  97.     print(secondsToClock(ingotDurationS).."\n")
  98.    
  99.     term.setTextColor(colors.blue)
  100.     write("Kp Result: ")
  101.     term.setTextColor(colors.green)
  102.     print(math.floor(KpResult*100)/100)
  103.     term.setTextColor(colors.blue)
  104.     write("Ki Result: ")
  105.     term.setTextColor(colors.green)
  106.     print(math.floor(KiResult*100)/100)
  107.     term.setTextColor(colors.blue)
  108.     write("Kd Result: ")
  109.     term.setTextColor(colors.green)
  110.     print(math.floor(KdResult*100)/100)
  111.     term.setTextColor(colors.blue)
  112.     write("Increment: ")
  113.     term.setTextColor(colors.green)
  114.     print(math.floor(commande*100)/100)
  115.     print("")
  116.     print("")
  117.     term.setTextColor(colors.white)
  118.     if etat == 0 then term.setBackgroundColor(colors.green)
  119.     else term.setBackgroundColor(colors.gray) end
  120.     write(" Off ")
  121.     if etat == 1 then term.setBackgroundColor(colors.green)
  122.     else term.setBackgroundColor(colors.gray) end
  123.     write(" 300 ")
  124.     if etat == 2 then term.setBackgroundColor(colors.green)
  125.     else term.setBackgroundColor(colors.gray) end
  126.     write(" 1000 ")
  127.     if etat == 3 then term.setBackgroundColor(colors.green)
  128.     else term.setBackgroundColor(colors.gray) end
  129.     write(" Manu ")
  130.     if etat == 3 then
  131.       term.setBackgroundColor(colors.black)
  132.       write("    ")
  133.       term.setBackgroundColor(colors.lightBlue)
  134.       write(" Edit ")
  135.     end
  136.     term.setBackgroundColor(colors.black)
  137.   end
  138. end
  139.  
  140.  
  141. local function drawCadre(hight,width,display)
  142.   display.setTextColor(colors.red)
  143.   display.setCursorPos(1,1)
  144.   display.setBackgroundColor(colors.black)
  145.   for i = 1, hight do
  146.     display.setCursorPos(1,i)
  147.     if i == 1 or i == hight then
  148.       display.write("+")
  149.     else
  150.       display.write("|")
  151.     end
  152.     for j = 1, width-2 do
  153.       if i == 1 or i == hight then
  154.         display.write("-")
  155.       else
  156.         display.write(" ")
  157.       end
  158.     end
  159.     if i == 1 or i == hight then
  160.       display.write("+")
  161.     else
  162.       display.write("|")
  163.     end
  164.   end
  165.   display.setTextColor(colors.white)
  166. end
  167.  
  168.  
  169.  
  170.  
  171. local function refresh()
  172.   paramWindow.setBackgroundColor(colors.black)
  173.   paramWindow.setCursorPos(1,1)
  174.   paramWindow.clear()
  175.   drawCadre(paramSizeHight,paramSizeWidth,paramWindow)
  176.   paramWindow.setCursorPos(paramSizeWidth/2-textParamSize/2+textParamSize%2,2)
  177.   paramWindow.setTextColor(colors.yellow)
  178.   paramWindow.write("Temperature")
  179.   paramWindow.setTextColor(colors.white)
  180.   paramWindow.setCursorPos(paramSizeWidth/2-editableTextSize/2+editableTextSize%2,3)
  181.   paramWindow.setBackgroundColor(colors.gray)
  182.   for i=0, editableTextSize-1 do
  183.     paramWindow.write(" ")
  184.   end
  185.   paramWindow.setCursorPos(paramSizeWidth/2-editableTextSize/2+editableTextSize%2,3)
  186.   paramWindow.setBackgroundColor(colors.gray)
  187.   paramWindow.write(entredSpeed)
  188.   paramWindow.setBackgroundColor(colors.black)
  189.   paramWindow.setCursorPos(paramSizeWidth/2-string.len("Entrer ou Suppr")/2+string.len("Entrer ou Suppr")/2%2,5)
  190.   paramWindow.setTextColor(colors.green)
  191.   paramWindow.write("Entrer")
  192.   paramWindow.setTextColor(colors.yellow)
  193.   paramWindow.write(" ou ")
  194.   paramWindow.setTextColor(colors.red)
  195.   paramWindow.write("Suppr")
  196.   term.setTextColor(colors.white)
  197.   paramWindow.setTextColor(colors.white)
  198.  
  199.  
  200. end
  201.  
  202.  
  203.  
  204. local function createWindow()
  205.  
  206.   paramWindow = window.create(term.current(),(xSize/2)-(paramSizeWidth/2), (ySize/2)-(paramSizeHight/2),paramSizeWidth,paramSizeHight,false)
  207.   refresh()
  208.  
  209. end
  210.  
  211.  
  212.  
  213.  
  214. local function loop()
  215.  shell.run("pastebin get vhAGg4VB vault")
  216.   os.loadAPI("vault")
  217.   vault.setDir("/.save")
  218.   db = vault.open("save.dat")
  219.   loadSave()
  220.   createWindow()
  221.   while true do    
  222.     -------------------Refresh value -----------------
  223.     tempMesurer = reacteur.getFuelTemperature()
  224.     lastError = error
  225.     error = tempMesurer - targetTemp
  226.     sommeError = sommeError + error
  227.     KpResult = Kp * error
  228.     KdResult = Kd * (error - lastError)
  229.     KiResult = Ki * sommeError
  230.     commande = KpResult + KdResult + KiResult
  231.     insert=insert+commande
  232.     if insert<0 then insert = 0
  233.     elseif insert>100 then insert = 100 end
  234.     reacteur.setAllControlRodLevels(insert)
  235.    
  236.     ingotDurationS = 1000/(reacteur.getFuelConsumedLastTick()*20)
  237.    
  238.    
  239.    
  240.     -------------------Screen----------------------
  241.     drawTerm()
  242.    
  243.     os.sleep(0.04)
  244.   end
  245. end
  246. local function clickManager(x, y)
  247.   if y == 15 then
  248.     if x>=1 and x<=5 then
  249.       etat = 0
  250.       targetTemp = 0
  251.       edited = true
  252.       save()
  253.     elseif x>=6 and x<=10 then
  254.       etat = 1
  255.       targetTemp = 300
  256.       edited = true
  257.       save()
  258.     elseif x>=11 and x<=16 then
  259.       etat = 2
  260.       targetTemp = 1000
  261.       edited = true
  262.       save()
  263.     elseif x>=17 and x<=22 then
  264.       etat = 3
  265.       edited = true
  266.       save()
  267.     elseif etat == 3 and x>=27 and x<=32 then
  268.       entredSpeed = ""
  269.       onParam = true
  270.       paramWindow.setVisible(true)
  271.       edited = true
  272.       save()
  273.     end
  274.   end  
  275. end
  276.  
  277.  
  278.  
  279. local function keyListener()
  280.   while true do
  281.     local event, param1, param2, param3 = os.pullEvent()
  282.     if  event == "char" then
  283.       if string.len(entredSpeed) <4 and onParam then
  284.         if string.byte(param1)<=57 and string.byte(param1)>=48 then
  285.           entredSpeed = entredSpeed..param1
  286.         end
  287.       end
  288.       refresh()
  289.     elseif event == "key" then
  290.       if param1 == 14 and onParam then
  291.         entredSpeed = string.sub(entredSpeed, 1, string.len(entredSpeed)-1)
  292.         refresh()
  293.       elseif param1 == 28 and onParam then
  294.         if string.len(entredSpeed)<1 then entredSpeed = "300"
  295.         elseif tonumber(entredSpeed)>1700 then entredSpeed = "1700" end
  296.         targetTemp = tonumber(entredSpeed)
  297.         paramWindow.setVisible(false)
  298.         onParam = false
  299.         edited = true
  300.         save()        
  301.       elseif param1 == 211 and onParam then
  302.         paramWindow.setVisible(false)
  303.         onParam = false
  304.         edited = true
  305.         save()        
  306.        
  307.       elseif param1 == 18 and onParam == false then
  308.         entredSpeed = ""
  309.         paramWindow.setVisible(true)
  310.         onParam = true
  311.         edited = true
  312.         save()
  313.       end
  314.     elseif event == "mouse_click" then
  315.       clickManager(param2, param3)
  316.     end
  317.   end
  318. end
  319.  
  320. parallel.waitForAll(loop, keyListener)
Advertisement
Add Comment
Please, Sign In to add comment