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

Wireless problem, logistic tutor needed.

Started by Cota, 12 November 2012 - 08:21 PM
Cota #1
Posted 12 November 2012 - 09:21 PM
So i had been trying to make a totally self grid tunnel turtle squad (9 wireless turtles to be exact) and my problem is a logistic part of the code.

The turtles are in this position if you saw then from the back:

1 2 3
4 5 6
7 8 9

1, 3, 7 and 9 dig and place blocks in their 2 respective corners while 2, 4, 6 and 8 only dig their respective side while 5 is the host computer that only digs and goes front,
the problem is that for the sake of some turtles going faster than others, computer 5 reads wireless messages from all of the other computers (taking advantage that this turtle finishes faster) and after all client turtles give their finish message, turtle 5 gives and approval message for the next chain of steps.

Edit:Found out that if i activate the host turtle 1st and then the other turtles they do one step and then the last client turtle that got activated gets stuck.

Logic:

They dig their sides, up and down, and then they place a decorative block-> turtle that finishes sends a message to turtle 5 -> when 5 gets a finish message from all turtles, 5 sends a message to the other turtles to go forward (while turtle.forward ~= true do turtle.dig()) -> "repeat"

My problem is that is seems to lag or get stuck since the turtles do their fist dig and never go forward for the next dig, i dont know what im doing wrong :P/>/>


Host turtle, aka "5":

local function P5()
T1=0
T2=0
T3=0
T4=0
T6=0
T7=0
T8=0
T9=0
Total=0
Steps=0
term.clear()
term.setCursorPos(1,1)
print(Total)
print("-1")--debug
print(Steps)--debug
while Steps < 64 do
print("-2")

--NOTE: Here is where the code stops responding since  print("-3")--debug never is displayed.

local event, param1, param2 = os.pullEvent("rednet_message")
print(param2)
print("-3")--debug
if(param2=="T1OK") then
rednet.send(12,"OK")
T1=1
Total=T1+T2+T3+T4+T6+T7+T8+T9
end
if(param2=="T2OK") then
rednet.send(13,"OK")
T2=1
Total=T1+T2+T3+T4+T6+T7+T8+T9
end
if(param2=="T3OK") then
rednet.send(14,"OK")
T3=1
Total=T1+T2+T3+T4+T6+T7+T8+T9
end
if(param2=="T4OK") then
rednet.send(15,"OK")
T4=1
Total=T1+T2+T3+T4+T6+T7+T8+T9
end
if(param2=="T6OK") then
rednet.send(17,"OK")
T6=1
Total=T1+T2+T3+T4+T6+T7+T8+T9
end
if(param2=="T7OK") then
rednet.send(18,"OK")
T7=1
Total=T1+T2+T3+T4+T6+T7+T8+T9
end
if(param2=="T8OK") then
rednet.send(19,"OK")
T8=1
Total=T1+T2+T3+T4+T6+T7+T8+T9
end
if(param2=="T9OK") then
rednet.send(20,"OK")
T9=1
Total=T1+T2+T3+T4+T6+T7+T8+T9
end

term.clear()
term.setCursorPos(1,1)
print(Total)
print("-4")--debug

if(Total>=8)  then
rednet.send(12,"Next")
sleep(0.1)
rednet.send(13,"Next")
sleep(0.1)
rednet.send(14,"Next")
sleep(0.1)
rednet.send(15,"Next")
sleep(0.1)
--rednet.send(16,"Next")
rednet.send(17,"Next")
sleep(0.1)
rednet.send(18,"Next")
sleep(0.1)
rednet.send(19,"Next")
sleep(0.1)
rednet.send(20,"Next")

while turtle.forward()==false do
turtle.dig()
end
Steps=Steps+1
Total=0
T1=0
T2=0
T3=0
T4=0
T6=0
T7=0
T8=0
T9=0
end
sleep(0)

end
turtle.turnLeft()
turtle.turnLeft()
end

Base code for the other turtles:

local function P1()
turtle.select(1)
while Steps<64 do
--Detect and place up
while turtle.placeUp() ~= true do
turtle.digUp()
sleep(1)
end
Placecount=Placecount+1
if(Placecount>=64) then
Slot=Slot+1
Placecount=0
turtle.select(Slot)
end
--Detect and place up

--Detect and place left
turtle.turnLeft()
while turtle.place() ~= true do
turtle.dig()
sleep(1)
end

Placecount=Placecount+1
if(Placecount>=64) then
Slot=Slot+1
Placecount=0
turtle.select(Slot)
end
turtle.turnRight()
--Detect and place left


--Wireless
Lock=0
Lock2=0
while Lock==0 do
if Lock2~=1 then
rednet.send(T5ID,"T1OK")
term.clear()
term.setCursorPos(1,1)
print("T1OK")
end
local event, param1, param2 = os.pullEvent("rednet_message")
if(param2=="OK") then
Lock2=1
print(param2)
end
if(param2=="Next") then
Lock=1
end
sleep(0)
end
Lock=0
Lock2=0
--Wireless

if(Steps<63) then
while turtle.forward()== false do
turtle.dig()
end
end
Steps=Steps+1
end

turtle.turnLeft()
turtle.turnLeft()
end
XoX #2
Posted 12 November 2012 - 11:17 PM
If that is the exact code then the other turtles never send anything because of this line.

while Lock==0 do
Because Lock is nil , you only got Lock1, and Lock2. If this is not the exact code then please give us the exact code because we can't look for errors in a poor copy of the code.

Also, tip for the future, instead of color-coding everything you can simply use the code tags, makes everything easier to read.
Cota #3
Posted 13 November 2012 - 07:21 AM
yeah i kind of just cut the logic parts of the code, Lock1 and Lock2 do are set to cero on the start, the complete code is 1424 lines, thats why im using those debud messages print"-x"–debug, gonna edit the original post.
KaoS #4
Posted 13 November 2012 - 07:57 AM
I tried something similar to this. the turtles in my squad were meant to roam though, all while being commanded. it all got too much, I had them all linking together and accepting commands from the host turtle but then making the host turtle intelligently assign actions to the clients was way more advanced than my experience. why don't you store all commands server side and transmit them to the turtles. I find it makes things a lot more organised for some reason. I also notice that you have no registration, the server just knows who the clients are… here are links to the initial prototype of the squad before I over-complicated it [server] [client] hope they help a little :P/>/>
XoX #5
Posted 13 November 2012 - 08:47 AM
I do realize that Lock1 and 2 were set but in the piece of code you posted it was neither of them it was just Lock.
So even though you set Lock1 and Lock2, you are still comparing Lock ( which is nil )