It looks like I'm a couple of years too late to be helpful in getting ready for Lemons.
The aux serial port for my rcpMk3 is 6.
I expect this code should work at much slower tickRates than 60, but I haven't done much testing yet. It is designed to run with minimal delays at high tickRates, so never waits, nor needs to wait for serial input. While serial data is ready, it reads it all, so even at low tickRates it should keep up fine. I think 10 will be enough to keep up. It will depend on the size of the internal serial buffer. But it looks the Innovate AFM only generates about 6 bytes per update and looks like its going at about 10hz, so 60 bytes per second.
I'm tossing out all the data except the air fuel ratio, so if your Innovate is logging more data, you'll have to add code to read that.
I'm pretty new to lua, so feel free to offer suggestions for improvements.
Code: Select all
-- documentation for RCP serial interface here: https://wiki.autosportlabs.com/RaceCapturePro_Lua_Scripting
-- documentation for ISP2 here: http://www.innovatemotorsports.com/support/downloads/Seriallog-2.pdf
afSerNum=6
afInitd=false
afChan=-1
function afInit()
afInitd = initSer(afSerNum, 19200, 8, 0, 1)
if afInitd then
println('serial initialized')
ispInit()
else
println('failed to initialize serial')
end
end
-- identify header with bits 15,13,9,7
hMaskH=0xA2
hMaskL=0x80
ispAFR = nil
-- this is a non-blocking implementation of the isp2 reader.
-- the following functions, when triggered will put the subsequent function
-- into the ispNextByteHandler variable, so when everything goes smoothly they will each find the
-- byte they are expecting.
-- the ispHandleHh function is responsible for consuming any unused packets (there is packet count
-- detected, but I am ignoring it for now. If you expect or need more packets, you'll need to add
-- functions to this sequence to process them.
function ispInit()
ispHl = nil
ispHh = nil
ispNextByteHandler = ispHandleHh
end
function ispHandleHh( h )
if bit.band(h,hMaskH) == hMaskH then
ispHh = h
ispNextByteHandler = ispHandleHl
end
end
function ispHandleHl( l )
if bit.band(l,hMaskL) == hMaskL then
ispHl = l
ispNextByteHandler = ispHandleAFh
ispPkt = bit.band(l,0x7F) + 0x80*bit.band(ispHh,1)
else
ispInit()
end
end
function ispHandleAFh( h )
ispAFh = h
ispNextByteHandler = ispHandleAFl
end
function ispHandleAFl( l )
ispAFl = l
ispNextByteHandler = ispHandleLh
end
function ispHandleLh( h )
ispLh = h
ispNextByteHandler = ispHandleLl
end
function ispHandleLl( l )
ispLl = l
local func = bit.band(bit.rshift(ispAFh,2),7)
-- this is the afr multiplier (not the afr)
local af = bit.band( ispAFh, 1) * 0x80 +
bit.band( ispAFl, 0x7F)
local lambda = bit.band(ispLh,0x7F)*0x80 + bit.band(ispLl,0x7F)
if func == 0 then
ispAFR = (lambda+500)*af/10000
else
println( "afr: func"..func.." mult:"..af.." lambda:"..lambda )
end
ispInit()
end
function afReadISP2nb(serPort)
c = readCSer( serPort,0 )
while c ~= nil do
ispNextByteHandler( c )
c = readCSer( serPort,0 )
end
if ispAFR ~= nil then
setChannel( afChan, ispAFR )
ispAFR = nil
end
end
function onTick()
if afInitd then
afReadISP2nb( afSerNum )
end
end
afInit()
afChan=addChannel("AFR",10,1,8,23,"%")
setTickRate(60)