Check my script please

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
ross2004
Posts: 24
Joined: Mon Sep 18, 2017 3:58 pm
Location: Staunton, VA

Check my script please

Post by ross2004 »

I'm brand new to RCP, and brand new to scripting, so please take it easy on me :D I'm trying to get my scripting in order for my ChumpCar build. I have four things I'm trying to accomplish: activating a warning light (ECT, oil temp, oil press, voltage), control my accusump, start logging at 10mph, and create a virtual channel for fuel level. I've attempted to copy and paste from the scripting examples and modify it to my needs. Thank you!

function checkAutoLogging()
if getGpsSpeed() > 10 then
startLogging()
else
stopLogging()
end
end

--The real analog channel should be named
--something other than FuelLevel
fuel2Id = addChannel("FuelLevel", 10, 0, 0,100,"%")

--change this to make a bigger averaging window
maxAvg = 300
--300 = 10 seconds averaging at 30Hz tick rate

--do not change
fuelAvg={}
fuel2Index = 1

function updateFuelAvg(value)
local i
if #fuelAvg == 0 then
--initialize averaging table
for i = 1, maxAvg do fuelAvg=0 end
end
fuelAvg[fuel2Index] = value
fuel2Index = fuel2Index + 1
if fuel2Index > maxAvg then fuel2Index = 1 end
local sum = 0
for i = 1, #fuelAvg do
sum = sum + fuelAvg
end
setChannel(fuel2Id, sum / maxAvg)
end
end

function accusump()
rpmThreshold = 2500
lowOilPressure = 40
local accusump = 0
if getTimerRpm(0) > rpmThreshold and getAnalog(6) < lowOilPressure then
accusump = 1
end
setGpio(1, accusump)
end
end

function warningLight()
if getAnalog(4) > 215 or getAnalog(6) < 40 or getAnalog(8) < 13 or getAnalog(5) > 275 then
setGpio(0, 1)
else
setGpio(0, 0)
end
end

setTickRate(30)
function onTick()
checkAutoLogging()
updateFuelAvg(getAnalog(3))
accusump()
warningLight()
end

ross2004
Posts: 24
Joined: Mon Sep 18, 2017 3:58 pm
Location: Staunton, VA

Post by ross2004 »

[lua] Gracefully stopping Lua Task
[lua] Destroying Lua State
[lua] Initializing Lua state
[lua] memory usage: 16KB
[lua] Starting Lua Task
[lua] Loading script. Length: 1238
[lua] Startup script error: ([string "function checkAutoLogging() ..."]:36.0: '<eof>' expected near 'end')
[lua] Failure: Failed to load script

Any help?

ross2004
Posts: 24
Joined: Mon Sep 18, 2017 3:58 pm
Location: Staunton, VA

Post by ross2004 »

I feel like I'm talking to myself, but- does this mean my script should run?

[lua] Gracefully stopping Lua Task
[lua] Destroying Lua State
[lua] Initializing Lua state
[lua] memory usage: 16KB
[lua] Starting Lua Task
[lua] Loading script. Length: 1035
timebase/logging/telemetry sample rate: 50/25/10
[lua] Successfully loaded script.

psfp
Posts: 49
Joined: Mon Aug 21, 2017 10:40 pm
Location: DF - Brazil

Post by psfp »

ross2004 wrote:I feel like I'm talking to myself, but- does this mean my script should run?

[lua] Gracefully stopping Lua Task
[lua] Destroying Lua State
[lua] Initializing Lua state
[lua] memory usage: 16KB
[lua] Starting Lua Task
[lua] Loading script. Length: 1035
timebase/logging/telemetry sample rate: 50/25/10
[lua] Successfully loaded script.
Yes, it seems to be running. Looks like you fixed the initial script by deleting some "end" statements.

It's easier to check the script this way:

Code: Select all

function checkAutoLogging&#40;&#41;
    if getGpsSpeed&#40;&#41; > 10 then
        startLogging&#40;&#41;
    else
        stopLogging&#40;&#41;
    end
end

--The real analog channel should be named
--something other than FuelLevel
fuel2Id = addChannel&#40;"FuelLevel", 10, 0, 0,100,"%"&#41;

--change this to make a bigger averaging window
maxAvg = 300
--300 = 10 seconds averaging at 30Hz tick rate

--do not change
fuelAvg=&#123;&#125;
fuel2Index = 1

function updateFuelAvg&#40;value&#41;
    local i
    if #fuelAvg == 0 then
        --initialize averaging table
        for i = 1, maxAvg do fuelAvg&#91;i&#93;=0 end
    end
    fuelAvg&#91;fuel2Index&#93; = value
    fuel2Index = fuel2Index + 1
    if fuel2Index > maxAvg then fuel2Index = 1 end
    local sum = 0
    for i = 1, #fuelAvg do
        sum = sum + fuelAvg&#91;i&#93;
    end
    setChannel&#40;fuel2Id, sum / maxAvg&#41;
end

function accusump&#40;&#41;
    rpmThreshold = 2500
    lowOilPressure = 40
    local accusump = 0
    if getTimerRpm&#40;0&#41; > rpmThreshold and getAnalog&#40;6&#41; < lowOilPressure then
        accusump = 1
    end
    setGpio&#40;1, accusump&#41;
end

function warningLight&#40;&#41;
    if getAnalog&#40;4&#41; > 215 or getAnalog&#40;6&#41; < 40 or getAnalog&#40;8&#41; < 13 or getAnalog&#40;5&#41; > 275 then
        setGpio&#40;0, 1&#41;
    else
        setGpio&#40;0, 0&#41;
    end
end

setTickRate&#40;30&#41;
function onTick&#40;&#41;
    checkAutoLogging&#40;&#41;
    updateFuelAvg&#40;getAnalog&#40;3&#41;&#41;
    accusump&#40;&#41;
    warningLight&#40;&#41;
end
Double check if those analog channels are correct.
--Paulo

ross2004
Posts: 24
Joined: Mon Sep 18, 2017 3:58 pm
Location: Staunton, VA

Post by ross2004 »

Thank you- yes for some reason when I copy/pasted the script I lost the formatting.

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

Post by TXBDan »

I would highly advise triggering the accusump off a dedicated pressure switch and not to rely on the RCP to control the trigger. Frankly, the RCP just isn't reliable enough to entrust with your engine.

ross2004
Posts: 24
Joined: Mon Sep 18, 2017 3:58 pm
Location: Staunton, VA

Post by ross2004 »

Well that's disappointing to hear. The pressure switch is there, I can always use it. My reasoning for having RCP control it was to keep it from discharging oil at idle when the pressure is low.

Post Reply