@@ -16,16 +16,19 @@ const uint32_t TOYOTA_RT_INTERVAL = 250000; // 250ms between real time checks
16
16
const int TOYOTA_MAX_ACCEL = 1500 ; // 1.5 m/s2
17
17
const int TOYOTA_MIN_ACCEL = -3000 ; // 3.0 m/s2
18
18
19
- const int TOYOTA_GAS_INTERCEPTOR_THRESHOLD = 475 ; // ratio between offset and gain from dbc file
19
+ const int TOYOTA_STANDSTILL_THRSLD = 100 ; // 1kph
20
+ const int TOYOTA_GAS_INTERCEPTOR_THRSLD = 475 ; // ratio between offset and gain from dbc file
20
21
21
22
const AddrBus TOYOTA_TX_MSGS [] = {{0x283 , 0 }, {0x2E6 , 0 }, {0x2E7 , 0 }, {0x33E , 0 }, {0x344 , 0 }, {0x365 , 0 }, {0x366 , 0 }, {0x4CB , 0 }, // DSU bus 0
22
23
{0x128 , 1 }, {0x141 , 1 }, {0x160 , 1 }, {0x161 , 1 }, {0x470 , 1 }, // DSU bus 1
23
24
{0x2E4 , 0 }, {0x411 , 0 }, {0x412 , 0 }, {0x343 , 0 }, {0x1D2 , 0 }, // LKAS + ACC
24
25
{0x200 , 0 }, {0x750 , 0 }}; // interceptor + Blindspot monitor
25
26
26
27
AddrCheckStruct toyota_rx_checks [] = {
27
- {.addr = {0x260 }, .bus = 0 , .check_checksum = true, .max_counter = 0U , .expected_timestep = 20000U },
28
- {.addr = {0x1D2 }, .bus = 0 , .check_checksum = true, .max_counter = 0U , .expected_timestep = 30000U },
28
+ {.addr = { 0xaa }, .bus = 0 , .check_checksum = false, .expected_timestep = 12000U },
29
+ {.addr = {0x260 }, .bus = 0 , .check_checksum = true, .expected_timestep = 20000U },
30
+ {.addr = {0x1D2 }, .bus = 0 , .check_checksum = true, .expected_timestep = 30000U },
31
+ {.addr = {0x224 , 0x226 }, .bus = 0 , .check_checksum = false, .expected_timestep = 25000U },
29
32
};
30
33
const int TOYOTA_RX_CHECKS_LEN = sizeof (toyota_rx_checks ) / sizeof (toyota_rx_checks [0 ]);
31
34
@@ -37,7 +40,9 @@ int toyota_desired_torque_last = 0; // last desired steer torque
37
40
int toyota_rt_torque_last = 0 ; // last desired torque for real time check
38
41
uint32_t toyota_ts_last = 0 ;
39
42
int toyota_cruise_engaged_last = 0 ; // cruise state
40
- int toyota_gas_prev = 0 ;
43
+ bool toyota_gas_prev = false;
44
+ bool toyota_brake_prev = false;
45
+ bool toyota_moving = false;
41
46
struct sample_t toyota_torque_meas ; // last 3 motor torques produced by the eps
42
47
43
48
@@ -65,7 +70,7 @@ static int toyota_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
65
70
int addr = GET_ADDR (to_push );
66
71
67
72
// get eps motor torque (0.66 factor in dbc)
68
- if (addr == 0x260 ) {
73
+ if (( addr == 0x260 ) && ( bus == 0 ) ) {
69
74
int torque_meas_new = (GET_BYTE (to_push , 5 ) << 8 ) | GET_BYTE (to_push , 6 );
70
75
torque_meas_new = to_signed (torque_meas_new , 16 );
71
76
@@ -81,7 +86,7 @@ static int toyota_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
81
86
}
82
87
83
88
// enter controls on rising edge of ACC, exit controls on ACC off
84
- if (addr == 0x1D2 ) {
89
+ if (( addr == 0x1D2 ) && ( bus == 0 ) ) {
85
90
// 5th bit is CRUISE_ACTIVE
86
91
int cruise_engaged = GET_BYTE (to_push , 0 ) & 0x20 ;
87
92
if (!cruise_engaged ) {
@@ -93,21 +98,43 @@ static int toyota_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
93
98
toyota_cruise_engaged_last = cruise_engaged ;
94
99
}
95
100
101
+ // sample speed
102
+ if ((addr == 0xaa ) && (bus == 0 )) {
103
+ int speed = 0 ;
104
+ // sum 4 wheel speeds
105
+ for (int i = 0 ; i < 8 ; i += 2 ) {
106
+ int next_byte = i + 1 ; // hack to deal with misra 10.8
107
+ speed += (GET_BYTE (to_push , i ) << 8 ) + GET_BYTE (to_push , next_byte );
108
+ }
109
+ toyota_moving = (speed / 4 ) > TOYOTA_STANDSTILL_THRSLD ;
110
+ }
111
+
112
+ // exit controls on rising edge of brake pedal
113
+ // most cars have brake_pressed on 0x226, corolla and rav4 on 0x224
114
+ if (((addr == 0x224 ) || (addr == 0x226 )) && (bus == 0 )) {
115
+ int byte = (addr == 0x224 ) ? 0 : 4 ;
116
+ bool brake = ((GET_BYTE (to_push , byte ) >> 5 ) & 1 ) != 0 ;
117
+ if (brake && (!toyota_brake_prev || toyota_moving )) {
118
+ controls_allowed = 0 ;
119
+ }
120
+ toyota_brake_prev = brake ;
121
+ }
122
+
96
123
// exit controls on rising edge of interceptor gas press
97
- if (addr == 0x201 ) {
124
+ if (( addr == 0x201 ) && ( bus == 0 ) ) {
98
125
gas_interceptor_detected = 1 ;
99
126
int gas_interceptor = GET_INTERCEPTOR (to_push );
100
- if ((gas_interceptor > TOYOTA_GAS_INTERCEPTOR_THRESHOLD ) &&
101
- (gas_interceptor_prev <= TOYOTA_GAS_INTERCEPTOR_THRESHOLD )) {
127
+ if ((gas_interceptor > TOYOTA_GAS_INTERCEPTOR_THRSLD ) &&
128
+ (gas_interceptor_prev <= TOYOTA_GAS_INTERCEPTOR_THRSLD )) {
102
129
controls_allowed = 0 ;
103
130
}
104
131
gas_interceptor_prev = gas_interceptor ;
105
132
}
106
133
107
134
// exit controls on rising edge of gas press
108
- if (addr == 0x2C1 ) {
109
- int gas = GET_BYTE (to_push , 6 );
110
- if (( gas > 0 ) && ( toyota_gas_prev == 0 ) && !gas_interceptor_detected ) {
135
+ if (( addr == 0x2C1 ) && ( bus == 0 ) ) {
136
+ bool gas = GET_BYTE (to_push , 6 ) != 0 ;
137
+ if (gas && ! toyota_gas_prev && !gas_interceptor_detected ) {
111
138
controls_allowed = 0 ;
112
139
}
113
140
toyota_gas_prev = gas ;
0 commit comments