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

Remote controlled turtle

Started by NanoBob, 02 May 2013 - 06:48 AM
NanoBob #1
Posted 02 May 2013 - 08:48 AM
hello all

This topic is about my wireless turtle program : WirelessTurtle v1.5
The "cool" feature about this is no other signals will interfere,
It can be controlled from a nearby turtle/computer.
It's easy to set up, in your turtle and/or computer you have to type :
 pastebin get Zxq5hFqr startup
It's best to run it as startup.
Once you launched the program the program will ask you a few questions.
Once you have answered them all you can get started.

If you don't understand all questions here's the possible answers
Spoilerquestion 1:
Does this program already exist on your device?

The answer to this question is :"yes/no"
This question is made so if you put the file on a disk as disk/startup it will be copied from the disk without having to do it yourself (for the lazy people)

question 2:
do you want to transmit or receive?

The answer should be "receive/transmit"
This question is made to tell the program whether you want to receive, or transmit.


question 3:
What id would you like to link to?

This id will be show in the other device.
So go check that, it will say this is computer #[number]
Type that number in the other device
This is to make sure the computer will only control one turtle, and the turtle(receiver) will only accept messages from 1 computer(transmitter).

the code:
Spoiler


if turtle then
print[[---------------------------------------
Welcome to RemoteTurtle V2.0
---------------------------------------]]
else
if term.isColor() then
term.setTextColor(colors.lime)
end
print[[---------------------------------------------------
Welcome to RemoteTurtle V2.0
---------------------------------------------------
]]
end
if term.isColor() then
term.setTextColor(colors.white)
end
repeat
  print("Does the program already exist on your device")
  q=read()
  if
	q==("yes")
  then
	sleep(0)
  else
	if
	  q==("no")
	then
	  os.setComputerLabel("not yet defined")
	  fs.copy("disk/startup","startup")
	  print("You can now safely break the device and place it somewhere else without program loss")

	  error()  
	 end
  end
until q==("yes") or q==("no")

print("")
repeat
  print("Do you want this device to transmit or receive?")
  p=read()
  if
	p==("receive")
  then

os.setComputerLabel("Remote Turtle")
print("")
shell.run("id")
rednet.open("right")
print("")
print("What Id Do You Want To Link To?")
l=tonumber(read())
repeat
i,k=rednet.receive()
z=turtle.getFuelLevel()
if
  z==0 then
  shell.run("refuel")
end
z=turtle.getFuelLevel()
if
  z==0 then
  rednet.send(l,"p2")
end
if
  l==i and k==("w") then
  turtle.forward()
elseif
  l==i and k==("a") then
  turtle.turnLeft()
  turtle.forward()
elseif
  l==i and k==("s") then
  turtle.turnRight()
  turtle.turnRight()
  turtle.forward()
elseif
  l==i and k==("d") then
  turtle.turnRight()
  turtle.forward()
elseif
  l==i and k==("q") then
  turtle.up()
elseif
  l==i and k==("z") then
  turtle.down()
elseif
  l==i and k==("r") then
  shell.run("refuel")
elseif
  l==i and k==("x") then
  turtle.dig()
elseif
  l==i and k==("xup") then
  turtle.digUp()
elseif
  l==i and k==("xdown") then
  turtle.digDown()
elseif
  l==i and k==("turnleft") then
  turtle.turnLeft()
elseif
  l==i and k==("turnright") then
  turtle.turnRight()
elseif
  l==i and k==("clear") then
  os.setComputerLabel("")
elseif
  l==i and k==("f") then
  turtle.attack()
elseif
  l==i and k==("fup") then
  turtle.attackUp()
elseif
  l==i and k==("fdown") then
  turtle.attackDown()
elseif
  l==i and k==("reboot") then
  os.reboot()
elseif
  l==i and k==("shutdown") then
  os.shutdown()
elseif
  l==i and k==(cmd1) then
  turtle[cmd1e]()
elseif
  l==i and k==(cmd2) then
  turtle[cmd2e]()
elseif
  l==i and k==(cmd3) then
  turtle[cmd3e]()
elseif

  l==i and k==("AddCommand") then
  print("Which command do you want to change? 1/2/3")
  cwhat=read()
  print("for the question :What do you want to execute? you answer has to be what would fill in the blank spot: turtle.[BLANK SPOT]()")

  if cwhat == ("1") then
	print("What do you want to type to to execute")

	cmd1=read()  
	print("What do you want to execute?")
	cmd1e=read()
  elseif
	chwat == ("2") then
	print("What do you want to type to execute?")
	cmd2=read()
	print("What do you want to execute?")
	cmd2e=read()
  elseif
	chwat == ("3") then
	print("What do you want to type to execute?")
	cmd3=read()
	print("What do you want to execute?")
	cmd3e=read()
  end


else
  sleep(0.2)
  print("Doesn't match")
  rednet.send(l,"p")
end
sleep(0.2)
rednet.send(l,"Matches")

until
l==i and k==("stop") or l==i and k==("clear")
elseif
	p==("transmit")
  then
	os.setComputerLabel("Remote Controller")
	print("")
	shell.run("id")
	print("")


	  if
		peripheral.getType("right") == "modem" then
		rednet.open("right")
	  elseif
		peripheral.getType("left") == "modem" then
		rednet.open("left")
	  elseif
		peripheral.getType("top") == "modem" then
		rednet.open("top")
	  elseif
		peripheral.getType("bottom") == "modem" then
		rednet.open("bottom")
	  elseif
		peripheral.getType("back") == "modem" then
		rednet.open("back")
	  elseif
		peripheral.getType("front") == "modem" then
		rednet.open("front")
	  end



	print("")

	print("What id would you like to connect to?")
	l=tonumber(read())

	print("")
	print("Type /list for a list of commands")
	print("")

	repeat
	  write("Command : ")
	  x=read()
	  if
		x==("/list")
	  then
		print("r --> refuel (This should be done automaticly)")
		print("w/a/s/d --> movement")
		print("q/z --> up/down")
		print("x --> dig")
		print("xup --> dig up")
		print("xdown --> dig down")
		print("turnleft/turnright --> turn left/ turn right")
		print("f --> attack")
		print("fup/fdown --> attacks up/down")
		print("shutdown --> Shuts down transmitter and receiver")
		print("reboot --> reboots transmitter and receiver")
		print("stop --> Stops using this program")
		print("clear --> Clear Memory after picking up")
		print("AddCommand --> Allows you to add commands")

	  elseif
		x==("AddCommand") then
		print("To set this up you have to go to your turtle")
		rednet.send(l,x)
	  else
		rednet.send(l,x)
		sleep(0.00000001)
		y,u=rednet.receive(3)
		if u==("p2") and y==l then
		  if term.isColor() then
			term.setTextColor(colors.red)
		  end
		  print("Out of coal and fuel")
		  if term.setTextColor then
			term.setTextColor(colors.white)
		  end			
		end
		if u==("p") and y==l then
		  if term.isColor() then
			term.setTextColor(colors.red)
		  end
		  print("Incorrect Comand")
		  if term.isColor() then
			term.setTextColor(colors.white)
		  end

		elseif u==("Matches") and y==l then
		  if term.isColor() then
			term.setTextColor(colors.green)
		  end
		end
		  print("Command Correct")
		  if term.isColor() then
			term.setTextColor(colors.white)
		  end
		end

	until
	  x==("clear") or x==("stop")
	end

	if
	  x==("clear")
	then
	  os.setComputerLabel("")
	end

  until
p==("receive") or p==("transmit")

pastebin link:
Spoilerhttp://pastebin.com/Zxq5hFqr

Screenshots:
Spoilerthe "menu"

the "menu" in the turtle

Proof it works (spoilerception)
Spoilerwatch the turtle on the left

and see wehre it goes

Things being worked on:
Spoiler1.possibility to for example do: w 5 , instead of typing w 5 times
NanoBob #2
Posted 03 May 2013 - 08:21 AM
Can anyone try it out? Post feedback maybe? Please?
kalkins #3
Posted 04 May 2013 - 03:30 PM
It's a nice program that does what it's meant to do without problems, but you could make it better. here are some suggestions:
1. remove question 2.5 and make it find the modem automaticly
2. make it ask the question again if it doesn't understand the answer(in case the user typed a word wrong)
3. instead of asking if it's a computer or a turtle(which you can find out automaticly), ask if it should be transmitting or receiving
4. you didn't check the sender ID when the stop command was given. makes it possible for another computer to stop the program.
5. you don't need to assign i and k to id and message. you can just do i, k = rednet.receive()
6. correct the typos
7. instead of checking the ID every time you can check it once and then check the message
8. maybe make it possible to change the commands
9. In addition i would have preferred to use elseif instead of nesting every if statement, but that is up to you.
NanoBob #4
Posted 04 May 2013 - 05:29 PM
Thanks for the tips! I knew about the typos, but i haven't had the chance to fix em, but how do you find the modem automaticly? Or should i go ask in ask a pro,
kalkins #5
Posted 04 May 2013 - 06:17 PM
you use a for loop to go through the sides and see if there is a modem there


for key, side in pairs(rs.getSides()) do
    if peripheral.getType(side) == "modem" then
	    rednet.open(side)
    end
end

you can use the same code for other peripherals, such as monitors. just change "modem" to "monitor"
NanoBob #6
Posted 05 May 2013 - 04:46 AM
Thanks,
NanoBob #7
Posted 05 May 2013 - 06:51 AM
version 1.5 is out

in the new version I changed some things
it now does find the modem automaticly(question 2.5 is gone)
if you type something wrong it will ask again.
no possible interference from other transmitters (I might have skipped something but i hope not)
some minor changes in code
kalkins #8
Posted 10 May 2013 - 07:50 AM
looks good, but what if you want it to go a long way and dont want to type "w" all the time to get there? maybe you can make it so that you can type "w 5" and it moves forwards 5 blocks, and make it possible to send multiple commands at once, such as "w 5 d w 10" to make it move 5 blocks forwards, turn right, and then move another 10 blocks forward. it might require you to change most of your code, but it would make the program better
NanoBob #9
Posted 10 May 2013 - 05:12 PM
I've had that idea in mind for a long time, but I am currently working on something else (adding commands yourself from GUI) wich I will upload soon
H4X0RZ #10
Posted 10 May 2013 - 06:40 PM
Nice work :)/>/>
NanoBob #11
Posted 12 May 2013 - 05:06 PM
updated to version 2.0!

features:
-Adding commands through gui
-Collors for advanced computers.
-Nicer gui
-Big changes in code/indenting
-Turtle returns message saying whether the command was correct
-Automaticly refuels if there's fuel.
-Turtle returns message if out of fuel and coal.
-terminates program if answer to question 1 was no
GyroW #12
Posted 19 June 2013 - 10:11 AM
Hey there, I'm trying to code my own program and (if you don't mind i used part of your code for the rednet stuffs) but i can't get the
if k == ("w') to work
it always returns false could you help me out with this?
Tjakka5 #13
Posted 19 June 2013 - 10:41 AM
Hey there, I'm trying to code my own program and (if you don't mind i used part of your code for the rednet stuffs) but i can't get the
if k == ("w') to work
it always returns false could you help me out with this?


if (k == w) then --where w is a variable.
  doStuff()
end
KatraAmalia #14
Posted 21 October 2013 - 05:43 PM
Hey! I love your program, but I did have one question. When I'm adding a new command to the turtle, is it possible to set it up to run a program on the turtle? I know this is probably a very stupid question, but I can't figure it out. For example, if I have a turtle with a program called "tap", and I want to use your code to run the turtle program remotely, how would I set up the new command?
NanoBob #15
Posted 26 October 2013 - 10:01 AM
Sorry for the late response. I haven't been into computercraft for a long time.
And to answer your question , no I don't think you can run a custom program by adding a new command. but I am probably gonna redo a lot of the code to make it work better.