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

Get itens per second entering a turtle

Started by ItsRodrick, 14 February 2015 - 09:16 PM
ItsRodrick #1
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? :)/>
Dog #2
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).
Lignum #3
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 on 14 February 2015 - 09:37 PM
Bomb Bloke #4
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.
ItsRodrick #5
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 on 14 February 2015 - 10:09 PM
Quintuple Agent #6
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 on 14 February 2015 - 10:27 PM
ItsRodrick #7
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.
KingofGamesYami #8
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 on 15 February 2015 - 02:56 AM
Bomb Bloke #9
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 on 14 February 2015 - 10:37 PM
Quintuple Agent #10
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 on 14 February 2015 - 10:44 PM