I couldn't add any virtual channels, and had to skinny down some comments to get just one virtual channel to fit.
Lua out of memory with two new lines? (Mk1)
But really I needed to add at least another virtual channel, so measured wheel speed (corrected for correlation to GPS speed) and GPS speed were both in kph.
I modified the script to only run the factor calculation when inside the limits, and otherwise just manually set the lights on or off when outside the limits. These improvements let me add that second virtual channel. So this is how it looks now, after a few rounds of improvements:
Code: Select all
--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