This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
eddieo's profile picture

GPS Api 101 more functions

Started by eddieo, 15 July 2012 - 01:29 PM
eddieo #1
Posted 15 July 2012 - 03:29 PM
Hey all I'm back again with more info on the gps system. I have worked out a few more uses for the gps api. First you need to go through my first tutorial and get a gps system up and working. Once you have a working system the next thing you need is a wireless turtle. now for some code. We need to know where the turtle is x , y , z and also which direction its facing.I wrote a api for this purpose.



function getturtlepos()
local ch -- sets a var for the direction the turtle is facing
rednet.open("right") -- opens the modem
local cx,cy,cz = gps.locate(10) -- gets the turtles position
turtle.forward() -- moves the turtle forward 1 space
local fx,fy,fz = gps.locate(10) -- gets the turtles position
turtle.back() -- moves the turtle back to its original position
rednet.close("right") -- closes the modem
if fx > cx then --if final X is Greater than original X  turtle is facing East
ch = 3 --East
elseif fx < cx then --if final X is Less than original X  turtle is facing West
ch = 1 --West
elseif fz > cz then --if final Z is Greater than original Z  turtle is facing South
ch = 4 --South
elseif fz < cz then --if final Z is Less than original Z  turtle is facing North
ch = 2 --North
end
return cx , cy , cz , ch -- returns position and heading info
end

save this to a file in the apis directory name the file tgps

Now our turtle can find what its position and heading is. To make the turtle go somewhere we need to be able to store and recall waypoints here is some code to do just that


print("Enter name for location") -- prompts the user for input
local c = io.read() -- stores the user input in a var
local cl = string.lower(c) -- converts the stored input to lower case (for search reasons)
local cx, cy, cz, ch = tgps.getturtlepos() -- get current position using previous function
local pos = {cl,cx,cy,cz} -- makes a table
local cpos = textutils.serialize( pos ) -- turnes the table into a string
local file = fs.open("gpsc.txt" , "a") -- opens a file in append mode
file.writeLine(cpos) -- writes the string to the file
file.close() -- closes the file
once you use this code go to the turtle in the .minecraftsavesTest Worldcomputer directory you will find the file gpsc.txt inside the file should look like this
{[1]="home1",[2]=497,[3]=6,[4]=495,} "just like a table".
you can edit this file to add more points or move your turtle around and run the code.

Ok now to get the turtle to go to these positions



[left]print("Enter Destination")  -- prompts the user to input a name for current location[/left]
local goto = io.read()  -- retrieve user input and stores it in the goto var

local gotolower = string.lower(goto) -- converts input to lowercase


file = fs.open("gpsc.txt" , "r") -- opens file containing Coordinates

local i = 1
repeat -- repeats until user input is found
local line = file.readLine() -- reads a line from the file
pos = textutils.unserialize(line) -- converts the line into a table
i = i + 1
until pos[1] == gotolower -- when the pos table key 1 = user input


cx, cy, cz, ch = tgps.getturtlepos() -- one of my functions that gets the turtles position

if cx > pos[2] then -- If Current X is Greater than Stored X move West = 1 x goes minus
local steps = cx - pos[2] + 1 -- calculates how many spaces to move

if ch == 1 then   -- west
  local ch = 1
elseif ch == 2 then  -- north
  turtle.turnLeft()
  local ch = 1
elseif ch == 3 then  -- east  this if elseif  get the turtles current heading (way its facing) and turns it to the heading needed
  turtle.turnLeft()
  turtle.turnLeft()
  local ch = 1
elseif ch == 4 then  -- south
  turtle.turnRight()
  local ch = 1
end

local i = 1
repeat
  turtle.forward() -- Moves the turtle
  i=i+1
until i == steps
end

if cx < pos[2] then -- If Current X is Less than Stored X move East	= 3 x goes plus
local steps = pos[2] - cx + 1

if ch == 1 then   -- west
  turtle.turnLeft()
  turtle.turnLeft()
  local ch = 3
elseif ch == 2 then  -- north
  turtle.turnRight()
  local ch = 3
elseif ch == 3 then  -- east
  local ch = 3
elseif ch == 4 then  -- south
  turtle.turnLeft()
  local ch = 3
end

local i = 1
repeat
  turtle.forward()
  i=i+1
until i == steps
end


cx, cy, cz, ch = tgps.getturtlepos() -- get turtles current position and heading

if cz > pos[4] then -- If Current Z is Greater than Stored Z move North = 2  z goes minus
local steps = cz - pos[4] + 1

if ch == 1 then   -- west
  turtle.turnRight()
  local ch = 2
elseif ch == 2 then  -- north
  local ch = 2
elseif ch == 3 then  -- east
  turtle.turnLeft()
  local ch = 2
elseif ch == 4 then  -- south
  turtle.turnLeft()
  turtle.turnLeft()
  local ch = 2
end

local i = 1
repeat
  turtle.forward()
  i = i + 1
until i == steps
end

if cz < pos[4] then -- If Current Z is Less than Stored Z move South = 4 z gose +
local steps = pos[4] - cz + 1

if ch == 1 then   -- west
  turtle.turnLeft()
  local ch = 4
elseif ch == 2 then  -- north
  turtle.turnLeft()
  turtle.turnLeft()
  local ch = 4
elseif ch == 3 then  -- east
  turtle.turnRight()
  local ch = 4
elseif ch == 4 then  -- south
  local ch = 4
end

local i = 1
repeat
  turtle.forward()
  i = i + 1
until i == steps
end

file:close()
print("Arrived at Station")

When you run this code it prompts the user for a destination. Refering to our gpsc.txt file
{[1]="home1",[2]=497,[3]=6,[4]=495,}
the user keys in home1 The input gets converted to lower case. then it opens the gpsc.txt file and searches the file line by line until it finds the user input. it then compares the current position with the stored position and calculates how many steps to take and which heading to go it does west and east first then north and south second.

Thats about it I am not a programmer but I love to program I'm sure there is a better way to do this but it does work
any comments are alway welcome but please keep the snide remarks to yourself.

I'm working on more gps functionality so there will be more to come
xuma202 #2
Posted 15 July 2012 - 04:36 PM
Great work but the getturtlepos() function won't work if there is a block/mob in fronnt
eddieo #3
Posted 15 July 2012 - 05:22 PM
Great work but the getturtlepos() function won't work if there is a block/mob in fronnt
yea I know thats the next upgrade but thanks