Broken_Fre

Minecraft Turbine (BigReactor) Control

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