Advertisement
Guest User

movement.lua

a guest
Apr 17th, 2014
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.61 KB | None | 0 0
  1. local robot = require("robot")
  2.  
  3. -- Angles: 1=North; 2=West; 3=South; 4=East (Relative to robot placement! Faceing when placed will allways be North!).
  4. local angle = 1
  5.  
  6. -- tries to move the Robot forward by @times times, if there is a obstacle, swing the current Tool.
  7. function tryForward(times)
  8.   for i=1, times do
  9.     while not robot.forward() do
  10.       robot.swing()
  11.     end
  12.   end
  13. end
  14.  
  15. -- tries to move the Robot upwards by @times times, if there is an obstacle, swing the current Tool.
  16. function tryUp(times)
  17.   for i=1, times do
  18.     while not robot.up() do
  19.       robot.swingUp()
  20.     end
  21.   end
  22. end
  23.  
  24. --tries to move the Robot downwards by @times times, if there is an obstacle, swing the current Tool.
  25. function tryDown(times)
  26.   for i=1, times do
  27.     while not robot.down() do
  28.       robot.swingDown()
  29.     end
  30.   end
  31. end
  32.  
  33. -- turns the robot by @degree times in the Direction of @dir<1=Left; 2=Right>
  34. -- Possible degrees: 1=90°; 2=180°; 3=270°; 4=360°; 5=450°...
  35. -- keeps track of the robots current Angle.
  36. function turn(degree, dir)
  37.   local Degree = degree
  38.   local Direction = dir
  39.   for i=1, Degree do
  40.     if Direction==1 then
  41.       robot.turnLeft()
  42.       if angle<4 then
  43.         angle=angle+1
  44.       else
  45.         angle=1
  46.       end
  47.     end
  48.     if Direction==2 then
  49.       robot.turnRight()
  50.       if angle>1 then
  51.         angle=angle-1
  52.       else
  53.         angle=4
  54.       end
  55.     end
  56.   end
  57.   print(angle)
  58. end
  59.  
  60.  
  61. function returnHome(posX, posY, posZ, pointX, pointY, pointZ)
  62.   local diffX = posX-pointX
  63.   local diffY = posY-pointY
  64.   local diffZ = posZ-pointZ
  65.  
  66.   gotoY(diffY)
  67.   sleep(1)
  68.   turnToAngle(4)
  69.   gotoXZ(diffZ)
  70.   sleep(1)
  71.   turnToAngle(3)
  72.   gotoXZ(diffX)
  73. end
  74.  
  75. -- Goes forward a given amount of @times. Warning! Does not set the Angle automatically!
  76. local function gotoXZ(times)
  77.   if times==0 then
  78.     return true
  79.   elseif times>0 then
  80.     tryForward(times)
  81.     return true
  82.   end
  83. end
  84.  
  85. -- Goes up or down a set amount of @times, depending on if @times is pos. or neg.
  86. local function gotoY(times)
  87.   if times==0 then
  88.     return true
  89.   elseif times>0 then
  90.     tryDown(times)
  91.     return true
  92.   elseif times<0 then
  93.     local _times = (-1*times)
  94.     tryUp(_times)
  95.     return true
  96.   end
  97. end
  98.  
  99. -- Turns form the current Angle towards @target Angle.
  100. local function turnToAngle(target)
  101.   if angle~=nil then
  102.     if angle~=target then
  103.       if angle>target then
  104.         turn((angle-target), 2)
  105.         return true
  106.       end
  107.       if angle<target then
  108.         turn((target-angle), 1)
  109.         return true
  110.       end
  111.     else
  112.       return true
  113.     end
  114.   else
  115.     return false
  116.   end
  117. end
  118.  
  119. -- Forces to set the current Angle.(Not recommended!)
  120. function setAngle(_angle)
  121.   angle=_angle
  122. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement