@@ -4,28 +4,77 @@ const uint32_t CHRYSLER_RT_INTERVAL = 250000; // 250ms between real time checks
4
4
const int CHRYSLER_MAX_RATE_UP = 3 ;
5
5
const int CHRYSLER_MAX_RATE_DOWN = 3 ;
6
6
const int CHRYSLER_MAX_TORQUE_ERROR = 80 ; // max torque cmd in excess of torque motor
7
+ const int CHRYSLER_GAS_THRSLD = 30 ; // 7% more than 2m/s
8
+ const int CHRYSLER_STANDSTILL_THRSLD = 10 ; // about 1m/s
7
9
const AddrBus CHRYSLER_TX_MSGS [] = {{571 , 0 }, {658 , 0 }, {678 , 0 }};
8
10
9
11
// TODO: do checksum and counter checks
10
12
AddrCheckStruct chrysler_rx_checks [] = {
11
- {.addr = {544 }, .bus = 0 , .expected_timestep = 10000U },
12
- {.addr = {500 }, .bus = 0 , .expected_timestep = 20000U },
13
+ {.addr = {544 }, .bus = 0 , .check_checksum = true, .max_counter = 15U , .expected_timestep = 10000U },
14
+ {.addr = {514 }, .bus = 0 , .check_checksum = false, .max_counter = 0U , .expected_timestep = 10000U },
15
+ {.addr = {500 }, .bus = 0 , .check_checksum = true, .max_counter = 15U , .expected_timestep = 20000U },
16
+ {.addr = {308 }, .bus = 0 , .check_checksum = false, .max_counter = 15U , .expected_timestep = 20000U },
17
+ {.addr = {320 }, .bus = 0 , .check_checksum = true, .max_counter = 15U , .expected_timestep = 20000U },
13
18
};
14
19
const int CHRYSLER_RX_CHECK_LEN = sizeof (chrysler_rx_checks ) / sizeof (chrysler_rx_checks [0 ]);
15
20
16
21
int chrysler_rt_torque_last = 0 ;
17
22
int chrysler_desired_torque_last = 0 ;
18
23
int chrysler_cruise_engaged_last = 0 ;
24
+ int chrysler_speed = 0 ;
19
25
uint32_t chrysler_ts_last = 0 ;
20
26
struct sample_t chrysler_torque_meas ; // last few torques measured
21
27
28
+ static uint8_t chrysler_get_checksum (CAN_FIFOMailBox_TypeDef * to_push ) {
29
+ int checksum_byte = GET_LEN (to_push ) - 1 ;
30
+ return (uint8_t )(GET_BYTE (to_push , checksum_byte ));
31
+ }
32
+
33
+ static uint8_t chrysler_compute_checksum (CAN_FIFOMailBox_TypeDef * to_push ) {
34
+ /* This function does not want the checksum byte in the input data.
35
+ jeep chrysler canbus checksum from http://illmatics.com/Remote%20Car%20Hacking.pdf */
36
+ uint8_t checksum = 0xFF ;
37
+ int len = GET_LEN (to_push );
38
+ for (int j = 0 ; j < (len - 1 ); j ++ ) {
39
+ uint8_t shift = 0x80 ;
40
+ uint8_t curr = (uint8_t )GET_BYTE (to_push , j );
41
+ for (int i = 0 ; i < 8 ; i ++ ) {
42
+ uint8_t bit_sum = curr & shift ;
43
+ uint8_t temp_chk = checksum & 0x80U ;
44
+ if (bit_sum != 0U ) {
45
+ bit_sum = 0x1C ;
46
+ if (temp_chk != 0U ) {
47
+ bit_sum = 1 ;
48
+ }
49
+ checksum = checksum << 1 ;
50
+ temp_chk = checksum | 1U ;
51
+ bit_sum ^= temp_chk ;
52
+ } else {
53
+ if (temp_chk != 0U ) {
54
+ bit_sum = 0x1D ;
55
+ }
56
+ checksum = checksum << 1 ;
57
+ bit_sum ^= checksum ;
58
+ }
59
+ checksum = bit_sum ;
60
+ shift = shift >> 1 ;
61
+ }
62
+ }
63
+ return ~checksum ;
64
+ }
65
+
66
+ static uint8_t chrysler_get_counter (CAN_FIFOMailBox_TypeDef * to_push ) {
67
+ // Well defined counter only for 8 bytes messages
68
+ return (uint8_t )(GET_BYTE (to_push , 6 ) >> 4 );
69
+ }
70
+
22
71
static int chrysler_rx_hook (CAN_FIFOMailBox_TypeDef * to_push ) {
23
72
24
73
bool valid = addr_safety_check (to_push , chrysler_rx_checks , CHRYSLER_RX_CHECK_LEN ,
25
- NULL , NULL , NULL );
74
+ chrysler_get_checksum , chrysler_compute_checksum ,
75
+ chrysler_get_counter );
26
76
27
- if (valid ) {
28
- int bus = GET_BUS (to_push );
77
+ if (valid && (GET_BUS (to_push ) == 0 )) {
29
78
int addr = GET_ADDR (to_push );
30
79
31
80
// Measured eps torque
@@ -37,7 +86,7 @@ static int chrysler_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
37
86
}
38
87
39
88
// enter controls on rising edge of ACC, exit controls on ACC off
40
- if (addr == 0x1F4 ) {
89
+ if (addr == 500 ) {
41
90
int cruise_engaged = ((GET_BYTE (to_push , 2 ) & 0x38 ) >> 3 ) == 7 ;
42
91
if (cruise_engaged && !chrysler_cruise_engaged_last ) {
43
92
controls_allowed = 1 ;
@@ -48,10 +97,33 @@ static int chrysler_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
48
97
chrysler_cruise_engaged_last = cruise_engaged ;
49
98
}
50
99
51
- // TODO: add gas pressed check
100
+ // update speed
101
+ if (addr == 514 ) {
102
+ int speed_l = (GET_BYTE (to_push , 0 ) << 4 ) + (GET_BYTE (to_push , 1 ) >> 4 );
103
+ int speed_r = (GET_BYTE (to_push , 2 ) << 4 ) + (GET_BYTE (to_push , 3 ) >> 4 );
104
+ chrysler_speed = (speed_l + speed_r ) / 2 ;
105
+ }
106
+
107
+ // exit controls on rising edge of gas press
108
+ if (addr == 308 ) {
109
+ bool gas_pressed = (GET_BYTE (to_push , 5 ) & 0x7F ) != 0 ;
110
+ if (gas_pressed && !gas_pressed_prev && (chrysler_speed > CHRYSLER_GAS_THRSLD )) {
111
+ controls_allowed = 0 ;
112
+ }
113
+ gas_pressed_prev = gas_pressed ;
114
+ }
115
+
116
+ // exit controls on rising edge of brake press
117
+ if (addr == 320 ) {
118
+ bool brake_pressed = (GET_BYTE (to_push , 0 ) & 0x7 ) == 5 ;
119
+ if (brake_pressed && (!brake_pressed_prev || (chrysler_speed > CHRYSLER_STANDSTILL_THRSLD ))) {
120
+ controls_allowed = 0 ;
121
+ }
122
+ brake_pressed_prev = brake_pressed ;
123
+ }
52
124
53
125
// check if stock camera ECU is on bus 0
54
- if ((safety_mode_cnt > RELAY_TRNS_TIMEOUT ) && (bus == 0 ) && ( addr == 0x292 )) {
126
+ if ((safety_mode_cnt > RELAY_TRNS_TIMEOUT ) && (addr == 0x292 )) {
55
127
relay_malfunction = true;
56
128
}
57
129
}
0 commit comments