Last active
July 12, 2018 22:46
-
-
Save brentpicasso/ed76d612be30edfb3f9b4336a3588424 to your computer and use it in GitHub Desktop.
Manual PID query with delay between PIDs to deal with RX8 ecu that cannot process rapid OBDII queries.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--Implements a simple OBDII engine in lua scripting, with a configurable delay between PIDs. | |
-- IMPORTANT: Ensure built-in OBDII is turned off in Setup | |
-- | |
--500K baud. set your baud rate here. | |
initCAN(0, 500000) | |
--standard 11 bit PID request 0x7DF | |
--extended 29 bit PID request 0x18DB33F1 | |
--standard 11 bit PID response 0x7E8 | |
--extended 29 bit PID response 0x18DAF110 | |
standard = true | |
--delay between PID queries, in mS | |
delay = 1000 | |
--this function checks the PID response | |
function checkPidResponse() | |
local pid = nil | |
local id | |
local ext | |
local data | |
id, ext, data = rxCAN(0, 1000) | |
if ext == 1 and id == 0x18DAF110 then | |
pid = data[3] | |
end | |
if ext == 0 and id == 0x7E8 then | |
pid = data[3] | |
end | |
if pid ~= nil then | |
println('got response for PID ' ..pid) | |
local mapper = mappers[pid] | |
if mapper ~= nil then | |
local value = mapper(data) | |
local chId = pids[pid] | |
setChannel(chId, value) | |
return true | |
end | |
else | |
println('No PID response!') | |
end | |
end | |
function request_pid(pid) | |
local r | |
if standard then | |
r = txCAN(0, 0x7DF,0 , {2, 1, pid}) | |
else | |
r = txCAN(0, 0x18DB33F1,1 , {2, 1, pid}) | |
end | |
println('Request pid ' ..pid ..' transmission result ' ..r) | |
end | |
function onTick() | |
sleep(delay) | |
request_pid(query[queryIndex]) | |
checkPidResponse() | |
queryIndex = queryIndex + 1 | |
if queryIndex > queryLen then queryIndex = 1 end | |
end | |
function tempMapper(data) | |
local degC = data[4] - 40 | |
local degF = degC * 1.8 + 32 | |
return degF | |
--return degC | |
end | |
function fuelLevelMapper(data) | |
return data[4] / 2.55 | |
end | |
tempId = addChannel("EngineTemp", 1, 0, 0, 300) | |
fuelId = addChannel("FuelLevel", 1, 0, 0, 100) | |
setTickRate(1) | |
pids = {} | |
pids[5] = tempId | |
pids[47] = fuelId | |
query = {5, 47} | |
queryLen = #query | |
queryIndex = 1 | |
mappers = {} | |
mappers[5] = tempMapper | |
mappers[47] = fuelLevelMapper | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment