Jump to content




Get itens per second entering a turtle


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

#1 ItsRodrick

  • Members
  • 72 posts
  • Location= gps.locate() or "unknown"

Posted 14 February 2015 - 10:16 PM

Hey guys! So, I'm making a epic hyper cobblestone generator. But I want to know how much Itens are entering at a turtle, per second (or minute, then /60 :P)

Do you guys have any idea on how to do it? Like, I know how can I get the item count, detect when a item get into the turtle using os.pullEvent("turtle_inventory")... But I have no idea on how to do the math or something like that.

Could you guys help me? :)

#2 Dog

  • Members
  • 1,179 posts
  • LocationEarth orbit

Posted 14 February 2015 - 10:36 PM

You could count the inventory, start a 60 second timer, then count the inventory again when the timer goes off and divide that by 60. That would tell you how many items/seconds came in during the previous minute.

If you'd like a more 'real-time' count, then you could do the same thing every second instead of every minute (without dividing by 60 obviously).

#3 Lignum

  • Members
  • 558 posts

Posted 14 February 2015 - 10:37 PM

Something along the lines of...
local timer = os.startTimer(1)
local items = 0
local itemsPerSecond = 0

while true do
   local e, t = os.pullEvent()
   if e == "timer" then
      itemsPerSecond = items
      items = 0
      timer = os.startTimer(1)

      print("Current IPS: " .. itemsPerSecond)
   elseif e == "turtle_inventory" then
      items = items + 1
   end
end

This creates a timer that ticks every second. At the same time we're counting the items being inserted into the turtle using turtle_inventory. After a second has passed, store the counted items in a variable, reset and repeat.

EDIT: Ninja'd

Edited by Lignum, 14 February 2015 - 10:37 PM.


#4 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 14 February 2015 - 11:05 PM

Couple of points - you need to make sure your timer is running while you're counting (as Lignum has done), or else you'll got out of sync. You'll want as little time to pass between each timer being caught and then restarted as possible.

Simply relying on turtle_inventory events alone may not suffice if the turtle is being handed stacks of items at a time. I suspect you'll also need to account for items being removed from the turtle's inventory.

#5 ItsRodrick

  • Members
  • 72 posts
  • Location= gps.locate() or "unknown"

Posted 14 February 2015 - 11:08 PM

Ok, Lignum's program doesn't work. It always shows 1-4, but there are entering like, 10+. I think it's because there are entering more than 1 item per time (transfer nodes...)

How could I do it around that?

EDIT: saw Bomb Bloke's post, but still no idea on how to do that.. :|

Edited by RodrickLord, 14 February 2015 - 11:09 PM.


#6 Quintuple Agent

  • Members
  • 107 posts
  • LocationThis page

Posted 14 February 2015 - 11:17 PM

You will probably need to use .getItemCount(slot) inside a loop and save that to a table, then check how much each has changed per update, however the problem arises when taking into account the removal of items.
How are you moving items out of the turtle?

Also, you could have the turtle put all the cobble into a chest, so it's inventory is empty. Then on the next update use the loop to check how much cobble it has and then empty the inventory again and repeat.
Edit: When using a loop to get the item count then dumping the items into a chest, you should take note of how fast the turtle inventory is filling up, just so you can optimize the program.
Ex: If your generator is only filling up slot 1 and maybe slot 2, just have it check the first 2 or 3 slots, no need to check all 16 each time.

Edited by Quintuple Agent, 14 February 2015 - 11:27 PM.


#7 ItsRodrick

  • Members
  • 72 posts
  • Location= gps.locate() or "unknown"

Posted 14 February 2015 - 11:26 PM

Well, I have ExtraUtilities transfer nodes putting itens into a turtle. It's 3 nodes with 3 world interaction upgrades in each.
I'm not taking itens out of the turtle, because by now I want just to get how much itens are entering at it.

#8 KingofGamesYami

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

Posted 14 February 2015 - 11:34 PM

local timer = os.startTimer(1)
local items = 0
local itemsPerSecond = 0

while true do
  local event = { os.pullEvent() }
  if event[ 1 ] == "timer" and event[ 2 ] == timer then
    timer = os.startTimer( 1 )
    local num
    for i = 1, 16 do
      num = num + turtle.getItemCount( i )
    end
    itemsPerSecond = num - items
    items = num
  end
end

Something like this should work.

Edit: Followed BB's advice regarding the timer.

Edited by KingofGamesYami, 15 February 2015 - 03:56 AM.


#9 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 14 February 2015 - 11:36 PM

Well, you could use something along these lines:

local lastCount = 0

while true do
	local myTimer = os.startTimer(1)
	
	local count = 0
	
	for i = 1, 16 do
		count = count + turtle.getItemCount(i)
	end
	
	print(tostring((count - lastCount) * 60).." items per minute.")
	
	lastCount = count
	
	repeat
		local event, par1 = os.pullEvent("timer")
	until par1 == myTimer
end

Of course, you'll still need some method of removing items from the turtle, to prevent it from clogging up. If you're already using another transfer node for that purpose, then things get tricky - the turtle is unlikely to be able to keep track of how many items are entering it vs how many are leaving.

Edit: Ninja'd. But Yami, restart the timer immediately after catching the last one! Don't wait until after you've done your counting!

Edited by Bomb Bloke, 14 February 2015 - 11:37 PM.


#10 Quintuple Agent

  • Members
  • 107 posts
  • LocationThis page

Posted 14 February 2015 - 11:41 PM

Well, might as well throw my code into the ring
local sleepTime=10	 --Time between checks

function dumpItems()
local totalCount=0
local itemCount=0

for slot,1,16 do
  itemCount=0
  turtle.select(slot)
  itemCount = turtle.getItemCount()
  turtle.drop(itemCount)  --Will place items in inventory in front of it change to dropUp or dropDown if needed
  totalCount=totalCount+itemCount
end

return totalCount
end

dumpItems()

while true do
local itemSecond=0

itemSecond=dumpItems()
print(itemSecond/sleepTime.." Cobble/Second")

sleep(sleepTime)
end
This will check every 10 seconds and dump all the items into a chest infront of it

Edited by Quintuple Agent, 14 February 2015 - 11:44 PM.






2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users