Jump to content




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

turtle api lua

  • You cannot reply to this topic
5 replies to this topic

#1 Legovader09

  • New Members
  • 2 posts

Posted 08 July 2015 - 11:57 PM

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


#2 HPWebcamAble

  • Members
  • 933 posts
  • LocationWeb Development

Posted 09 July 2015 - 12: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.

#3 Legovader09

  • New Members
  • 2 posts

Posted 09 July 2015 - 01:48 AM

View PostHPWebcamAble, on 09 July 2015 - 12:14 AM, said:

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!

#4 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 09 July 2015 - 03: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.

#5 biggest yikes

  • Members
  • 573 posts

Posted 09 July 2015 - 01: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 by Atenefyr, 09 July 2015 - 02:03 PM.


#6 jerimo

  • Members
  • 74 posts

Posted 09 July 2015 - 06: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.computerc...e-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 by jerimo, 09 July 2015 - 06:54 PM.






3 user(s) are reading this topic

0 members, 3 guests, 0 anonymous users