Skip to content

Commit f57a5fc

Browse files
author
rusefillc
committed
No error MIM
1 parent 2444e49 commit f57a5fc

File tree

1 file changed

+136
-2
lines changed

1 file changed

+136
-2
lines changed

B6-temp-mim.md

Lines changed: 136 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,75 @@ ECU_BUS = 1
5353
-- really 'not ECU'
5454
TCU_BUS = 2
5555

56+
fakeTorque = 0
57+
rpm = 0
58+
tps = 0
59+
60+
function xorChecksum(data, targetIndex)
61+
local index = 1
62+
local result = 0
63+
while data[index] ~= nil do
64+
if index ~= targetIndex then
65+
result = result ~ data[index]
66+
end
67+
index = index + 1
68+
end
69+
data[targetIndex] = result
70+
return result
71+
end
72+
73+
function setBitRange(data, totalBitIndex, bitWidth, value)
74+
local byteIndex = totalBitIndex >> 3
75+
local bitInByteIndex = totalBitIndex - byteIndex * 8
76+
if (bitInByteIndex + bitWidth > 8) then
77+
bitsToHandleNow = 8 - bitInByteIndex
78+
setBitRange(data, totalBitIndex + bitsToHandleNow, bitWidth - bitsToHandleNow, value >> bitsToHandleNow)
79+
bitWidth = bitsToHandleNow
80+
end
81+
mask = (1 << bitWidth) - 1
82+
data[1 + byteIndex] = data[1 + byteIndex] & (~(mask << bitInByteIndex))
83+
maskedValue = value & mask
84+
shiftedValue = maskedValue << bitInByteIndex
85+
data[1 + byteIndex] = data[1 + byteIndex] | shiftedValue
86+
end
87+
5688
function relayFromECU(bus, id, dlc, data)
5789
totalEcuMessages = totalEcuMessages + 1
5890
-- print("Relaying to TCU " .. id)
5991
txCan(TCU_BUS, id, 0, data) -- relay non-TCU message to TCU
6092
end
6193

94+
function sendMotor1()
95+
engineTorque = fakeTorque * 0.9
96+
innerTorqWithoutExt = fakeTorque
97+
torqueLoss = 20
98+
requestedTorque = fakeTorque
99+
100+
motor1Data[2] = engineTorque / 0.39
101+
setTwoBytes(motor1Data, 2, rpm / 0.25)
102+
motor1Data[5] = innerTorqWithoutExt / 0.4
103+
motor1Data[6] = tps / 0.4
104+
motor1Data[7] = torqueLoss / 0.39
105+
motor1Data[8] = requestedTorque / 0.39
106+
107+
txCan(TCU_BUS, MOTOR_1, 0, motor1Data)
108+
end
109+
110+
function onMotor1(bus, id, dlc, data)
111+
totalEcuMessages = totalEcuMessages + 1
112+
rpm = getBitRange(data, 16, 16) * 0.25
113+
if rpm == 0 then
114+
canMotorInfoTotalCounter = 0
115+
end
116+
117+
tps = getBitRange(data, 40, 8) * 0.4
118+
119+
fakeTorque = interpolate(0, 6, 100, 60, tps)
120+
121+
-- sendMotor1()
122+
relayFromECU(bus, id, dlc, data)
123+
end
124+
62125
function relayFromECUAndEcho(bus, id, dlc, data)
63126
totalEcuMessages = totalEcuMessages + 1
64127
print("Relaying to TCU " .. id)
@@ -138,10 +201,70 @@ end
138201
function drop(bus, id, dlc, data)
139202
end
140203

204+
motorBreCounter = 0
205+
function onMotorBre(bus, id, dlc, data)
206+
motorBreCounter = (motorBreCounter + 1) % 16
207+
208+
setBitRange(motorBreData, 8, 4, motorBreCounter)
209+
xorChecksum(motorBreData, 1)
210+
211+
txCan(TCU_BUS, MOTOR_BRE, 0, motorBreData)
212+
end
213+
214+
215+
motor5FuelCounter = 0
216+
function onMotor5(bus, id, dlc, data)
217+
setBitRange(motor5Data, 5, 9, motor5FuelCounter)
218+
xorChecksum(motor5Data, 8)
219+
txCan(TCU_BUS, MOTOR_5, 0, motor5Data)
220+
end
221+
222+
counter16 = 0
223+
function onMotor6(bus, id, dlc, data)
224+
counter16 = (counter16 + 1) % 16
225+
226+
-- engineTorque = getBitRange(data, 8, 8) * 0.39
227+
-- actualTorque = getBitRange(data, 16, 8) * 0.39
228+
-- feedbackGearbox = getBitRange(data, 40, 8) * 0.39
229+
engineTorque = fakeTorque * 0.9
230+
actualTorque = fakeTorque
231+
feedbackGearbox = 255
232+
233+
motor6Data[2] = math.floor(engineTorque / 0.39)
234+
motor6Data[3] = math.floor(actualTorque / 0.39)
235+
motor6Data[6] = math.floor(feedbackGearbox / 0.39)
236+
setBitRange(motor6Data, 60, 4, counter16)
237+
238+
xorChecksum(motor6Data, 1)
239+
txCan(TCU_BUS, MOTOR_6, 0, motor6Data)
240+
end
241+
141242
function onMotor7(bus, id, dlc, data)
142243
txCan(TCU_BUS, MOTOR_7, 0, motor7Data)
143244
end
144245

246+
247+
canMotorInfoCounter = 0
248+
function onMotorInfo(bus, id, dlc, data)
249+
canMotorInfoTotalCounter = canMotorInfoTotalCounter + 1
250+
canMotorInfoCounter = (canMotorInfoCounter + 1) % 16
251+
-- canMotorInfoCounter = getBitRange(data, 0, 4)
252+
253+
baseByte = canMotorInfoTotalCounter < 6 and 0x80 or 0x90
254+
canMotorInfo[1] = baseByte + (canMotorInfoCounter)
255+
canMotorInfo1[1] = baseByte + (canMotorInfoCounter)
256+
canMotorInfo3[1] = baseByte + (canMotorInfoCounter)
257+
mod4 = canMotorInfoCounter % 4
258+
259+
if (mod4 == 0 or mod4 == 2) then
260+
txCan(TCU_BUS, MOTOR_INFO, 0, canMotorInfo)
261+
elseif (mod4 == 1) then
262+
txCan(TCU_BUS, MOTOR_INFO, 0, canMotorInfo1)
263+
else
264+
txCan(TCU_BUS, MOTOR_INFO, 0, canMotorInfo3)
265+
end
266+
end
267+
145268
hexstr = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "A", "B", "C", "D", "E", "F" }
146269

147270
function toHexString(num)
@@ -170,7 +293,7 @@ end
170293

171294
totalEcuMessages = 0
172295

173-
--canRxAdd(ECU_BUS, MOTOR_7, drop)
296+
canRxAdd(ECU_BUS, MOTOR_7, drop)
174297
--canRxAdd(ECU_BUS, ACC_GRA, drop)
175298

176299
-- kombi 3
@@ -228,7 +351,7 @@ canRxAdd(ECU_BUS, MOTOR_2, relayFromECU)
228351
canRxAdd(ECU_BUS, MOTOR_3, relayFromECU)
229352
canRxAdd(ECU_BUS, MOTOR_5, relayFromECU)
230353
canRxAdd(ECU_BUS, MOTOR_6, relayFromECU)
231-
canRxAdd(ECU_BUS, MOTOR_7, relayFromECU)
354+
--canRxAdd(ECU_BUS, MOTOR_7, relayFromECU)
232355
canRxAdd(ECU_BUS, ACC_GRA, relayFromECU)
233356
canRxAdd(ECU_BUS, MOTOR_INFO, relayFromECU)
234357

@@ -242,7 +365,18 @@ canRxAddMask(ECU_BUS, 0, 0, drop)
242365
--canRxAddMask(ECU_BUS, 0, 0, relayFromECU)
243366
canRxAddMask(TCU_BUS, 0, 0, relayFromTCU)
244367

368+
everySecondTimer = Timer.new()
369+
245370
function onTick()
246371
onMotor7(0, 0, 0, nil)
247372

373+
if everySecondTimer : getElapsedSeconds() > 1 then
374+
everySecondTimer : reset()
375+
print("Total from ECU " ..totalEcuMessages)
376+
motor5FuelCounter = motor5FuelCounter + 20
377+
378+
--onMotorInfo(0, 0, 0, nil)
379+
end
380+
381+
248382
end

0 commit comments

Comments
 (0)