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

[1.48]Remote controlled turtle

Started by maikyy, 03 January 2013 - 06:09 AM
maikyy #1
Posted 03 January 2013 - 07:09 AM
Hey all!

i was just working out how the rednet stuff works and all when i came up with a neat idea, a touchscreenmonitor wich remotely controls a turtle, like a joystick.
so i created one and here is the result!


The script for the computer
Spoiler

-------- Turtle Control V 1.0 -------
------- Script made by maikyy -------
---------- Computer script ----------
--- Defining the variables ---
local monitor = "top" -- Where your monitor is
local modem = "left"  -- Wheren your modem is
local turtle = 44	 -- Turtle ID #
local slot = 1		-- Currunt turtle slot
local fuel = 0		-- Current turtle fuel

--- Now defining the colors, you can change every color if you want ---
local c = { }
   c.dpad = colors.blue
   c.dig = colors.red
   c.place = colors.lime
   c.bg = colors.black
   c.text = colors.white
   c.comm = colors.orange
   c.fuel = colors.cyan
  
  
--- Wrapping the monitor and modem ---
mon = peripheral.wrap(monitor)
m = peripheral.wrap(modem)

--- This function sets the screen ---
local function render()		   -- Defining a new function named render()
   mon.setTextColor(c.text)	   -- This sets the text color
   mon.setBackgroundColor(c.bg)   -- This sets the bg color
   mon.clear()					-- This clears the screen (because we have set a bg color, the screen will be colored)
   mon.setBackgroundColor(c.dig)  -- this will be the color for digging a block
   mon.setCursorPos(1, 1)		 -- Sets the Cursor Position
   mon.write("D")				 -- This writes the letter D for Dig at the previous position with the color chosen as a background
   mon.setBackgroundColor(c.place)
   mon.setCursorPos(2, 1)
   mon.write("P")
   mon.setBackgroundColor(c.fuel)
   mon.setCursorPos(3, 1)
   mon.write( fuel )				-- This will write the Fuel left in the turtle
   mon.setBackgroundColor(c.dig)
   mon.setCursorPos(7, 2)
   mon.write("X")
   mon.setBackgroundColor(c.dpad)
   mon.setCursorPos(1, 2)
   mon.write("!^  ^A")
   mon.setCursorPos(1, 3)
   mon.write("< >".. slot .. " " )  -- This will write < > and the slotnumber of the turtle
   mon.setCursorPos(1,4)
   mon.write("iV  V")
   mon.setCursorPos(6, 3)
   mon.setBackgroundColor(c.dig)
   mon.write("^")
   mon.setCursorPos(6, 4)
   mon.write("v")
   mon.setBackgroundColor(c.place)
   mon.setCursorPos(7, 3)
   mon.write("^")
   mon.setCursorPos(7, 4)
   mon.write("v")
   mon.setBackgroundColor(c.comm)
   mon.setCursorPos(1, 5)
   mon.clearLine()
end
--- Create the screen and opening the modem ---
render()
m.open()
--- Now the main loop, this will wait for an event named "monitor_touch" ---
--- If the event is pulled then it will check what the x and y coords are ---
--- and then it will send the command to the turtle ---
while true do  -- starts an infinite loop
   event, click, x, y = os.pullEvent()  -- wait for an event
	 if event == "monitor_touch" then  -- if the event is you touching the monitor then
	  if x == 1 then  -- if the X coord is equel to 1
	  if y == 1 then  -- if the Y coord is equel to 1
	  m.send(turtle, "dig")  -- sends the message "dig" to turtle
	  mon.setCursorPos(1, 5) -- sets the cursor position
	  mon.clearLine()  -- clears the line
	  mon.write("Dig") -- writes dig on the screen
   end
   if y == 2 then
	  m.send(turtle, "mu")
	  idt, fuel = rednet.receive() -- receives a message back from the turtle with the fuel level
	  render()  -- re-renders the screen to update the fuel level
	  mon.setCursorPos(1, 5)
	  mon.clearLine()
	  mon.write("Up")
   end
   if y == 3 then
	  m.send(turtle, "tl")
	  mon.setCursorPos(1, 5)
	  mon.clearLine()
	  mon.write("Left")
   end
   if y == 4 then
	  m.send(turtle, "md")
	  idt, fuel = rednet.receive()
	  render()
	  mon.setCursorPos(1, 5)
	  mon.clearLine()
	  mon.write("Down")
   end
   end
   if x == 2 then	-- pretty much the same as above only keep going until all the coords are programmed
	 if y == 1 then
		m.send(turtle, "place")
	 mon.setCursorPos(1, 5)
	  mon.clearLine()
	  mon.write("Place")
   end
   if y == 2 then
	  m.send(turtle, "fw")
	  idt, fuel = rednet.receive()
	  render()
	  mon.setCursorPos(1, 5)
	  mon.clearLine()
	  mon.write("Forward")
   end
   if y == 4 then
	  m.send(turtle, "bw")
	  idt, fuel = rednet.receive()
	  render()
	  mon.setCursorPos(1, 5)
	  mon.clearLine()
	  mon.write("Back")
   end
   end
   if x == 3 then
	  if y == 3 then
		 m.send(turtle, "tr")
	  mon.setCursorPos(1, 5)
	  mon.clearLine()
	  mon.write("Right")
	  end
   end
   if x == 5 then
	  if y == 2 then
		 m.send(turtle, "su")
	  idt, slot = rednet.receive()  -- this will receive a message with the current slotnumber of the turtle
	  render()
	  mon.setCursorPos(1, 5)
	  mon.clearLine()
	  mon.write("Slot ^")
   end
   if y == 4 then
	  m.send(turtle, "sd")
	  idt, slot = rednet.receive()
	  render()
	  mon.setCursorPos(1, 5)
	  mon.clearLine()
	  mon.write("Slot v")
   end
   end
		 if x == 6 then
	 if y == 2 then
	  m.send(turtle, "at")
	  mon.setCursorPos(1, 5)
	  mon.clearLine()
	  mon.write("Attack")
   end
   if y == 3 then
	  m.send(turtle, "du")
	  mon.setCursorPos(1, 5)
	  mon.clearLine()
	  mon.write("Dig ^")
   end
   if y == 4 then
	  m.send(turtle, "dd")
	  mon.setCursorPos(1, 5)
	  mon.clearLine()
	  mon.write("Dig v")
   end
   end
   if x == 7 then
	  if y == 2 then
	  m.send(turtle, "ex")
	  mon.setCursorPos(1, 5)
	  mon.clearLine()
	  mon.write("Exited")
	  break				   -- breaks the loop and terminates the program on the pc and turtle
   end
   if y == 3 then
	  m.send(turtle, "pu")
	  mon.setCursorPos(1, 5)
	  mon.clearLine()
	  mon.write("Place ^")
   end
   if y == 4 then
	  m.send(turtle, "pd")
	  mon.setCursorPos(1, 5)
	  mon.clearLine()
	  mon.write("Place v")
   end
  end
end
end
m.close()
print("Terminated!")

And here is the script for the turtle
Spoiler

-------- Turtle Control V 1.0 -------
------- Script made by maikyy -------
------------ Turtle script ----------
--- Defining the variables ---
fuel = turtle.getFuelLevel()  -- checks the fuel level
slot = 1					  -- sets the turtle slot
m = peripheral.wrap("right")  -- wraps the modem

--- Defining the functions ---
function sendFuelLevel()
   fuel = turtle.getFuelLevel()
   fuel = tostring(fuel)
   m.broadcast(fuel)
   fuel = tonumber(fuel)
end
function sendSlotSelected()
   slot = tostring(slot)
   m.broadcast(slot)
   slot = tonumber(slot)
end
  
--- some stuff before we start the loop ---
turtle.select(slot)
print("Ready to be controlled...")
m.open()
--- starting the main loop ---
while true do  -- Start an infinite loop
  event, id, ms = os.pullEvent()  -- wait for an event
  
  if event == "rednet_message" then -- if the event is a rednet message then
	if ms == "ex" then  -- if the message received is "ex" then
	 term.clear()
  term.setCursorPos(1, 1)
  print("Disconnected")
  break   -- break the loop and terminate program
end
if ms == "fw" then
	 sendFuelLevel()
  turtle.forward()
end
if ms == "bw" then
	 sendFuelLevel()
  turtle.back()
end
if ms == "tl" then
	 turtle.turnLeft()
end
if ms == "tr" then
	 turtle.turnRight()
end
if ms == "dig" then
	 turtle.dig()
end
if ms == "place" then
	 turtle.place()
end
if ms == "su" then
	 if slot < 16 then
	  slot = slot + 1
   turtle.select(slot)
  else
	  slot = 1
   turtle.select(slot)
  end
  sendSlotSelected()
end
if ms == "sd" then
	 if slot > 1 then
	  slot = slot - 1
   turtle.select(slot)
  else
	  slot = 16
   turtle.select(slot)
  end
  sendSlotSelected()
end
if ms == "mu" then
	 sendFuelLevel()
  turtle.up()
end
if ms == "md" then
	 sendFuelLevel()
  turtle.down()
end
if ms == "du" then
	 turtle.digUp()
end
if ms == "dd" then
	 turtle.digDown()
end
if ms == "pu" then
	 turtle.placeUp()
end
if ms == "pd" then
	 turtle.placeDown()
end
if ms == "at" then
	 turtle.attack()
end
  end
end
m.close()

here is an image of the gui/tui (its really messy right now, just because i cant figure out a nice way to make one :P/> )
Spoiler

from left to right the buttons are:
D : dig a block in front of the turtle
P: Place a block in front of the turtle
then the fuel level of the turtle

!: move the turtle up
^: move forward
the next ^: turtle slot + 1
A: attack
X: shutdown (terminates the program on both the Pc and Turtle)
< >: turn left or right
the number is the currently selected slot on the turtle
and the red and green ^ are dig and place blocks above
i: move turtle down
V: go backwards
and the next 3 v's are in order, turtle slot -1, dig block below and place block below
the orange box tells you what button you pressed

And for the pastebins
SpoilerFor the Computer script: pastebin get mQK92q07 <name it whatever you like>
for the turtle script: pastebin get KC2ySSXc <name it whatever you like>


to get it to work you have to edit the pc script, and edit the value's: monitor, modem and turtle to your monitorposition, modemposition and turtle ID.

this is my first program so feedback is always appreciated! :D/>

p.s. sorry for my english, i'm dutch!
Heracles421 #2
Posted 03 January 2013 - 07:16 AM
This should go into the Tirtles sub-forum, not here. About the menu, maybe you can make the screen bigger (2x2) and separate the buttons to make the screen look less crowded
Oh, and you can add a function to search for the modem and the monitors automatically to stop limiting the user to a specific side only (If you want help PM me)