rusefi/firmware/config/engines/vw_b6.lua

487 lines
11 KiB
Lua
Raw Normal View History

2022-12-04 09:28:49 -08:00
strncpy(config->luaScript, R"(
AIRBAG = 0x050
2023-10-07 22:53:39 -07:00
TCU_1088_440 = 0x440
2023-10-08 14:38:59 -07:00
TCU_1344_540 = 0x540
2022-12-26 20:59:55 -08:00
-- 1440
2022-12-04 09:28:49 -08:00
BRAKE_2 = 0x5A0
2023-01-10 23:00:34 -08:00
VWTP_OUT = 0x200
VWTP_IN = 0x202
VWTP_TESTER = 0x300
2022-12-04 09:28:49 -08:00
2022-12-27 06:57:13 -08:00
-- 640
2022-12-04 09:28:49 -08:00
MOTOR_1 = 0x280
2022-12-28 22:35:46 -08:00
-- 644
MOTOR_BRE = 0x284
-- 648
MOTOR_2 = 0x288
2023-03-05 20:47:52 -08:00
-- 800
Kombi_1 = 0x320
2022-12-27 06:57:13 -08:00
-- 896
2022-12-04 09:28:49 -08:00
MOTOR_3 = 0x380
2022-12-28 22:35:46 -08:00
-- 1152
2022-12-04 09:28:49 -08:00
MOTOR_5 = 0x480
2022-12-27 06:57:13 -08:00
-- 1160
2022-12-04 09:28:49 -08:00
MOTOR_6 = 0x488
2022-12-29 15:13:40 -08:00
-- 1386
ACC_GRA = 0x56A
2022-12-28 22:35:46 -08:00
-- 1408 the one with variable payload
MOTOR_INFO = 0x580
-- 1416
2022-12-04 09:28:49 -08:00
MOTOR_7 = 0x588
2023-01-09 18:35:08 -08:00
TCU_BUS = 1
2023-01-10 08:45:45 -08:00
fakeTorque = 0
2022-12-27 06:57:13 -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
2022-12-26 18:21:21 -08:00
function onTcu2(bus, id, dlc, data)
2023-01-09 21:55:36 -08:00
-- print("onTcu2")
2022-12-26 18:21:21 -08:00
end
2022-12-04 09:28:49 -08: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)
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
end
function setTwoBytes(data, offset, value)
data[offset + 1] = value % 255
data[offset + 2] = (value >> 8) % 255
end
shallSleep = Timer.new()
-- we want to turn on with hardware switch while ignition key is off
hadIgnitionEvent = false
2023-01-10 21:08:50 -08:00
function onAirBag(bus, id, dlc, data)
-- looks like we have ignition key do not sleep!
shallSleep : reset()
hadIgnitionEvent = true
2022-12-04 09:28:49 -08:00
end
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
end
data[targetIndex] = result
return result
end
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
end
2023-10-07 22:53:39 -07:00
counter440 = 0
function onTcu1(bus, id, dlc, data)
-- print("onTcu1")
isShiftActive = getBitRange(data, 0, 1)
tcuStatus = getBitRange(data, 1, 1)
EGSRequirement = getBitRange(data, 7, 1)
counter440 = counter440 + 1
if counter440 % 1 == 0 then
print("TCU " .. isShiftActive .. " " .. tcuStatus .. " " .. EGSRequirement)
end
end
2023-01-10 08:45:45 -08:00
motor1Data = { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
2022-12-29 14:48:37 -08:00
motorBreData = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
motor2Data = { 0x8A, 0x8D, 0x10, 0x04, 0x00, 0x4C, 0xDC, 0x87 }
2023-01-09 18:35:08 -08:00
motor2mux = {0x8A, 0xE8, 0x2C, 0x64}
2022-12-04 09:28:49 -08:00
canMotorInfo = { 0x00, 0x00, 0x00, 0x14, 0x1C, 0x93, 0x48, 0x14 }
2023-01-09 18:35:08 -08:00
canMotorInfo1= { 0x99, 0x14, 0x00, 0x7F, 0x00, 0xF0, 0x47, 0x01 }
canMotorInfo3= { 0x9B, 0x14, 0x00, 0x11, 0x1F, 0xE0, 0x0C, 0x46 }
2022-12-04 09:28:49 -08:00
canMotor3 = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
2023-10-09 10:14:12 -07:00
motor5mux = {0x1c, 0x54, 0x84, 0xc2}
2022-12-04 09:28:49 -08:00
motor5Data = { 0x1C, 0x08, 0xF3, 0x55, 0x19, 0x00, 0x00, 0xAD }
2022-12-25 23:23:37 -08:00
motor6Data = { 0x00, 0x00, 0x00, 0x7E, 0xFE, 0xFF, 0xFF, 0x00 }
2022-12-29 15:13:40 -08:00
motor7Data = { 0x1A, 0x66, 0x7E, 0x00, 0x00, 0x00, 0x00, 0x00 }
2023-01-10 22:37:31 -08:00
accGraData = { 0x00, 0x00, 0x08, 0x00, 0x1A, 0x00, 0x02, 0x01 }
2022-12-04 09:28:49 -08:00
setTickRate(100)
everySecondTimer = Timer.new()
2022-12-25 11:01:39 -08:00
mafSensor = Sensor.new("maf")
mafCalibrationIndex = findCurveIndex("mafcurve")
2023-01-09 18:35:08 -08:00
canMotorInfoTotalCounter = 0
2023-01-10 22:37:31 -08:00
canRxAdd(AIRBAG, onAirBag)
2023-10-07 22:53:39 -07:00
canRxAdd(TCU_1088_440, onTcu1)
2023-10-08 14:38:59 -07:00
canRxAdd(TCU_1344_540, onTcu2)
2023-03-05 14:54:18 -08:00
--canRxAdd(BRAKE_2)
2023-01-10 08:45:45 -08:00
2023-03-05 17:12:58 -08:00
rpm = 0
2023-03-05 15:56:26 -08:00
function sendMotor1()
2023-01-10 08:45:45 -08:00
engineTorque = fakeTorque * 0.9
innerTorqWithoutExt = fakeTorque
torqueLoss = 20
requestedTorque = fakeTorque
2023-03-05 15:56:26 -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
2023-01-10 08:45:45 -08:00
-- print ('MOTOR_1 fakeTorque ' ..fakeTorque)
-- print ('MOTOR_1 engineTorque ' ..engineTorque ..' RPM ' ..rpm)
-- print ('MOTOR_1 innerTorqWithoutExt ' ..innerTorqWithoutExt ..' tps ' ..tps)
-- print ('MOTOR_1 torqueLoss ' ..torqueLoss ..' requestedTorque ' ..requestedTorque)
2023-03-05 15:56:26 -08:00
txCan(TCU_BUS, MOTOR_1, 0, motor1Data)
end
function onMotor1(bus, id, dlc, data)
2023-03-05 17:12:58 -08:00
rpm = math.floor(getSensor("RPM") + 0.5)
2023-03-05 15:56:26 -08:00
tps = getSensor("TPS1") or 0
fakeTorque = interpolate(0, 6, 100, 60, tps)
2023-10-07 22:11:44 -07:00
sendMotor1()
2023-01-10 08:45:45 -08:00
end
2023-01-09 21:55:36 -08:00
2023-03-05 14:54:18 -08:00
function sendMotor3()
2023-01-10 18:27:35 -08:00
iat = getSensor("IAT") or 0
tps = getSensor("TPS1") or 0
2023-10-07 22:11:44 -07:00
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, MOTOR_3, 0, canMotor3)
2023-01-10 18:27:35 -08:00
end
2023-01-10 08:03:20 -08:00
motorBreCounter = 0
2023-03-05 14:54:18 -08:00
function sendMotorBre()
2023-10-07 22:11:44 -07:00
motorBreCounter = (motorBreCounter + 1) % 16
2023-01-10 08:03:20 -08:00
2023-10-07 22:11:44 -07:00
setBitRange(motorBreData, 8, 4, motorBreCounter)
xorChecksum(motorBreData, 1)
2023-01-10 08:03:20 -08:00
2023-10-07 22:11:44 -07:00
txCan(TCU_BUS, MOTOR_BRE, 0, motorBreData)
2023-01-10 08:03:20 -08:00
end
motor2counter = 0
2023-03-05 14:54:18 -08:00
function sendMotor2()
2023-10-07 22:11:44 -07:00
motor2counter = (motor2counter + 1) % 16
2023-01-10 08:03:20 -08:00
2023-10-07 22:11:44 -07:00
minTorque = fakeTorque / 2
-- todo: add CLT
motor2Data[7] = math.floor(minTorque / 0.39)
2023-01-10 08:03:20 -08:00
--print ( "brake " .. getBitRange(data, 16, 2) .. " " .. rpm)
2023-10-07 22:11:44 -07:00
brakeBit = rpm < 2000 and 1 or 0
setBitRange(motor2Data, 16, 1, brakeBit)
2023-01-10 08:03:20 -08:00
2023-10-07 22:11:44 -07:00
index = math.floor(motor2counter / 4)
motor2Data[1] = motor2mux[1 + index]
2023-01-10 08:03:20 -08:00
2023-10-07 22:11:44 -07:00
txCan(TCU_BUS, MOTOR_2, 0, motor2Data)
2023-01-10 08:03:20 -08:00
end
2023-10-09 10:14:12 -07:00
motor5counter = 0
2023-01-10 08:03:20 -08:00
motor5FuelCounter = 0
2023-03-05 16:39:28 -08:00
function sendMotor5()
2023-10-09 10:14:12 -07:00
motor5counter = (motor5counter + 1) % 16
index = math.floor(motor5counter / 4)
motor5Data[1] = motor5mux[1 + index]
-- setBitRange(motor5Data, 5, 9, motor5FuelCounter)
2023-03-05 14:54:18 -08:00
xorChecksum(motor5Data, 8)
txCan(TCU_BUS, MOTOR_5, 0, motor5Data)
2023-01-10 08:03:20 -08:00
end
2023-03-05 14:54:18 -08:00
motor6counter = 0
function sendMotor6()
2023-10-07 22:11:44 -07:00
motor6counter = (motor6counter + 1) % 16
2023-01-10 08:03:20 -08:00
2023-10-07 22:11:44 -07:00
engineTorque = fakeTorque * 0.9
actualTorque = fakeTorque
feedbackGearbox = 255
2023-01-10 08:03:20 -08:00
2023-10-07 22:11:44 -07:00
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, motor6counter)
2023-01-10 08:03:20 -08:00
2023-10-07 22:11:44 -07:00
xorChecksum(motor6Data, 1)
txCan(TCU_BUS, MOTOR_6, 0, motor6Data)
2023-01-10 08:03:20 -08:00
end
2023-01-10 22:37:31 -08:00
accGraCounter = 0
2023-03-05 14:54:18 -08:00
function sendAccGra()
2023-10-07 22:11:44 -07:00
accGraCounter = (accGraCounter + 1) % 16
setBitRange(accGraData, 60, 4, accGraCounter)
xorChecksum(accGraData, 1)
2023-01-10 22:37:31 -08:00
2023-10-07 22:11:44 -07:00
txCan(TCU_BUS, ACC_GRA, 0, accGraData)
2023-01-10 22:37:31 -08:00
end
2023-01-10 08:03:20 -08:00
canMotorInfoCounter = 0
2023-10-08 12:08:20 -07:00
function sendMotorInfo()
2023-10-07 22:11:44 -07:00
canMotorInfoTotalCounter = canMotorInfoTotalCounter + 1
canMotorInfoCounter = (canMotorInfoCounter + 1) % 16
baseByte = canMotorInfoTotalCounter < 6 and 0x80 or 0x90
canMotorInfo[1] = baseByte + (canMotorInfoCounter)
canMotorInfo1[1] = baseByte + (canMotorInfoCounter)
canMotorInfo3[1] = baseByte + (canMotorInfoCounter)
mod4 = canMotorInfoCounter % 4
if (mod4 == 0 or mod4 == 2) then
txCan(TCU_BUS, MOTOR_INFO, 0, canMotorInfo)
elseif (mod4 == 1) then
txCan(TCU_BUS, MOTOR_INFO, 0, canMotorInfo1)
else
txCan(TCU_BUS, MOTOR_INFO, 0, canMotorInfo3)
end
2023-01-10 08:03:20 -08:00
end
2023-03-05 14:54:18 -08:00
function sendMotor7()
2023-10-07 22:11:44 -07:00
txCan(TCU_BUS, MOTOR_7, 0, motor7Data)
2023-01-10 08:03:20 -08:00
end
2023-01-10 23:00:34 -08:00
local tcuId = 0
function onCanHello(bus, id, dlc, data)
-- here we handle 201 packets
print('Got Hello Response ' ..arrayToString(data))
tcuId = data[6] * 256 + data[5]
print('From TCU ' ..tcuId)
txCan(1, tcuId, 0, { 0xA0, 0x0F, 0x8A, 0xFF, 0x32, 0xFF })
end
2023-01-11 09:36:11 -08:00
local sendCounter = 2
local packetCounter = 1
local payLoadIndex = 0
2023-01-10 23:00:34 -08:00
local groups = { 10 }
-- todo: smarter array size calculation?
local groupsSize = 1
local groupIndex = 1
2023-03-05 20:47:52 -08:00
vssSensor = Sensor.new("VehicleSpeed")
2023-03-06 16:18:38 -08:00
vssSensor : setTimeout(2000)
2023-03-05 20:47:52 -08:00
function onKombi(bus, id, dlc, data)
2023-10-07 22:11:44 -07:00
speed = getBitRange(data, 46, 10) * 0.32
vssSensor : set(speed)
2023-03-05 20:47:52 -08:00
end
canRxAdd(Kombi_1, onKombi)
2023-10-08 09:16:44 -07:00
-- unused method did we mean to reset codes? todo: probably remove soon
2023-01-10 23:00:34 -08:00
function onCanTester(bus, id, dlc, data)
-- here we handle 300 packets
-- print('Got from tester ' ..arrayToString(data))
if data[1] == 0xA3 then
-- print ("Keep-alive")
txCan(1, tcuId, 0, { 0xA1, 0x0F, 0x8A, 0xFF, 0x4A, 0xFF })
groupIndex = groupIndex + 1
if groupIndex > groupsSize then
groupIndex = 1
end
groupId = groups[groupIndex]
print (groupIndex .." " ..groupId)
reqFirst = 0x10 + sendCounter
print("Requesting next group " ..groupId .." with counter " ..sendCounter)
txCan(1, tcuId, 0, { reqFirst, 0x00, 0x02, 0x21, groupId })
sendCounter = sendCounter + 1
if sendCounter == 16 then
sendCounter = 0
end
return
end
if data[1] == 0xA1 then
print ("Happy 300 packet")
txCan(1, tcuId, 0, { 0x10, 0x00, 0x02, 0x10, 0x89 })
return
end
if data[1] == 0xA8 then
print ("They said Bye-Bye")
return
end
if data[1] == 0x10 and dlc == 5 then
ackPacket = 0xB0 + packetCounter
print ("Sending ACK B1 " ..ackPacket)
txCan(1, tcuId, 0, { ackPacket })
-- request first group from array
txCan(1, tcuId, 0, { 0x11, 0x00, 0x02, 0x21, groups[1] })
return
end
top4 = math.floor(data[1] / 16)
if top4 == 0xB then
-- print("Got ACK")
return
end
if top4 == 2 or top4 == 1 then
print ("Looks like payload index " ..payLoadIndex ..": " ..arrayToString(data))
if groupId == 2 and payLoadIndex == 0 then
L7 = data[7]
H9 = data[8]
V = 256 * H9 + L7
print("V 0 " ..V)
end
if groupId == 2 and payLoadIndex == 1 then
L3 = data[3]
H4 = data[4]
V = 256 * H4 + L3
print("V 1 " ..V)
end
if groupId == 2 and payLoadIndex == 2 then
L2 = data[2]
H3 = data[3]
V = 256 * H3 + L2
print("V 2 " ..V)
end
payLoadIndex = payLoadIndex + 1
packetCounter = packetCounter + 1
if packetCounter > 15 then
packetCounter = 0
end
if top4 == 1 then
ackPacket = 0xB0 + packetCounter
print ("Sending payload ACK " ..ackPacket)
txCan(1, tcuId, 0, { ackPacket })
payLoadIndex = 0
end
return
end
print('Got unexpected ' ..arrayToString(data))
end
canRxAdd(VWTP_IN, onCanHello)
2023-03-05 20:47:52 -08:00
--txCan(1, VWTP_OUT, 0, { 0x02, 0xC0, 0x00, 0x10, 0x00, 0x03, 0x01 })
2023-01-10 08:03:20 -08:00
2022-12-04 09:28:49 -08:00
function onTick()
2022-12-25 11:01:39 -08:00
2022-12-29 15:13:40 -08:00
freqValue = getSensor("AuxSpeed1") or 0
2022-12-25 11:01:39 -08:00
mafValue = curve(mafCalibrationIndex, 5)
2022-12-29 15:13:40 -08:00
-- print(freqValue .. " mafValue=" .. mafValue)
2022-12-25 11:01:39 -08:00
mafSensor : set(mafValue)
2022-12-04 09:28:49 -08:00
rpm = getSensor("RPM") or 0
vbat = getSensor("BatteryVoltage") or 0
2023-10-07 22:09:16 -07:00
if rpm == 0 then
canMotorInfoTotalCounter = 0
end
2023-01-09 18:35:08 -08:00
2023-10-07 22:09:16 -07:00
onMotor1(0, 0, 0, nil)
sendMotor3()
2023-03-05 14:54:18 -08:00
2023-10-07 22:09:16 -07:00
sendMotor2()
sendMotor5()
sendMotor6()
sendMotor7()
sendMotorBre()
sendAccGra()
2022-12-04 09:28:49 -08:00
2022-12-29 15:13:40 -08:00
local timeToTurnOff = shallSleep : getElapsedSeconds() > 2
local connectedToUsb = vbat < 4
2022-12-04 09:28:49 -08:00
if hadIgnitionEvent and timeToTurnOff then
-- looks like ignition key was removed
2023-10-07 22:09:16 -07:00
-- mcu_standby()
2022-12-04 09:28:49 -08:00
end
if everySecondTimer : getElapsedSeconds() > 1 then
everySecondTimer : reset()
2023-10-09 10:14:12 -07:00
print("CAN OK " .. getOutput("canWriteOk") .. " not OK " .. getOutput("canWriteNotOk"))
if rpm > 0 then
motor5FuelCounter = motor5FuelCounter + 20
end
2022-12-04 09:28:49 -08:00
2023-10-08 12:08:20 -07:00
sendMotorInfo()
2023-01-09 18:35:08 -08:00
2022-12-04 09:28:49 -08:00
end
end
)", efi::size(config->luaScript));