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

Remote Controle for Turtle

Started by CodeMonkey42, 31 March 2014 - 11:22 PM
CodeMonkey42 #1
Posted 01 April 2014 - 01:22 AM
Pocket Computer as Remote for Turtle

or at least my take on it

v1.0

This is some basic code that I scratched together as soon as I found the pocket computers. Using the wireless modem and the rednet api, I was able to create a simple command based protocol. The application works by starting the client side of the code on a wireless turtle, and then connecting to that turtle with the remote program on the pocket computer. Once in the pocket computer, a list of commands will be shown and all keyboard input will be directed to the turtle.
Instructions:
  • Start “client” on the turtle
  • Start “remote” on the pocket computer and input the turtle ID (printed on start of “client”)
  • You can reconnect as many times as you want, and you can have as many remotes for one turtle as you want
Pastebin Codes [Thanks to DD008]:
For Client: GYYPGizn
For Remote: gKe06v1M

Client Side v1.0

local sSide = "right"
print("Turtle running on Channel "..os.getComputerID())
rednet.open(sSide)
while true do
local id, msg = rednet.receive(1)

if msg then
  if msg == "Init" then
   rednet.send(id, "Good")
  else
   msg = tonumber(msg)

   if msg == 16 then
	print("Stopping")
	rednet.close(sSide)
	return
   elseif msg == 30 then turtle.turnLeft()
   elseif msg == 32 then turtle.turnRight()
   elseif msg == 17 then turtle.forward()
   elseif msg == 31 then turtle.back()
   elseif msg == 57 then turtle.up()
   elseif msg == 42 then turtle.down()
   elseif msg == 21 then turtle.digUp()
   elseif msg == 35 then turtle.dig()
   elseif msg == 49 then turtle.digDown()
   elseif msg == 22 then turtle.placeUp()
   elseif msg == 36 then turtle.place()
   elseif msg == 50 then turtle.placeDown()
   else print("Unknown command "..msg)
   end
  end
end
end

Remote Side v1.0

local tArgs = {...}
local channel = 0
local sSide = "back"

-- Get Channel Number
print("What channel?")
channel = tonumber(read())

-- Connect
print("Connecting to "..channel)
rednet.open(sSide)
rednet.send(channel, "Init")
local id, msg = rednet.receive(1)

if not msg or id ~= channel then
	print("Could not connect")
	return
end

-- Print commands
print("Commands:")
print("Q	- Disconnect/Quit")
print("WASD - Move/Turn")
print("Space- Move Up")
print("Shift- Move Down")
print("Y	- Mine Up")
print("H	- Mine")
print("N	- Mine Down")
print("U	- Place Up")
print("J	- Place")
print("M	- Place Down")

-- Program Loop
while true do
	local event, sc = os.pullEvent("key")
	
	if sc == 16 then
		rednet.send(channel, sc)
		print("Closing")
		rednet.close(sSide)
		return
	elseif sc == 17 or
	sc == 30 or
	sc == 31 or
	sc == 32 or
	sc == 42 or
	sc == 57 or
	sc == 21 or
	sc == 22 or
	sc == 35 or
	sc == 36 or
	sc == 49 or
	sc == 50 then
		rednet.send(channel, sc)
	end
end

Licensing:
General Public License (GPL): You can use it how you want, but with credit to the original author (CodeMonkey42).
Edited on 11 June 2014 - 12:22 AM
ByteZz #2
Posted 02 April 2014 - 11:23 AM
This is brilliant. With your permission, I would like to be able to create a configuration system so that the config can be loaded, edited or created so it doesn't need to ask for the channel everytime. It uses a very good API that I have learned to use properly. I would be delighted to know if you'd like me to add this functionality. I'll add it in anyway for my own personal use but would only release it with your permission.
CodeMonkey42 #3
Posted 02 April 2014 - 02:09 PM
By all means, this is an open source project, and should be used as such.
apemanzilla #4
Posted 02 April 2014 - 04:12 PM
By all means, this is an open source project, and should be used as such.
Aren't all CC projects open source? :P/>
Lyqyd #5
Posted 02 April 2014 - 04:42 PM
No. CC projects all have visible source code, but that does not make them open source, since any creative work falls under copyright protections and must be licensed for use, modification, redistribution, etc.
ByteZz #6
Posted 02 April 2014 - 07:09 PM
Thanks for the clarifications guys. So without the permission of the original creator, I won't release it. Of course, I will manipulate this good code to benefit me but if the author wants me to, I can easily release the code.
oeed #7
Posted 02 April 2014 - 09:58 PM
By all means, this is an open source project, and should be used as such.
Thanks for the clarifications guys. So without the permission of the original creator, I won't release it. Of course, I will manipulate this good code to benefit me but if the author wants me to, I can easily release the code.

He has sort of.

CodeMonkey42, for clarification it might be best to state what license you're using. If you don't care what people do with it MIT is probably what you're looking for.
CodeMonkey42 #8
Posted 03 April 2014 - 12:10 AM
To clarify, General Public License (GPL). I have updated the original post to include this.
ByteZz #9
Posted 03 April 2014 - 03:04 AM
To clarify, General Public License (GPL). I have updated the original post to include this.

Thank you for the license. I'll get to work and then credit you.
DD008 #10
Posted 07 June 2014 - 11:03 AM
Hey guys there was no pastebin code but i made one for you guys its: pastebin get GYYPGizn client

And also here is the remote for the pocket: pastebin get gKe06v1M Remote


By the way the Q key for movement is also for Quit
and the movement keys don't work
plus the Shift and space key
Edited on 07 June 2014 - 09:14 AM
jaffacakesareamazing #11
Posted 07 June 2014 - 12:38 PM
Another thing that could be added (in use of CC 1.6 features) is the ability to use a protocol to communicate on as then it won't interfere with other rednet communication systems around the world. I did that in mine, it didn't take too long, just add the rednet.host([protocol name], [hostname for computer]) in the client and remote and it should work without interfearing with other devices communicating wirelessly! (Kind of like frequencies in the real world) :)/>
CodeMonkey42 #12
Posted 11 June 2014 - 02:17 AM
Hey guys there was no pastebin code but i made one for you guys its: pastebin get GYYPGizn client

And also here is the remote for the pocket: pastebin get gKe06v1M Remote


By the way the Q key for movement is also for Quit
and the movement keys don't work
plus the Shift and space key
Thankyou for that. I will fix the code in the post, and also add the pastbin codes.
Danny #13
Posted 22 July 2014 - 07:41 AM
Some screenshots would be awesome, thanks.