Advertisement
Guest User

Untitled

a guest
Dec 10th, 2018
383
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.34 KB | None | 0 0
  1. robot = require("robot")
  2. component = require("component")
  3.  
  4. function getPosition()
  5.     local x,y,z = component.navigation.getPosition()
  6.     if x == nil or y == nil or z == nil then
  7.      print("Error: out of bounds, can not locate self...")
  8.      os.exit()
  9.     end
  10.     return x,y,z
  11.    end
  12.  
  13. StartPositionX , StartPositionY , StartPositionZ = component.navigation.getPosition() -- where the robot is when program started
  14. if getPosition then
  15.     CurrentPositionX , CurrentPositionY , CurrentPositionZ = component.navigation.getPosition()
  16.     print("Current Pos: [X: "..CurrentPositionX.." Y: "..CurrentPositionY.." Z: "..CurrentPositionZ .."]")
  17. end
  18. DistX = "NCY"
  19. DistY = "NCY"--Not Calibrated Yet...
  20. DistZ = "NCY"
  21. Down = false
  22.  
  23. --function pointToDirection(dir)
  24.  --while nav.getFacing() ~= dir do robot.turnLeft() end
  25. --end
  26.  
  27. function getPosition()
  28. local   x,y,z = component.navigation.getPosition()
  29.  if x == nil or y == nil or z == nil then
  30.   print("Error: out of bounds, can not locate self...")
  31.   os.exit()
  32.  end
  33. end
  34.  
  35. function CheckDirection(Value1 , Value2 , Value3) -- Value1 : (Changing Value) , Value2 : (Value That Gets Compared To Value) , Value3 : X , Y , or Z
  36.  print("Checking Direction: "..Value3)
  37.  local loop = true
  38.  
  39.  
  40.  
  41.  if Value3 == "X" then
  42.   while loop == true do
  43.     print("Checking...")
  44.   print(Value1 , Value2)
  45.   robot.forward()
  46.   CurrentPositionX , CurrentPositionY , CurrentPositionZ = getPosition() -- Where The Robot Currently Is
  47.    Value1 = CurrentPositionX --To check if the dir is correct (E(+) , W(-))
  48.    if Value1 ~= Value2 then
  49.     if Value1 < Value2 then -- Robot got closer to origin
  50.      print("Got Closer To Origin")
  51.      loop = false
  52.      return true
  53.     elseif Value1 > Value2 then -- Robot got farther from origin
  54.     print("Moving Back Wrong Dir")
  55.     robot.back()
  56.      robot.turnLeft()
  57.     end
  58.    elseif Value1 == Value2 then
  59.     print("Moving Back Wrong Dir")
  60.     robot.back()
  61.     robot.turnLeft()
  62.    end
  63.   end
  64.  
  65.  
  66.  elseif Value3 == "Y" then
  67.   while loop == true do
  68.     print("Checking...")
  69.    robot.up()
  70.   CurrentPositionX , CurrentPositionY , CurrentPositionZ = getPosition() -- Where The Robot Currently Is
  71.    Value1 = CurrentPositionY --To check if the dir is correct (Up Or Down)
  72.    if Value1 < Value2 then  -- Robot got closer to origin
  73.     loop = false
  74.     return true
  75.    elseif Value1 > Value2 then-- Robot got farther from origin
  76.     Down = true
  77.     robot.down()
  78.    end
  79.   end
  80.  
  81.  
  82.  elseif Value3 == "Z" then
  83.   while loop == true do
  84.     print("Checking...")
  85.    robot.forward()
  86.   CurrentPositionX , CurrentPositionY , CurrentPositionZ = getPosition() -- Where The Robot Currently Is
  87.    Value1 = CurrentPositionZ --To check if the dir is correct (N(-) , S(+))
  88.    if Value1 ~= Value2 then
  89.     if Value1 < Value2 then -- Robot got closer to origin
  90.      loop = false
  91.      return true
  92.     elseif Value1 > Value2 then-- Robot got farther from origin
  93.     print("Moving Back Wrong Dir")
  94.     robot.back()
  95.      robot.turnLeft()
  96.     end
  97.    elseif Value1 == Value2 then
  98.     print("Moving Back Wrong Dir")
  99.     robot.back()
  100.     robot.turnLeft()
  101.    end
  102.   end
  103.  end
  104. end
  105.  
  106. function Goto_Origin_X(X)
  107.  print("Going to X")
  108.  if CheckDirection(CurrentPositionX , StartPositionX , "X") then
  109.   DistX = CurrentPositionX - StartPositionX
  110.   print("DistanceX: "..DistX)
  111.   for Count = 0 , DistX do
  112.    if robot.detect() == false then
  113.     robot.forward()
  114.    elseif robot.detect() == true then
  115.     robot.up()
  116.    end
  117.   end
  118.  end
  119. end
  120.  
  121. function Goto_Origin_Y(Y)
  122.  print("Going To Y")
  123.  if CheckDirection(CurrentPositionY , StartPositionY , "Y") then
  124.   DistY = CurrentPositionY - StartPositionY
  125.   print("DistanceY: "..DistY)
  126.   for Count = 0 , DistY do
  127.    if robot.detect() == false then
  128.     if Down == false then
  129.      robot.up()
  130.     else
  131.      robot.down()
  132.     end
  133.    elseif robot.detect() == true then
  134.     robot.up()
  135.    end
  136.   end
  137.  end
  138. end
  139.  
  140. function Goto_Origin_Z(Z)
  141.  print("Going To Z")
  142.  if CheckDirection(CurrentPositionZ , StartPositionZ , "Z") then
  143.   DistZ = CurrentPositionZ - StartPositionZ
  144.   print("DistanceZ: "..DistZ)
  145.   for Count = 0 , DistZ do
  146.    if robot.detect() == false then
  147.     robot.forward()
  148.    elseif robot.detect() == true then
  149.     robot.up()
  150.    end
  151.   end
  152.  end
  153. end
  154.  
  155. Goto_Origin_X(CurrentPositionX)
  156. Goto_Origin_Y(CurrentPositionY)
  157. Goto_Origin_Z(CurrentPositionZ)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement