Different onTick frequencies?

Discussion on the Lua Scripting capabilities for RaceCapture/Pro. Also see the <a href="http://autosportlabs.net/RaceCapturePro_Lua_Scripting">Lua Scripting Guide</a>

Moderators: JeffC, stieg

Post Reply
notso2slo
Posts: 12
Joined: Wed Jan 06, 2016 8:55 pm
Contact:

Different onTick frequencies?

Post 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?

brentp
Site Admin
Posts: 5986
Joined: Wed Jan 24, 2007 6:36 am

Post 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!
Brent Picasso
CEO and Founder, Autosport Labs
Facebook | Twitter

TXBDan
Posts: 68
Joined: Sat Mar 25, 2017 1:43 am

Post 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?

brentp
Site Admin
Posts: 5986
Joined: Wed Jan 24, 2007 6:36 am

Post 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.
Brent Picasso
CEO and Founder, Autosport Labs
Facebook | Twitter

notso2slo
Posts: 12
Joined: Wed Jan 06, 2016 8:55 pm
Contact:

Post 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

wcorredor
Posts: 49
Joined: Thu Oct 27, 2016 6:29 pm

Post 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

notso2slo
Posts: 12
Joined: Wed Jan 06, 2016 8:55 pm
Contact:

Post 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&#40;10&#41;
maxRpmId = addChannel&#40;"MaxRpm", 1,0&#41;

function onTick&#40;&#41;
  maxRev&#40;&#41;

-- Max RPM marker
function maxRev&#40;&#41;
  
  if rpm > maxRpm 
	then
    maxRpm = rpm
    setChannel&#40;maxRpmId, maxRpm&#41;
  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?

Post Reply