Posted: Wed May 03, 2017 2:50 am
Glad it is working.
What does the final script look like?
What does the final script look like?
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(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)
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