Help with shift light script

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

boggie1688
Posts: 138
Joined: Fri Apr 07, 2017 3:47 pm
Location: Oakland, CA

Post by boggie1688 »

Glad it is working.

What does the final script look like?

PopKorn78
Posts: 62
Joined: Mon Mar 16, 2015 2:07 am

Post by PopKorn78 »

Here it is, I changed it for the ligths part to try to have them blinking. I used another script that was on the forum. Right now, they are only solid, but at least the work and I have RPM on the tablet.

Code: Select all

FREQ_HZ = 30
loop_time = 1000/ FREQ_HZ
MAX_BLINK_PERIOD = 1000
DUTY_CYCLE = 0.2

--testRPM = 2000

LL1 = 6600
L12 = 7000
L23 = 7500
UL3 = 8000

function ctMAX&#40;ll,ul,RPM&#41;
	factor = &#40;ul - RPM&#41; / &#40;ul - ll&#41;
	blink_period = factor * MAX_BLINK_PERIOD
	return blink_period / loop_time
end

function variblink&#40;ll,ul,RPM,chan&#41;
	if j <= DUTY_CYCLE * ctMAX&#40;ll,ul,RPM&#41; then
		setGpio&#40;chan,0&#41;
	else
		setGpio&#40;chan,10&#41;
	end
	if j < ctMAX&#40;ll,ul,RPM&#41; then
		j = j + 1
	else
		j = 0
	end
end

function setLight&#40;ll,ul,RPM,chan&#41;
	if RPM < ll then
		setGpio&#40;chan,0&#41;
	elseif RPM >= ul then
		setGpio&#40;chan,10&#41;
	else
	variblink&#40;ll,ul,RPM,chan&#41;
	end
end

--the CAN baud rate 
CAN_baud = 250000 
--CAN channel to listen on. 0=first CAN channel, 1=second 
CAN_chan = 0 

--virtual channels
--addChannel&#40; name, sampleRate, &#91;precision&#93;, &#91;min&#93;, &#91;max&#93;, &#91;units&#93; &#41;
rpmId = addChannel&#40;"RPM", 50, 0, 0, 8000, "RPM"&#41; 
vltId = addChannel&#40;"EcuVolts", 10, 1, 0, 20, "volts"&#41; 
gearid = addChannel&#40;"Gear", 25, 0, 0, 6&#41;
iatId = addChannel&#40;"IAT", 1, 0, 0, 120, "F"&#41; 
ectId = addChannel&#40;"EngineTemp", 1, 0, 80, 250, "F"&#41; 
tpsId = addChannel&#40;"TPS", 10, 0, 0, 100, "%"&#41; 
mapId = addChannel&#40;"MAP", 10, 2, -15, 25, "PSI"&#41; 
injId = addChannel&#40;"InjectorPW", 10, 3, 0, 100, "ms"&#41; 
ignId = addChannel&#40;"Ignition", 10, 0, -20, 20, "D"&#41; 
knkId = addChannel&#40;"Knock", 1, 0, 0, 15, "count"&#41; 
camId = addChannel&#40;"CamTiming", 10, 0, -20, 20, "D"&#41; 
fuel2Id = addChannel&#40;"FuelLevel", 10, 0, 0,100,"%"&#41;

function toF&#40;value&#41;
 return value * 1.8 + 32
end

function setRpm&#40;value&#41;
RPM = value
setLight&#40;LL1,L12,RPM,1&#41;
setLight&#40;L12,L23,RPM,0&#41;
setLight&#40;L23,UL3,RPM,2&#41;
return value
end 

--offset/length in bytes? 
--format is&#58; &#91;CAN Id&#93; = function&#40;data&#41; map_chan&#40;<channel id>, data, <CAN offset>, <CAN length>, <multiplier>, <adder>&#41; 
CAN_map = &#123; 
&#91;1632&#93; = function&#40;data&#41; map_chan&#40;rpmId, data, 0, 2, 1, 0, setRpm&#41; map_chan&#40;vltId, data, 5, 1, 0.1, 0&#41; map_chan&#40;gearid, data, 0, 1, 1, 0&#41; end, 
&#91;1633&#93; = function&#40;data&#41; map_chan&#40;iatId, data, 0, 2, 1, 0, toF&#41; map_chan&#40;ectId, data, 2, 2, 1, 0, toF&#41; end, 
&#91;1634&#93; = function&#40;data&#41; map_chan&#40;tpsId, data, 0, 2, 1, 0&#41; map_chan&#40;mapId, data, 2, 2, 0.0145037738, -14.7&#41; end, 
&#91;1635&#93; = function&#40;data&#41; map_chan&#40;injId, data, 0, 2, 0.001, 0&#41; map_chan&#40;ignId, data, 2, 2, 1, 0&#41; end,  
&#91;1637&#93; = function&#40;data&#41; map_chan&#40;knkId, data, 0, 2, 1, 0&#41; end, 
&#91;1638&#93; = function&#40;data&#41; map_chan&#40;camId, data, 2, 2, 1, 0&#41; end 
&#125; 

setTickRate&#40;FREQ_HZ&#41;
j = 0

function onTick&#40;&#41;
--RPM=testRPM
processCAN&#40;CAN_chan&#41;
--setLight&#40;LL1,L12,RPM,1&#41;
--setLight&#40;L12,L23,RPM,0&#41;
--setLight&#40;L23,UL3,RPM,2&#41;
end

--===========do not edit below=========== 
function processCAN&#40;chan&#41; 
    repeat 
        local id, e, data = rxCAN&#40;chan&#41; 
        if id ~= nil then 
            local map = CAN_map&#91;id&#93; 
            if map ~= nil then 
                map&#40;data&#41;          
            end 
        end 
    until id == nil 
end 

--Map CAN channel, big endian format 
function map_chan&#40;cid, data, offset, len, mult, add, filter&#41; 
    offset = offset + 1 
    local value = 0 
    while len > 0 do 
        value = &#40;value * 256&#41; + data&#91;offset&#93; 
        offset = offset + 1 
        len = len - 1 
    end 
 local cv = value * mult + add
 if filter ~= nil then cv = filter&#40;cv&#41; end
    setChannel&#40;cid, cv&#41;
end 

initCAN&#40;CAN_chan, CAN_baud&#41; 

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

Post by brentp »

Hey, glad you got it working. You know, you can take your script to the next level by moving the CAN mappings to the new direct CAN mapping scheme:

https://www.autosportlabs.com/racecaptu ... -released/

For now you'll need to pluck RPM via lua scripting, but you can move all of your other channels to the direct CAN mapping capabilities, which will lighten up your script and improve responsiveness.

For the blinking, I'm looking at this function:

function setLight(ll,ul,RPM,chan)
if RPM < ll then
setGpio(chan,0)
elseif RPM >= ul then
setGpio(chan,10)
else
variblink(ll,ul,RPM,chan)
end

I'm a bit unsure of the conditions that would actually cause variblink() to be called. Do you have a link to the original blinking shift light script you found that ostensibly worked?

You could try to put a debug print statement righ before the variblink() line to see if it ever gets called:

function setLight(ll,ul,RPM,chan)
if RPM < ll then
setGpio(chan,0)
elseif RPM >= ul then
setGpio(chan,10)
else
println("variblink!")
variblink(ll,ul,RPM,chan)
end

and then watch the RCP log for those messages.
Brent Picasso
CEO and Founder, Autosport Labs
Facebook | Twitter

PopKorn78
Posts: 62
Joined: Mon Mar 16, 2015 2:07 am

Post by PopKorn78 »

Hi Brent, I used the script from GTSpirit. I'm using it for a base to improve that what I really want.

In the end, what I want is to have the lights to be solid while the RPM rises and the all lights to start blinking when RPM gets to 500 of the upper limit.

IE:
LL1: 6200 (Solid Green)
LL2: 6700 (Solid Green and Yellow)
LL3: 7200 (Solid Green, Yellow and Red)
LL4: 7500 (Blinking Green, Yellow and Red)

After that, I will be looking to have the Green and Yellow lights to blink individually as warning lights for Oil Pressur and Engine Temp, but that will be for later.

Code: Select all

--virtual channels 
SpeedGPS = addChannel&#40;"SpeedGPS", 25, 2, 0, 200, "KPH"&#41; 
SpeedWhl = addChannel&#40;"SpeedWhl", 25, 2, 0, 200, "KPH"&#41; 

-- CONSTANTS -- 
FREQ_HZ = 25  --1000/FREQ_HZ=loop time in ms. e.g. 25Hz=40ms-- 
loop_time = 1000 / FREQ_HZ 
MAX_BLINK_PERIOD = 1200 --in ms &#40;1000ms to 1500ms suggested&#41;-- 
DUTY_CYCLE = 0.5 
--testRPM = 5200  --test interface-- 
LL1 = 5000 --lower limit to start blinking light 1-- 
L12 = 6000 --sets light 1 on, starts blinking light 2-- 
L23 = 6500 --sets light 2 on, starts blinking light 3-- 
UL3 = 7000 --light 3 upper limit above which goes solid-- 
-- FUNCTIONS -- 
function ctMAX&#40;ll,ul,rpm&#41; 
   factor = &#40;ul - rpm&#41; / &#40;ul - ll&#41; 
   blink_period = factor * MAX_BLINK_PERIOD 
   return blink_period / loop_time 
end 
function variblink&#40;ll,ul,rpm,chan&#41; 
   if j <= DUTY_CYCLE * ctMAX&#40;ll,ul,rpm&#41; then 
      setAnalogOut&#40;chan,10&#41; 
   else 
      setAnalogOut&#40;chan,0&#41; 
   end 
   if j < ctMAX&#40;ll,ul,rpm&#41; then 
      j = j + 1 
   else 
      j = 0 
   end 
end 
function setLight&#40;ll,ul,rpm,chan&#41; 
   if rpm < ll then 
        setAnalogOut&#40;chan,10&#41; 
    elseif rpm >= ul then 
        setAnalogOut&#40;chan,0&#41; 
   else 
   variblink&#40;ll,ul,rpm,chan&#41;    
   end 
end    
-- Ini -- 
setTickRate&#40;FREQ_HZ&#41; 
j = 0 
-- RUN -- 
function onTick&#40;&#41; 
setChannel&#40;SpeedGPS, getGpsSpeed&#40;&#41;*1.61&#41; 
setChannel&#40;SpeedWhl, getTimerFreq&#40;1&#41;*0.92&#41; 
RPM=getTimerRpm&#40;0&#41; 
--RPM=testRPM 

setLight&#40;LL1,L12,RPM,0&#41; 
setLight&#40;L12,L23,RPM,1&#41; 
setLight&#40;L23,UL3,RPM,2&#41; 
end

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

Post by brentp »

Got it. Does using this original script at least blink your LEDs as designed? That would be the first base-line test I would recommend.

Unfortunately at the moment I'm not set up to precisely emulate the environment, so it's hard for me to test it.
Brent Picasso
CEO and Founder, Autosport Labs
Facebook | Twitter

PopKorn78
Posts: 62
Joined: Mon Mar 16, 2015 2:07 am

Post by PopKorn78 »

No, blinking is not working. I will use the solid for now, by I will work on the blink and come back with results.

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

Post by brentp »

Got it. You might want to ping the original author of the script to see if there's an updated one that fixes any bugs, maybe he has something quick for you!
Brent Picasso
CEO and Founder, Autosport Labs
Facebook | Twitter

Post Reply