AEM infinity CAN BUS data
AEM infinity CAN BUS data
Hello all, please excuse my noobness but I am just not understanding how to translate the can bus data i am receiving and creating a virtual channel to display that information. I have converted the ID that AEM gives
Message ID: 0x01F0A000 Sources: AEM V2 & EMS‐4 (30‐6XXX) Infinity EMS (30‐71XX) 20ms continuous (50hz) Byte Label Data Type Scaling Offset Range Scaling Offset Range
6 Intake Air Temp 8 bit signed, 2's comp 1 Deg C/bit 0 ‐128 to 127 C 1.8 Deg F/bit 32 ‐198.4 to 260.6 F
7 Coolant Temp 8 bit signed, 2's comp 1 Deg C/bit 0 ‐128 to 127 C 1.8 Deg F/bit 32 ‐198.4 to 260.6 F
ID 1 2 3 4 5 6 7 8
32546816.0, 28.0, 189.0, 22.0, 43.0, 0.0, 240.0, 48.0, 80.0
So my translated ID is 32546816.0 byte 7 for coolant temp and based on scaling guage should read 86 degrees. My question is how the heck do i translate this into Lua script and put it into a virtual channel. Thanks for any help. Again sorry for the noobness. I tried researching as much as i could before asking.
Message ID: 0x01F0A000 Sources: AEM V2 & EMS‐4 (30‐6XXX) Infinity EMS (30‐71XX) 20ms continuous (50hz) Byte Label Data Type Scaling Offset Range Scaling Offset Range
6 Intake Air Temp 8 bit signed, 2's comp 1 Deg C/bit 0 ‐128 to 127 C 1.8 Deg F/bit 32 ‐198.4 to 260.6 F
7 Coolant Temp 8 bit signed, 2's comp 1 Deg C/bit 0 ‐128 to 127 C 1.8 Deg F/bit 32 ‐198.4 to 260.6 F
ID 1 2 3 4 5 6 7 8
32546816.0, 28.0, 189.0, 22.0, 43.0, 0.0, 240.0, 48.0, 80.0
So my translated ID is 32546816.0 byte 7 for coolant temp and based on scaling guage should read 86 degrees. My question is how the heck do i translate this into Lua script and put it into a virtual channel. Thanks for any help. Again sorry for the noobness. I tried researching as much as i could before asking.
It says byte 7 but thats actually the 8th bit as it starts from 0.
here is the doc you need for the AEM CAN protocol:
http://aemelectronics.com/sites/default ... Public.pdf
So for ID 32546816 (which is the Hex -> Dec of the 0x01F0A000 data)
RPM Bits 0 , 1 [28 , 189] that would be 28*256 + 189 = 7357 * .39063 = 2874rpm
TPS Bits 4, 5 [0, 240] that would be = 240 * 0.0015259 = 0.366%
ATS Bit 6 [48] that would be 48 Deg C
CTS Bit 7 [80] that would be 80 Deg C
The map part of the example scripts would be written as:
If you still have issues shout out and I can set the base script up for you.
here is the doc you need for the AEM CAN protocol:
http://aemelectronics.com/sites/default ... Public.pdf
So for ID 32546816 (which is the Hex -> Dec of the 0x01F0A000 data)
RPM Bits 0 , 1 [28 , 189] that would be 28*256 + 189 = 7357 * .39063 = 2874rpm
TPS Bits 4, 5 [0, 240] that would be = 240 * 0.0015259 = 0.366%
ATS Bit 6 [48] that would be 48 Deg C
CTS Bit 7 [80] that would be 80 Deg C
The map part of the example scripts would be written as:
Code: Select all
CAN_map = {
[32546816] = function(data)
map_chan(rpmId, data, 0, 2, 0.39063, 0)
map_chan(tpsId, data, 4, 2, 0.0015259, 0)
map_chan(atsId, data, 6, 1, 1, 0)
map_chan(ctsId, data, 7, 1, 1, 0)
end
}
it'll only take me a sec... here you go:
Code: Select all
--Designed for AEM CAN Logging
--AEM CAN V2 Broadcast Protocol
--============================== Set Variables =================================
setTickRate(25)
initCAN(0, 500000)
CAN_chan = 0 -- 0=first CAN channel, 1=second
--comment out channels if reqd
--note IDs in HEX and must be converted to Decimal
--usage: map_chan(var, data, starting byte, byte length, Unit, Offset)
CAN_map = {
[32546816] = function(data)
map_chan(rpmId, data, 0, 2, 0.39063, 0)
map_chan(tpsId, data, 4, 2, 0.0015259, 0)
map_chan(atsId, data, 6, 1, 1, 0)
map_chan(ctsId, data, 7, 1, 1, 0)
end
}
--=============================== Add Virtual Channels ==========================
--usage: addChannel("Name",samplerate{1,10,25,50,100,200},precision(2),min(0),max(1000),units)
rpmId = addChannel("RPM", 25, 0, 0, 10000, 'RPM')
tpsId = addChannel("TPS", 10, 1, 0, 100, '%')
atsId = addChannel("ATS", 10, 1, 0, 200, 'C')
ctsId = addChannel("CTS", 10, 1, 0, 200, 'C')
--========================== Init ===================================
function onTick()
processCAN()
end
--========================== CAN =============================
function processCAN() --finds an id that exists then passes it to
repeat
local id, e, data = rxCAN(0)
if id ~= nil then -- step through CAN data until you get a value
local map = CAN_map[id]
if map ~= nil then
map(data) --this runs map_chan function with the CAN 'data'
end
end
until id == nil
end
function map_chan(cid, data, offset, len, mult, add) --eg (rpmId, data, 0, 2, 1, 0)
offset = offset + 1
local value = 0
while len > 0 do
value = (value * 256) + data[offset]
offset = offset + 1
len = len - 1
end
setChannel(cid, (value * mult) + add)
end
Just noticed you're in the US... so you need to change the lines:
map_chan(atsId, data, 6, 1, 1, 0)
map_chan(ctsId, data, 7, 1, 1, 0)
to
map_chan(atsId, data, 6, 1, 1.8, 32)
map_chan(ctsId, data, 7, 1, 1.8, 32)
so that you get it in Fahrenheit ... you'll also need to change the units in your virtual channel map:
atsId = addChannel("ATS", 10, 1, 0, 200, 'C')
ctsId = addChannel("CTS", 10, 1, 0, 200, 'C')
to
atsId = addChannel("ATS", 10, 1, 0, 300, 'F')
ctsId = addChannel("CTS", 10, 1, 0, 300, 'F')
map_chan(atsId, data, 6, 1, 1, 0)
map_chan(ctsId, data, 7, 1, 1, 0)
to
map_chan(atsId, data, 6, 1, 1.8, 32)
map_chan(ctsId, data, 7, 1, 1.8, 32)
so that you get it in Fahrenheit ... you'll also need to change the units in your virtual channel map:
atsId = addChannel("ATS", 10, 1, 0, 200, 'C')
ctsId = addChannel("CTS", 10, 1, 0, 200, 'C')
to
atsId = addChannel("ATS", 10, 1, 0, 300, 'F')
ctsId = addChannel("CTS", 10, 1, 0, 300, 'F')
So if there data on other ID's not associated with this first one, can i add it to the end of this script
CAN_map = {
[32546816] = function(data)
map_chan(rpmId, data, 0, 2, 0.39063, 0)
map_chan(tpsId, data, 4, 2, 0.0015259, 0)
map_chan(atsId, data, 6, 1, 1, 0)
map_chan(ctsId, data, 7, 1, 1, 0)
end
CAN_map = {
[32546820] = function(data)
map_chan(FuelPressId, data, 3, 1, 0.580151, 0)
map_chan(OilPressId, data, 4, 1, 0.580151, 0)
end
CAN_map = {
[32546816] = function(data)
map_chan(rpmId, data, 0, 2, 0.39063, 0)
map_chan(tpsId, data, 4, 2, 0.0015259, 0)
map_chan(atsId, data, 6, 1, 1, 0)
map_chan(ctsId, data, 7, 1, 1, 0)
end
CAN_map = {
[32546820] = function(data)
map_chan(FuelPressId, data, 3, 1, 0.580151, 0)
map_chan(OilPressId, data, 4, 1, 0.580151, 0)
end
close... but should be:
also, keep your virtual channel names to be shorter than 8 characters. I changed the above to fpsId and opsId
note: there is a comma after the first array entry, but not the last.
Array = {[1] = Hi, [2] = This, [3] = is, [4] = an, [5] = array}
print Array[2] will return "This"
In this case CAN_map[32546820] will return:
What you are doing here is making an array called CAN_map that is mapping a function to a location in the array [32546816]... The bottom part of the script is finding an ID in the CAN stream, checking if there is data for that ID in the CAN_Map and then grabbing the data (ie the map_chan stuff) and then running it as a function. It's all a bit tricky to get your head around.
so this function only does something if it finds an ID that matches your ID in the CAN_map ie 32546816 or 32546820. If it finds any other ID it will discard it.
If the ID matches then makes a function called map from the data in the CAN_map array and runs it with the data that follows the ID which is the 8 bytes of data that contains the sensor values.
which makes:
This function calls the map_chan function that does some simple math to convert the required byte data to a value and maps it to a virtual channel using setChannel.
Code: Select all
CAN_map = {
[32546816] = function(data)
map_chan(rpmId, data, 0, 2, 0.39063, 0)
map_chan(tpsId, data, 4, 2, 0.0015259, 0)
map_chan(atsId, data, 6, 1, 1, 0)
map_chan(ctsId, data, 7, 1, 1, 0)
end,
[32546820] = function(data)
map_chan(fpsId, data, 3, 1, 0.580151, 0)
map_chan(opsId, data, 4, 1, 0.580151, 0)
end
}
note: there is a comma after the first array entry, but not the last.
Array = {[1] = Hi, [2] = This, [3] = is, [4] = an, [5] = array}
print Array[2] will return "This"
In this case CAN_map[32546820] will return:
Code: Select all
function(data)
map_chan(FuelPressId, data, 3, 1, 0.580151, 0)
map_chan(OilPressId, data, 4, 1, 0.580151, 0)
end
Code: Select all
function processCAN()
repeat
local id, e, data = rxCAN(0)
if id ~= nil then
local map = CAN_map[id]
if map ~= nil then
map(data)
end
end
until id == nil
end
If the ID matches then makes a function called map from the data in the CAN_map array and runs it with the data that follows the ID which is the 8 bytes of data that contains the sensor values.
Code: Select all
local map = CAN_map[id]
Code: Select all
map(data)
map_chan(FuelPressId, data, 3, 1, 0.580151, 0)
map_chan(OilPressId, data, 4, 1, 0.580151, 0)
end
here try this:
You should see channels called "RPM, TPS, ATS, CTS, FPS and OPS" pop up in the channels list on the RCP dashboard.
Code: Select all
setTickRate(25)
initCAN(0, 500000)
CAN_chan = 0
CAN_map = {
[32546816] = function(data)
map_chan(rpmId, data, 0, 2, 0.39063, 0)
map_chan(tpsId, data, 4, 2, 0.0015259, 0)
map_chan(atsId, data, 6, 1, 1, 0)
map_chan(ctsId, data, 7, 1, 1, 0)
end,
[32546820] = function(data)
map_chan(fpsId, data, 3, 1, 0.580151, 0)
map_chan(opsId, data, 4, 1, 0.580151, 0)
end
}
rpmId = addChannel("RPM", 25, 0, 0, 10000, 'RPM')
tpsId = addChannel("TPS", 10, 1, 0, 100, '%')
atsId = addChannel("ATS", 10, 1, 0, 200, 'C')
ctsId = addChannel("CTS", 10, 1, 0, 200, 'C')
fpsId = addChannel("FPS", 10, 1, 0, 200, 'PSI')
opsId = addChannel("OPS", 10, 1, 0, 200, 'PSI')
function onTick()
processCAN()
end
function processCAN()
repeat
local id, e, data = rxCAN(0)
if id ~= nil then
local map = CAN_map[id]
if map ~= nil then
map(data)
end
end
until id == nil
end
function map_chan(cid, data, offset, len, mult, add)
offset = offset + 1
local value = 0
while len > 0 do
value = (value * 256) + data[offset]
offset = offset + 1
len = len - 1
end
setChannel(cid, (value * mult) + add)
end
So I tried the script and I am getting this error message:
running lua script len(7161)...lua: startup script error: ([string "startup"]:196.0: unexpected symbol near '+')
done
Any idea what this means? Current script I am using:
setTickRate(25)
initCAN(0, 500000)
CAN_chan = 0
CAN_map = {
[32546816] = function(data)
map_chan(rpmId, data, 0, 2, 0.39063, 0)
map_chan(tpsId, data, 4, 2, 0.0015259, 0)
map_chan(atsId, data, 6, 1, 1, 0)
map_chan(ctsId, data, 7, 1, 1, 0)
end,
[32546820] = function(data)
map_chan(mapId, data, 0, 2, 0.014504 , ‐14.6960)
map_chan(fpsId, data, 3, 1, 0.580151, 0)
map_chan(opsId, data, 4, 1, 0.580151, 0)
map_chan(afrId, data, 5, 1, 0.057227, 7.325)
end
}
rpmId = addChannel("RPM", 25, 0, 0, 10000, 'RPM')
tpsId = addChannel("TPS", 10, 1, 0, 100, '%')
atsId = addChannel("IAT", 10, 1, 0, 200, 'C')
ctsId = addChannel("EngineTemp", 10, 1, 0, 200, 'C')
mapId = addChannel("Boost", 10, 1, 0, 935, 'PSI')
fpsId = addChannel("FuelPress", 10, 1, 0, 200, 'PSI')
opsId = addChannel("OilPress", 10, 1, 0, 200, 'PSI')
afrId = addChannel("AFR", 10, 1, 0, 21.9, 'PSI')
function onTick()
processCAN()
end
function processCAN()
repeat
local id, e, data = rxCAN(0)
if id ~= nil then
local map = CAN_map[id]
if map ~= nil then
map(data)
end
end
until id == nil
end
function map_chan(cid, data, offset, len, mult, add)
offset = offset + 1
local value = 0
while len > 0 do
value = (value * 256) + data[offset]
offset = offset + 1
len = len - 1
end
setChannel(cid, (value * mult) + add)
end
running lua script len(7161)...lua: startup script error: ([string "startup"]:196.0: unexpected symbol near '+')
done
Any idea what this means? Current script I am using:
setTickRate(25)
initCAN(0, 500000)
CAN_chan = 0
CAN_map = {
[32546816] = function(data)
map_chan(rpmId, data, 0, 2, 0.39063, 0)
map_chan(tpsId, data, 4, 2, 0.0015259, 0)
map_chan(atsId, data, 6, 1, 1, 0)
map_chan(ctsId, data, 7, 1, 1, 0)
end,
[32546820] = function(data)
map_chan(mapId, data, 0, 2, 0.014504 , ‐14.6960)
map_chan(fpsId, data, 3, 1, 0.580151, 0)
map_chan(opsId, data, 4, 1, 0.580151, 0)
map_chan(afrId, data, 5, 1, 0.057227, 7.325)
end
}
rpmId = addChannel("RPM", 25, 0, 0, 10000, 'RPM')
tpsId = addChannel("TPS", 10, 1, 0, 100, '%')
atsId = addChannel("IAT", 10, 1, 0, 200, 'C')
ctsId = addChannel("EngineTemp", 10, 1, 0, 200, 'C')
mapId = addChannel("Boost", 10, 1, 0, 935, 'PSI')
fpsId = addChannel("FuelPress", 10, 1, 0, 200, 'PSI')
opsId = addChannel("OilPress", 10, 1, 0, 200, 'PSI')
afrId = addChannel("AFR", 10, 1, 0, 21.9, 'PSI')
function onTick()
processCAN()
end
function processCAN()
repeat
local id, e, data = rxCAN(0)
if id ~= nil then
local map = CAN_map[id]
if map ~= nil then
map(data)
end
end
until id == nil
end
function map_chan(cid, data, offset, len, mult, add)
offset = offset + 1
local value = 0
while len > 0 do
value = (value * 256) + data[offset]
offset = offset + 1
len = len - 1
end
setChannel(cid, (value * mult) + add)
end