Advertisement
Guest User

movement.lua

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