rusefi_documentation/B6-temp-TCU-simpler-repacka...

294 lines
7.9 KiB
Plaintext
Raw Normal View History

2022-08-25 09:53:01 -07:00
2022-12-25 21:14:59 -08:00
-- 640
2022-08-26 19:25:22 -07:00
MOTOR_1 = 0x280
2022-12-28 22:27:22 -08:00
-- 644
MOTOR_BRE = 0x284
-- 648
MOTOR_2 = 0x288
2022-12-27 07:37:41 -08:00
-- 896
2022-08-26 19:25:22 -07:00
MOTOR_3 = 0x380
2022-12-27 07:37:41 -08:00
-- 1152
2022-08-26 19:25:22 -07:00
MOTOR_5 = 0x480
2022-12-25 21:14:59 -08:00
-- 1160
2022-08-26 19:25:22 -07:00
MOTOR_6 = 0x488
2022-12-29 15:08:05 -08:00
-- 1386
ACC_GRA = 0x56A
2022-12-27 07:37:41 -08:00
-- 1408 the one with variable payload
MOTOR_INFO = 0x580
2022-12-25 21:14:59 -08:00
-- 1416
2022-08-26 19:25:22 -07:00
MOTOR_7 = 0x588
2022-12-25 21:14:59 -08:00
fakeTorque = 0
2022-09-17 20:48:14 -07:00
2022-09-04 08:32:43 -07:00
function xorChecksum(data, targetIndex)
2023-01-02 11:22:23 -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
2022-09-04 08:32:43 -07:00
end
2022-08-30 01:14:35 -07:00
2022-09-17 19:26:47 -07:00
function setBitRange(data, totalBitIndex, bitWidth, value)
2023-01-02 11:22:23 -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
2022-09-17 19:26:47 -07:00
end
2022-09-06 14:36:26 -07:00
2022-08-25 09:53:01 -07:00
function getBitRange(data, bitIndex, bitWidth)
2023-01-02 11:22:23 -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
2022-09-17 19:26:47 -07:00
end
2022-08-25 09:53:01 -07:00
-- 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(300)
ECU_BUS = 1
-- really 'not ECU'
TCU_BUS = 2
2022-08-26 18:29:50 -07:00
function setTwoBytes(data, offset, value)
2023-01-02 11:22:23 -08:00
value = math.floor(value)
data[offset + 2] = value >> 8
data[offset + 1] = value & 0xff
2022-09-17 19:26:47 -07:00
end
2022-08-26 18:29:50 -07:00
2022-08-25 09:53:01 -07:00
hexstr = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "A", "B", "C", "D", "E", "F" }
function toHexString(num)
2023-01-02 11:22:23 -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
2022-08-25 09:53:01 -07:00
end
function arrayToString(arr)
2023-01-02 11:22:23 -08:00
local str = ""
local index = 1
while arr[index] ~= nil do
str = str.." "..toHexString(arr[index])
index = index + 1
end
return str
2022-08-25 09:53:01 -07:00
end
totalEcuMessages = 0
2022-12-28 19:54:57 -08:00
motor1Data = { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
2022-12-29 14:52:50 -08:00
motorBreData={ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
motor2Data = { 0x8A, 0x8D, 0x10, 0x04, 0x00, 0x4C, 0xDC, 0x87 }
2022-12-30 18:47:12 -08:00
motor2mux = {0x8A, 0xE8, 0x2C, 0x64}
2022-09-17 19:26:47 -07:00
canMotor3 = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
2022-12-25 11:48:39 -08:00
motor5Data = { 0x1C, 0x08, 0xF3, 0x55, 0x19, 0x00, 0x00, 0xAD }
2022-12-25 21:14:59 -08:00
motor6Data = { 0x00, 0x00, 0x00, 0x7E, 0xFE, 0xFF, 0xFF, 0x00 }
2022-12-29 15:08:05 -08:00
accGraData = { 0x00, 0x00, 0x08, 0x00, 0x1A, 0x00, 0x02, 0x01 }
2022-12-30 19:11:59 -08:00
canMotorInfo = { 0x00, 0x00, 0x00, 0x14, 0x1C, 0x93, 0x48, 0x14 }
canMotorInfo1= { 0x99, 0x14, 0x00, 0x7F, 0x00, 0xF0, 0x47, 0x01 }
canMotorInfo3= { 0x9B, 0x14, 0x00, 0x11, 0x1F, 0xE0, 0x0C, 0x46 }
motor7Data = { 0x1A, 0x66, 0x7E, 0x00, 0x00, 0x00, 0x00, 0x00 }
2022-08-26 18:29:50 -07:00
2023-01-09 18:07:32 -08:00
canMotorInfoTotalCounter = 0
2022-08-25 09:53:01 -07:00
function onMotor1(bus, id, dlc, data)
2022-12-30 19:11:59 -08:00
totalEcuMessages = totalEcuMessages + 1
2023-01-02 11:22:23 -08:00
rpm = getBitRange(data, 16, 16) * 0.25
2023-01-09 18:07:32 -08:00
if rpm == 0 then
canMotorInfoTotalCounter = 0
end
2023-01-02 11:22:23 -08:00
tps = getBitRange(data, 40, 8) * 0.4
2022-09-17 19:26:47 -07:00
2023-01-02 11:22:23 -08:00
fakeTorque = interpolate(0, 6, 100, 60, tps)
2022-09-17 19:26:47 -07:00
2023-01-02 11:22:23 -08:00
-- engineTorque = getBitRange(data, 8, 8) * 0.39
-- innerTorqWithoutExt = getBitRange(data, 32, 8) * 0.4
-- torqueLoss = getBitRange(data, 48, 8) * 0.39
-- requestedTorque = getBitRange(data, 56, 8) * 0.39
2022-09-17 19:26:47 -07:00
2023-01-02 11:22:23 -08:00
engineTorque = fakeTorque * 0.9
innerTorqWithoutExt = fakeTorque
torqueLoss = 20
requestedTorque = fakeTorque
2022-09-17 19:26:47 -07:00
2023-01-02 11:22:23 -08:00
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
2022-09-17 19:26:47 -07:00
2023-01-02 11:22:23 -08:00
-- print ('MOTOR_1 fakeTorque ' ..fakeTorque)
-- print ('MOTOR_1 engineTorque ' ..engineTorque ..' RPM ' ..rpm)
-- print ('MOTOR_1 innerTorqWithoutExt ' ..innerTorqWithoutExt ..' tps ' ..tps)
2022-09-17 19:26:47 -07:00
2023-01-02 11:22:23 -08:00
-- print ('MOTOR_1 torqueLoss ' ..torqueLoss ..' requestedTorque ' ..requestedTorque)
2022-09-17 19:26:47 -07:00
2023-01-09 21:46:36 -08:00
txCan(TCU_BUS, MOTOR_1, 0, motor1Data)
2022-08-25 09:53:01 -07:00
end
2022-12-30 19:11:59 -08:00
motorBreCounter = 0
function onMotorBre(bus, id, dlc, data)
2023-01-02 11:22:23 -08:00
motorBreCounter = (motorBreCounter + 1) % 16
2022-12-30 19:11:59 -08:00
setBitRange(motorBreData, 8, 4, motorBreCounter)
xorChecksum(motorBreData, 1)
2023-01-09 21:46:36 -08:00
txCan(TCU_BUS, MOTOR_BRE, 0, motorBreData)
2022-12-30 19:11:59 -08:00
end
motor2counter = 0
function onMotor2(bus, id, dlc, data)
2023-01-02 11:22:23 -08:00
motor2counter = (motor2counter + 1) % 16
2022-12-30 19:11:59 -08:00
minTorque = fakeTorque / 2
2023-01-10 22:37:50 -08:00
-- todo: add CLT
2022-12-30 19:11:59 -08:00
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)
2022-12-30 19:36:18 -08:00
index = math.floor(motor2counter / 4)
motor2Data[1] = motor2mux[1 + index]
2022-12-30 19:11:59 -08:00
2023-01-09 21:46:36 -08:00
txCan(TCU_BUS, MOTOR_2, 0, motor2Data)
2022-12-30 19:11:59 -08:00
end
2022-09-05 18:48:57 -07:00
function onMotor3(bus, id, dlc, data)
2022-12-30 19:11:59 -08:00
totalEcuMessages = totalEcuMessages + 1
2023-01-02 11:22:23 -08:00
iat = getBitRange(data, 8, 8) * 0.75 - 48
pps = getBitRange(data, 16, 8) * 0.40
tps = getBitRange(data, 56, 8) * 0.40
-- print ('MOTOR_1 pps ' ..pps ..' tps ' ..tps ..' iat ' ..iat)
desired_wheel_torque = fakeTorque
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
2023-01-09 21:46:36 -08:00
txCan(TCU_BUS, MOTOR_3, 0, canMotor3)
2022-09-05 18:48:57 -07:00
end
2022-08-25 09:53:01 -07:00
2022-12-30 18:47:12 -08:00
motor5FuelCounter = 0
2022-09-17 20:48:14 -07:00
function onMotor5(bus, id, dlc, data)
2023-01-02 11:22:23 -08:00
setBitRange(motor5Data, 5, 9, motor5FuelCounter)
xorChecksum(motor5Data, 8)
2023-01-09 21:46:36 -08:00
txCan(TCU_BUS, MOTOR_5, 0, motor5Data)
2022-09-17 20:48:14 -07:00
end
2022-12-30 18:47:12 -08:00
counter16 = 0
2022-12-25 21:14:59 -08:00
function onMotor6(bus, id, dlc, data)
2023-01-02 11:22:23 -08:00
counter16 = (counter16 + 1) % 16
-- engineTorque = getBitRange(data, 8, 8) * 0.39
-- actualTorque = getBitRange(data, 16, 8) * 0.39
-- feedbackGearbox = getBitRange(data, 40, 8) * 0.39
engineTorque = fakeTorque * 0.9
actualTorque = fakeTorque
feedbackGearbox = 255
motor6Data[2] = math.floor(engineTorque / 0.39)
motor6Data[3] = math.floor(actualTorque / 0.39)
motor6Data[6] = math.floor(feedbackGearbox / 0.39)
setBitRange(motor6Data, 60, 4, counter16)
xorChecksum(motor6Data, 1)
2023-01-09 21:46:36 -08:00
txCan(TCU_BUS, MOTOR_6, 0, motor6Data)
2022-12-25 21:14:59 -08:00
end
2022-12-30 19:11:59 -08:00
accGraCounter = 0
function onAccGra(bus, id, dlc, data)
2023-01-02 11:22:23 -08:00
accGraCounter = (accGraCounter + 1) % 16
setBitRange(accGraData, 60, 4, accGraCounter)
2022-12-30 19:11:59 -08:00
xorChecksum(accGraData, 1)
2023-01-02 11:22:23 -08:00
txCan(TCU_BUS, id, 0, accGraData)
-- txCan(TCU_BUS, id, 0, data)
2022-12-30 19:11:59 -08:00
end
2022-12-30 18:47:12 -08:00
canMotorInfoCounter = 0
2022-12-28 22:27:22 -08:00
function onMotorInfo(bus, id, dlc, data)
2023-01-09 18:07:32 -08:00
canMotorInfoTotalCounter = canMotorInfoTotalCounter + 1
-- canMotorInfoCounter = (canMotorInfoCounter + 1) % 16
canMotorInfoCounter = getBitRange(data, 0, 4)
2023-01-02 11:22:23 -08:00
2023-01-09 18:07:32 -08:00
baseByte = canMotorInfoTotalCounter < 6 and 0x80 or 0x90
canMotorInfo[1] = baseByte + (canMotorInfoCounter)
canMotorInfo1[1] = baseByte + (canMotorInfoCounter)
canMotorInfo3[1] = baseByte + (canMotorInfoCounter)
mod4 = canMotorInfoCounter % 4
2023-01-02 11:22:23 -08:00
if (mod4 == 0 or mod4 == 2) then
2023-01-09 18:07:32 -08:00
txCan(TCU_BUS, MOTOR_INFO, 0, canMotorInfo)
2023-01-02 11:22:23 -08:00
elseif (mod4 == 1) then
2023-01-09 18:07:32 -08:00
txCan(TCU_BUS, MOTOR_INFO, 0, canMotorInfo1)
2023-01-02 11:22:23 -08:00
else
2023-01-09 18:07:32 -08:00
txCan(TCU_BUS, MOTOR_INFO, 0, canMotorInfo3)
2022-12-30 18:47:12 -08:00
end
2022-12-28 22:27:22 -08:00
end
function onMotor7(bus, id, dlc, data)
2023-01-09 21:46:36 -08:00
txCan(TCU_BUS, MOTOR_7, 0, motor7Data)
2022-12-28 22:27:22 -08:00
end
2022-09-17 19:26:47 -07:00
canRxAdd(ECU_BUS, MOTOR_1, onMotor1)
2022-12-28 22:27:22 -08:00
canRxAdd(ECU_BUS, MOTOR_BRE, onMotorBre)
canRxAdd(ECU_BUS, MOTOR_2, onMotor2)
canRxAdd(ECU_BUS, MOTOR_3, onMotor3)
canRxAdd(ECU_BUS, MOTOR_5, onMotor5)
2022-12-29 16:34:58 -08:00
canRxAdd(ECU_BUS, MOTOR_INFO, onMotorInfo)
2022-12-25 21:14:59 -08:00
canRxAdd(ECU_BUS, MOTOR_6, onMotor6)
2022-12-28 22:27:22 -08:00
canRxAdd(ECU_BUS, MOTOR_7, onMotor7)
2022-12-29 15:08:05 -08:00
canRxAdd(ECU_BUS, ACC_GRA, onAccGra)
2022-12-28 22:27:22 -08:00
2022-08-25 09:53:01 -07:00
everySecondTimer = Timer.new()
2022-12-28 19:54:57 -08:00
2022-12-25 11:00:12 -08:00
mafSensor = Sensor.new("maf")
mafCalibrationIndex = findCurveIndex("mafcurve")
2022-08-25 09:53:01 -07:00
function onTick()
2022-12-25 11:48:39 -08:00
freqValue = getSensor("AuxSpeed1") or 0
2023-01-10 07:50:48 -08:00
-- mafValue = curve(mafCalibrationIndex, 5)
2023-01-02 11:22:23 -08:00
-- print(freqValue .. " mafValue=" .. mafValue)
2023-01-10 07:50:48 -08:00
-- mafSensor : set(mafValue)
2023-01-02 11:22:23 -08:00
if everySecondTimer : getElapsedSeconds() > 1 then
everySecondTimer : reset()
print("Total from ECU " ..totalEcuMessages)
motor5FuelCounter = motor5FuelCounter + 20
end
2022-09-05 18:48:57 -07:00
end