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

gps movement system, trouble with the y coordinate

Started by birdini, 05 April 2014 - 01:48 AM
birdini #1
Posted 05 April 2014 - 03:48 AM
Hey guys, here's the thing…I need a favor. I've been testing this code for days now and can't seem to figure out a solution. I need a fresh set of eyes to look at this thing. I've designed it to be an api as I intend to use it for turtle navigation later. The issue occurs when I tell the turtle to go to a specific y coordinate(testing on an "overworld" preset world). Instead of going to the y coordinate I designate, it just goes back to ground level. The code will not throw any errors at you, it just doesn't work properly. I don't know why.

Here's the code:

--0 = south, 1 = west, 2 = north, 3 = east
--x = +east,-west
--z = -north,+south
--[[Global Variables]]--
xpos,ypos,zpos = gps.locate(10)
heading = 0

--[[functions]]--
function turnLeft()
turtle.turnLeft()
heading = heading - 1
    if heading == -1 then heading = 3 end
end
function turnRight()
turtle.turnRight()
heading = heading + 1
if heading == 4 then heading = 0 end
end
function faceDir(h)
if h >= 4 then h = 0 end
repeat turnRight() until heading == h
end
function forward(x)
local i = nil
x=tonumber(x)
for i=1,x do
turtle.forward()
gps.locate(10)
end
end
function up(x)
local i = nil
x=tonumber(x)
for i=1,x do
if not turtle.detectUp() then do
turtle.up()
end
xpos,ypos,zpos=gps.locate(10)
end
end
end
function down(x)
local i = nil
x=tonumber(x)
for i=1,x do
if not turtle.detectDown() then do turtle.down() end
xpos,ypos,zpos=gps.locate(10)
end
end
end
function checkFuel(x)
if turtle.getFuelLevel() > x then return false end
return true
end
function refuel(x)
turtle.select(16)
turtle.placeUp()
turtle.suckUp(x)
turtle.digUp()
turtle.select(1)
end
function goto(x,y,z)
for i = 1,10 do turtle.up() end
xpos,ypos,zpos=gps.locate(10)
if (xpos - x) > 0 then faceDir(1) else faceDir(3) end
if checkFuel(math.abs(xpos-x)) then do refuel() end end
    print("x - "..(math.abs(xpos-x)))
    forward(math.abs(xpos-x))
    xpos = xpos-x
if (zpos - z) > 0 then faceDir(2) else faceDir(0) end
    print("z - "..(math.abs(zpos-z)))
    forward(math.abs(zpos-z))
    zpos=zpos-z
print("facing direction: "..heading)
--for b=1,10 do turtle.down() end
print("moving y - "..(math.abs(ypos-y)))
if (ypos - y) > 0 then do up(math.abs(ypos-y)) end end
if (ypos - y) < 0 then do down(math.abs(ypos-y)) end end
xpos,ypos,zpos=gps.locate(10)
end

I realize that some of it may be redundant / could be done better. I'm just looking to get it working first, then I'll clean it up. The turtle navigates to the x and z coordinates just fine as far as I have tested. The turtle does do an additional unintended turn when "goto" calls the "faceDir" function, just fyi. I'd like to focus on the navigational problem though.

The only requirement for the program to function correctly is for the turtle to face south before you start calling functions. it will store any directional changes in the global "heading" variable. I am running the beta version of computercraft(1.6.1) and minecraft(1.6.4) if that helps.

I'd like to thank you all in advance for your time. Cheers!
guesswhat123212 #2
Posted 05 April 2014 - 04:13 AM
do you have this turtle on the ground when you tell it this or in the air? I ask because at the beginning of your goto function you tell the turtle to go up 10 times and then get its x,y,z from gps, but near the end you have it go down 10spaces before it checks ypos -y.
I would try putting

xpos,ypos,zpos=gps.locate(10)

one line higher


edit: also if anything is in your turtles way it will mess up its movement and its coord's (at least until you call gps.locate again.
Edited on 05 April 2014 - 02:17 AM
birdini #3
Posted 05 April 2014 - 04:01 PM
Thanks for the reply.
The "forward" function updates the position as the turtle moves so it shouldn't matter how many gps.locate()s I throw in. I'll try it to see if it makes a difference though.

I've tried starting the turtle on the ground and in the air, I didn't notice a difference.

Several lines of code were commented out for debug purposes, watch out for "–"

I have tested the turtle in an environment where it won't run into anything except the ground( which is the reason i made this post) the.x and z coordinates work just fine, it's the y coordinate I'm having trouble with

Please feel free to do whatever you want to this code, I'm only interested in getting it working.
Edited on 05 April 2014 - 02:04 PM
guesswhat123212 #4
Posted 05 April 2014 - 04:10 PM
I did not notice that you called gps.locate during your forward, however you do not tell it to assign the values, so where you are calling it you need to change it to

xpos,ypos,zpos = gps.locate()
Edited on 05 April 2014 - 02:10 PM
birdini #5
Posted 05 April 2014 - 05:16 PM
*headdesk* thank you for spotting that, I'll fix that right away. It shouldn't have any effect on the up/down motion of the turtle though… Bah, It would be much simpler if it were just errors, lol
birdini #6
Posted 05 April 2014 - 05:43 PM
Hah, I can't believe I missed that, thanks, I'll fix that right away. That shouldn't effect the up/down motion of the turtle though… Bah, this would be so much easier if it was just errors, lol

Hah, I can't believe I missed that, thanks, I'll fix that right away. That shouldn't effect the up/down motion of the turtle though… Bah, this would be so much easier if it was just errors, lol