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

Satelite builder

Started by dustpuppy, 06 October 2012 - 02:57 PM
dustpuppy #1
Posted 06 October 2012 - 04:57 PM
Hi,
to get a turtle reading the right GPS position you need to setup a GPS host system with 4 computers, like this http://www.computercraft.info/forums2/index.php?/topic/3088-how-to-guide-gps-global-position-system/
To do this on every world could be a lot of work.
So i' ve made a little program, that build a satelite system in 100 meter height.
Program needs to be started with x,y,z coordinates and facing of the turtle. Then simply follow the instructions to fill the slots.

Hope it's usefull.


local tArgs = { ... }
if #tArgs < 4 then
  print("Usage : satelite <x> <y> <z> <f>")
  return
end
local Xpos = tonumber(tArgs[1])
local Ypos = tonumber(tArgs[2])
local Zpos = tonumber(tArgs[3])
local facing = tonumber(tArgs[4])

function buildUp(floppySlot)
  -- Put the disk drive
  turtle.select(3)
  turtle.place()
  -- Load disk into drive and write startup file
  turtle.select(floppySlot)
  turtle.drop()
  file = fs.open("disk/startup","w")
  file.writeLine("shell.run(\"gps\",\"host\","..Xpos..","..Ypos..","..Zpos..")")
  file.close()
  -- put the computer
  turtle.up()
  turtle.select(2)
  turtle.place()
  -- place the modem
  turtle.up()
  turtle.up()
  turtle.forward()
  turtle.select(4)
  turtle.placeDown()
  turtle.back()
  turtle.down()
  turtle.down()
  -- turn computer on
  computer = peripheral.wrap("front")
  sleep(0.5)
  if computer then
    computer.turnOn()
  end
  turtle.down()
end

if type(Xpos) ~= "number" then
  print("x must be number")
  return
end
if type(Ypos) ~= "number" then
  print("y must be number")
  return
end
if type(Zpos) ~= "number" then
  print("z must be number")
  return
end
if type(facing) ~= "number" then
  print("facing must be number")
  return
end
print("Please fill up following slots")
print("1 = Fuel")
print("2 = Computer (4x)")
print("3 = Disk drive (4x)")
print("4 = Modem (4x)")
print("5 - 8 = Floppy disk (4x)")
print("Press any key to continue")
while true do
  event, param1 = os.pullEvent()
  if event == "char" then
    break
  end
end
while turtle.getItemCount(1) > 0 do
  turtle.refuel()
end
print("Fuel level is "..turtle.getFuelLevel())
print("Going up to 100 meter to build satelite")
for height = 1, 100, 1 do
  turtle.up()
end
Ypos = Ypos + 101
-- Go 10 forward to set the first computer
for c = 1, 10, 1 do
  turtle.forward()
end
if facing == 0 then
  Zpos = Zpos + 11
end
if facing == 2 then
  Zpos = Zpos - 11
end
if facing == 1 then
  Xpos = Xpos - 11
end
if facing == 3 then
  Xpos = Xpos + 11
end
buildUp(5)
-- Go back 20 and put the second computer
turtle.turnRight()
turtle.turnRight()
if facing == 0 then
  facing = 2
elseif facing == 1 then
  facing = 3
elseif facing == 2 then
  facing = 0
elseif facing == 3 then
  facing = 1
end
for c = 1, 20, 1 do
  turtle.forward()
end
if facing == 0 then
  Zpos = Zpos + 22
end
if facing == 2 then
  Zpos = Zpos - 22
end
if facing == 1 then
  Xpos = Xpos - 22
end
if facing == 3 then
  Xpos = Xpos + 22
end
buildUp(6)
-- go 10 forward to the middle
turtle.turnRight()
turtle.turnRight()
if facing == 0 then
  facing = 2
elseif facing == 1 then
  facing = 3
elseif facing == 2 then
  facing = 0
elseif facing == 3 then
  facing = 1
end
for c = 1, 10, 1 do
  turtle.forward()
end
if facing == 0 then
  Zpos = Zpos + 11
end
if facing == 2 then
  Zpos = Zpos - 11
end
if facing == 1 then
  Xpos = Xpos - 11
end
if facing == 3 then
  Xpos = Xpos + 11
end
-- and 10 more up
for c = 1, 10, 1 do
  turtle.up()
end
Ypos = Ypos + 10
-- turn 90 degree to setup the second row
turtle.turnRight()
facing = facing + 1
if facing == 4 then
  facing = 0
end
-- Go 10 forward to set the third computer
for c = 1, 10, 1 do
  turtle.forward()
end
if facing == 0 then
  Zpos = Zpos + 11
end
if facing == 2 then
  Zpos = Zpos - 11
end
if facing == 1 then
  Xpos = Xpos - 11
end
if facing == 3 then
  Xpos = Xpos + 11
end
buildUp(7)
-- Go back 20 and put the last computer
turtle.turnRight()
turtle.turnRight()
if facing == 0 then
  facing = 2
elseif facing == 1 then
  facing = 3
elseif facing == 2 then
  facing = 0
elseif facing == 3 then
  facing = 1
end
for c = 1, 20, 1 do
  turtle.forward()
end
if facing == 0 then
  Zpos = Zpos + 22
end
if facing == 2 then
  Zpos = Zpos - 22
end
if facing == 1 then
  Xpos = Xpos - 22
end
if facing == 3 then
  Xpos = Xpos + 22
end
buildUp(8)
-- go 10 forward to the middle
turtle.turnRight()
turtle.turnRight()
for c = 1, 10, 1 do
  turtle.forward()
end
-- and back to earth
while not turtle.detectDown() do
  turtle.down()
end
Sebra #2
Posted 06 October 2012 - 08:33 PM
1. Hide code under spoiler, please.
2. Why 100 only? You should use maximum height for maximum efficiency.
3. I think modems on top is slightly more efficient. :D/>/>
dustpuppy #3
Posted 06 October 2012 - 08:40 PM
If i did it more height, i need to calculate from the starting point. If player stands on a hill, it could end over 256 and then it will not work anymore. The modem is on top of the computer.
sjele #4
Posted 06 October 2012 - 08:45 PM

while true do
  turtle.up()
  if not turtle.up() then
	break
  end
end

--Rest of code
dustpuppy #5
Posted 06 October 2012 - 09:24 PM
Here a new version. Now it's going up to 251, no matter where it starts.

Spoilerlocal tArgs = { … }

if #tArgs < 4 then
print("Usage : satelite <x> <y> <z> <f>")
return
end

local Xpos = tonumber(tArgs[1])
local Ypos = tonumber(tArgs[2])
local Zpos = tonumber(tArgs[3])
local facing = tonumber(tArgs[4])


function buildUp(floppySlot)
– Put the disk drive
turtle.select(3)
turtle.place()

– Load disk into drive and write startup file
turtle.select(floppySlot)
turtle.drop()

file = fs.open("disk/startup","w")
file.writeLine("shell.run(\"gps\",\"host\","..Xpos..","..Ypos..","..Zpos..")")
file.close()

– put the computer
turtle.up()
turtle.select(2)
turtle.place()

– place the modem
turtle.up()
turtle.up()
turtle.forward()
turtle.select(4)
turtle.placeDown()
turtle.back()
turtle.down()
turtle.down()

– turn computer on
computer = peripheral.wrap("front")
sleep(0.5)
if computer then
computer.turnOn()
end

turtle.down()
end


if type(Xpos) ~= "number" then
print("x must be number")
return
end

if type(Ypos) ~= "number" then
print("y must be number")
return
end

if type(Zpos) ~= "number" then
print("z must be number")
return
end

if type(facing) ~= "number" then
print("facing must be number")
return
end

print("Please fill up following slots")
print("1 = Fuel")
print("2 = Computer (4x)")
print("3 = Disk drive (4x)")
print("4 = Modem (4x)")
print("5 - 8 = Floppy disk (4x)")
print("Press any key to continue")

while true do
event, param1 = os.pullEvent()
if event == "char" then
break
end
end

while turtle.getItemCount(1) > 0 do
turtle.refuel()
end

print("Fuel level is "..turtle.getFuelLevel())
local height = 240 - Ypos
print("Going up "..height.." to build satelite")

for c = 1, height, 1 do
turtle.up()
end
Ypos = Ypos + height + 1

– Go 10 forward to set the first computer
for c = 1, 10, 1 do
turtle.forward()
end

if facing == 0 then
Zpos = Zpos + 11
end
if facing == 2 then
Zpos = Zpos - 11
end
if facing == 1 then
Xpos = Xpos - 11
end
if facing == 3 then
Xpos = Xpos + 11
end

buildUp(5)

– Go back 20 and put the second computer
turtle.turnRight()
turtle.turnRight()
if facing == 0 then
facing = 2
elseif facing == 1 then
facing = 3
elseif facing == 2 then
facing = 0
elseif facing == 3 then
facing = 1
end

for c = 1, 20, 1 do
turtle.forward()
end

if facing == 0 then
Zpos = Zpos + 22
end
if facing == 2 then
Zpos = Zpos - 22
end
if facing == 1 then
Xpos = Xpos - 22
end
if facing == 3 then
Xpos = Xpos + 22
end

buildUp(6)

– go 10 forward to the middle
turtle.turnRight()
turtle.turnRight()
if facing == 0 then
facing = 2
elseif facing == 1 then
facing = 3
elseif facing == 2 then
facing = 0
elseif facing == 3 then
facing = 1
end

for c = 1, 10, 1 do
turtle.forward()
end

if facing == 0 then
Zpos = Zpos + 11
end
if facing == 2 then
Zpos = Zpos - 11
end
if facing == 1 then
Xpos = Xpos - 11
end
if facing == 3 then
Xpos = Xpos + 11
end

– and 10 more up
for c = 1, 10, 1 do
turtle.up()
end
Ypos = Ypos + 10

– turn 90 degree to setup the second row
turtle.turnRight()
facing = facing + 1
if facing == 4 then
facing = 0
end

– Go 10 forward to set the third computer
for c = 1, 10, 1 do
turtle.forward()
end

if facing == 0 then
Zpos = Zpos + 11
end
if facing == 2 then
Zpos = Zpos - 11
end
if facing == 1 then
Xpos = Xpos - 11
end
if facing == 3 then
Xpos = Xpos + 11
end

buildUp(7)

– Go back 20 and put the last computer
turtle.turnRight()
turtle.turnRight()
if facing == 0 then
facing = 2
elseif facing == 1 then
facing = 3
elseif facing == 2 then
facing = 0
elseif facing == 3 then
facing = 1
end

for c = 1, 20, 1 do
turtle.forward()
end

if facing == 0 then
Zpos = Zpos + 22
end
if facing == 2 then
Zpos = Zpos - 22
end
if facing == 1 then
Xpos = Xpos - 22
end
if facing == 3 then
Xpos = Xpos + 22
end

buildUp(8)

– go 10 forward to the middle
turtle.turnRight()
turtle.turnRight()
for c = 1, 10, 1 do
turtle.forward()
end

– and back to earth
while not turtle.detectDown() do
turtle.down()
end
Sebra #6
Posted 06 October 2012 - 09:34 PM
You are learn fast :D/>/>
It is possible to use 1 disk, 1 drive and do not leave it.
dustpuppy #7
Posted 06 October 2012 - 09:38 PM
But if i leave it, it's possible to update the computers by a turtle with easy changing the boot disk.
ChunLing #8
Posted 09 October 2012 - 07:09 PM
Nice. You can tighten this up a lot though, the array doesn't need to be that big to get accurate triangulation, since the rednet range is relatively short (making it so big just means you lose a few extra blocks of effective range). An array of just having each computer placed one block from the center would work fine out to the range limit, but then it would be too difficult to place the modems. Also, for purposes of rednet, the computer, not the modem, is the point of origin. So you should put the computers as high as possible (if you put the modems on the side–or front like I ended up doing, they're just as high anyway).

You can also make it so that the turtle automatically detects existing GPS signals or just sets itself as the new center (and East, the positive x axis, as the turtle facing). It can do a gps.locate() from the center, then move forward one and do another, and subtract the results of the first to get the facing. I like this because I'm lazy and I don't like being told I have to put in extra parameters.
dustpuppy #9
Posted 09 October 2012 - 08:43 PM
I've made it that size, because it's only a part of my real satelite system. I am doing a storage system, where i have another server in the middle and some other systems like a server storing all positions of all turtles are also on work.
ChunLing #10
Posted 10 October 2012 - 06:08 PM
Your central control server can be at ground level and still be in range to communicate with the GPS array. All you have to do is program them to act as relays rather than just responding to GPS PING messages.
slango20 #11
Posted 11 October 2012 - 07:14 PM
Your central control server can be at ground level and still be in range to communicate with the GPS array. All you have to do is program them to act as relays rather than just responding to GPS PING messages.
how would you do that? I'm planning on a massive storage and retrieval room, where a turtle acts in place of logistics pipes, and attacks and kills mobs that wander in (if they are in its path)
dustpuppy #12
Posted 11 October 2012 - 07:33 PM
Your central control server can be at ground level and still be in range to communicate with the GPS array. All you have to do is program them to act as relays rather than just responding to GPS PING messages.

Then i have to change the GPS api. I want to keep the original apis.
Sebra #13
Posted 11 October 2012 - 08:25 PM
copy &amp; edit
ChunLing #14
Posted 11 October 2012 - 10:41 PM
You don't need to actually edit the gps API, you can just make a new program that both responds to "PING" messages and messages that have the form "^(RELAY:)(%d+)(.*)$" (or something like that) by stripping off "RELAY:" and the number and sending the remaining message to the ID matching that number. Then have your array programed to use that instead of the regular GPS program.

True, I'm not bothering to do that myself, at least not yet. My interests in turtles run mainly in the direction of using them to help carry out the grunt work in minecraft, not creating some kind of endless self-replicating army controlled by a central computer. But you don't need a new API, just a slightly improved version of an existing program.
evilguard #15
Posted 17 October 2012 - 04:07 AM
If i did it more height, i need to calculate from the starting point. If player stands on a hill, it could end over 256 and then it will not work anymore. The modem is on top of the computer.

hums, Would be nice if the programs actually ask for actual GPS coordinate and then calculate about that? So that don't mean anything is the turtle is on a mountain in a quarry hole :D/>/>

Just saying. nice programs btw :P/>/>
ChunLing #16
Posted 17 October 2012 - 05:30 AM
The version I include in User Friendly Program combining several useful functions doesn't worry about the initial height, it just goes up till it hits the height limit and presumes that to be 255 unless an exiting GPS system tells it otherwise. It also asks for the starting coordinates and facing during the launch prep.

I also have a version of the relay host, but I'd like to flesh it out with more functions.
MrZuribachi #17
Posted 17 October 2012 - 03:54 PM
A GPS-System is quite usefull (I think), but I'm quite confused about it. Could you add a demonstration-video please? Would be very kind
ChunLing #18
Posted 17 October 2012 - 04:51 PM
I can't because I'm too lazy to post a video that will take longer for you to watch and explain less than simply copy/pasting the code into a turtle's folder and running it. But I'm not too lazy to post an explanation of what the code does.
Spoiler
local x1Ps,y1Ps,z1Ps,x2Ps,z2Ps = 0,255,0,1,0
local function gpsnode(xPos,yPos,zPos)
  turtle.select(2)
  turtle.place()
  for i=4,7 do if turtle.getItemCount(i) > 0 then turtle.select(i) break end end
  turtle.drop()
  strtfl = fs.open("disk/startup","w")
  strtfl.writeLine("shell.run("gps","host","..xPos..","..yPos..","..zPos..")")
  strtfl.close()
  turtle.up()
  turtle.forward()
  turtle.turnLeft()
  turtle.back()
  turtle.select(1)
  turtle.place()
  turtle.back()
  turtle.select(3)
  turtle.place()
  turtle.turnLeft()
  turtle.forward()
  turtle.turnRight()
  turtle.forward()
  turtle.forward()
  turtle.turnRight()
  peripheral.call("front","turnOn")
  turtle.down()
end
local function plcaxis(xPos,yPos,zPos)
  x1Ps,y1Ps,z1Ps = gps.locate(2,true)
  turtle.forward()
  if x1Ps then --there is a GPS system, use it to determine coordinates and facing
   x2Ps,yPos,z2Ps = gps.locate(2)
   x2Ps,z2Ps = x2Ps-x1Ps,z2Ps-z1Ps
  else x1Ps,y1Ps,z1Ps = xPos,yPos,zPos end
  turtle.down()
  gpsnode(x1Ps+(x2Ps*2),y1Ps,z1Ps+(z2Ps*2))
-- Go back 2 and set the second node
  turtle.turnRight()
  turtle.turnRight()
  for c=1,2 do turtle.forward() end
  gpsnode(x1Ps-(x2Ps*2),y1Ps,z1Ps-(z2Ps*2))
end
print([[Fill inventory as follows
Slot 1 > 4+ Computers
Slot 2 > 4+ Disk drives
Slot 3 > 4+ Wireless Modems (4x)
Slots 4-7 > Floppy disks
Enter turtle's current x]])
local x3Ps = tonumber(io.read())
print("Enter turtle's current z")
local z3Ps = tonumber(io.read())
print("Enter turtle's current facing")
for i=0, tonumber(io.read()) do turtle.turnLeft() end
while turtle.getFuelLevel() < 500 do
  print("Low Fuel Warning")
  print("Place fuel in Slot 16 and press Enter")
  io.read()
  turtle.select(16)
  print("Current fuel level is"..turtle.getFuelLevel())
  if not turtle.refuel(turtle.getItemCount(16)) then
   print("Slot "..cldt.slt.." does not contain fuel")
  else print("New fuel level is"..turtle.getFuelLevel()) end
end
-- launch turtle
while turtle.up() do end
plcaxis(x3Ps,255,z3Ps)
-- Go back to the middle and turn
x2Ps,z2Ps = 0,1
turtle.back()
turtle.turnRight()
plcaxis(x3Ps,254,z3Ps)
while turtle.down() do end
In the first line, we declare some variables. These variables are intended to hold default values for xyz coordinates and additional xz coordinates 1 meter from the xz coordinates to indicate a displacement of 1 meter in a horizontal direction (east, in this case)

Then we have a function called gpsnode. This function causes the turtle to place a block from slot two (which should contain disk drives), put an item from slot 4-7 (which should be disks) into the inventory of the placed block, write a startup file (which will start the gps hosting program with coordinates passed to the function as arguments) to a disk in a disk drive placed next to the turtle(which we have just done if the slots were loaded correctly), and then proceed to place a block from slot one (which should contain computers) on the top of the previous block and a block from slot 3 (which should contain modems) on the side of that block. The turtle then maneuvers back around to the block it placed from slot 1 (which should be a computer) and tries to turn it on. It then goes down one meter to be exactly in the position and facing it was when it started this function.

Then we have a function called plcaxis which also takes xyz coordinates as arguments. This function assigns our programs local xyz coordinate variables to be equal to the output of a gps.locate call with a two second timeout (and verbose output). Then the turtle moves forward one meter and checks if it got coordinate values or nil from the gps.locate call. If it has coordinates, it makes another gps.locate call and subtracts the xz coordinates from the previous coordinates to get xz coordinates that will indicate one meter of horizontal movement. If it got nil from the first gps.locate, it just assumes that the coordinates are those initially passed to the function. The turtle then positions itself and calls the gpsnode function defined above at coordinates equal to its starting coordinates plus double the one meter displacement coordinates calculated or passed to the function. Then it turns around, goes to the other side of the current axis, and places another gpsnode at its starting coordinates minus double the displacement coordinates.

These functions defined, the turtle resumes it's main program, first outputing a description of how you are to set up the inventory and asking you to input some numbers representing the x and z coordinates and the facing. On receiving the facing, it spins around till the facing equals 3 (based on the value you entered for facing). If it doesn't have a sufficient quantity of fuel to make the trip, it asks for fuel until it has enough.

Then it goes up till it can go up no more. When it can go up no more, it calls plcaxis, starting in what you told it was an easterly direction. It then resets the presumed xz displacement and y coordinates, returns to the center, turns 90 degrees, and calls plcaxis again (it will end each plcaxis call one meter lower than it started it, so the new axis is one meter below the first as well as rotated 90 degrees). Then, work presumably finished, it will descend until it can descend no more.

If you loaded the turtle correctly and gave it good coordinates, then you now have a working four point GPS array with the largest range possible. If you put the wrong blocks/items in the slots, then you now have a non working dummy array that will mock you from above whenever the sun is high and the weather clear. Oh, and if you launched this indoors, then you have an array on your ceiling, where you can admire its design but where it will not do you very much good.
dustpuppy #19
Posted 17 October 2012 - 06:40 PM
Take a look into my other threat.
http://www.computercraft.info/forums2/index.php?/topic/5002-turtle-control-and-chest-inventory-system/
Snablesnot #20
Posted 02 November 2012 - 01:14 AM
I am new to CC and lua. however I don't understand why it would not just be simpler to create 4 of the plain wireless turtles and have a program that uses them as the gps.
launch program on turtle 1 and enter world coordinates. and tell it the 3 other ids of fellow gps turtles and give it some fuel let it rocket to the stars.
start the program on the other 3 turtles and tell it the id of its master coordinator turtle (turtle 1) and they position themselves accordingly.

Would this make sense?
ChunLing #21
Posted 02 November 2012 - 11:37 AM
It is pretty simple, and I have gps serving capability on my turtles for just that reason. But turtles don't work as hosts with the regular gps program, you'd have to write your own (not that it's hard). And you still need a program to put them in place, cause they're way up there.

Overall, I guess it comes down to an iron v. redstone resource issue. Do you use a lot of redstone, and have iron to spare? Then do this with wireless turtles.

I should note that I have a program that lets you recover the disk drives too, so really resource-wise it's all on the side of the computers. Programming side it's still a split between writing a turtle gps server and having all the file transfers and stuff, but since programs are kinda infinitely reusable I see it as a non-issue.
Edited on 02 November 2012 - 10:42 AM