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

How to make turtle move a certain distance, via wireless control?

Started by Legovader09, 08 July 2015 - 10:57 PM
Legovader09 #1
Posted 09 July 2015 - 12:57 AM
Hello, so this is the first time experimenting with ComputerCraft scripts, and creating a turtle I have this problem - I can't seem to get the turtle to move forward by a specific amount.
This is what I came up with so far, and it does nothing but give me an error about attempting to compare table.

The wireless computer literally just sends the command (referred to as message below) to the turtle.

The problem is that I can't seem to type something like "forward 10" as my turtle would not pick up on it, as it only responds when it receives "forward" or "f", this is why I am having trouble with the parameters.

Any help would be really appreciated! Thank you in advance.

Regards,
Dom

the "message" seen below refers to this piece of code:

id,message = rednet.receive()


	local args = { message }
	if message == "forward" or message == "f" then
	  if args > "1" then
		for i = 1, args[2] do
		  turtle.forward()
		end
	  else
		turtle.forward()
	  end
	end
HPWebcamAble #2
Posted 09 July 2015 - 01:14 AM
Based on the second bit of code, it looks like 'message' is some sort of table. I think.

Could you post the all the code involved? That would make it much easier to see what you are trying to do.
Legovader09 #3
Posted 09 July 2015 - 02:48 AM
Based on the second bit of code, it looks like 'message' is some sort of table. I think.

Could you post the all the code involved? That would make it much easier to see what you are trying to do.
Okay, the wireless computer's code is here:

rednet.open("back")
while true do
  term.clear()
  term.setCursorPos(1,1)
  print("Command:")
  command = read()
  rednet.send(28,command)
end
And the code for the turtle is as follows:

rednet.open("left")
rednet.send(12,"MineBot now operating")
while true do
  id,message = rednet.receive()
  if id == 7 then
    if message == "mine" then
	  H = 0
	  rednet.send(12,"Mining...")
	  repeat
	    turtle.digDown()
	    turtle.down()
	    turtle.dig()
	    turtle.forward()
	    H = H + 1
	    if turtle.getFuelLevel() == 0 then
		  turtle.refuel()
		  local x, y, z = gps.locate(5)
		  if not x then
		    rednet.send(12,"Could not find location")
		  else
		    rednet.send(12,"MineBot Location: " .. x .. "," .. y .. "," .. z .. " ")
		  end
	    end
	  until H == 50
    end
    if message == "return" then
	  rednet.send(12, "Returning to surface")
	  turtle.turnRight()
	  turtle.turnRight()
	  H = 50
	  repeat
	    turtle.up()
	    sleep(0.1)
	    turtle.forward()
	    sleep(0.1)
	    H = H - 1
	  until H == 0
	  if H == 0 then
	    turtle.turnRight()
	    turtle.turnRight()
	  end
    end
    if message == "180" then
	  turtle.turnRight()
	  turtle.turnRight()
    end
    if message == "forward" or message == "f" then
	  turtle.forward()
    end
    if message == "back" or message == "b" then
	  turtle.back()
    end
    if message == "up" then
	  turtle.up()
    elseif message =="up1" then
	  turtle.up()
	  turtle.forward()
    end
    if message == "down" then
	  turtle.down()
    elseif message == "down1" then
	  turtle.down()
	  turtle.forward()
    end
    if message == "left" then
	  turtle.turnLeft()
    end
    if message == "right" then
	  turtle.turnRight()
    end
    if message == "reboot" then
	  rednet.send(12,"Restarting turtle...")
	  sleep(2)
	  os.reboot()
    end
    if message == "gps" then
	  local x, y, z = gps.locate(5)
	  if not x then
	    rednet.send(12,"Could not find location")
	  else
	    rednet.send(12,"Current location: ".. x .. "," .. y .. "," .. z .. " ")
	  end
    end
    if message == "off" then
	  rednet.send(12,"Powering off...")
	  sleep(1)
	  rednet.send(12,"System is offline.")
	  os.shutdown()
    end
    if message == "save" then
	  rednet.send(12,"Writing to disk...")
	  sleep(2)
	  if disk.isPresent("right") then
	    if disk.hasData("right") then
		  disk.setLabel("right","MineOS 1.0.0")
		  shell.run("cp","startup","disk/MineOS1.0")
		  rednet.send(12,"Success!")
		  sleep(2)
		  rednet.send(12,"Ejecting floppy disk...")
		  disk.eject("right")
	    else
		  rednet.send(12,"No floppy disk found")
	    end
	  elseif disk.isPresent("left") then
  if disk.hasData("left") then
		  disk.setLabel("left","MineOS 1.0.0")
		  shell.run("cp","startup","disk/MineOS1.0")
		  rednet.send(12,"Success!")
		  sleep(2)
		  rednet.send(12,"Ejecting floppy disk...")
		  disk.eject("left")
	    else
		  rednet.send(12,"No floppy disk found")
	    end
	  elseif disk.isPresent("back") then
  if disk.hasData("back") then
		  disk.setLabel("back","MineOS 1.0.0")
		  shell.run("cp","startup","disk/MineOS1.0")
		  rednet.send(12,"Success!")
		  sleep(2)
		  rednet.send(12,"Ejecting floppy disk...")
		  disk.eject("back")
	    else
		  rednet.send(12,"No floppy disk found")
	    end
	  elseif disk.isPresent("front") then
  if disk.hasData("front") then
		  disk.setLabel("front","MineOS 1.0.0")
		  shell.run("cp","startup","disk/MineOS1.0")
		  rednet.send(12,"Success!")
		  sleep(2)
		  rednet.send(12,"Ejecting floppy disk...")
		  disk.eject("front")
	    else
		  rednet.send(12,"No floppy disk found")
	    end
	  elseif disk.isPresent("top") then
  if disk.hasData("top") then
		  disk.setLabel("top","MineOS 1.0.0")
		  shell.run("cp","startup","disk/MineOS1.0")
		  rednet.send(12,"Success!")
		  sleep(2)
		  rednet.send(12,"Ejecting floppy disk...")
		  disk.eject("top")
	    else
		  rednet.send(12,"No floppy disk found")
	    end
	  elseif disk.isPresent("bottom") then
  if disk.hasData("bottom") then
		  disk.setLabel("bottom","MineOS 1.0.0")
		  shell.run("cp","startup","disk/MineOS1.0")
		  rednet.send(12,"Success!")
		  sleep(2)
		  rednet.send(12,"Ejecting floppy disk...")
		  disk.eject("bottom")
	    else
		  rednet.send(12,"No floppy disk found")
	    end
   else
	    rednet.send(12,"No disk drive present")
	  end
    end
    if message == "fuel" then
	  local fuel = turtle.getFuelLevel()
	  rednet.send(12,"Fuel remaining: "..fuel.." blocks")
    end
  end
end

Hope this helps!
KingofGamesYami #4
Posted 09 July 2015 - 04:47 AM
Myself, I'd parse the command before trying to compare it.

For example:

local id,message = rednet.receive()
local command = message:match( "%S+" ) --#take the first set of non-space values
local tArgs = {}
for arg in message:match( command .. " (.+)" ):gmatch( "%S+" ) do --#get everything after the command, iterate through the connected non-space characters (aka "words")
  tArgs[ #tArgs + 1 ] = arg --#...and put them in a table
end

You can then refer to command when comparing to "forwards" or "f", and tArgs[ 1 ] for the distance, if you entered something like:

forward 3

Keep in mind, this does return strings. tonumber will probably be needed for numbers to work properly.
biggest yikes #5
Posted 09 July 2015 - 02:57 PM
You'll want to split the arguments by each space, like the way done in the above post.
That way if you say something like "forward 10" the program will take args[2] as "10" and args[1] as "forward".
Edited on 09 July 2015 - 01:03 PM
jerimo #6
Posted 09 July 2015 - 07:53 PM
If you want a simple way to run complex commands this way I had made a program to do exactly that; http://www.computercraft.info/forums2/index.php?/topic/22278-do-the-ultimate-turtle-usage-program
All you need is to parse it so that each argument is a single entry in a table, then do runProgram(table, 1) and it will do it all for you, including sub programs and the such
Edit: the table needs to be in a format along these lines; {forward, 10} for your example
Edited on 09 July 2015 - 05:54 PM