RegisterSearchFAQMemberlistUsergroupsLog in
Reply to topic Page 2 of 2
Goto page Previous  1, 2
Help with shift light script
Author Message
Reply with quote
Post  
Glad it is working.

What does the final script look like?

View user's profile Send private message
Reply with quote
Post  
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:
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(ll,ul,RPM)
   factor = (ul - RPM) / (ul - ll)
   blink_period = factor * MAX_BLINK_PERIOD
   return blink_period / loop_time
end

function variblink(ll,ul,RPM,chan)
   if j <= DUTY_CYCLE * ctMAX(ll,ul,RPM) then
      setGpio(chan,0)
   else
      setGpio(chan,10)
   end
   if j < ctMAX(ll,ul,RPM) then
      j = j + 1
   else
      j = 0
   end
end

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
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( name, sampleRate, [precision], [min], [max], [units] )
rpmId = addChannel("RPM", 50, 0, 0, 8000, "RPM")
vltId = addChannel("EcuVolts", 10, 1, 0, 20, "volts")
gearid = addChannel("Gear", 25, 0, 0, 6)
iatId = addChannel("IAT", 1, 0, 0, 120, "F")
ectId = addChannel("EngineTemp", 1, 0, 80, 250, "F")
tpsId = addChannel("TPS", 10, 0, 0, 100, "%")
mapId = addChannel("MAP", 10, 2, -15, 25, "PSI")
injId = addChannel("InjectorPW", 10, 3, 0, 100, "ms")
ignId = addChannel("Ignition", 10, 0, -20, 20, "D")
knkId = addChannel("Knock", 1, 0, 0, 15, "count")
camId = addChannel("CamTiming", 10, 0, -20, 20, "D")
fuel2Id = addChannel("FuelLevel", 10, 0, 0,100,"%")

function toF(value)
 return value * 1.8 + 32
end

function setRpm(value)
RPM = value
setLight(LL1,L12,RPM,1)
setLight(L12,L23,RPM,0)
setLight(L23,UL3,RPM,2)
return value
end

--offset/length in bytes?
--format is: [CAN Id] = function(data) map_chan(<channel id>, data, <CAN offset>, <CAN length>, <multiplier>, <adder>)
CAN_map = {
[1632] = function(data) map_chan(rpmId, data, 0, 2, 1, 0, setRpm) map_chan(vltId, data, 5, 1, 0.1, 0) map_chan(gearid, data, 0, 1, 1, 0) end,
[1633] = function(data) map_chan(iatId, data, 0, 2, 1, 0, toF) map_chan(ectId, data, 2, 2, 1, 0, toF) end,
[1634] = function(data) map_chan(tpsId, data, 0, 2, 1, 0) map_chan(mapId, data, 2, 2, 0.0145037738, -14.7) end,
[1635] = function(data) map_chan(injId, data, 0, 2, 0.001, 0) map_chan(ignId, data, 2, 2, 1, 0) end, 
[1637] = function(data) map_chan(knkId, data, 0, 2, 1, 0) end,
[1638] = function(data) map_chan(camId, data, 2, 2, 1, 0) end
}

setTickRate(FREQ_HZ)
j = 0

function onTick()
--RPM=testRPM
processCAN(CAN_chan)
--setLight(LL1,L12,RPM,1)
--setLight(L12,L23,RPM,0)
--setLight(L23,UL3,RPM,2)
end

--===========do not edit below===========
function processCAN(chan)
    repeat
        local id, e, data = rxCAN(chan)
        if id ~= nil then
            local map = CAN_map[id]
            if map ~= nil then
                map(data)         
            end
        end
    until id == nil
end

--Map CAN channel, big endian format
function map_chan(cid, data, offset, len, mult, add, filter)
    offset = offset + 1
    local value = 0
    while len > 0 do
        value = (value * 256) + data[offset]
        offset = offset + 1
        len = len - 1
    end
 local cv = value * mult + add
 if filter ~= nil then cv = filter(cv) end
    setChannel(cid, cv)
end

initCAN(CAN_chan, CAN_baud)


View user's profile Send private message
Reply with quote
Post  
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/racecapture-firmware-2-11-0-racecapture-app-1-9-0-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
Founder, Autosport Labs
Facebook | Twitter
View user's profile Send private message Send e-mail
Reply with quote
Post  
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:
--virtual channels
SpeedGPS = addChannel("SpeedGPS", 25, 2, 0, 200, "KPH")
SpeedWhl = addChannel("SpeedWhl", 25, 2, 0, 200, "KPH")

-- 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 (1000ms to 1500ms suggested)--
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(ll,ul,rpm)
   factor = (ul - rpm) / (ul - ll)
   blink_period = factor * MAX_BLINK_PERIOD
   return blink_period / loop_time
end
function variblink(ll,ul,rpm,chan)
   if j <= DUTY_CYCLE * ctMAX(ll,ul,rpm) then
      setAnalogOut(chan,10)
   else
      setAnalogOut(chan,0)
   end
   if j < ctMAX(ll,ul,rpm) then
      j = j + 1
   else
      j = 0
   end
end
function setLight(ll,ul,rpm,chan)
   if rpm < ll then
        setAnalogOut(chan,10)
    elseif rpm >= ul then
        setAnalogOut(chan,0)
   else
   variblink(ll,ul,rpm,chan)   
   end
end   
-- Ini --
setTickRate(FREQ_HZ)
j = 0
-- RUN --
function onTick()
setChannel(SpeedGPS, getGpsSpeed()*1.61)
setChannel(SpeedWhl, getTimerFreq(1)*0.92)
RPM=getTimerRpm(0)
--RPM=testRPM

setLight(LL1,L12,RPM,0)
setLight(L12,L23,RPM,1)
setLight(L23,UL3,RPM,2)
end


View user's profile Send private message
Reply with quote
Post  
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
Founder, Autosport Labs
Facebook | Twitter
View user's profile Send private message Send e-mail
Reply with quote
Post  
No, blinking is not working. I will use the solid for now, by I will work on the blink and come back with results.

View user's profile Send private message
Reply with quote
Post  
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
Founder, Autosport Labs
Facebook | Twitter
View user's profile Send private message Send e-mail
Display posts from previous:
Reply to topic Page 2 of 2
Goto page Previous  1, 2
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