rusefi_documentation/B6-temp.md

239 lines
6.0 KiB
Markdown
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
MOTOR_3 = 0x380
MOTOR_INFO = 0x580
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-25 21:14:59 -08:00
-- 1416
2022-08-26 19:25:22 -07:00
MOTOR_7 = 0x588
2022-12-25 11:48:39 -08:00
fuelCounter = 0
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)
local index = 1
local result = 0
while data[index] ~= nil do
if index ~= targetIndex then
result = result ~ data[index]
end
index = index + 1
2022-08-30 01:14:35 -07:00
end
2022-09-04 08:32:43 -07:00
data[targetIndex] = result
return result
end
2022-08-30 01:14:35 -07:00
2022-09-17 19:26:47 -07:00
function setBitRange(data, totalBitIndex, bitWidth, value)
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)
2022-09-06 14:36:26 -07:00
bitWidth = bitsToHandleNow
2022-09-17 19:26:47 -07:00
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
end
2022-09-06 14:36:26 -07:00
2022-08-25 09:53:01 -07:00
function getBitRange(data, bitIndex, bitWidth)
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)
2022-09-17 19:26:47 -07:00
value = math.floor(value)
data[offset + 2] = value >> 8
data[offset + 1] = value & 0xff
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)
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
totalEcuMessages = 0
totalTcuMessages = 0
2022-09-17 19:26:47 -07:00
motor1Data = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
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-09-17 19:26:47 -07:00
motor7Data = { 0x1A, 0x66, 0x7E, 0x00, 0x00, 0x00, 0x00, 0x00 }
2022-08-26 18:29:50 -07:00
2022-08-25 09:53:01 -07:00
function onMotor1(bus, id, dlc, data)
2022-09-17 19:26:47 -07:00
rpm = getBitRange(data, 16, 16) * 0.25
tps = getBitRange(data, 40, 8) * 0.4
fakeTorque = interpolate(0, 6, 100, 60, tps)
-- 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-12-25 21:14:59 -08:00
engineTorque = fakeTorque * 0.9
2022-09-17 19:26:47 -07:00
innerTorqWithoutExt = fakeTorque
2022-12-25 21:14:59 -08:00
torqueLoss = 20
2022-09-17 19:26:47 -07:00
requestedTorque = fakeTorque
motor1Data[2] = engineTorque / 0.39
2022-08-26 18:29:50 -07:00
setTwoBytes(motor1Data, 2, rpm / 0.25)
motor1Data[5] = innerTorqWithoutExt / 0.4
2022-09-17 19:26:47 -07:00
motor1Data[6] = tps / 0.4
2022-08-26 18:29:50 -07:00
motor1Data[7] = torqueLoss / 0.39
motor1Data[8] = requestedTorque / 0.39
2022-09-17 19:26:47 -07:00
print ('fakeTorque ' ..fakeTorque)
print ('engineTorque ' ..engineTorque ..' RPM ' ..rpm)
print ('innerTorqWithoutExt ' ..innerTorqWithoutExt ..' tps ' ..tps)
print ('torqueLoss ' ..torqueLoss ..' requestedTorque ' ..requestedTorque)
txCan(TCU_BUS, id, 0, motor1Data)
2022-08-25 09:53:01 -07:00
end
2022-09-05 18:48:57 -07:00
function onMotor3(bus, id, dlc, data)
iat = getBitRange(data, 8, 8) * 0.75 - 48
pps = getBitRange(data, 16, 8) * 0.40
tps = getBitRange(data, 56, 8) * 0.40
2022-09-17 19:26:47 -07:00
print ('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
txCan(TCU_BUS, id, 0, canMotor3)
2022-09-05 18:48:57 -07:00
end
2022-08-25 09:53:01 -07:00
2022-09-17 20:48:14 -07:00
function onMotor5(bus, id, dlc, data)
2022-09-17 21:34:57 -07:00
setBitRange(motor5Data, 5, 9, fuelCounter)
xorChecksum(motor5Data, 8)
2022-09-17 20:48:14 -07:00
txCan(TCU_BUS, id, 0, motor5Data)
end
2022-12-25 21:14:59 -08:00
function onMotor6(bus, id, dlc, data)
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
2022-12-26 09:18:53 -08:00
motor6Data[2] = math.floor(engineTorque / 0.39)
2022-12-25 21:14:59 -08:00
motor6Data[3] = math.floor(actualTorque / 0.39)
motor6Data[6] = math.floor(feedbackGearbox / 0.39)
xorChecksum(motor6Data, 1)
txCan(TCU_BUS, id, 0, motor6Data)
end
function silentDrop(bus, id, dlc, data)
end
2022-08-25 09:53:01 -07:00
function printAndDrop(bus, id, dlc, data)
2022-09-17 19:26:47 -07:00
print('Dropping ' ..arrayToString(data))
2022-08-25 09:53:01 -07:00
end
function onAnythingFromECU(bus, id, dlc, data)
2022-09-17 19:26:47 -07:00
totalEcuMessages = totalEcuMessages + 1
2022-12-27 05:31:24 -08:00
-- txCan(TCU_BUS, id, 0, data) -- relay non-TCU message to TCU
2022-08-25 09:53:01 -07:00
end
function onAnythingFromTCU(bus, id, dlc, data)
2022-09-17 19:26:47 -07:00
totalTcuMessages = totalTcuMessages + 1
2022-12-27 05:31:24 -08:00
-- txCan(ECU_BUS, id, 0, data) -- relay non-ECU message to ECU
2022-08-25 09:53:01 -07:00
end
-- VAG Motor_1 just as example
2022-09-17 19:26:47 -07:00
canRxAdd(ECU_BUS, MOTOR_1, onMotor1)
canRxAdd(ECU_BUS, MOTOR_3, onMotor3)
2022-09-17 21:34:57 -07:00
canRxAdd(ECU_BUS, MOTOR_5, onMotor5)
2022-09-17 19:26:47 -07:00
canRxAdd(ECU_BUS, MOTOR_INFO, printAndDrop)
2022-12-25 21:14:59 -08:00
canRxAdd(ECU_BUS, MOTOR_6, onMotor6)
2022-09-17 19:26:47 -07:00
canRxAdd(ECU_BUS, MOTOR_7, printAndDrop)
2022-08-25 09:53:01 -07:00
-- last option: unconditional forward of all remaining messages
canRxAddMask(ECU_BUS, 0, 0, onAnythingFromECU)
canRxAddMask(TCU_BUS, 0, 0, onAnythingFromTCU)
everySecondTimer = Timer.new()
2022-08-28 10:40:57 -07:00
canMotorInfoCounter = 0
2022-08-25 09:53:01 -07: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
2022-12-25 11:00:12 -08:00
mafValue = curve(mafCalibrationIndex, 5)
-- print(freqValue .. " mafValue=" .. mafValue)
mafSensor : set(mafValue)
2022-09-05 18:48:57 -07:00
if everySecondTimer : getElapsedSeconds() > 1 then
everySecondTimer : reset()
print("Total from ECU " ..totalEcuMessages .." from TCU " ..totalTcuMessages)
2022-12-25 11:48:39 -08:00
2022-09-17 20:48:14 -07:00
fuelCounter = fuelCounter + 20
2022-08-28 09:52:45 -07:00
2022-08-25 09:53:01 -07:00
2022-09-05 18:48:57 -07:00
canMotorInfoCounter = (canMotorInfoCounter + 1) % 8
motor7Data[1] = 0x90 + (canMotorInfoCounter * 2)
txCan(1, MOTOR_INFO, 0, motor7Data)
2022-08-25 09:53:01 -07:00
2022-09-05 18:48:57 -07:00
end
end
2022-08-25 09:53:01 -07:00
2022-09-17 19:26:47 -07:00
2022-08-25 09:53:01 -07:00
```