RegisterSearchFAQMemberlistUsergroupsLog in
Reply to topic Page 1 of 1
Lua script sub-functions?
Author Message
Reply with quote
Post Lua script sub-functions? 
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. Very Happy 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

View user's profile Send private message
Reply with quote
Post  
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. Cool

View user's profile Send private message
Reply with quote
Post  
writing a function in lua is as simple as going:

Code:
function add_two_numbers(a,b)
   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
View user's profile Send private message Send e-mail
Reply with quote
Post Shift lights in Lua 
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:
--[[
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

]]


-- Globals --
LIGHTS = 3
LIGHT_VALS = {}
FREQUENCY_HZ = 25

-- Global Init --
setTickRate(FREQUENCY_HZ)

function toggleLight(index)
   LIGHT_VALS[index] = not LIGHT_VALS[index]
end

function lightOn(index)
   LIGHT_VALS[index] = true
end

function lightOff(index)
   LIGHT_VALS[index] = false
end

local lCount = 0
repeat
   lightOff(lCount)
   lCount = lCount + 1
until lCount >= LIGHTS

-- Script --
function updateLights()
   local l = 0
   repeat
      -- Convert our booleans to 1 (true) or 0 (false) --
      local val = LIGHT_VALS[l]
      local dc = 0
      if not val then
         dc = 100
      end
      setPwmDutyCycle(l, dc)

      l = l + 1
   until l >= LIGHTS
end

function checkRpm()
   local rpm = getTimerRpm(0)
   if rpm < 3000 then
        lightOff(0)
        lightOff(1)
        lightOff(2)
   elseif 3000 < rpm and rpm < 4500 then
   lightOn(0)
        lightOff(1)
        lightOff(2)
   elseif 4500 < rpm and rpm < 5000 then
        lightOn(0)
        lightOn(1)
        lightOff(2)
   elseif 5000 < rpm and rpm < 5500 then
        lightOn(0)
        lightOn(1)
        lightOn(2)
   else
        toggleLight(0)
        toggleLight(1)
        toggleLight(2)
   end
end


function controlLights()
   checkRpm()
   updateLights()
end

-- Start!

function onTick()
   controlLights()
end



_________________
Andrew Stiegmann (Stieg)
Principal Engineer
Autosport Labs Inc.
View user's profile Send private message Send e-mail Visit poster's website
Reply with quote
Post  
Blog post here:

http://www.autosportlabs.com/sequential-shift-light-with-flashing-alert-on-racecapturepro/

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
View user's profile Send private message Send e-mail
Display posts from previous:
Reply to topic Page 1 of 1
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum