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

Remote control your turtle 101

Started by eddieo, 27 June 2012 - 09:29 PM
eddieo #1
Posted 27 June 2012 - 11:29 PM
While trying to learn how to get the turtles to function I learned a few things let me share them. First you need a computer with a modem and a wireless turtle.Then you need to know the turtles ids you can get it by typing id at the turtles prompt. The turtle will respond This is computer 1 or 2 or 5 please note this. Now we need to get the turtle to listen for instructions

Turtles code save it to a file and name it listen


rednet.open("right")  
print("Waiting for command")  
id, message  = rednet.receive()
term.clear()
if message == "test" then
  shell.run("dance")
else
  print("Does Not Compute")
end
  1. rednet.open("right")–starts the turtle listening for a message.
  2. print("Waiting for command")– prints on the screen Waiting for command until an event happens
  3. id, message = rednet.receive()– Sets up the receive event the computer will send its id# and a message. The message will always be a string.
  4. term.clear()– clears the screen
  5. if message == "test" then – once a message is received and it equals the if test then it executes the next line
  6. shell.run("dance")– Shell.run will execute what ever program that is stated. dance is an included CC program
  7. else the the received message does not equal the if test then the next line is executed print("Does Not Compute")– prints on the screen Does Not Compute end– ends the if statement
Ok that should get the turtle ready to do something. Now lets set the computer to send a message to make the turtle dance.

Computers code save it to a file named makedance


rednet.open("right")
rednet.send(2, "test")

Thats all we need
  1. rednet.open("right")– right equals the side of the computer the modem is on it could be left top bottom etc
  2. rednet.send(2, "test")– the 2 equals the turtles id and test is the message. Remember test from our if statement above if we send the turtle test it will make it dance if we send it anything else it will print out Does not compute.
now go to the turtle and type in listen the screen should say Waiting for command. go to the computer and type in makedance.
now stand back and watch the turtle dance. Click on the turtle to stop it dancing.

This method is easy to modify to make the turtle do what ever you want. I can expand on this topic if anyone needs me to.
cmurtheepic #2
Posted 20 July 2012 - 09:10 PM
on the turtle's line number 4 i keep getting attempt to call nil
eddieo #3
Posted 23 July 2012 - 03:42 AM
I just cut and pasted both programs and they ran properly. I have no idea why its not working for you. make sure that what ever editor you are using is copying it exactly . Have you edited your start up file and or your bios file? line 4 just clears the terminal screen you could delete the line
fixerpro #4
Posted 08 August 2012 - 12:47 PM
try using shell.run "clear"
srentiln #5
Posted 12 October 2012 - 10:33 PM
Don't know if you still keep an eye on this thread or not, but I have a question that is probably going to seem like an obvious answer to most people. Either it isn't addressed anywhere or I just am terrible with search terms…probably the latter, heh

If I want the turtle to go back to listening after it executes a program, how do I do that?

I'm working on a fairly basic item storing and retrieving system using a wireless turtle. I would like it to execute the program I tell it to and then wait for another message.

My first thought is to put "return id, message = rednet.receive() " at the end of the programs, but I've never really used returns before (mostly web coding experience) so I don't know if that's valid.
ChunLing #6
Posted 16 October 2012 - 08:49 PM
Here's the core of my remote control program.
cldt = {}
-- snip
function trply(mssgstr) -- i.e. "unbreakable block in path"
    print(mssgstr)
    if cldt.rcID then sleep(0.1) rednet.send(cldt.rcID,mssgstr) end
end
-- snip
function rctrl(psswrd,rtID)
    cldt.rcID = rtID
    rednet.open("right")
    if psswrd then cldt[1] = psswrd cldt.slt = 1 turtle.select(cldt.slt) else
    print("Enter confirmation password")
    cldt[1] = io.read() end
    print("Send password from control terminal")
    repeat cldt[2], cldt[3] = rednet.receive()
        if not cldt.rcID and cldt[3] == cldt[1] then cldt.rcID = cldt[2] end
    until cldt[2] == cldt.rcID and cldt[3] == cldt[1] trply("password accepted")
    local hndl = fs.open("startup","w")
    hndl.writeLine("shell.run("clfn",1,""..cldt[1].."")") hndl.close()
    while cldt.rcID do cldt[2],rnmssg = rednet.receive()
        if cldt[2] == cldt.rcID then
            _,_,cldt[3], cldt[5] = rnmssg:find("^(%a+)(.*)")
            if turtle[cldt[3]] then trply(tostring(turtle[cldt[3]](tonumber(cldt[5]))))
       	 elseif cldt[3] == "trmn8RC" then cldt.rcID = nil end
        end
    end
    print("Remote Control Terminated") fs.delete("startup")
end --snip
Note that this is excerpted from a longer program (User Friendly Program combining several useful functions), and will not function by itself.

But the relevant part for your question is this line, "while cldt.rcID do cldt[2],rnmssg = rednet.receive() … end". This loops the program until something sets cldt.rcID to false or nil (that something being "elseif cldt[3] == "trmn8RC" then cldt.rcID = nil end").

If you want, you can go through and rename all the variables with "meaningful" identifiers. I personally don't do this because the presence of "meaningful" identifiers tends to distract the coder from analysing what the code actually does (I find this particularly true of code novices, they often think that simply calling a variable "x position" means it will magically contain the x coordinate they want). But I'm told that many people prefer to have meaningful identifiers to help trace what the code does.
bbqroast #7
Posted 30 October 2012 - 10:20 PM
Don't know if you still keep an eye on this thread or not, but I have a question that is probably going to seem like an obvious answer to most people. Either it isn't addressed anywhere or I just am terrible with search terms…probably the latter, heh

If I want the turtle to go back to listening after it executes a program, how do I do that?

I'm working on a fairly basic item storing and retrieving system using a wireless turtle. I would like it to execute the program I tell it to and then wait for another message.

My first thought is to put "return id, message = rednet.receive() " at the end of the programs, but I've never really used returns before (mostly web coding experience) so I don't know if that's valid.
Use a while loop:

while true do
  // Code for listening for and responding to messages as show above goes here.
end
ChunLing #8
Posted 31 October 2012 - 08:26 AM
I prefer a repeat until loop in the case that I am certainly going to want to execute a section of code at least once but may wish to terminate it after any loop as a result of something that happened in that loop (in this case, reception of the message "trmn8RC"). The improvement in performance and stability is small but real.
RoD #9
Posted 30 June 2013 - 06:44 PM
I am also worknig on a remote turtle controler…
This is the code so far(not finnished)
This is for the computer:

shell.run("clear")
rednet.open("right")
print"Wellcome to turtle controler by RoD!"
while true do
event, scancode = os.pullEvent("key")
if (tostring(scancode)) == "17" then
print("W")
rednet.send(15, w, true)
elseif (tostring(scancode)) == "30" then
print("A")
rednet.send(15, a, true)
elseif (tostring(scancode)) == "31" then
print("S")
rednet.send(15, s, true)
elseif (tostring(scancode)) == "32" then
print("D")
rednet.send(15, d, true)
else
print("Unrecognized command")
end
end
--This code will convert the W,A,S,D keys to an input that will be sent to a turtle
and then the turtle will receive the inputs and convert it to commands like: turtle.forward()

rednet.open("right")
shell.run("clear")
print("Wellcome to turtle controler by RoD!")
local senderId, m = rednet.receive()
if m == w then
turtle.forward()
shell.run("clear")
shell.run("startup")
elseif m == s then
turtle.back()
shell.run("clear")
shell.run("startup")
end
-- this code is incoplete because i am having some trouble with the back command (its going forward XDDD)
The turtle will then say to the computer if there's a block and then it receive the command to brake the block.
BTW: i know that instead of:

print("Blablabla")
I can do:

print"Blablabla"
but i am used to type it that way XDD
Tips and advices are wellcome :D/> if you see something that could have been improved please tell me!