rusefi_documentation/B6-temp-TCU-main-in-the-mid...

489 lines
12 KiB
Plaintext
Raw Permalink Normal View History

2023-03-05 11:52:41 -08:00
AIRBAG = 0x050
2023-10-07 21:36:11 -07:00
2023-10-08 20:19:34 -07:00
onEcuTimer = Timer.new()
onEcuTimer : reset ()
2023-10-07 21:36:11 -07:00
TCU_1088_440 = 0x440
TCU_1344_540 = 0x540
2023-10-08 20:51:55 -07:00
BRAKE_1_416 = 0x1A0
BRAKE_8_428 = 0x1AC
2023-10-08 21:09:00 -07:00
Komf_1_912 = 0x390
2023-10-08 20:51:55 -07:00
BRAKE_3_1184 = 0x4a0
BRAKE_5_1192 = 0x4a8
BRAKE_2_1440 = 0x5A0
2023-03-05 11:52:41 -08:00
-- 640
MOTOR_1 = 0x280
-- 644
MOTOR_BRE = 0x284
-- 648
MOTOR_2 = 0x288
-- 896
MOTOR_3 = 0x380
-- 1152
MOTOR_5 = 0x480
-- 1160
MOTOR_6 = 0x488
-- 1386
ACC_GRA = 0x56A
-- 1408 the one with variable payload
MOTOR_INFO = 0x580
-- 1416
MOTOR_7 = 0x588
motor1Data = { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
2023-03-05 14:29:05 -08:00
motorBreData = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
2023-03-05 11:52:41 -08:00
motor2Data = { 0x8A, 0x8D, 0x10, 0x04, 0x00, 0x4C, 0xDC, 0x87 }
2023-03-05 14:29:05 -08:00
motor2mux = { 0x8A, 0xE8, 0x2C, 0x64 }
2023-03-05 11:52:41 -08:00
canMotor3 = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
motor5Data = { 0x1C, 0x08, 0xF3, 0x55, 0x19, 0x00, 0x00, 0xAD }
motor6Data = { 0x00, 0x00, 0x00, 0x7E, 0xFE, 0xFF, 0xFF, 0x00 }
accGraData = { 0x00, 0x00, 0x08, 0x00, 0x1A, 0x00, 0x02, 0x01 }
2024-02-10 19:07:44 -08:00
canMotorInfo02 = { 0x00, 0x00, 0x00, 0x14, 0x1C, 0x93, 0x48, 0x14 }
2023-03-05 14:29:05 -08:00
canMotorInfo1 = { 0x99, 0x14, 0x00, 0x7F, 0x00, 0xF0, 0x47, 0x01 }
canMotorInfo3 = { 0x9B, 0x14, 0x00, 0x11, 0x1F, 0xE0, 0x0C, 0x46 }
2023-03-05 11:52:41 -08:00
motor7Data = { 0x1A, 0x66, 0x7E, 0x00, 0x00, 0x00, 0x00, 0x00 }
2023-10-08 20:51:55 -07:00
payload416 = {0x00, 0x43, 0x00, 0x00, 0x00, 0xfe, 0x00, 0x5d}
payload428 = {0xff, 0x0e, 0x00, 0x00, 0x69, 0x01, 0x0b, 0x92}
2023-10-08 21:09:00 -07:00
payload912 = {0x6a, 0x40, 0x50, 0x00, 0x00, 0x00, 0x20, 0x00}
2023-10-08 20:51:55 -07:00
payload1440 = {0x7e, 0x00, 0x00, 0x83, 0x33, 0x00, 0x10, 0xab}
2023-03-05 11:52:41 -08:00
canMotorInfoTotalCounter = 0
VWTP_OUT = 0x200
cuType = 0x02 -- TCU
VWTP_IN = 0x200 + cuType
VWTP_TESTER = 0x300
-- sometimes we want to cut a CAN bus and install rusEFI into that cut
-- https://en.wikipedia.org/wiki/Man-in-the-middle_attack
-- this controls onCanRx rate as well!
setTickRate(100)
ECU_BUS = 1
TCU_BUS = 2
fakeTorque = 0
rpm = 0
tps = 0
function xorChecksum(data, targetIndex)
2023-03-05 14:29:05 -08:00
local index = 1
local result = 0
while data[index] ~= nil do
if index ~= targetIndex then
result = result ~ data[index]
end
index = index + 1
end
data[targetIndex] = result
return result
2023-03-05 11:52:41 -08:00
end
function setBitRange(data, totalBitIndex, bitWidth, value)
2023-03-05 14:29:05 -08:00
local byteIndex = totalBitIndex >> 3
local bitInByteIndex = totalBitIndex - byteIndex * 8
if (bitInByteIndex + bitWidth > 8) then
bitsToHandleNow = 8 - bitInByteIndex
setBitRange(data, totalBitIndex + bitsToHandleNow, bitWidth - bitsToHandleNow, value >> bitsToHandleNow)
bitWidth = bitsToHandleNow
end
mask = (1 << bitWidth) - 1
data[1 + byteIndex] = data[1 + byteIndex] & (~(mask << bitInByteIndex))
maskedValue = value & mask
shiftedValue = maskedValue << bitInByteIndex
data[1 + byteIndex] = data[1 + byteIndex] | shiftedValue
2023-03-05 11:52:41 -08:00
end
function relayFromECU(bus, id, dlc, data)
2023-10-08 20:19:34 -07:00
onEcuTimer : reset ()
2023-03-05 14:29:05 -08:00
totalEcuMessages = totalEcuMessages + 1
-- print("Relaying to TCU " .. id)
2023-10-08 21:09:00 -07:00
txCan(TCU_BUS, id, 0, data)
2023-03-05 11:52:41 -08:00
end
2023-03-05 14:59:41 -08:00
function setTwoBytes(data, offset, value)
data[offset + 1] = value % 255
data[offset + 2] = (value >> 8) % 255
end
2023-03-05 11:52:41 -08:00
function sendMotor1()
2023-03-05 14:29:05 -08:00
engineTorque = fakeTorque * 0.9
innerTorqWithoutExt = fakeTorque
torqueLoss = 20
requestedTorque = fakeTorque
motor1Data[2] = engineTorque / 0.39
setTwoBytes(motor1Data, 2, rpm / 0.25)
motor1Data[5] = innerTorqWithoutExt / 0.4
motor1Data[6] = tps / 0.4
motor1Data[7] = torqueLoss / 0.39
motor1Data[8] = requestedTorque / 0.39
txCan(TCU_BUS, MOTOR_1, 0, motor1Data)
2023-03-05 11:52:41 -08:00
end
function getBitRange(data, bitIndex, bitWidth)
2023-03-05 14:29:05 -08:00
byteIndex = bitIndex >> 3
shift = bitIndex - byteIndex * 8
value = data[1 + byteIndex]
if (shift + bitWidth > 8) then
value = value + data[2 + byteIndex] * 256
end
mask = (1 << bitWidth) - 1
return (value >> shift) & mask
2023-03-05 11:52:41 -08:00
end
2023-03-05 14:37:34 -08:00
motor2counter = 0
function sendMotor2()
motor2counter = (motor2counter + 1) % 16
minTorque = fakeTorque / 2
-- todo: add CLT
motor2Data[7] = math.floor(minTorque / 0.39)
--print ( "brake " .. getBitRange(data, 16, 2) .. " " .. rpm)
brakeBit = rpm < 2000 and 1 or 0
setBitRange(motor2Data, 16, 1, brakeBit)
index = math.floor(motor2counter / 4)
motor2Data[1] = motor2mux[1 + index]
txCan(TCU_BUS, MOTOR_2, 0, motor2Data)
end
2023-03-05 11:52:41 -08:00
function sendMotor3()
2023-03-05 14:29:05 -08:00
desired_wheel_torque = fakeTorque
2023-10-08 20:19:34 -07:00
iat = iat or 30
2023-03-05 14:29:05 -08:00
canMotor3[2] = (iat + 48) / 0.75
canMotor3[3] = tps / 0.4
canMotor3[5] = 0x20
setBitRange(canMotor3, 24, 12, math.floor(desired_wheel_torque / 0.39))
canMotor3[8] = tps / 0.4
txCan(TCU_BUS, MOTOR_3, 0, canMotor3)
2023-03-05 11:52:41 -08:00
end
2024-02-10 19:24:18 -08:00
motor3counter = 0
2023-03-05 11:52:41 -08:00
function onMotor3(bus, id, dlc, data)
2024-02-10 19:24:18 -08:00
motor3counter = (motor3counter + 1) % 16
2023-03-05 14:29:05 -08:00
totalEcuMessages = totalEcuMessages + 1
2023-10-08 20:51:55 -07:00
iat = 30 -- getBitRange(data, 8, 8) * 0.75 - 48
pps = 7 -- getBitRange(data, 16, 8) * 0.40
tps = 7 -- getBitRange(data, 56, 8) * 0.40
2024-02-10 19:24:18 -08:00
if motor3counter % 70 == 0 then
print ('MOTOR_3 pps ' ..pps ..' tps ' ..tps ..' iat ' ..iat)
end
2023-03-05 11:52:41 -08:00
end
function onMotor1(bus, id, dlc, data)
2023-03-05 14:29:05 -08:00
totalEcuMessages = totalEcuMessages + 1
2023-10-08 20:51:55 -07:00
rpm = 0 -- getBitRange(data, 16, 16) * 0.25
2023-03-05 14:29:05 -08:00
if rpm == 0 then
canMotorInfoTotalCounter = 0
end
2023-10-08 20:51:55 -07:00
tps = 0 -- getBitRange(data, 40, 8) * 0.4
2023-03-05 14:29:05 -08:00
fakeTorque = interpolate(0, 6, 100, 60, tps)
2023-03-05 14:59:41 -08:00
sendMotor1()
2023-03-05 11:52:41 -08:00
end
function relayFromECUAndEcho(bus, id, dlc, data)
totalEcuMessages = totalEcuMessages + 1
2023-03-05 14:29:05 -08:00
print("Relaying to TCU " ..id)
2023-03-05 11:52:41 -08:00
txCan(TCU_BUS, id, 0, data) -- relay non-TCU message to TCU
end
totalTcuMessages = 0
2023-10-07 21:36:11 -07:00
counter440 = 0
function onTcu440(bus, id, dlc, data)
totalTcuMessages = totalTcuMessages + 1
-- print("Relaying onTcu440 to ECU " .. id)
isShiftActive = getBitRange(data, 0, 1)
tcuStatus = getBitRange(data, 1, 1)
EGSRequirement = getBitRange(data, 7, 1)
counter440 = counter440 + 1
if counter440 % 70 == 0 then
print("TCU " .. isShiftActive .. " " .. tcuStatus .. " " .. EGSRequirement)
end
2023-10-08 21:09:00 -07:00
txCan(ECU_BUS, id, 0, data)
2023-10-07 21:36:11 -07:00
end
2023-03-05 11:52:41 -08:00
function relayFromTCU(bus, id, dlc, data)
totalTcuMessages = totalTcuMessages + 1
2023-03-05 14:29:05 -08:00
-- print("Relaying to ECU " .. id)
2023-10-08 21:09:00 -07:00
txCan(ECU_BUS, id, 0, data)
2023-03-05 11:52:41 -08:00
end
function relayTcuDiagHelloFromTCU(bus, id, dlc, data)
2023-03-05 14:29:05 -08:00
totalTcuMessages = totalTcuMessages + 1
print("Relaying TcuDiagHello to ECU " ..id ..arrayToString(data))
2023-10-08 21:09:00 -07:00
txCan(ECU_BUS, id, 0, data)
2023-03-05 11:52:41 -08:00
end
local payLoadIndex = 0
function relayTpPayloadFromTCU(bus, id, dlc, data)
2023-03-05 14:29:05 -08:00
totalTcuMessages = totalTcuMessages + 1
-- print("Relaying TP ECU " ..id ..arrayToString(data))
txCan(ECU_BUS, id, 0, data) -- relay non-ECU message to ECU
2023-03-05 11:52:41 -08:00
2023-03-05 14:29:05 -08:00
if data[1] == 0xA3 then
-- print ("Keep-alive")
return
end
2023-03-05 11:52:41 -08:00
if data[1] == 0xA1 then
print ("I sniff Happy 300 packet")
return
end
if data[1] == 0xA8 then
print ("I sniff They said Bye-Bye")
return
end
if data[1] == 0x10 and dlc == 5 then
return
end
top4 = math.floor(data[1] / 16)
if top4 == 0xB then
2023-03-05 14:29:05 -08:00
-- ACK
2023-03-05 11:52:41 -08:00
return
end
if top4 == 2 or top4 == 1 then
print ("Looks like payload index " ..payLoadIndex ..": " ..arrayToString(data))
if payLoadIndex == 1 then
H4 = data[4]
H7 = data[7] or -1
2023-03-05 14:29:05 -08:00
-- print("h5/h7 " ..H4 .." " ..H7)
2023-03-05 11:52:41 -08:00
if H7 == 0 then
2023-10-07 21:14:29 -07:00
-- print("I sniff diag: TCU is happy!")
2023-03-05 11:52:41 -08:00
else
2023-10-07 21:14:29 -07:00
-- print("I sniff diag: TCU has ERROR ERROR ERROR")
2023-03-05 11:52:41 -08:00
end
end
payLoadIndex = payLoadIndex + 1
2023-10-07 21:14:29 -07:00
if top4 == 1 or top4 == 2 then
2023-03-05 11:52:41 -08:00
payLoadIndex = 0
len = data[3]
2023-03-05 14:29:05 -08:00
if data[2] == 0 and data[4] == 0x58 then
if len == 2 then
print("I sniff TCU NO CODES")
else
2023-10-07 21:14:29 -07:00
print("**************** I sniff ERROR ERROR " .. data[5])
2023-03-05 14:29:05 -08:00
end
end
2023-03-05 11:52:41 -08:00
end
2023-03-05 14:29:05 -08:00
return
2023-03-05 11:52:41 -08:00
end
2023-03-05 14:29:05 -08:00
print('Got unexpected ' ..arrayToString(data))
2023-03-05 11:52:41 -08:00
end
function drop(bus, id, dlc, data)
end
motorBreCounter = 0
2023-03-05 12:06:33 -08:00
function sendMotorBre()
2023-03-05 14:29:05 -08:00
motorBreCounter = (motorBreCounter + 1) % 16
2023-03-05 11:52:41 -08:00
2023-03-05 14:29:05 -08:00
setBitRange(motorBreData, 8, 4, motorBreCounter)
xorChecksum(motorBreData, 1)
2023-03-05 11:52:41 -08:00
2023-03-05 14:29:05 -08:00
txCan(TCU_BUS, MOTOR_BRE, 0, motorBreData)
2023-03-05 11:52:41 -08:00
end
2023-10-08 20:19:34 -07:00
motor5counter = 0
2023-03-05 11:52:41 -08:00
motor5FuelCounter = 0
2023-03-05 14:42:55 -08:00
function sendMotor5()
2023-10-08 20:19:34 -07:00
-- motor5counter = (motor5counter + 1) % 16
-- todo index = math.floor(motor5counter / 4)
2023-03-05 14:29:05 -08:00
setBitRange(motor5Data, 5, 9, motor5FuelCounter)
xorChecksum(motor5Data, 8)
txCan(TCU_BUS, MOTOR_5, 0, motor5Data)
2023-03-05 11:52:41 -08:00
end
2024-02-10 19:24:18 -08:00
motor6Counter = 0
2023-03-05 14:28:28 -08:00
function sendMotor6()
2024-02-10 19:24:18 -08:00
motor6Counter = (motor6Counter + 1) % 16
2023-03-05 11:52:41 -08:00
2023-03-05 14:29:05 -08:00
engineTorque = fakeTorque * 0.9
actualTorque = fakeTorque
feedbackGearbox = 255
2023-03-05 11:52:41 -08:00
2023-03-05 14:29:05 -08:00
motor6Data[2] = math.floor(engineTorque / 0.39)
motor6Data[3] = math.floor(actualTorque / 0.39)
motor6Data[6] = math.floor(feedbackGearbox / 0.39)
2024-02-10 19:24:18 -08:00
setBitRange(motor6Data, 60, 4, motor6Counter)
2023-03-05 11:52:41 -08:00
2023-03-05 14:29:05 -08:00
xorChecksum(motor6Data, 1)
txCan(TCU_BUS, MOTOR_6, 0, motor6Data)
2023-03-05 11:52:41 -08:00
end
2023-03-05 12:01:12 -08:00
function sendMotor7()
2023-03-05 14:29:05 -08:00
txCan(TCU_BUS, MOTOR_7, 0, motor7Data)
2023-03-05 11:52:41 -08:00
end
2023-03-05 14:51:38 -08:00
accGraCounter = 0
function sendAccGra()
accGraCounter = (accGraCounter + 1) % 16
setBitRange(accGraData, 60, 4, accGraCounter)
xorChecksum(accGraData, 1)
txCan(TCU_BUS, ACC_GRA, 0, accGraData)
end
2023-03-05 11:52:41 -08:00
canMotorInfoCounter = 0
2023-03-05 12:01:12 -08:00
function sendMotorInfo()
2023-03-05 14:29:05 -08:00
canMotorInfoTotalCounter = canMotorInfoTotalCounter + 1
canMotorInfoCounter = (canMotorInfoCounter + 1) % 16
baseByte = canMotorInfoTotalCounter < 6 and 0x80 or 0x90
2024-02-10 19:07:44 -08:00
canMotorInfo02[1] = baseByte + (canMotorInfoCounter)
2023-03-05 14:29:05 -08:00
canMotorInfo1[1] = baseByte + (canMotorInfoCounter)
canMotorInfo3[1] = baseByte + (canMotorInfoCounter)
mod4 = canMotorInfoCounter % 4
if (mod4 == 0 or mod4 == 2) then
2024-02-10 19:07:44 -08:00
txCan(TCU_BUS, MOTOR_INFO, 0, canMotorInfo02)
2023-03-05 14:29:05 -08:00
elseif (mod4 == 1) then
txCan(TCU_BUS, MOTOR_INFO, 0, canMotorInfo1)
else
txCan(TCU_BUS, MOTOR_INFO, 0, canMotorInfo3)
end
2023-03-05 11:52:41 -08:00
end
hexstr = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "A", "B", "C", "D", "E", "F" }
function toHexString(num)
2023-03-05 14:29:05 -08:00
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
2023-03-05 11:52:41 -08:00
end
function arrayToString(arr)
2023-03-05 14:29:05 -08:00
local str = ""
local index = 1
while arr[index] ~= nil do
str = str.." "..toHexString(arr[index])
index = index + 1
end
return str
2023-03-05 11:52:41 -08:00
end
totalEcuMessages = 0
-- REQUIRED kombi 3
canRxAdd(ECU_BUS, 1312, relayFromECU)
-- REQUIRED Soll_Verbauliste_neu
canRxAdd(ECU_BUS, 1500, relayFromECU)
-- REQUIRED GRA_Neu
canRxAdd(ECU_BUS, 906, relayFromECU)
2023-10-08 21:09:00 -07:00
2023-10-08 20:51:55 -07:00
canRxAdd(ECU_BUS, BRAKE_1_416, relayFromECU)
2023-10-08 21:10:13 -07:00
--canRxAdd(ECU_BUS, BRAKE_2_1440, relayFromECU)
2023-10-08 20:51:55 -07:00
canRxAdd(ECU_BUS, BRAKE_8_428, relayFromECU)
2023-10-08 21:09:00 -07:00
2023-10-08 20:51:55 -07:00
canRxAdd(ECU_BUS, BRAKE_3_1184, relayFromECU)
2023-10-08 21:09:00 -07:00
2023-10-08 20:51:55 -07:00
canRxAdd(ECU_BUS, BRAKE_5_1192, relayFromECU)
2023-10-08 21:11:51 -07:00
--canRxAdd(ECU_BUS, Komf_1_912, relayFromECU)
2023-03-05 11:52:41 -08:00
-- Systeminfo_1
canRxAdd(ECU_BUS, 1488, relayFromECU)
-- REQUIRED? Diagnose_1
canRxAdd(ECU_BUS, 2000, relayFromECU)
canRxAdd(ECU_BUS, MOTOR_1, onMotor1)
2023-03-05 14:29:05 -08:00
-- canRxAdd(ECU_BUS, MOTOR_BRE, relayFromECU)
2023-03-05 14:37:34 -08:00
-- canRxAdd(ECU_BUS, MOTOR_2, relayFromECU)
2023-03-05 14:28:28 -08:00
canRxAdd(ECU_BUS, MOTOR_3, onMotor3)
2023-03-05 14:29:05 -08:00
-- canRxAdd(ECU_BUS, MOTOR_3, relayFromECU)
2023-03-05 14:28:28 -08:00
2023-03-05 14:42:55 -08:00
-- canRxAdd(ECU_BUS, MOTOR_5, onMotor5)
2023-03-05 14:29:05 -08:00
-- canRxAdd(ECU_BUS, MOTOR_5, relayFromECU)
2023-03-05 14:28:28 -08:00
2023-03-05 14:51:38 -08:00
--canRxAdd(ECU_BUS, MOTOR_6, sendMotor6)
2023-03-05 14:29:05 -08:00
-- canRxAdd(ECU_BUS, MOTOR_6, relayFromECU)
2023-03-05 14:28:28 -08:00
2023-03-05 14:51:38 -08:00
--canRxAdd(ECU_BUS, ACC_GRA, relayFromECU)
2023-03-05 14:29:05 -08:00
-- canRxAdd(ECU_BUS, MOTOR_INFO, relayFromECU)
2023-03-05 11:52:41 -08:00
canRxAdd(ECU_BUS, VWTP_OUT, relayFromECU)
canRxAdd(ECU_BUS, 0x760, relayFromECU)
canRxAddMask(ECU_BUS, 0, 0, drop)
2023-03-05 14:29:05 -08:00
-- canRxAddMask(ECU_BUS, 0, 0, relayFromECU)
2023-10-08 11:56:19 -07:00
canRxAdd(TCU_BUS, VWTP_IN, relayTcuDiagHelloFromTCU)
canRxAdd(TCU_BUS, VWTP_TESTER, relayTpPayloadFromTCU)
2023-10-07 21:36:11 -07:00
canRxAdd(TCU_1088_440, onTcu440)
2023-03-05 11:52:41 -08:00
canRxAddMask(TCU_BUS, 0, 0, relayFromTCU)
everySecondTimer = Timer.new()
function onTick()
2023-10-08 20:19:34 -07:00
if onEcuTimer : getElapsedSeconds() > 1 then
-- do not spam if ECU is silent
return
end
2023-03-05 14:37:34 -08:00
sendMotor2()
2023-03-05 14:51:38 -08:00
sendMotor3()
2023-03-05 14:42:55 -08:00
sendMotor5()
2023-03-05 14:51:38 -08:00
sendMotor6()
2023-03-05 14:29:05 -08:00
sendMotor7()
sendMotorBre()
2023-03-05 14:51:38 -08:00
sendAccGra()
2023-03-05 11:52:41 -08:00
2023-10-08 20:51:55 -07:00
-- txCan(TCU_BUS, BRAKE_1_416, 0, payload416)
-- txCan(TCU_BUS, BRAKE_8_428, 0, payload428)
2023-10-08 21:10:13 -07:00
txCan(TCU_BUS, BRAKE_2_1440, 0, payload1440)
2023-10-08 20:51:55 -07:00
2023-10-08 21:11:51 -07:00
txCan(TCU_BUS, Komf_1_912, 0, payload912)
2023-10-08 21:09:00 -07:00
2023-03-05 14:29:05 -08:00
if everySecondTimer : getElapsedSeconds() > 1 then
everySecondTimer : reset()
print("Total from ECU " ..totalEcuMessages)
motor5FuelCounter = motor5FuelCounter + 20
sendMotorInfo()
end
2023-03-05 11:52:41 -08:00
end
2023-03-05 14:29:05 -08:00