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

Basic but Flexible Remote Control

Started by ChunLing, 09 October 2012 - 05:43 PM
ChunLing #1
Posted 09 October 2012 - 07:43 PM
I had a hard time finding a simple, capable remote control program for a turtle, so I came up with this.

cldt = {}
function trply(mssgstr) -- i.e. "unbreakable block in path"
    print(mssgstr)
    if cldt.rcID then
    rednet.send(cldt.rcID,mssgstr) end
end

rednet.open("right")
print("Enter confirmation password")
cldt[1] = io.read()
print("Send password from control terminal")

repeat cldt[2], cldt[3] = rednet.receive() -- you can set a timeout value here, but I didn't
  if cldt[3] == cldt[1] then cldt.rcID = cldt[2] trply("password accepted") break end
until not cldt[2]
while cldt.rcID do cldt[2], cldt[3] = rednet.receive()
  if cldt[2] == cldt.rcID then
   if turtle[cldt[3]] then turtle[cldt[3]]()
   elseif cldt[3] == "trmn8RC" then cldt.rcID = nil end
  end
end
print("Remote Control Terminated")

Once a rednet message matching the password is received, the turtle will execute any turtle API command it receives from the ID that send the password. A corresponding controller program is useful but not necessary. What may be needed is to add additional functions (stored in a table named "turtle" for easy access) to provide additional functionality, since the program currently lacks a way to pass arguments to the functions it calls (which is why turtle.select() and such are not supported).

rcmdlst = {
[79] = "place",
[71] = "placeUp",
[82] = "placeDown",
[83] = "drop",
[210] = "place",
[211] = "dig",
[199] = "dropUp",
[207] = "dropDown",
[201] = "up",
[209] = "down",
[200] = "forward",
[208] = "back",
[203] = "turnLeft",
[205] = "turnRight",
[26] = "digUp",
[27] = "digDown",
[28] = "suck",
[51] = "suckUp",
[52] = "suckDown",
[45] = "trmn8RC",
}
cldt = {}
shell.run('clear')
print("Enter confirmation password for remote turtle control")
if not rednet.broadcast(io.read()) then print("Modem Access Error") return false
else cldt[1],cldt[2] = rednet.receive() print(cldt[1],cldt[2]) end
if cldt[2] == "password accepted" then
print("Use numberpad to control turtleID"..cldt[1]..[[
170 to place[Up/Down]
'.' Home End, to drop[Up/Down]
Enter, <, >, to suck[Up/Down]
Del, [, ] to dig [Up/Down]
Direction Keys and PgUp/Dn to move
Press x to end Remote Control]])
cldt[3] = false
repeat
  cldt[4],cldt[5],cldt[6] = os.pullEvent()
  if cldt[4] == "key" and rcmdlst[cldt[5]] then rednet.broadcast(rcmdlst[cldt[5]])
   if cldt[5] == 45 then cldt[3] = true end
  elseif cldt[4] == "rednet_message" then print(cldt[5]..": "..cldt[6]) end
until cldt[3]
else print("Remote Control confirmation failed") end
Custom Boot Maker #2
Posted 05 November 2012 - 06:30 AM
Maybe Paste Bin This But 1 Comment "AWESOME!"
robhol #3
Posted 07 November 2012 - 01:01 AM
Why not just use normal variable names? You gain nothing by removing vowels in the variable names..
ChunLing #4
Posted 07 November 2012 - 12:16 PM
I know I've explained this a few times before, but oh well. It's embarrassing, but when I use "meaningful" identifiers, I have trouble remembering what the code actually does. When I use an identifier that doesn't have a formal, predefined meaning, it's easier to remember only the code that assigns it value.

So I personally definitely do gain something by not using "normal" variable names.
ScruffyRules #5
Posted 07 November 2012 - 09:46 PM
Why not just use normal variable names? You gain nothing by removing vowels in the variable names..
who cares, that how he writes his programs…
ChunLing #6
Posted 08 November 2012 - 07:15 PM
Some people legitimately care, like when I post something in ask a pro. Of course, when I try to convert identifiers to be more meaningful, I'm always forgetting a few (precisely because after I see that I've named a variable after what it's supposed to do, it makes me forget to check what it actually does) and end up posting broken code. I also kinda suck at commenting code.
ScruffyRules #7
Posted 10 November 2012 - 06:59 PM
Some people legitimately care, like when I post something in ask a pro. Of course, when I try to convert identifiers to be more meaningful, I'm always forgetting a few (precisely because after I see that I've named a variable after what it's supposed to do, it makes me forget to check what it actually does) and end up posting broken code. I also kinda suck at commenting code.
Touché Touché!
tacomental #8
Posted 13 December 2012 - 01:09 PM
That being said in general as a programmer you should strive for code that is termed "self-documenting"

Not doing this is fine if you don't intend anyone else to ever read your code but when you share it then people trying to decipher whats going on inherit a headache.

That being said I'm going to try and decipher what the heck is going on in here because i'm tryign to develop my own wireless mining turtle managment program and i need to learn the basics. So thank you :)/>.
ChunLing #9
Posted 14 December 2012 - 01:56 PM
Yeah, I just happen to suck at translating from one language to another. And I also get confused if there is vocabulary overlap with non-identical meanings (probably part of why I suck at translating). For me, once something is "readable" in lua I have real trouble expressing what it says in another language.

I'm that way with natural languages too. And there I usually don't have the extra problem of having to define my own terminology the way a programmer must.

By the way, the RC function in clfn is a good bit more refined and versatile, but it's embedded in a larger program so it might be harder to read?