Advertisement
hyndgrinder

vecMove

May 9th, 2014
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.61 KB | None | 0 0
  1. ----------------vecMove API----------------------
  2. -----Move your turtle using vectoring-------------
  3.  
  4. --------------------------------Functions-----------------------------------
  5. ---------getCoords()-----------------------------
  6. --load last know coordinates (dropped by detHead API)
  7. function getCoords()
  8.     local coordFile=fs.open("/nav/gpsCoord","r")
  9.     local coordData=coordFile.readAll()
  10.     coordFile.close()
  11.     local coordTbl=textutils.unserialize(coordData)
  12.     local x=coordTbl[1]
  13.     local y=coordTbl[2]
  14.     local z=coordTbl[3]
  15.     local d=coordTbl[4]
  16.     print (x,", ", y,", ", z,", ", d)
  17.     return x, y, z, d
  18. end
  19.  
  20. ----------checkPos-------------------------
  21. --gets the current position and builds a displacement vector
  22. function checkPos(Dest)
  23.   currPos=vector.new(gps.locate())
  24.   disp=Dest-currPos
  25.   --term.clear()
  26.   --term.setCursorPos(1,1)
  27.  
  28.   --output to for validation
  29.   print ("Destination: ",Dest)
  30.   print ("Current Position: ",currPos)
  31.   print ("Displacement: ",disp)
  32.   print ("Movement map:")
  33.   --For future use:  determine is horizontal displacement is resolved
  34.   local hDist=math.abs(disp.x)+math.abs(disp.z)
  35.   return disp
  36. end
  37. -----------setHead------------------
  38. --requires the current heading and target heading.  
  39. --Then spins the turtle right until the target is reached
  40. function setHead(current, target)
  41.    repeat
  42.     print("Current: ", current,". Target: ", target)
  43.     if current~=target then
  44.       turtle.turnRight()
  45.       current=current+1
  46.       if current==4 then current=0 end  --Resets to South after East is reached
  47.     end
  48.     until current==target
  49.   print("Heading is now ", dirTbl[current])
  50.   return true, current
  51. end
  52.  
  53. ----------shellGo-----------
  54. --moves the turtle with go command;
  55. --keeps program from producing errors
  56. --and/or breaking unexpected obstacles like my chests.
  57. --Turtle will hold position until the
  58. --obstacle is removed manually, or program is terminated.
  59. function shellGo(distance)
  60.   shell.run("go", "forward", distance)
  61. end
  62. ----------updatePos----------
  63. --runs the detHead API to refresh the
  64. --gpsCoord (last known location) file with changes
  65. function updatePos(_p1)
  66.     local origin=_p1.x," ",_p1.y," ",_p1.z
  67.     local _p2=vector.new(gps.locate())
  68.     local target=_p2.x," ",_p2.y," ",_p2.z
  69.     shell.run("detHead", origin, target)
  70.     return true, _p2
  71. end
  72.  
  73. --------------------------------Process----------------------------------
  74. local _tArgs={...}
  75. rednet.open("right")
  76.  
  77. --load directional DB--
  78. local dirFile=fs.open("/nav/dir.tab","r")
  79. local dirData=dirFile.readAll()
  80. dirFile.close()
  81. dirTbl=textutils.unserialize(dirData)
  82.  
  83. --Input Destination--
  84. if #_tArgs~=3 then
  85.   print ("Usage: vecMove.2 <x y z>")
  86.   return
  87. end
  88.  
  89. local strDestx=_tArgs[1]
  90. local strDesty=_tArgs[2]
  91. local strDestz=_tArgs[3]
  92.  
  93. local Dest=vector.new(strDestx, strDesty, strDestz)
  94.  
  95. local x, y, z, d=getCoords()
  96.  
  97. local disp=checkPos(Dest)
  98.  
  99. --EAST---------------------------
  100.   if disp.x>0 then
  101.     local success, dir=setHead(d,3)
  102.     print ("Turned east?  ",success," Direction: ", dirTbl[dir])
  103.     if success==true then
  104.       shellGo(disp.x)
  105.       d=dir
  106.     else
  107.       print ("Set East Heading failed")
  108.     end
  109. --WEST----------------------------
  110.   elseif disp.x<0 then
  111.     local success, dir=setHead(d,1)
  112.     print ("Turned west? ", success, "Direction: ", dirTbl[dir])
  113.     if success==true then
  114.       shellGo(math.abs(disp.x))
  115.       d=dir
  116.     else
  117.       print ("Set West Heading failed")
  118.     end
  119.   elseif disp.x==0 then
  120.     print ("X axis is resolved")
  121.   else
  122.     print ("X Movement Error!")
  123.   end
  124.  
  125.     local verPosUpdate, currPos=updatePos(currPos)
  126.   checkPos(Dest)
  127.  
  128. --SOUTH-------------------------------
  129.   if disp.z>0 then
  130.     local success, dir=setHead(d,0)
  131.     print ("Turned south? ", success, "Direction: ", dirTbl[dir])
  132.     if success==true then
  133.       shellGo(disp.z)
  134.       d=dir
  135.     else
  136.       print ("Set South Heading failed")
  137.     end
  138. --NORTH-------------------------------
  139.   elseif disp.z<0 then
  140.     local success, dir=setHead(d,2)
  141.     print ("Turned north? ", success, "Direction: ", dirTbl[dir])
  142.     if success==true then
  143.       shellGo(math.abs(disp.z))
  144.       d=dir
  145.     else
  146.       print ("Set North Heading failed")
  147.     end
  148.   elseif disp.z==0 then
  149.     print ("Z axis is resolved")
  150.     else
  151.     print ("Set Z heading failed")
  152.   end
  153.   print("made it through Z")
  154. local verPosUpdate, currPos=updatePos(currPos)
  155. checkPos(Dest)
  156.  
  157. --  checkPos(Dest)
  158.   print ("y displacement", disp.y)
  159.   if disp.y>0 then
  160.     shell.run("go", "up", disp.y)
  161.   else if disp.y<0 then
  162.     shell.run("go", "down", math.abs(disp.y))
  163.   end
  164.   --  checkPos(Dest)
  165. end
  166. --end
  167. rednet.close("right")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement