code formatting

This commit is contained in:
rusefillc 2023-03-05 17:29:05 -05:00
parent c7efe77c7a
commit 03df014ff9
1 changed files with 161 additions and 160 deletions

View File

@ -27,16 +27,16 @@ MOTOR_INFO = 0x580
MOTOR_7 = 0x588 MOTOR_7 = 0x588
motor1Data = { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } motor1Data = { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
motorBreData={ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } motorBreData = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
motor2Data = { 0x8A, 0x8D, 0x10, 0x04, 0x00, 0x4C, 0xDC, 0x87 } motor2Data = { 0x8A, 0x8D, 0x10, 0x04, 0x00, 0x4C, 0xDC, 0x87 }
motor2mux = {0x8A, 0xE8, 0x2C, 0x64} motor2mux = { 0x8A, 0xE8, 0x2C, 0x64 }
canMotor3 = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } canMotor3 = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
motor5Data = { 0x1C, 0x08, 0xF3, 0x55, 0x19, 0x00, 0x00, 0xAD } motor5Data = { 0x1C, 0x08, 0xF3, 0x55, 0x19, 0x00, 0x00, 0xAD }
motor6Data = { 0x00, 0x00, 0x00, 0x7E, 0xFE, 0xFF, 0xFF, 0x00 } motor6Data = { 0x00, 0x00, 0x00, 0x7E, 0xFE, 0xFF, 0xFF, 0x00 }
accGraData = { 0x00, 0x00, 0x08, 0x00, 0x1A, 0x00, 0x02, 0x01 } accGraData = { 0x00, 0x00, 0x08, 0x00, 0x1A, 0x00, 0x02, 0x01 }
canMotorInfo = { 0x00, 0x00, 0x00, 0x14, 0x1C, 0x93, 0x48, 0x14 } canMotorInfo = { 0x00, 0x00, 0x00, 0x14, 0x1C, 0x93, 0x48, 0x14 }
canMotorInfo1= { 0x99, 0x14, 0x00, 0x7F, 0x00, 0xF0, 0x47, 0x01 } canMotorInfo1 = { 0x99, 0x14, 0x00, 0x7F, 0x00, 0xF0, 0x47, 0x01 }
canMotorInfo3= { 0x9B, 0x14, 0x00, 0x11, 0x1F, 0xE0, 0x0C, 0x46 } canMotorInfo3 = { 0x9B, 0x14, 0x00, 0x11, 0x1F, 0xE0, 0x0C, 0x46 }
motor7Data = { 0x1A, 0x66, 0x7E, 0x00, 0x00, 0x00, 0x00, 0x00 } motor7Data = { 0x1A, 0x66, 0x7E, 0x00, 0x00, 0x00, 0x00, 0x00 }
canMotorInfoTotalCounter = 0 canMotorInfoTotalCounter = 0
@ -61,104 +61,104 @@ rpm = 0
tps = 0 tps = 0
function xorChecksum(data, targetIndex) function xorChecksum(data, targetIndex)
local index = 1 local index = 1
local result = 0 local result = 0
while data[index] ~= nil do while data[index] ~= nil do
if index ~= targetIndex then if index ~= targetIndex then
result = result ~ data[index] result = result ~ data[index]
end end
index = index + 1 index = index + 1
end end
data[targetIndex] = result data[targetIndex] = result
return result return result
end end
function setBitRange(data, totalBitIndex, bitWidth, value) function setBitRange(data, totalBitIndex, bitWidth, value)
local byteIndex = totalBitIndex >> 3 local byteIndex = totalBitIndex >> 3
local bitInByteIndex = totalBitIndex - byteIndex * 8 local bitInByteIndex = totalBitIndex - byteIndex * 8
if (bitInByteIndex + bitWidth > 8) then if (bitInByteIndex + bitWidth > 8) then
bitsToHandleNow = 8 - bitInByteIndex bitsToHandleNow = 8 - bitInByteIndex
setBitRange(data, totalBitIndex + bitsToHandleNow, bitWidth - bitsToHandleNow, value >> bitsToHandleNow) setBitRange(data, totalBitIndex + bitsToHandleNow, bitWidth - bitsToHandleNow, value >> bitsToHandleNow)
bitWidth = bitsToHandleNow bitWidth = bitsToHandleNow
end end
mask = (1 << bitWidth) - 1 mask = (1 << bitWidth) - 1
data[1 + byteIndex] = data[1 + byteIndex] & (~(mask << bitInByteIndex)) data[1 + byteIndex] = data[1 + byteIndex] & (~(mask << bitInByteIndex))
maskedValue = value & mask maskedValue = value & mask
shiftedValue = maskedValue << bitInByteIndex shiftedValue = maskedValue << bitInByteIndex
data[1 + byteIndex] = data[1 + byteIndex] | shiftedValue data[1 + byteIndex] = data[1 + byteIndex] | shiftedValue
end end
function relayFromECU(bus, id, dlc, data) function relayFromECU(bus, id, dlc, data)
totalEcuMessages = totalEcuMessages + 1 totalEcuMessages = totalEcuMessages + 1
-- print("Relaying to TCU " .. id) -- print("Relaying to TCU " .. id)
txCan(TCU_BUS, id, 0, data) -- relay non-TCU message to TCU txCan(TCU_BUS, id, 0, data) -- relay non-TCU message to TCU
end end
function sendMotor1() function sendMotor1()
engineTorque = fakeTorque * 0.9 engineTorque = fakeTorque * 0.9
innerTorqWithoutExt = fakeTorque innerTorqWithoutExt = fakeTorque
torqueLoss = 20 torqueLoss = 20
requestedTorque = fakeTorque requestedTorque = fakeTorque
motor1Data[2] = engineTorque / 0.39 motor1Data[2] = engineTorque / 0.39
setTwoBytes(motor1Data, 2, rpm / 0.25) setTwoBytes(motor1Data, 2, rpm / 0.25)
motor1Data[5] = innerTorqWithoutExt / 0.4 motor1Data[5] = innerTorqWithoutExt / 0.4
motor1Data[6] = tps / 0.4 motor1Data[6] = tps / 0.4
motor1Data[7] = torqueLoss / 0.39 motor1Data[7] = torqueLoss / 0.39
motor1Data[8] = requestedTorque / 0.39 motor1Data[8] = requestedTorque / 0.39
txCan(TCU_BUS, MOTOR_1, 0, motor1Data) txCan(TCU_BUS, MOTOR_1, 0, motor1Data)
end end
function getBitRange(data, bitIndex, bitWidth) function getBitRange(data, bitIndex, bitWidth)
byteIndex = bitIndex >> 3 byteIndex = bitIndex >> 3
shift = bitIndex - byteIndex * 8 shift = bitIndex - byteIndex * 8
value = data[1 + byteIndex] value = data[1 + byteIndex]
if (shift + bitWidth > 8) then if (shift + bitWidth > 8) then
value = value + data[2 + byteIndex] * 256 value = value + data[2 + byteIndex] * 256
end end
mask = (1 << bitWidth) - 1 mask = (1 << bitWidth) - 1
return (value >> shift) & mask return (value >> shift) & mask
end end
function sendMotor3() function sendMotor3()
desired_wheel_torque = fakeTorque desired_wheel_torque = fakeTorque
canMotor3[2] = (iat + 48) / 0.75 canMotor3[2] = (iat + 48) / 0.75
canMotor3[3] = tps / 0.4 canMotor3[3] = tps / 0.4
canMotor3[5] = 0x20 canMotor3[5] = 0x20
setBitRange(canMotor3, 24, 12, math.floor(desired_wheel_torque / 0.39)) setBitRange(canMotor3, 24, 12, math.floor(desired_wheel_torque / 0.39))
canMotor3[8] = tps / 0.4 canMotor3[8] = tps / 0.4
txCan(TCU_BUS, MOTOR_3, 0, canMotor3) txCan(TCU_BUS, MOTOR_3, 0, canMotor3)
end end
function onMotor3(bus, id, dlc, data) function onMotor3(bus, id, dlc, data)
totalEcuMessages = totalEcuMessages + 1 totalEcuMessages = totalEcuMessages + 1
iat = getBitRange(data, 8, 8) * 0.75 - 48 iat = getBitRange(data, 8, 8) * 0.75 - 48
pps = getBitRange(data, 16, 8) * 0.40 pps = getBitRange(data, 16, 8) * 0.40
tps = getBitRange(data, 56, 8) * 0.40 tps = getBitRange(data, 56, 8) * 0.40
-- print ('MOTOR_1 pps ' ..pps ..' tps ' ..tps ..' iat ' ..iat) -- print ('MOTOR_1 pps ' ..pps ..' tps ' ..tps ..' iat ' ..iat)
sendMotor3() sendMotor3()
end end
function onMotor1(bus, id, dlc, data) function onMotor1(bus, id, dlc, data)
totalEcuMessages = totalEcuMessages + 1 totalEcuMessages = totalEcuMessages + 1
rpm = getBitRange(data, 16, 16) * 0.25 rpm = getBitRange(data, 16, 16) * 0.25
if rpm == 0 then if rpm == 0 then
canMotorInfoTotalCounter = 0 canMotorInfoTotalCounter = 0
end end
tps = getBitRange(data, 40, 8) * 0.4
fakeTorque = interpolate(0, 6, 100, 60, tps) tps = getBitRange(data, 40, 8) * 0.4
-- sendMotor1() fakeTorque = interpolate(0, 6, 100, 60, tps)
relayFromECU(bus, id, dlc, data)
-- sendMotor1()
relayFromECU(bus, id, dlc, data)
end end
function relayFromECUAndEcho(bus, id, dlc, data) function relayFromECUAndEcho(bus, id, dlc, data)
totalEcuMessages = totalEcuMessages + 1 totalEcuMessages = totalEcuMessages + 1
print("Relaying to TCU " .. id) print("Relaying to TCU " ..id)
txCan(TCU_BUS, id, 0, data) -- relay non-TCU message to TCU txCan(TCU_BUS, id, 0, data) -- relay non-TCU message to TCU
end end
@ -166,29 +166,29 @@ totalTcuMessages = 0
function relayFromTCU(bus, id, dlc, data) function relayFromTCU(bus, id, dlc, data)
totalTcuMessages = totalTcuMessages + 1 totalTcuMessages = totalTcuMessages + 1
-- print("Relaying to ECU " .. id) -- print("Relaying to ECU " .. id)
txCan(ECU_BUS, id, 0, data) -- relay non-ECU message to ECU txCan(ECU_BUS, id, 0, data) -- relay non-ECU message to ECU
end end
function relayTcuDiagHelloFromTCU(bus, id, dlc, data) function relayTcuDiagHelloFromTCU(bus, id, dlc, data)
totalTcuMessages = totalTcuMessages + 1 totalTcuMessages = totalTcuMessages + 1
print("Relaying TcuDiagHello to ECU " .. id .. arrayToString(data)) print("Relaying TcuDiagHello to ECU " ..id ..arrayToString(data))
txCan(ECU_BUS, id, 0, data) -- relay non-ECU message to ECU txCan(ECU_BUS, id, 0, data) -- relay non-ECU message to ECU
end end
local payLoadIndex = 0 local payLoadIndex = 0
function relayTpPayloadFromTCU(bus, id, dlc, data) function relayTpPayloadFromTCU(bus, id, dlc, data)
totalTcuMessages = totalTcuMessages + 1 totalTcuMessages = totalTcuMessages + 1
-- print("Relaying TP ECU " ..id ..arrayToString(data)) -- print("Relaying TP ECU " ..id ..arrayToString(data))
txCan(ECU_BUS, id, 0, data) -- relay non-ECU message to ECU txCan(ECU_BUS, id, 0, data) -- relay non-ECU message to ECU
if data[1] == 0xA3 then if data[1] == 0xA3 then
-- print ("Keep-alive") -- print ("Keep-alive")
return return
end end
if data[1] == 0xA1 then if data[1] == 0xA1 then
print ("I sniff Happy 300 packet") print ("I sniff Happy 300 packet")
@ -206,7 +206,7 @@ function relayTpPayloadFromTCU(bus, id, dlc, data)
top4 = math.floor(data[1] / 16) top4 = math.floor(data[1] / 16)
if top4 == 0xB then if top4 == 0xB then
-- ACK -- ACK
return return
end end
@ -217,7 +217,7 @@ function relayTpPayloadFromTCU(bus, id, dlc, data)
H4 = data[4] H4 = data[4]
H7 = data[7] or -1 H7 = data[7] or -1
-- print("h5/h7 " ..H4 .." " ..H7) -- print("h5/h7 " ..H4 .." " ..H7)
if H7 == 0 then if H7 == 0 then
print("I sniff diag: TCU is happy!") print("I sniff diag: TCU is happy!")
@ -233,19 +233,19 @@ function relayTpPayloadFromTCU(bus, id, dlc, data)
payLoadIndex = 0 payLoadIndex = 0
len = data[3] len = data[3]
if data[2] == 0 and data[4] == 0x58 then if data[2] == 0 and data[4] == 0x58 then
if len == 2 then if len == 2 then
print("I sniff TCU NO CODES") print("I sniff TCU NO CODES")
else else
print("I sniff TCU NO CODES") print("I sniff TCU NO CODES")
end end
end end
end end
return return
end end
print('Got unexpected ' ..arrayToString(data)) print('Got unexpected ' ..arrayToString(data))
end end
function drop(bus, id, dlc, data) function drop(bus, id, dlc, data)
@ -253,94 +253,94 @@ end
motorBreCounter = 0 motorBreCounter = 0
function sendMotorBre() function sendMotorBre()
motorBreCounter = (motorBreCounter + 1) % 16 motorBreCounter = (motorBreCounter + 1) % 16
setBitRange(motorBreData, 8, 4, motorBreCounter) setBitRange(motorBreData, 8, 4, motorBreCounter)
xorChecksum(motorBreData, 1) xorChecksum(motorBreData, 1)
txCan(TCU_BUS, MOTOR_BRE, 0, motorBreData) txCan(TCU_BUS, MOTOR_BRE, 0, motorBreData)
end end
motor5FuelCounter = 0 motor5FuelCounter = 0
function onMotor5(bus, id, dlc, data) function onMotor5(bus, id, dlc, data)
setBitRange(motor5Data, 5, 9, motor5FuelCounter) setBitRange(motor5Data, 5, 9, motor5FuelCounter)
xorChecksum(motor5Data, 8) xorChecksum(motor5Data, 8)
txCan(TCU_BUS, MOTOR_5, 0, motor5Data) txCan(TCU_BUS, MOTOR_5, 0, motor5Data)
end end
counter16 = 0 counter16 = 0
function sendMotor6() function sendMotor6()
counter16 = (counter16 + 1) % 16 counter16 = (counter16 + 1) % 16
engineTorque = fakeTorque * 0.9 engineTorque = fakeTorque * 0.9
actualTorque = fakeTorque actualTorque = fakeTorque
feedbackGearbox = 255 feedbackGearbox = 255
motor6Data[2] = math.floor(engineTorque / 0.39) motor6Data[2] = math.floor(engineTorque / 0.39)
motor6Data[3] = math.floor(actualTorque / 0.39) motor6Data[3] = math.floor(actualTorque / 0.39)
motor6Data[6] = math.floor(feedbackGearbox / 0.39) motor6Data[6] = math.floor(feedbackGearbox / 0.39)
setBitRange(motor6Data, 60, 4, counter16) setBitRange(motor6Data, 60, 4, counter16)
xorChecksum(motor6Data, 1) xorChecksum(motor6Data, 1)
txCan(TCU_BUS, MOTOR_6, 0, motor6Data) txCan(TCU_BUS, MOTOR_6, 0, motor6Data)
end end
function sendMotor7() function sendMotor7()
txCan(TCU_BUS, MOTOR_7, 0, motor7Data) txCan(TCU_BUS, MOTOR_7, 0, motor7Data)
end end
canMotorInfoCounter = 0 canMotorInfoCounter = 0
function sendMotorInfo() function sendMotorInfo()
canMotorInfoTotalCounter = canMotorInfoTotalCounter + 1 canMotorInfoTotalCounter = canMotorInfoTotalCounter + 1
canMotorInfoCounter = (canMotorInfoCounter + 1) % 16 canMotorInfoCounter = (canMotorInfoCounter + 1) % 16
baseByte = canMotorInfoTotalCounter < 6 and 0x80 or 0x90 baseByte = canMotorInfoTotalCounter < 6 and 0x80 or 0x90
canMotorInfo[1] = baseByte + (canMotorInfoCounter) canMotorInfo[1] = baseByte + (canMotorInfoCounter)
canMotorInfo1[1] = baseByte + (canMotorInfoCounter) canMotorInfo1[1] = baseByte + (canMotorInfoCounter)
canMotorInfo3[1] = baseByte + (canMotorInfoCounter) canMotorInfo3[1] = baseByte + (canMotorInfoCounter)
mod4 = canMotorInfoCounter % 4 mod4 = canMotorInfoCounter % 4
if (mod4 == 0 or mod4 == 2) then if (mod4 == 0 or mod4 == 2) then
txCan(TCU_BUS, MOTOR_INFO, 0, canMotorInfo) txCan(TCU_BUS, MOTOR_INFO, 0, canMotorInfo)
elseif (mod4 == 1) then elseif (mod4 == 1) then
txCan(TCU_BUS, MOTOR_INFO, 0, canMotorInfo1) txCan(TCU_BUS, MOTOR_INFO, 0, canMotorInfo1)
else else
txCan(TCU_BUS, MOTOR_INFO, 0, canMotorInfo3) txCan(TCU_BUS, MOTOR_INFO, 0, canMotorInfo3)
end end
end end
hexstr = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "A", "B", "C", "D", "E", "F" } hexstr = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "A", "B", "C", "D", "E", "F" }
function toHexString(num) function toHexString(num)
if num == 0 then if num == 0 then
return '0' return '0'
end end
local result = "" local result = ""
while num > 0 do while num > 0 do
local n = num % 16 local n = num % 16
result = hexstr[n + 1] ..result result = hexstr[n + 1] ..result
num = math.floor(num / 16) num = math.floor(num / 16)
end end
return result return result
end end
function arrayToString(arr) function arrayToString(arr)
local str = "" local str = ""
local index = 1 local index = 1
while arr[index] ~= nil do while arr[index] ~= nil do
str = str.." "..toHexString(arr[index]) str = str.." "..toHexString(arr[index])
index = index + 1 index = index + 1
end end
return str return str
end end
totalEcuMessages = 0 totalEcuMessages = 0
canRxAdd(ECU_BUS, MOTOR_7, drop) canRxAdd(ECU_BUS, MOTOR_7, drop)
--canRxAdd(ECU_BUS, ACC_GRA, drop) -- canRxAdd(ECU_BUS, ACC_GRA, drop)
-- REQUIRED kombi 3 -- REQUIRED kombi 3
canRxAdd(ECU_BUS, 1312, relayFromECU) canRxAdd(ECU_BUS, 1312, relayFromECU)
@ -366,20 +366,20 @@ canRxAdd(ECU_BUS, 1488, relayFromECU)
canRxAdd(ECU_BUS, 2000, relayFromECU) canRxAdd(ECU_BUS, 2000, relayFromECU)
canRxAdd(ECU_BUS, MOTOR_1, onMotor1) canRxAdd(ECU_BUS, MOTOR_1, onMotor1)
--canRxAdd(ECU_BUS, MOTOR_BRE, relayFromECU) -- canRxAdd(ECU_BUS, MOTOR_BRE, relayFromECU)
canRxAdd(ECU_BUS, MOTOR_2, relayFromECU) canRxAdd(ECU_BUS, MOTOR_2, relayFromECU)
canRxAdd(ECU_BUS, MOTOR_3, onMotor3) canRxAdd(ECU_BUS, MOTOR_3, onMotor3)
--canRxAdd(ECU_BUS, MOTOR_3, relayFromECU) -- canRxAdd(ECU_BUS, MOTOR_3, relayFromECU)
canRxAdd(ECU_BUS, MOTOR_5, onMotor5) canRxAdd(ECU_BUS, MOTOR_5, onMotor5)
--canRxAdd(ECU_BUS, MOTOR_5, relayFromECU) -- canRxAdd(ECU_BUS, MOTOR_5, relayFromECU)
canRxAdd(ECU_BUS, MOTOR_6, sendMotor6) canRxAdd(ECU_BUS, MOTOR_6, sendMotor6)
--canRxAdd(ECU_BUS, MOTOR_6, relayFromECU) -- canRxAdd(ECU_BUS, MOTOR_6, relayFromECU)
canRxAdd(ECU_BUS, ACC_GRA, relayFromECU) canRxAdd(ECU_BUS, ACC_GRA, relayFromECU)
--canRxAdd(ECU_BUS, MOTOR_INFO, relayFromECU) -- canRxAdd(ECU_BUS, MOTOR_INFO, relayFromECU)
canRxAdd(ECU_BUS, VWTP_OUT, relayFromECU) canRxAdd(ECU_BUS, VWTP_OUT, relayFromECU)
canRxAdd(ECU_BUS, 0x760, relayFromECU) canRxAdd(ECU_BUS, 0x760, relayFromECU)
@ -388,22 +388,23 @@ canRxAdd(TCU_BUS, VWTP_IN, relayTcuDiagHelloFromTCU)
canRxAdd(TCU_BUS, VWTP_TESTER, relayTpPayloadFromTCU) canRxAdd(TCU_BUS, VWTP_TESTER, relayTpPayloadFromTCU)
canRxAddMask(ECU_BUS, 0, 0, drop) canRxAddMask(ECU_BUS, 0, 0, drop)
--canRxAddMask(ECU_BUS, 0, 0, relayFromECU) -- canRxAddMask(ECU_BUS, 0, 0, relayFromECU)
canRxAddMask(TCU_BUS, 0, 0, relayFromTCU) canRxAddMask(TCU_BUS, 0, 0, relayFromTCU)
everySecondTimer = Timer.new() everySecondTimer = Timer.new()
function onTick() function onTick()
sendMotor7() sendMotor7()
sendMotorBre() sendMotorBre()
if everySecondTimer : getElapsedSeconds() > 1 then if everySecondTimer : getElapsedSeconds() > 1 then
everySecondTimer : reset() everySecondTimer : reset()
print("Total from ECU " ..totalEcuMessages) print("Total from ECU " ..totalEcuMessages)
motor5FuelCounter = motor5FuelCounter + 20 motor5FuelCounter = motor5FuelCounter + 20
sendMotorInfo() sendMotorInfo()
end end
end end