fome-fw/firmware/controllers/lua/examples/vw-tp.txt

207 lines
4.2 KiB
Plaintext
Raw Normal View History

2022-12-01 15:47:08 -08:00
-- this controls onCanRx rate as well!
2022-12-23 16:01:08 -08:00
setTickRate(300)
timeout = 3000
cltSensor = Sensor.new("clt")
cltSensor : setTimeout(timeout)
iatSensor = Sensor.new("iat")
iatSensor : setTimeout(timeout)
rpmSensor = Sensor.new("rpm")
rpmSensor : setTimeout(timeout)
2022-12-01 15:47:08 -08:00
2022-12-23 15:20:17 -08:00
mapSensor = Sensor.new("map")
2022-12-23 16:01:08 -08:00
mapSensor : setTimeout(timeout)
2022-12-23 15:20:17 -08:00
2022-12-01 15:47:08 -08:00
hexstr = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "A", "B", "C", "D", "E", "F" }
function toHexString(num)
if num == 0 then
return '0'
end
local result = ""
while num > 0 do
local n = num % 16
result = hexstr[n + 1] ..result
num = math.floor(num / 16)
end
return result
end
function arrayToString(arr)
local str = ""
local index = 1
while arr[index] ~= nil do
str = str.." "..toHexString(arr[index])
index = index + 1
end
return str
end
VWTP_OUT = 0x200
2023-01-10 22:47:33 -08:00
cuType = 0x01
-- todo: VWTP_IN should be 0x200 + cuType maybe?
2022-12-01 15:47:08 -08:00
VWTP_IN = 0x201
VWTP_TESTER = 0x300
2023-01-10 22:47:33 -08:00
local cuId = 0
2022-12-01 15:47:08 -08:00
function onCanHello(bus, id, dlc, data)
-- here we handle 201 packets
print('Got Hello Response ' ..arrayToString(data))
2023-01-10 22:47:33 -08:00
cuId = data[6] * 256 + data[5]
print('From ECU ' ..cuId)
2023-01-13 12:33:43 -08:00
out = { 0xA0, 0x0F, 0x8A, 0xFF, 0x32, 0xFF }
txCan(1, cuId, 0, out)
2022-12-01 15:47:08 -08:00
end
2023-01-13 12:27:30 -08:00
local sendCounter = 0
2022-12-01 17:23:31 -08:00
local packetCounter = 1
2022-12-01 17:40:50 -08:00
local payLoadIndex = 0
2022-12-23 16:08:09 -08:00
local groups = { 2, 4, 31 }
2022-12-23 16:01:08 -08:00
-- todo: smarter array size calculation?
2022-12-23 16:08:09 -08:00
local groupsSize = 3
2022-12-23 16:01:08 -08:00
local groupIndex = 1
2022-12-01 15:47:08 -08:00
2023-01-13 12:27:30 -08:00
function nextReq()
local result = 0x10 + sendCounter
sendCounter = sendCounter + 1
if sendCounter == 16 then
sendCounter = 0
end
return result
end
2022-12-01 15:47:08 -08:00
function onCanTester(bus, id, dlc, data)
-- here we handle 300 packets
2022-12-01 17:40:50 -08:00
-- print('Got from tester ' ..arrayToString(data))
2022-12-01 15:47:08 -08:00
2022-12-01 17:23:31 -08:00
if data[1] == 0xA3 then
2022-12-01 17:40:50 -08:00
-- print ("Keep-alive")
2023-01-10 22:47:33 -08:00
txCan(1, cuId, 0, { 0xA1, 0x0F, 0x8A, 0xFF, 0x4A, 0xFF })
2022-12-01 17:23:31 -08:00
2022-12-23 16:01:08 -08:00
groupIndex = groupIndex + 1
if groupIndex > groupsSize then
groupIndex = 1
end
groupId = groups[groupIndex]
2023-01-13 12:27:30 -08:00
print ("KA " .. groupIndex .." " ..groupId)
2022-12-23 16:01:08 -08:00
2023-01-13 12:27:30 -08:00
reqFirst = nextReq()
print("Requesting next group " ..groupId .." with counter " ..reqFirst)
2023-01-10 22:47:33 -08:00
txCan(1, cuId, 0, { reqFirst, 0x00, 0x02, 0x21, groupId })
2022-12-01 15:47:08 -08:00
2022-12-01 17:23:31 -08:00
return
end
if data[1] == 0xA1 then
2023-01-13 12:27:30 -08:00
print ("Happy 300 packet, follow-up...")
txCan(1, cuId, 0, { nextReq(), 0x00, 0x02, 0x10, 0x89 })
2022-12-01 15:47:08 -08:00
return
end
if data[1] == 0xA8 then
print ("They said Bye-Bye")
return
end
2022-12-01 17:23:31 -08:00
if data[1] == 0x10 and dlc == 5 then
ackPacket = 0xB0 + packetCounter
print ("Sending ACK B1 " ..ackPacket)
2023-01-10 22:47:33 -08:00
txCan(1, cuId, 0, { ackPacket })
2022-12-23 16:01:08 -08:00
-- request first group from array
2023-01-13 12:27:30 -08:00
txCan(1, cuId, 0, { nextReq(), 0x00, 0x02, 0x21, groups[1] })
2022-12-01 15:47:08 -08:00
return
end
2022-12-01 17:23:31 -08:00
top4 = math.floor(data[1] / 16)
2022-12-01 15:47:08 -08:00
2022-12-01 17:23:31 -08:00
if top4 == 0xB then
2022-12-01 17:40:50 -08:00
-- print("Got ACK")
2022-12-01 17:23:31 -08:00
return
end
if top4 == 2 or top4 == 1 then
2022-12-01 17:40:50 -08:00
print ("Looks like payload index " ..payLoadIndex ..": " ..arrayToString(data))
2022-12-23 15:20:17 -08:00
if groupId == 2 and payLoadIndex == 0 then
2022-12-23 16:01:08 -08:00
rpmL = data[7]
rpmH = data[8]
rpm = rpmH * rpmL / 5
2022-12-23 16:08:09 -08:00
print(rpmL .." " ..rpmH .." rpm " ..rpm)
2022-12-23 16:01:08 -08:00
rpmSensor : set(rpm)
2022-12-23 15:20:17 -08:00
end
if groupId == 2 and payLoadIndex == 1 then
2022-12-23 16:01:08 -08:00
elL = data[3]
elH = data[4]
el = 100 * elH / elL
2022-12-23 16:08:09 -08:00
print(elL .." " ..elH .." EL " ..el)
2022-12-23 16:01:08 -08:00
end
2022-12-23 15:20:17 -08:00
if groupId == 2 and payLoadIndex == 2 then
2022-12-23 16:01:08 -08:00
mapL = data[2]
mapH = data[3]
map = 1000.0 * mapH / mapL
2022-12-23 16:08:09 -08:00
print(mapL .." " ..mapH .." MAP " ..map)
2022-12-23 16:01:08 -08:00
mapSensor : set(map / 10)
end
if groupId == 4 and payLoadIndex == 1 then
cltL = data[6]
cltH = data[7]
clt = cltL * (cltH - 100) / 10
2022-12-23 16:08:09 -08:00
print(cltL .." " ..cltH .." CLT " ..clt)
2022-12-23 16:01:08 -08:00
cltSensor : set(clt)
end
if groupId == 4 and payLoadIndex == 2 then
iatL = data[2]
iatH = data[3]
iat = iatL * (iatH - 100) / 10
2022-12-23 16:08:09 -08:00
print(iatL .." " ..iatH .." IAT " ..iat)
2022-12-23 16:01:08 -08:00
iatSensor : set(iat)
end
2022-12-23 15:20:17 -08:00
2022-12-01 17:40:50 -08:00
payLoadIndex = payLoadIndex + 1
2022-12-01 17:23:31 -08:00
packetCounter = packetCounter + 1
if packetCounter > 15 then
packetCounter = 0
end
if top4 == 1 then
2023-01-13 12:50:03 -08:00
-- todo: should we ACK top4 == 2?
2022-12-01 17:23:31 -08:00
ackPacket = 0xB0 + packetCounter
2022-12-01 17:40:50 -08:00
print ("Sending payload ACK " ..ackPacket)
2023-01-10 22:47:33 -08:00
txCan(1, cuId, 0, { ackPacket })
2022-12-01 17:40:50 -08:00
payLoadIndex = 0
2022-12-01 17:23:31 -08:00
end
return
end
2022-12-01 15:47:08 -08:00
print('Got unexpected ' ..arrayToString(data))
end
canRxAdd(VWTP_IN, onCanHello)
canRxAdd(VWTP_TESTER, onCanTester)
2023-01-10 22:47:33 -08:00
txCan(1, VWTP_OUT, 0, { cuType, 0xC0, 0x00, 0x10, 0x00, 0x03, 0x01 })
2022-12-01 15:47:08 -08:00
function onTick()
end