Skip to content

Commit 0f21b19

Browse files
authored
Cleanup pedal nomenclature (#467)
* consolidate gas and brake nomenclature * fixes in code and tests
1 parent ceff91d commit 0f21b19

17 files changed

+78
-128
lines changed

board/safety/safety_chrysler.h

+6-8
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ const int CHRYSLER_RX_CHECK_LEN = sizeof(chrysler_rx_checks) / sizeof(chrysler_r
2121
int chrysler_rt_torque_last = 0;
2222
int chrysler_desired_torque_last = 0;
2323
int chrysler_cruise_engaged_last = 0;
24-
bool chrysler_gas_prev = false;
25-
bool chrysler_brake_prev = false;
2624
int chrysler_speed = 0;
2725
uint32_t chrysler_ts_last = 0;
2826
struct sample_t chrysler_torque_meas; // last few torques measured
@@ -108,20 +106,20 @@ static int chrysler_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
108106

109107
// exit controls on rising edge of gas press
110108
if (addr == 308) {
111-
bool gas = (GET_BYTE(to_push, 5) & 0x7F) != 0;
112-
if (gas && !chrysler_gas_prev && (chrysler_speed > CHRYSLER_GAS_THRSLD)) {
109+
bool gas_pressed = (GET_BYTE(to_push, 5) & 0x7F) != 0;
110+
if (gas_pressed && !gas_pressed_prev && (chrysler_speed > CHRYSLER_GAS_THRSLD)) {
113111
controls_allowed = 0;
114112
}
115-
chrysler_gas_prev = gas;
113+
gas_pressed_prev = gas_pressed;
116114
}
117115

118116
// exit controls on rising edge of brake press
119117
if (addr == 320) {
120-
bool brake = (GET_BYTE(to_push, 0) & 0x7) == 5;
121-
if (brake && (!chrysler_brake_prev || (chrysler_speed > CHRYSLER_STANDSTILL_THRSLD))) {
118+
bool brake_pressed = (GET_BYTE(to_push, 0) & 0x7) == 5;
119+
if (brake_pressed && (!brake_pressed_prev || (chrysler_speed > CHRYSLER_STANDSTILL_THRSLD))) {
122120
controls_allowed = 0;
123121
}
124-
chrysler_brake_prev = brake;
122+
brake_pressed_prev = brake_pressed;
125123
}
126124

127125
// check if stock camera ECU is on bus 0

board/safety/safety_ford.h

+7-9
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
// brake rising edge
88
// brake > 0mph
99

10-
int ford_brake_prev = 0;
11-
int ford_gas_prev = 0;
1210
bool ford_moving = false;
1311

1412
static int ford_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
@@ -39,20 +37,20 @@ static int ford_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
3937
// exit controls on rising edge of brake press or on brake press when
4038
// speed > 0
4139
if (addr == 0x165) {
42-
int brake = GET_BYTE(to_push, 0) & 0x20;
43-
if (brake && (!(ford_brake_prev) || ford_moving)) {
40+
int brake_pressed = GET_BYTE(to_push, 0) & 0x20;
41+
if (brake_pressed && (!brake_pressed_prev || ford_moving)) {
4442
controls_allowed = 0;
4543
}
46-
ford_brake_prev = brake;
44+
brake_pressed_prev = brake_pressed;
4745
}
4846

4947
// exit controls on rising edge of gas press
5048
if (addr == 0x204) {
51-
int gas = (GET_BYTE(to_push, 0) & 0x03) | GET_BYTE(to_push, 1);
52-
if (gas && !(ford_gas_prev)) {
49+
bool gas_pressed = ((GET_BYTE(to_push, 0) & 0x03) | GET_BYTE(to_push, 1)) != 0;
50+
if (gas_pressed && !gas_pressed_prev) {
5351
controls_allowed = 0;
5452
}
55-
ford_gas_prev = gas;
53+
gas_pressed_prev = gas_pressed;
5654
}
5755

5856
if ((safety_mode_cnt > RELAY_TRNS_TIMEOUT) && (bus == 0) && (addr == 0x3CA)) {
@@ -74,7 +72,7 @@ static int ford_tx_hook(CAN_FIFOMailBox_TypeDef *to_send) {
7472

7573
// disallow actuator commands if gas or brake (with vehicle moving) are pressed
7674
// and the the latching controls_allowed flag is True
77-
int pedal_pressed = ford_gas_prev || (ford_brake_prev && ford_moving);
75+
int pedal_pressed = gas_pressed_prev || (brake_pressed_prev && ford_moving);
7876
bool current_controls_allowed = controls_allowed && !(pedal_pressed);
7977

8078
if (relay_malfunction) {

board/safety/safety_gm.h

+7-12
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ AddrCheckStruct gm_rx_checks[] = {
3333
};
3434
const int GM_RX_CHECK_LEN = sizeof(gm_rx_checks) / sizeof(gm_rx_checks[0]);
3535

36-
int gm_brake_prev = 0;
37-
int gm_gas_prev = 0;
3836
bool gm_moving = false;
3937
int gm_rt_torque_last = 0;
4038
int gm_desired_torque_last = 0;
@@ -81,25 +79,22 @@ static int gm_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
8179
// exit controls on rising edge of brake press or on brake press when
8280
// speed > 0
8381
if (addr == 241) {
84-
int brake = GET_BYTE(to_push, 1);
8582
// Brake pedal's potentiometer returns near-zero reading
8683
// even when pedal is not pressed
87-
if (brake < 10) {
88-
brake = 0;
89-
}
90-
if (brake && (!gm_brake_prev || gm_moving)) {
84+
bool brake_pressed = GET_BYTE(to_push, 1) >= 10;
85+
if (brake_pressed && (!brake_pressed_prev || gm_moving)) {
9186
controls_allowed = 0;
9287
}
93-
gm_brake_prev = brake;
88+
brake_pressed_prev = brake_pressed;
9489
}
9590

9691
// exit controls on rising edge of gas press
9792
if (addr == 417) {
98-
int gas = GET_BYTE(to_push, 6);
99-
if (gas && !gm_gas_prev) {
93+
bool gas_pressed = GET_BYTE(to_push, 6) != 0;
94+
if (gas_pressed && !gas_pressed_prev) {
10095
controls_allowed = 0;
10196
}
102-
gm_gas_prev = gas;
97+
gas_pressed_prev = gas_pressed;
10398
}
10499

105100
// exit controls on regen paddle
@@ -143,7 +138,7 @@ static int gm_tx_hook(CAN_FIFOMailBox_TypeDef *to_send) {
143138

144139
// disallow actuator commands if gas or brake (with vehicle moving) are pressed
145140
// and the the latching controls_allowed flag is True
146-
int pedal_pressed = gm_gas_prev || (gm_brake_prev && gm_moving);
141+
int pedal_pressed = gas_pressed_prev || (brake_pressed_prev && gm_moving);
147142
bool current_controls_allowed = controls_allowed && !pedal_pressed;
148143

149144
// BRAKE: safety check

board/safety/safety_honda.h

+7-9
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ AddrCheckStruct honda_bh_rx_checks[] = {
2828
const int HONDA_BH_RX_CHECKS_LEN = sizeof(honda_bh_rx_checks) / sizeof(honda_bh_rx_checks[0]);
2929

3030
int honda_brake = 0;
31-
int honda_gas_prev = 0;
32-
bool honda_brake_pressed_prev = false;
3331
bool honda_moving = false;
3432
bool honda_alt_brake_msg = false;
3533
bool honda_fwd_brake = false;
@@ -112,10 +110,10 @@ static int honda_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
112110
bool is_user_brake_msg = honda_alt_brake_msg ? ((addr) == 0x1BE) : ((addr) == 0x17C);
113111
if (is_user_brake_msg) {
114112
bool brake_pressed = honda_alt_brake_msg ? (GET_BYTE((to_push), 0) & 0x10) : (GET_BYTE((to_push), 6) & 0x20);
115-
if (brake_pressed && (!(honda_brake_pressed_prev) || honda_moving)) {
113+
if (brake_pressed && (!brake_pressed_prev || honda_moving)) {
116114
controls_allowed = 0;
117115
}
118-
honda_brake_pressed_prev = brake_pressed;
116+
brake_pressed_prev = brake_pressed;
119117
}
120118

121119
// exit controls on rising edge of gas press if interceptor (0x201 w/ len = 6)
@@ -133,11 +131,11 @@ static int honda_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
133131
// exit controls on rising edge of gas press if no interceptor
134132
if (!gas_interceptor_detected) {
135133
if (addr == 0x17C) {
136-
int gas = GET_BYTE(to_push, 0);
137-
if (gas && !honda_gas_prev) {
134+
bool gas_pressed = GET_BYTE(to_push, 0) != 0;
135+
if (gas_pressed && !gas_pressed_prev) {
138136
controls_allowed = 0;
139137
}
140-
honda_gas_prev = gas;
138+
gas_pressed_prev = gas_pressed;
141139
}
142140
}
143141
if ((bus == 2) && (addr == 0x1FA)) {
@@ -194,8 +192,8 @@ static int honda_tx_hook(CAN_FIFOMailBox_TypeDef *to_send) {
194192

195193
// disallow actuator commands if gas or brake (with vehicle moving) are pressed
196194
// and the the latching controls_allowed flag is True
197-
int pedal_pressed = honda_gas_prev || (gas_interceptor_prev > HONDA_GAS_INTERCEPTOR_THRESHOLD) ||
198-
(honda_brake_pressed_prev && honda_moving);
195+
int pedal_pressed = gas_pressed_prev || (gas_interceptor_prev > HONDA_GAS_INTERCEPTOR_THRESHOLD) ||
196+
(brake_pressed_prev && honda_moving);
199197
bool current_controls_allowed = controls_allowed && !(pedal_pressed);
200198

201199
// BRAKE: safety check

board/safety/safety_hyundai.h

+6-8
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ int hyundai_rt_torque_last = 0;
2222
int hyundai_desired_torque_last = 0;
2323
int hyundai_cruise_engaged_last = 0;
2424
int hyundai_speed = 0;
25-
bool hyundai_gas_last = false;
26-
bool hyundai_brake_last = false;
2725
uint32_t hyundai_ts_last = 0;
2826
struct sample_t hyundai_torque_driver; // last few driver torques measured
2927

@@ -56,11 +54,11 @@ static int hyundai_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
5654

5755
// exit controls on rising edge of gas press
5856
if (addr == 608) {
59-
bool gas = (GET_BYTE(to_push, 7) >> 6) != 0;
60-
if (gas && ! hyundai_gas_last) {
57+
bool gas_pressed = (GET_BYTE(to_push, 7) >> 6) != 0;
58+
if (gas_pressed && !gas_pressed_prev) {
6159
controls_allowed = 0;
6260
}
63-
hyundai_gas_last = gas;
61+
gas_pressed_prev = gas_pressed;
6462
}
6563

6664
// sample subaru wheel speed, averaging opposite corners
@@ -72,11 +70,11 @@ static int hyundai_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
7270

7371
// exit controls on rising edge of brake press
7472
if (addr == 916) {
75-
bool brake = (GET_BYTE(to_push, 6) >> 7) != 0;
76-
if (brake && (!hyundai_brake_last || (hyundai_speed > HYUNDAI_STANDSTILL_THRSLD))) {
73+
bool brake_pressed = (GET_BYTE(to_push, 6) >> 7) != 0;
74+
if (brake_pressed && (!brake_pressed_prev || (hyundai_speed > HYUNDAI_STANDSTILL_THRSLD))) {
7775
controls_allowed = 0;
7876
}
79-
hyundai_brake_last = brake;
77+
brake_pressed_prev = brake_pressed;
8078
}
8179

8280
// check if stock camera ECU is on bus 0

board/safety/safety_nissan.h

+6-8
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ float nissan_speed = 0;
2929
uint32_t nissan_ts_angle_last = 0;
3030
int nissan_cruise_engaged_last = 0;
3131
int nissan_desired_angle_last = 0;
32-
int nissan_gas_prev = 0;
33-
int nissan_brake_prev = 0;
3432

3533
struct sample_t nissan_angle_meas; // last 3 steer angles
3634

@@ -64,11 +62,11 @@ static int nissan_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
6462

6563
// exit controls on rising edge of gas press
6664
if (addr == 0x15c) {
67-
int gas = ((GET_BYTE(to_push, 5) << 2) | ((GET_BYTE(to_push, 6) >> 6) & 0x3));
68-
if ((gas > 0) && (nissan_gas_prev == 0)) {
65+
bool gas_pressed = ((GET_BYTE(to_push, 5) << 2) | ((GET_BYTE(to_push, 6) >> 6) & 0x3));
66+
if (gas_pressed && !gas_pressed_prev) {
6967
controls_allowed = 0;
7068
}
71-
nissan_gas_prev = gas;
69+
gas_pressed_prev = gas_pressed;
7270
}
7371

7472
// 0x169 is lkas cmd. If it is on bus 0, then relay is unexpectedly closed
@@ -91,11 +89,11 @@ static int nissan_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
9189

9290
// exit controls on rising edge of brake press, or if speed > 0 and brake
9391
if (addr == 0x454) {
94-
int brake = (GET_BYTE(to_push, 2) & 0x80);
95-
if ((brake > 0) && ((nissan_brake_prev == 0) || (nissan_speed > 0.))) {
92+
bool brake_pressed = (GET_BYTE(to_push, 2) & 0x80) != 0;
93+
if (brake_pressed && (!brake_pressed_prev || (nissan_speed > 0.))) {
9694
controls_allowed = 0;
9795
}
98-
nissan_brake_prev = brake;
96+
brake_pressed_prev = brake_pressed;
9997
}
10098
}
10199
}

board/safety/safety_subaru.h

+6-8
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ int subaru_rt_torque_last = 0;
3535
int subaru_desired_torque_last = 0;
3636
int subaru_speed = 0;
3737
uint32_t subaru_ts_last = 0;
38-
bool subaru_gas_last = false;
39-
bool subaru_brake_last = false;
4038
bool subaru_global = false;
4139
struct sample_t subaru_torque_driver; // last few driver torques measured
4240

@@ -106,22 +104,22 @@ static int subaru_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
106104

107105
// exit controls on rising edge of brake press (TODO: missing check for unsupported legacy models)
108106
if ((addr == 0x139) && subaru_global) {
109-
bool brake = (GET_BYTES_48(to_push) & 0xFFF0) > 0;
110-
if (brake && (!subaru_brake_last || (subaru_speed > SUBARU_STANDSTILL_THRSLD))) {
107+
bool brake_pressed = (GET_BYTES_48(to_push) & 0xFFF0) > 0;
108+
if (brake_pressed && (!brake_pressed_prev || (subaru_speed > SUBARU_STANDSTILL_THRSLD))) {
111109
controls_allowed = 0;
112110
}
113-
subaru_brake_last = brake;
111+
brake_pressed_prev = brake_pressed;
114112
}
115113

116114
// exit controls on rising edge of gas press
117115
if (((addr == 0x40) && subaru_global) ||
118116
((addr == 0x140) && !subaru_global)) {
119117
int byte = subaru_global ? 4 : 0;
120-
bool gas = GET_BYTE(to_push, byte) != 0;
121-
if (gas && !subaru_gas_last) {
118+
bool gas_pressed = GET_BYTE(to_push, byte) != 0;
119+
if (gas_pressed && !gas_pressed_prev) {
122120
controls_allowed = 0;
123121
}
124-
subaru_gas_last = gas;
122+
gas_pressed_prev = gas_pressed;
125123
}
126124

127125
if ((safety_mode_cnt > RELAY_TRNS_TIMEOUT) &&

board/safety/safety_tesla.h

-2
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ float tesla_ts_angle_last = 0;
3434

3535
int tesla_controls_allowed_last = 0;
3636

37-
int tesla_brake_prev = 0;
38-
int tesla_gas_prev = 0;
3937
int tesla_speed = 0;
4038
int eac_status = 0;
4139

board/safety/safety_toyota.h

+6-8
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@ int toyota_desired_torque_last = 0; // last desired steer torque
4040
int toyota_rt_torque_last = 0; // last desired torque for real time check
4141
uint32_t toyota_ts_last = 0;
4242
int toyota_cruise_engaged_last = 0; // cruise state
43-
bool toyota_gas_prev = false;
44-
bool toyota_brake_prev = false;
4543
bool toyota_moving = false;
4644
struct sample_t toyota_torque_meas; // last 3 motor torques produced by the eps
4745

@@ -112,11 +110,11 @@ static int toyota_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
112110
// most cars have brake_pressed on 0x226, corolla and rav4 on 0x224
113111
if ((addr == 0x224) || (addr == 0x226)) {
114112
int byte = (addr == 0x224) ? 0 : 4;
115-
bool brake = ((GET_BYTE(to_push, byte) >> 5) & 1) != 0;
116-
if (brake && (!toyota_brake_prev || toyota_moving)) {
113+
bool brake_pressed = ((GET_BYTE(to_push, byte) >> 5) & 1) != 0;
114+
if (brake_pressed && (!brake_pressed_prev || toyota_moving)) {
117115
controls_allowed = 0;
118116
}
119-
toyota_brake_prev = brake;
117+
brake_pressed_prev = brake_pressed;
120118
}
121119

122120
// exit controls on rising edge of interceptor gas press
@@ -132,11 +130,11 @@ static int toyota_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
132130

133131
// exit controls on rising edge of gas press
134132
if (addr == 0x2C1) {
135-
bool gas = GET_BYTE(to_push, 6) != 0;
136-
if (gas && !toyota_gas_prev && !gas_interceptor_detected) {
133+
bool gas_pressed = GET_BYTE(to_push, 6) != 0;
134+
if (gas_pressed && !gas_pressed_prev && !gas_interceptor_detected) {
137135
controls_allowed = 0;
138136
}
139-
toyota_gas_prev = gas;
137+
gas_pressed_prev = gas_pressed;
140138
}
141139

142140
// 0x2E4 is lkas cmd. If it is on bus 0, then relay is unexpectedly closed

board/safety/safety_volkswagen.h

+5-7
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ int volkswagen_rt_torque_last = 0;
3636
int volkswagen_desired_torque_last = 0;
3737
uint32_t volkswagen_ts_last = 0;
3838
bool volkswagen_moving = false;
39-
bool volkswagen_brake_pressed_prev = false;
40-
int volkswagen_gas_prev = 0;
4139
int volkswagen_torque_msg = 0;
4240
int volkswagen_lane_msg = 0;
4341
uint8_t volkswagen_crc8_lut_8h2f[256]; // Static lookup table for CRC8 poly 0x2F, aka 8H2F/AUTOSAR
@@ -137,21 +135,21 @@ static int volkswagen_mqb_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
137135
// Exit controls on rising edge of gas press
138136
// Signal: Motor_20.MO_Fahrpedalrohwert_01
139137
if (addr == MSG_MOTOR_20) {
140-
int gas = (GET_BYTES_04(to_push) >> 12) & 0xFF;
141-
if ((gas > 0) && (volkswagen_gas_prev == 0)) {
138+
bool gas_pressed = ((GET_BYTES_04(to_push) >> 12) & 0xFF) != 0;
139+
if (gas_pressed && !gas_pressed_prev) {
142140
controls_allowed = 0;
143141
}
144-
volkswagen_gas_prev = gas;
142+
gas_pressed_prev = gas_pressed;
145143
}
146144

147145
// Exit controls on rising edge of brake press
148146
// Signal: ESP_05.ESP_Fahrer_bremst
149147
if (addr == MSG_ESP_05) {
150148
bool brake_pressed = (GET_BYTE(to_push, 3) & 0x4) >> 2;
151-
if (brake_pressed && (!(volkswagen_brake_pressed_prev) || volkswagen_moving)) {
149+
if (brake_pressed && (!brake_pressed_prev || volkswagen_moving)) {
152150
controls_allowed = 0;
153151
}
154-
volkswagen_brake_pressed_prev = brake_pressed;
152+
brake_pressed_prev = brake_pressed;
155153
}
156154

157155
// If there are HCA messages on bus 0 not sent by OP, there's a relay problem

board/safety_declarations.h

+2
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ bool controls_allowed = false;
8585
bool relay_malfunction = false;
8686
bool gas_interceptor_detected = false;
8787
int gas_interceptor_prev = 0;
88+
bool gas_pressed_prev = false;
89+
bool brake_pressed_prev = false;
8890

8991
// time since safety mode has been changed
9092
uint32_t safety_mode_cnt = 0U;

0 commit comments

Comments
 (0)