xavierlebel

gps3tut

Jun 7th, 2024 (edited)
371
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.50 KB | None | 0 0
  1. Turtle GPS self-tracker expansion (tutorial)
  2.  
  3. Important: When starting, the program assumes that it is facing west, so first you must face West and then place the turtle. Here are some ways to check where you are facing: You can see the sun/moon rising right in front of you. You are facing East. You can see the sun/moon falling right in front of you. You are facing West. Clouds go west. Check where they go. Press F3 and locate the number behind "f". This one is below "x", "y" and "z". If this number is 0, you are facing North. If this number is 1, you are facing West. If this number is 2, you are facing South. If this number is 3, you are facing East.
  4. Contents
  5.  
  6.     1 Code
  7.     2 How to Use
  8.         2.1 setLocation
  9.         2.2 manSetLocation
  10.         2.3 getLocation
  11.         2.4 forward, back, up, down, turnLeft and turnRight
  12.         2.5 jump
  13.  
  14. Code
  15.  
  16. Note: This will only work on a Turtle. That said, even if it worked on a computer, it wouldnt have any practical use because a computer cant move.
  17. Before you start, you must add this to the program that you want to use it with at the top line:
  18.  
  19. os.loadApi("tst")
  20.  
  21. This will allow the program to use the API. This tutorial assumes that you use the file name "tst" for the API, but if you like to you can change it. You must then also change every reference to "tst" in your program, including the line above. Write this in the computer/turtle:
  22.  
  23. -- Turtle Self-tracking System created by Latias1290.
  24.  
  25. local xPos, yPos, zPos = nil
  26. face = 1
  27. cal = false
  28.  
  29. function setLocation() -- get gps using other computers
  30.  xPos, yPos, zPos = gps.locate()
  31.  cal = true
  32. end
  33.  
  34. function manSetLocation(number x, number y, number z) -- manually set location
  35.  xPos = x
  36.  yPos = y
  37.  zPos = z
  38.  cal = true
  39. end
  40.  
  41. function getLocation() -- return the location
  42.  if xPos ~= nil then
  43.   return xPos, yPos, zPos
  44.  else
  45.   return nil
  46.  end
  47. end
  48.  
  49. function turnLeft() -- turn left
  50.  if face == 0 then
  51.   face = 1
  52.  elseif face == 1 then
  53.   face = 2
  54.  elseif face == 2 then
  55.   face = 3
  56.  elseif face == 3 then
  57.   face = 0
  58.  end
  59. end
  60.  
  61. function turnRight() -- turn right
  62.  if face == 0 then
  63.   face = 3
  64.  elseif face == 1 then
  65.   face = 0
  66.  elseif face == 2 then
  67.   face = 1
  68.  elseif face == 3 then
  69.   face = 2
  70.  end
  71. end
  72.  
  73. function forward() -- go forward
  74.  turtle.forward()
  75.  if cal == true then
  76.   if face == 0 then
  77.    zPos = zPos - 1
  78.   elseif face == 1 then
  79.    xPos = xPos - 1
  80.   elseif face == 2 then
  81.    zPos = zPos + 1
  82.   elseif face == 3 then
  83.    xPos = xPos + 1
  84.   end
  85.  else
  86.   print("Not Calibrated.")
  87.  end
  88. end
  89.  
  90. function back() -- go back
  91.  turtle.back()
  92.  if cal == true then
  93.   if face == 0 then
  94.    zPos = zPos + 1
  95.   elseif face == 1 then
  96.    xPos = xPos + 1
  97.   elseif face == 2 then
  98.    zPos = zPos - 1
  99.   elseif face == 2 then
  100.    xPos = xPos - 1
  101.   end
  102.  else
  103.   print("Not Calibrated.")
  104.  end
  105. end
  106.  
  107. function up() -- go up
  108.  turtle.up()
  109.  if cal == true then
  110.   yPos = yPos + 1
  111.  else
  112.   print("Not Calibrated.")
  113.  end
  114. end
  115.  
  116. function down() -- go down
  117.  turtle.down()
  118.  if cal == true then
  119.   yPos = yPos - 1
  120.  else
  121.   print("Not Calibrated.")
  122.  end
  123.  
  124.  function jump() -- perform a jump. useless? yup!
  125.   turtle.up()
  126.   turtle.down()
  127.  end
  128.  
  129. How to Use
  130.  
  131. This might seem daunting at the first moment. But dont worry, I will walk you through it. Note that to use it properly, you must always put "tst." in front of it, or how you named it followed by a dot. So if I wanted to have my turtle go 1 block forward, I typed "tst.forward()". Note that if you do not do either tst.getLocation OR tst.setLocation(coords), the API will print "Not Calibrated." every time you use a command. This will not prevent you from using it, however.
  132. setLocation
  133.  
  134. To use this, you will need at least 4 computers equipped with a Wireless Modem, and having it host a GPS. More info about that here and here.
  135. manSetLocation
  136.  
  137. For this one you will need to know the location of your turtle. Any location will work, but the API loses its point if you use another coordinate set. A good way to obtain the coordinates is to open up the debug screen(press F3), stand on the turtle, write down the x, y and z fields, substract 1 from the Y coordinate and use these.
  138. getLocation
  139.  
  140. This one is pretty simple. It returns the coordinates. Note that you will have to overwrite all of them. For example, if we had the variables x, y and z, and we wanted to have the value set to our location, we did this:
  141.  
  142. x, y, z = tst.getLocation()
  143.  
  144. Simple right? Note that this will return nil if you are not calibrated, which means if you do it before you do setLocation or manSetLocation, x, y and z will be null!
  145. forward, back, up, down, turnLeft and turnRight
  146.  
  147. This one is pretty easy as well. It makes your turtle move 1 block in the direction you used(its obvious which function goes where) and update its location according to where it went.
  148. jump
  149.  
  150. This one works a bit different. It makes the turtle go up, then go down. "But thats useless!" you might say, and indeed it is!
  151. Categories:
  152.  
  153.     Unofficial APIsTutorials
  154.  
  155. Navigation menu
  156.  
  157.     Log in
  158.  
  159.     Page
  160.     Discussion
  161.  
  162.     Read
  163.     View source
  164.     View history
  165.  
  166.     Main page
  167.     Recent changes
  168.     Random page
  169.  
  170. Links
  171.  
  172.     Main Site
  173.     Forums
  174.  
  175. Tools
  176.  
  177.     What links here
  178.     Related changes
  179.     Special pages
  180.     Printable version
  181.     Permanent link
  182.     Page information
  183.  
  184.     This page was last modified on 17 May 2017, at 17:27.
  185.     This page has been accessed 28,250 times.
  186.  
  187.     Privacy policy
  188.     About ComputerCraft Wiki
  189.     Disclaimers
  190.  
  191.     Powered by MediaWiki
  192.  
  193.  
Advertisement
Add Comment
Please, Sign In to add comment