Virtual Channel for Gear Calculator

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
rob_h
Posts: 19
Joined: Thu Mar 13, 2014 4:18 am
Location: United States

Virtual Channel for Gear Calculator

Post by rob_h »

Does anyone have the math behind determining which gear I'm in based on engine RPM and vehicle speed?

I have the final drive, tire diameter, and trans gear ratios so I know I have all the variables.

L Cubed
Posts: 24
Joined: Wed Aug 06, 2014 9:11 pm

Post by L Cubed »

Here is the math your are looking for:

( RPM / GearRatio / FinalDrive ) * TireDiameter * pi / 1056 = Speed
Where Tire Diameter is in inches and speed is in MPH.

I wrote up some Lua Script code you can try if you want. It creates a virtual channel (which only works with Version 2 firmware as far as I know) called "Analog1" and will give you the active gear at 5 Hz sample rate. This is setup as a 5 speed so if you more or less just copy the a gear row and change it to 6th or delete the ones you don't need/use.
Attachments
Gear Calc Script.txt
(1.62 KiB) Downloaded 154 times

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

Post by brentp »

This is great, thank you. We'll add this to the wiki!
Brent Picasso
CEO and Founder, Autosport Labs
Facebook | Twitter

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

Post by brentp »

added here: http://autosportlabs.net/RaceCapturePro ... alculation

ping us if you want access to the wiki for more scripts; we have account creation currently locked down due to the spam bot hordes.
Brent Picasso
CEO and Founder, Autosport Labs
Facebook | Twitter

Ls_Rx7
Posts: 23
Joined: Tue Jan 08, 2013 12:18 am
Location: Washington

Post by Ls_Rx7 »

Is anyone using this script or having any issues with it?

I tried using this script with 2.8.5 and 1.3.13 but am getting an error message. "lua: startup script error: ([string " ..."]:83.0: malformed number near '1stGear')"

Here is the script I am trying.

--below values are constants for the vehicle
local 1stGear = 2.66
local 2ndGear = 1.78
local 3rdGear = 1.3
local 4thGear = 1.0
local 5thGear = 0.74
local 6thGear = 0.50
local FinalDrive = 4.10
--diameter in inches
local TireDia = 25.1
--allowable error of gear ratio to allow for measurement variation
local gearErr = 0.1
local rpmSpeedRatio = 0
--initialized to 0 so if it doesn't work you know
local gearPos = 0 --this is the gear channel variable

function onTick() --updates gear position every second by default

--assumes Pulse Input channel one is for the RPM signal and speed in MPH
local speed = getGpsSpeed()
local rpm = getTimerRpm(0)

--this part only works for firmware version 2.0 per the RCP page
gearId = addChannel("Gear",5)

if speed > 10 then
--makes sure your rolling so as not to divide by 0

rpmSpeedRatio = (rpm/speed)/(FinalDrive*1056/(TireDia*3.14159))

if ((1stGear - rpmSpeedRatio)^2) < (gearErr^2) then gearPos = 1 end
if ((2ndGear - rpmSpeedRatio)^2) < (gearErr^2) then gearPos = 2 end
if ((3rdGear - rpmSpeedRatio)^2) < (gearErr^2) then gearPos = 3 end
if ((4thGear - rpmSpeedRatio)^2) < (gearErr^2) then gearPos = 4 end
if ((5thGear - rpmSpeedRatio)^2) < (gearErr^2) then gearPos = 5 end
if ((6thGear - rpmSpeedRatio)^2) < (gearErr^2) then gearPos = 6 end

else gearPos = 0 end

setChannel(gearId, gearPos) --outputs to virtual channel

end


I also put the script in http://ideone.com/RvZmRF and received the error message

" Compilation error time: 0 memory: 0 signal:0 luac: prog.lua:2: <name> expected near '1' " on line 2 of the script.

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

Post by brentp »

Youre right, that script won't work out of the box; you cannot start a variable name with a number. try prefixing all of the variable names with an underscore to get around the issue perhaps?
Brent Picasso
CEO and Founder, Autosport Labs
Facebook | Twitter

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

Post by brentp »

We fixed the variable names in the script here; it still needs to be tested in an actual environment:

http://autosportlabs.net/RaceCapturePro ... alculation

Let us know how it works out!
Brent Picasso
CEO and Founder, Autosport Labs
Facebook | Twitter

Post Reply