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

Help Required for Farming System

Started by Kobra, 08 May 2015 - 05:36 PM
Kobra #1
Posted 08 May 2015 - 07:36 PM
ok I am writting a code system that uses 1 main computer craft computer for acting as the system interface and 9 wireless farming turtles for my planting

my farm system has been built like this
KEY
B = Brick
F = Farmland
W = Water
P = Piston
C = Computer
T = Turtle

Top Down View
Level
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
BBWWWBWWWBWWWBWWWBWWWBWWWBWWWBWWWBWWWBB
BBPPPBPPPBPPPBPPPBPPPBPPPBPPPBPPPBPPPBB
BWFFFWFFFWFFFWFFFWFFFWFFFWFFFWFFFWFFFWB
BWFFFWFFFWFFFWFFFWFFFWFFFWFFFWFFFWFFFWB
BWFFFWFFFWFFFWFFFWFFFWFFFWFFFWFFFWFFFWB
BWFFFWFFFWFFFWFFFWFFFWFFFWFFFWFFFWFFFWB
BWFFFWFFFWFFFWFFFWFFFWFFFWFFFWFFFWFFFWB
BWFFFWFFFWFFFWFFFWFFFWFFFWFFFWFFFWFFFWB
BWFFFWFFFWFFFWFFFWFFFWFFFWFFFWFFFWFFFWB
-1 BWFFFWFFFWFFFWFFFWFFFWFFFWFFFWFFFWFFFWB
-1 BWFFFWFFFWFFFWFFFWFFFWFFFWFFFWFFFWFFFWB
-1 BWFFFWFFFWFFFWFFFWFFFWFFFWFFFWFFFWFFFWB
-1 BWFFFWFFFWFFFWFFFWFFFWFFFWFFFWFFFWFFFWB
-1 BWFFFWFFFWFFFWFFFWFFFWFFFWFFFWFFFWFFFWB
-1 BWFFFWFFFWFFFWFFFWFFFWFFFWFFFWFFFWFFFWB
-1 BWFFFWFFFWFFFWFFFWFFFWFFFWFFFWFFFWFFFWB
-2 BWFFFWFFFWFFFWFFFWFFFWFFFWFFFWFFFWFFFWB
-2 BWFFFWFFFWFFFWFFFWFFFWFFFWFFFWFFFWFFFWB
-2 BWFFFWFFFWFFFWFFFWFFFWFFFWFFFWFFFWFFFWB
-2 BWFFFWFFFWFFFWFFFWFFFWFFFWFFFWFFFWFFFWB
-2 BWFFFWFFFWFFFWFFFWFFFWFFFWFFFWFFFWFFFWB
-2 BWFFFWFFFWFFFWFFFWFFFWFFFWFFFWFFFWFFFWB
-2 BWFFFWFFFWFFFWFFFWFFFWFFFWFFFWFFFWFFFWB
-2 BBFFFBFFFBFFFBFFFBFFFBFFFBFFFBFFFBFFFBB
-3 BWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWB
-2 BBBTBBBTBBBTBBBTBBBTBBBTBBBTBBBTBBBTBBC

Side View

BBP
BWP
BBB
BBBFFFFFFF 0
BBBBBBBBBBFFFFFFF T -1
BBBBBBBBBBBBBBBBBFFFFFFFF B -2
BBBBBBBBBBBBBBBBBBBBBBBBBWB -3
BBBBBBBBBBBBBBBBBBBBBBBBBBB -4

thats how I have my farming system set up for the turtles now what I am hoping to do, is to have it so
the computer can send a message to one of the turtles to sow that row with seeds then using the same
computer to send via a wireless redstone block or via a computer to the pistons so that when the crop
is ready it can auto harvest by sending a message to the row that is harvesting so only one row can be
sent or so all the row can be harvested but I am only a beginner at LUA Coding and would like some help
with coding this system

so far I have worked out a way to get the turtles to line up with north south east and west with this
code below now I am sure it can be done with less work then I have done but as I say I do not known LUA
that well so I could do with some help with trimming what is not needed and keeping what is needed I
would love to be able to make it use GPS for the X/Y/Zcoord


xcoord = 0 --East/West
zcoord = 0 --North/South
ycoord = 64 --Height
xTarget   -- East and  West
zTarget  -- North and South
yTarget  --Height
zDiff = { -1, 0, 1, 0}
xDiff = { 0, 1,0, -1 }
Ori = 1
Ors = {"North", "East", "South", "West"}
function look(dir)
  while dir ~= Ors[Ori]
  right()
  end
end
function goto(xTarget, zTarget, yTarget)
  while yTarget < ycoord do
    moveDown()
  end
  while yTarget > ycoord do
    moveUp()
  end
  if xTarget < xcoord then
    look("West")
    while xTarget < xcoord do
    moveForward()
    end
  end 
   if xTarget > xcoord then
    look("East")
    while xTarget > xcoord do
    moveForward()
    end
  end 
  if zTarget < zcoord then
    look("North")
    while zTarget < zcoord do
    moveForward()
    end
  end 
   if zTarget > zcoord then
    look("South")
    while zTarget > zcoord do
    moveForward()
    end
  end	 
end

function left()
   Ori = Ori - 1
   Ori = (Ori - 1) % 4
   Ori = Ori + 1
turtle.turnLeft()  
end
function right()
   Ori = Ori - 1
   Ori = (Ori + 1) % 4
   Ori = Ori + 1
turtle.turnRight()  
end
function moveForward()
   xcoord = xcoord + xDiff[Ori]
   zcoord = zcoord + zDiff[Ori]
   moved = false
	 while not(moved) do
	  moved = turtle.forward()
	 end
end
function moveUp()
   ycoord = ycoord + 1
   moved = false
	 while not(moved) do
	   moved = turtle.up()
	 end	 
end
function moveDown()
   ycoord = ycoord - 1
   moved = false
	 while not(moved) do
	  moved = turtle.down()
   end  
end
goto()
Creator #2
Posted 08 May 2015 - 08:19 PM
Can you please add a screenshot. ;)/>
Lupus590 #3
Posted 08 May 2015 - 08:32 PM
Sounds like you need something to keep track of the turtles position in the world.
KingofGamesYami recently completed a rewrite of Lama (Location aware movement API) as part of the Hive project.
(original lama forum post - please don't necro)
Use this to download Yami's lama, he even wrote a wiki

pastebin run vyBeH8ZV lama
Even if you don't use Yami's code, looking at it may help give you direction.
[/samelessPlug]

As for the rest of your farm, I would recommend getting the turtle to do a row of wheat at a time, checking if there is a block under him before planting (if yes, assume it's soil and he needs to go up)
If the water behaves, you could only uproot one row at a time too.

Write and test your code in bits too, get the turtle to do one row on one level, the tell him how to go up to the next level in the same row, then how to go to the next row.
Edited on 08 May 2015 - 06:35 PM
Bomb Bloke #4
Posted 09 May 2015 - 09:28 AM
This sort of thing:

moved = false
         while not(moved) do
          moved = turtle.forward()
         end

… can be shrunk a bit:

while not turtle.forward() do end

… or even to:

repeat until turtle.forward()

You can also crunch your goto function down somewhat:

function goto(xTarget, zTarget, yTarget)
  while yTarget < ycoord do moveDown() end
  while yTarget > ycoord do moveUp() end

  while xTarget ~= xcoord do
    if xTarget < xcoord then look("West") else look("East") end
    moveForward()
  end

  while zcoord ~= zTarget do
    if zTarget < zcoord then look("North") else look("South") end
    moveForward()
  end
end

And even this sort of thing:

if zTarget < zcoord then look("North") else look("South") end

… can be condensed further with the techniques discussed here:

look(zTarget < zcoord and "North" or "South")

All in all, though, the basic logic you're using seems sound. :)/>

Regarding use of a GPS, here's an example of the sort of code most all of my turtle scripts execute on startup:

local pos, facings = {}, {"north","east","south","west"}

-- Ping the GPS servers until I get a valid reading:
do
        local tempx, tempy, tempz
        while tempx == nil or tempy == nil or tempz == nil do
                tempx, tempy, tempz = gps.locate(5)
                sleep(5)
        end

        while not turtle.forward() do while not turtle.turnLeft() do end end
        pos.x,pos.y,pos.z = gps.locate(5)

        if pos.x < tempx then pos.direction = 4
        elseif pos.x > tempx then pos.direction = 2
        elseif pos.z < tempz then pos.direction = 1
        else pos.direction = 3 end
end

print("I'm at "..pos.x..","..pos.y..","..pos.z..", I have "..turtle.getFuelLevel().." fuel and I'm facing "..facings[pos.direction]..".")

I then just have them update the variables in the pos table as they move around. Note the use of elseif, by the way.
Kobra #5
Posted 10 May 2015 - 08:55 PM
http://sta.sh/218vi7lb7mf6 Here is the Screenshots for it and Lupus590 suggestion looks promissing I will let you known more when I have tried it out some and give you a update on what is happening