Jump to content




Turtle turning to a certain direction


  • You cannot reply to this topic
11 replies to this topic

#1 popdog15

  • Members
  • 82 posts

Posted 09 May 2015 - 05:44 AM

All right. I'm working on a to be storage program that counts items and can pull them out of storage using a network of turtles. So far I have written a framework for the turtle, and after that I will be writing a server and client. My turtle code is currently: Code

My problem lies at line 257 and later upon calling it at 277. I'm telling the turtle to get an item to the east when it's facing north, and in theory, it should just turn left 3 times and then stop, but it doesn't. It just keeps on turning no matter what. No idea why. Maybe the current 'pos' is not accurate? I don't know.



Previous issue that is resolved.
Spoiler

Edited by popdog15, 10 May 2015 - 06:56 AM.


#2 Creator

    Mad Dash Victor

  • Members
  • 2,168 posts
  • LocationYou will never find me, muhahahahahaha

Posted 09 May 2015 - 07:57 AM

You don't need to flush. Closing files is enough.

#3 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 09 May 2015 - 08:09 AM

Flush simply ensures that any data you've queued to be written to disk, gets written to disk before the script goes any further. Normally data you've requested be written goes into a buffer to be actually written when the VM thinks it's worth kicking the HDD into action (sending one big write request is more efficient than ordering lots of little ones). Closing a file automatically flushes it, but, in cases where you think your script might crash in between your write commands and your close call, you can request it manually. Of course, if you've simply opened the file and haven't made any write calls yet, flushing does nothing (and in cases where you're closing the files directly after ordering writes, it effectively does nothing).

It seems you never define "newF". The fourth line of your "pos" file will quickly become blank, and "f" will thus become an empty string.

Frankly my recommendation is to forget about having your turtles "remember" their states in between boots, and simply code them to figure it out each time they boot up based on a GPS setup. There's no point in flogging your hard drive when you'll never move out of range of a satellite system.

Here's an example of the sort of code most all of my turtle scripts execute on startup:

local pos, facings = {}, {"north","east","south","west"}

-- Ping the GPS servers until I get a valid reading:
do
        local tempx, tempy, tempz
        while tempx == nil or tempy == nil or tempz == nil do
                tempx, tempy, tempz = gps.locate(5)
                sleep(5)
        end

        while not turtle.forward() do while not turtle.turnLeft() do end end
        pos.x,pos.y,pos.z = gps.locate(5)

        if pos.x < tempx then pos.direction = 4
        elseif pos.x > tempx then pos.direction = 2
        elseif pos.z < tempz then pos.direction = 1
        else pos.direction = 3 end
end

print("I'm at "..pos.x..","..pos.y..","..pos.z..", I have "..turtle.getFuelLevel().." fuel and I'm facing "..facings[pos.direction]..".")

I then just have them update the variables in the pos table as they move around.

#4 popdog15

  • Members
  • 82 posts

Posted 09 May 2015 - 03:28 PM

View PostBomb Bloke, on 09 May 2015 - 08:09 AM, said:

Flush simply ensures that any data you've queued to be written to disk, gets written to disk before the script goes any further. Normally data you've requested be written goes into a buffer to be actually written when the VM thinks it's worth kicking the HDD into action (sending one big write request is more efficient than ordering lots of little ones). Closing a file automatically flushes it, but, in cases where you think your script might crash in between your write commands and your close call, you can request it manually. Of course, if you've simply opened the file and haven't made any write calls yet, flushing does nothing (and in cases where you're closing the files directly after ordering writes, it effectively does nothing).

It seems you never define "newF". The fourth line of your "pos" file will quickly become blank, and "f" will thus become an empty string.

Frankly my recommendation is to forget about having your turtles "remember" their states in between boots, and simply code them to figure it out each time they boot up based on a GPS setup. There's no point in flogging your hard drive when you'll never move out of range of a satellite system.

Here's an example of the sort of code most all of my turtle scripts execute on startup:

local pos, facings = {}, {"north","east","south","west"}

-- Ping the GPS servers until I get a valid reading:
do
		local tempx, tempy, tempz
		while tempx == nil or tempy == nil or tempz == nil do
				tempx, tempy, tempz = gps.locate(5)
				sleep(5)
		end

		while not turtle.forward() do while not turtle.turnLeft() do end end
		pos.x,pos.y,pos.z = gps.locate(5)

		if pos.x < tempx then pos.direction = 4
		elseif pos.x > tempx then pos.direction = 2
		elseif pos.z < tempz then pos.direction = 1
		else pos.direction = 3 end
end

print("I'm at "..pos.x..","..pos.y..","..pos.z..", I have "..turtle.getFuelLevel().." fuel and I'm facing "..facings[pos.direction]..".")

I then just have them update the variables in the pos table as they move around.
Oh. I thought it flush purged the file of it's contents. I changed it around so that it deletes the 'pos' file every time and just re-writes it, and with that, it works now.

Edited by popdog15, 09 May 2015 - 03:29 PM.


#5 valithor

  • Members
  • 1,053 posts

Posted 09 May 2015 - 03:48 PM

View Postpopdog15, on 09 May 2015 - 03:28 PM, said:

View PostBomb Bloke, on 09 May 2015 - 08:09 AM, said:

Flush simply ensures that any data you've queued to be written to disk, gets written to disk before the script goes any further. Normally data you've requested be written goes into a buffer to be actually written when the VM thinks it's worth kicking the HDD into action (sending one big write request is more efficient than ordering lots of little ones). Closing a file automatically flushes it, but, in cases where you think your script might crash in between your write commands and your close call, you can request it manually. Of course, if you've simply opened the file and haven't made any write calls yet, flushing does nothing (and in cases where you're closing the files directly after ordering writes, it effectively does nothing).

It seems you never define "newF". The fourth line of your "pos" file will quickly become blank, and "f" will thus become an empty string.

Frankly my recommendation is to forget about having your turtles "remember" their states in between boots, and simply code them to figure it out each time they boot up based on a GPS setup. There's no point in flogging your hard drive when you'll never move out of range of a satellite system.

Here's an example of the sort of code most all of my turtle scripts execute on startup:

local pos, facings = {}, {"north","east","south","west"}

-- Ping the GPS servers until I get a valid reading:
do
		local tempx, tempy, tempz
		while tempx == nil or tempy == nil or tempz == nil do
				tempx, tempy, tempz = gps.locate(5)
				sleep(5)
		end

		while not turtle.forward() do while not turtle.turnLeft() do end end
		pos.x,pos.y,pos.z = gps.locate(5)

		if pos.x < tempx then pos.direction = 4
		elseif pos.x > tempx then pos.direction = 2
		elseif pos.z < tempz then pos.direction = 1
		else pos.direction = 3 end
end

print("I'm at "..pos.x..","..pos.y..","..pos.z..", I have "..turtle.getFuelLevel().." fuel and I'm facing "..facings[pos.direction]..".")

I then just have them update the variables in the pos table as they move around.
Oh. I thought it flush purged the file of it's contents. I changed it around so that it deletes the 'pos' file every time and just re-writes it, and with that, it works now.

Just so you know by opening a file in write mode using the fs api (fs.open(file,"w")) it will delete the contents automatically. The only reason the contents are not being deleted when you open it is because you are using append mode which adds the information to the end.

So instead of deleting the file you can just do this:
local h = fs.open(file,"w") -- because it is opened in "w" mode it will delete all contents
h.writeLine(stuff)
h.close()

Append mode or "a" mode will just add whatever you want to write to the end of the file.

#6 popdog15

  • Members
  • 82 posts

Posted 09 May 2015 - 04:31 PM

View Postvalithor, on 09 May 2015 - 03:48 PM, said:

View Postpopdog15, on 09 May 2015 - 03:28 PM, said:

View PostBomb Bloke, on 09 May 2015 - 08:09 AM, said:

Flush simply ensures that any data you've queued to be written to disk, gets written to disk before the script goes any further. Normally data you've requested be written goes into a buffer to be actually written when the VM thinks it's worth kicking the HDD into action (sending one big write request is more efficient than ordering lots of little ones). Closing a file automatically flushes it, but, in cases where you think your script might crash in between your write commands and your close call, you can request it manually. Of course, if you've simply opened the file and haven't made any write calls yet, flushing does nothing (and in cases where you're closing the files directly after ordering writes, it effectively does nothing).

It seems you never define "newF". The fourth line of your "pos" file will quickly become blank, and "f" will thus become an empty string.

Frankly my recommendation is to forget about having your turtles "remember" their states in between boots, and simply code them to figure it out each time they boot up based on a GPS setup. There's no point in flogging your hard drive when you'll never move out of range of a satellite system.

Here's an example of the sort of code most all of my turtle scripts execute on startup:

local pos, facings = {}, {"north","east","south","west"}

-- Ping the GPS servers until I get a valid reading:
do
		local tempx, tempy, tempz
		while tempx == nil or tempy == nil or tempz == nil do
				tempx, tempy, tempz = gps.locate(5)
				sleep(5)
		end

		while not turtle.forward() do while not turtle.turnLeft() do end end
		pos.x,pos.y,pos.z = gps.locate(5)

		if pos.x < tempx then pos.direction = 4
		elseif pos.x > tempx then pos.direction = 2
		elseif pos.z < tempz then pos.direction = 1
		else pos.direction = 3 end
end

print("I'm at "..pos.x..","..pos.y..","..pos.z..", I have "..turtle.getFuelLevel().." fuel and I'm facing "..facings[pos.direction]..".")

I then just have them update the variables in the pos table as they move around.
Oh. I thought it flush purged the file of it's contents. I changed it around so that it deletes the 'pos' file every time and just re-writes it, and with that, it works now.

Just so you know by opening a file in write mode using the fs api (fs.open(file,"w")) it will delete the contents automatically. The only reason the contents are not being deleted when you open it is because you are using append mode which adds the information to the end.

So instead of deleting the file you can just do this:
local h = fs.open(file,"w") -- because it is opened in "w" mode it will delete all contents
h.writeLine(stuff)
h.close()

Append mode or "a" mode will just add whatever you want to write to the end of the file.
Oh. All righty then, thanks.

Edited by popdog15, 09 May 2015 - 04:31 PM.


#7 popdog15

  • Members
  • 82 posts

Posted 10 May 2015 - 06:59 AM

Bump as of edit? (If allowed?)

Edited by popdog15, 10 May 2015 - 06:59 AM.


#8 flaghacker

  • Members
  • 655 posts

Posted 10 May 2015 - 07:32 AM

View Postpopdog15, on 10 May 2015 - 06:59 AM, said:

Bump as of edit? (If allowed?)

Pleaso don't edit your original post, just write a new one. It's going to become a mess like this.

Edited by flaghacker, 10 May 2015 - 07:32 AM.


#9 popdog15

  • Members
  • 82 posts

Posted 10 May 2015 - 08:11 AM

View Postflaghacker, on 10 May 2015 - 07:32 AM, said:

View Postpopdog15, on 10 May 2015 - 06:59 AM, said:

Bump as of edit? (If allowed?)

Pleaso don't edit your original post, just write a new one. It's going to become a mess like this.
I thought this was better than flooding the "Ask a Pro" board, but all right.

#10 flaghacker

  • Members
  • 655 posts

Posted 10 May 2015 - 09:01 AM

View Postpopdog15, on 10 May 2015 - 08:11 AM, said:

View Postflaghacker, on 10 May 2015 - 07:32 AM, said:

View Postpopdog15, on 10 May 2015 - 06:59 AM, said:

Bump as of edit? (If allowed?)

Pleaso don't edit your original post, just write a new one. It's going to become a mess like this.
I thought this was better than flooding the "Ask a Pro" board, but all right.

No no, don't flood the Ask a pro board! I meant that you should post a new reply to this topic. Something like

Quote

Thanks, my problem is resolved! But now I'm getting this error:
...

Edited by flaghacker, 10 May 2015 - 09:01 AM.


#11 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 10 May 2015 - 11:04 AM

View Postpopdog15, on 09 May 2015 - 05:44 AM, said:

My problem lies at line 257 and later upon calling it at 277. I'm telling the turtle to get an item to the east when it's facing north, and in theory, it should just turn left 3 times and then stop, but it doesn't. It just keeps on turning no matter what. No idea why. Maybe the current 'pos' is not accurate? I don't know.

So around line 257, we have:

function figureOutTurn(newf)
        while f ~= newf do
        turtle.turnLeft()
        end
end

... a loop that'll repeat so long as f isn't newf. And since it never bothers to change those variables, it's indeed going to keep going for quite a while...

Did you intend to call your changeTurtle() function from there?

#12 popdog15

  • Members
  • 82 posts

Posted 10 May 2015 - 03:08 PM

View PostBomb Bloke, on 10 May 2015 - 11:04 AM, said:

View Postpopdog15, on 09 May 2015 - 05:44 AM, said:

My problem lies at line 257 and later upon calling it at 277. I'm telling the turtle to get an item to the east when it's facing north, and in theory, it should just turn left 3 times and then stop, but it doesn't. It just keeps on turning no matter what. No idea why. Maybe the current 'pos' is not accurate? I don't know.

So around line 257, we have:

function figureOutTurn(newf)
		while f ~= newf do
		turtle.turnLeft()
		end
end

... a loop that'll repeat so long as f isn't newf. And since it never bothers to change those variables, it's indeed going to keep going for quite a while...

Did you intend to call your changeTurtle() function from there?
Oh, yeah, that'd be the problem. I'm silly sometimes, thanks.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users