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

turtle reboot during program

Started by enterprise0709, 30 April 2014 - 11:20 AM
enterprise0709 #1
Posted 30 April 2014 - 01:20 PM
(NOTE: i posted this again after i started this topic because i wasn't sure this one was send propperly, please delete the other one if it comes through for aproval, thank you!)

Hi there, i am writing a quarrying program (with the help of BSGSamuel's tutorial on youtube) that get's it's coordinates over rednet from a server. I am trying to write a feature that has the turtle send out a jobrequest again (using a broadcast) if it didn't get a reply in time, and gives up after several attempts.

However the turtle keeps "crashing" after a certain piece of code has been executed. No error code just a screen turning black.

Here's the code as it is (for troubleshooting purpose it's slightly modefied to work standallone, the error is still there though)


xDes = 0
yDes = 0
zDes = 0
orid = 0

t = 1

jobAv = true

function getJob()
  while jobAv do

	while  t < 6 do
	rednet.broadcast("JOBREQ")
	print("Jobrequest send, awaiting response from job server..")
	ID,Mess,Diss = rednet.receive(3)

		if ID == nil then
		print("request timeout "..t)
		t = t + 1
		else
		t = 6
		end

  end

  end

--no matter if the turtle got a reply or not, the turtle "crashes" after this point.
  
	if Mess == "YES" then
	print("reply received from: "..ID)	  
  
	ID,Mess,Diss = rednet.receive()
	xDes = Mess

	ID,Mess,Diss = rednet.receive()
	yDes = Mess
  
	ID,Mess,Diss = rednet.receive()
	zDes = Mess
  
	ID,Mess,Diss = rednet.receive()
	orid = Mess
	print("order received for X: "..xDes.." Y: "..yDes.." Z: "..zDes)

-- in the actual code a quarrying function would start running here using the just received coordinates
  
	elseif Mess == "NO" then
	print("no jobs available")
	  jobAv = false
  
	else
	print("no jobs available")
	  jobAv = false
  
	end
	print("FUNCTION COMPLETE")
  end

  rednet.open("right")  
  getJob()

what it SHOUD do:
1: send out a broadcast containing a string value ("JOBREQ")
2: any computer listening for the string value from 1 would reply and say wheater it got a quarrying job available ("YES" or "NO")
3: the turtle listens for several seconds for a reply. if it does not get a reply in time it will send the broadcast again
4: turtle will try 6 times before giving up, changing jobAv to false and thus ending the function
5: if the reply is "YES" it will start listening for the X Y Z coordinates and desired orientation respectively
6: a quarrying function will be executed
7: turtle will repeat untill it get's no more reply or a "NO" for an answere, both will end the function

hope some one can help me out with this! :)/>
Edited on 30 April 2014 - 12:58 PM
CometWolf #2
Posted 30 April 2014 - 04:42 PM
Let's see if i can't explain to you what your code is actually doing here

  while jobAv do --this variable is currently set to true, so it loops
		while  t < 6 do --a counter for amount of receiver tries, you'd probably be better off with a for loop, but anyways...
		rednet.broadcast("JOBREQ")
		print("Jobrequest send, awaiting response from job server..")
		ID,Mess,Diss = rednet.receive(3)
				if ID == nil then
				print("request timeout "..t)
				t = t + 1
				else --we got a message, this loop ends, and won't run again until t has benn reset
				t = 6
				end
  end
  end --this loop however will just keep running, and not really do anything, since jobAv is still set to true
Although normally this should result in an error along the lines of "Failed to yield" or something like that, since a constant loop not doing anything, does obviously not yield.
Edited on 30 April 2014 - 02:43 PM
enterprise0709 #3
Posted 30 April 2014 - 06:29 PM
FOUND IT!

took the previous version of the program that did not have a time out feature and tried again. now it now works like a charm!

here's the code:

function getJob()

t = 1
  
print("sending jobrequest, listening for reply from server..")

while jobAv do

  while t < 5 do
rednet.broadcast("JOBREQ")
ID,Mess,Diss = rednet.receive(10)
   if ID == nil then
   t = t + 1
	 print("request timeout")
   else
   t = 5
   end
  
   if t == 5 and ID == nil then
   Mess = "EMPTY"
   end
  
   end
  
	if Mess == "YES" then
	print("reply received from: "..ID)	  
  
	ID,Mess,Diss = rednet.receive()
	xDes = Mess
  
	ID,Mess,Diss = rednet.receive()
	yDes = Mess
  
	ID,Mess,Diss = rednet.receive()
	zDes = Mess
  
	ID,Mess,Diss = rednet.receive()
	orid = Mess
	print("order received for X: "..xDes.." Y: "..yDes.." Z: "..zDes)
  
--quarry function
  
	elseif Mess == "NO" then
	  print("No jobs available")
	  jobAv = false
  
	else
	  print("ERROR no jobservers available! boot up a jobserver	on  a computer with rednet and try again")
	  jobAv = false
	end
  end
end

Houray for backups!
strangely enough. it seems like im doing the EXACT SAME THING the second time!
Edited on 30 April 2014 - 04:32 PM
electrodude512 #4
Posted 30 April 2014 - 10:43 PM
Although normally this should result in an error along the lines of "Failed to yield" or something like that, since a constant loop not doing anything, does obviously not yield.
His code does yield - the rednet.receive function contains an os.pullEvent("rednet_message") call.
CometWolf #5
Posted 30 April 2014 - 11:06 PM
Im just gonna assume you didn't bother to read my comments, like the OP >.>
Bomb Bloke #6
Posted 01 May 2014 - 02:32 AM
FWIW I read them, and you're exactly right - that last "end" you commented is linked to the "while jobAv do" loop, which will repeat forever and cause a failure to yield (meaning the "end" should be moved further down). "t" will max out on the first iteration of that loop, so on subsequent iterations there'll be no calls to rednet.receive().

Congrats on the quads, by the way. ;)/>

Failure to yield generally forces a system to shut down (requiring the user the exit the interface and manually right-click the device in order to boot it back up). Whether there's time for the error to be visibly rendered (let alone read!) is a bit random, as apparently there's no pause between displaying it and turning off.
Dog #7
Posted 01 May 2014 - 02:56 AM
…Failure to yield generally forces a system to shut down (requiring the user the exit the interface and manually right-click the device in order to boot it back up). Whether there's time for the error to be visibly rendered (let alone read!) is a bit random, as apparently there's no pause between displaying it and turning off.
Good stuff; probably explains quite a few of the unknown shutdowns I've experienced when troubleshooting projects - thanks, Bomb Bloke :)/>

And, yes, congrats on breaking 1K, ComeWolf!
Edited on 01 May 2014 - 01:07 AM