Skip to content

Commit 7caba24

Browse files
energeerbiasini
authored andcommitted
Addition to Bosch safety to support Hatchback (#111)
* Addition to Bosch safety to support Hatchback The computer brake is shown on 0x17c sensor on Accord and CR-V. We assumed all Bosch Hondas had the new 0x1be message which reports manual brake, but Civic Hatchback is not like this- It doesn't have this message and 0x17c works like the other Hondas so we are are passing a parameter from Openpilot for this. * Renamed variable * Make comment more descriptive * Added safety check for cancel-only spamming * Add regression test for brake on accord and crv Init with bosch safety variables Some more testing changes (still broken) Make second test work * Adds one more ttest * Cannot implicitly convert type 'int' to 'bool ' * ok to spam resume if controls_allowed==true * need to use current_controls_allowed. Still need to fix the message blocking * checking for bus 0 on button spam * better to use the car name in front of global vars * even better name and fixed safety tests
1 parent 63ca46b commit 7caba24

File tree

4 files changed

+67
-10
lines changed

4 files changed

+67
-10
lines changed

board/safety/safety_honda.h

+20-5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ int gas_interceptor_prev = 0;
1616
int ego_speed = 0;
1717
// TODO: auto-detect bosch hardware based on CAN messages?
1818
bool bosch_hardware = false;
19+
bool honda_alt_brake_msg = false;
1920

2021
static void honda_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
2122

@@ -36,11 +37,14 @@ static void honda_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
3637
}
3738
}
3839

39-
// user brake signal is different for nidec vs bosch hardware
40-
// nidec hardware: 0x17C bit 53
41-
// bosch hardware: 0x1BE bit 4
42-
#define IS_USER_BRAKE_MSG(to_push) (!bosch_hardware ? to_push->RIR>>21 == 0x17C : to_push->RIR>>21 == 0x1BE)
43-
#define USER_BRAKE_VALUE(to_push) (!bosch_hardware ? to_push->RDHR & 0x200000 : to_push->RDLR & 0x10)
40+
// user brake signal on 0x17C reports applied brake from computer brake on accord
41+
// and crv, which prevents the usual brake safety from working correctly. these
42+
// cars have a signal on 0x1BE which only detects user's brake being applied so
43+
// in these cases, this is used instead.
44+
// most hondas: 0x17C bit 53
45+
// accord, crv: 0x1BE bit 4
46+
#define IS_USER_BRAKE_MSG(to_push) (!honda_alt_brake_msg ? to_push->RIR>>21 == 0x17C : to_push->RIR>>21 == 0x1BE)
47+
#define USER_BRAKE_VALUE(to_push) (!honda_alt_brake_msg ? to_push->RDHR & 0x200000 : to_push->RDLR & 0x10)
4448
// exit controls on rising edge of brake press or on brake press when
4549
// speed > 0
4650
if (IS_USER_BRAKE_MSG(to_push)) {
@@ -115,6 +119,14 @@ static int honda_tx_hook(CAN_FIFOMailBox_TypeDef *to_send) {
115119
if ((to_send->RDLR & 0xFFFF0000) != to_send->RDLR) return 0;
116120
}
117121
}
122+
123+
// FORCE CANCEL: safety check only relevant when spamming the cancel button in Bosch HW
124+
// ensuring that only the cancel button press is sent (VAL 2) when controls are off.
125+
// This avoids unintended engagements while still allowing resume spam
126+
if (((to_send->RIR>>21) == 0x296) && bosch_hardware &&
127+
!current_controls_allowed && ((to_send->RDTR >> 4) & 0xFF) == 0) {
128+
if (((to_send->RDLR >> 5) & 0x7) != 2) return 0;
129+
}
118130

119131
// 1 allows the message through
120132
return true;
@@ -128,6 +140,7 @@ static int honda_tx_lin_hook(int lin_num, uint8_t *data, int len) {
128140
static void honda_init(int16_t param) {
129141
controls_allowed = 0;
130142
bosch_hardware = false;
143+
honda_alt_brake_msg = false;
131144
}
132145

133146
static int honda_fwd_hook(int bus_num, CAN_FIFOMailBox_TypeDef *to_fwd) {
@@ -146,6 +159,8 @@ const safety_hooks honda_hooks = {
146159
static void honda_bosch_init(int16_t param) {
147160
controls_allowed = 0;
148161
bosch_hardware = true;
162+
// Checking for alternate brake override from safety parameter
163+
honda_alt_brake_msg = param == 1 ? true : false;
149164
}
150165

151166
static int honda_bosch_fwd_hook(int bus_num, CAN_FIFOMailBox_TypeDef *to_fwd) {

tests/safety/libpandasafety_py.py

+2
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@
5252
int honda_tx_hook(CAN_FIFOMailBox_TypeDef *to_send);
5353
int get_brake_prev(void);
5454
int get_gas_prev(void);
55+
void set_honda_alt_brake_msg(bool);
56+
void set_bosch_hardware(bool);
5557
5658
void init_tests_cadillac(void);
5759
void cadillac_init(int16_t param);

tests/safety/test.c

+8
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,14 @@ int get_gas_prev(void){
105105
return gas_prev;
106106
}
107107

108+
void set_honda_alt_brake_msg(bool c){
109+
honda_alt_brake_msg = c;
110+
}
111+
112+
void set_bosch_hardware(bool c){
113+
bosch_hardware = c;
114+
}
115+
108116
void init_tests_toyota(void){
109117
torque_meas.min = 0;
110118
torque_meas.max = 0;

tests/safety/test_honda.py

+37-5
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ def _speed_msg(self, speed):
1818

1919
return to_send
2020

21-
def _button_msg(self, buttons):
21+
def _button_msg(self, buttons, msg):
2222
to_send = libpandasafety_py.ffi.new('CAN_FIFOMailBox_TypeDef *')
23-
to_send[0].RIR = 0x1A6 << 21
23+
to_send[0].RIR = msg << 21
2424
to_send[0].RDLR = buttons << 5
2525

2626
return to_send
@@ -32,6 +32,13 @@ def _brake_msg(self, brake):
3232

3333
return to_send
3434

35+
def _alt_brake_msg(self, brake):
36+
to_send = libpandasafety_py.ffi.new('CAN_FIFOMailBox_TypeDef *')
37+
to_send[0].RIR = 0x1BE << 21
38+
to_send[0].RDLR = 0x10 if brake else 0
39+
40+
return to_send
41+
3542
def _gas_msg(self, gas):
3643
to_send = libpandasafety_py.ffi.new('CAN_FIFOMailBox_TypeDef *')
3744
to_send[0].RIR = 0x17C << 21
@@ -65,18 +72,18 @@ def test_default_controls_not_allowed(self):
6572

6673
def test_resume_button(self):
6774
RESUME_BTN = 4
68-
self.safety.honda_rx_hook(self._button_msg(RESUME_BTN))
75+
self.safety.honda_rx_hook(self._button_msg(RESUME_BTN, 0x1A6))
6976
self.assertTrue(self.safety.get_controls_allowed())
7077

7178
def test_set_button(self):
7279
SET_BTN = 3
73-
self.safety.honda_rx_hook(self._button_msg(SET_BTN))
80+
self.safety.honda_rx_hook(self._button_msg(SET_BTN, 0x1A6))
7481
self.assertTrue(self.safety.get_controls_allowed())
7582

7683
def test_cancel_button(self):
7784
CANCEL_BTN = 2
7885
self.safety.set_controls_allowed(1)
79-
self.safety.honda_rx_hook(self._button_msg(CANCEL_BTN))
86+
self.safety.honda_rx_hook(self._button_msg(CANCEL_BTN, 0x1A6))
8087
self.assertFalse(self.safety.get_controls_allowed())
8188

8289
def test_sample_speed(self):
@@ -94,6 +101,17 @@ def test_disengage_on_brake(self):
94101
self.safety.honda_rx_hook(self._brake_msg(1))
95102
self.assertFalse(self.safety.get_controls_allowed())
96103

104+
def test_alt_disengage_on_brake(self):
105+
self.safety.set_honda_alt_brake_msg(1)
106+
self.safety.set_controls_allowed(1)
107+
self.safety.honda_rx_hook(self._alt_brake_msg(1))
108+
self.assertFalse(self.safety.get_controls_allowed())
109+
110+
self.safety.set_honda_alt_brake_msg(0)
111+
self.safety.set_controls_allowed(1)
112+
self.safety.honda_rx_hook(self._alt_brake_msg(1))
113+
self.assertTrue(self.safety.get_controls_allowed())
114+
97115
def test_allow_brake_at_zero_speed(self):
98116
# Brake was already pressed
99117
self.safety.honda_rx_hook(self._brake_msg(True))
@@ -143,6 +161,20 @@ def test_steer_safety_check(self):
143161
self.assertTrue(self.safety.honda_tx_hook(self._send_steer_msg(0x0000)))
144162
self.assertFalse(self.safety.honda_tx_hook(self._send_steer_msg(0x1000)))
145163

164+
def test_spam_cancel_safety_check(self):
165+
RESUME_BTN = 4
166+
SET_BTN = 3
167+
CANCEL_BTN = 2
168+
BUTTON_MSG = 0x296
169+
self.safety.set_bosch_hardware(1)
170+
self.safety.set_controls_allowed(0)
171+
self.assertTrue(self.safety.honda_tx_hook(self._button_msg(CANCEL_BTN, BUTTON_MSG)))
172+
self.assertFalse(self.safety.honda_tx_hook(self._button_msg(RESUME_BTN, BUTTON_MSG)))
173+
self.assertFalse(self.safety.honda_tx_hook(self._button_msg(SET_BTN, BUTTON_MSG)))
174+
# do not block resume if we are engaged already
175+
self.safety.set_controls_allowed(1)
176+
self.assertTrue(self.safety.honda_tx_hook(self._button_msg(RESUME_BTN, BUTTON_MSG)))
177+
146178

147179
if __name__ == "__main__":
148180
unittest.main()

0 commit comments

Comments
 (0)