Skip to content

Commit 2ebbe36

Browse files
authored
Subaru: disengage on gas press (#446)
* Subaru: check for gas pressed * added tests * rx freq check on throttle * also support for the not yet supported pre-global platform
1 parent ccf75c4 commit 2ebbe36

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

board/safety/safety_subaru.h

+11-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const AddrBus SUBARU_TX_MSGS[] = {{0x122, 0}, {0x164, 0}, {0x221, 0}, {0x322, 0}
1212

1313
// TODO: do checksum and counter checks after adding the signals to the outback dbc file
1414
AddrCheckStruct subaru_rx_checks[] = {
15+
{.addr = { 0x40, 0x140}, .bus = 0, .expected_timestep = 10000U},
1516
{.addr = {0x119, 0x371}, .bus = 0, .expected_timestep = 20000U},
1617
{.addr = {0x240, 0x144}, .bus = 0, .expected_timestep = 50000U},
1718
};
@@ -21,6 +22,7 @@ int subaru_cruise_engaged_last = 0;
2122
int subaru_rt_torque_last = 0;
2223
int subaru_desired_torque_last = 0;
2324
uint32_t subaru_ts_last = 0;
25+
bool subaru_gas_last = false;
2426
struct sample_t subaru_torque_driver; // last few driver torques measured
2527

2628
static int subaru_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
@@ -53,7 +55,15 @@ static int subaru_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
5355
subaru_cruise_engaged_last = cruise_engaged;
5456
}
5557

56-
// TODO: enforce cancellation on gas pressed
58+
// exit controls on rising edge of gas press
59+
if (((addr == 0x40) || (addr == 0x140)) && (bus == 0)) {
60+
int byte = (addr == 0x40) ? 4 : 0;
61+
bool gas = GET_BYTE(to_push, byte) != 0;
62+
if (gas && !subaru_gas_last) {
63+
controls_allowed = 0;
64+
}
65+
subaru_gas_last = gas;
66+
}
5767

5868
if ((safety_mode_cnt > RELAY_TRNS_TIMEOUT) && (bus == 0) && ((addr == 0x122) || (addr == 0x164))) {
5969
relay_malfunction = true;

board/safety/safety_toyota.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ static int toyota_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
106106

107107
// exit controls on rising edge of gas press
108108
if (addr == 0x2C1) {
109-
int gas = GET_BYTE(to_push, 6) & 0xFF;
109+
int gas = GET_BYTE(to_push, 6);
110110
if ((gas > 0) && (toyota_gas_prev == 0) && !gas_interceptor_detected) {
111111
controls_allowed = 0;
112112
}

tests/safety/test_subaru.py

+12
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ def _torque_msg(self, torque):
5252
to_send[0].RDLR = (t << 16)
5353
return to_send
5454

55+
def _gas_msg(self, gas):
56+
to_send = make_msg(0, 0x40)
57+
to_send[0].RDHR = gas & 0xFF
58+
return to_send
59+
5560
def test_spam_can_buses(self):
5661
test_spam_can_buses(self, TX_MSGS)
5762

@@ -74,6 +79,13 @@ def test_disable_control_allowed_from_cruise(self):
7479
self.safety.safety_rx_hook(to_push)
7580
self.assertFalse(self.safety.get_controls_allowed())
7681

82+
def test_disengage_on_gas(self):
83+
self.safety.set_controls_allowed(True)
84+
self.safety.safety_rx_hook(self._gas_msg(0))
85+
self.assertTrue(self.safety.get_controls_allowed())
86+
self.safety.safety_rx_hook(self._gas_msg(1))
87+
self.assertFalse(self.safety.get_controls_allowed())
88+
7789
def test_steer_safety_check(self):
7890
for enabled in [0, 1]:
7991
for t in range(-3000, 3000):

0 commit comments

Comments
 (0)