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

need help with wireless comunicating with turtles

Started by mrgreaper, 06 March 2012 - 01:36 AM
mrgreaper #1
Posted 06 March 2012 - 02:36 AM
ok if you have seen my other thread you will know i have been learning lua and making an auto turtle go home script …well that script is done
in part of it the turtle sends a signal wirelessly do a terminal that controls a door, it works beutifuly… but when i try to expand upon that else wheres it just wont work :unsure:/>/>

ok heres the startup script for the turtle

function os.pullEvent()
rednet.open("right")
local sEvent, param1, param2, event, p1, p2, p3, p4, p5 = os.pullEventRaw()
if event == "terminate" then
os.reboot()
end
if sEvent == "rednet_message" and param1 == 473 and param2 == "home" then
rednet.send(473, "ok bos im on my way")
shell.run(gohome)
end
if sEvent == "rednet_message" and param1 == 473 and param2 == "undock" then
rednet.send(473, "ok bos im on my way")
shell.run(undock)
end
if sEvent == "rednet_message" and param1 == 473 and param2 == "go" then
rednet.send(473, "ok im going to need some details")
rednet.send(473, "Whats the xcord?")
-- rednet.send(485, "1") --the script will be waiting for this so it can continue and ask the input
id, message  = rednet.receive()
message = tonumber(message)
x = message
rednet.send(473, "Whats the ycord?")
-- rednet.send(485, "1") --the script will be waiting for this so it can continue and ask the input
id, message  = rednet.receive()
message = tonumber(message)
y = message
rednet.send(473, "Whats the zcord?")
-- rednet.send(485, "1") --the script will be waiting for this so it can continue and ask the input
id, message  = rednet.receive()
message = tonumber(message)
z = message
rednet.send(473, "can i mine along the way? true/flase")
-- rednet.send(485, "1") --the script will be waiting for this so it can continue and ask the input
id, message  = rednet.receive()
print ("ok im on my way to "..x..y..z)
magicTurtle.goToPos(x,y,z,message)
magicTurtle.savePos()-- for ease of use latter and incase of crash
end
if sEvent == "rednet_message" and param1 == 473 and param2 == "whereareyou" then
local x,y,z=magicTurtle.getPos()
print ("boss wants to know where i am, im telling him "..x..y..z)
rednet.send(473, "boss im at "..x..y..z)
end
return sEvent, param1, param2, event, p1, p2, p3, p4, p5
end
print ("turtle starting, getting last know cords...hope thier right")
magicTurtle.getPosF()
local x,y,z=magicTurtle.getPos()
print ("OK so im now at "..x..y..z)
rednet.send(473, "OK so im now at "..x..y..z)
print ("now what?")
rednet.send(473, "Boss, im ready")
--loopback()
function loopback()
--so the program doesnt end
sleep(60)
loopback()
end
loopback()

heres the master terminal script


--master computer script
-- reference 77 285 286 303 144(basemainpc) 396(turtle1) 473(master comp)
function os.pullEvent()
rednet.open("left")
local sEvent, param1, param2, event, p1, p2, p3, p4, p5 = os.pullEventRaw()
if event == "terminate" then
os.reboot()
end
if sEvent == "rednet_message" and param1 == 396 then
print (param2)
end
return sEvent, param1, param2, event, p1, p2, p3, p4, p5
end
function main()
rednet.open("left")
print ("master turtle  control")
print ("Select turtle")
print ("1) turtle 396")
q = io.read()
q = tonumber(q)
if q == 1 then
turtle = 392
else
print ("you failed to select a turtle")
main()
end
print ("select function:")
print ("1) recall turtle")
print ("2) undock turtle")
print ("3) order turtle to go somewhere")
print ("4) ask the turtle where it is")
w = io.read()
w = tonumber(w)
if w == 1 then
rednet.send(396, "home")
elseif w == 2 then
rednet.send(396, "undock")
print ("test")
elseif w == 3 then
gp()
elseif w == 4 then
rednet.send(396, "whereareyou")
else
print ("hmmm i didint regonise that option")
main()
end
end
function gp()
rednet.send(396, "go")
id, message  = rednet.receive()
print (message)
v = io.read()
rednet.send(396, v)
  id, message  = rednet.receive()
print (message)
v = io.read()
rednet.send(396, v)
  id, message  = rednet.receive()
print (message)
v = io.read()
rednet.send(396, v)
   id, message  = rednet.receive()
print (message)
v = io.read()
rednet.send(396, v)
end
main()

the main terminal is 473 and its modem is on the left side of it (opposite side to the turtle)
the turtle is 396

the turtles program gets to the waiting stage and idles but doesnot pickup ANY of the wireless signals sent from 473, and if i manualy tell it to undock etc its messages arnt displayed on 473

the two that do work together
turtles gohome script

-- reference 77 285 286 303 144
function os.pullEvent()
local event, p1, p2, p3, p4, p5 = os.pullEventRaw()
if event == "terminate" then
os.reboot() 
end
return event, p1, p2, p3, p4, p5
end
function gohome()
magicTurtle.savePos()-- for ease of use latter and incase of crash
tim=0
tim2=0
print ("Im off home")--i will make these print to a master terminal as well in the final version B)/>/>
local x,y,z=magicTurtle.getPos() 
flyup()
--ok so were at 120 lets go near the base
magicTurtle.goToPos(19590,120,489,false) --not the actual cords but close enough for goverment work false so it wont break anything
--now we need it to go down to 78
print ("Descending")
magicTurtle.goToPos(19590,78,493,false)
--now to make it go to the door
magicTurtle.goToPos(19590,79,423,false)
--now to make it open the door
print("knock knock, room for a poor old turtle?")
rednet.open("right") --should be open already on the main script?
rednet.send(303, "littlecomputerletmein")
outerdock() -- right lets get the little turtle through the door
indock() -- we wait for the door to close
unload() -- unload the stuff
print ("cycle complete, sleepy turtle sleep now?")
sleep(60)
os.shutdown()
end
function flyup()
print ("Up up and awaaaaay")
magicTurtle.savePos()-- for ease of use latter and incase of crash
local x,y,z=magicTurtle.getPos()
magicTurtle.goToPos(x,120,z,true)
--if y == 120 then --can make that 250 when we get the anvil B)/>/>
--end
--else magicTurtle.up(1)
--flyup()
--end
end
function outerdock()
magicTurtle.savePos()-- for ease of use latter and incase of crash
detect=turtle.detect()
if detect == false then --make sure the doors open first wouldnt want him to bang his nose errr screen
--magicTurtle.goToPos(19590,79,421,false) --gets a bit confused bless him
magicTurtle.forward(2) --1 into the hole 1 through it)
else
tim = tim + 1
if tim == 60 then
print ("ummm boss we got us a problem")
else
sleep(1)
outerdock()
end
end
end
function indock()
magicTurtle.savePos()-- for ease of use latter and incase of crash
magicTurtle.faceSouth() --faces the door as thier is no detect back (oops)
detect2=turtle.detect()
if detect2 == true then --wait for the door to close behind us
-- magicTurtle.goToPos(19495,79,417,false) -- api dosnt like going to cords in a confined space
magicTurtle.faceNorth() --face away from the door
magicTurtle.forward(4) -- fly 4 squares into the room
magicTurtle.faceEast() -- so he is looking at his docking station
magicTurtle.forward(5) -- fly to his docking port
magicTurtle.faceWest() -- face into the room with his back resting on the filtier
magicTurtle.savePos()-- for ease of use latter and incase of crash
else tim2 = tim2 + 1
if tim2 == 60 then
print("come on close the door its cold in here")
else
sleep(1)
indock()
end
end
end
function unload()
magicTurtle.savePos()-- for ease of use latter and incase of crash (though this should be the same as outerdock one)
--redpulse back 9 -- red pulse doesnt work from scripts
redstone.setOutput("back",true)
sleep(1)
redstone.setOutput("back",false)
sleep(1)
  redstone.setOutput("back",true)
sleep(1)
redstone.setOutput("back",false)
sleep(1)
  redstone.setOutput("back",true)
sleep(1)
redstone.setOutput("back",false)
sleep(1)
  redstone.setOutput("back",true)
sleep(1)
redstone.setOutput("back",false)
sleep(1)
  redstone.setOutput("back",true)
sleep(1)
redstone.setOutput("back",false)
sleep(1)
  redstone.setOutput("back",true)
sleep(1)
redstone.setOutput("back",false)
sleep(1)
  redstone.setOutput("back",true)
sleep(1)
redstone.setOutput("back",false)
sleep(1)
  redstone.setOutput("back",true)
sleep(1)
redstone.setOutput("back",false)
sleep(1)
  redstone.setOutput("back",true)
sleep(1)
redstone.setOutput("back",false)
sleep(1)
print ("i feel as light as a feather now")
end
gohome()

and the door control it communicates with (on pc 303)


function os.pullEvent()
rednet.open("back")
local sEvent, param1, param2, event, p1, p2, p3, p4, p5 = os.pullEventRaw()
if event == "terminate" then
os.reboot()
end
if sEvent == "rednet_message" and param1 == 396 and param2 == "littlecomputerletmein" then
rs.setOutput("bottom", true)
sleep(8)
rs.setOutput("bottom", false)   
end
return sEvent, param1, param2, event, p1, p2, p3, p4, p5
end
function wa()
  term.clear()
  term.setCursorPos( 1, 1 )
  print ("waiting")
  sleep(20)
wa()
door() --from here down is redundent code
  term.clear()
  term.setCursorPos( 5, 5 )
print ("WARNING DOOR OPEN")
rs.setOutput("bottom", true)
sleep(10)
rs.setOutput("bottom", false)
wa()
end
wa()

that works it opens the door perfectly and yet its the same method i used for the communication between turtle and main pc that dont work arghhhhh

so umm where did i go wrong

side note anyone that wants to use my code in thier stuff…. go for it its all stuff i have learnt here B)/>/>
mrgreaper #2
Posted 07 March 2012 - 12:15 AM
any ideas? im completly stumped
ChaosBeing #3
Posted 07 March 2012 - 12:57 AM
I can't read through the whole thing right now, but if I were you and I didn't know in what part of my code it was failing, here's the first things I'd try:

To help with debugging, add a few print statements through the code so that you can see what parts of the code are working correctly. This is my usual go-to debugging method, because until you know exactly what's happening where, you can't really tell what's going wrong. It doesn't have to be anything fancy, just print("Debug 1!") so that you can find it easily.

Try removing the ID checks. (I'd add a comment in the code to remind me to put them back later.) If you've typoed somewhere, or you're using the wrong ID somewhere, this should then make it work. (You'll want to make sure nothing else is transmitting, though.) Also, I think I saw that you hard coded in the ID for the terminal and/or turtle. It'd be a much better idea to simply make a variable up top to hold this value than to hard code it every time. (If I'm mistaken, then please ignore this. :unsure:/>/>)

If you think the problem is that one of them isn't hearing messages sent by the other, set up a listening terminal. Make sure that any communication that either the terminal or turtle sends to the other is also sent to the listener, so that you can get a log of everything sent between the two. If one of them isn't broadcasting the correct messages at the correct time, then that could easily be your problem.