Page 1 of 1

Different onTick frequencies?

Posted: Wed Apr 26, 2017 3:43 pm
by notso2slo
I have some functions that I want checked 10 times a second, so I use setTickRate(10) to get 10hz. However, other functions are fine at 1hz, and I dont want to have more data than I need.

Is there a way for some functions to run at 10hz and others at 1hz?

Here was my thought, the functions are defined earlier in the code
local timer = 0
setTickRate(10)
function onTick()
overrevMarker()
if timer = 0 then timer = 1 end
if timer = 1 then timer = 2 end
if timer = 2 then timer = 3 end
if timer = 3 then timer = 4 end
if timer = 4 then timer = 5 end
if timer = 5 then timer = 6 end
if timer = 6 then timer = 7 end
if timer = 7 then timer = 8 end
if timer = 8 then timer = 9 end
if timer = 9 then timer = 0 end
If timer = 0 then
startLog()
checkGear()
end
end
That way, some functions are only run every 10th tick?

Posted: Thu Apr 27, 2017 5:15 pm
by brentp
You're close.

You can increment a global variable as a counter and use a % (modulo) operator to trigger a function at a lower rate than the main tick rate.

give that technique a shot and see if that helps!

Posted: Thu Apr 27, 2017 6:28 pm
by TXBDan
How does the LUA tick rate interact with the sample rate setting for that channel?

Say i have a LUA script to read an analog input and the onTick is at 10Hz. What if in my analog channel setup for that channel i have it set to 25Hz?

Posted: Thu Apr 27, 2017 6:32 pm
by brentp
The configured rate for the channel is independent from the rate the lua onTick() runs. In other words, when you get the current analog channel value in Lua, it's independent from the configured rate.

Posted: Wed Sep 13, 2017 10:49 pm
by notso2slo
brentp wrote:You're close.

You can increment a global variable as a counter and use a % (modulo) operator to trigger a function at a lower rate than the main tick rate.

give that technique a shot and see if that helps!
Not a programmer here... Can you give me an example?

Given examples, I can modify to suit my needs. But creating.... Not as good at

Posted: Fri Sep 15, 2017 2:06 am
by wcorredor
This is an example of how to make a function run less often or only every x ticks.

function Every5()
if tk % 5 ~= 0 then --
return
end

{Your function code here run every 5 ticks}

end

function Every10()
if tk % 10 ~= 0 then --
return
end

{Your function code here runs every 10 ticks}

end

tk = 0
setTickRate(25)

function onTick()

tk = tk + 1

Every5()
Every10()

{Your code here runs 25 times per second}

end

Posted: Fri Sep 15, 2017 4:56 pm
by notso2slo
Awesome, thanks! I will stare at that for a while and try to comprehend...

But when I was playing with my code more, I realized there might be a simpler way of reducing the data in my logger for things like max RPM... My main concern is reducing the data points in the logged data to keep reviewing simple.

Code: Select all

setTickRate(10)
maxRpmId = addChannel("MaxRpm", 1,0)

function onTick()
  maxRev()

-- Max RPM marker
function maxRev()
  
  if rpm > maxRpm 
	then
    maxRpm = rpm
    setChannel(maxRpmId, maxRpm)
  end
end
By simply setting the addCchannel to ("MaxRpm", 1,0) it will only log one time a second, even if the number is updated 10 times a second.

So the maximum RPM is calculated and updated 10 times a second, but only logged once a second.

At least I assume that's the way that will work?