5 posts
Posted 04 August 2012 - 08:04 AM
I'm attempting to make a hivemind style set of mining turtles, I would like a turtle to broadcast a message when the front and top/bottom are clear, then all machines that are also done would broadcast a response I assume simultaneously, is it possible to count all the responses to see if all turtles are done and if so how, If this is obvious/simple sorry but I'm relatively new to lua and am doing this mostly to help my understanding of rednet.
49 posts
Posted 04 August 2012 - 10:19 AM
You could do something like this:
local count = 0
while count <= 10 do
local id, msg = rednet.receive (TIMEOUT) -- replace TIMEOUT with whatever number suits your case
if check_message (msg) -- where check_message is a function of yours which checks, whether the received message was indeed another turtle reporting in
count = count + 1
end
end
1 posts
Posted 04 August 2012 - 02:39 PM
You could do something like this:
local count = 0
while count <= 10 do
local id, msg = rednet.receive (TIMEOUT) -- replace TIMEOUT with whatever number suits your case
if check_message (msg) -- where check_message is a function of yours which checks, whether the received message was indeed another turtle reporting in
count = count + 1
end
end
That would work, or if you wanted to know what turtles were done mining (by ID) you could do this:
local done = {}
while table.maxn(done) <= 10 do
local id, msg = rednet.receive (TIMEOUT) -- replace TIMEOUT with whatever number suits your case
if check_message (msg) -- where check_message is a function of yours which checks, whether the received message was indeed another turtle reporting in
table.insert(done,id)
end
end
Then to print all the ids you could do:
for i=1,table.maxn(done) do
print(done[i])
end
-FalconDestroyer18
5 posts
Posted 05 August 2012 - 07:29 AM
So far Ponder your code doesn't seem to be working, heres the code i'm working with
rednet.open("right")
local id = 0
function waiting()
print("waiting")
local count = 0
while count ~= 7 do
local ids, msg = rednet.receive(.01)
if msg == "done" then
count = count + 1
if count == 7 then
turtle.broadcast("continue")
end
end
end
end
while true do
event, p1, p2 = os.pullEvent("rednet_message")
if p2 == "start" then
turtle.turnLeft()
turtle.turnLeft()
turtle.suck()
turtle.refuel()
turtle.turnLeft()
turtle.turnLeft()
for i=0, ((turtle.getFuelLevel()-2)/2) do
while turtle.detect() do
turtle.dig()
end
while turtle.detectUp() do
turtle.digUp()
end
while turtle.detectDown() do
turtle.digDown()
end
print("broadcasting")
rednet.broadcast("finished")
waiting()
print("finished waiting")
while p2 ~= "continue" do
event, p1, p2 = os.pullEvent("rednet_message")
if p2 == "finished" then
rednet.broadcast("done")
print("broadcasting done")
end
print("moving forward")
turtle.forward()
end
end
end
end
It gets to the waiting and then stops I think the issue is they are all running waiting() at the exact same time which means none of them respond to each other and are stuck waiting for each other to respond. I can't think of a good way to handle this, part of the point of this program is I would like them to all act as one, moving together and mining together, however without communicating with each other things like gravel will hold up some but not all and separate them so this is important to it. I hope this is possible.
49 posts
Posted 05 August 2012 - 01:26 PM
Yeah, the problem is, that they all get to the first call of waiting, but at that point no turtle has ever send a "done" message and even if they would have missed it anyway since the didn't listen up. You should probably make a function which constantly checks for incoming messages and run it in the background with the
parallel API.
while turtle.detect() do
turtle.dig()
end
while turtle.detectUp() do
turtle.digUp()
end
while turtle.detectDown() do
turtle.digDown()
end
I don't know why you are using while here, the turtle can only detect and mine blocks directly near itself, so dig () would be called only once anyway. So I'd either put it in if clauses or just call them.
if turtle.detect () then
turtle.dig ()
end
if turtle.detectUp () then
turtle.digUp ()
end
if turtle.detectDown () then
turtle.digDown ()
end
-- or simply
turtle.dig ()
turtle.digUp ()
turtle.digDown ()
Also, are you missing an "end" somewhere or is the indention just unfortunate?
Why exactly is my code not working? Have you defined a function check_message like it says in the comment? Otherwise it is almost like your code, so that's the only thing I can think of which could go wrong.
5 posts
Posted 05 August 2012 - 07:29 PM
the indention just got messed up and the while detect() dig is due to gravel if there is cobble above them, and gravel above that they will break the cobble move forward and then the gravel will block their path back so this way it should clear out all the gravel without it blocking their path, same with the forward one, if its gravel then it will dig, attempt to move forward and then fail, but the rest of the bot's around will move forward separating them, over a larger distance this becomes worse, and i'm going for the moving as one, so that is to take care of that, i'm reading the parallel api, i'm not exactly sure how it works, would i want it to be something like this
parallel.waitForAny(waiting(),
print("finished waiting")
while p2 ~= "continue" do
event, p1, p2 = os.pullEvent("rednet_message")
if p2 == "finished" then
rednet.broadcast("done")
print("broadcasting done")
end
print("moving forward")
end
)
turtle.forward()
Sorry i'm not really sure on the formatting for parallel, I'm trying this now hoping it works for me
5 posts
Posted 05 August 2012 - 08:16 PM
I can't seem to get parallel to work this is my current code
function waiting()
print("waiting")
local count = 0
while count ~= 7 do
local ids, msg = rednet.receive(.01)
if msg == "done" then
count = count + 1
if count == 7 then
turtle.broadcast("continue")
end
end
end
end
function responding()
rednet.broadcast("finished")
while true do
event, p1, p2 = os.pullEvent("rednet_message")
if p2 == "finished" then
rednet.broadcast("done")
print("broadcasting done")
end
end
end
function finished()
event, p1, p2 = os.pullEvent()
while p2 ~= "continue" do
end
end
while true do
event, p1, p2 = os.pullEvent("rednet_message")
if p2 == "start" then
turtle.turnLeft()
turtle.turnLeft()
turtle.suck()
turtle.refuel()
turtle.turnLeft()
turtle.turnLeft()
for i=0, ((turtle.getFuelLevel()-2)/2) do
while turtle.detect() do
turtle.dig()
end
while turtle.detectUp() do
turtle.digUp()
end
while turtle.detectDown() do
turtle.digDown()
end
print("broadcasting")
parallel.waitForAny(waiting(),responding(),finished())
print("moving forward")
turtle.forward()
end
end
end
I can't think of whats going wrong, it should be running the responding function and the waiting function together so it would work, but it still stops at waiting and doesn't continue on, is my syntax correct for parallel.waitForAny() and what am I doing wrong, also on a side not, am I correct in assuming that the event, p1, p2 are different in each function? or am I mixing up events because that could be one of my issues.