Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal
  • 0
kinthiri

Counting ticks since last checked

Question

So this will probably be a very nub question, but I'm struggling with it.

 

I'm building a system to manage my Mekanism Induction Matrix and the Mekanism Reactor that keeps it running. 

 

Managing the reactor isn't a problem. But what I'm trying to do is count the flow of energy into and out of my induction matrix in RF/t. Except I'm having a real hassle figuring out how to count the number of ticks since I last checked.

 

Currently I have a table that is storing and updated each cycle through the app. There is a pause of about 5 seconds in between where the event handler just listens with an event.pull(5), then we go through and update everything again.

 

m{} is the matrix currently being looked at.

cEnergyStored is the current energy stored in the matrix, being compared to m.energyStored which was saved in the last cycle.

cTime = os.time(), and m.energyTime is the value of os.time() last time around.

 

I thought I'd go with traditional energy values. So energy over time.

 

So I'm looking at something along the lines of ...

local cEnergyPerTick = ( ( cEnergyStored - m.energyStored ) / ( cTime - m.energyTime ) ) *0.4

This is multiplied by 0.4 because Mekanism uses Joules and I'm wanting RF. 

 

Problem is, this is miles off the mark. Looking at the wiki, it says time in "in game seconds" (which means nothing to me. I'm assuming that means the 24000 ticks per day) and to get ticks multiply os.time() by ((1000/60/60) - 6000) but this throws the numbers off even further (1000/60/60 may as well be 0 for this purpose.) The closest I can get to a "near" mark of what is actually going through according to the induction matrix UI itself is to multiply by 1.4 instead of 0.4. But even that is still significantly off as to be useless.

 

So question.... I'm new to this and still trying to figure out this math, but how do I calculate the ticks since the last time I checked?

 

os.time() doesn't appear to give me the number of ticks as it did in that other mod I gave up on. os.clock() and os.date() are not even close to useful for this purpose.

 

Sorry if this is very noob. I was expecting os.time() to just present number of ticks since some arbitrary point. (eg, world creation, or daily dawn)

Link to post
Share on other sites

1 answer to this question

Recommended Posts

  • 0
  • Solution

So working on this at 2am was probably a bad idea.

 

After rethinking things and sleeping on it, I think I managed to resolve it, but I'd still like some confirmation from someone that is a little more familiar.

 

Using the same power / time formula above, if I change the os.time() to 

cTime = ((os.time() * 1000) / 60 /60) - 6000

That does appear to give me a more accurate result.

 

Is this the correct formula for working out ticks? 

 

For those that are interested, here is the full function I'm using for monitoring the Induction Matrix from Mekanism. I don't see any reason why it wouldn't work with other power storage with minimal changes. I just happen to only care about the storage of the Induction Matrix. It made my t4 Draconic Capacitor look tiny by comparison, for much less cost/effort.

 

As an explanation, there is a function run when the script starts that iterates through component.list("induction_matrix") (because someday I might have more than one) and adds what it finds to a table with some basic parameters (such as those used in this function.) Then the main loop calls this function (and others) in between sitting idle waiting for event.pull() to get something from the network, or an exit event.

All this goes back to a central computer that sends it on to other parts of the network to display on various screens around the place (such as by my ore processing systems) or that will tell the computer managing the reactor to change properties including injection rate to increase/decrease power production as needed. This is why working out the power consumption is a headache. The reactor tells me what it's producing, but I have to calculate the power consumption through the induction matrix manually as that doesn't expose the power in vs power out itself.

function checkPower()
    local curRFEnergy = 0
    local maxRFEnergy = 0
    local avgRFtick = 0
    local prcntRFEnergy = 0
       
    for i,m in ipairs(imatrix) do 
            local cMatrix = component.proxy(m.address)
            local cTime = ((os.time() * 1000) / 60 / 60 ) - 6000 
            local cEnergyStored = cMatrix.getEnergyStored()
            local cPercentFull = ( cEnergyStored / m.energyMax ) * 100 
            local cEnergyPerTick = ( ( cEnergyStored - m.energyStored ) / ( cTime - m.energyTime ) ) 
            
            curRFEnergy = curRFEnergy + ( cEnergyStored * 0.4 )
            maxRFEnergy = maxRFEnergy + ( m.energyMax * 0.4 )
            avgRFtick = avgRFtick + ( cEnergyPerTick * 0.4 )
            m.energyTime = cTime
            m.energyStored = cEnergyStored
            m.percentFull = cPercentFull
    end
    
    prcntRFEnergy = ((curRFEnergy/maxRFEnergy)*100)    
    return curRFEnergy, avgRFtick, prcntRFEnergy
end

Hopefully this is useful to someone.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.