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

Advanced GPS goto program with position file

Started by Cakejoke, 06 October 2012 - 06:19 PM
Cakejoke #1
Posted 06 October 2012 - 08:19 PM
What I did is I wrote a program for moving a turtle to given coordinates and after that rotates it to an optionally given direction.

The cool thing is that it saves the turtle's coordinates to a data/pos.txt file after every move, enabling it to work even when there are no GPS hosts in range. The postion just has to be synced once using the "syncpos" program and then you can use "goto" + x, y, z coordinates (+ direction (optional, must be between 0 and 3)).

The code:
goto:
Spoiler
local tArgs = {...}
local x, y, z, f
local xCur, yCur, zCur, fCur
local xDif, yDif, zDif
local posIn, posOut, pos

local function syncFile()
  posOut = io.open("data/pos.txt", "w")
  posOut:write(textutils.serialize({xCur, yCur, zCur, fCur}))
  posOut:close()
end

local function abort(msg)
  syncFile()
  print("ERROR: "..msg)
  do return end
end

local function up(n)
  for i = 1, n do
	while not turtle.up() do
	  turtle.digUp()
	end
	yCur = yCur + 1
	syncFile()
  end
end

local function down(n)
  for i = 1, n do
	while not turtle.down() do
	  turtle.digDown()
	end
	yCur = yCur - 1
	syncFile()
  end
end

local function moveF(dir, n)
  while fCur ~= dir do
	turtle.turnLeft()
	if dir < 0 or dir > 3 then
	  do return end
	end
	if fCur ~= 0 then
	  fCur = fCur - 1
	else
	  fCur = 3
	end
	syncFile()
  end
  for i = 1, n do
	while not turtle.forward() do
	  up(1)
	end
	if fCur == 0 then
	  zCur = zCur + 1
	elseif fCur == 1 then
	  xCur = xCur - 1
	elseif fCur == 2 then
	  zCur = zCur - 1
	elseif fCur == 3 then
	  xCur = xCur + 1
	else
	  abort("invalid state for fCur")
	end
	syncFile()
  end
end

if not tArgs[1] or not tArgs[2] or not tArgs[3] then
  print("Usage: goto <x> <y> <z> [<f>]")
  do return end
end

x = tonumber(tArgs[1])
y = tonumber(tArgs[2])
z = tonumber(tArgs[3])

if tArgs[4] then
  f = tonumber(tArgs[4])
end

posIn = io.open("data/pos.txt", "r")
pos = posIn:read()
posIn:close()

pos = textutils.unserialize(pos)

xCur = tonumber(pos[1])
yCur = tonumber(pos[2])
zCur = tonumber(pos[3])
fCur = tonumber(pos[4])

xDif = x - xCur
zDif = z - zCur

if xDif &amp;lt; 0 then
  xDif = xDif * -1
  moveF(1, xDif)
else
  moveF(3, xDif)
end

if zDif &amp;lt; 0 then
  zDif = zDif * -1
  moveF(2, zDif)
else
  moveF(0, zDif)
end

yDif = y - yCur

if yDif &amp;lt; 0 then
  yDif = yDif * -1
  down(yDif)
else
  up(yDif)
end

if f then
  moveF(f, 0)
end

syncFile()

Pastebin link: http://pastebin.com/Ti7a1wVD

syncpos:
Spoiler
local xIni, yIni, zIni
local xCur, yCur, zCur
local fCur
local posOut

xIni, yIni, zIni = gps.locate(5)

if not xIni or not yIni or not zIni then
  print("No or insufficient GPS hosts found")
  do return end
end

while not turtle.forward() do
  while not turtle.up() do
	turtle.digUp()
  end
end

xCur, yCur, zCur = gps.locate(5)
if zCur &amp;gt; zIni then
  fCur = 0
elseif xCur &amp;lt; xIni then
  fCur = 1
elseif zCur &amp;lt; zIni then
  fCur = 2
elseif xCur &amp;gt; xIni then
  fCur = 3
else
  exit()
end

while not turtle.back() do
  while not turtle.up() do
	turtle.digUp()
  end
end

xCur, yCur, zCur = gps.locate(5)


if not fs.isDir("data") then
fs.delete("data")
fs.makeDir("data")
end

posOut = io.open("data/pos.txt", "w")
posOut:write(textutils.serialize({xCur, yCur, zCur, fCur}))
posOut:close()

Pastebin link: http://pastebin.com/k3B7tUKH

Update notes:
- syncpos now exits without an error if there are no GPS hosts, just returns a message
- added code to syncpos to automatically create a data folder if there is none present, be careful not to name a file "data", syncpos will delete it
- more bugfixing

Planned additions:
- builing program for GPS satellites, without need to input coordinates
- advanced excavate with input for exact position and size

If you are interested in how the programs work, feel free to ask and I'll add an explanation to this post.</f></z></y></x>
jag #2
Posted 06 October 2012 - 09:12 PM
Cool! Could be useful to use this system in a excavate program, but ofc you'll have to write your own program.. But still, very cool!
Cakejoke #3
Posted 06 October 2012 - 09:17 PM
Cool! Could be useful to use this system in a excavate program, but ofc you'll have to write your own program.. But still, very cool!
That shouldn't be too hard.

*adds GPS excavate to planned additions*
jag #4
Posted 06 October 2012 - 09:34 PM
That would really be cool, so you don't have to restart the robot each time you exit the world (or when the server reboots on a server)
Cakejoke #5
Posted 06 October 2012 - 10:07 PM
That would really be cool, so you don't have to restart the robot each time you exit the world (or when the server reboots on a server)
Well, it would save its coordinates, but it wouldn't continue with the program after a reload. For that you'd have to create another file with the saved progress and a startup script to read this and continue based on the information in the file. That would be quite tricky to code.
BlackoutIsHere #6
Posted 07 October 2012 - 02:00 AM
That would really be cool, so you don't have to restart the robot each time you exit the world (or when the server reboots on a server)
Well, it would save its coordinates, but it wouldn't continue with the program after a reload. For that you'd have to create another file with the saved progress and a startup script to read this and continue based on the information in the file. That would be quite tricky to code.
If it is just a simple loop you can execute a goto command in the startup file right?
gloin366 #7
Posted 10 October 2012 - 08:56 PM
I have a problem at executing the code. The error code sounds like this:
bios:[string "goto]:42:
'then' expected.

What to do?
Cakejoke #8
Posted 12 October 2012 - 06:12 PM
I have a problem at executing the code. The error code sounds like this:
bios:[string "goto]:42:
'then' expected.

What to do?
If you took the code out of the spoilers, you should try again, I might have fixed it now.
ChunLing #9
Posted 12 October 2012 - 11:20 PM
I have a program that allows a turtle to be remotely controlled, and it can get it's GPS and send that back to the controller. So if you set the turtle up with a startup file that started the remote control (and there is a function that allows that to be done from the control terminal), then you could program the controller to automatically look for turtles and start/continue controlling them according to some master plan.
johnnyjr100 #10
Posted 13 October 2012 - 02:42 PM
For the goto part i get the error bios:206: [string "goto"]:11:
'<eof>' expected
i cannot find any reason it should be crshing
Cakejoke #11
Posted 13 October 2012 - 07:42 PM
I have a program that allows a turtle to be remotely controlled, and it can get it's GPS and send that back to the controller. So if you set the turtle up with a startup file that started the remote control (and there is a function that allows that to be done from the control terminal), then you could program the controller to automatically look for turtles and start/continue controlling them according to some master plan.
Haha, that's funny, I also have a remote control program for the turtle, wrote it before this. A potential problem of this though, is that the controller has only a limited range and my GPS program is designed to work out of range of a GPS station. Maybe I should build a feature into my remote-control program to turn on manuel control as soon as the turtle gets out of range.

For the goto part i get the error bios:206: [string "goto"]:11:
'<eof>' expected
i cannot find any reason it should be crshing
Hmm, no idea why this could happen. Did you just use the pastebin program to get the code, or did you copy it from the spoiler? Because for me it seems to work fine.
ChunLing #12
Posted 13 October 2012 - 11:08 PM
You can also have turtles act as gps nodes, like so:

gpshst = function()
local rqstr,mssgrq = nil
local x,y,z = gps.locate(2)
repeat
  rqstr,mssgrq = rednet.receive()
  if mssgrq == "PING" then rednet.send(rqstr, textutils.serialize({x,y,z}))
  elseif cldt.rcID == rqstr and mssgrq == "gpshst" then x = nil end
until not x
end
you call the function when you get near the edge of the gps range, then when you don't need it anymore you send "gpshst" to that turtle again to turn it off again. Or you can put up a proper GPS array, I have a function for that but it's longer and needs the stuff to place the array.

I know, you're going for full inertial guidance. But you've apparently already got that done, right? You just need to track the facing of the turtle and correctly increment/decrement the relevant axis after each successful move.
Blockeh #13
Posted 14 October 2012 - 02:19 AM
Good Code:) Adding a Bomb-Drop to use on servers.
Doyle3694 #14
Posted 14 October 2012 - 11:20 AM
Maybe make syncpos available for arguments even? so that you can give it arguements with it's X,Y,Z and P?
Cakejoke #15
Posted 14 October 2012 - 01:28 PM
I know, you're going for full inertial guidance. But you've apparently already got that done, right? You just need to track the facing of the turtle and correctly increment/decrement the relevant axis after each successful move.
Yeah, my program changes the variables after every move and writes the changes to the file immediately. But when you do a manuel "go forward", for example, you'll have to give it the correct new coordinates. I wonder if I write my own go and turn programs, will it use this or the program from ROM? Let's try…

Maybe make syncpos available for arguments even? so that you can give it arguements with it's X,Y,Z and P?
I could do that, but when you now the turtle's coordinates, you can also just open the file and change the values yourself.
{[1]=x,[2]=y,[3]=z,[4]=f,}
ChunLing #16
Posted 15 October 2012 - 06:27 AM
Yeah…that's kinda defeating the point of automation, though. You could make a function for when you can't get a good GPS fix that issues a PING and tries to move toward the location of the responding host nearest to the last known position. Or even give it homing, so that it tracks whether it is moving closer or further from the host, and can calculate whether it should try turning or reversing direction. Then once it is getting returns from four hosts, it can set it's location.
evilguard #17
Posted 19 October 2012 - 05:25 AM
Hey everyone,

I have something to ask if you could add it to the programs that would be cool. if not then… ho well :P/>/>

I'm actually playing a server and as far as i could love a goto programs it seem maybe "too simple". What i would love is a GoTo programs that could not only register position but also ask for what to do. hums… well its hard to explain more since i'm not english. Lets me give you an exemple :

I have 2 friend home with in the radius of my gps sattellite, some farm, some chest, a quarry… So what i would love is to use my turtle as a retriever/delivery system. So if i want to send my friend a ressource but i am working i can load the turtle and send it to deliver to a chest in my friend base and then come back. If i want to take any ressource from my farm/quarry i could just sent it to a position and then he could suck item and drop it into the sorting system.

The turtle would be set with some basic question as :

Where to go? Input gps location
What to do? Suck or drop?
Wich side? do i drop in front, under or on top?
Then? come home and drop item in a chest and wait for news commands.

I hope i was enought clear about that and that anyone may want to create it or that programs could be update witht that idea :)/>/>

Take care
EViLGUARD
ChunLing #18
Posted 19 October 2012 - 07:55 AM
Wouldn't it be easier and more efficient to just use a train of chestcarts pushed by a powered cart?

But yeah, I've got a program that can basically do that, though it doesn't use save positions and stuff. And it seems like cakewalk's should be able to do it pretty easily too.
evilguard #19
Posted 19 October 2012 - 06:17 PM
Well if anyone could do it i'm sure its on that forum that i will find it heh. But yes since Cake already make it that far i think he would be the best to do it since its more "adding" to the code then building it from the basic.

Its kinda like the Assembly cart but for turtle :P/>/>
Chessehead17 #20
Posted 13 December 2012 - 04:01 PM
so is this dead or or what? because it dosen;t seem to be working with computercraft for MC 1.4.5