Xonoa

gantry_control

Jun 27th, 2021 (edited)
787
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.30 KB | None | 0 0
  1. --endstop positions
  2. xEndstop, yEndstop = ...
  3.  
  4.  
  5. local w,h term.getSize()
  6. local fineBool = false
  7. local xInv = false
  8. local yInv = false
  9.  
  10. --how big the platform is
  11. platformSize = 8
  12.  
  13. --side definitions
  14. IO = "front"
  15. reverse = "left"
  16. toggleY = "back"
  17. toggleH = "right"
  18. rsIn = "bottom"
  19.  
  20. --time required to move 1 block horozontally
  21. stepTime = 0.45
  22.  
  23.  
  24. --*************Functions*************
  25.  
  26. --initializes mining platform connection
  27. function initPlatform()
  28.     rsRoute(toggleY, "on")
  29.     rsRoute(toggleH, "on")
  30.     rsRoute(IO, "on")
  31.     sleep(2)
  32.     rsRoute(reverse, "on")
  33.     sleep(4)
  34.     clearRS()
  35. end
  36.  
  37. --finds endstops
  38. function findExtent()
  39.     back()
  40.     right()
  41.     fine()
  42.     --find x endstop
  43.     while rs.getAnalogInput(rsIn) ~= 3 do
  44.         left()
  45.         xEndstop = xEndstop + 1
  46.         sleep(0.15)
  47.     end
  48.    
  49.     for x=0,(xEndstop) do
  50.         right()
  51.         sleep(0.15)
  52.     end
  53.    
  54.     --find y endstop
  55.     while rs.getAnalogInput(rsIn) ~= 4 do
  56.         forward()
  57.         yEndstop = yEndstop + 1
  58.         sleep(0.15)
  59.     end
  60.    
  61.     for x=0,(yEndstop) do
  62.         back()
  63.         sleep(0.15)
  64.     end
  65.    
  66.     fine()
  67.     --debug
  68.     --print(xEndstop)
  69.     --print(yEndstop)
  70. end
  71.  
  72. --sets colors for text and BG
  73. function setTheme(FG, BG)
  74.     term.setBackgroundColor(BG)
  75.     term.setTextColor(FG)
  76. end
  77.  
  78. --sets redstone off
  79. function clearRS()
  80.     rs.setAnalogOutput(IO,0)
  81.     rs.setAnalogOutput(toggleH,0)
  82.     rs.setAnalogOutput(toggleY,0)
  83.     rs.setAnalogOutput(reverse,0)
  84.     rs.setAnalogOutput(rsIn,0)
  85. end
  86.  
  87.  
  88. --draw functions
  89. function drawBtn(text, width, height, xpos, ypos)
  90.     --positions are for the top left of the btn
  91.    
  92.     local linesAbove = math.floor((height-1)/2)
  93.     local linesBelow = math.ceil((height-1)/2)
  94.     local spaceLeft = math.floor((width-#text)/2)
  95.     local spaceRight = math.ceil((width-#text)/2)
  96.    
  97.     --draw lines above text
  98.     for line=0,linesAbove do
  99.         term.setCursorPos(xpos,ypos+line)
  100.         for character=1,width do
  101.             write(" ")
  102.         end
  103.     end
  104.    
  105.     --draw lines below text
  106.     for line=0,linesBelow do
  107.         term.setCursorPos(xpos, ypos+linesAbove+line)
  108.         for character=1,width do
  109.             write(" ")
  110.         end
  111.     end
  112.    
  113.     --draw text line
  114.     term.setCursorPos(xpos, ypos+linesAbove)
  115.     for spaces=1,spaceLeft do
  116.         write(" " )
  117.     end
  118.     write(text)
  119.     for spaces=1,spaceRight do
  120.         write(" ")
  121.     end
  122. end
  123.  
  124.  
  125. --outputs the redstone signals
  126. function rsRoute(freq, command)
  127.     if command=="on" then
  128.         rs.setAnalogOutput(freq, 15)
  129.     end
  130.    
  131.     if command=="off" then
  132.         rs.setAnalogOutput(freq, 0)
  133.     end
  134.    
  135.     if command=="pulse" then
  136.         rs.setAnalogOutput(freq,15)
  137.         sleep(0.1)
  138.         rs.setAnalogOutput(freq, 0)
  139.     end
  140. end
  141.  
  142.  
  143. --functions to translate commands into redstone
  144. function moveX(distance)
  145.     rsRoute(toggleY, "off")
  146.     rsRoute(toggleH, "off")
  147.    
  148.     if xInv then distance = -1*distance end
  149.    
  150.     if distance>0 then
  151.         rsRoute(reverse, "off")
  152.     elseif distance<0 then
  153.         rsRoute(reverse, "on")
  154.         distance = -1 * distance
  155.     else
  156.         sleep(0.1)
  157.     end
  158.    
  159.     sleep(0.1)
  160.     rsRoute(IO, "on")
  161.     sleep(stepTime*distance)
  162.     rsRoute(IO, "off")
  163. end
  164.  
  165. function moveY(distance)
  166.     rsRoute(toggleY, "on")
  167.     rsRoute(toggleH, "off")
  168.    
  169.     if yInv then distance = -1*distance end
  170.    
  171.     if distance>0 then
  172.         rsRoute(reverse, "off")
  173.     elseif distance<0 then
  174.         rsRoute(reverse, "on")
  175.         distance = -1 * distance
  176.     else
  177.         sleep(0.1)
  178.     end
  179.    
  180.     sleep(0.1)
  181.     rsRoute(IO, "on")
  182.     sleep(stepTime*distance)
  183.     rsRoute(IO, "off")
  184. end
  185.  
  186. function digToBR()
  187.  clearRS()
  188.     rsRoute(toggleY, "on")
  189.     rsRoute(toggleH, "on")
  190.    
  191.     local atBottom = false
  192.     local atTop = false
  193.    
  194.     rsRoute(IO,"on")
  195.  sleep(2)
  196.    
  197.     while not atBottom do
  198.         os.pullEvent("redstone")
  199.         if rs.getAnalogInput(rsIn) == 1 then
  200.             rsRoute(reverse, "on")
  201.             atBottom = true
  202.         elseif rs.getAnalogInput(rsIn) > 1 then
  203.             --whatever else we need to do later
  204.         else
  205.             sleep(0.1)
  206.         end
  207.     end
  208.    
  209.     while not atTop do
  210.         os.pullEvent("redstone")
  211.         if rs.getAnalogInput(rsIn) == 2 then
  212.             rsRoute(IO, "off")
  213.             atTop = true
  214.         elseif rs.getAnalogInput(rsIn) ~= 2 then
  215.             --other stuff later maybe
  216.         else
  217.             sleep(0.1)
  218.         end
  219.     end
  220. end
  221.  
  222.  
  223. --functinos for the buttons
  224. function forward()
  225.     --print("forward")
  226.     if fineBool then moveY(-1) end
  227.     if not fineBool then moveY(-4) end
  228. end
  229.  
  230. function back()
  231.     --print("back")
  232.     if fineBool then moveY(1) end
  233.     if not fineBool then moveY(4) end
  234. end
  235.  
  236. function left()
  237.     --print("left")
  238.     if fineBool then moveX(1) end
  239.     if not fineBool then moveX(4) end
  240. end
  241.  
  242. function right()
  243.     --print("right")
  244.     if fineBool then moveX(-1) end
  245.     if not fineBool then moveX(-4) end
  246. end
  247.  
  248. function fine()
  249.     fineBool = not fineBool
  250.    
  251.     if fineBool then
  252.         setTheme(colors.black, colors.green)
  253.     else
  254.         setTheme(colors.black, colors.red)
  255.     end
  256.     drawBtn("FINE",6,3,21,4)
  257. end
  258.  
  259. function invertX()
  260.     xInv = not xInv
  261.    
  262.     if xInv then
  263.         setTheme(colors.black, colors.green)
  264.     else
  265.         setTheme(colors.black, colors.red)
  266.     end
  267.     drawBtn("-X",3,3,4,14)
  268. end
  269.  
  270. function invertY()
  271.     yInv = not yInv
  272.    
  273.     if yInv then
  274.         setTheme(colors.black, colors.green)
  275.     else
  276.         setTheme(colors.black, colors.red)
  277.     end
  278.     drawBtn("-Y",3,3,14,14)
  279. end
  280.  
  281. function toBR()
  282.     digToBR()
  283. end
  284.  
  285. function mine()
  286.     --init variables
  287.     local xSteps = math.floor(xEndstop/platformSize)
  288.     local ySteps = math.floor(yEndstop/platformSize)
  289.     local backwards = false
  290.    
  291.     --turns off fine control
  292.     if fineBool then fine() end
  293.    
  294.     --main loop
  295.     for x=1,xSteps do
  296.         for y=1,ySteps do
  297.             digToBR()
  298.             sleep(0.15)
  299.             if backwards then
  300.                 for s=1,(platformSize/4) do back() sleep(0.15) end
  301.             else
  302.                 for s=1,(platformSize/4) do forward() sleep(0.15) end
  303.             end
  304.         end
  305.         sleep(0.15)
  306.         digToBR()
  307.         sleep(0.15)
  308.         for s=1,(platformSize/4) do left() sleep(0.15) end
  309.         backwards = not backwards
  310.         sleep(10)
  311.     end
  312. end
  313.  
  314.  
  315.  
  316. --************MAIN*****************--
  317. --clear Term and redstone
  318. term.setBackgroundColor(colors.black)
  319. term.clear()
  320. clearRS()
  321.  
  322. --draw momentary buttons
  323. setTheme(colors.black, colors.white)
  324. drawBtn("^",3,3,9,4)
  325. drawBtn("<",3,3,4,9)
  326. drawBtn(">",3,3,14,9)
  327. drawBtn("v",3,3,9,14)
  328. setTheme(colors.white, colors.black)
  329.  
  330. --draw toggle buttons
  331. setTheme(colors.black, colors.red)
  332. drawBtn("FINE",6,3,21,4)
  333. drawBtn("-X",3,3,4,14)
  334. drawBtn("-Y",3,3,14,14)
  335. setTheme(colors.black, colors.green)
  336. drawBtn("2 BR",6,3,21,9)
  337. drawBtn("AREA",6,3,21,14)
  338. setTheme(colors.white, colors.black)
  339.  
  340. --sets cursor at 1,1 for debug
  341. term.setCursorPos(1,1)
  342.  
  343. --initialize
  344. initPlatform()
  345. if (xEndstop==yEndstop) and (xEndstop==0) then
  346.     findExtent()
  347. end
  348.  
  349.  
  350. while true do
  351.     local _,_,mX,mY = os.pullEvent("mouse_click")
  352.    
  353.     local x = 0
  354.     local y = 0
  355.    
  356.     if (4 <= mX) and  (mX <= 6) then
  357.         x = 1
  358.     elseif (9 <= mX) and  (mX <= 11) then
  359.         x = 2
  360.     elseif (14 <= mX) and  (mX <= 20) then
  361.         x = 3
  362.     elseif (21 <= mX) and  (mX <= 26) then
  363.         x = 4
  364.     else
  365.         x = 0
  366.     end
  367.    
  368.     if (4 <= mY) and  (mY <= 6) then
  369.         y = 1
  370.     elseif (9 <= mY) and  (mY <= 11) then
  371.         y = 2
  372.     elseif (14 <= mY) and  (mY <= 16) then
  373.         y = 3
  374.     else
  375.         y = 0
  376.     end
  377.    
  378.     if (x==1) and (y==2) then left() end
  379.     if (x==1) and (y==3) then invertX() end
  380.     if (x==3) and (y==3) then invertY() end
  381.     if (x==2) and (y==1) then forward() end
  382.     if (x==2) and (y==3) then back() end
  383.     if (x==3) and (y==2) then right() end
  384.     if (x==4) and (y==1) then fine() end
  385.     if (x==4) and (y==2) then toBR() end
  386.     if (x==4) and (y==3) then mine() end
  387. end
  388.  
Add Comment
Please, Sign In to add comment