Help with shift light script
Posted: Wed Apr 19, 2017 2:16 am
Hi all, I need help to get my shift light to work.
With my script, I get this error:
[lua] Script error: [string "tickrate = 30..."]:35.0: attempt to compare number with nil
If I change line 35 from "local r = rpm" to "local r = rpmId", I'm not getting any errors, but the shift light do not work.
Here is the script:
tickRate = 30
--the CAN baud rate
CAN_baud = 250000
--CAN channel to listen on. 0=first CAN channel, 1=second
CAN_chan = 0
ledState = 0
--change this to make a bigger averaging window
maxAvg = 300
--300 = 10 seconds averaging at 30Hz tick rate
--do not change
fuelAvg={}
fuel2Index = 1
--add your virtual channels here
rpmId = addChannel("RPM", 25, 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 Shift()
local r = RPM
if r > 6300 then setGpio(2,1) else setGpio(2,0) end
if r > 6800 then setGpio(1,1) else setGpio(1,0) end
if r > 7300 then setGpio(0,1) else setGpio(0,0) end
end
function updateFuelAvg(value)
local i
if #fuelAvg == 0 then
--initialize averaging table
for i = 1, maxAvg do fuelAvg=0 end
end
fuelAvg[fuel2Index] = value
fuel2Index = fuel2Index + 1
if fuel2Index > maxAvg then fuel2Index = 1 end
local sum = 0
for i = 1, #fuelAvg do
sum = sum + fuelAvg
end
setChannel(fuel2Id, sum / maxAvg)
end
function Logging()
if getGpsSpeed() > 10 then
startLogging()
else
stopLogging()
end
end
--customize here for CAN channel mapping
--offset/length in bytes?
--format is: [CAN Id] = function(data) map_chan(<channel id>, data, <CAN offset>, <CAN length>, <multiplier>, <adder>)
CAN_map = {
--did not bother logging gear speed and target cam angle
[1632] = function(data) map_chan(rpmId, data, 0, 2, 1, 0) 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
}
function onTick()
processCAN(CAN_chan)
Shift()
updateFuelAvg(getAnalog(0))
Logging()
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)
setTickRate(tickRate)
With my script, I get this error:
[lua] Script error: [string "tickrate = 30..."]:35.0: attempt to compare number with nil
If I change line 35 from "local r = rpm" to "local r = rpmId", I'm not getting any errors, but the shift light do not work.
Here is the script:
tickRate = 30
--the CAN baud rate
CAN_baud = 250000
--CAN channel to listen on. 0=first CAN channel, 1=second
CAN_chan = 0
ledState = 0
--change this to make a bigger averaging window
maxAvg = 300
--300 = 10 seconds averaging at 30Hz tick rate
--do not change
fuelAvg={}
fuel2Index = 1
--add your virtual channels here
rpmId = addChannel("RPM", 25, 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 Shift()
local r = RPM
if r > 6300 then setGpio(2,1) else setGpio(2,0) end
if r > 6800 then setGpio(1,1) else setGpio(1,0) end
if r > 7300 then setGpio(0,1) else setGpio(0,0) end
end
function updateFuelAvg(value)
local i
if #fuelAvg == 0 then
--initialize averaging table
for i = 1, maxAvg do fuelAvg=0 end
end
fuelAvg[fuel2Index] = value
fuel2Index = fuel2Index + 1
if fuel2Index > maxAvg then fuel2Index = 1 end
local sum = 0
for i = 1, #fuelAvg do
sum = sum + fuelAvg
end
setChannel(fuel2Id, sum / maxAvg)
end
function Logging()
if getGpsSpeed() > 10 then
startLogging()
else
stopLogging()
end
end
--customize here for CAN channel mapping
--offset/length in bytes?
--format is: [CAN Id] = function(data) map_chan(<channel id>, data, <CAN offset>, <CAN length>, <multiplier>, <adder>)
CAN_map = {
--did not bother logging gear speed and target cam angle
[1632] = function(data) map_chan(rpmId, data, 0, 2, 1, 0) 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
}
function onTick()
processCAN(CAN_chan)
Shift()
updateFuelAvg(getAnalog(0))
Logging()
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)
setTickRate(tickRate)