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

Security loopback not funtioning

Started by jeroentjuh99, 12 May 2013 - 04:24 AM
jeroentjuh99 #1
Posted 12 May 2013 - 06:24 AM
Hi,
I created a mining machine with CC. It is controlled by one Master Computer and 15 turtles. I coded everything with timing, but the time the turtles need is variable. When the time the turtles need >> the time the computer is coded, everything gets clogged up.

That's why I changed the coding. (When the turtles are done, they send a security message back, and when the computer has 15 messages, it does the next loop). The problem is that I don't know what is wrong with the code.

This is my code:
Computer:
Autocycle:

while true do
  if redstone.getInput("top") == true then
	shell.run("cycle2")
  end
  sleep(2)
end

cycle2:

local dingetje = 0
function continue()
rednet.open("right")
while true do
senderID, message, distance = rednet.receive()
dingetje = dingetje + 1
if dingetje == 15 then
dingetje = 0
shell.run("cycle2")
end
end
end
local tArgs={...}
if tArgs[1] == nil then tArgs[1] = 1 end
for i = 1, tArgs[1] do
  shell.run("move")
  sleep(1)
  shell.run("command")
  print("Iteration: "..i)
end
continue()


Turtles:
AwaitCommand:

rednet.open("right")
while true do
  senderID, message, distance = rednet.receive()
  print(message)
  shell.run(message)
end

Mine:

function deploy()
  turtle.select(1)
  turtle.place()
end
function clearInv()
  for i = 1,16 do
	turtle.select(i)
	turtle.dropDown()
   end
   turtle.select(1)
end
function getMiner()
  turtle.select(1)
  while turtle.getItemCount(1) > 0 do
	clearInv()
	sleep(1)
  end
  turtle.dig()
end
deploy()
sleep(3)
clearInv()
sleep(3)
clearInv()
getMiner()
rednet.open("right")
rednet.send(21, "done")

Can someone help me?
Note: I labeled all of my turtles to q1, maybe that wasn't a real smart thing to do…
Note2: Rednet gets opened in two separate programs for the turtles.
Lyqyd #2
Posted 12 May 2013 - 04:59 PM
Split into new topic.
KaoS #3
Posted 12 May 2013 - 06:20 PM
please tell us the issue you are experiencing… is the code giving an error? what is happening?

also if you are using creative mode check that you haven't accidentally made multiple turtles with the same ID, if you have they cannot message eachother
jeroentjuh99 #4
Posted 13 May 2013 - 03:33 AM
please tell us the issue you are experiencing… is the code giving an error? what is happening?

also if you are using creative mode check that you haven't accidentally made multiple turtles with the same ID, if you have they cannot message eachother
Well, I don't get any errors. I don't see anything happening. As far as I know, the turtles doesn't sent a rednet-message to the computer, and the computer is waiting for 15 times the message. Or, the computer isn't counting it properly and waits untill he got 15 messages before doing anything else.
The turtles have the same ID, but they don't need to message eachother, everything works fine if the computer needs to send a signal to the turtles.
KaoS #5
Posted 13 May 2013 - 07:48 AM
I see. After reading through your code I notice that you did not include the "move" or "command" file, please include them if the following does not work

In your program you use quite a few sleep() calls, this is a very bad idea. if you receive a message while sleeping that message is missed. You do not need to sleep to yield if you are rednet.receive()ing so try removing the sleep calls and see if that works
jeroentjuh99 #6
Posted 13 May 2013 - 09:01 AM
I see. After reading through your code I notice that you did not include the "move" or "command" file, please include them if the following does not work

In your program you use quite a few sleep() calls, this is a very bad idea. if you receive a message while sleeping that message is missed. You do not need to sleep to yield if you are rednet.receive()ing so try removing the sleep calls and see if that works
Okay, here are move:

function pulse(side)
  redstone.setOutput(side, true)
  sleep(0.2)
  redstone.setOutput(side, false)
  sleep(2)
end
pulse("back")
And command:

rednet.open("right")
rednet.send(1, "mine") --all turtles are labelled as 1
Altough, you don't really need it (I guess). It fails at the end of mine, where is sends done to the main computrer, or at continue() where is counts the amount of the received messages.

I suppose I'm wrong, but I can't find a sleep() after rednet.recieve()
KaoS #7
Posted 13 May 2013 - 09:32 AM
It doesn't have to be after rednet.receive(). It has to be before it, as I said if they send you a message and you are using sleep() at that time the message is lost, then if you rednet.receive() afterwards you won't get the message. You should remove EVERY sleep() in your code, after that see if it works, if you get too long without yielding then let me know and I will make an intelligent sleep function which will not lose events
jeroentjuh99 #8
Posted 13 May 2013 - 09:44 AM
I use the sleep function to time stuff. If I remove every sleep(), it won't work. but I'll try…

Edit: I just did that, and I got java-nullpointer exeption. It did not show on what line or because of what, but it did pulse(side)
KaoS #9
Posted 13 May 2013 - 10:34 AM

rednet.open("right")
rednet.send(1, "mine") --all turtles are labelled as 1
the label is not the target of the message, it is the ID. use the program "ID" or the command os.computerID() to check the ID of a turtle/computer. perhaps consider using rednet.broadcast as well

give this a try as the entire code for the main computer

EDIT: if this code works please let me know, there are a few improvements I would like to make
Edited on 13 May 2013 - 08:36 AM
jeroentjuh99 #10
Posted 13 May 2013 - 12:14 PM
the turtles received the mine command, so that piece of code worked. I'll give your code a shot!

Edit: your code seem to be more stable, but again, after he mined, it stops. He doesn't go forward and mine again :(/>

Edit2: I let the computer print the value of dingetje by print(type(dingetje)) <-found it in lua tutorial
but it doesn't print it. Does that mean the turtles are not sending the message "done"?
Edited on 13 May 2013 - 10:38 AM
D3matt #11
Posted 14 May 2013 - 12:34 AM
Try just setting up the computer in a print(rednet.receive()) loop to see if the turtles are sending the message.
jeroentjuh99 #12
Posted 14 May 2013 - 08:11 AM
Try just setting up the computer in a print(rednet.receive()) loop to see if the turtles are sending the message.
I tried that with this code:

local tArgs={...}
tArgs[1]=tArgs[1] or 1
rednet.open("right")
local function pulse(side)
redstone.setOutput(side, true)
sleep(0.2)
redstone.setOutput(side, false)
sleep(2)
end
while true do
if redstone.getInput("top") then
  function continue()
   local dingetje = 0
   rednet.open("right")
   while true do
	senderID, message, distance = rednet.receive()
	print(rednet.receive())
   end
  end
  for i = 1, tArgs[1] do
   pulse("back")
   sleep(1)
   rednet.broadcast("mine")
   print("Iteration: "..i)
  end
  continue()
end
os.pullEvent("redstone")
end
And it did not print anything, except for iteration: 1
Edited on 14 May 2013 - 06:11 AM
D3matt #13
Posted 14 May 2013 - 08:28 AM
Add prints at all logic points or possible failure points on the turtle's end. As simple as print("Digging a block"), print("Sending message "done") etc. helps a LOT, most of my programs go through this kind of debugging stage at some point.
jeroentjuh99 #14
Posted 14 May 2013 - 03:36 PM
Add prints at all logic points or possible failure points on the turtle's end. As simple as print("Digging a block"), print("Sending message "done") etc. helps a LOT, most of my programs go through this kind of debugging stage at some point.
the turtles printed the message,so that's good (i hope…)
D3matt #15
Posted 14 May 2013 - 09:35 PM
Well, it all depends where you put the prints. You need to put them where you can clearly tell what your program is and isn't doing.
jeroentjuh99 #16
Posted 15 May 2013 - 09:56 AM
I printed it at the end of program "mine"