Advertisement
EastmanLG

Computercraft Roomba

May 27th, 2019
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.27 KB | None | 0 0
  1. local fuelSlot = 16
  2.  
  3. function checkResources()
  4.     --Check fuel
  5.     local selectedSlot = turtle.getSelectedSlot()
  6.     local fuelThreshold = 2
  7.  
  8.     if(turtle.getFuelLevel() < fuelThreshold) then
  9.         print("Turtle out of fuel!")
  10.         if(turtle.getItemCount(fuelSlot)>0) then
  11.             turtle.select(fuelSlot) --switch to fuel slot
  12.             if(turtle.refuel(1))then --refuel one coal
  13.                 print("Turtle Refueled")
  14.             end
  15.             turtle.select(selectedSlot) --switch back to previous slot
  16.         end
  17.     end
  18. end
  19.  
  20. function turnAround()
  21.     turtle.turnRight()
  22.     turtle.turnRight()
  23. end
  24.  
  25. function checksurroundings()
  26.     --Checking surroundings return values
  27.     -- -1 - unreliable
  28.     -- 0 - no surrounding blocks
  29.     -- 1 - block only on left
  30.     -- 2 - block only on right
  31.     -- 4 - block only on front
  32.     -- 3 - blocks on left, right
  33.     -- 5 - blocks on front, left
  34.     -- 6 - blocks on front, right
  35.     -- 7 - blocks on front, left, and right
  36.  
  37.     local surroundings = 0
  38.  
  39.     if(turtle.turnLeft()) then--turn left to face left
  40.         if(turtle.detect()) then --detect
  41.             surroundings = surroundings + 1 -- 1 - block on left
  42.         end
  43.     else --unable to turn left
  44.         print "Unreliable left detect"
  45.         return -1
  46.     end
  47.  
  48.     if(turtle.turnRight()) then --Turn right to face forward
  49.         if(turtle.detect()) then
  50.             surroundings = surroundings + 4 -- 2 - block on front
  51.         end
  52.     else --unable to turn right returns unreliable
  53.         print "Unreliable front detect"
  54.         return -1
  55.  
  56.     end
  57.  
  58.     if(turtle.turnRight()) then --Turn right to face right
  59.         if(turtle.detect()) then
  60.             surroundings = surroundings + 2 -- 4 - block on right
  61.         end
  62.         turtle.turnLeft() --return to face forwards
  63.     else --unable to turn right returns unreliable
  64.         print "Unreliable right detect"
  65.         return -1
  66.     end
  67.  
  68.     return surroundings
  69. end
  70.  
  71. function rotateToPosition(position)
  72.     local timeout = 4 --how many times to turn before returning failure
  73.     local timer = 0 --incremental timer
  74.     local success = false
  75.  
  76.     while(not success) do
  77.         if(timer<timeout) then
  78.             local surroundings = checksurroundings()
  79.             if(surroundings == position)then
  80.                 success = true
  81.             else
  82.                 turtle.turnRight()
  83.             end
  84.         end
  85.         return false
  86.     end
  87.  
  88.     return success
  89. end
  90.  
  91. function getPerimeterPath()
  92.     local perimeterPath = {}
  93.     --Forward
  94.     --Left turn
  95.     --Right turn
  96.     --turnaround
  97.  
  98.     local lastSurroundings = 0
  99.  
  100.     local ogX,ogY,ogZ = gps.locate()
  101.     local retToStart = false
  102.     local startSurroundings = checksurroundings()
  103.  
  104.     while(not retToStart) do
  105.         checkResources()
  106.  
  107.         local surroundings = checksurroundings() --get current surroundings
  108.         --print("new: " .. tostring(surroundings)) --for diagnostics
  109.         --print("last: " .. tostring(lastSurroundings)) --for diagnostics
  110.        
  111.  
  112.         if(surroundings == 0)then --Nothing around the turtle
  113.             turtle.turnLeft()
  114.             turtle.forward()
  115.             table.insert(perimeterPath, "left")
  116.             table.insert(perimeterPath, "forward")
  117.             print("move: turn left and forward")
  118.  
  119.         elseif(surroundings == 1) then --Follow a left wall
  120.             turtle.forward()
  121.             table.insert(perimeterPath, "forward")
  122.             print("move: forward")
  123.  
  124.         elseif(surroundings == 2) then --Only block on right (probably previously followed wall)
  125.             turtle.turnLeft()
  126.             turtle.forward()
  127.             table.insert(perimeterPath, "left")
  128.             table.insert(perimeterPath, "forward")
  129.             print("move: turn left and forward")
  130.  
  131.         elseif(surroundings == 3)then --Follow a left wall
  132.             turtle.forward()
  133.             table.insert(perimeterPath, "forward")
  134.             print("move: forwards")
  135.  
  136.         elseif(surroundings == 4) then --Only block in front
  137.             turtle.turnLeft()
  138.             turtle.forward()
  139.             table.insert(perimeterPath, "left")
  140.             table.insert(perimeterPath, "forward")
  141.             print("move: turn left and forwards")
  142.  
  143.  
  144.         elseif(surroundings == 5) then --Normal corner
  145.             turtle.turnRight()
  146.             turtle.forward()
  147.             table.insert(perimeterPath, "right")
  148.             table.insert(perimeterPath, "forward")
  149.             print("move: turn right and forwards")
  150.  
  151.         elseif(surroundings == 6) then --Block ahead and to right, able to move left
  152.             turtle.turnLeft()
  153.             turtle.forward()
  154.             table.insert(perimeterPath, "left")
  155.             table.insert(perimeterPath, "forward")
  156.             print("move: turn left and forwards")
  157.  
  158.         elseif(surroundings == 7) then --Cornered in, turn around!
  159.             turnAround()
  160.             turtle.forward()
  161.             table.insert(perimeterPath, "uturn")
  162.             table.insert(perimeterPath, "forward")
  163.             print("move: turn around and forwards")
  164.         end
  165.    
  166.         local newX,newY,newZ = gps.locate()
  167.         --print("gps: " .. tostring(newX) .. "," .. tostring(newY) .. "," .. tostring(newZ))
  168.         if(newX == ogX and newY == ogY and newZ == ogZ) then
  169.             retToStart = true
  170.         end
  171.  
  172.         --print("----------------------")
  173.  
  174.         lastSurroundings = surroundings
  175.     end
  176.  
  177.     if(rotateToPosition(startSurroundings)) then
  178.         print("Returned to base!")
  179.     else
  180.         print("unable to find starting base position")
  181.     end
  182.     return perimeterPath
  183. end
  184.  
  185. function doAction(action)
  186.     if(action == "forward") then
  187.         print("move: forward")
  188.         turtle.forward()
  189.     elseif(action == "left") then
  190.         print("move: turn left")
  191.         turtle.turnLeft()
  192.     elseif(action == "right")then
  193.         print("move: turn right")
  194.         turtle.turnRight()
  195.     elseif(action =="uturn")then
  196.         print("move: turn around")
  197.         turnAround()
  198.     else
  199.         print("Action not recognised")
  200.     end
  201. end
  202.  
  203. local perimeterPath = getPerimeterPath()
  204.  
  205. for i, pathPiece in ipairs(perimeterPath) do
  206.     doAction(pathPiece)
  207. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement