@@ -53,12 +53,75 @@ ECU_BUS = 1
53
53
-- really 'not ECU'
54
54
TCU_BUS = 2
55
55
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
+
56
88
function relayFromECU(bus, id, dlc, data)
57
89
totalEcuMessages = totalEcuMessages + 1
58
90
-- print("Relaying to TCU " .. id)
59
91
txCan(TCU_BUS, id, 0, data) -- relay non-TCU message to TCU
60
92
end
61
93
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
+
62
125
function relayFromECUAndEcho(bus, id, dlc, data)
63
126
totalEcuMessages = totalEcuMessages + 1
64
127
print("Relaying to TCU " .. id)
@@ -138,10 +201,70 @@ end
138
201
function drop(bus, id, dlc, data)
139
202
end
140
203
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
+
141
242
function onMotor7(bus, id, dlc, data)
142
243
txCan(TCU_BUS, MOTOR_7, 0, motor7Data)
143
244
end
144
245
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
+
145
268
hexstr = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "A", "B", "C", "D", "E", "F" }
146
269
147
270
function toHexString(num)
170
293
171
294
totalEcuMessages = 0
172
295
173
- -- canRxAdd(ECU_BUS, MOTOR_7, drop)
296
+ canRxAdd(ECU_BUS, MOTOR_7, drop)
174
297
--canRxAdd(ECU_BUS, ACC_GRA, drop)
175
298
176
299
-- kombi 3
@@ -228,7 +351,7 @@ canRxAdd(ECU_BUS, MOTOR_2, relayFromECU)
228
351
canRxAdd(ECU_BUS, MOTOR_3, relayFromECU)
229
352
canRxAdd(ECU_BUS, MOTOR_5, relayFromECU)
230
353
canRxAdd(ECU_BUS, MOTOR_6, relayFromECU)
231
- canRxAdd(ECU_BUS, MOTOR_7, relayFromECU)
354
+ -- canRxAdd(ECU_BUS, MOTOR_7, relayFromECU)
232
355
canRxAdd(ECU_BUS, ACC_GRA, relayFromECU)
233
356
canRxAdd(ECU_BUS, MOTOR_INFO, relayFromECU)
234
357
@@ -242,7 +365,18 @@ canRxAddMask(ECU_BUS, 0, 0, drop)
242
365
--canRxAddMask(ECU_BUS, 0, 0, relayFromECU)
243
366
canRxAddMask(TCU_BUS, 0, 0, relayFromTCU)
244
367
368
+ everySecondTimer = Timer.new()
369
+
245
370
function onTick()
246
371
onMotor7(0, 0, 0, nil)
247
372
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
+
248
382
end
0 commit comments