Lua script sub-functions?

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
GTIspirit
Posts: 233
Joined: Wed Jan 09, 2013 11:20 am
Location: SE Michigan

Lua script sub-functions?

Post by GTIspirit »

What is the syntax for writing a Lua script function (sub-function?)?

I see a number of built-in Functions, but I would like to write my own sub-function for use within one of the existing Functions. Specifically, I want to write a sub-function that will calculate a value 0 to 100 based on engine speed between low and high limits. The math is the easy part, the exact syntax to define it is what I need help with.

What I'm getting at is staged, variable duty cycle shift light. :D Because then I don't need to repeat the above manual syntax three times, I could just call the same sub-function for each of the three LED's. :wink:

GTIspirit
Posts: 233
Joined: Wed Jan 09, 2013 11:20 am
Location: SE Michigan

Post by GTIspirit »

In Excel format this is the calculation I want to make as a sub-function:
=IF((UL-PP)>(UL-LL),0,IF((UL-PP)<(0),1,1-(UL-PP)/(UL-LL)))

Where
UL is the upper rpm limit
LL is the lower rpm limit
PP is the process point (actual engine speed)

I guess this would be a few lines of code, which I don't want to repeat for each of the LED's, that is why I was asking how to make sub-functions in Lua scripting.

So if you're following me here, when engine speed is below the lower limit the LED would be off. When it is between the lower and upper limits the light would blink with varying on duration, faster as it approaches the upper limit. When it is above the upper limit then the light stays on. 8)

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

Post by brentp »

writing a function in lua is as simple as going:

Code: Select all

function add_two_numbers&#40;a,b&#41;
   return a + b
end
you can include these into your overall script to provide useful capabilities.

The RaceCapture/Pro uses pretty standard Lua scripting, so any of the Lua scripting reference / tutorials will help.

For example, this might help: http://luatut.com/crash_course.html

We have an example of flashing LEDs above a certain point; let me ping a team member and they might be able to help.
Brent Picasso
CEO and Founder, Autosport Labs
Facebook | Twitter

stieg
Posts: 100
Joined: Fri Dec 20, 2013 1:37 am
Location: Madison, WI
Contact:

Shift lights in Lua

Post by stieg »

Here is a script I wrote for our Lemons car. It controls our shift lights by controlling the duty cycle of the PWM outputs. The outputs were acting as current sinks, so on (duty cycle at 100%) meant the lights were off, and vice versa at 0%. The script is attached.

Code: Select all

--&#91;&#91;
Cheesy RCP script

Inputs as wired on 2014-12-05 -- Stieg

Input
Timer 0 - RPM

Tach Lights
0 - Green Light
1 - Yellow Light
2 - Red Light

&#93;&#93;


-- Globals --
LIGHTS = 3
LIGHT_VALS = &#123;&#125;
FREQUENCY_HZ = 25

-- Global Init --
setTickRate&#40;FREQUENCY_HZ&#41;

function toggleLight&#40;index&#41;
   LIGHT_VALS&#91;index&#93; = not LIGHT_VALS&#91;index&#93;
end

function lightOn&#40;index&#41;
   LIGHT_VALS&#91;index&#93; = true
end

function lightOff&#40;index&#41;
   LIGHT_VALS&#91;index&#93; = false
end

local lCount = 0
repeat
   lightOff&#40;lCount&#41;
   lCount = lCount + 1
until lCount >= LIGHTS

-- Script --
function updateLights&#40;&#41;
   local l = 0
   repeat
      -- Convert our booleans to 1 &#40;true&#41; or 0 &#40;false&#41; --
      local val = LIGHT_VALS&#91;l&#93;
      local dc = 0
      if not val then
         dc = 100
      end
      setPwmDutyCycle&#40;l, dc&#41;

      l = l + 1
   until l >= LIGHTS
end

function checkRpm&#40;&#41;
   local rpm = getTimerRpm&#40;0&#41;
   if rpm < 3000 then
        lightOff&#40;0&#41;
        lightOff&#40;1&#41;
        lightOff&#40;2&#41;
   elseif 3000 < rpm and rpm < 4500 then
	lightOn&#40;0&#41;
        lightOff&#40;1&#41;
        lightOff&#40;2&#41;
   elseif 4500 < rpm and rpm < 5000 then
        lightOn&#40;0&#41;
        lightOn&#40;1&#41;
        lightOff&#40;2&#41;
   elseif 5000 < rpm and rpm < 5500 then
        lightOn&#40;0&#41;
        lightOn&#40;1&#41;
        lightOn&#40;2&#41;
   else
        toggleLight&#40;0&#41;
        toggleLight&#40;1&#41;
        toggleLight&#40;2&#41;
   end
end


function controlLights&#40;&#41;
   checkRpm&#40;&#41;
   updateLights&#40;&#41;
end

-- Start!

function onTick&#40;&#41;
   controlLights&#40;&#41;
end
Andrew Stiegmann (Stieg)
Principal Engineer
Autosport Labs Inc.

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

Post by brentp »

Blog post here:

http://www.autosportlabs.com/sequential ... apturepro/

Note, I learned from Stieg that the red flashing LED is controlled separately, so I updated the code in the how-to so that it can control a 4th flashing indicator, just like you see in the video. Enjoy!
Brent Picasso
CEO and Founder, Autosport Labs
Facebook | Twitter

Post Reply