From 5e97fc71747426ec0b3c35e9db747cdfe84452b5 Mon Sep 17 00:00:00 2001 From: Martin Lillepuu Date: Thu, 28 May 2020 00:00:44 +0300 Subject: [PATCH 001/113] Add subaru long TX and RX messages --- board/safety/safety_subaru.h | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/board/safety/safety_subaru.h b/board/safety/safety_subaru.h index b2e8c0738f..aa76d63184 100644 --- a/board/safety/safety_subaru.h +++ b/board/safety/safety_subaru.h @@ -12,7 +12,7 @@ const int SUBARU_STANDSTILL_THRSLD = 20; // about 1kph const int SUBARU_L_DRIVER_TORQUE_ALLOWANCE = 75; const int SUBARU_L_DRIVER_TORQUE_FACTOR = 10; -const CanMsg SUBARU_TX_MSGS[] = {{0x122, 0, 8}, {0x221, 0, 8}, {0x322, 0, 8}}; +const CanMsg SUBARU_TX_MSGS[] = {{0x122, 0, 8}, {0x321, 0, 8}, {0x322, 0, 8}, {0x220, 0, 8}, {0x221, 0, 8}, {0x222, 0, 8}, {0x240, 2, 8}, {0x13c, 2, 8}}; const int SUBARU_TX_MSGS_LEN = sizeof(SUBARU_TX_MSGS) / sizeof(SUBARU_TX_MSGS[0]); AddrCheckStruct subaru_rx_checks[] = { @@ -279,16 +279,28 @@ static int subaru_fwd_hook(int bus_num, CAN_FIFOMailBox_TypeDef *to_fwd) { int bus_fwd = -1; if (!relay_malfunction) { + int addr = GET_ADDR(to_fwd); + if (bus_num == 0) { - bus_fwd = 2; // Camera CAN + // 0x240 is CruiseControl for Global + // 0x13c is Brake_Status for Global + int block_msg = ((addr == 0x240) || + (addr == 0x13c)); + if (!block_msg) { + bus_fwd = 2; // Camera CAN + } } if (bus_num == 2) { - // Global platform - // 0x122 ES_LKAS - // 0x221 ES_Distance - // 0x322 ES_LKAS_State - int addr = GET_ADDR(to_fwd); - int block_msg = ((addr == 0x122) || (addr == 0x221) || (addr == 0x322)); + // Global Platform: + // 0x122 is ES_LKAS + // 0x220 is ES_Brake + // 0x221 is ES_Distance + // 0x222 is ES_Status + // 0x321 is ES_DashStatus + // 0x322 is ES_LKAS_State + int block_msg = ((addr == 0x122) || (addr == 0x220) || + (addr == 0x221) || (addr == 0x222) || + (addr == 0x321) || (addr == 0x322)); if (!block_msg) { bus_fwd = 0; // Main CAN } From 76676b7cfa49c4c42926855e5d2b0e88665f6700 Mon Sep 17 00:00:00 2001 From: Martin Lillepuu Date: Wed, 3 Jun 2020 22:55:22 +0300 Subject: [PATCH 002/113] misra c2012 fix --- board/safety/safety_subaru.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/board/safety/safety_subaru.h b/board/safety/safety_subaru.h index aa76d63184..b99abf7133 100644 --- a/board/safety/safety_subaru.h +++ b/board/safety/safety_subaru.h @@ -284,8 +284,7 @@ static int subaru_fwd_hook(int bus_num, CAN_FIFOMailBox_TypeDef *to_fwd) { if (bus_num == 0) { // 0x240 is CruiseControl for Global // 0x13c is Brake_Status for Global - int block_msg = ((addr == 0x240) || - (addr == 0x13c)); + int block_msg = ((addr == 0x240) || (addr == 0x13c)); if (!block_msg) { bus_fwd = 2; // Camera CAN } From 64919ade75b5ef09b3a2b91f1ca45e910dc7956f Mon Sep 17 00:00:00 2001 From: Martin Lillepuu Date: Wed, 3 Jun 2020 23:05:54 +0300 Subject: [PATCH 003/113] fix tests --- tests/safety/test_subaru.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/safety/test_subaru.py b/tests/safety/test_subaru.py index 8510765b1e..0a9e9a3659 100644 --- a/tests/safety/test_subaru.py +++ b/tests/safety/test_subaru.py @@ -24,15 +24,15 @@ class TestSubaruSafety(common.PandaSafetyTest): cnt_speed = 0 cnt_brake = 0 - TX_MSGS = [[0x122, 0], [0x221, 0], [0x322, 0]] + TX_MSGS = [[0x122, 0], [0x220, 0], [0x221, 0], [0x222, 0], [0x321, 0], [0x322, 0], [0x240, 2], [0x13c, 2]] STANDSTILL_THRESHOLD = 20 # 1kph (see dbc file) RELAY_MALFUNCTION_ADDR = 0x122 RELAY_MALFUNCTION_BUS = 0 - FWD_BLACKLISTED_ADDRS = {2: [0x122, 0x221, 0x322]} + FWD_BLACKLISTED_ADDRS = {0: [0x240, 0x13c], 2: [0x122, 0x220, 0x221, 0x222, 0x321, 0x322]} FWD_BUS_LOOKUP = {0: 2, 2: 0} def setUp(self): - self.packer = CANPackerPanda("subaru_global_2017") + self.packer = CANPackerPanda("subaru_global_2017_generated") self.safety = libpandasafety_py.libpandasafety self.safety.set_safety_hooks(Panda.SAFETY_SUBARU, 0) self.safety.init_tests() From aba9ad25e090aa6c1c2da146ade90eb925ac1ebc Mon Sep 17 00:00:00 2001 From: Martin Lillepuu Date: Wed, 3 Jun 2020 23:20:30 +0300 Subject: [PATCH 004/113] Reorder signals --- board/safety/safety_subaru.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/board/safety/safety_subaru.h b/board/safety/safety_subaru.h index b99abf7133..a2db473406 100644 --- a/board/safety/safety_subaru.h +++ b/board/safety/safety_subaru.h @@ -12,7 +12,7 @@ const int SUBARU_STANDSTILL_THRSLD = 20; // about 1kph const int SUBARU_L_DRIVER_TORQUE_ALLOWANCE = 75; const int SUBARU_L_DRIVER_TORQUE_FACTOR = 10; -const CanMsg SUBARU_TX_MSGS[] = {{0x122, 0, 8}, {0x321, 0, 8}, {0x322, 0, 8}, {0x220, 0, 8}, {0x221, 0, 8}, {0x222, 0, 8}, {0x240, 2, 8}, {0x13c, 2, 8}}; +const CanMsg SUBARU_TX_MSGS[] = {{0x122, 0, 8}, {0x220, 0, 8}, {0x221, 0, 8}, {0x222, 0, 8}, {0x321, 0, 8}, {0x322, 0, 8}, {0x13c, 2, 8}, {0x240, 2, 8}}; const int SUBARU_TX_MSGS_LEN = sizeof(SUBARU_TX_MSGS) / sizeof(SUBARU_TX_MSGS[0]); AddrCheckStruct subaru_rx_checks[] = { @@ -282,9 +282,9 @@ static int subaru_fwd_hook(int bus_num, CAN_FIFOMailBox_TypeDef *to_fwd) { int addr = GET_ADDR(to_fwd); if (bus_num == 0) { - // 0x240 is CruiseControl for Global // 0x13c is Brake_Status for Global - int block_msg = ((addr == 0x240) || (addr == 0x13c)); + // 0x240 is CruiseControl for Global + int block_msg = ((addr == 0x13c) || (addr == 0x240)); if (!block_msg) { bus_fwd = 2; // Camera CAN } From 3ed3139a5d8395f30b394cde7afc2e55c895e7e2 Mon Sep 17 00:00:00 2001 From: Martin Lillepuu Date: Wed, 23 Feb 2022 18:39:42 +0200 Subject: [PATCH 005/113] increase max steering torque --- board/safety/safety_subaru.h | 2 +- tests/safety/test_subaru.py | 2 +- tests/safety/test_subaru_legacy.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/board/safety/safety_subaru.h b/board/safety/safety_subaru.h index 5565da2164..e8d2178972 100644 --- a/board/safety/safety_subaru.h +++ b/board/safety/safety_subaru.h @@ -1,4 +1,4 @@ -const int SUBARU_MAX_STEER = 2047; // 1s +const int SUBARU_MAX_STEER = 3071; // 1s // real time torque limit to prevent controls spamming // the real time limit is 1500/sec const int SUBARU_MAX_RT_DELTA = 940; // max delta torque allowed for real time checks diff --git a/tests/safety/test_subaru.py b/tests/safety/test_subaru.py index 0f8ac19b18..5195dda7ac 100755 --- a/tests/safety/test_subaru.py +++ b/tests/safety/test_subaru.py @@ -8,7 +8,7 @@ MAX_RATE_UP = 50 MAX_RATE_DOWN = 70 -MAX_STEER = 2047 +MAX_STEER = 3071 MAX_RT_DELTA = 940 RT_INTERVAL = 250000 diff --git a/tests/safety/test_subaru_legacy.py b/tests/safety/test_subaru_legacy.py index 393a434a52..8ad826cad9 100755 --- a/tests/safety/test_subaru_legacy.py +++ b/tests/safety/test_subaru_legacy.py @@ -8,7 +8,7 @@ MAX_RATE_UP = 50 MAX_RATE_DOWN = 70 -MAX_STEER = 2047 +MAX_STEER = 3071 MAX_RT_DELTA = 940 RT_INTERVAL = 250000 From 90847c963a823fa33093ad24ad278858463d9695 Mon Sep 17 00:00:00 2001 From: Martin Lillepuu Date: Mon, 15 Aug 2022 01:08:48 +0300 Subject: [PATCH 006/113] merge fix --- board/safety/safety_subaru.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/board/safety/safety_subaru.h b/board/safety/safety_subaru.h index c674ad810e..5c2dc8970d 100644 --- a/board/safety/safety_subaru.h +++ b/board/safety/safety_subaru.h @@ -28,7 +28,7 @@ const CanMsg SUBARU_TX_MSGS[] = { {0x221, 0, 8}, {0x222, 0, 8}, {0x321, 0, 8}, - {0x322, 0, 8} + {0x322, 0, 8}, {0x13c, 2, 8}, {0x240, 2, 8} }; From 5c0b301444a69d5a16495e861317864aae3af576 Mon Sep 17 00:00:00 2001 From: Martin Lillepuu Date: Mon, 15 Aug 2022 01:13:01 +0300 Subject: [PATCH 007/113] merge fix, remove cnt from test --- tests/safety/test_subaru.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/tests/safety/test_subaru.py b/tests/safety/test_subaru.py index 4315847b1f..4cea9bbc84 100755 --- a/tests/safety/test_subaru.py +++ b/tests/safety/test_subaru.py @@ -5,11 +5,6 @@ import panda.tests.safety.common as common from panda.tests.safety.common import CANPackerPanda - cnt_torque_driver = 0 - cnt_cruise = 0 - cnt_speed = 0 - cnt_brake = 0 - class TestSubaruSafety(common.PandaSafetyTest, common.DriverTorqueSteeringSafetyTest): TX_MSGS = [[0x122, 0], [0x220, 0], [0x221, 0], [0x222, 0], [0x321, 0], [0x322, 0], [0x240, 2], [0x13c, 2]] From cbb8056d95485185364b378ca8c2483da2a99466 Mon Sep 17 00:00:00 2001 From: Martin Lillepuu Date: Fri, 19 Aug 2022 13:11:35 +0300 Subject: [PATCH 008/113] Move subaru logitudinal behind flag, add safety test --- board/safety/safety_subaru.h | 56 ++++++++++++++++++++++++++---------- python/__init__.py | 1 + tests/safety/common.py | 2 +- tests/safety/test_subaru.py | 15 ++++++++-- 4 files changed, 56 insertions(+), 18 deletions(-) diff --git a/board/safety/safety_subaru.h b/board/safety/safety_subaru.h index 5c2dc8970d..a807a751e1 100644 --- a/board/safety/safety_subaru.h +++ b/board/safety/safety_subaru.h @@ -23,6 +23,14 @@ const SteeringLimits SUBARU_GEN2_STEERING_LIMITS = { const int SUBARU_STANDSTILL_THRSLD = 20; // about 1kph const CanMsg SUBARU_TX_MSGS[] = { + {0x122, 0, 8}, + {0x221, 0, 8}, + {0x321, 0, 8}, + {0x322, 0, 8} +}; +#define SUBARU_TX_MSGS_LEN (sizeof(SUBARU_TX_MSGS) / sizeof(SUBARU_TX_MSGS[0])) + +const CanMsg SUBARU_LONG_TX_MSGS[] = { {0x122, 0, 8}, {0x220, 0, 8}, {0x221, 0, 8}, @@ -32,7 +40,7 @@ const CanMsg SUBARU_TX_MSGS[] = { {0x13c, 2, 8}, {0x240, 2, 8} }; -#define SUBARU_TX_MSGS_LEN (sizeof(SUBARU_TX_MSGS) / sizeof(SUBARU_TX_MSGS[0])) +#define SUBARU_LONG_TX_MSGS_LEN (sizeof(SUBARU_LONG_TX_MSGS) / sizeof(SUBARU_LONG_TX_MSGS[0])) const CanMsg SUBARU_GEN2_TX_MSGS[] = { {0x122, 0, 8}, @@ -64,7 +72,10 @@ addr_checks subaru_gen2_rx_checks = {subaru_gen2_addr_checks, SUBARU_GEN2_ADDR_C const uint16_t SUBARU_PARAM_GEN2 = 1; +const uint16_t SUBARU_PARAM_LONGITUDINAL = 2; + bool subaru_gen2 = false; +bool subaru_longitudinal = false; static uint32_t subaru_get_checksum(CANPacket_t *to_push) { @@ -136,6 +147,8 @@ static int subaru_tx_hook(CANPacket_t *to_send, bool longitudinal_allowed) { if (subaru_gen2) { tx = msg_allowed(to_send, SUBARU_GEN2_TX_MSGS, SUBARU_GEN2_TX_MSGS_LEN); + } else if (subaru_longitudinal) { + tx = msg_allowed(to_send, SUBARU_LONG_TX_MSGS, SUBARU_LONG_TX_MSGS_LEN); } else { tx = msg_allowed(to_send, SUBARU_TX_MSGS, SUBARU_TX_MSGS_LEN); } @@ -160,24 +173,33 @@ static int subaru_fwd_hook(int bus_num, CANPacket_t *to_fwd) { int addr = GET_ADDR(to_fwd); if (bus_num == 0) { - // 0x13c is Brake_Status for Global - // 0x240 is CruiseControl for Global - int block_msg = ((addr == 0x13c) || (addr == 0x240)); - if (!block_msg) { + if (subaru_longitudinal) { + // 0x13c is Brake_Status + // 0x240 is CruiseControl + int block_msg = ((addr == 0x13c) || (addr == 0x240)); + if (!block_msg) { + bus_fwd = 2; // Camera CAN + } + } else { bus_fwd = 2; // Camera CAN } } if (bus_num == 2) { - // Global Platform: - // 0x122 is ES_LKAS - // 0x220 is ES_Brake - // 0x221 is ES_Distance - // 0x222 is ES_Status - // 0x321 is ES_DashStatus - // 0x322 is ES_LKAS_State - int block_msg = ((addr == 0x122) || (addr == 0x220) || - (addr == 0x221) || (addr == 0x222) || - (addr == 0x321) || (addr == 0x322)); + int block_msg = -1; + if (subaru_longitudinal) { + // 0x122 is ES_LKAS + // 0x220 is ES_Brake + // 0x221 is ES_Distance + // 0x222 is ES_Status + // 0x321 is ES_DashStatus + // 0x322 is ES_LKAS_State + block_msg = ((addr == 0x122) || (addr == 0x220) || + (addr == 0x221) || (addr == 0x222) || + (addr == 0x321) || (addr == 0x322)); + } else { + block_msg = ((addr == 0x122) || (addr == 0x321) || + (addr == 0x322)); + } if (!block_msg) { bus_fwd = 0; // Main CAN } @@ -189,6 +211,10 @@ static int subaru_fwd_hook(int bus_num, CANPacket_t *to_fwd) { static const addr_checks* subaru_init(uint16_t param) { subaru_gen2 = GET_FLAG(param, SUBARU_PARAM_GEN2); +#ifdef ALLOW_DEBUG + subaru_longitudinal = GET_FLAG(param, SUBARU_PARAM_LONGITUDINAL); +#endif + if (subaru_gen2) { subaru_rx_checks = (addr_checks){subaru_gen2_addr_checks, SUBARU_GEN2_ADDR_CHECK_LEN}; } else { diff --git a/python/__init__.py b/python/__init__.py index 3c23a5538f..ee8f67e539 100644 --- a/python/__init__.py +++ b/python/__init__.py @@ -206,6 +206,7 @@ class Panda: FLAG_CHRYSLER_RAM_HD = 2 FLAG_SUBARU_GEN2 = 1 + FLAG_SUBARU_LONG = 2 FLAG_GM_HW_CAM = 1 diff --git a/tests/safety/common.py b/tests/safety/common.py index 553f972616..5cd696f424 100644 --- a/tests/safety/common.py +++ b/tests/safety/common.py @@ -564,7 +564,7 @@ def test_tx_hook_on_wrong_safety_mode(self): continue if {attr, current_test}.issubset({'TestToyotaSafety', 'TestToyotaAltBrakeSafety', 'TestToyotaStockLongitudinal'}): continue - if {attr, current_test}.issubset({'TestSubaruSafety', 'TestSubaruGen2Safety'}): + if {attr, current_test}.issubset({'TestSubaruSafety', 'TestSubaruGen2Safety', 'TestSubaruLongitudinalSafety'}): continue if {attr, current_test}.issubset({'TestVolkswagenPqSafety', 'TestVolkswagenPqStockSafety', 'TestVolkswagenPqLongSafety'}): continue diff --git a/tests/safety/test_subaru.py b/tests/safety/test_subaru.py index 4cea9bbc84..79470c163c 100755 --- a/tests/safety/test_subaru.py +++ b/tests/safety/test_subaru.py @@ -7,11 +7,11 @@ class TestSubaruSafety(common.PandaSafetyTest, common.DriverTorqueSteeringSafetyTest): - TX_MSGS = [[0x122, 0], [0x220, 0], [0x221, 0], [0x222, 0], [0x321, 0], [0x322, 0], [0x240, 2], [0x13c, 2]] + TX_MSGS = [[0x122, 0], [0x221, 0], [0x321, 0], [0x322, 0]] STANDSTILL_THRESHOLD = 20 # 1kph (see dbc file) RELAY_MALFUNCTION_ADDR = 0x122 RELAY_MALFUNCTION_BUS = 0 - FWD_BLACKLISTED_ADDRS = {0: [0x240, 0x13c], 2: [0x122, 0x220, 0x221, 0x222, 0x321, 0x322]} + FWD_BLACKLISTED_ADDRS = {2: [0x122, 0x321, 0x322]} FWD_BUS_LOOKUP = {0: 2, 2: 0} MAX_RATE_UP = 50 @@ -77,6 +77,17 @@ def setUp(self): self.safety.set_safety_hooks(Panda.SAFETY_SUBARU, Panda.FLAG_SUBARU_GEN2) self.safety.init_tests() +class TestSubaruLongitudinalSafety(TestSubaruSafety): + TX_MSGS = [[0x122, 0], [0x220, 0], [0x221, 0], [0x222, 0], [0x321, 0], [0x322, 0], [0x240, 2], [0x13c, 2]] + FWD_BLACKLISTED_ADDRS = {0: [0x240, 0x13c], 2: [0x122, 0x220, 0x221, 0x222, 0x321, 0x322]} + + def setUp(self): + self.packer = CANPackerPanda("subaru_global_2017_generated") + self.safety = libpandasafety_py.libpandasafety + self.safety.set_safety_hooks(Panda.SAFETY_SUBARU, Panda.FLAG_SUBARU_LONG) + self.safety.init_tests() + + if __name__ == "__main__": unittest.main() From 1697d0bbff40c4cf182f746044e2a8f146a7dca2 Mon Sep 17 00:00:00 2001 From: Martin Lillepuu Date: Thu, 8 Sep 2022 08:09:21 +0300 Subject: [PATCH 009/113] update subaru_longitudinal checks --- board/safety/safety_subaru.h | 43 +++++++++++++++--------------------- 1 file changed, 18 insertions(+), 25 deletions(-) diff --git a/board/safety/safety_subaru.h b/board/safety/safety_subaru.h index a807a751e1..f051348016 100644 --- a/board/safety/safety_subaru.h +++ b/board/safety/safety_subaru.h @@ -173,33 +173,28 @@ static int subaru_fwd_hook(int bus_num, CANPacket_t *to_fwd) { int addr = GET_ADDR(to_fwd); if (bus_num == 0) { - if (subaru_longitudinal) { - // 0x13c is Brake_Status - // 0x240 is CruiseControl - int block_msg = ((addr == 0x13c) || (addr == 0x240)); - if (!block_msg) { - bus_fwd = 2; // Camera CAN - } - } else { + // Global Platform + // 0x13c is Brake_Status + // 0x240 is CruiseControl + bool block_msg = subaru_longitudinal ? (addr == 0x13c) || (addr == 0x240) : false; + if (!block_msg) { bus_fwd = 2; // Camera CAN } } + if (bus_num == 2) { - int block_msg = -1; - if (subaru_longitudinal) { - // 0x122 is ES_LKAS - // 0x220 is ES_Brake - // 0x221 is ES_Distance - // 0x222 is ES_Status - // 0x321 is ES_DashStatus - // 0x322 is ES_LKAS_State - block_msg = ((addr == 0x122) || (addr == 0x220) || - (addr == 0x221) || (addr == 0x222) || - (addr == 0x321) || (addr == 0x322)); - } else { - block_msg = ((addr == 0x122) || (addr == 0x321) || - (addr == 0x322)); - } + // Global Platform + // 0x122 is ES_LKAS + // 0x220 is ES_Brake + // 0x221 is ES_Distance + // 0x222 is ES_Status + // 0x321 is ES_DashStatus + // 0x322 is ES_LKAS_State + bool block_msg = subaru_longitudinal ? (addr == 0x122) || (addr == 0x220) || + (addr == 0x221) || (addr == 0x222) || + (addr == 0x321) || (addr == 0x322) + : (addr == 0x122) || (addr == 0x321) || + (addr == 0x322); if (!block_msg) { bus_fwd = 0; // Main CAN } @@ -231,5 +226,3 @@ const safety_hooks subaru_hooks = { .tx_lin = nooutput_tx_lin_hook, .fwd = subaru_fwd_hook, }; - - From 61178a155cdbb3e0a86ca34b485f57c7991bb95b Mon Sep 17 00:00:00 2001 From: Adeeb Shihadeh Date: Fri, 16 Sep 2022 22:37:23 -0700 Subject: [PATCH 010/113] cleanup --- board/safety/safety_subaru.h | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/board/safety/safety_subaru.h b/board/safety/safety_subaru.h index 5da94abc85..457aa42e1a 100644 --- a/board/safety/safety_subaru.h +++ b/board/safety/safety_subaru.h @@ -172,9 +172,9 @@ static int subaru_fwd_hook(int bus_num, CANPacket_t *to_fwd) { // Global Platform // 0x13c is Brake_Status // 0x240 is CruiseControl - bool block_msg = subaru_longitudinal ? (addr == 0x13c) || (addr == 0x240) : false; + bool block_msg = subaru_longitudinal && ((addr == 0x13c) || (addr == 0x240)); if (!block_msg) { - bus_fwd = 2; // Camera CAN + bus_fwd = 2; // to the eyesight camera } } @@ -186,11 +186,9 @@ static int subaru_fwd_hook(int bus_num, CANPacket_t *to_fwd) { // 0x222 is ES_Status // 0x321 is ES_DashStatus // 0x322 is ES_LKAS_State - bool block_msg = subaru_longitudinal ? (addr == 0x122) || (addr == 0x220) || - (addr == 0x221) || (addr == 0x222) || - (addr == 0x321) || (addr == 0x322) - : (addr == 0x122) || (addr == 0x321) || - (addr == 0x322); + bool block_common = (addr == 0x122) || (addr == 0x321) || (addr == 0x322); + bool block_long = (addr == 0x220) || (addr == 0x221) || (addr == 0x222); + bool block_msg = block_common || (subaru_longitudinal && block_long); if (!block_msg) { bus_fwd = 0; // Main CAN } From 1211f9ec64ae6de035cd54bc89bdfa025194ce71 Mon Sep 17 00:00:00 2001 From: Martin Lillepuu Date: Sun, 2 Oct 2022 13:03:41 +0300 Subject: [PATCH 011/113] Add subaru longitudinal checks --- board/safety/safety_subaru.h | 49 +++++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/board/safety/safety_subaru.h b/board/safety/safety_subaru.h index 457aa42e1a..6e9683a35f 100644 --- a/board/safety/safety_subaru.h +++ b/board/safety/safety_subaru.h @@ -20,6 +20,23 @@ const SteeringLimits SUBARU_GEN2_STEERING_LIMITS = { .type = TorqueDriverLimited, }; +const int SUBARU_BRAKE_MIN = 0; +const int SUBARU_BRAKE_MAX = 400; + +const int SUBARU_THROTTLE_MIN = 0; +const int SUBARU_THROTTLE_MAX = 3400; + +const int SUBARU_RPM_MIN = 0; +const int SUBARU_RPM_MAX = 3200; + +/* TODO +const int SUBARU_THROTTLE_DELTA_UP = 50; +const int SUBARU_THROTTLE_DELTA_DOWN = 50; + +const int SUBARU_RPM_DELTA_UP = 50; +const int SUBARU_RPM_DELTA_DOWN = 50; +*/ + const CanMsg SUBARU_TX_MSGS[] = { {0x122, 0, 8}, {0x221, 0, 8}, @@ -136,10 +153,10 @@ static int subaru_rx_hook(CANPacket_t *to_push) { } static int subaru_tx_hook(CANPacket_t *to_send, bool longitudinal_allowed) { - UNUSED(longitudinal_allowed); int tx = 1; int addr = GET_ADDR(to_send); + bool violation = false; if (subaru_gen2) { tx = msg_allowed(to_send, SUBARU_GEN2_TX_MSGS, SUBARU_GEN2_TX_MSGS_LEN); @@ -160,6 +177,36 @@ static int subaru_tx_hook(CANPacket_t *to_send, bool longitudinal_allowed) { } } + + // longitudinal checks + if (addr == 0x220) { + int brake_pressure = ((GET_BYTES_04(to_send) >> 16) & 0xFFFFU); + + if (!longitudinal_allowed && brake_pressure != 0) { + violation = true; + } + violation |= max_limit_check(brake_pressure, SUBARU_BRAKE_MAX, SUBARU_BRAKE_MIN); + } + if (addr == 0x221) { + int cruise_throttle = ((GET_BYTES_04(to_send) >> 16) & 0xFFFU); + + if (!longitudinal_allowed && cruise_throttle != 0) { + violation = true; + } + violation |= max_limit_check(cruise_throttle, SUBARU_THROTTLE_MAX, SUBARU_THROTTLE_MIN); + } + if (addr == 0x222) { + int cruise_rpm = ((GET_BYTES_04(to_send) >> 16) & 0xFFFU); + + if (!longitudinal_allowed && cruise_rpm != 0) { + violation = true; + } + violation |= max_limit_check(cruise_rpm, SUBARU_RPM_MAX, SUBARU_RPM_MIN); + } + + if (violation) { + tx = 0; + } return tx; } From 577e45da7e04f107ad497e182db3ed0396b318ea Mon Sep 17 00:00:00 2001 From: Martin Lillepuu Date: Sun, 2 Oct 2022 13:07:39 +0300 Subject: [PATCH 012/113] misra fix --- board/safety/safety_subaru.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/board/safety/safety_subaru.h b/board/safety/safety_subaru.h index 6e9683a35f..edc384a8db 100644 --- a/board/safety/safety_subaru.h +++ b/board/safety/safety_subaru.h @@ -182,7 +182,7 @@ static int subaru_tx_hook(CANPacket_t *to_send, bool longitudinal_allowed) { if (addr == 0x220) { int brake_pressure = ((GET_BYTES_04(to_send) >> 16) & 0xFFFFU); - if (!longitudinal_allowed && brake_pressure != 0) { + if (!longitudinal_allowed && (brake_pressure != 0)) { violation = true; } violation |= max_limit_check(brake_pressure, SUBARU_BRAKE_MAX, SUBARU_BRAKE_MIN); @@ -190,7 +190,7 @@ static int subaru_tx_hook(CANPacket_t *to_send, bool longitudinal_allowed) { if (addr == 0x221) { int cruise_throttle = ((GET_BYTES_04(to_send) >> 16) & 0xFFFU); - if (!longitudinal_allowed && cruise_throttle != 0) { + if (!longitudinal_allowed && (cruise_throttle != 0)) { violation = true; } violation |= max_limit_check(cruise_throttle, SUBARU_THROTTLE_MAX, SUBARU_THROTTLE_MIN); @@ -198,7 +198,7 @@ static int subaru_tx_hook(CANPacket_t *to_send, bool longitudinal_allowed) { if (addr == 0x222) { int cruise_rpm = ((GET_BYTES_04(to_send) >> 16) & 0xFFFU); - if (!longitudinal_allowed && cruise_rpm != 0) { + if (!longitudinal_allowed && (cruise_rpm != 0)) { violation = true; } violation |= max_limit_check(cruise_rpm, SUBARU_RPM_MAX, SUBARU_RPM_MIN); From 03cdfba1855d18f073f4c777b3e556bf97c69bb7 Mon Sep 17 00:00:00 2001 From: Martin Lillepuu Date: Sun, 2 Oct 2022 14:07:40 +0300 Subject: [PATCH 013/113] Add subaru rpm and throttle rate limit checks --- board/safety/safety_subaru.h | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/board/safety/safety_subaru.h b/board/safety/safety_subaru.h index edc384a8db..372ec00650 100644 --- a/board/safety/safety_subaru.h +++ b/board/safety/safety_subaru.h @@ -25,17 +25,14 @@ const int SUBARU_BRAKE_MAX = 400; const int SUBARU_THROTTLE_MIN = 0; const int SUBARU_THROTTLE_MAX = 3400; +const int SUBARU_THROTTLE_DELTA = 50; const int SUBARU_RPM_MIN = 0; const int SUBARU_RPM_MAX = 3200; +const int SUBARU_RPM_DELTA = 50; -/* TODO -const int SUBARU_THROTTLE_DELTA_UP = 50; -const int SUBARU_THROTTLE_DELTA_DOWN = 50; - -const int SUBARU_RPM_DELTA_UP = 50; -const int SUBARU_RPM_DELTA_DOWN = 50; -*/ +int subaru_cruise_throttle_last = 0; +int subaru_cruise_rpm_last = 0; const CanMsg SUBARU_TX_MSGS[] = { {0x122, 0, 8}, @@ -152,6 +149,17 @@ static int subaru_rx_hook(CANPacket_t *to_push) { return valid; } +bool subaru_rate_limit_check(int val, int val_last, const int MAX_VAL_DELTA) { + + // *** rate limit check *** + int highest_val = MAX(val_last, 0) + MAX_VAL_DELTA; + int lowest_val = MIN(val_last, 0) - MAX_VAL_DELTA; + + // check for violation + return (val < lowest_val) || (val > highest_val); +} + + static int subaru_tx_hook(CANPacket_t *to_send, bool longitudinal_allowed) { int tx = 1; @@ -194,6 +202,8 @@ static int subaru_tx_hook(CANPacket_t *to_send, bool longitudinal_allowed) { violation = true; } violation |= max_limit_check(cruise_throttle, SUBARU_THROTTLE_MAX, SUBARU_THROTTLE_MIN); + violation |= subaru_rate_limit_check(cruise_throttle, subaru_cruise_throttle_last, SUBARU_THROTTLE_DELTA); + subaru_cruise_throttle_last = cruise_throttle; } if (addr == 0x222) { int cruise_rpm = ((GET_BYTES_04(to_send) >> 16) & 0xFFFU); @@ -202,6 +212,8 @@ static int subaru_tx_hook(CANPacket_t *to_send, bool longitudinal_allowed) { violation = true; } violation |= max_limit_check(cruise_rpm, SUBARU_RPM_MAX, SUBARU_RPM_MIN); + violation |= subaru_rate_limit_check(cruise_rpm, subaru_cruise_rpm_last, SUBARU_RPM_DELTA); + subaru_cruise_rpm_last = cruise_rpm; } if (violation) { From 2b90cc9350780655151eb6857165b1b02fe76ad1 Mon Sep 17 00:00:00 2001 From: Martin Lillepuu Date: Sun, 16 Oct 2022 16:02:34 +0000 Subject: [PATCH 014/113] temporary disable cruise throttle and cruise rpm checks --- board/safety/safety_subaru.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/board/safety/safety_subaru.h b/board/safety/safety_subaru.h index 372ec00650..4d85bd9e46 100644 --- a/board/safety/safety_subaru.h +++ b/board/safety/safety_subaru.h @@ -195,6 +195,7 @@ static int subaru_tx_hook(CANPacket_t *to_send, bool longitudinal_allowed) { } violation |= max_limit_check(brake_pressure, SUBARU_BRAKE_MAX, SUBARU_BRAKE_MIN); } + /* FIXME: write tests and fix eyesight faults if (addr == 0x221) { int cruise_throttle = ((GET_BYTES_04(to_send) >> 16) & 0xFFFU); @@ -215,6 +216,7 @@ static int subaru_tx_hook(CANPacket_t *to_send, bool longitudinal_allowed) { violation |= subaru_rate_limit_check(cruise_rpm, subaru_cruise_rpm_last, SUBARU_RPM_DELTA); subaru_cruise_rpm_last = cruise_rpm; } + */ if (violation) { tx = 0; From 98249dcff9fe6fe88cd493b0888420374a1640fb Mon Sep 17 00:00:00 2001 From: Martin Lillepuu Date: Wed, 7 Dec 2022 06:49:10 +0000 Subject: [PATCH 015/113] update subaru long test --- tests/safety/test_subaru.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/safety/test_subaru.py b/tests/safety/test_subaru.py index 7d6e55c936..66e9117528 100755 --- a/tests/safety/test_subaru.py +++ b/tests/safety/test_subaru.py @@ -83,7 +83,7 @@ class TestSubaruLongitudinalSafety(TestSubaruSafety): def setUp(self): self.packer = CANPackerPanda("subaru_global_2017_generated") - self.safety = libpandasafety_py.libpandasafety + self.safety = libpanda_py.libpanda self.safety.set_safety_hooks(Panda.SAFETY_SUBARU, Panda.FLAG_SUBARU_LONG) self.safety.init_tests() From e53c8478e13a8d22e960d7296e6d0bb56ea8b43c Mon Sep 17 00:00:00 2001 From: Martin Lillepuu Date: Sat, 10 Dec 2022 21:14:17 +0000 Subject: [PATCH 016/113] update longitudinal safety and add tests --- board/safety/safety_subaru.h | 47 +++++++++++++++++------------------- tests/safety/test_subaru.py | 36 +++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 25 deletions(-) diff --git a/board/safety/safety_subaru.h b/board/safety/safety_subaru.h index 96ff6b0d03..9cf95ba185 100644 --- a/board/safety/safety_subaru.h +++ b/board/safety/safety_subaru.h @@ -186,37 +186,34 @@ static int subaru_tx_hook(CANPacket_t *to_send) { } - // longitudinal checks - /* FIXME: write tests and fix eyesight faults - if (addr == 0x220) { - int brake_pressure = ((GET_BYTES_04(to_send) >> 16) & 0xFFFFU); - - if (!controls_allowed && (brake_pressure != 0)) { - violation = true; + if (subaru_longitudinal) { + // allow es_brake aeb passthrough and check limits + if (addr == 0x220) { + bool es_brake_active = GET_BIT(to_send, 38U) != 0; + int es_brake_pressure = ((GET_BYTES_04(to_send) >> 16) & 0xFFFFU); + + if (!controls_allowed && (es_brake_pressure != 0) && !es_brake_active) { + violation = true; + } + violation |= max_limit_check(es_brake_pressure, SUBARU_BRAKE_MAX, SUBARU_BRAKE_MIN); } - violation |= max_limit_check(brake_pressure, SUBARU_BRAKE_MAX, SUBARU_BRAKE_MIN); - } - if (addr == 0x221) { - int cruise_throttle = ((GET_BYTES_04(to_send) >> 16) & 0xFFFU); + // check es_distance cruise_throttle limits + if ((addr == 0x221) && controls_allowed) { + int cruise_throttle = ((GET_BYTES_04(to_send) >> 16) & 0xFFFU); - if (!longitudinal_allowed && (cruise_throttle != 0)) { - violation = true; + violation |= max_limit_check(cruise_throttle, SUBARU_THROTTLE_MAX, SUBARU_THROTTLE_MIN); + violation |= subaru_rate_limit_check(cruise_throttle, subaru_cruise_throttle_last, SUBARU_THROTTLE_DELTA); + subaru_cruise_throttle_last = cruise_throttle; } - violation |= max_limit_check(cruise_throttle, SUBARU_THROTTLE_MAX, SUBARU_THROTTLE_MIN); - violation |= subaru_rate_limit_check(cruise_throttle, subaru_cruise_throttle_last, SUBARU_THROTTLE_DELTA); - subaru_cruise_throttle_last = cruise_throttle; - } - if (addr == 0x222) { - int cruise_rpm = ((GET_BYTES_04(to_send) >> 16) & 0xFFFU); + // check es_status cruise_rpm limits + if ((addr == 0x222) && controls_allowed) { + int cruise_rpm = ((GET_BYTES_04(to_send) >> 16) & 0xFFFU); - if (!longitudinal_allowed && (cruise_rpm != 0)) { - violation = true; + violation |= max_limit_check(cruise_rpm, SUBARU_RPM_MAX, SUBARU_RPM_MIN); + violation |= subaru_rate_limit_check(cruise_rpm, subaru_cruise_rpm_last, SUBARU_RPM_DELTA); + subaru_cruise_rpm_last = cruise_rpm; } - violation |= max_limit_check(cruise_rpm, SUBARU_RPM_MAX, SUBARU_RPM_MIN); - violation |= subaru_rate_limit_check(cruise_rpm, subaru_cruise_rpm_last, SUBARU_RPM_DELTA); - subaru_cruise_rpm_last = cruise_rpm; } - */ if (violation) { tx = 0; diff --git a/tests/safety/test_subaru.py b/tests/safety/test_subaru.py index 66e9117528..b331a4a926 100755 --- a/tests/safety/test_subaru.py +++ b/tests/safety/test_subaru.py @@ -71,6 +71,13 @@ class TestSubaruGen2Safety(TestSubaruSafety): MAX_RATE_DOWN = 40 MAX_TORQUE = 1000 + RPM_MAX = 3200 + THROTTLE_MAX = 3400 + BRAKE_MAX = 400 + + RPM_DELTA = 50 + THROTTLE_DELTA = 50 + def setUp(self): self.packer = CANPackerPanda("subaru_global_2017_generated") self.safety = libpanda_py.libpanda @@ -87,6 +94,35 @@ def setUp(self): self.safety.set_safety_hooks(Panda.SAFETY_SUBARU, Panda.FLAG_SUBARU_LONG) self.safety.init_tests() + def _es_brake_msg(self, brake=0, active=False): + values = {"Brake_Pressure": brake} + values = {"Cruise_Brake_Active": active} + return self.packer.make_can_msg_panda("ES_Brake", 0, values) + + def _es_distance_msg(self, throttle=0): + values = {"Cruise_Throttle": throttle} + return self.packer.make_can_msg_panda("ES_Distance", 0, values) + + def _es_status_msg(self, rpm=0): + values = {"Cruise_RPM": rpm} + return self.packer.make_can_msg_panda("ES_Status", 0, values) + + def test_es_brake_msg(self): + self.assertTrue(self._tx(self._es_brake_msg())) + self.assertFalse(self._tx(self._es_brake_msg(brake=1))) + self.assertFalse(self._tx(self._es_brake_msg(brake=BRAKE_MAX+1))) + + def test_es_distance_msg(self): + self.assertTrue(self._tx(self._es_distance_msg())) + self.assertTrue(self._tx(self._es_distance_msg(throttle=THROTTLE_DELTA))) + self.assertFalse(self._tx(self._es_distance_msg(throttle=THROTTLE_DELTA+THROTTLE_DELTA+1))) + self.assertFalse(self._tx(self._es_distance_msg(throttle=THROTTLE_MAX+1))) + + def test_es_status_msg(self): + self.assertTrue(self._tx(self._es_status_msg())) + self.assertTrue(self._tx(self._es_status_msg(rpm=RPM_DELTA))) + self.assertFalse(self._tx(self._es_status_msg(rpm=RPM_DELTA+RPM_DELTA+1))) + self.assertFalse(self._tx(self._es_status_msg(rpm=RPM_MAX+1))) if __name__ == "__main__": From 2e3f5e347e90a124c545be3bed88c8adaac66b3f Mon Sep 17 00:00:00 2001 From: Martin Lillepuu Date: Sat, 10 Dec 2022 23:04:54 +0000 Subject: [PATCH 017/113] fix tests --- tests/safety/test_subaru.py | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/tests/safety/test_subaru.py b/tests/safety/test_subaru.py index b331a4a926..778d55d6c2 100755 --- a/tests/safety/test_subaru.py +++ b/tests/safety/test_subaru.py @@ -71,13 +71,6 @@ class TestSubaruGen2Safety(TestSubaruSafety): MAX_RATE_DOWN = 40 MAX_TORQUE = 1000 - RPM_MAX = 3200 - THROTTLE_MAX = 3400 - BRAKE_MAX = 400 - - RPM_DELTA = 50 - THROTTLE_DELTA = 50 - def setUp(self): self.packer = CANPackerPanda("subaru_global_2017_generated") self.safety = libpanda_py.libpanda @@ -88,6 +81,13 @@ class TestSubaruLongitudinalSafety(TestSubaruSafety): TX_MSGS = [[0x122, 0], [0x220, 0], [0x221, 0], [0x222, 0], [0x321, 0], [0x322, 0], [0x240, 2], [0x13c, 2]] FWD_BLACKLISTED_ADDRS = {0: [0x240, 0x13c], 2: [0x122, 0x220, 0x221, 0x222, 0x321, 0x322]} + RPM_MAX = 3200 + THROTTLE_MAX = 3400 + BRAKE_MAX = 400 + + RPM_DELTA = 50 + THROTTLE_DELTA = 50 + def setUp(self): self.packer = CANPackerPanda("subaru_global_2017_generated") self.safety = libpanda_py.libpanda @@ -114,15 +114,15 @@ def test_es_brake_msg(self): def test_es_distance_msg(self): self.assertTrue(self._tx(self._es_distance_msg())) - self.assertTrue(self._tx(self._es_distance_msg(throttle=THROTTLE_DELTA))) - self.assertFalse(self._tx(self._es_distance_msg(throttle=THROTTLE_DELTA+THROTTLE_DELTA+1))) - self.assertFalse(self._tx(self._es_distance_msg(throttle=THROTTLE_MAX+1))) + self.assertTrue(self._tx(self._es_distance_msg(throttle=self.THROTTLE_DELTA))) + self.assertFalse(self._tx(self._es_distance_msg(throttle=self.THROTTLE_DELTA+self.THROTTLE_DELTA+1))) + self.assertFalse(self._tx(self._es_distance_msg(throttle=self.THROTTLE_MAX+1))) def test_es_status_msg(self): self.assertTrue(self._tx(self._es_status_msg())) - self.assertTrue(self._tx(self._es_status_msg(rpm=RPM_DELTA))) - self.assertFalse(self._tx(self._es_status_msg(rpm=RPM_DELTA+RPM_DELTA+1))) - self.assertFalse(self._tx(self._es_status_msg(rpm=RPM_MAX+1))) + self.assertTrue(self._tx(self._es_status_msg(rpm=self.RPM_DELTA))) + self.assertFalse(self._tx(self._es_status_msg(rpm=self.RPM_DELTA+self.RPM_DELTA+1))) + self.assertFalse(self._tx(self._es_status_msg(rpm=self.RPM_MAX+1))) if __name__ == "__main__": From cf23e8d2ac07eefcc2a0897edf249f7cb733aca4 Mon Sep 17 00:00:00 2001 From: Martin Lillepuu Date: Sat, 10 Dec 2022 23:06:48 +0000 Subject: [PATCH 018/113] fix misra --- board/safety/safety_subaru.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/board/safety/safety_subaru.h b/board/safety/safety_subaru.h index 9cf95ba185..ab283874a5 100644 --- a/board/safety/safety_subaru.h +++ b/board/safety/safety_subaru.h @@ -189,7 +189,7 @@ static int subaru_tx_hook(CANPacket_t *to_send) { if (subaru_longitudinal) { // allow es_brake aeb passthrough and check limits if (addr == 0x220) { - bool es_brake_active = GET_BIT(to_send, 38U) != 0; + bool es_brake_active = GET_BIT(to_send, 38U) != 0U; int es_brake_pressure = ((GET_BYTES_04(to_send) >> 16) & 0xFFFFU); if (!controls_allowed && (es_brake_pressure != 0) && !es_brake_active) { From 63c34758e11d379dae521a1fbe7511d2d38d5263 Mon Sep 17 00:00:00 2001 From: Martin Lillepuu Date: Sun, 11 Dec 2022 08:51:16 +0000 Subject: [PATCH 019/113] subaru long signals limits checks --- board/safety/safety_subaru.h | 27 +++------------------------ tests/safety/test_subaru.py | 26 ++++++++++---------------- 2 files changed, 13 insertions(+), 40 deletions(-) diff --git a/board/safety/safety_subaru.h b/board/safety/safety_subaru.h index ab283874a5..97e6ac18b5 100644 --- a/board/safety/safety_subaru.h +++ b/board/safety/safety_subaru.h @@ -25,11 +25,11 @@ const int SUBARU_BRAKE_MAX = 400; const int SUBARU_THROTTLE_MIN = 0; const int SUBARU_THROTTLE_MAX = 3400; -const int SUBARU_THROTTLE_DELTA = 50; +//const int SUBARU_THROTTLE_DELTA = 50; const int SUBARU_RPM_MIN = 0; const int SUBARU_RPM_MAX = 3200; -const int SUBARU_RPM_DELTA = 50; +//const int SUBARU_RPM_DELTA = 50; int subaru_cruise_throttle_last = 0; int subaru_cruise_rpm_last = 0; @@ -149,16 +149,6 @@ static int subaru_rx_hook(CANPacket_t *to_push) { return valid; } -bool subaru_rate_limit_check(int val, int val_last, const int MAX_VAL_DELTA) { - - // *** rate limit check *** - int highest_val = MAX(val_last, 0) + MAX_VAL_DELTA; - int lowest_val = MIN(val_last, 0) - MAX_VAL_DELTA; - - // check for violation - return (val < lowest_val) || (val > highest_val); -} - static int subaru_tx_hook(CANPacket_t *to_send) { @@ -187,31 +177,20 @@ static int subaru_tx_hook(CANPacket_t *to_send) { } if (subaru_longitudinal) { - // allow es_brake aeb passthrough and check limits + // check es_brake brake_pressure limits if (addr == 0x220) { - bool es_brake_active = GET_BIT(to_send, 38U) != 0U; int es_brake_pressure = ((GET_BYTES_04(to_send) >> 16) & 0xFFFFU); - - if (!controls_allowed && (es_brake_pressure != 0) && !es_brake_active) { - violation = true; - } violation |= max_limit_check(es_brake_pressure, SUBARU_BRAKE_MAX, SUBARU_BRAKE_MIN); } // check es_distance cruise_throttle limits if ((addr == 0x221) && controls_allowed) { int cruise_throttle = ((GET_BYTES_04(to_send) >> 16) & 0xFFFU); - violation |= max_limit_check(cruise_throttle, SUBARU_THROTTLE_MAX, SUBARU_THROTTLE_MIN); - violation |= subaru_rate_limit_check(cruise_throttle, subaru_cruise_throttle_last, SUBARU_THROTTLE_DELTA); - subaru_cruise_throttle_last = cruise_throttle; } // check es_status cruise_rpm limits if ((addr == 0x222) && controls_allowed) { int cruise_rpm = ((GET_BYTES_04(to_send) >> 16) & 0xFFFU); - violation |= max_limit_check(cruise_rpm, SUBARU_RPM_MAX, SUBARU_RPM_MIN); - violation |= subaru_rate_limit_check(cruise_rpm, subaru_cruise_rpm_last, SUBARU_RPM_DELTA); - subaru_cruise_rpm_last = cruise_rpm; } } diff --git a/tests/safety/test_subaru.py b/tests/safety/test_subaru.py index 778d55d6c2..a84c51174b 100755 --- a/tests/safety/test_subaru.py +++ b/tests/safety/test_subaru.py @@ -81,12 +81,9 @@ class TestSubaruLongitudinalSafety(TestSubaruSafety): TX_MSGS = [[0x122, 0], [0x220, 0], [0x221, 0], [0x222, 0], [0x321, 0], [0x322, 0], [0x240, 2], [0x13c, 2]] FWD_BLACKLISTED_ADDRS = {0: [0x240, 0x13c], 2: [0x122, 0x220, 0x221, 0x222, 0x321, 0x322]} - RPM_MAX = 3200 - THROTTLE_MAX = 3400 - BRAKE_MAX = 400 - - RPM_DELTA = 50 - THROTTLE_DELTA = 50 + MAX_RPM = 3200 + MAX_BRAKE = 400 + MAX_THROTTLE = 3400 def setUp(self): self.packer = CANPackerPanda("subaru_global_2017_generated") @@ -94,9 +91,8 @@ def setUp(self): self.safety.set_safety_hooks(Panda.SAFETY_SUBARU, Panda.FLAG_SUBARU_LONG) self.safety.init_tests() - def _es_brake_msg(self, brake=0, active=False): + def _es_brake_msg(self, brake=0): values = {"Brake_Pressure": brake} - values = {"Cruise_Brake_Active": active} return self.packer.make_can_msg_panda("ES_Brake", 0, values) def _es_distance_msg(self, throttle=0): @@ -109,20 +105,18 @@ def _es_status_msg(self, rpm=0): def test_es_brake_msg(self): self.assertTrue(self._tx(self._es_brake_msg())) - self.assertFalse(self._tx(self._es_brake_msg(brake=1))) - self.assertFalse(self._tx(self._es_brake_msg(brake=BRAKE_MAX+1))) + self.assertTrue(self._tx(self._es_brake_msg(brake=self.MAX_BRAKE))) + self.assertFalse(self._tx(self._es_brake_msg(brake=self.MAX_BRAKE+1))) def test_es_distance_msg(self): self.assertTrue(self._tx(self._es_distance_msg())) - self.assertTrue(self._tx(self._es_distance_msg(throttle=self.THROTTLE_DELTA))) - self.assertFalse(self._tx(self._es_distance_msg(throttle=self.THROTTLE_DELTA+self.THROTTLE_DELTA+1))) - self.assertFalse(self._tx(self._es_distance_msg(throttle=self.THROTTLE_MAX+1))) + self.assertTrue(self._tx(self._es_distance_msg(throttle=self.MAX_THROTTLE))) + self.assertFalse(self._tx(self._es_distance_msg(throttle=self.MAX_THROTTLE+1))) def test_es_status_msg(self): self.assertTrue(self._tx(self._es_status_msg())) - self.assertTrue(self._tx(self._es_status_msg(rpm=self.RPM_DELTA))) - self.assertFalse(self._tx(self._es_status_msg(rpm=self.RPM_DELTA+self.RPM_DELTA+1))) - self.assertFalse(self._tx(self._es_status_msg(rpm=self.RPM_MAX+1))) + self.assertTrue(self._tx(self._es_status_msg(rpm=self.MAX_RPM))) + self.assertFalse(self._tx(self._es_status_msg(rpm=self.MAX_RPM+1))) if __name__ == "__main__": From b9cfddf2b4ad2a0da01e88d26712d61e65f97444 Mon Sep 17 00:00:00 2001 From: Martin Lillepuu Date: Sun, 11 Dec 2022 09:46:06 +0000 Subject: [PATCH 020/113] Add controls_allowed to long tests --- tests/safety/test_subaru.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/safety/test_subaru.py b/tests/safety/test_subaru.py index a84c51174b..f0e7440322 100755 --- a/tests/safety/test_subaru.py +++ b/tests/safety/test_subaru.py @@ -109,11 +109,13 @@ def test_es_brake_msg(self): self.assertFalse(self._tx(self._es_brake_msg(brake=self.MAX_BRAKE+1))) def test_es_distance_msg(self): + self.safety.set_controls_allowed(True) self.assertTrue(self._tx(self._es_distance_msg())) self.assertTrue(self._tx(self._es_distance_msg(throttle=self.MAX_THROTTLE))) self.assertFalse(self._tx(self._es_distance_msg(throttle=self.MAX_THROTTLE+1))) def test_es_status_msg(self): + self.safety.set_controls_allowed(True) self.assertTrue(self._tx(self._es_status_msg())) self.assertTrue(self._tx(self._es_status_msg(rpm=self.MAX_RPM))) self.assertFalse(self._tx(self._es_status_msg(rpm=self.MAX_RPM+1))) From b13a453990b29ce1a8f865672065dacbc0a9378e Mon Sep 17 00:00:00 2001 From: Martin Lillepuu Date: Sun, 11 Dec 2022 10:12:24 +0000 Subject: [PATCH 021/113] remove unused variables --- board/safety/safety_subaru.h | 5 ----- 1 file changed, 5 deletions(-) diff --git a/board/safety/safety_subaru.h b/board/safety/safety_subaru.h index 97e6ac18b5..6fa419997e 100644 --- a/board/safety/safety_subaru.h +++ b/board/safety/safety_subaru.h @@ -25,14 +25,9 @@ const int SUBARU_BRAKE_MAX = 400; const int SUBARU_THROTTLE_MIN = 0; const int SUBARU_THROTTLE_MAX = 3400; -//const int SUBARU_THROTTLE_DELTA = 50; const int SUBARU_RPM_MIN = 0; const int SUBARU_RPM_MAX = 3200; -//const int SUBARU_RPM_DELTA = 50; - -int subaru_cruise_throttle_last = 0; -int subaru_cruise_rpm_last = 0; const CanMsg SUBARU_TX_MSGS[] = { {0x122, 0, 8}, From d1eff3e354d22aa9550e878f2ff497c7ad61355e Mon Sep 17 00:00:00 2001 From: Martin Lillepuu Date: Mon, 12 Dec 2022 07:00:31 +0000 Subject: [PATCH 022/113] Add AEB passthru and tests --- board/safety/safety_subaru.h | 3 ++- tests/safety/test_subaru.py | 9 ++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/board/safety/safety_subaru.h b/board/safety/safety_subaru.h index 6fa419997e..a0aa356852 100644 --- a/board/safety/safety_subaru.h +++ b/board/safety/safety_subaru.h @@ -174,8 +174,9 @@ static int subaru_tx_hook(CANPacket_t *to_send) { if (subaru_longitudinal) { // check es_brake brake_pressure limits if (addr == 0x220) { + bool aeb = (bus == 2) && (GET_BIT(to_send, 38U) == 1); int es_brake_pressure = ((GET_BYTES_04(to_send) >> 16) & 0xFFFFU); - violation |= max_limit_check(es_brake_pressure, SUBARU_BRAKE_MAX, SUBARU_BRAKE_MIN); + violation |= !aeb && max_limit_check(es_brake_pressure, SUBARU_BRAKE_MAX, SUBARU_BRAKE_MIN); } // check es_distance cruise_throttle limits if ((addr == 0x221) && controls_allowed) { diff --git a/tests/safety/test_subaru.py b/tests/safety/test_subaru.py index f0e7440322..dafe51c3dd 100755 --- a/tests/safety/test_subaru.py +++ b/tests/safety/test_subaru.py @@ -91,9 +91,10 @@ def setUp(self): self.safety.set_safety_hooks(Panda.SAFETY_SUBARU, Panda.FLAG_SUBARU_LONG) self.safety.init_tests() - def _es_brake_msg(self, brake=0): + def _es_brake_msg(self, brake=0, bus=0, aeb=0): values = {"Brake_Pressure": brake} - return self.packer.make_can_msg_panda("ES_Brake", 0, values) + values = {"Cruise_Brake_Active": aeb} + return self.packer.make_can_msg_panda("ES_Brake", bus, values) def _es_distance_msg(self, throttle=0): values = {"Cruise_Throttle": throttle} @@ -106,7 +107,9 @@ def _es_status_msg(self, rpm=0): def test_es_brake_msg(self): self.assertTrue(self._tx(self._es_brake_msg())) self.assertTrue(self._tx(self._es_brake_msg(brake=self.MAX_BRAKE))) - self.assertFalse(self._tx(self._es_brake_msg(brake=self.MAX_BRAKE+1))) + self.assertFalse(self._tx(self._es_brake_msg(brake=self.MAX_BRAKE+10))) + self.assertFalse(self._tx(self._es_brake_msg(brake=self.MAX_BRAKE+10, bus=2, aeb=0))) + self.assertTrue(self._tx(self._es_brake_msg(brake=self.MAX_BRAKE+10, bus=2, aeb=1))) def test_es_distance_msg(self): self.safety.set_controls_allowed(True) From 0de5ee112e7a3255d8c79613ce161e2e4d864394 Mon Sep 17 00:00:00 2001 From: Martin Lillepuu Date: Mon, 12 Dec 2022 07:11:33 +0000 Subject: [PATCH 023/113] Add bus --- board/safety/safety_subaru.h | 1 + 1 file changed, 1 insertion(+) diff --git a/board/safety/safety_subaru.h b/board/safety/safety_subaru.h index a0aa356852..b13e216f9c 100644 --- a/board/safety/safety_subaru.h +++ b/board/safety/safety_subaru.h @@ -172,6 +172,7 @@ static int subaru_tx_hook(CANPacket_t *to_send) { } if (subaru_longitudinal) { + const int bus = GET_BUS(to_push); // check es_brake brake_pressure limits if (addr == 0x220) { bool aeb = (bus == 2) && (GET_BIT(to_send, 38U) == 1); From aef8ab130374a8d33b5f8854310c82514a97d8cb Mon Sep 17 00:00:00 2001 From: Martin Lillepuu Date: Mon, 12 Dec 2022 07:29:56 +0000 Subject: [PATCH 024/113] Update AEB logic --- board/safety/safety_subaru.h | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/board/safety/safety_subaru.h b/board/safety/safety_subaru.h index b13e216f9c..46f4cfc70d 100644 --- a/board/safety/safety_subaru.h +++ b/board/safety/safety_subaru.h @@ -84,6 +84,7 @@ const uint16_t SUBARU_PARAM_LONGITUDINAL = 2; bool subaru_gen2 = false; bool subaru_longitudinal = false; +bool subaru_aeb = false static uint32_t subaru_get_checksum(CANPacket_t *to_push) { return (uint8_t)GET_BYTE(to_push, 0); @@ -120,6 +121,10 @@ static int subaru_rx_hook(CANPacket_t *to_push) { update_sample(&torque_driver, torque_driver_new); } + if ((addr == 0x220) && (bus == 2)) { + subaru_aeb = (GET_BIT(to_send, 38U) == 1) + } + // enter controls on rising edge of ACC, exit controls on ACC off if ((addr == 0x240) && (bus == alt_bus)) { bool cruise_engaged = GET_BIT(to_push, 41U) != 0U; @@ -172,12 +177,10 @@ static int subaru_tx_hook(CANPacket_t *to_send) { } if (subaru_longitudinal) { - const int bus = GET_BUS(to_push); // check es_brake brake_pressure limits if (addr == 0x220) { - bool aeb = (bus == 2) && (GET_BIT(to_send, 38U) == 1); int es_brake_pressure = ((GET_BYTES_04(to_send) >> 16) & 0xFFFFU); - violation |= !aeb && max_limit_check(es_brake_pressure, SUBARU_BRAKE_MAX, SUBARU_BRAKE_MIN); + violation |= !subaru_aeb && max_limit_check(es_brake_pressure, SUBARU_BRAKE_MAX, SUBARU_BRAKE_MIN); } // check es_distance cruise_throttle limits if ((addr == 0x221) && controls_allowed) { From 660b2580a3616a78ff1f89cb7eab325d3ba906e0 Mon Sep 17 00:00:00 2001 From: Martin Lillepuu Date: Mon, 12 Dec 2022 07:55:35 +0000 Subject: [PATCH 025/113] Add GEN2 AEB support --- board/safety/safety_subaru.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/board/safety/safety_subaru.h b/board/safety/safety_subaru.h index 46f4cfc70d..c388ac67ef 100644 --- a/board/safety/safety_subaru.h +++ b/board/safety/safety_subaru.h @@ -112,6 +112,7 @@ static int subaru_rx_hook(CANPacket_t *to_push) { if (valid) { const int bus = GET_BUS(to_push); const int alt_bus = subaru_gen2 ? 1 : 0; + const int alt_bus2 = subaru_gen2 ? 1 : 2; int addr = GET_ADDR(to_push); if ((addr == 0x119) && (bus == 0)) { @@ -121,8 +122,8 @@ static int subaru_rx_hook(CANPacket_t *to_push) { update_sample(&torque_driver, torque_driver_new); } - if ((addr == 0x220) && (bus == 2)) { - subaru_aeb = (GET_BIT(to_send, 38U) == 1) + if ((addr == 0x220) && (bus == alt_bus2)) { + subaru_aeb = (GET_BIT(to_push, 38U) == 1) } // enter controls on rising edge of ACC, exit controls on ACC off From 5fd7c698d50226def0ea24735ec17f84a3e1cb9b Mon Sep 17 00:00:00 2001 From: Martin Lillepuu Date: Mon, 12 Dec 2022 08:09:02 +0000 Subject: [PATCH 026/113] syntax fix --- board/safety/safety_subaru.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/board/safety/safety_subaru.h b/board/safety/safety_subaru.h index c388ac67ef..006fb2c07e 100644 --- a/board/safety/safety_subaru.h +++ b/board/safety/safety_subaru.h @@ -84,7 +84,7 @@ const uint16_t SUBARU_PARAM_LONGITUDINAL = 2; bool subaru_gen2 = false; bool subaru_longitudinal = false; -bool subaru_aeb = false +bool subaru_aeb = false; static uint32_t subaru_get_checksum(CANPacket_t *to_push) { return (uint8_t)GET_BYTE(to_push, 0); @@ -123,7 +123,7 @@ static int subaru_rx_hook(CANPacket_t *to_push) { } if ((addr == 0x220) && (bus == alt_bus2)) { - subaru_aeb = (GET_BIT(to_push, 38U) == 1) + subaru_aeb = GET_BIT(to_push, 38U) != 0U; } // enter controls on rising edge of ACC, exit controls on ACC off From 0575d7a33d2ec2d8f7c6084c9f2eda264f55ea0f Mon Sep 17 00:00:00 2001 From: Martin Lillepuu Date: Mon, 12 Dec 2022 08:48:29 +0000 Subject: [PATCH 027/113] Update AEB tests --- tests/libpanda/safety_helpers.h | 4 ++++ tests/libpanda/safety_helpers.py | 2 ++ tests/safety/test_subaru.py | 13 ++++++------- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/tests/libpanda/safety_helpers.h b/tests/libpanda/safety_helpers.h index 46b05fc7a0..75ef31d88c 100644 --- a/tests/libpanda/safety_helpers.h +++ b/tests/libpanda/safety_helpers.h @@ -144,6 +144,10 @@ void set_honda_fwd_brake(bool c){ honda_fwd_brake = c; } +void set_subaru_aeb(bool c){ + subaru_aeb = c; +} + void init_tests(void){ // get HW_TYPE from env variable set in test.sh if (getenv("HW_TYPE")) { diff --git a/tests/libpanda/safety_helpers.py b/tests/libpanda/safety_helpers.py index a6ba4b0412..42137d00dd 100644 --- a/tests/libpanda/safety_helpers.py +++ b/tests/libpanda/safety_helpers.py @@ -41,6 +41,7 @@ def setup_safety_helpers(ffi): void set_honda_alt_brake_msg(bool c); void set_honda_bosch_long(bool c); int get_honda_hw(void); + void set_subaru_aeb(bool c); """) class PandaSafety: @@ -84,4 +85,5 @@ def set_honda_alt_brake_msg(self, c: bool) -> None: ... def set_honda_bosch_long(self, c: bool) -> None: ... def get_honda_hw(self) -> int: ... + def set_subaru_aeb(self, c: bool) -> None: ... diff --git a/tests/safety/test_subaru.py b/tests/safety/test_subaru.py index dafe51c3dd..1e7cec2225 100755 --- a/tests/safety/test_subaru.py +++ b/tests/safety/test_subaru.py @@ -91,10 +91,9 @@ def setUp(self): self.safety.set_safety_hooks(Panda.SAFETY_SUBARU, Panda.FLAG_SUBARU_LONG) self.safety.init_tests() - def _es_brake_msg(self, brake=0, bus=0, aeb=0): + def _es_brake_msg(self, brake=0): values = {"Brake_Pressure": brake} - values = {"Cruise_Brake_Active": aeb} - return self.packer.make_can_msg_panda("ES_Brake", bus, values) + return self.packer.make_can_msg_panda("ES_Brake", 0, values) def _es_distance_msg(self, throttle=0): values = {"Cruise_Throttle": throttle} @@ -108,17 +107,17 @@ def test_es_brake_msg(self): self.assertTrue(self._tx(self._es_brake_msg())) self.assertTrue(self._tx(self._es_brake_msg(brake=self.MAX_BRAKE))) self.assertFalse(self._tx(self._es_brake_msg(brake=self.MAX_BRAKE+10))) - self.assertFalse(self._tx(self._es_brake_msg(brake=self.MAX_BRAKE+10, bus=2, aeb=0))) - self.assertTrue(self._tx(self._es_brake_msg(brake=self.MAX_BRAKE+10, bus=2, aeb=1))) + self.safety.set_subaru_aeb(1) + self.assertTrue(self._tx(self._es_brake_msg(brake=self.MAX_BRAKE+10))) def test_es_distance_msg(self): - self.safety.set_controls_allowed(True) + self.safety.set_controls_allowed(1) self.assertTrue(self._tx(self._es_distance_msg())) self.assertTrue(self._tx(self._es_distance_msg(throttle=self.MAX_THROTTLE))) self.assertFalse(self._tx(self._es_distance_msg(throttle=self.MAX_THROTTLE+1))) def test_es_status_msg(self): - self.safety.set_controls_allowed(True) + self.safety.set_controls_allowed(1) self.assertTrue(self._tx(self._es_status_msg())) self.assertTrue(self._tx(self._es_status_msg(rpm=self.MAX_RPM))) self.assertFalse(self._tx(self._es_status_msg(rpm=self.MAX_RPM+1))) From 77ddd7fcd0ad635f7c000da98d2aa4134553a10b Mon Sep 17 00:00:00 2001 From: Martin Lillepuu Date: Sun, 5 Mar 2023 13:34:31 +0200 Subject: [PATCH 028/113] Add comment for subaru_aeb --- board/safety/safety_subaru.h | 1 + 1 file changed, 1 insertion(+) diff --git a/board/safety/safety_subaru.h b/board/safety/safety_subaru.h index 006fb2c07e..83beee348f 100644 --- a/board/safety/safety_subaru.h +++ b/board/safety/safety_subaru.h @@ -122,6 +122,7 @@ static int subaru_rx_hook(CANPacket_t *to_push) { update_sample(&torque_driver, torque_driver_new); } + // ES_Brake Cruise_Brake_Active if ((addr == 0x220) && (bus == alt_bus2)) { subaru_aeb = GET_BIT(to_push, 38U) != 0U; } From c1d84c52aceebd50590d8aaf181e9964389d822b Mon Sep 17 00:00:00 2001 From: Martin Lillepuu Date: Mon, 20 Mar 2023 21:00:00 +0200 Subject: [PATCH 029/113] Do not check cruise_throttle and cruise_rpm limits when gas pressed --- board/safety/safety_subaru.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/board/safety/safety_subaru.h b/board/safety/safety_subaru.h index 83beee348f..0e58df7cee 100644 --- a/board/safety/safety_subaru.h +++ b/board/safety/safety_subaru.h @@ -185,12 +185,12 @@ static int subaru_tx_hook(CANPacket_t *to_send) { violation |= !subaru_aeb && max_limit_check(es_brake_pressure, SUBARU_BRAKE_MAX, SUBARU_BRAKE_MIN); } // check es_distance cruise_throttle limits - if ((addr == 0x221) && controls_allowed) { + if ((addr == 0x221) && controls_allowed && !gas_pressed) { int cruise_throttle = ((GET_BYTES_04(to_send) >> 16) & 0xFFFU); violation |= max_limit_check(cruise_throttle, SUBARU_THROTTLE_MAX, SUBARU_THROTTLE_MIN); } // check es_status cruise_rpm limits - if ((addr == 0x222) && controls_allowed) { + if ((addr == 0x222) && controls_allowed && !gas_pressed) { int cruise_rpm = ((GET_BYTES_04(to_send) >> 16) & 0xFFFU); violation |= max_limit_check(cruise_rpm, SUBARU_RPM_MAX, SUBARU_RPM_MIN); } From 4cd4a1211ca0d1c7ff68938ec401f7aca0fab051 Mon Sep 17 00:00:00 2001 From: Adeeb Shihadeh Date: Sat, 15 Apr 2023 10:41:10 -0700 Subject: [PATCH 030/113] use long limits struct --- board/safety/safety_subaru.h | 29 +++++++++++++++++------------ board/safety_declarations.h | 3 +++ 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/board/safety/safety_subaru.h b/board/safety/safety_subaru.h index efe2432525..aca30b62d4 100644 --- a/board/safety/safety_subaru.h +++ b/board/safety/safety_subaru.h @@ -20,14 +20,14 @@ const SteeringLimits SUBARU_GEN2_STEERING_LIMITS = { .type = TorqueDriverLimited, }; -const int SUBARU_BRAKE_MIN = 0; -const int SUBARU_BRAKE_MAX = 400; - -const int SUBARU_THROTTLE_MIN = 0; -const int SUBARU_THROTTLE_MAX = 3400; +const LongitudinalLimits SUBARU_LONG_LIMITS = { + .min_gas = 0, + .max_gas = 3400, + .min_rpm = 0, + .max_rpm = 3200, + .max_brake = 400, +}; -const int SUBARU_RPM_MIN = 0; -const int SUBARU_RPM_MAX = 3200; const CanMsg SUBARU_TX_MSGS[] = { {0x122, 0, 8}, @@ -175,24 +175,29 @@ static int subaru_tx_hook(CANPacket_t *to_send) { if (steer_torque_cmd_checks(desired_torque, -1, limits)) { tx = 0; } - } if (subaru_longitudinal) { // check es_brake brake_pressure limits if (addr == 0x220) { int es_brake_pressure = ((GET_BYTES_04(to_send) >> 16) & 0xFFFFU); - violation |= !subaru_aeb && max_limit_check(es_brake_pressure, SUBARU_BRAKE_MAX, SUBARU_BRAKE_MIN); + violation |= subaru_aeb || longitudinal_brake_checks(es_brake_pressure, SUBARU_LONG_LIMITS); } + // check es_distance cruise_throttle limits if ((addr == 0x221) && controls_allowed && !gas_pressed) { int cruise_throttle = ((GET_BYTES_04(to_send) >> 16) & 0xFFFU); - violation |= max_limit_check(cruise_throttle, SUBARU_THROTTLE_MAX, SUBARU_THROTTLE_MIN); + violation |= longitudinal_gas_checks(cruise_throttle, SUBARU_LONG_LIMITS); } + // check es_status cruise_rpm limits - if ((addr == 0x222) && controls_allowed && !gas_pressed) { + if (addr == 0x222) { int cruise_rpm = ((GET_BYTES_04(to_send) >> 16) & 0xFFFU); - violation |= max_limit_check(cruise_rpm, SUBARU_RPM_MAX, SUBARU_RPM_MIN); + if (get_longitudinal_allowed()) { + violation |= max_limit_check(cruise_rpm, SUBARU_LONG_LIMITS.max_rpm, SUBARU_LONG_LIMITS.min_rpm); + } else { + violation |= cruise_rpm != 0; + } } } diff --git a/board/safety_declarations.h b/board/safety_declarations.h index 6dde0d4a3a..0c6fa760a9 100644 --- a/board/safety_declarations.h +++ b/board/safety_declarations.h @@ -75,6 +75,9 @@ typedef struct { const int inactive_gas; const int max_brake; + const int min_rpm; + const int max_rpm; + // speed cmd limits const int inactive_speed; } LongitudinalLimits; From c0847a6ecd4282224f12c89f11724f4d0b727654 Mon Sep 17 00:00:00 2001 From: Martin Lillepuu Date: Wed, 19 Apr 2023 19:17:19 +0300 Subject: [PATCH 031/113] Subaru: longitudinal checks only when controls_allowed, use ES_LKAS_State LKAS_Alert for AEB --- board/safety/safety_subaru.h | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/board/safety/safety_subaru.h b/board/safety/safety_subaru.h index aca30b62d4..ee5e858313 100644 --- a/board/safety/safety_subaru.h +++ b/board/safety/safety_subaru.h @@ -122,9 +122,10 @@ static int subaru_rx_hook(CANPacket_t *to_push) { update_sample(&torque_driver, torque_driver_new); } - // ES_Brake Cruise_Brake_Active - if ((addr == 0x220) && (bus == alt_bus2)) { - subaru_aeb = GET_BIT(to_push, 38U) != 0U; + // ES_LKAS_State LKAS_Alert: 2, 5, 6 + if ((addr == 0x322) && (bus == alt_bus2)) { + int lkas_alert = ((GET_BYTE(to_push, 4) >> 3) & 5U); + subaru_aeb = (lkas_alert == 2U || lkas_alert == 5U || lkas_alert == 6U); } // enter controls on rising edge of ACC, exit controls on ACC off @@ -139,7 +140,7 @@ static int subaru_rx_hook(CANPacket_t *to_push) { } if ((addr == 0x13c) && (bus == alt_bus)) { - brake_pressed = ((GET_BYTE(to_push, 7) >> 6) & 1U); + brake_pressed = GET_BIT(to_push, 62U) != 0U; } if ((addr == 0x40) && (bus == 0)) { @@ -177,7 +178,7 @@ static int subaru_tx_hook(CANPacket_t *to_send) { } } - if (subaru_longitudinal) { + if (subaru_longitudinal && controls_allowed) { // check es_brake brake_pressure limits if (addr == 0x220) { int es_brake_pressure = ((GET_BYTES_04(to_send) >> 16) & 0xFFFFU); @@ -185,13 +186,13 @@ static int subaru_tx_hook(CANPacket_t *to_send) { } // check es_distance cruise_throttle limits - if ((addr == 0x221) && controls_allowed && !gas_pressed) { + if ((addr == 0x221) && !gas_pressed) { int cruise_throttle = ((GET_BYTES_04(to_send) >> 16) & 0xFFFU); violation |= longitudinal_gas_checks(cruise_throttle, SUBARU_LONG_LIMITS); } // check es_status cruise_rpm limits - if (addr == 0x222) { + if ((addr == 0x222) && !gas_pressed) { int cruise_rpm = ((GET_BYTES_04(to_send) >> 16) & 0xFFFU); if (get_longitudinal_allowed()) { violation |= max_limit_check(cruise_rpm, SUBARU_LONG_LIMITS.max_rpm, SUBARU_LONG_LIMITS.min_rpm); From 7166dd567392a7629fa80c4149ac81a011c2935f Mon Sep 17 00:00:00 2001 From: Martin Lillepuu Date: Wed, 19 Apr 2023 19:31:51 +0300 Subject: [PATCH 032/113] fix misra, set controls_allowed for es_brake test --- board/safety/safety_subaru.h | 2 +- tests/safety/test_subaru.py | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/board/safety/safety_subaru.h b/board/safety/safety_subaru.h index ee5e858313..bb4ab28187 100644 --- a/board/safety/safety_subaru.h +++ b/board/safety/safety_subaru.h @@ -125,7 +125,7 @@ static int subaru_rx_hook(CANPacket_t *to_push) { // ES_LKAS_State LKAS_Alert: 2, 5, 6 if ((addr == 0x322) && (bus == alt_bus2)) { int lkas_alert = ((GET_BYTE(to_push, 4) >> 3) & 5U); - subaru_aeb = (lkas_alert == 2U || lkas_alert == 5U || lkas_alert == 6U); + subaru_aeb = ((lkas_alert == 2U) || (lkas_alert == 5U) || (lkas_alert == 6U)); } // enter controls on rising edge of ACC, exit controls on ACC off diff --git a/tests/safety/test_subaru.py b/tests/safety/test_subaru.py index 1e7cec2225..2b49c8d798 100755 --- a/tests/safety/test_subaru.py +++ b/tests/safety/test_subaru.py @@ -104,6 +104,7 @@ def _es_status_msg(self, rpm=0): return self.packer.make_can_msg_panda("ES_Status", 0, values) def test_es_brake_msg(self): + self.safety.set_controls_allowed(1) self.assertTrue(self._tx(self._es_brake_msg())) self.assertTrue(self._tx(self._es_brake_msg(brake=self.MAX_BRAKE))) self.assertFalse(self._tx(self._es_brake_msg(brake=self.MAX_BRAKE+10))) From adf234c2091d02c72717191d07beb958eaa0c137 Mon Sep 17 00:00:00 2001 From: Martin Lillepuu Date: Sun, 23 Apr 2023 18:54:43 +0300 Subject: [PATCH 033/113] fix misra --- board/safety/safety_subaru.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/board/safety/safety_subaru.h b/board/safety/safety_subaru.h index bb4ab28187..aa23323b90 100644 --- a/board/safety/safety_subaru.h +++ b/board/safety/safety_subaru.h @@ -124,8 +124,8 @@ static int subaru_rx_hook(CANPacket_t *to_push) { // ES_LKAS_State LKAS_Alert: 2, 5, 6 if ((addr == 0x322) && (bus == alt_bus2)) { - int lkas_alert = ((GET_BYTE(to_push, 4) >> 3) & 5U); - subaru_aeb = ((lkas_alert == 2U) || (lkas_alert == 5U) || (lkas_alert == 6U)); + const int lkas_alert = ((GET_BYTE(to_push, 4) >> 3) & 5U); + subaru_aeb = ((lkas_alert == 2) || (lkas_alert == 5) || (lkas_alert == 6)); } // enter controls on rising edge of ACC, exit controls on ACC off From 3de6861ed432abb67fb77ba4d529f52e2678c243 Mon Sep 17 00:00:00 2001 From: Martin Lillepuu Date: Thu, 27 Apr 2023 11:23:40 +0300 Subject: [PATCH 034/113] Fix es_brake violation check --- board/safety/safety_subaru.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/board/safety/safety_subaru.h b/board/safety/safety_subaru.h index aa23323b90..d6303470ad 100644 --- a/board/safety/safety_subaru.h +++ b/board/safety/safety_subaru.h @@ -182,7 +182,7 @@ static int subaru_tx_hook(CANPacket_t *to_send) { // check es_brake brake_pressure limits if (addr == 0x220) { int es_brake_pressure = ((GET_BYTES_04(to_send) >> 16) & 0xFFFFU); - violation |= subaru_aeb || longitudinal_brake_checks(es_brake_pressure, SUBARU_LONG_LIMITS); + violation |= !subaru_aeb && longitudinal_brake_checks(es_brake_pressure, SUBARU_LONG_LIMITS); } // check es_distance cruise_throttle limits From e260a043e005917240c1ee8985802fd99a084b91 Mon Sep 17 00:00:00 2001 From: Martin Lillepuu Date: Thu, 27 Apr 2023 14:56:03 +0300 Subject: [PATCH 035/113] Add 0x323 to longitudinal test --- tests/safety/test_subaru.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/safety/test_subaru.py b/tests/safety/test_subaru.py index dd73fcb63b..b4128f26dc 100755 --- a/tests/safety/test_subaru.py +++ b/tests/safety/test_subaru.py @@ -78,8 +78,8 @@ def setUp(self): self.safety.init_tests() class TestSubaruLongitudinalSafety(TestSubaruSafety): - TX_MSGS = [[0x122, 0], [0x220, 0], [0x221, 0], [0x222, 0], [0x321, 0], [0x322, 0], [0x240, 2], [0x13c, 2]] - FWD_BLACKLISTED_ADDRS = {0: [0x240, 0x13c], 2: [0x122, 0x220, 0x221, 0x222, 0x321, 0x322]} + TX_MSGS = [[0x122, 0], [0x220, 0], [0x221, 0], [0x222, 0], [0x321, 0], [0x322, 0], [0x323, 0], [0x240, 2], [0x13c, 2]] + FWD_BLACKLISTED_ADDRS = {0: [0x240, 0x13c], 2: [0x122, 0x220, 0x221, 0x222, 0x321, 0x322, 0x323]} MAX_RPM = 3200 MAX_BRAKE = 400 From 63da689bd65962e1658f3e3f6a23244917d232b7 Mon Sep 17 00:00:00 2001 From: Martin Lillepuu Date: Fri, 28 Apr 2023 08:52:05 +0300 Subject: [PATCH 036/113] Remove stock fcw from aeb signals --- board/safety/safety_subaru.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/board/safety/safety_subaru.h b/board/safety/safety_subaru.h index 27869dca80..daa4ce74c4 100644 --- a/board/safety/safety_subaru.h +++ b/board/safety/safety_subaru.h @@ -124,10 +124,10 @@ static int subaru_rx_hook(CANPacket_t *to_push) { update_sample(&torque_driver, torque_driver_new); } - // ES_LKAS_State LKAS_Alert: 2, 5, 6 + // ES_LKAS_State LKAS_Alert: 5, 6 if ((addr == 0x322) && (bus == alt_bus2)) { const int lkas_alert = ((GET_BYTE(to_push, 4) >> 3) & 5U); - subaru_aeb = ((lkas_alert == 2) || (lkas_alert == 5) || (lkas_alert == 6)); + subaru_aeb = ((lkas_alert == 5) || (lkas_alert == 6)); } // enter controls on rising edge of ACC, exit controls on ACC off From 0a559fc21376a79d091477589165a3be461eef0d Mon Sep 17 00:00:00 2001 From: Martin Lillepuu Date: Thu, 25 May 2023 07:59:21 +0300 Subject: [PATCH 037/113] Use GET_BYTES --- board/safety/safety_subaru.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/board/safety/safety_subaru.h b/board/safety/safety_subaru.h index 0f74082ce4..170e770bbf 100644 --- a/board/safety/safety_subaru.h +++ b/board/safety/safety_subaru.h @@ -183,19 +183,19 @@ static int subaru_tx_hook(CANPacket_t *to_send) { if (subaru_longitudinal && controls_allowed) { // check es_brake brake_pressure limits if (addr == 0x220) { - int es_brake_pressure = ((GET_BYTES_04(to_send) >> 16) & 0xFFFFU); + int es_brake_pressure = ((GET_BYTES(to_send, 0, 4) >> 16) & 0xFFFFU); violation |= !subaru_aeb && longitudinal_brake_checks(es_brake_pressure, SUBARU_LONG_LIMITS); } // check es_distance cruise_throttle limits if ((addr == 0x221) && !gas_pressed) { - int cruise_throttle = ((GET_BYTES_04(to_send) >> 16) & 0xFFFU); + int cruise_throttle = ((GET_BYTES(to_send, 0, 4) >> 16) & 0xFFFU); violation |= longitudinal_gas_checks(cruise_throttle, SUBARU_LONG_LIMITS); } // check es_status cruise_rpm limits if ((addr == 0x222) && !gas_pressed) { - int cruise_rpm = ((GET_BYTES_04(to_send) >> 16) & 0xFFFU); + int cruise_rpm = ((GET_BYTES(to_send, 0, 4) >> 16) & 0xFFFU); if (get_longitudinal_allowed()) { violation |= max_limit_check(cruise_rpm, SUBARU_LONG_LIMITS.max_rpm, SUBARU_LONG_LIMITS.min_rpm); } else { From 86e8d321d055c5f89cde7ee26a0014df1bcd5b39 Mon Sep 17 00:00:00 2001 From: Martin Lillepuu Date: Sun, 4 Jun 2023 17:20:41 +0300 Subject: [PATCH 038/113] Revert "export FW_PATH" This reverts commit 2a5058d858b36ff7dc5477119a54f0aab137b266. --- __init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/__init__.py b/__init__.py index d3a558f437..8b43e99380 100644 --- a/__init__.py +++ b/__init__.py @@ -1,4 +1,4 @@ -from .python.constants import McuType, BASEDIR, FW_PATH # noqa: F401 +from .python.constants import McuType, BASEDIR # noqa: F401 from .python.serial import PandaSerial # noqa: F401 from .python import (Panda, PandaDFU, # noqa: F401 pack_can_buffer, unpack_can_buffer, calculate_checksum, From 5aae6716fa5506aac49dadd287229a45f0a3b94e Mon Sep 17 00:00:00 2001 From: Martin Lillepuu Date: Sun, 4 Jun 2023 17:21:14 +0300 Subject: [PATCH 039/113] Revert "cleanup fw filename conventions (#1434)" This reverts commit 4dd2735e38b63940ca1e1f42b4522ca4c1a0a1cc. --- python/__init__.py | 7 +++---- python/constants.py | 14 +++++++------- python/dfu.py | 6 ++---- release/make_release.sh | 5 ++++- 4 files changed, 16 insertions(+), 16 deletions(-) diff --git a/python/__init__.py b/python/__init__.py index f92bf27df8..fb1d7784ed 100644 --- a/python/__init__.py +++ b/python/__init__.py @@ -14,7 +14,7 @@ from itertools import accumulate from .base import BaseHandle -from .constants import FW_PATH, McuType +from .constants import McuType from .dfu import PandaDFU from .isotp import isotp_send, isotp_recv from .spi import PandaSpiHandle, PandaSpiException @@ -486,7 +486,7 @@ def flash_static(handle, code, mcu_type): def flash(self, fn=None, code=None, reconnect=True): if not fn: - fn = os.path.join(FW_PATH, self._mcu_type.config.app_fn) + fn = self._mcu_type.config.app_path assert os.path.isfile(fn) logging.debug("flash: main version is %s", self.get_version()) if not self.bootstub: @@ -539,8 +539,7 @@ def wait_for_dfu(dfu_serial: Optional[str], timeout: Optional[int] = None) -> bo def up_to_date(self) -> bool: current = self.get_signature() - fn = os.path.join(FW_PATH, self.get_mcu_type().config.app_fn) - expected = Panda.get_signature_from_firmware(fn) + expected = Panda.get_signature_from_firmware(self.get_mcu_type().config.app_path) return (current == expected) def call_control_api(self, msg): diff --git a/python/constants.py b/python/constants.py index 4c3e778ad1..c55fd2c9b8 100644 --- a/python/constants.py +++ b/python/constants.py @@ -3,7 +3,7 @@ from typing import List, NamedTuple BASEDIR = os.path.join(os.path.dirname(os.path.realpath(__file__)), "../") -FW_PATH = os.path.join(BASEDIR, "board/obj/") + class McuConfig(NamedTuple): mcu: str @@ -13,9 +13,9 @@ class McuConfig(NamedTuple): sector_sizes: List[int] serial_number_address: int app_address: int - app_fn: str + app_path: str bootstub_address: int - bootstub_fn: str + bootstub_path: str Fx = ( 0x1FFF7A10, @@ -23,9 +23,9 @@ class McuConfig(NamedTuple): [0x4000 for _ in range(4)] + [0x10000] + [0x20000 for _ in range(11)], 0x1FFF79C0, 0x8004000, - "panda.bin.signed", + os.path.join(BASEDIR, "board", "obj", "panda.bin.signed"), 0x8000000, - "bootstub.panda.bin", + os.path.join(BASEDIR, "board", "obj", "bootstub.panda.bin"), ) F2Config = McuConfig("STM32F2", 0x411, *Fx) F4Config = McuConfig("STM32F4", 0x463, *Fx) @@ -39,9 +39,9 @@ class McuConfig(NamedTuple): [0x20000 for _ in range(7)], 0x080FFFC0, 0x8020000, - "panda_h7.bin.signed", + os.path.join(BASEDIR, "board", "obj", "panda_h7.bin.signed"), 0x8000000, - "bootstub.panda_h7.bin", + os.path.join(BASEDIR, "board", "obj", "bootstub.panda_h7.bin"), ) @enum.unique diff --git a/python/dfu.py b/python/dfu.py index bebc243ced..f1d20842ba 100644 --- a/python/dfu.py +++ b/python/dfu.py @@ -1,4 +1,3 @@ -import os import usb1 import struct import binascii @@ -7,7 +6,7 @@ from .base import BaseSTBootloaderHandle from .spi import STBootloaderSPIHandle, PandaSpiException from .usb import STBootloaderUSBHandle -from .constants import FW_PATH, McuType +from .constants import McuType class PandaDFU: @@ -113,8 +112,7 @@ def program_bootstub(self, code_bootstub): self._handle.program(self._mcu_type.config.bootstub_address, code_bootstub) def recover(self): - fn = os.path.join(FW_PATH, self._mcu_type.config.bootstub_fn) - with open(fn, "rb") as f: + with open(self._mcu_type.config.bootstub_path, "rb") as f: code = f.read() self.program_bootstub(code) self.reset() diff --git a/release/make_release.sh b/release/make_release.sh index 5628c4998d..150c510736 100755 --- a/release/make_release.sh +++ b/release/make_release.sh @@ -19,4 +19,7 @@ rm obj/* scons -u cd obj RELEASE_NAME=$(awk '{print $1}' version) -zip -j ../../release/panda-$RELEASE_NAME.zip version panda.bin.signed bootstub.panda.bin panda_h7.bin.signed bootstub.panda_h7.bin +rm panda.bin panda_h7.bin +mv panda.bin.signed panda.bin +mv panda_h7.bin.signed panda_h7.bin +zip -j ../../release/panda-$RELEASE_NAME.zip version panda.bin bootstub.panda.bin panda_h7.bin bootstub.panda_h7.bin From 60e6847dcede788610b2652b7a247227b1f49a8a Mon Sep 17 00:00:00 2001 From: Martin Lillepuu Date: Sun, 25 Jun 2023 21:58:08 +0300 Subject: [PATCH 040/113] Revert "Revert "export FW_PATH"" This reverts commit 86e8d321d055c5f89cde7ee26a0014df1bcd5b39. --- __init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/__init__.py b/__init__.py index f24b67111d..21e35b9e98 100644 --- a/__init__.py +++ b/__init__.py @@ -1,4 +1,4 @@ -from .python.constants import McuType, BASEDIR # noqa: F401 +from .python.constants import McuType, BASEDIR, FW_PATH # noqa: F401 from .python.serial import PandaSerial # noqa: F401 from .python import (Panda, PandaDFU, # noqa: F401 pack_can_buffer, unpack_can_buffer, calculate_checksum, unpack_log, From 4d1f681e8357bb754abff1faa5e28736e909008d Mon Sep 17 00:00:00 2001 From: Martin Lillepuu Date: Sun, 25 Jun 2023 22:16:55 +0300 Subject: [PATCH 041/113] Revert "Revert "cleanup fw filename conventions (#1434)"" This reverts commit 5aae6716fa5506aac49dadd287229a45f0a3b94e. --- python/__init__.py | 7 ++++--- python/constants.py | 14 +++++++------- python/dfu.py | 6 ++++-- release/make_release.sh | 5 +---- 4 files changed, 16 insertions(+), 16 deletions(-) diff --git a/python/__init__.py b/python/__init__.py index 2e55e03d09..59a2c4029d 100644 --- a/python/__init__.py +++ b/python/__init__.py @@ -14,7 +14,7 @@ from itertools import accumulate from .base import BaseHandle -from .constants import McuType +from .constants import FW_PATH, McuType from .dfu import PandaDFU from .isotp import isotp_send, isotp_recv from .spi import PandaSpiHandle, PandaSpiException @@ -506,7 +506,7 @@ def flash_static(handle, code, mcu_type): def flash(self, fn=None, code=None, reconnect=True): if not fn: - fn = self._mcu_type.config.app_path + fn = os.path.join(FW_PATH, self._mcu_type.config.app_fn) assert os.path.isfile(fn) logging.debug("flash: main version is %s", self.get_version()) if not self.bootstub: @@ -571,7 +571,8 @@ def wait_for_panda(serial: Optional[str], timeout: int) -> bool: def up_to_date(self) -> bool: current = self.get_signature() - expected = Panda.get_signature_from_firmware(self.get_mcu_type().config.app_path) + fn = os.path.join(FW_PATH, self.get_mcu_type().config.app_fn) + expected = Panda.get_signature_from_firmware(fn) return (current == expected) def call_control_api(self, msg): diff --git a/python/constants.py b/python/constants.py index c55fd2c9b8..4c3e778ad1 100644 --- a/python/constants.py +++ b/python/constants.py @@ -3,7 +3,7 @@ from typing import List, NamedTuple BASEDIR = os.path.join(os.path.dirname(os.path.realpath(__file__)), "../") - +FW_PATH = os.path.join(BASEDIR, "board/obj/") class McuConfig(NamedTuple): mcu: str @@ -13,9 +13,9 @@ class McuConfig(NamedTuple): sector_sizes: List[int] serial_number_address: int app_address: int - app_path: str + app_fn: str bootstub_address: int - bootstub_path: str + bootstub_fn: str Fx = ( 0x1FFF7A10, @@ -23,9 +23,9 @@ class McuConfig(NamedTuple): [0x4000 for _ in range(4)] + [0x10000] + [0x20000 for _ in range(11)], 0x1FFF79C0, 0x8004000, - os.path.join(BASEDIR, "board", "obj", "panda.bin.signed"), + "panda.bin.signed", 0x8000000, - os.path.join(BASEDIR, "board", "obj", "bootstub.panda.bin"), + "bootstub.panda.bin", ) F2Config = McuConfig("STM32F2", 0x411, *Fx) F4Config = McuConfig("STM32F4", 0x463, *Fx) @@ -39,9 +39,9 @@ class McuConfig(NamedTuple): [0x20000 for _ in range(7)], 0x080FFFC0, 0x8020000, - os.path.join(BASEDIR, "board", "obj", "panda_h7.bin.signed"), + "panda_h7.bin.signed", 0x8000000, - os.path.join(BASEDIR, "board", "obj", "bootstub.panda_h7.bin"), + "bootstub.panda_h7.bin", ) @enum.unique diff --git a/python/dfu.py b/python/dfu.py index f1d20842ba..bebc243ced 100644 --- a/python/dfu.py +++ b/python/dfu.py @@ -1,3 +1,4 @@ +import os import usb1 import struct import binascii @@ -6,7 +7,7 @@ from .base import BaseSTBootloaderHandle from .spi import STBootloaderSPIHandle, PandaSpiException from .usb import STBootloaderUSBHandle -from .constants import McuType +from .constants import FW_PATH, McuType class PandaDFU: @@ -112,7 +113,8 @@ def program_bootstub(self, code_bootstub): self._handle.program(self._mcu_type.config.bootstub_address, code_bootstub) def recover(self): - with open(self._mcu_type.config.bootstub_path, "rb") as f: + fn = os.path.join(FW_PATH, self._mcu_type.config.bootstub_fn) + with open(fn, "rb") as f: code = f.read() self.program_bootstub(code) self.reset() diff --git a/release/make_release.sh b/release/make_release.sh index 150c510736..5628c4998d 100755 --- a/release/make_release.sh +++ b/release/make_release.sh @@ -19,7 +19,4 @@ rm obj/* scons -u cd obj RELEASE_NAME=$(awk '{print $1}' version) -rm panda.bin panda_h7.bin -mv panda.bin.signed panda.bin -mv panda_h7.bin.signed panda_h7.bin -zip -j ../../release/panda-$RELEASE_NAME.zip version panda.bin bootstub.panda.bin panda_h7.bin bootstub.panda_h7.bin +zip -j ../../release/panda-$RELEASE_NAME.zip version panda.bin.signed bootstub.panda.bin panda_h7.bin.signed bootstub.panda_h7.bin From b86bba471c32529d4ebc42ad0f0001bdfd36b1c6 Mon Sep 17 00:00:00 2001 From: Justin Newberry Date: Mon, 10 Jul 2023 17:48:34 +0000 Subject: [PATCH 042/113] cleaned up safety --- tests/safety/test_subaru.py | 73 +++++++++++++++++++++++-------------- 1 file changed, 45 insertions(+), 28 deletions(-) diff --git a/tests/safety/test_subaru.py b/tests/safety/test_subaru.py index b4128f26dc..ac75700846 100755 --- a/tests/safety/test_subaru.py +++ b/tests/safety/test_subaru.py @@ -5,14 +5,19 @@ import panda.tests.safety.common as common from panda.tests.safety.common import CANPackerPanda +def lkas_tx_msgs(alt_bus): + return [[0x122, 0], [0x221, alt_bus], [0x321, 0], [0x322, 0], [0x323, 0]] -class TestSubaruSafety(common.PandaSafetyTest, common.DriverTorqueSteeringSafetyTest): - TX_MSGS = [[0x122, 0], [0x221, 0], [0x321, 0], [0x322, 0], [0x323, 0]] - STANDSTILL_THRESHOLD = 0 # kph +def long_tx_msgs(alt_bus): + return [[0x220, alt_bus], [0x222, alt_bus], [0x240, 2], [0x13c, 2]] + + +class TestSubaruSafetyBase(common.PandaSafetyTest, common.DriverTorqueSteeringSafetyTest): + STANDSTILL_THRESHOLD = 0 # kph RELAY_MALFUNCTION_ADDR = 0x122 RELAY_MALFUNCTION_BUS = 0 - FWD_BLACKLISTED_ADDRS = {2: [0x122, 0x321, 0x322, 0x323]} FWD_BUS_LOOKUP = {0: 2, 2: 0} + FWD_BLACKLISTED_ADDRS = {2: [0x122, 0x321, 0x322, 0x323]} MAX_RATE_UP = 50 MAX_RATE_DOWN = 70 @@ -26,10 +31,23 @@ class TestSubaruSafety(common.PandaSafetyTest, common.DriverTorqueSteeringSafety ALT_BUS = 0 + MAX_RATE_UP = 50 + MAX_RATE_DOWN = 70 + MAX_TORQUE = 2047 + + MAX_RPM = 3200 + MAX_BRAKE = 400 + MAX_THROTTLE = 3400 + + @classmethod + def setUpClass(cls): + if "Base" in cls.__name__: + raise unittest.SkipTest + def setUp(self): self.packer = CANPackerPanda("subaru_global_2017_generated") self.safety = libpanda_py.libpanda - self.safety.set_safety_hooks(Panda.SAFETY_SUBARU, 0) + self.safety.set_safety_hooks(Panda.SAFETY_SUBARU, self.FLAGS) self.safety.init_tests() def _set_prev_torque(self, t): @@ -60,50 +78,34 @@ def _user_gas_msg(self, gas): def _pcm_status_msg(self, enable): values = {"Cruise_Activated": enable} - return self.packer.make_can_msg_panda("CruiseControl", self.ALT_BUS, values) + return self.packer.make_can_msg_panda("Cruise_Status", self.ALT_BUS, values) -class TestSubaruGen2Safety(TestSubaruSafety): - TX_MSGS = [[0x122, 0], [0x221, 1], [0x321, 0], [0x322, 0], [0x323, 0]] +class TestSubaruGen2SafetyBase(TestSubaruSafetyBase): ALT_BUS = 1 MAX_RATE_UP = 40 MAX_RATE_DOWN = 40 MAX_TORQUE = 1000 - def setUp(self): - self.packer = CANPackerPanda("subaru_global_2017_generated") - self.safety = libpanda_py.libpanda - self.safety.set_safety_hooks(Panda.SAFETY_SUBARU, Panda.FLAG_SUBARU_GEN2) - self.safety.init_tests() -class TestSubaruLongitudinalSafety(TestSubaruSafety): - TX_MSGS = [[0x122, 0], [0x220, 0], [0x221, 0], [0x222, 0], [0x321, 0], [0x322, 0], [0x323, 0], [0x240, 2], [0x13c, 2]] +class TestSubaruLongitudinalSafetyBase(TestSubaruSafetyBase): FWD_BLACKLISTED_ADDRS = {0: [0x240, 0x13c], 2: [0x122, 0x220, 0x221, 0x222, 0x321, 0x322, 0x323]} - MAX_RPM = 3200 - MAX_BRAKE = 400 - MAX_THROTTLE = 3400 - - def setUp(self): - self.packer = CANPackerPanda("subaru_global_2017_generated") - self.safety = libpanda_py.libpanda - self.safety.set_safety_hooks(Panda.SAFETY_SUBARU, Panda.FLAG_SUBARU_LONG) - self.safety.init_tests() - def _es_brake_msg(self, brake=0): values = {"Brake_Pressure": brake} - return self.packer.make_can_msg_panda("ES_Brake", 0, values) + return self.packer.make_can_msg_panda("ES_Brake", self.ALT_BUS, values) def _es_distance_msg(self, throttle=0): values = {"Cruise_Throttle": throttle} - return self.packer.make_can_msg_panda("ES_Distance", 0, values) + return self.packer.make_can_msg_panda("ES_Distance", self.ALT_BUS, values) def _es_status_msg(self, rpm=0): values = {"Cruise_RPM": rpm} - return self.packer.make_can_msg_panda("ES_Status", 0, values) + return self.packer.make_can_msg_panda("ES_Status", self.ALT_BUS, values) def test_es_brake_msg(self): + self.safety.set_subaru_aeb(0) self.safety.set_controls_allowed(1) self.assertTrue(self._tx(self._es_brake_msg())) self.assertTrue(self._tx(self._es_brake_msg(brake=self.MAX_BRAKE))) @@ -124,5 +126,20 @@ def test_es_status_msg(self): self.assertFalse(self._tx(self._es_status_msg(rpm=self.MAX_RPM+1))) +class TestSubaruGen1Safety(TestSubaruSafetyBase): + FLAGS = 0 + TX_MSGS = lkas_tx_msgs(0) + + +class TestSubaruGen2Safety(TestSubaruGen2SafetyBase): + FLAGS = Panda.FLAG_SUBARU_GEN2 + TX_MSGS = lkas_tx_msgs(1) + + +class TestSubaruGen1LongitudinalSafety(TestSubaruLongitudinalSafetyBase): + FLAGS = Panda.FLAG_SUBARU_LONG + TX_MSGS = lkas_tx_msgs(0) + long_tx_msgs(0) + + if __name__ == "__main__": unittest.main() From d0a5abd2b38b403eb567e7004463ee4fb402479f Mon Sep 17 00:00:00 2001 From: Justin Newberry Date: Mon, 10 Jul 2023 15:22:43 -0700 Subject: [PATCH 043/113] cleanup subaru long safety --- board/safety/safety_subaru.h | 64 +++++++++++---------- tests/libpanda/safety_helpers.h | 4 ++ tests/libpanda/safety_helpers.py | 2 + tests/safety/common.py | 74 +++++++++++++++++++++++- tests/safety/test_subaru.py | 98 ++++++++++++++++++++------------ 5 files changed, 173 insertions(+), 69 deletions(-) diff --git a/board/safety/safety_subaru.h b/board/safety/safety_subaru.h index 7c296ba3e6..847a9c68ad 100644 --- a/board/safety/safety_subaru.h +++ b/board/safety/safety_subaru.h @@ -46,33 +46,34 @@ const LongitudinalLimits SUBARU_LONG_LIMITS = { #define SUBARU_ALT_BUS 1 #define SUBARU_CAM_BUS 2 + +#define SUBARU_COMMON_TX_MSGS(alt_bus) \ + {MSG_SUBARU_ES_LKAS, SUBARU_MAIN_BUS, 8}, \ + {MSG_SUBARU_ES_Distance, alt_bus, 8}, \ + {MSG_SUBARU_ES_DashStatus, SUBARU_MAIN_BUS, 8}, \ + {MSG_SUBARU_ES_LKAS_State, SUBARU_MAIN_BUS, 8}, \ + {MSG_SUBARU_ES_Infotainment, SUBARU_MAIN_BUS, 8}, \ + +#define SUBARU_LONG_TX_MSGS(alt_bus) \ + {MSG_SUBARU_ES_Brake, SUBARU_MAIN_BUS, 8}, \ + {MSG_SUBARU_ES_Status, SUBARU_MAIN_BUS, 8}, \ + {MSG_SUBARU_Brake_Status, SUBARU_CAM_BUS, 8}, \ + {MSG_SUBARU_CruiseControl, SUBARU_CAM_BUS, 8}, \ + + const CanMsg SUBARU_TX_MSGS[] = { - {MSG_SUBARU_ES_LKAS, SUBARU_MAIN_BUS, 8}, - {MSG_SUBARU_ES_Distance, SUBARU_MAIN_BUS, 8}, - {MSG_SUBARU_ES_DashStatus, SUBARU_MAIN_BUS, 8}, - {MSG_SUBARU_ES_LKAS_State, SUBARU_MAIN_BUS, 8}, - {MSG_SUBARU_ES_Infotainment, SUBARU_MAIN_BUS, 8}, + SUBARU_COMMON_TX_MSGS(0) }; #define SUBARU_TX_MSGS_LEN (sizeof(SUBARU_TX_MSGS) / sizeof(SUBARU_TX_MSGS[0])) const CanMsg SUBARU_LONG_TX_MSGS[] = { - {0x122, 0, 8}, - {0x220, 0, 8}, - {0x221, 0, 8}, - {0x222, 0, 8}, - {0x321, 0, 8}, - {0x322, 0, 8}, - {0x13c, 2, 8}, - {0x240, 2, 8} + SUBARU_COMMON_TX_MSGS(0) + SUBARU_LONG_TX_MSGS(0) }; #define SUBARU_LONG_TX_MSGS_LEN (sizeof(SUBARU_LONG_TX_MSGS) / sizeof(SUBARU_LONG_TX_MSGS[0])) const CanMsg SUBARU_GEN2_TX_MSGS[] = { - {MSG_SUBARU_ES_LKAS, SUBARU_MAIN_BUS, 8}, - {MSG_SUBARU_ES_Distance, SUBARU_ALT_BUS, 8}, - {MSG_SUBARU_ES_DashStatus, SUBARU_MAIN_BUS, 8}, - {MSG_SUBARU_ES_LKAS_State, SUBARU_MAIN_BUS, 8}, - {MSG_SUBARU_ES_Infotainment, SUBARU_MAIN_BUS, 8} + SUBARU_COMMON_TX_MSGS(1) }; #define SUBARU_GEN2_TX_MSGS_LEN (sizeof(SUBARU_GEN2_TX_MSGS) / sizeof(SUBARU_GEN2_TX_MSGS[0])) @@ -130,8 +131,8 @@ static int subaru_rx_hook(CANPacket_t *to_push) { if (valid) { const int bus = GET_BUS(to_push); - const int alt_bus = subaru_gen2 ? SUBARU_ALT_BUS : SUBARU_MAIN_BUS; - const int alt_bus2 = subaru_gen2 ? SUBARU_ALT_BUS : SUBARU_CAM_BUS; + const int alt_main_bus = subaru_gen2 ? SUBARU_ALT_BUS : SUBARU_MAIN_BUS; + const int alt_cam_bus = subaru_gen2 ? SUBARU_ALT_BUS : SUBARU_CAM_BUS; int addr = GET_ADDR(to_push); if ((addr == MSG_SUBARU_Steering_Torque) && (bus == SUBARU_MAIN_BUS)) { @@ -141,24 +142,23 @@ static int subaru_rx_hook(CANPacket_t *to_push) { update_sample(&torque_driver, torque_driver_new); } - // ES_LKAS_State LKAS_Alert: 5, 6 - if ((addr == 0x322) && (bus == alt_bus2)) { - const int lkas_alert = ((GET_BYTE(to_push, 4) >> 3) & 5U); - subaru_aeb = ((lkas_alert == 5) || (lkas_alert == 6)); + if ((addr == MSG_SUBARU_ES_Brake) && (bus == alt_cam_bus)) { + const int aeb_status = (GET_BYTE(to_push, 4) & 0xFU); + subaru_aeb = aeb_status == 8; } // enter controls on rising edge of ACC, exit controls on ACC off - if ((addr == MSG_SUBARU_CruiseControl) && (bus == alt_bus)) { + if ((addr == MSG_SUBARU_CruiseControl) && (bus == alt_main_bus)) { bool cruise_engaged = GET_BIT(to_push, 41U) != 0U; pcm_cruise_check(cruise_engaged); } // update vehicle moving with any non-zero wheel speed - if ((addr == MSG_SUBARU_Wheel_Speeds) && (bus == alt_bus)) { + if ((addr == MSG_SUBARU_Wheel_Speeds) && (bus == alt_main_bus)) { vehicle_moving = ((GET_BYTES(to_push, 0, 4) >> 12) != 0U) || (GET_BYTES(to_push, 4, 4) != 0U); } - if ((addr == MSG_SUBARU_Brake_Status) && (bus == alt_bus)) { + if ((addr == MSG_SUBARU_Brake_Status) && (bus == alt_main_bus)) { brake_pressed = GET_BIT(to_push, 62U) != 0U; } @@ -197,21 +197,21 @@ static int subaru_tx_hook(CANPacket_t *to_send) { } } - if (subaru_longitudinal && controls_allowed) { + if (subaru_longitudinal) { // check es_brake brake_pressure limits - if (addr == 0x220) { + if (addr == MSG_SUBARU_ES_Brake) { int es_brake_pressure = ((GET_BYTES(to_send, 0, 4) >> 16) & 0xFFFFU); violation |= !subaru_aeb && longitudinal_brake_checks(es_brake_pressure, SUBARU_LONG_LIMITS); } // check es_distance cruise_throttle limits - if ((addr == 0x221) && !gas_pressed) { + if ((addr == MSG_SUBARU_ES_Distance) && !gas_pressed) { int cruise_throttle = ((GET_BYTES(to_send, 0, 4) >> 16) & 0xFFFU); violation |= longitudinal_gas_checks(cruise_throttle, SUBARU_LONG_LIMITS); } // check es_status cruise_rpm limits - if ((addr == 0x222) && !gas_pressed) { + if ((addr == MSG_SUBARU_ES_Status) && !gas_pressed) { int cruise_rpm = ((GET_BYTES(to_send, 0, 4) >> 16) & 0xFFFU); if (get_longitudinal_allowed()) { violation |= max_limit_check(cruise_rpm, SUBARU_LONG_LIMITS.max_rpm, SUBARU_LONG_LIMITS.min_rpm); @@ -244,9 +244,11 @@ static int subaru_fwd_hook(int bus_num, int addr) { (addr == MSG_SUBARU_ES_DashStatus) || (addr == MSG_SUBARU_ES_LKAS_State) || (addr == MSG_SUBARU_ES_Infotainment)); + bool block_long = ((addr == MSG_SUBARU_ES_Brake) || (addr == MSG_SUBARU_ES_Distance) || (addr == MSG_SUBARU_ES_Status)); + bool block_msg = block_lkas || (subaru_longitudinal && block_long); if (!block_msg) { bus_fwd = SUBARU_MAIN_BUS; // Main CAN diff --git a/tests/libpanda/safety_helpers.h b/tests/libpanda/safety_helpers.h index a7100896e6..6cf252f3e6 100644 --- a/tests/libpanda/safety_helpers.h +++ b/tests/libpanda/safety_helpers.h @@ -172,6 +172,10 @@ void set_subaru_aeb(bool c){ subaru_aeb = c; } +bool get_subaru_aeb(){ + return subaru_aeb; +} + void init_tests(void){ // get HW_TYPE from env variable set in test.sh if (getenv("HW_TYPE")) { diff --git a/tests/libpanda/safety_helpers.py b/tests/libpanda/safety_helpers.py index a8e5b40fce..992651d4d4 100644 --- a/tests/libpanda/safety_helpers.py +++ b/tests/libpanda/safety_helpers.py @@ -49,6 +49,7 @@ def setup_safety_helpers(ffi): void set_honda_bosch_long(bool c); int get_honda_hw(void); void set_subaru_aeb(bool c); + bool get_subaru_aeb(); """) class PandaSafety(Protocol): @@ -99,4 +100,5 @@ def set_honda_bosch_long(self, c: bool) -> None: ... def get_honda_hw(self) -> int: ... def set_subaru_aeb(self, c: bool) -> None: ... + def get_subaru_aeb(self) -> bool: ... diff --git a/tests/safety/common.py b/tests/safety/common.py index ebb416a345..f42ccde2df 100644 --- a/tests/safety/common.py +++ b/tests/safety/common.py @@ -137,7 +137,6 @@ def test_gas_interceptor_safety_check(self): class LongitudinalAccelSafetyTest(PandaSafetyTestBase, abc.ABC): - MAX_ACCEL: float = 2.0 MIN_ACCEL: float = -3.5 INACTIVE_ACCEL: float = 0.0 @@ -175,6 +174,77 @@ def test_accel_actuation_limits(self, stock_longitudinal=False): self.assertEqual(should_tx, self._tx(self._accel_msg(accel))) +class LongitudinalGasBrakeSafetyTest(PandaSafetyTestBase, abc.ABC): + MIN_BRAKE: int = 0 + MAX_BRAKE: int = None + + MIN_RPM: int = 0 + MAX_RPM: int = None + + MIN_THROTTLE: int = 0 + MAX_THROTTLE: int = None + + @classmethod + def setUpClass(cls): + if cls.__name__ == "LongitudinalGasBrakeSafetyTest": + cls.safety = None + raise unittest.SkipTest + + @abc.abstractmethod + def _throttle_msg(self, throttle: int): + pass + + @abc.abstractmethod + def _rpm_msg(self, rpm: int): + pass + + @abc.abstractmethod + def _brake_msg(self, brake: int): + pass + + def test_gas_brake_limits_correct(self): + # Assert that max brake, and at least one of max rpm or max throttle is set + self.assertTrue(self.MAX_BRAKE is not None) + self.assertTrue(self.MAX_RPM is not None or self.MAX_THROTTLE is not None) + + self.assertGreater(self.MAX_BRAKE, self.MIN_BRAKE) + self.assertGreater(self.MAX_RPM, self.MIN_RPM) + self.assertGreater(self.MAX_THROTTLE, self.MIN_THROTTLE) + + def test_throttle_safety_check(self): + for enabled in [0, 1]: + for g in range(int(self.MIN_THROTTLE * 1.5), int(self.MAX_THROTTLE * 1.5)): + self.safety.set_controls_allowed(enabled) + if g > self.MAX_THROTTLE or (not enabled and abs(g) > 0): + self.assertFalse(self._tx(self._throttle_msg(g))) + else: + self.assertTrue(self._tx(self._throttle_msg(g))) + + def test_rpm_safety_check(self): + if self.MAX_RPM is None: + raise unittest.SkipTest + + for enabled in [0, 1]: + for r in range(int(self.MIN_RPM), int(self.MAX_RPM * 1.2)): + self.safety.set_controls_allowed(enabled) + if r > self.MAX_RPM or (not enabled and abs(r) > 0): + self.assertFalse(self._tx(self._rpm_msg(r))) + else: + self.assertTrue(self._tx(self._rpm_msg(r))) + + def test_throttle_safety_check(self): + if self.MAX_THROTTLE is None: + raise unittest.SkipTest + + for enabled in [0, 1]: + for t in range(int(self.MIN_THROTTLE), int(self.MAX_THROTTLE * 1.2)): + self.safety.set_controls_allowed(enabled) + if t > self.MAX_THROTTLE or (not enabled and abs(t) > 0): + self.assertFalse(self._tx(self._throttle_msg(t)), msg=f"{t} throttle was allowed when {enabled}") + else: + self.assertTrue(self._tx(self._throttle_msg(t))) + + class TorqueSteeringSafetyTestBase(PandaSafetyTestBase, abc.ABC): MAX_RATE_UP = 0 @@ -894,7 +964,7 @@ def test_tx_hook_on_wrong_safety_mode(self): continue if attr.startswith('TestToyota') and current_test.startswith('TestToyota'): continue - if {attr, current_test}.issubset({'TestSubaruSafety', 'TestSubaruGen2Safety', 'TestSubaruLongitudinalSafety'}): + if {attr, current_test}.issubset({'TestSubaruGen1Safety', 'TestSubaruGen2Safety', 'TestSubaruGen1LongitudinalSafety'}): continue if {attr, current_test}.issubset({'TestVolkswagenPqSafety', 'TestVolkswagenPqStockSafety', 'TestVolkswagenPqLongSafety'}): continue diff --git a/tests/safety/test_subaru.py b/tests/safety/test_subaru.py index ac75700846..87e26de1ac 100755 --- a/tests/safety/test_subaru.py +++ b/tests/safety/test_subaru.py @@ -5,19 +5,41 @@ import panda.tests.safety.common as common from panda.tests.safety.common import CANPackerPanda + +MSG_SUBARU_Brake_Status = 0x13c +MSG_SUBARU_CruiseControl = 0x240 +MSG_SUBARU_Throttle = 0x40 +MSG_SUBARU_Steering_Torque = 0x119 +MSG_SUBARU_Wheel_Speeds = 0x13a +MSG_SUBARU_ES_LKAS = 0x122 +MSG_SUBARU_ES_Brake = 0x220 +MSG_SUBARU_ES_Distance = 0x221 +MSG_SUBARU_ES_Status = 0x222 +MSG_SUBARU_ES_DashStatus = 0x321 +MSG_SUBARU_ES_LKAS_State = 0x322 +MSG_SUBARU_ES_Infotainment = 0x323 + + def lkas_tx_msgs(alt_bus): - return [[0x122, 0], [0x221, alt_bus], [0x321, 0], [0x322, 0], [0x323, 0]] + return [[MSG_SUBARU_ES_LKAS, 0], + [MSG_SUBARU_ES_Distance, alt_bus], + [MSG_SUBARU_ES_DashStatus, 0], + [MSG_SUBARU_ES_LKAS_State, 0], + [MSG_SUBARU_ES_Infotainment, 0]] -def long_tx_msgs(alt_bus): - return [[0x220, alt_bus], [0x222, alt_bus], [0x240, 2], [0x13c, 2]] +def long_tx_msgs(): + return [[MSG_SUBARU_ES_Brake, 0], + [MSG_SUBARU_ES_Status, 0], + [MSG_SUBARU_CruiseControl, 2], + [MSG_SUBARU_Brake_Status, 2]] class TestSubaruSafetyBase(common.PandaSafetyTest, common.DriverTorqueSteeringSafetyTest): STANDSTILL_THRESHOLD = 0 # kph - RELAY_MALFUNCTION_ADDR = 0x122 + RELAY_MALFUNCTION_ADDR = MSG_SUBARU_ES_LKAS RELAY_MALFUNCTION_BUS = 0 FWD_BUS_LOOKUP = {0: 2, 2: 0} - FWD_BLACKLISTED_ADDRS = {2: [0x122, 0x321, 0x322, 0x323]} + FWD_BLACKLISTED_ADDRS = {2: [MSG_SUBARU_ES_LKAS, MSG_SUBARU_ES_DashStatus, MSG_SUBARU_ES_LKAS_State, MSG_SUBARU_ES_Infotainment]} MAX_RATE_UP = 50 MAX_RATE_DOWN = 70 @@ -35,13 +57,11 @@ class TestSubaruSafetyBase(common.PandaSafetyTest, common.DriverTorqueSteeringSa MAX_RATE_DOWN = 70 MAX_TORQUE = 2047 - MAX_RPM = 3200 - MAX_BRAKE = 400 - MAX_THROTTLE = 3400 - @classmethod def setUpClass(cls): - if "Base" in cls.__name__: + if cls.__name__.endswith("Base"): + cls.packer = None + cls.safety = None raise unittest.SkipTest def setUp(self): @@ -78,7 +98,7 @@ def _user_gas_msg(self, gas): def _pcm_status_msg(self, enable): values = {"Cruise_Activated": enable} - return self.packer.make_can_msg_panda("Cruise_Status", self.ALT_BUS, values) + return self.packer.make_can_msg_panda("CruiseControl", self.ALT_BUS, values) class TestSubaruGen2SafetyBase(TestSubaruSafetyBase): @@ -89,41 +109,47 @@ class TestSubaruGen2SafetyBase(TestSubaruSafetyBase): MAX_TORQUE = 1000 -class TestSubaruLongitudinalSafetyBase(TestSubaruSafetyBase): - FWD_BLACKLISTED_ADDRS = {0: [0x240, 0x13c], 2: [0x122, 0x220, 0x221, 0x222, 0x321, 0x322, 0x323]} +class TestSubaruLongitudinalSafetyBase(TestSubaruSafetyBase, common.LongitudinalGasBrakeSafetyTest): + MAX_RPM = 3200 + MAX_THROTTLE = 3400 + MAX_BRAKE = 400 + + FWD_BLACKLISTED_ADDRS = {0: [MSG_SUBARU_CruiseControl, MSG_SUBARU_Brake_Status], + 2: [MSG_SUBARU_ES_LKAS, MSG_SUBARU_ES_Brake, MSG_SUBARU_ES_Distance, + MSG_SUBARU_ES_Status, MSG_SUBARU_ES_DashStatus, + MSG_SUBARU_ES_LKAS_State, MSG_SUBARU_ES_Infotainment]} - def _es_brake_msg(self, brake=0): - values = {"Brake_Pressure": brake} - return self.packer.make_can_msg_panda("ES_Brake", self.ALT_BUS, values) + def _brake_msg(self, brake, aeb=False, bus=None): + if bus is None: bus = self.ALT_BUS + values = {"Brake_Pressure": brake, "AEB_Status": 8 if aeb else 0} + return self.packer.make_can_msg_panda("ES_Brake", bus, values) - def _es_distance_msg(self, throttle=0): + def _throttle_msg(self, throttle): values = {"Cruise_Throttle": throttle} return self.packer.make_can_msg_panda("ES_Distance", self.ALT_BUS, values) - def _es_status_msg(self, rpm=0): + def _rpm_msg(self, rpm): values = {"Cruise_RPM": rpm} return self.packer.make_can_msg_panda("ES_Status", self.ALT_BUS, values) - def test_es_brake_msg(self): - self.safety.set_subaru_aeb(0) - self.safety.set_controls_allowed(1) - self.assertTrue(self._tx(self._es_brake_msg())) - self.assertTrue(self._tx(self._es_brake_msg(brake=self.MAX_BRAKE))) - self.assertFalse(self._tx(self._es_brake_msg(brake=self.MAX_BRAKE+10))) - self.safety.set_subaru_aeb(1) - self.assertTrue(self._tx(self._es_brake_msg(brake=self.MAX_BRAKE+10))) + def _aeb_msg(self, aeb): + return self._brake_msg(5, aeb, bus=2) - def test_es_distance_msg(self): + def test_aeb_passthrough(self): + # Allow higher braking when AEB is triggered self.safety.set_controls_allowed(1) - self.assertTrue(self._tx(self._es_distance_msg())) - self.assertTrue(self._tx(self._es_distance_msg(throttle=self.MAX_THROTTLE))) - self.assertFalse(self._tx(self._es_distance_msg(throttle=self.MAX_THROTTLE+1))) - def test_es_status_msg(self): - self.safety.set_controls_allowed(1) - self.assertTrue(self._tx(self._es_status_msg())) - self.assertTrue(self._tx(self._es_status_msg(rpm=self.MAX_RPM))) - self.assertFalse(self._tx(self._es_status_msg(rpm=self.MAX_RPM+1))) + self.assertFalse(self.safety.get_subaru_aeb()) + self._rx(self._aeb_msg(False)) + self.assertFalse(self.safety.get_subaru_aeb()) + + self.assertTrue(self._tx(self._brake_msg(brake=self.MAX_BRAKE))) + self.assertFalse(self._tx(self._brake_msg(brake=self.MAX_BRAKE+10))) + + self._rx(self._aeb_msg(True)) + self.assertTrue(self.safety.get_subaru_aeb()) + + self.assertTrue(self._tx(self._brake_msg(brake=self.MAX_BRAKE+10))) class TestSubaruGen1Safety(TestSubaruSafetyBase): @@ -138,7 +164,7 @@ class TestSubaruGen2Safety(TestSubaruGen2SafetyBase): class TestSubaruGen1LongitudinalSafety(TestSubaruLongitudinalSafetyBase): FLAGS = Panda.FLAG_SUBARU_LONG - TX_MSGS = lkas_tx_msgs(0) + long_tx_msgs(0) + TX_MSGS = lkas_tx_msgs(0) + long_tx_msgs() if __name__ == "__main__": From 3705973bc27e6761de49bae0aa5c8e263ab6ac6a Mon Sep 17 00:00:00 2001 From: Justin Newberry Date: Mon, 10 Jul 2023 16:18:40 -0700 Subject: [PATCH 044/113] subaru_aeb -> stock_aeb for other platforms --- board/safety/safety_subaru.h | 5 ++--- board/safety_declarations.h | 3 +++ tests/libpanda/safety_helpers.h | 8 ++++---- tests/libpanda/safety_helpers.py | 8 ++++---- tests/safety/common.py | 31 ++++++++++++++++--------------- tests/safety/test_subaru.py | 6 +++--- 6 files changed, 32 insertions(+), 29 deletions(-) diff --git a/board/safety/safety_subaru.h b/board/safety/safety_subaru.h index 847a9c68ad..dcc5d0fb5e 100644 --- a/board/safety/safety_subaru.h +++ b/board/safety/safety_subaru.h @@ -104,7 +104,6 @@ const uint16_t SUBARU_PARAM_LONGITUDINAL = 2; bool subaru_gen2 = false; bool subaru_longitudinal = false; -bool subaru_aeb = false; static uint32_t subaru_get_checksum(CANPacket_t *to_push) { return (uint8_t)GET_BYTE(to_push, 0); @@ -144,7 +143,7 @@ static int subaru_rx_hook(CANPacket_t *to_push) { if ((addr == MSG_SUBARU_ES_Brake) && (bus == alt_cam_bus)) { const int aeb_status = (GET_BYTE(to_push, 4) & 0xFU); - subaru_aeb = aeb_status == 8; + stock_aeb = (aeb_status == 8); } // enter controls on rising edge of ACC, exit controls on ACC off @@ -201,7 +200,7 @@ static int subaru_tx_hook(CANPacket_t *to_send) { // check es_brake brake_pressure limits if (addr == MSG_SUBARU_ES_Brake) { int es_brake_pressure = ((GET_BYTES(to_send, 0, 4) >> 16) & 0xFFFFU); - violation |= !subaru_aeb && longitudinal_brake_checks(es_brake_pressure, SUBARU_LONG_LIMITS); + violation |= !stock_aeb && longitudinal_brake_checks(es_brake_pressure, SUBARU_LONG_LIMITS); } // check es_distance cruise_throttle limits diff --git a/board/safety_declarations.h b/board/safety_declarations.h index 8bd9357ebd..5c55a8081f 100644 --- a/board/safety_declarations.h +++ b/board/safety_declarations.h @@ -223,6 +223,9 @@ uint32_t ts_angle_last = 0; int desired_angle_last = 0; struct sample_t angle_meas; // last 6 steer angles/curvatures +// for safety modes that need to passthru aeb +bool stock_aeb = false; + // This can be set with a USB command // It enables features that allow alternative experiences, like not disengaging on gas press // It is only either 0 or 1 on mainline comma.ai openpilot diff --git a/tests/libpanda/safety_helpers.h b/tests/libpanda/safety_helpers.h index 6cf252f3e6..9fee1953c0 100644 --- a/tests/libpanda/safety_helpers.h +++ b/tests/libpanda/safety_helpers.h @@ -168,12 +168,12 @@ void set_honda_fwd_brake(bool c){ honda_fwd_brake = c; } -void set_subaru_aeb(bool c){ - subaru_aeb = c; +void set_stock_aeb(bool c){ + stock_aeb = c; } -bool get_subaru_aeb(){ - return subaru_aeb; +bool get_stock_aeb(){ + return stock_aeb; } void init_tests(void){ diff --git a/tests/libpanda/safety_helpers.py b/tests/libpanda/safety_helpers.py index 992651d4d4..7be88ea6bf 100644 --- a/tests/libpanda/safety_helpers.py +++ b/tests/libpanda/safety_helpers.py @@ -21,6 +21,8 @@ def setup_safety_helpers(ffi): int get_vehicle_speed_max(void); int get_current_safety_mode(void); int get_current_safety_param(void); + void set_stock_aeb(bool c); + bool get_stock_aeb(); void set_torque_meas(int min, int max); int get_torque_meas_min(void); @@ -48,8 +50,6 @@ def setup_safety_helpers(ffi): void set_honda_alt_brake_msg(bool c); void set_honda_bosch_long(bool c); int get_honda_hw(void); - void set_subaru_aeb(bool c); - bool get_subaru_aeb(); """) class PandaSafety(Protocol): @@ -99,6 +99,6 @@ def set_honda_alt_brake_msg(self, c: bool) -> None: ... def set_honda_bosch_long(self, c: bool) -> None: ... def get_honda_hw(self) -> int: ... - def set_subaru_aeb(self, c: bool) -> None: ... - def get_subaru_aeb(self) -> bool: ... + def set_stock_aeb(self, c: bool) -> None: ... + def get_stock_aeb(self) -> bool: ... diff --git a/tests/safety/common.py b/tests/safety/common.py index f42ccde2df..6a46f9af40 100644 --- a/tests/safety/common.py +++ b/tests/safety/common.py @@ -175,21 +175,21 @@ def test_accel_actuation_limits(self, stock_longitudinal=False): class LongitudinalGasBrakeSafetyTest(PandaSafetyTestBase, abc.ABC): - MIN_BRAKE: int = 0 - MAX_BRAKE: int = None + MIN_BRAKE = 0 + MAX_BRAKE = None - MIN_RPM: int = 0 - MAX_RPM: int = None + MIN_RPM = 0 + MAX_RPM = None - MIN_THROTTLE: int = 0 - MAX_THROTTLE: int = None + MIN_THROTTLE = 0 + MAX_THROTTLE = None @classmethod def setUpClass(cls): if cls.__name__ == "LongitudinalGasBrakeSafetyTest": cls.safety = None raise unittest.SkipTest - + @abc.abstractmethod def _throttle_msg(self, throttle: int): pass @@ -211,21 +211,22 @@ def test_gas_brake_limits_correct(self): self.assertGreater(self.MAX_RPM, self.MIN_RPM) self.assertGreater(self.MAX_THROTTLE, self.MIN_THROTTLE) - def test_throttle_safety_check(self): + def test_brake_safety_check(self): + self.safety.set_stock_aeb(0) for enabled in [0, 1]: - for g in range(int(self.MIN_THROTTLE * 1.5), int(self.MAX_THROTTLE * 1.5)): + for b in range(self.MIN_BRAKE, int(self.MAX_BRAKE * 1.5)): self.safety.set_controls_allowed(enabled) - if g > self.MAX_THROTTLE or (not enabled and abs(g) > 0): - self.assertFalse(self._tx(self._throttle_msg(g))) + if b > self.MAX_BRAKE or (not enabled and abs(b) > 0): + self.assertFalse(self._tx(self._brake_msg(b))) else: - self.assertTrue(self._tx(self._throttle_msg(g))) + self.assertTrue(self._tx(self._brake_msg(b))) def test_rpm_safety_check(self): if self.MAX_RPM is None: raise unittest.SkipTest for enabled in [0, 1]: - for r in range(int(self.MIN_RPM), int(self.MAX_RPM * 1.2)): + for r in range(self.MIN_RPM, int(self.MAX_RPM * 1.2)): self.safety.set_controls_allowed(enabled) if r > self.MAX_RPM or (not enabled and abs(r) > 0): self.assertFalse(self._tx(self._rpm_msg(r))) @@ -237,10 +238,10 @@ def test_throttle_safety_check(self): raise unittest.SkipTest for enabled in [0, 1]: - for t in range(int(self.MIN_THROTTLE), int(self.MAX_THROTTLE * 1.2)): + for t in range(self.MIN_THROTTLE, int(self.MAX_THROTTLE * 1.2)): self.safety.set_controls_allowed(enabled) if t > self.MAX_THROTTLE or (not enabled and abs(t) > 0): - self.assertFalse(self._tx(self._throttle_msg(t)), msg=f"{t} throttle was allowed when {enabled}") + self.assertFalse(self._tx(self._throttle_msg(t))) else: self.assertTrue(self._tx(self._throttle_msg(t))) diff --git a/tests/safety/test_subaru.py b/tests/safety/test_subaru.py index 87e26de1ac..48b32fa893 100755 --- a/tests/safety/test_subaru.py +++ b/tests/safety/test_subaru.py @@ -139,15 +139,15 @@ def test_aeb_passthrough(self): # Allow higher braking when AEB is triggered self.safety.set_controls_allowed(1) - self.assertFalse(self.safety.get_subaru_aeb()) + self.assertFalse(self.safety.get_stock_aeb()) self._rx(self._aeb_msg(False)) - self.assertFalse(self.safety.get_subaru_aeb()) + self.assertFalse(self.safety.get_stock_aeb()) self.assertTrue(self._tx(self._brake_msg(brake=self.MAX_BRAKE))) self.assertFalse(self._tx(self._brake_msg(brake=self.MAX_BRAKE+10))) self._rx(self._aeb_msg(True)) - self.assertTrue(self.safety.get_subaru_aeb()) + self.assertTrue(self.safety.get_stock_aeb()) self.assertTrue(self._tx(self._brake_msg(brake=self.MAX_BRAKE+10))) From 08416253d24b2b7a038a1e660c2b361badf673d6 Mon Sep 17 00:00:00 2001 From: Justin Newberry Date: Mon, 10 Jul 2023 16:29:16 -0700 Subject: [PATCH 045/113] fix unittests --- Dockerfile | 4 ++-- board/safety/safety_subaru.h | 4 ++-- tests/safety/common.py | 12 ++++++------ tests/safety/test_subaru.py | 4 ++-- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/Dockerfile b/Dockerfile index d1684abcd7..b40fa98c51 100644 --- a/Dockerfile +++ b/Dockerfile @@ -51,8 +51,8 @@ RUN curl -L https://github.com/pyenv/pyenv-installer/raw/master/bin/pyenv-instal ENV PATH="/root/.pyenv/bin:/root/.pyenv/shims:${PATH}" ENV PANDA_PATH=/tmp/openpilot/panda -ENV OPENPILOT_REF="e276d2a417a5133fb91c93b2ef30df68a7d5f225" -ENV OPENDBC_REF="9ae9fbfe56f79dca66c673a6479751a15ad61780" +ENV OPENPILOT_REF="e63e2dde18b9ae2925d1a38c527dccfd0c99d4c5" +ENV OPENDBC_REF="fe8d535a7fd99eeb15526ca944a6019b9a1e5ea0" COPY requirements.txt /tmp/ RUN pyenv install 3.11.4 && \ diff --git a/board/safety/safety_subaru.h b/board/safety/safety_subaru.h index dcc5d0fb5e..9fae9e42ad 100644 --- a/board/safety/safety_subaru.h +++ b/board/safety/safety_subaru.h @@ -54,7 +54,7 @@ const LongitudinalLimits SUBARU_LONG_LIMITS = { {MSG_SUBARU_ES_LKAS_State, SUBARU_MAIN_BUS, 8}, \ {MSG_SUBARU_ES_Infotainment, SUBARU_MAIN_BUS, 8}, \ -#define SUBARU_LONG_TX_MSGS(alt_bus) \ +#define SUBARU_COMMON_LONG_TX_MSGS(alt_bus) \ {MSG_SUBARU_ES_Brake, SUBARU_MAIN_BUS, 8}, \ {MSG_SUBARU_ES_Status, SUBARU_MAIN_BUS, 8}, \ {MSG_SUBARU_Brake_Status, SUBARU_CAM_BUS, 8}, \ @@ -68,7 +68,7 @@ const CanMsg SUBARU_TX_MSGS[] = { const CanMsg SUBARU_LONG_TX_MSGS[] = { SUBARU_COMMON_TX_MSGS(0) - SUBARU_LONG_TX_MSGS(0) + SUBARU_COMMON_LONG_TX_MSGS(0) }; #define SUBARU_LONG_TX_MSGS_LEN (sizeof(SUBARU_LONG_TX_MSGS) / sizeof(SUBARU_LONG_TX_MSGS[0])) diff --git a/tests/safety/common.py b/tests/safety/common.py index 6a46f9af40..9d86749bb7 100644 --- a/tests/safety/common.py +++ b/tests/safety/common.py @@ -175,14 +175,14 @@ def test_accel_actuation_limits(self, stock_longitudinal=False): class LongitudinalGasBrakeSafetyTest(PandaSafetyTestBase, abc.ABC): - MIN_BRAKE = 0 - MAX_BRAKE = None + MIN_BRAKE: int = 0 + MAX_BRAKE: Optional[int] = None - MIN_RPM = 0 - MAX_RPM = None + MIN_RPM: int = 0 + MAX_RPM: Optional[int] = None - MIN_THROTTLE = 0 - MAX_THROTTLE = None + MIN_THROTTLE: int = 0 + MAX_THROTTLE: Optional[int] = None @classmethod def setUpClass(cls): diff --git a/tests/safety/test_subaru.py b/tests/safety/test_subaru.py index 48b32fa893..65e102bec0 100755 --- a/tests/safety/test_subaru.py +++ b/tests/safety/test_subaru.py @@ -35,6 +35,7 @@ def long_tx_msgs(): class TestSubaruSafetyBase(common.PandaSafetyTest, common.DriverTorqueSteeringSafetyTest): + FLAGS = 0 STANDSTILL_THRESHOLD = 0 # kph RELAY_MALFUNCTION_ADDR = MSG_SUBARU_ES_LKAS RELAY_MALFUNCTION_BUS = 0 @@ -120,9 +121,8 @@ class TestSubaruLongitudinalSafetyBase(TestSubaruSafetyBase, common.Longitudinal MSG_SUBARU_ES_LKAS_State, MSG_SUBARU_ES_Infotainment]} def _brake_msg(self, brake, aeb=False, bus=None): - if bus is None: bus = self.ALT_BUS values = {"Brake_Pressure": brake, "AEB_Status": 8 if aeb else 0} - return self.packer.make_can_msg_panda("ES_Brake", bus, values) + return self.packer.make_can_msg_panda("ES_Brake", self.ALT_BUS if bus is None else bus, values) def _throttle_msg(self, throttle): values = {"Cruise_Throttle": throttle} From 52b7367d67dfa5753c68ec82418c03d333dfcb68 Mon Sep 17 00:00:00 2001 From: Justin Newberry Date: Mon, 10 Jul 2023 17:21:00 -0700 Subject: [PATCH 046/113] pretty --- board/safety/safety_subaru.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/board/safety/safety_subaru.h b/board/safety/safety_subaru.h index 9fae9e42ad..84c8430317 100644 --- a/board/safety/safety_subaru.h +++ b/board/safety/safety_subaru.h @@ -54,7 +54,7 @@ const LongitudinalLimits SUBARU_LONG_LIMITS = { {MSG_SUBARU_ES_LKAS_State, SUBARU_MAIN_BUS, 8}, \ {MSG_SUBARU_ES_Infotainment, SUBARU_MAIN_BUS, 8}, \ -#define SUBARU_COMMON_LONG_TX_MSGS(alt_bus) \ +#define SUBARU_COMMON_LONG_TX_MSGS(alt_bus) \ {MSG_SUBARU_ES_Brake, SUBARU_MAIN_BUS, 8}, \ {MSG_SUBARU_ES_Status, SUBARU_MAIN_BUS, 8}, \ {MSG_SUBARU_Brake_Status, SUBARU_CAM_BUS, 8}, \ From 78f6d34cce637a9291fccc51a9c2522a6b693bb9 Mon Sep 17 00:00:00 2001 From: Justin Newberry Date: Tue, 11 Jul 2023 09:39:47 -0700 Subject: [PATCH 047/113] need to upload this route --- tests/safety_replay/test_safety_replay.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/safety_replay/test_safety_replay.py b/tests/safety_replay/test_safety_replay.py index 2dcc07cd1a..6b297bd6cf 100755 --- a/tests/safety_replay/test_safety_replay.py +++ b/tests/safety_replay/test_safety_replay.py @@ -18,6 +18,8 @@ ReplayRoute("f89c604cf653e2bf|2018-09-29--13-46-50.bz2", Panda.SAFETY_GM), # GM.VOLT ReplayRoute("6fb4948a7ebe670e|2019-11-12--00-35-53.bz2", Panda.SAFETY_CHRYSLER), # CHRYSLER.PACIFICA_2018_HYBRID ReplayRoute("0744286ead2fbb96|2023-05-01--16-27-01--35--rlog.bz2", Panda.SAFETY_SUBARU), # SUBARU.IMPREZA + ReplayRoute("8de015561e1ea4a0|2023-07-02--20-14-07--13--rlog.bz2", Panda.SAFETY_SUBARU, # SUBARU.CROSSTREK (openpilot long) + Panda.FLAG_SUBARU_LONG), ReplayRoute("bad6ae3584ece5b5|2023-04-29--11-23-48--7--rlog.bz2", Panda.SAFETY_SUBARU, # SUBARU.OUTBACK Panda.FLAG_SUBARU_GEN2, ALT_EXP.DISABLE_DISENGAGE_ON_GAS), ReplayRoute("76b83eb0245de90e|2020-03-05--19-16-05.bz2", Panda.SAFETY_VOLKSWAGEN_MQB), # VOLKSWAGEN.GOLF (stock ACC) @@ -28,7 +30,7 @@ ReplayRoute("fbbfa6af821552b9|2020-03-03--08-09-43.bz2", Panda.SAFETY_NISSAN), # NISSAN.XTRAIL ReplayRoute("5b7c365c50084530_2020-04-15--16-13-24--3--rlog.bz2", Panda.SAFETY_HYUNDAI), # HYUNDAI.SONATA ReplayRoute("610ebb9faaad6b43|2020-06-13--15-28-36.bz2", Panda.SAFETY_HYUNDAI_LEGACY), # HYUNDAI.IONIQ_EV_LTD - ReplayRoute("5ab784f361e19b78_2020-06-08--16-30-41.bz2", Panda.SAFETY_SUBARU_PREGLOBAL), # SUBARU.OUTBACK_PREGLOBAL + ReplayRoute("5ab784f361e19b78_2020-06-08--16-30-41.bz2", Panda.SAFETY_SUBARU_PREGLOBAL), # SUBARU.OUTBACK_PREGLOBAL ReplayRoute("bb50caf5f0945ab1|2021-06-19--17-20-18.bz2", Panda.SAFETY_TESLA), # TESLA.AP2_MODELS ReplayRoute("bd6a637565e91581_2021-10-29--22-18-31--1--rlog.bz2", Panda.SAFETY_MAZDA), # MAZDA.CX9_2021 ReplayRoute("1a5d045d2c531a6d_2022-06-07--22-03-00--1--rlog.bz2", Panda.SAFETY_HONDA_BOSCH, # HONDA.CIVIC_2022 From 8e648050c5ddc07367bfebe14eebb72b2ac91a71 Mon Sep 17 00:00:00 2001 From: Justin Newberry Date: Tue, 11 Jul 2023 16:52:52 -0700 Subject: [PATCH 048/113] remove AEB stuff for now --- board/safety/safety_subaru.h | 8 +------- board/safety_declarations.h | 3 --- tests/libpanda/safety_helpers.h | 8 -------- tests/libpanda/safety_helpers.py | 6 ------ tests/safety/common.py | 1 - tests/safety/test_subaru.py | 25 +++---------------------- 6 files changed, 4 insertions(+), 47 deletions(-) diff --git a/board/safety/safety_subaru.h b/board/safety/safety_subaru.h index 84c8430317..c41e5e265a 100644 --- a/board/safety/safety_subaru.h +++ b/board/safety/safety_subaru.h @@ -131,7 +131,6 @@ static int subaru_rx_hook(CANPacket_t *to_push) { if (valid) { const int bus = GET_BUS(to_push); const int alt_main_bus = subaru_gen2 ? SUBARU_ALT_BUS : SUBARU_MAIN_BUS; - const int alt_cam_bus = subaru_gen2 ? SUBARU_ALT_BUS : SUBARU_CAM_BUS; int addr = GET_ADDR(to_push); if ((addr == MSG_SUBARU_Steering_Torque) && (bus == SUBARU_MAIN_BUS)) { @@ -141,11 +140,6 @@ static int subaru_rx_hook(CANPacket_t *to_push) { update_sample(&torque_driver, torque_driver_new); } - if ((addr == MSG_SUBARU_ES_Brake) && (bus == alt_cam_bus)) { - const int aeb_status = (GET_BYTE(to_push, 4) & 0xFU); - stock_aeb = (aeb_status == 8); - } - // enter controls on rising edge of ACC, exit controls on ACC off if ((addr == MSG_SUBARU_CruiseControl) && (bus == alt_main_bus)) { bool cruise_engaged = GET_BIT(to_push, 41U) != 0U; @@ -200,7 +194,7 @@ static int subaru_tx_hook(CANPacket_t *to_send) { // check es_brake brake_pressure limits if (addr == MSG_SUBARU_ES_Brake) { int es_brake_pressure = ((GET_BYTES(to_send, 0, 4) >> 16) & 0xFFFFU); - violation |= !stock_aeb && longitudinal_brake_checks(es_brake_pressure, SUBARU_LONG_LIMITS); + violation |= longitudinal_brake_checks(es_brake_pressure, SUBARU_LONG_LIMITS); } // check es_distance cruise_throttle limits diff --git a/board/safety_declarations.h b/board/safety_declarations.h index 5c55a8081f..8bd9357ebd 100644 --- a/board/safety_declarations.h +++ b/board/safety_declarations.h @@ -223,9 +223,6 @@ uint32_t ts_angle_last = 0; int desired_angle_last = 0; struct sample_t angle_meas; // last 6 steer angles/curvatures -// for safety modes that need to passthru aeb -bool stock_aeb = false; - // This can be set with a USB command // It enables features that allow alternative experiences, like not disengaging on gas press // It is only either 0 or 1 on mainline comma.ai openpilot diff --git a/tests/libpanda/safety_helpers.h b/tests/libpanda/safety_helpers.h index 9fee1953c0..f02c4da343 100644 --- a/tests/libpanda/safety_helpers.h +++ b/tests/libpanda/safety_helpers.h @@ -168,14 +168,6 @@ void set_honda_fwd_brake(bool c){ honda_fwd_brake = c; } -void set_stock_aeb(bool c){ - stock_aeb = c; -} - -bool get_stock_aeb(){ - return stock_aeb; -} - void init_tests(void){ // get HW_TYPE from env variable set in test.sh if (getenv("HW_TYPE")) { diff --git a/tests/libpanda/safety_helpers.py b/tests/libpanda/safety_helpers.py index 7be88ea6bf..594803e303 100644 --- a/tests/libpanda/safety_helpers.py +++ b/tests/libpanda/safety_helpers.py @@ -21,8 +21,6 @@ def setup_safety_helpers(ffi): int get_vehicle_speed_max(void); int get_current_safety_mode(void); int get_current_safety_param(void); - void set_stock_aeb(bool c); - bool get_stock_aeb(); void set_torque_meas(int min, int max); int get_torque_meas_min(void); @@ -98,7 +96,3 @@ def set_honda_fwd_brake(self, c: bool) -> None: ... def set_honda_alt_brake_msg(self, c: bool) -> None: ... def set_honda_bosch_long(self, c: bool) -> None: ... def get_honda_hw(self) -> int: ... - - def set_stock_aeb(self, c: bool) -> None: ... - def get_stock_aeb(self) -> bool: ... - diff --git a/tests/safety/common.py b/tests/safety/common.py index 9d86749bb7..ffe30b6054 100644 --- a/tests/safety/common.py +++ b/tests/safety/common.py @@ -212,7 +212,6 @@ def test_gas_brake_limits_correct(self): self.assertGreater(self.MAX_THROTTLE, self.MIN_THROTTLE) def test_brake_safety_check(self): - self.safety.set_stock_aeb(0) for enabled in [0, 1]: for b in range(self.MIN_BRAKE, int(self.MAX_BRAKE * 1.5)): self.safety.set_controls_allowed(enabled) diff --git a/tests/safety/test_subaru.py b/tests/safety/test_subaru.py index 65e102bec0..52076c34bf 100755 --- a/tests/safety/test_subaru.py +++ b/tests/safety/test_subaru.py @@ -120,9 +120,9 @@ class TestSubaruLongitudinalSafetyBase(TestSubaruSafetyBase, common.Longitudinal MSG_SUBARU_ES_Status, MSG_SUBARU_ES_DashStatus, MSG_SUBARU_ES_LKAS_State, MSG_SUBARU_ES_Infotainment]} - def _brake_msg(self, brake, aeb=False, bus=None): - values = {"Brake_Pressure": brake, "AEB_Status": 8 if aeb else 0} - return self.packer.make_can_msg_panda("ES_Brake", self.ALT_BUS if bus is None else bus, values) + def _brake_msg(self, brake): + values = {"Brake_Pressure": brake} + return self.packer.make_can_msg_panda("ES_Brake", self.ALT_BUS, values) def _throttle_msg(self, throttle): values = {"Cruise_Throttle": throttle} @@ -132,25 +132,6 @@ def _rpm_msg(self, rpm): values = {"Cruise_RPM": rpm} return self.packer.make_can_msg_panda("ES_Status", self.ALT_BUS, values) - def _aeb_msg(self, aeb): - return self._brake_msg(5, aeb, bus=2) - - def test_aeb_passthrough(self): - # Allow higher braking when AEB is triggered - self.safety.set_controls_allowed(1) - - self.assertFalse(self.safety.get_stock_aeb()) - self._rx(self._aeb_msg(False)) - self.assertFalse(self.safety.get_stock_aeb()) - - self.assertTrue(self._tx(self._brake_msg(brake=self.MAX_BRAKE))) - self.assertFalse(self._tx(self._brake_msg(brake=self.MAX_BRAKE+10))) - - self._rx(self._aeb_msg(True)) - self.assertTrue(self.safety.get_stock_aeb()) - - self.assertTrue(self._tx(self._brake_msg(brake=self.MAX_BRAKE+10))) - class TestSubaruGen1Safety(TestSubaruSafetyBase): FLAGS = 0 From 4110eaca7c8f3ac5c265caf188104267530a8f4d Mon Sep 17 00:00:00 2001 From: Justin Newberry Date: Wed, 12 Jul 2023 11:05:37 -0700 Subject: [PATCH 049/113] remove unrequired rpm checks --- board/safety/safety_subaru.h | 41 ++++++++--------------- board/safety_declarations.h | 3 -- tests/libpanda/safety_helpers.py | 2 +- tests/safety/common.py | 24 ++----------- tests/safety_replay/test_safety_replay.py | 4 +-- 5 files changed, 19 insertions(+), 55 deletions(-) diff --git a/board/safety/safety_subaru.h b/board/safety/safety_subaru.h index c41e5e265a..daacfc024c 100644 --- a/board/safety/safety_subaru.h +++ b/board/safety/safety_subaru.h @@ -23,9 +23,8 @@ const SteeringLimits SUBARU_GEN2_STEERING_LIMITS = { const LongitudinalLimits SUBARU_LONG_LIMITS = { .min_gas = 0, .max_gas = 3400, - .min_rpm = 0, - .max_rpm = 3200, .max_brake = 400, + .inactive_gas = 0 }; #define MSG_SUBARU_Brake_Status 0x13c @@ -60,39 +59,37 @@ const LongitudinalLimits SUBARU_LONG_LIMITS = { {MSG_SUBARU_Brake_Status, SUBARU_CAM_BUS, 8}, \ {MSG_SUBARU_CruiseControl, SUBARU_CAM_BUS, 8}, \ +#define SUBARU_COMMON_ADDR_CHECKS(alt_bus) \ + {.msg = {{MSG_SUBARU_Throttle, SUBARU_MAIN_BUS, 8, .check_checksum = true, .max_counter = 15U, .expected_timestep = 10000U}, { 0 }, { 0 }}}, \ + {.msg = {{MSG_SUBARU_Steering_Torque, SUBARU_MAIN_BUS, 8, .check_checksum = true, .max_counter = 15U, .expected_timestep = 20000U}, { 0 }, { 0 }}}, \ + {.msg = {{MSG_SUBARU_Wheel_Speeds, alt_bus, 8, .check_checksum = true, .max_counter = 15U, .expected_timestep = 20000U}, { 0 }, { 0 }}}, \ + {.msg = {{MSG_SUBARU_Brake_Status, alt_bus, 8, .check_checksum = true, .max_counter = 15U, .expected_timestep = 20000U}, { 0 }, { 0 }}}, \ + {.msg = {{MSG_SUBARU_CruiseControl, alt_bus, 8, .check_checksum = true, .max_counter = 15U, .expected_timestep = 50000U}, { 0 }, { 0 }}}, \ const CanMsg SUBARU_TX_MSGS[] = { - SUBARU_COMMON_TX_MSGS(0) + SUBARU_COMMON_TX_MSGS(SUBARU_MAIN_BUS) }; #define SUBARU_TX_MSGS_LEN (sizeof(SUBARU_TX_MSGS) / sizeof(SUBARU_TX_MSGS[0])) const CanMsg SUBARU_LONG_TX_MSGS[] = { - SUBARU_COMMON_TX_MSGS(0) - SUBARU_COMMON_LONG_TX_MSGS(0) + SUBARU_COMMON_TX_MSGS(SUBARU_MAIN_BUS) + SUBARU_COMMON_LONG_TX_MSGS(SUBARU_MAIN_BUS) }; #define SUBARU_LONG_TX_MSGS_LEN (sizeof(SUBARU_LONG_TX_MSGS) / sizeof(SUBARU_LONG_TX_MSGS[0])) const CanMsg SUBARU_GEN2_TX_MSGS[] = { - SUBARU_COMMON_TX_MSGS(1) + SUBARU_COMMON_TX_MSGS(SUBARU_ALT_BUS) }; #define SUBARU_GEN2_TX_MSGS_LEN (sizeof(SUBARU_GEN2_TX_MSGS) / sizeof(SUBARU_GEN2_TX_MSGS[0])) AddrCheckStruct subaru_addr_checks[] = { - {.msg = {{MSG_SUBARU_Throttle, SUBARU_MAIN_BUS, 8, .check_checksum = true, .max_counter = 15U, .expected_timestep = 10000U}, { 0 }, { 0 }}}, - {.msg = {{MSG_SUBARU_Steering_Torque, SUBARU_MAIN_BUS, 8, .check_checksum = true, .max_counter = 15U, .expected_timestep = 20000U}, { 0 }, { 0 }}}, - {.msg = {{MSG_SUBARU_Wheel_Speeds, SUBARU_MAIN_BUS, 8, .check_checksum = true, .max_counter = 15U, .expected_timestep = 20000U}, { 0 }, { 0 }}}, - {.msg = {{MSG_SUBARU_Brake_Status, SUBARU_MAIN_BUS, 8, .check_checksum = true, .max_counter = 15U, .expected_timestep = 20000U}, { 0 }, { 0 }}}, - {.msg = {{MSG_SUBARU_CruiseControl, SUBARU_MAIN_BUS, 8, .check_checksum = true, .max_counter = 15U, .expected_timestep = 50000U}, { 0 }, { 0 }}}, + SUBARU_COMMON_ADDR_CHECKS(SUBARU_MAIN_BUS) }; #define SUBARU_ADDR_CHECK_LEN (sizeof(subaru_addr_checks) / sizeof(subaru_addr_checks[0])) addr_checks subaru_rx_checks = {subaru_addr_checks, SUBARU_ADDR_CHECK_LEN}; AddrCheckStruct subaru_gen2_addr_checks[] = { - {.msg = {{MSG_SUBARU_Throttle, SUBARU_MAIN_BUS, 8, .check_checksum = true, .max_counter = 15U, .expected_timestep = 10000U}, { 0 }, { 0 }}}, - {.msg = {{MSG_SUBARU_Steering_Torque, SUBARU_MAIN_BUS, 8, .check_checksum = true, .max_counter = 15U, .expected_timestep = 20000U}, { 0 }, { 0 }}}, - {.msg = {{MSG_SUBARU_Wheel_Speeds, SUBARU_ALT_BUS, 8, .check_checksum = true, .max_counter = 15U, .expected_timestep = 20000U}, { 0 }, { 0 }}}, - {.msg = {{MSG_SUBARU_Brake_Status, SUBARU_ALT_BUS, 8, .check_checksum = true, .max_counter = 15U, .expected_timestep = 20000U}, { 0 }, { 0 }}}, - {.msg = {{MSG_SUBARU_CruiseControl, SUBARU_ALT_BUS, 8, .check_checksum = true, .max_counter = 15U, .expected_timestep = 50000U}, { 0 }, { 0 }}}, + SUBARU_COMMON_ADDR_CHECKS(SUBARU_ALT_BUS) }; #define SUBARU_GEN2_ADDR_CHECK_LEN (sizeof(subaru_gen2_addr_checks) / sizeof(subaru_gen2_addr_checks[0])) addr_checks subaru_gen2_rx_checks = {subaru_gen2_addr_checks, SUBARU_GEN2_ADDR_CHECK_LEN}; @@ -198,20 +195,10 @@ static int subaru_tx_hook(CANPacket_t *to_send) { } // check es_distance cruise_throttle limits - if ((addr == MSG_SUBARU_ES_Distance) && !gas_pressed) { + if (addr == MSG_SUBARU_ES_Distance) { int cruise_throttle = ((GET_BYTES(to_send, 0, 4) >> 16) & 0xFFFU); violation |= longitudinal_gas_checks(cruise_throttle, SUBARU_LONG_LIMITS); } - - // check es_status cruise_rpm limits - if ((addr == MSG_SUBARU_ES_Status) && !gas_pressed) { - int cruise_rpm = ((GET_BYTES(to_send, 0, 4) >> 16) & 0xFFFU); - if (get_longitudinal_allowed()) { - violation |= max_limit_check(cruise_rpm, SUBARU_LONG_LIMITS.max_rpm, SUBARU_LONG_LIMITS.min_rpm); - } else { - violation |= cruise_rpm != 0; - } - } } if (violation) { diff --git a/board/safety_declarations.h b/board/safety_declarations.h index 8bd9357ebd..b2f0ea9854 100644 --- a/board/safety_declarations.h +++ b/board/safety_declarations.h @@ -90,9 +90,6 @@ typedef struct { const int inactive_gas; const int max_brake; - const int min_rpm; - const int max_rpm; - // speed cmd limits const int inactive_speed; } LongitudinalLimits; diff --git a/tests/libpanda/safety_helpers.py b/tests/libpanda/safety_helpers.py index 594803e303..923d8ce425 100644 --- a/tests/libpanda/safety_helpers.py +++ b/tests/libpanda/safety_helpers.py @@ -95,4 +95,4 @@ def init_tests(self) -> None: ... def set_honda_fwd_brake(self, c: bool) -> None: ... def set_honda_alt_brake_msg(self, c: bool) -> None: ... def set_honda_bosch_long(self, c: bool) -> None: ... - def get_honda_hw(self) -> int: ... + def get_honda_hw(self) -> int: ... \ No newline at end of file diff --git a/tests/safety/common.py b/tests/safety/common.py index ffe30b6054..e97092b05d 100644 --- a/tests/safety/common.py +++ b/tests/safety/common.py @@ -178,9 +178,6 @@ class LongitudinalGasBrakeSafetyTest(PandaSafetyTestBase, abc.ABC): MIN_BRAKE: int = 0 MAX_BRAKE: Optional[int] = None - MIN_RPM: int = 0 - MAX_RPM: Optional[int] = None - MIN_THROTTLE: int = 0 MAX_THROTTLE: Optional[int] = None @@ -194,21 +191,16 @@ def setUpClass(cls): def _throttle_msg(self, throttle: int): pass - @abc.abstractmethod - def _rpm_msg(self, rpm: int): - pass - @abc.abstractmethod def _brake_msg(self, brake: int): pass def test_gas_brake_limits_correct(self): - # Assert that max brake, and at least one of max rpm or max throttle is set + # Assert that max brake and max throttle is set self.assertTrue(self.MAX_BRAKE is not None) - self.assertTrue(self.MAX_RPM is not None or self.MAX_THROTTLE is not None) + self.assertTrue(self.MAX_THROTTLE is not None) self.assertGreater(self.MAX_BRAKE, self.MIN_BRAKE) - self.assertGreater(self.MAX_RPM, self.MIN_RPM) self.assertGreater(self.MAX_THROTTLE, self.MIN_THROTTLE) def test_brake_safety_check(self): @@ -220,18 +212,6 @@ def test_brake_safety_check(self): else: self.assertTrue(self._tx(self._brake_msg(b))) - def test_rpm_safety_check(self): - if self.MAX_RPM is None: - raise unittest.SkipTest - - for enabled in [0, 1]: - for r in range(self.MIN_RPM, int(self.MAX_RPM * 1.2)): - self.safety.set_controls_allowed(enabled) - if r > self.MAX_RPM or (not enabled and abs(r) > 0): - self.assertFalse(self._tx(self._rpm_msg(r))) - else: - self.assertTrue(self._tx(self._rpm_msg(r))) - def test_throttle_safety_check(self): if self.MAX_THROTTLE is None: raise unittest.SkipTest diff --git a/tests/safety_replay/test_safety_replay.py b/tests/safety_replay/test_safety_replay.py index 6b297bd6cf..4c04373307 100755 --- a/tests/safety_replay/test_safety_replay.py +++ b/tests/safety_replay/test_safety_replay.py @@ -18,8 +18,8 @@ ReplayRoute("f89c604cf653e2bf|2018-09-29--13-46-50.bz2", Panda.SAFETY_GM), # GM.VOLT ReplayRoute("6fb4948a7ebe670e|2019-11-12--00-35-53.bz2", Panda.SAFETY_CHRYSLER), # CHRYSLER.PACIFICA_2018_HYBRID ReplayRoute("0744286ead2fbb96|2023-05-01--16-27-01--35--rlog.bz2", Panda.SAFETY_SUBARU), # SUBARU.IMPREZA - ReplayRoute("8de015561e1ea4a0|2023-07-02--20-14-07--13--rlog.bz2", Panda.SAFETY_SUBARU, # SUBARU.CROSSTREK (openpilot long) - Panda.FLAG_SUBARU_LONG), + ReplayRoute("8de015561e1ea4a0|2023-07-02--20-14-07----rlog.bz2", Panda.SAFETY_SUBARU, # SUBARU.CROSSTREK (openpilot long) + Panda.FLAG_SUBARU_LONG, ALT_EXP.DISABLE_DISENGAGE_ON_GAS), ReplayRoute("bad6ae3584ece5b5|2023-04-29--11-23-48--7--rlog.bz2", Panda.SAFETY_SUBARU, # SUBARU.OUTBACK Panda.FLAG_SUBARU_GEN2, ALT_EXP.DISABLE_DISENGAGE_ON_GAS), ReplayRoute("76b83eb0245de90e|2020-03-05--19-16-05.bz2", Panda.SAFETY_VOLKSWAGEN_MQB), # VOLKSWAGEN.GOLF (stock ACC) From 4849837156fb4b0b2da1985185cafb29655cbfba Mon Sep 17 00:00:00 2001 From: Justin Newberry Date: Wed, 12 Jul 2023 16:08:49 -0700 Subject: [PATCH 050/113] add comment --- board/safety/safety_subaru.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/board/safety/safety_subaru.h b/board/safety/safety_subaru.h index daacfc024c..44b128effa 100644 --- a/board/safety/safety_subaru.h +++ b/board/safety/safety_subaru.h @@ -22,8 +22,8 @@ const SteeringLimits SUBARU_GEN2_STEERING_LIMITS = { const LongitudinalLimits SUBARU_LONG_LIMITS = { .min_gas = 0, - .max_gas = 3400, - .max_brake = 400, + .max_gas = 3400, // approx 2.5 m/s^2 + .max_brake = 400, // approx -2.5 m/s^2 .inactive_gas = 0 }; From 21fc7640cc29a379ad24681924b730cf9922998b Mon Sep 17 00:00:00 2001 From: Justin Newberry Date: Thu, 13 Jul 2023 10:22:32 -0700 Subject: [PATCH 051/113] added comment about acceleration and use throttle limit for rpm too --- board/safety/safety_subaru.h | 8 ++++++- tests/safety/common.py | 28 +++++++++-------------- tests/safety/test_subaru.py | 4 +++- tests/safety_replay/test_safety_replay.py | 5 ++-- 4 files changed, 24 insertions(+), 21 deletions(-) diff --git a/board/safety/safety_subaru.h b/board/safety/safety_subaru.h index 44b128effa..4761a8fcea 100644 --- a/board/safety/safety_subaru.h +++ b/board/safety/safety_subaru.h @@ -22,7 +22,7 @@ const SteeringLimits SUBARU_GEN2_STEERING_LIMITS = { const LongitudinalLimits SUBARU_LONG_LIMITS = { .min_gas = 0, - .max_gas = 3400, // approx 2.5 m/s^2 + .max_gas = 3400, // approx 1.2 m/s^2 at low speeds, less at higher speeds .max_brake = 400, // approx -2.5 m/s^2 .inactive_gas = 0 }; @@ -199,6 +199,12 @@ static int subaru_tx_hook(CANPacket_t *to_send) { int cruise_throttle = ((GET_BYTES(to_send, 0, 4) >> 16) & 0xFFFU); violation |= longitudinal_gas_checks(cruise_throttle, SUBARU_LONG_LIMITS); } + + // check es_status cruise_rpm limits + if (addr == MSG_SUBARU_ES_Status) { + int cruise_rpm = ((GET_BYTES(to_send, 0, 4) >> 16) & 0xFFFU); + violation |= longitudinal_gas_checks(cruise_rpm, SUBARU_LONG_LIMITS); + } } if (violation) { diff --git a/tests/safety/common.py b/tests/safety/common.py index e97092b05d..566ca361d9 100644 --- a/tests/safety/common.py +++ b/tests/safety/common.py @@ -202,29 +202,23 @@ def test_gas_brake_limits_correct(self): self.assertGreater(self.MAX_BRAKE, self.MIN_BRAKE) self.assertGreater(self.MAX_THROTTLE, self.MIN_THROTTLE) - - def test_brake_safety_check(self): + + def _generic_limit_safety_check(self, msg_function, MIN_VALUE, MAX_VALUE, MAX_VALUE_MULTIPLIER): for enabled in [0, 1]: - for b in range(self.MIN_BRAKE, int(self.MAX_BRAKE * 1.5)): + for v in range(int(MIN_VALUE), int(MAX_VALUE*MAX_VALUE_MULTIPLIER)): self.safety.set_controls_allowed(enabled) - if b > self.MAX_BRAKE or (not enabled and abs(b) > 0): - self.assertFalse(self._tx(self._brake_msg(b))) + if v > MAX_VALUE or (not enabled and abs(v) > 0): + self.assertFalse(self._tx(msg_function(v))) else: - self.assertTrue(self._tx(self._brake_msg(b))) + self.assertTrue(self._tx(msg_function(v))) + + def test_brake_safety_check(self): + self._generic_limit_safety_check(self._brake_msg, self.MIN_BRAKE, self.MAX_BRAKE, 1.5) def test_throttle_safety_check(self): - if self.MAX_THROTTLE is None: - raise unittest.SkipTest - - for enabled in [0, 1]: - for t in range(self.MIN_THROTTLE, int(self.MAX_THROTTLE * 1.2)): - self.safety.set_controls_allowed(enabled) - if t > self.MAX_THROTTLE or (not enabled and abs(t) > 0): - self.assertFalse(self._tx(self._throttle_msg(t))) - else: - self.assertTrue(self._tx(self._throttle_msg(t))) - + self._generic_limit_safety_check(self._throttle_msg, self.MIN_THROTTLE, self.MAX_THROTTLE, 1.2) + class TorqueSteeringSafetyTestBase(PandaSafetyTestBase, abc.ABC): MAX_RATE_UP = 0 diff --git a/tests/safety/test_subaru.py b/tests/safety/test_subaru.py index 52076c34bf..17700d6558 100755 --- a/tests/safety/test_subaru.py +++ b/tests/safety/test_subaru.py @@ -111,7 +111,6 @@ class TestSubaruGen2SafetyBase(TestSubaruSafetyBase): class TestSubaruLongitudinalSafetyBase(TestSubaruSafetyBase, common.LongitudinalGasBrakeSafetyTest): - MAX_RPM = 3200 MAX_THROTTLE = 3400 MAX_BRAKE = 400 @@ -132,6 +131,9 @@ def _rpm_msg(self, rpm): values = {"Cruise_RPM": rpm} return self.packer.make_can_msg_panda("ES_Status", self.ALT_BUS, values) + def test_rpm_safety_check(self): + self._generic_limit_safety_check(self._rpm_msg, self.MIN_THROTTLE, self.MAX_THROTTLE, 1.2) + class TestSubaruGen1Safety(TestSubaruSafetyBase): FLAGS = 0 diff --git a/tests/safety_replay/test_safety_replay.py b/tests/safety_replay/test_safety_replay.py index 4c04373307..d38ab11e02 100755 --- a/tests/safety_replay/test_safety_replay.py +++ b/tests/safety_replay/test_safety_replay.py @@ -13,13 +13,14 @@ ReplayRoute = namedtuple("ReplayRoute", ("route", "safety_mode", "param", "alternative_experience"), defaults=(0, 0)) logs = [ + # TODO: need a route with updated safety stuff + #ReplayRoute("8de015561e1ea4a0|2023-07-02--20-14-07--9--rlog.bz2", Panda.SAFETY_SUBARU, # SUBARU.CROSSTREK (openpilot long) + # Panda.FLAG_SUBARU_LONG, ALT_EXP.DISABLE_DISENGAGE_ON_GAS), ReplayRoute("2425568437959f9d|2019-12-22--16-24-37.bz2", Panda.SAFETY_HONDA_NIDEC), # HONDA.CIVIC (fcw presents: 0x1FA blocked as expected) ReplayRoute("38bfd238edecbcd7|2019-06-07--10-15-25.bz2", Panda.SAFETY_TOYOTA, 66), # TOYOTA.PRIUS ReplayRoute("f89c604cf653e2bf|2018-09-29--13-46-50.bz2", Panda.SAFETY_GM), # GM.VOLT ReplayRoute("6fb4948a7ebe670e|2019-11-12--00-35-53.bz2", Panda.SAFETY_CHRYSLER), # CHRYSLER.PACIFICA_2018_HYBRID ReplayRoute("0744286ead2fbb96|2023-05-01--16-27-01--35--rlog.bz2", Panda.SAFETY_SUBARU), # SUBARU.IMPREZA - ReplayRoute("8de015561e1ea4a0|2023-07-02--20-14-07----rlog.bz2", Panda.SAFETY_SUBARU, # SUBARU.CROSSTREK (openpilot long) - Panda.FLAG_SUBARU_LONG, ALT_EXP.DISABLE_DISENGAGE_ON_GAS), ReplayRoute("bad6ae3584ece5b5|2023-04-29--11-23-48--7--rlog.bz2", Panda.SAFETY_SUBARU, # SUBARU.OUTBACK Panda.FLAG_SUBARU_GEN2, ALT_EXP.DISABLE_DISENGAGE_ON_GAS), ReplayRoute("76b83eb0245de90e|2020-03-05--19-16-05.bz2", Panda.SAFETY_VOLKSWAGEN_MQB), # VOLKSWAGEN.GOLF (stock ACC) From f52659bd229a33679e09b1537b0ba5c6dc0f1dba Mon Sep 17 00:00:00 2001 From: Justin Newberry Date: Thu, 13 Jul 2023 17:31:22 -0700 Subject: [PATCH 052/113] inactive_throttle_fix --- board/safety/safety_subaru.h | 11 +++-------- tests/safety/common.py | 10 ++++++---- tests/safety/test_subaru.py | 4 +--- 3 files changed, 10 insertions(+), 15 deletions(-) diff --git a/board/safety/safety_subaru.h b/board/safety/safety_subaru.h index 4761a8fcea..04b74a2e24 100644 --- a/board/safety/safety_subaru.h +++ b/board/safety/safety_subaru.h @@ -21,10 +21,11 @@ const SteeringLimits SUBARU_GEN2_STEERING_LIMITS = { }; const LongitudinalLimits SUBARU_LONG_LIMITS = { + .max_brake = 400, // approx -2.5 m/s^2 + .min_gas = 0, .max_gas = 3400, // approx 1.2 m/s^2 at low speeds, less at higher speeds - .max_brake = 400, // approx -2.5 m/s^2 - .inactive_gas = 0 + .inactive_gas = 808, }; #define MSG_SUBARU_Brake_Status 0x13c @@ -199,12 +200,6 @@ static int subaru_tx_hook(CANPacket_t *to_send) { int cruise_throttle = ((GET_BYTES(to_send, 0, 4) >> 16) & 0xFFFU); violation |= longitudinal_gas_checks(cruise_throttle, SUBARU_LONG_LIMITS); } - - // check es_status cruise_rpm limits - if (addr == MSG_SUBARU_ES_Status) { - int cruise_rpm = ((GET_BYTES(to_send, 0, 4) >> 16) & 0xFFFU); - violation |= longitudinal_gas_checks(cruise_rpm, SUBARU_LONG_LIMITS); - } } if (violation) { diff --git a/tests/safety/common.py b/tests/safety/common.py index 566ca361d9..147eaeba97 100644 --- a/tests/safety/common.py +++ b/tests/safety/common.py @@ -181,6 +181,8 @@ class LongitudinalGasBrakeSafetyTest(PandaSafetyTestBase, abc.ABC): MIN_THROTTLE: int = 0 MAX_THROTTLE: Optional[int] = None + INACTIVE_THROTTLE: Optional[int] = 0 + @classmethod def setUpClass(cls): if cls.__name__ == "LongitudinalGasBrakeSafetyTest": @@ -203,20 +205,20 @@ def test_gas_brake_limits_correct(self): self.assertGreater(self.MAX_BRAKE, self.MIN_BRAKE) self.assertGreater(self.MAX_THROTTLE, self.MIN_THROTTLE) - def _generic_limit_safety_check(self, msg_function, MIN_VALUE, MAX_VALUE, MAX_VALUE_MULTIPLIER): + def _generic_limit_safety_check(self, msg_function, MIN_VALUE, MAX_VALUE, MAX_VALUE_MULTIPLIER, INACTIVE_VALUE=0): for enabled in [0, 1]: for v in range(int(MIN_VALUE), int(MAX_VALUE*MAX_VALUE_MULTIPLIER)): self.safety.set_controls_allowed(enabled) - if v > MAX_VALUE or (not enabled and abs(v) > 0): + if (not enabled and v != INACTIVE_VALUE) or v > MAX_VALUE: self.assertFalse(self._tx(msg_function(v))) else: - self.assertTrue(self._tx(msg_function(v))) + self.assertTrue(self._tx(msg_function(v)), msg=f"{v} was not allowed") def test_brake_safety_check(self): self._generic_limit_safety_check(self._brake_msg, self.MIN_BRAKE, self.MAX_BRAKE, 1.5) def test_throttle_safety_check(self): - self._generic_limit_safety_check(self._throttle_msg, self.MIN_THROTTLE, self.MAX_THROTTLE, 1.2) + self._generic_limit_safety_check(self._throttle_msg, self.MIN_THROTTLE, self.MAX_THROTTLE, 1.2, self.INACTIVE_THROTTLE) class TorqueSteeringSafetyTestBase(PandaSafetyTestBase, abc.ABC): diff --git a/tests/safety/test_subaru.py b/tests/safety/test_subaru.py index 17700d6558..8472d8cd93 100755 --- a/tests/safety/test_subaru.py +++ b/tests/safety/test_subaru.py @@ -113,6 +113,7 @@ class TestSubaruGen2SafetyBase(TestSubaruSafetyBase): class TestSubaruLongitudinalSafetyBase(TestSubaruSafetyBase, common.LongitudinalGasBrakeSafetyTest): MAX_THROTTLE = 3400 MAX_BRAKE = 400 + INACTIVE_THROTTLE = 808 FWD_BLACKLISTED_ADDRS = {0: [MSG_SUBARU_CruiseControl, MSG_SUBARU_Brake_Status], 2: [MSG_SUBARU_ES_LKAS, MSG_SUBARU_ES_Brake, MSG_SUBARU_ES_Distance, @@ -131,9 +132,6 @@ def _rpm_msg(self, rpm): values = {"Cruise_RPM": rpm} return self.packer.make_can_msg_panda("ES_Status", self.ALT_BUS, values) - def test_rpm_safety_check(self): - self._generic_limit_safety_check(self._rpm_msg, self.MIN_THROTTLE, self.MAX_THROTTLE, 1.2) - class TestSubaruGen1Safety(TestSubaruSafetyBase): FLAGS = 0 From 93d6abbb706b1ddb575500c97b33cc498ed652a5 Mon Sep 17 00:00:00 2001 From: Justin Newberry Date: Fri, 14 Jul 2023 08:44:29 -0700 Subject: [PATCH 053/113] Update board/safety/safety_subaru.h Co-authored-by: Shane Smiskol --- board/safety/safety_subaru.h | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/board/safety/safety_subaru.h b/board/safety/safety_subaru.h index 04b74a2e24..48be8bf9ad 100644 --- a/board/safety/safety_subaru.h +++ b/board/safety/safety_subaru.h @@ -21,13 +21,10 @@ const SteeringLimits SUBARU_GEN2_STEERING_LIMITS = { }; const LongitudinalLimits SUBARU_LONG_LIMITS = { - .max_brake = 400, // approx -2.5 m/s^2 - - .min_gas = 0, .max_gas = 3400, // approx 1.2 m/s^2 at low speeds, less at higher speeds .inactive_gas = 808, + .max_brake = 400, // approx -2.5 m/s^2 }; - #define MSG_SUBARU_Brake_Status 0x13c #define MSG_SUBARU_CruiseControl 0x240 #define MSG_SUBARU_Throttle 0x40 From eede344f3244fa3fab0a073c17a32aeeb4a8dcb8 Mon Sep 17 00:00:00 2001 From: Justin Newberry Date: Fri, 14 Jul 2023 09:14:55 -0700 Subject: [PATCH 054/113] added comments about long limits --- board/safety/safety_subaru.h | 7 ++++--- tests/safety/common.py | 4 ++-- tests/safety/test_subaru.py | 2 +- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/board/safety/safety_subaru.h b/board/safety/safety_subaru.h index 48be8bf9ad..81b59e1bea 100644 --- a/board/safety/safety_subaru.h +++ b/board/safety/safety_subaru.h @@ -21,10 +21,11 @@ const SteeringLimits SUBARU_GEN2_STEERING_LIMITS = { }; const LongitudinalLimits SUBARU_LONG_LIMITS = { - .max_gas = 3400, // approx 1.2 m/s^2 at low speeds, less at higher speeds - .inactive_gas = 808, - .max_brake = 400, // approx -2.5 m/s^2 + .max_gas = 3400, // approx 1.2 m/s^2 at low speeds, less at higher speeds + .inactive_gas = 1818, // this is zero acceleration + .max_brake = 600, // approx -3.5 m/s^2 }; + #define MSG_SUBARU_Brake_Status 0x13c #define MSG_SUBARU_CruiseControl 0x240 #define MSG_SUBARU_Throttle 0x40 diff --git a/tests/safety/common.py b/tests/safety/common.py index 147eaeba97..2d9133b22e 100644 --- a/tests/safety/common.py +++ b/tests/safety/common.py @@ -178,7 +178,7 @@ class LongitudinalGasBrakeSafetyTest(PandaSafetyTestBase, abc.ABC): MIN_BRAKE: int = 0 MAX_BRAKE: Optional[int] = None - MIN_THROTTLE: int = 0 + MIN_THROTTLE: int = 1818 MAX_THROTTLE: Optional[int] = None INACTIVE_THROTTLE: Optional[int] = 0 @@ -212,7 +212,7 @@ def _generic_limit_safety_check(self, msg_function, MIN_VALUE, MAX_VALUE, MAX_VA if (not enabled and v != INACTIVE_VALUE) or v > MAX_VALUE: self.assertFalse(self._tx(msg_function(v))) else: - self.assertTrue(self._tx(msg_function(v)), msg=f"{v} was not allowed") + self.assertTrue(self._tx(msg_function(v))) def test_brake_safety_check(self): self._generic_limit_safety_check(self._brake_msg, self.MIN_BRAKE, self.MAX_BRAKE, 1.5) diff --git a/tests/safety/test_subaru.py b/tests/safety/test_subaru.py index 8472d8cd93..fba801acc7 100755 --- a/tests/safety/test_subaru.py +++ b/tests/safety/test_subaru.py @@ -113,7 +113,7 @@ class TestSubaruGen2SafetyBase(TestSubaruSafetyBase): class TestSubaruLongitudinalSafetyBase(TestSubaruSafetyBase, common.LongitudinalGasBrakeSafetyTest): MAX_THROTTLE = 3400 MAX_BRAKE = 400 - INACTIVE_THROTTLE = 808 + INACTIVE_THROTTLE = 1818 FWD_BLACKLISTED_ADDRS = {0: [MSG_SUBARU_CruiseControl, MSG_SUBARU_Brake_Status], 2: [MSG_SUBARU_ES_LKAS, MSG_SUBARU_ES_Brake, MSG_SUBARU_ES_Distance, From dc9cf4ea2d62ef9621ca4153cf2a5993cba386fe Mon Sep 17 00:00:00 2001 From: Justin Newberry Date: Fri, 14 Jul 2023 10:21:35 -0700 Subject: [PATCH 055/113] increase max brake --- tests/safety/common.py | 2 +- tests/safety/test_subaru.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/safety/common.py b/tests/safety/common.py index 2d9133b22e..d437f4cdcf 100644 --- a/tests/safety/common.py +++ b/tests/safety/common.py @@ -209,7 +209,7 @@ def _generic_limit_safety_check(self, msg_function, MIN_VALUE, MAX_VALUE, MAX_VA for enabled in [0, 1]: for v in range(int(MIN_VALUE), int(MAX_VALUE*MAX_VALUE_MULTIPLIER)): self.safety.set_controls_allowed(enabled) - if (not enabled and v != INACTIVE_VALUE) or v > MAX_VALUE: + if (not enabled and v != INACTIVE_VALUE) or v > MAX_VALUE or v < MIN_VALUE: self.assertFalse(self._tx(msg_function(v))) else: self.assertTrue(self._tx(msg_function(v))) diff --git a/tests/safety/test_subaru.py b/tests/safety/test_subaru.py index fba801acc7..551b68a68a 100755 --- a/tests/safety/test_subaru.py +++ b/tests/safety/test_subaru.py @@ -112,7 +112,7 @@ class TestSubaruGen2SafetyBase(TestSubaruSafetyBase): class TestSubaruLongitudinalSafetyBase(TestSubaruSafetyBase, common.LongitudinalGasBrakeSafetyTest): MAX_THROTTLE = 3400 - MAX_BRAKE = 400 + MAX_BRAKE = 600 INACTIVE_THROTTLE = 1818 FWD_BLACKLISTED_ADDRS = {0: [MSG_SUBARU_CruiseControl, MSG_SUBARU_Brake_Status], From 2c7e0fdb8d63b5a50a287dd31bf6b274d404ab0c Mon Sep 17 00:00:00 2001 From: Justin Newberry Date: Fri, 14 Jul 2023 17:20:08 -0700 Subject: [PATCH 056/113] revert that --- tests/libpanda/safety_helpers.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/libpanda/safety_helpers.py b/tests/libpanda/safety_helpers.py index 923d8ce425..3cf8aae04d 100644 --- a/tests/libpanda/safety_helpers.py +++ b/tests/libpanda/safety_helpers.py @@ -95,4 +95,5 @@ def init_tests(self) -> None: ... def set_honda_fwd_brake(self, c: bool) -> None: ... def set_honda_alt_brake_msg(self, c: bool) -> None: ... def set_honda_bosch_long(self, c: bool) -> None: ... - def get_honda_hw(self) -> int: ... \ No newline at end of file + def get_honda_hw(self) -> int: ... + From 5006eb2ca531fcf72e701da2fadb803a341c8c22 Mon Sep 17 00:00:00 2001 From: Justin Newberry Date: Fri, 14 Jul 2023 17:23:55 -0700 Subject: [PATCH 057/113] cleanup --- tests/safety/test_subaru.py | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/tests/safety/test_subaru.py b/tests/safety/test_subaru.py index 0b53d3a8d2..c8c3b800e0 100755 --- a/tests/safety/test_subaru.py +++ b/tests/safety/test_subaru.py @@ -56,17 +56,6 @@ class TestSubaruSafetyBase(common.PandaSafetyTest, common.DriverTorqueSteeringSa DRIVER_TORQUE_FACTOR = 50 ALT_BUS = SUBARU_MAIN_BUS - - MAX_RATE_UP = 50 - MAX_RATE_DOWN = 70 - MAX_TORQUE = 2047 - - @classmethod - def setUpClass(cls): - if cls.__name__.endswith("Base"): - cls.packer = None - cls.safety = None - raise unittest.SkipTest def setUp(self): self.packer = CANPackerPanda("subaru_global_2017_generated") From 706fd78c21650f29cd9ddaf47992b171a2e7f477 Mon Sep 17 00:00:00 2001 From: Justin Newberry Date: Fri, 14 Jul 2023 18:53:43 -0700 Subject: [PATCH 058/113] rename throttle to gas --- tests/safety/common.py | 20 ++++++++++---------- tests/safety/test_subaru.py | 8 ++++---- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/tests/safety/common.py b/tests/safety/common.py index 95b133358b..2f5dbf78c1 100644 --- a/tests/safety/common.py +++ b/tests/safety/common.py @@ -178,10 +178,10 @@ class LongitudinalGasBrakeSafetyTest(PandaSafetyTestBase, abc.ABC): MIN_BRAKE: int = 0 MAX_BRAKE: Optional[int] = None - MIN_THROTTLE: int = 1818 - MAX_THROTTLE: Optional[int] = None + MIN_GAS: int = 1818 + MAX_GAS: Optional[int] = None - INACTIVE_THROTTLE: Optional[int] = 0 + INACTIVE_GAS: Optional[int] = 0 @classmethod def setUpClass(cls): @@ -190,7 +190,7 @@ def setUpClass(cls): raise unittest.SkipTest @abc.abstractmethod - def _throttle_msg(self, throttle: int): + def _gas_msg(self, gas: int): pass @abc.abstractmethod @@ -200,16 +200,16 @@ def _brake_msg(self, brake: int): def test_gas_brake_limits_correct(self): # Assert that max brake and max throttle is set self.assertTrue(self.MAX_BRAKE is not None) - self.assertTrue(self.MAX_THROTTLE is not None) + self.assertTrue(self.MAX_GAS is not None) self.assertGreater(self.MAX_BRAKE, self.MIN_BRAKE) - self.assertGreater(self.MAX_THROTTLE, self.MIN_THROTTLE) + self.assertGreater(self.MAX_GAS, self.MIN_GAS) - def _generic_limit_safety_check(self, msg_function, MIN_VALUE, MAX_VALUE, MAX_VALUE_MULTIPLIER, INACTIVE_VALUE=0): + def _generic_limit_safety_check(self, msg_function, min_value, max_value, max_value_multiplier, inactive_value=0): for enabled in [0, 1]: - for v in range(int(MIN_VALUE), int(MAX_VALUE*MAX_VALUE_MULTIPLIER)): + for v in range(int(min_value), int(max_value*max_value_multiplier)): self.safety.set_controls_allowed(enabled) - if (not enabled and v != INACTIVE_VALUE) or v > MAX_VALUE or v < MIN_VALUE: + if (not enabled and v != inactive_value) or v > max_value or v < min_value: self.assertFalse(self._tx(msg_function(v))) else: self.assertTrue(self._tx(msg_function(v))) @@ -218,7 +218,7 @@ def test_brake_safety_check(self): self._generic_limit_safety_check(self._brake_msg, self.MIN_BRAKE, self.MAX_BRAKE, 1.5) def test_throttle_safety_check(self): - self._generic_limit_safety_check(self._throttle_msg, self.MIN_THROTTLE, self.MAX_THROTTLE, 1.2, self.INACTIVE_THROTTLE) + self._generic_limit_safety_check(self._gas_msg, self.MIN_GAS, self.MAX_GAS, 1.2, self.INACTIVE_GAS) class TorqueSteeringSafetyTestBase(PandaSafetyTestBase, abc.ABC): diff --git a/tests/safety/test_subaru.py b/tests/safety/test_subaru.py index c8c3b800e0..4734b63014 100755 --- a/tests/safety/test_subaru.py +++ b/tests/safety/test_subaru.py @@ -103,9 +103,9 @@ class TestSubaruGen2SafetyBase(TestSubaruSafetyBase): class TestSubaruLongitudinalSafetyBase(TestSubaruSafetyBase, common.LongitudinalGasBrakeSafetyTest): - MAX_THROTTLE = 3400 + MAX_GAS = 3400 MAX_BRAKE = 600 - INACTIVE_THROTTLE = 1818 + INACTIVE_GAS = 1818 FWD_BLACKLISTED_ADDRS = {0: [MSG_SUBARU_CruiseControl, MSG_SUBARU_Brake_Status], 2: [MSG_SUBARU_ES_LKAS, MSG_SUBARU_ES_Brake, MSG_SUBARU_ES_Distance, @@ -116,8 +116,8 @@ def _brake_msg(self, brake): values = {"Brake_Pressure": brake} return self.packer.make_can_msg_panda("ES_Brake", self.ALT_BUS, values) - def _throttle_msg(self, throttle): - values = {"Cruise_Throttle": throttle} + def _gas_msg(self, gas): + values = {"Cruise_Throttle": gas} return self.packer.make_can_msg_panda("ES_Distance", self.ALT_BUS, values) def _rpm_msg(self, rpm): From 41e4a4bd0da7e404d60f0c3494ec568d5e0ce6c4 Mon Sep 17 00:00:00 2001 From: Justin Newberry Date: Sat, 15 Jul 2023 09:15:08 -0700 Subject: [PATCH 059/113] add safety replay --- tests/safety_replay/test_safety_replay.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/safety_replay/test_safety_replay.py b/tests/safety_replay/test_safety_replay.py index d38ab11e02..a1529167a4 100755 --- a/tests/safety_replay/test_safety_replay.py +++ b/tests/safety_replay/test_safety_replay.py @@ -14,8 +14,8 @@ logs = [ # TODO: need a route with updated safety stuff - #ReplayRoute("8de015561e1ea4a0|2023-07-02--20-14-07--9--rlog.bz2", Panda.SAFETY_SUBARU, # SUBARU.CROSSTREK (openpilot long) - # Panda.FLAG_SUBARU_LONG, ALT_EXP.DISABLE_DISENGAGE_ON_GAS), + ReplayRoute("8de015561e1ea4a0|2023-07-15--09-44-25--0--rlog.bz2", Panda.SAFETY_SUBARU, # SUBARU.CROSSTREK (openpilot long) + Panda.FLAG_SUBARU_LONG, ALT_EXP.DISABLE_DISENGAGE_ON_GAS), ReplayRoute("2425568437959f9d|2019-12-22--16-24-37.bz2", Panda.SAFETY_HONDA_NIDEC), # HONDA.CIVIC (fcw presents: 0x1FA blocked as expected) ReplayRoute("38bfd238edecbcd7|2019-06-07--10-15-25.bz2", Panda.SAFETY_TOYOTA, 66), # TOYOTA.PRIUS ReplayRoute("f89c604cf653e2bf|2018-09-29--13-46-50.bz2", Panda.SAFETY_GM), # GM.VOLT From 0397443e2f12a381180ec0bc7b400650d1dfcc6d Mon Sep 17 00:00:00 2001 From: Justin Newberry Date: Sat, 15 Jul 2023 15:02:28 -0700 Subject: [PATCH 060/113] remove todo --- tests/safety_replay/test_safety_replay.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/safety_replay/test_safety_replay.py b/tests/safety_replay/test_safety_replay.py index a1529167a4..2c9d68c2c2 100755 --- a/tests/safety_replay/test_safety_replay.py +++ b/tests/safety_replay/test_safety_replay.py @@ -13,7 +13,6 @@ ReplayRoute = namedtuple("ReplayRoute", ("route", "safety_mode", "param", "alternative_experience"), defaults=(0, 0)) logs = [ - # TODO: need a route with updated safety stuff ReplayRoute("8de015561e1ea4a0|2023-07-15--09-44-25--0--rlog.bz2", Panda.SAFETY_SUBARU, # SUBARU.CROSSTREK (openpilot long) Panda.FLAG_SUBARU_LONG, ALT_EXP.DISABLE_DISENGAGE_ON_GAS), ReplayRoute("2425568437959f9d|2019-12-22--16-24-37.bz2", Panda.SAFETY_HONDA_NIDEC), # HONDA.CIVIC (fcw presents: 0x1FA blocked as expected) From 3429b8dc1814ca15cf4d0a0a516b07f8fbe9dd97 Mon Sep 17 00:00:00 2001 From: Justin Newberry Date: Mon, 17 Jul 2023 10:46:23 -0700 Subject: [PATCH 061/113] rename throttle to gas --- tests/safety/common.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/safety/common.py b/tests/safety/common.py index 2f5dbf78c1..51845f3cda 100644 --- a/tests/safety/common.py +++ b/tests/safety/common.py @@ -198,7 +198,7 @@ def _brake_msg(self, brake: int): pass def test_gas_brake_limits_correct(self): - # Assert that max brake and max throttle is set + # Assert that max brake and max gas is set self.assertTrue(self.MAX_BRAKE is not None) self.assertTrue(self.MAX_GAS is not None) @@ -217,7 +217,7 @@ def _generic_limit_safety_check(self, msg_function, min_value, max_value, max_va def test_brake_safety_check(self): self._generic_limit_safety_check(self._brake_msg, self.MIN_BRAKE, self.MAX_BRAKE, 1.5) - def test_throttle_safety_check(self): + def test_gas_safety_check(self): self._generic_limit_safety_check(self._gas_msg, self.MIN_GAS, self.MAX_GAS, 1.2, self.INACTIVE_GAS) From de43707fe0a3c3a3d177f762ebfc52f5526fa945 Mon Sep 17 00:00:00 2001 From: Justin Newberry Date: Mon, 17 Jul 2023 10:48:54 -0700 Subject: [PATCH 062/113] move subaru stuff out of common test --- tests/safety/common.py | 4 ++-- tests/safety/test_subaru.py | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/tests/safety/common.py b/tests/safety/common.py index 51845f3cda..233039abad 100644 --- a/tests/safety/common.py +++ b/tests/safety/common.py @@ -178,7 +178,7 @@ class LongitudinalGasBrakeSafetyTest(PandaSafetyTestBase, abc.ABC): MIN_BRAKE: int = 0 MAX_BRAKE: Optional[int] = None - MIN_GAS: int = 1818 + MIN_GAS: int = 0 MAX_GAS: Optional[int] = None INACTIVE_GAS: Optional[int] = 0 @@ -198,7 +198,7 @@ def _brake_msg(self, brake: int): pass def test_gas_brake_limits_correct(self): - # Assert that max brake and max gas is set + # Assert that max brake and max throttle is set self.assertTrue(self.MAX_BRAKE is not None) self.assertTrue(self.MAX_GAS is not None) diff --git a/tests/safety/test_subaru.py b/tests/safety/test_subaru.py index 4734b63014..c9f5d80525 100755 --- a/tests/safety/test_subaru.py +++ b/tests/safety/test_subaru.py @@ -104,8 +104,9 @@ class TestSubaruGen2SafetyBase(TestSubaruSafetyBase): class TestSubaruLongitudinalSafetyBase(TestSubaruSafetyBase, common.LongitudinalGasBrakeSafetyTest): MAX_GAS = 3400 - MAX_BRAKE = 600 INACTIVE_GAS = 1818 + + MAX_BRAKE = 600 FWD_BLACKLISTED_ADDRS = {0: [MSG_SUBARU_CruiseControl, MSG_SUBARU_Brake_Status], 2: [MSG_SUBARU_ES_LKAS, MSG_SUBARU_ES_Brake, MSG_SUBARU_ES_Distance, From 339c343de9b2ad09be00635c68bb01da7cf1efbd Mon Sep 17 00:00:00 2001 From: Justin Newberry Date: Mon, 31 Jul 2023 12:52:52 -0700 Subject: [PATCH 063/113] PR cleanup --- board/safety/safety_subaru.h | 10 +++++----- tests/libpanda/safety_helpers.py | 1 - tests/safety/common.py | 2 ++ tests/safety/test_subaru.py | 2 +- 4 files changed, 8 insertions(+), 7 deletions(-) diff --git a/board/safety/safety_subaru.h b/board/safety/safety_subaru.h index 06356f86ba..0a2da96d0a 100644 --- a/board/safety/safety_subaru.h +++ b/board/safety/safety_subaru.h @@ -16,7 +16,7 @@ const SteeringLimits SUBARU_GEN2_STEERING_LIMITS = SUBARU_STEERING_LIMITS_GENERA const LongitudinalLimits SUBARU_LONG_LIMITS = { .max_gas = 3400, // approx 1.2 m/s^2 at low speeds, less at higher speeds - .inactive_gas = 1818, // this is zero acceleration + .inactive_gas = 1818, // this is zero acceleration, and 808 appears to be engine braking .max_brake = 600, // approx -3.5 m/s^2 }; @@ -153,7 +153,6 @@ static int subaru_rx_hook(CANPacket_t *to_push) { return valid; } - static int subaru_tx_hook(CANPacket_t *to_send) { int tx = 1; @@ -174,9 +173,7 @@ static int subaru_tx_hook(CANPacket_t *to_send) { desired_torque = -1 * to_signed(desired_torque, 13); const SteeringLimits limits = subaru_gen2 ? SUBARU_GEN2_STEERING_LIMITS : SUBARU_STEERING_LIMITS; - if (steer_torque_cmd_checks(desired_torque, -1, limits)) { - tx = 0; - } + violation |= steer_torque_cmd_checks(desired_torque, -1, limits); } if (subaru_longitudinal) { @@ -204,6 +201,9 @@ static int subaru_fwd_hook(int bus_num, int addr) { if (bus_num == SUBARU_MAIN_BUS) { // Global Platform + + // we need to intercept CruiseControl to keep eyesight in a "ready" state, + // and Brake_Staus to fake the ES_Brake feedback signal to eyesight bool block_msg = subaru_longitudinal && ((addr == MSG_SUBARU_Brake_Status) || (addr == MSG_SUBARU_CruiseControl)); if (!block_msg) { bus_fwd = SUBARU_CAM_BUS; // to the eyesight camera diff --git a/tests/libpanda/safety_helpers.py b/tests/libpanda/safety_helpers.py index 4ab587409f..31adf51070 100644 --- a/tests/libpanda/safety_helpers.py +++ b/tests/libpanda/safety_helpers.py @@ -98,4 +98,3 @@ def set_honda_fwd_brake(self, c: bool) -> None: ... def set_honda_alt_brake_msg(self, c: bool) -> None: ... def set_honda_bosch_long(self, c: bool) -> None: ... def get_honda_hw(self) -> int: ... - diff --git a/tests/safety/common.py b/tests/safety/common.py index fbd73de574..ab6f50d5b7 100644 --- a/tests/safety/common.py +++ b/tests/safety/common.py @@ -137,6 +137,7 @@ def test_gas_interceptor_safety_check(self): class LongitudinalAccelSafetyTest(PandaSafetyTestBase, abc.ABC): + MAX_ACCEL: float = 2.0 MIN_ACCEL: float = -3.5 INACTIVE_ACCEL: float = 0.0 @@ -175,6 +176,7 @@ def test_accel_actuation_limits(self, stock_longitudinal=False): class LongitudinalGasBrakeSafetyTest(PandaSafetyTestBase, abc.ABC): + MIN_BRAKE: int = 0 MAX_BRAKE: Optional[int] = None diff --git a/tests/safety/test_subaru.py b/tests/safety/test_subaru.py index c9f5d80525..71abb08983 100755 --- a/tests/safety/test_subaru.py +++ b/tests/safety/test_subaru.py @@ -56,7 +56,7 @@ class TestSubaruSafetyBase(common.PandaSafetyTest, common.DriverTorqueSteeringSa DRIVER_TORQUE_FACTOR = 50 ALT_BUS = SUBARU_MAIN_BUS - + def setUp(self): self.packer = CANPackerPanda("subaru_global_2017_generated") self.safety = libpanda_py.libpanda From ef909e804f984e117967c741a29acb46a623bdaf Mon Sep 17 00:00:00 2001 From: Justin Newberry Date: Mon, 31 Jul 2023 13:05:52 -0700 Subject: [PATCH 064/113] added min gas --- board/safety/safety_subaru.h | 1 + tests/safety/test_subaru.py | 1 + 2 files changed, 2 insertions(+) diff --git a/board/safety/safety_subaru.h b/board/safety/safety_subaru.h index 0a2da96d0a..afe3fac6ab 100644 --- a/board/safety/safety_subaru.h +++ b/board/safety/safety_subaru.h @@ -15,6 +15,7 @@ const SteeringLimits SUBARU_GEN2_STEERING_LIMITS = SUBARU_STEERING_LIMITS_GENERA const LongitudinalLimits SUBARU_LONG_LIMITS = { + .min_gas = 808, .max_gas = 3400, // approx 1.2 m/s^2 at low speeds, less at higher speeds .inactive_gas = 1818, // this is zero acceleration, and 808 appears to be engine braking .max_brake = 600, // approx -3.5 m/s^2 diff --git a/tests/safety/test_subaru.py b/tests/safety/test_subaru.py index 71abb08983..be6c3844ba 100755 --- a/tests/safety/test_subaru.py +++ b/tests/safety/test_subaru.py @@ -103,6 +103,7 @@ class TestSubaruGen2SafetyBase(TestSubaruSafetyBase): class TestSubaruLongitudinalSafetyBase(TestSubaruSafetyBase, common.LongitudinalGasBrakeSafetyTest): + MIN_GAS = 808 MAX_GAS = 3400 INACTIVE_GAS = 1818 From 1b2886b3238a232b33abfe6af93f170b12daf0e0 Mon Sep 17 00:00:00 2001 From: Justin Newberry Date: Tue, 1 Aug 2023 10:00:32 -0700 Subject: [PATCH 065/113] reduce initial complexity by not intercepting cruisecontrol or brake_status --- board/safety/safety_subaru.h | 11 +---------- tests/safety/test_subaru.py | 4 +--- 2 files changed, 2 insertions(+), 13 deletions(-) diff --git a/board/safety/safety_subaru.h b/board/safety/safety_subaru.h index afe3fac6ab..d33f3fcab4 100644 --- a/board/safety/safety_subaru.h +++ b/board/safety/safety_subaru.h @@ -49,8 +49,6 @@ const LongitudinalLimits SUBARU_LONG_LIMITS = { #define SUBARU_COMMON_LONG_TX_MSGS(alt_bus) \ {MSG_SUBARU_ES_Brake, SUBARU_MAIN_BUS, 8}, \ {MSG_SUBARU_ES_Status, SUBARU_MAIN_BUS, 8}, \ - {MSG_SUBARU_Brake_Status, SUBARU_CAM_BUS, 8}, \ - {MSG_SUBARU_CruiseControl, SUBARU_CAM_BUS, 8}, \ #define SUBARU_COMMON_ADDR_CHECKS(alt_bus) \ {.msg = {{MSG_SUBARU_Throttle, SUBARU_MAIN_BUS, 8, .check_checksum = true, .max_counter = 15U, .expected_timestep = 10000U}, { 0 }, { 0 }}}, \ @@ -201,14 +199,7 @@ static int subaru_fwd_hook(int bus_num, int addr) { int bus_fwd = -1; if (bus_num == SUBARU_MAIN_BUS) { - // Global Platform - - // we need to intercept CruiseControl to keep eyesight in a "ready" state, - // and Brake_Staus to fake the ES_Brake feedback signal to eyesight - bool block_msg = subaru_longitudinal && ((addr == MSG_SUBARU_Brake_Status) || (addr == MSG_SUBARU_CruiseControl)); - if (!block_msg) { - bus_fwd = SUBARU_CAM_BUS; // to the eyesight camera - } + bus_fwd = SUBARU_CAM_BUS; // to the eyesight camera } if (bus_num == SUBARU_CAM_BUS) { diff --git a/tests/safety/test_subaru.py b/tests/safety/test_subaru.py index be6c3844ba..76164071df 100755 --- a/tests/safety/test_subaru.py +++ b/tests/safety/test_subaru.py @@ -33,9 +33,7 @@ def lkas_tx_msgs(alt_bus): def long_tx_msgs(): return [[MSG_SUBARU_ES_Brake, SUBARU_MAIN_BUS], - [MSG_SUBARU_ES_Status, SUBARU_MAIN_BUS], - [MSG_SUBARU_CruiseControl, SUBARU_CAM_BUS], - [MSG_SUBARU_Brake_Status, SUBARU_CAM_BUS]] + [MSG_SUBARU_ES_Status, SUBARU_MAIN_BUS]] class TestSubaruSafetyBase(common.PandaSafetyTest, common.DriverTorqueSteeringSafetyTest): FLAGS = 0 From 1fd74296f4cb14c0d068be3263e13d042e05bf0f Mon Sep 17 00:00:00 2001 From: Justin Newberry Date: Tue, 1 Aug 2023 10:12:10 -0700 Subject: [PATCH 066/113] create common gas brake safety test --- tests/safety/common.py | 50 ++++++++++++++++++++++++++++++++++++++++- tests/safety/test_gm.py | 49 +++++++++++----------------------------- 2 files changed, 62 insertions(+), 37 deletions(-) diff --git a/tests/safety/common.py b/tests/safety/common.py index 1b141d85e6..6dbc5e0bfe 100644 --- a/tests/safety/common.py +++ b/tests/safety/common.py @@ -175,6 +175,54 @@ def test_accel_actuation_limits(self, stock_longitudinal=False): self.assertEqual(should_tx, self._tx(self._accel_msg(accel))) +class LongitudinalGasBrakeSafetyTest(PandaSafetyTestBase, abc.ABC): + + MIN_BRAKE: int = 0 + MAX_BRAKE: Optional[int] = None + + MIN_GAS: int = 0 + MAX_GAS: Optional[int] = None + + INACTIVE_GAS: Optional[int] = 0 + + @classmethod + def setUpClass(cls): + if cls.__name__ == "LongitudinalGasBrakeSafetyTest": + cls.safety = None + raise unittest.SkipTest + + @abc.abstractmethod + def _gas_msg(self, gas: int): + pass + + @abc.abstractmethod + def _brake_msg(self, brake: int): + pass + + def test_gas_brake_limits_correct(self): + # Assert that max brake and max throttle is set + self.assertTrue(self.MAX_BRAKE is not None) + self.assertTrue(self.MAX_GAS is not None) + + self.assertGreater(self.MAX_BRAKE, self.MIN_BRAKE) + self.assertGreater(self.MAX_GAS, self.MIN_GAS) + + def _generic_limit_safety_check(self, msg_function, min_value, max_value, max_value_multiplier, inactive_value=0): + for enabled in [0, 1]: + for v in range(int(min_value), int(max_value*max_value_multiplier)): + self.safety.set_controls_allowed(enabled) + if (not enabled and v != inactive_value) or v > max_value or v < min_value: + self.assertFalse(self._tx(msg_function(v)), (v, min_value, max_value)) + else: + self.assertTrue(self._tx(msg_function(v)), (v, min_value, max_value)) + + def test_brake_safety_check(self): + self._generic_limit_safety_check(self._brake_msg, self.MIN_BRAKE, self.MAX_BRAKE, 1.5) + + def test_gas_safety_check(self): + self._generic_limit_safety_check(self._gas_msg, self.MIN_GAS, self.MAX_GAS, 1.2, self.INACTIVE_GAS) + + class TorqueSteeringSafetyTestBase(PandaSafetyTestBase, abc.ABC): MAX_RATE_UP = 0 @@ -895,7 +943,7 @@ def test_tx_hook_on_wrong_safety_mode(self): continue if attr.startswith('TestToyota') and current_test.startswith('TestToyota'): continue - if {attr, current_test}.issubset({'TestSubaruGen1Safety', 'TestSubaruGen2Safety'}): + if {attr, current_test}.issubset({'TestSubaruGen1Safety', 'TestSubaruGen2Safety', 'TestSubaruGen1LongitudinalSafety'}): continue if {attr, current_test}.issubset({'TestVolkswagenPqSafety', 'TestVolkswagenPqStockSafety', 'TestVolkswagenPqLongSafety'}): continue diff --git a/tests/safety/test_gm.py b/tests/safety/test_gm.py index f6b604ad31..662b46d359 100755 --- a/tests/safety/test_gm.py +++ b/tests/safety/test_gm.py @@ -14,7 +14,7 @@ class Buttons: CANCEL = 6 -class GmLongitudinalBase(common.PandaSafetyTest): +class GmLongitudinalBase(common.PandaSafetyTest, common.LongitudinalGasBrakeSafetyTest): # pylint: disable=no-member,abstract-method PCM_CRUISE = False # openpilot can control the PCM state if longitudinal @@ -53,6 +53,14 @@ def test_cruise_engaged_prev(self): def _pcm_status_msg(self, enable): pass + + def _brake_msg(self, brake): + values = {"FrictionBrakeCmd": -brake} + return self.packer_chassis.make_can_msg_panda("EBCMFrictionBrakeCmd", self.BRAKE_BUS, values) + + def _gas_msg(self, gas): + values = {"GasRegenCmd": gas} + return self.packer.make_can_msg_panda("ASCMGasRegenCmd", 0, values) class TestGmSafetyBase(common.PandaSafetyTest, common.DriverTorqueSteeringSafetyTest): @@ -70,11 +78,6 @@ class TestGmSafetyBase(common.PandaSafetyTest, common.DriverTorqueSteeringSafety DRIVER_TORQUE_ALLOWANCE = 65 DRIVER_TORQUE_FACTOR = 4 - MAX_GAS = 0 - MAX_REGEN = 0 - INACTIVE_REGEN = 0 - MAX_BRAKE = 0 - PCM_CRUISE = True # openpilot is tied to the PCM state if not longitudinal @classmethod @@ -118,14 +121,6 @@ def _user_gas_msg(self, gas): values["CruiseState"] = self.safety.get_controls_allowed() return self.packer.make_can_msg_panda("AcceleratorPedal2", 0, values) - def _send_brake_msg(self, brake): - values = {"FrictionBrakeCmd": -brake} - return self.packer_chassis.make_can_msg_panda("EBCMFrictionBrakeCmd", self.BRAKE_BUS, values) - - def _send_gas_msg(self, gas): - values = {"GasRegenCmd": gas} - return self.packer.make_can_msg_panda("ASCMGasRegenCmd", 0, values) - def _torque_driver_msg(self, torque): values = {"LKADriverAppldTrq": torque} return self.packer.make_can_msg_panda("PSCMStatus", 0, values) @@ -138,24 +133,6 @@ def _button_msg(self, buttons): values = {"ACCButtons": buttons} return self.packer.make_can_msg_panda("ASCMSteeringButton", self.BUTTONS_BUS, values) - def test_brake_safety_check(self): - for enabled in [0, 1]: - for b in range(0, 500): - self.safety.set_controls_allowed(enabled) - if abs(b) > self.MAX_BRAKE or (not enabled and b != 0): - self.assertFalse(self._tx(self._send_brake_msg(b))) - else: - self.assertTrue(self._tx(self._send_brake_msg(b))) - - def test_gas_safety_check(self): - # Block if enabled and out of actuation range, disabled and not inactive regen, or if stock longitudinal - for enabled in [0, 1]: - for gas_regen in range(0, 2 ** 12 - 1): - self.safety.set_controls_allowed(enabled) - should_tx = ((enabled and self.MAX_REGEN <= gas_regen <= self.MAX_GAS) or - gas_regen == self.INACTIVE_REGEN) - self.assertEqual(should_tx, self._tx(self._send_gas_msg(gas_regen)), (enabled, gas_regen)) - class TestGmAscmSafety(GmLongitudinalBase, TestGmSafetyBase): TX_MSGS = [[384, 0], [1033, 0], [1034, 0], [715, 0], [880, 0], # pt bus @@ -167,8 +144,8 @@ class TestGmAscmSafety(GmLongitudinalBase, TestGmSafetyBase): BRAKE_BUS = 2 MAX_GAS = 3072 - MAX_REGEN = 1404 - INACTIVE_REGEN = 1404 + MIN_GAS = 1404 + INACTIVE_GAS = 1404 MAX_BRAKE = 400 def setUp(self): @@ -237,8 +214,8 @@ class TestGmCameraLongitudinalSafety(GmLongitudinalBase, TestGmCameraSafetyBase) BUTTONS_BUS = 0 # rx only MAX_GAS = 3400 - MAX_REGEN = 1514 - INACTIVE_REGEN = 1554 + MIN_GAS = 1514 # Max regen + INACTIVE_GAS = 1554 MAX_BRAKE = 400 def setUp(self): From 2c2b95ea1e350da60b4c65b54083144574ad6cff Mon Sep 17 00:00:00 2001 From: Justin Newberry Date: Tue, 1 Aug 2023 10:14:51 -0700 Subject: [PATCH 067/113] remove unrelated subaru reference --- tests/safety/common.py | 2 +- tests/safety/test_gm.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/safety/common.py b/tests/safety/common.py index 6dbc5e0bfe..b4e8f3e8d3 100644 --- a/tests/safety/common.py +++ b/tests/safety/common.py @@ -943,7 +943,7 @@ def test_tx_hook_on_wrong_safety_mode(self): continue if attr.startswith('TestToyota') and current_test.startswith('TestToyota'): continue - if {attr, current_test}.issubset({'TestSubaruGen1Safety', 'TestSubaruGen2Safety', 'TestSubaruGen1LongitudinalSafety'}): + if {attr, current_test}.issubset({'TestSubaruGen1Safety', 'TestSubaruGen2Safety'}): continue if {attr, current_test}.issubset({'TestVolkswagenPqSafety', 'TestVolkswagenPqStockSafety', 'TestVolkswagenPqLongSafety'}): continue diff --git a/tests/safety/test_gm.py b/tests/safety/test_gm.py index 662b46d359..01557bd432 100755 --- a/tests/safety/test_gm.py +++ b/tests/safety/test_gm.py @@ -144,7 +144,7 @@ class TestGmAscmSafety(GmLongitudinalBase, TestGmSafetyBase): BRAKE_BUS = 2 MAX_GAS = 3072 - MIN_GAS = 1404 + MIN_GAS = 1404 # Max Regen INACTIVE_GAS = 1404 MAX_BRAKE = 400 From c9bcbc52633d2a79c93e2b8aee10f027c3999339 Mon Sep 17 00:00:00 2001 From: Justin Newberry Date: Tue, 1 Aug 2023 10:17:15 -0700 Subject: [PATCH 068/113] also test below min_value --- tests/safety/common.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/safety/common.py b/tests/safety/common.py index b4e8f3e8d3..f65c119091 100644 --- a/tests/safety/common.py +++ b/tests/safety/common.py @@ -209,7 +209,7 @@ def test_gas_brake_limits_correct(self): def _generic_limit_safety_check(self, msg_function, min_value, max_value, max_value_multiplier, inactive_value=0): for enabled in [0, 1]: - for v in range(int(min_value), int(max_value*max_value_multiplier)): + for v in range(0, int(max_value*max_value_multiplier)): self.safety.set_controls_allowed(enabled) if (not enabled and v != inactive_value) or v > max_value or v < min_value: self.assertFalse(self._tx(msg_function(v)), (v, min_value, max_value)) From d7607239e6041a3b5c461ec1087eb44fe461a906 Mon Sep 17 00:00:00 2001 From: Justin Newberry Date: Tue, 1 Aug 2023 10:44:24 -0700 Subject: [PATCH 069/113] fix fwd hook test --- tests/safety/test_subaru.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/safety/test_subaru.py b/tests/safety/test_subaru.py index 76164071df..50483749d7 100755 --- a/tests/safety/test_subaru.py +++ b/tests/safety/test_subaru.py @@ -107,8 +107,7 @@ class TestSubaruLongitudinalSafetyBase(TestSubaruSafetyBase, common.Longitudinal MAX_BRAKE = 600 - FWD_BLACKLISTED_ADDRS = {0: [MSG_SUBARU_CruiseControl, MSG_SUBARU_Brake_Status], - 2: [MSG_SUBARU_ES_LKAS, MSG_SUBARU_ES_Brake, MSG_SUBARU_ES_Distance, + FWD_BLACKLISTED_ADDRS = {2: [MSG_SUBARU_ES_LKAS, MSG_SUBARU_ES_Brake, MSG_SUBARU_ES_Distance, MSG_SUBARU_ES_Status, MSG_SUBARU_ES_DashStatus, MSG_SUBARU_ES_LKAS_State, MSG_SUBARU_ES_Infotainment]} From 5156dac113e91c9da4693f9d24694b74d5e1c16a Mon Sep 17 00:00:00 2001 From: Justin Newberry Date: Tue, 1 Aug 2023 14:35:45 -0700 Subject: [PATCH 070/113] better name and hardcoded --- tests/safety/common.py | 16 ++++++++-------- tests/safety/test_gm.py | 7 ------- 2 files changed, 8 insertions(+), 15 deletions(-) diff --git a/tests/safety/common.py b/tests/safety/common.py index f65c119091..4bda844e18 100644 --- a/tests/safety/common.py +++ b/tests/safety/common.py @@ -3,7 +3,7 @@ import unittest import importlib import numpy as np -from typing import Dict, List, Optional +from typing import Callable, Dict, List, Optional from opendbc.can.packer import CANPacker # pylint: disable=import-error from panda import ALTERNATIVE_EXPERIENCE @@ -207,20 +207,20 @@ def test_gas_brake_limits_correct(self): self.assertGreater(self.MAX_BRAKE, self.MIN_BRAKE) self.assertGreater(self.MAX_GAS, self.MIN_GAS) - def _generic_limit_safety_check(self, msg_function, min_value, max_value, max_value_multiplier, inactive_value=0): + def _generic_limit_safety_check(self, msg_function, min_allowed_value: int, max_allowed_value: int, inactive_value=0): for enabled in [0, 1]: - for v in range(0, int(max_value*max_value_multiplier)): + for v in range(0, max_allowed_value+1): self.safety.set_controls_allowed(enabled) - if (not enabled and v != inactive_value) or v > max_value or v < min_value: - self.assertFalse(self._tx(msg_function(v)), (v, min_value, max_value)) + if (not enabled and v != inactive_value) or v > max_allowed_value or v < min_allowed_value: + self.assertFalse(self._tx(msg_function(v)), (v, min_allowed_value, max_allowed_value)) else: - self.assertTrue(self._tx(msg_function(v)), (v, min_value, max_value)) + self.assertTrue(self._tx(msg_function(v)), (v, min_allowed_value, max_allowed_value)) def test_brake_safety_check(self): - self._generic_limit_safety_check(self._brake_msg, self.MIN_BRAKE, self.MAX_BRAKE, 1.5) + self._generic_limit_safety_check(self._brake_msg, self.MIN_BRAKE, self.MAX_BRAKE) def test_gas_safety_check(self): - self._generic_limit_safety_check(self._gas_msg, self.MIN_GAS, self.MAX_GAS, 1.2, self.INACTIVE_GAS) + self._generic_limit_safety_check(self._gas_msg, self.MIN_GAS, self.MAX_GAS, self.INACTIVE_GAS) class TorqueSteeringSafetyTestBase(PandaSafetyTestBase, abc.ABC): diff --git a/tests/safety/test_gm.py b/tests/safety/test_gm.py index 01557bd432..756a735270 100755 --- a/tests/safety/test_gm.py +++ b/tests/safety/test_gm.py @@ -199,13 +199,6 @@ def test_buttons(self): self._rx(self._pcm_status_msg(enabled)) self.assertEqual(enabled, self._tx(self._button_msg(Buttons.CANCEL))) - # GM Cam safety mode does not allow longitudinal messages - def test_brake_safety_check(self): - pass - - def test_gas_safety_check(self): - pass - class TestGmCameraLongitudinalSafety(GmLongitudinalBase, TestGmCameraSafetyBase): TX_MSGS = [[384, 0], [789, 0], [715, 0], [880, 0], # pt bus From c1aa9763198d4971b44a0134e910113494f5c7be Mon Sep 17 00:00:00 2001 From: Justin Newberry Date: Tue, 1 Aug 2023 14:44:13 -0700 Subject: [PATCH 071/113] use same as acceleration limits --- tests/safety/common.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/safety/common.py b/tests/safety/common.py index 4bda844e18..01ad720dfd 100644 --- a/tests/safety/common.py +++ b/tests/safety/common.py @@ -209,7 +209,7 @@ def test_gas_brake_limits_correct(self): def _generic_limit_safety_check(self, msg_function, min_allowed_value: int, max_allowed_value: int, inactive_value=0): for enabled in [0, 1]: - for v in range(0, max_allowed_value+1): + for v in np.concatenate((np.arange(min_allowed_value - 1, max_allowed_value + 1, 1), [0, inactive_value])): self.safety.set_controls_allowed(enabled) if (not enabled and v != inactive_value) or v > max_allowed_value or v < min_allowed_value: self.assertFalse(self._tx(msg_function(v)), (v, min_allowed_value, max_allowed_value)) From dd3ea6bf09ffc6d3e1c66945ea91c19ba4d82ec2 Mon Sep 17 00:00:00 2001 From: Justin Newberry Date: Tue, 1 Aug 2023 14:44:45 -0700 Subject: [PATCH 072/113] revert gm --- tests/safety/test_gm.py | 56 +++++++++++++++++++++++++++++++---------- 1 file changed, 43 insertions(+), 13 deletions(-) diff --git a/tests/safety/test_gm.py b/tests/safety/test_gm.py index 756a735270..f6b604ad31 100755 --- a/tests/safety/test_gm.py +++ b/tests/safety/test_gm.py @@ -14,7 +14,7 @@ class Buttons: CANCEL = 6 -class GmLongitudinalBase(common.PandaSafetyTest, common.LongitudinalGasBrakeSafetyTest): +class GmLongitudinalBase(common.PandaSafetyTest): # pylint: disable=no-member,abstract-method PCM_CRUISE = False # openpilot can control the PCM state if longitudinal @@ -53,14 +53,6 @@ def test_cruise_engaged_prev(self): def _pcm_status_msg(self, enable): pass - - def _brake_msg(self, brake): - values = {"FrictionBrakeCmd": -brake} - return self.packer_chassis.make_can_msg_panda("EBCMFrictionBrakeCmd", self.BRAKE_BUS, values) - - def _gas_msg(self, gas): - values = {"GasRegenCmd": gas} - return self.packer.make_can_msg_panda("ASCMGasRegenCmd", 0, values) class TestGmSafetyBase(common.PandaSafetyTest, common.DriverTorqueSteeringSafetyTest): @@ -78,6 +70,11 @@ class TestGmSafetyBase(common.PandaSafetyTest, common.DriverTorqueSteeringSafety DRIVER_TORQUE_ALLOWANCE = 65 DRIVER_TORQUE_FACTOR = 4 + MAX_GAS = 0 + MAX_REGEN = 0 + INACTIVE_REGEN = 0 + MAX_BRAKE = 0 + PCM_CRUISE = True # openpilot is tied to the PCM state if not longitudinal @classmethod @@ -121,6 +118,14 @@ def _user_gas_msg(self, gas): values["CruiseState"] = self.safety.get_controls_allowed() return self.packer.make_can_msg_panda("AcceleratorPedal2", 0, values) + def _send_brake_msg(self, brake): + values = {"FrictionBrakeCmd": -brake} + return self.packer_chassis.make_can_msg_panda("EBCMFrictionBrakeCmd", self.BRAKE_BUS, values) + + def _send_gas_msg(self, gas): + values = {"GasRegenCmd": gas} + return self.packer.make_can_msg_panda("ASCMGasRegenCmd", 0, values) + def _torque_driver_msg(self, torque): values = {"LKADriverAppldTrq": torque} return self.packer.make_can_msg_panda("PSCMStatus", 0, values) @@ -133,6 +138,24 @@ def _button_msg(self, buttons): values = {"ACCButtons": buttons} return self.packer.make_can_msg_panda("ASCMSteeringButton", self.BUTTONS_BUS, values) + def test_brake_safety_check(self): + for enabled in [0, 1]: + for b in range(0, 500): + self.safety.set_controls_allowed(enabled) + if abs(b) > self.MAX_BRAKE or (not enabled and b != 0): + self.assertFalse(self._tx(self._send_brake_msg(b))) + else: + self.assertTrue(self._tx(self._send_brake_msg(b))) + + def test_gas_safety_check(self): + # Block if enabled and out of actuation range, disabled and not inactive regen, or if stock longitudinal + for enabled in [0, 1]: + for gas_regen in range(0, 2 ** 12 - 1): + self.safety.set_controls_allowed(enabled) + should_tx = ((enabled and self.MAX_REGEN <= gas_regen <= self.MAX_GAS) or + gas_regen == self.INACTIVE_REGEN) + self.assertEqual(should_tx, self._tx(self._send_gas_msg(gas_regen)), (enabled, gas_regen)) + class TestGmAscmSafety(GmLongitudinalBase, TestGmSafetyBase): TX_MSGS = [[384, 0], [1033, 0], [1034, 0], [715, 0], [880, 0], # pt bus @@ -144,8 +167,8 @@ class TestGmAscmSafety(GmLongitudinalBase, TestGmSafetyBase): BRAKE_BUS = 2 MAX_GAS = 3072 - MIN_GAS = 1404 # Max Regen - INACTIVE_GAS = 1404 + MAX_REGEN = 1404 + INACTIVE_REGEN = 1404 MAX_BRAKE = 400 def setUp(self): @@ -199,6 +222,13 @@ def test_buttons(self): self._rx(self._pcm_status_msg(enabled)) self.assertEqual(enabled, self._tx(self._button_msg(Buttons.CANCEL))) + # GM Cam safety mode does not allow longitudinal messages + def test_brake_safety_check(self): + pass + + def test_gas_safety_check(self): + pass + class TestGmCameraLongitudinalSafety(GmLongitudinalBase, TestGmCameraSafetyBase): TX_MSGS = [[384, 0], [789, 0], [715, 0], [880, 0], # pt bus @@ -207,8 +237,8 @@ class TestGmCameraLongitudinalSafety(GmLongitudinalBase, TestGmCameraSafetyBase) BUTTONS_BUS = 0 # rx only MAX_GAS = 3400 - MIN_GAS = 1514 # Max regen - INACTIVE_GAS = 1554 + MAX_REGEN = 1514 + INACTIVE_REGEN = 1554 MAX_BRAKE = 400 def setUp(self): From b1dc602f5d0e3de57bb438b7ce8fbb2100ada847 Mon Sep 17 00:00:00 2001 From: Justin Newberry Date: Tue, 1 Aug 2023 14:45:35 -0700 Subject: [PATCH 073/113] remove callable --- tests/safety/common.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/safety/common.py b/tests/safety/common.py index 01ad720dfd..4ea5b22048 100644 --- a/tests/safety/common.py +++ b/tests/safety/common.py @@ -3,7 +3,7 @@ import unittest import importlib import numpy as np -from typing import Callable, Dict, List, Optional +from typing import Dict, List, Optional from opendbc.can.packer import CANPacker # pylint: disable=import-error from panda import ALTERNATIVE_EXPERIENCE From 38cbce1b3efbc5bd60d4d55d93e4641182918fdb Mon Sep 17 00:00:00 2001 From: Justin Newberry Date: Mon, 7 Aug 2023 11:51:07 -0700 Subject: [PATCH 074/113] also limit rpm --- board/safety/safety_subaru.h | 8 +++++++- tests/safety/test_subaru.py | 23 ++++++++++++++++++++++- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/board/safety/safety_subaru.h b/board/safety/safety_subaru.h index d33f3fcab4..88b40d85d2 100644 --- a/board/safety/safety_subaru.h +++ b/board/safety/safety_subaru.h @@ -16,7 +16,7 @@ const SteeringLimits SUBARU_GEN2_STEERING_LIMITS = SUBARU_STEERING_LIMITS_GENERA const LongitudinalLimits SUBARU_LONG_LIMITS = { .min_gas = 808, - .max_gas = 3400, // approx 1.2 m/s^2 at low speeds, less at higher speeds + .max_gas = 3400, // approx 2 m/s^2 when maxing cruise_rpm and cruise_throttle .inactive_gas = 1818, // this is zero acceleration, and 808 appears to be engine braking .max_brake = 600, // approx -3.5 m/s^2 }; @@ -187,6 +187,12 @@ static int subaru_tx_hook(CANPacket_t *to_send) { int cruise_throttle = ((GET_BYTES(to_send, 0, 4) >> 16) & 0xFFFU); violation |= longitudinal_gas_checks(cruise_throttle, SUBARU_LONG_LIMITS); } + + // check es_status cruise_rpm limits + if (addr == MSG_SUBARU_ES_Status) { + int cruise_rpm = ((GET_BYTES(to_send, 0, 4) >> 16) & 0xFFFU); + violation |= longitudinal_gas_checks(cruise_rpm, SUBARU_LONG_LIMITS); + } } if (violation) { diff --git a/tests/safety/test_subaru.py b/tests/safety/test_subaru.py index 50483749d7..f2033e2b23 100755 --- a/tests/safety/test_subaru.py +++ b/tests/safety/test_subaru.py @@ -100,17 +100,38 @@ class TestSubaruGen2SafetyBase(TestSubaruSafetyBase): MAX_TORQUE = 1000 -class TestSubaruLongitudinalSafetyBase(TestSubaruSafetyBase, common.LongitudinalGasBrakeSafetyTest): +class TestSubaruLongitudinalSafetyBase(TestSubaruSafetyBase): MIN_GAS = 808 MAX_GAS = 3400 INACTIVE_GAS = 1818 + MAX_POSSIBLE_GAS = 2**12 + MIN_BRAKE = 0 MAX_BRAKE = 600 + MAX_POSSIBLE_BRAKE = 2**16 FWD_BLACKLISTED_ADDRS = {2: [MSG_SUBARU_ES_LKAS, MSG_SUBARU_ES_Brake, MSG_SUBARU_ES_Distance, MSG_SUBARU_ES_Status, MSG_SUBARU_ES_DashStatus, MSG_SUBARU_ES_LKAS_State, MSG_SUBARU_ES_Infotainment]} + + def _generic_limit_safety_check(self, msg_function, min_value, max_value, max_possible_value, inactive_value=0): + for enabled in [0, 1]: + for v in range(min_value, max_possible_value): + self.safety.set_controls_allowed(enabled) + if (not enabled and v != inactive_value) or v > max_value or v < min_value: + self.assertFalse(self._tx(msg_function(v))) + else: + self.assertTrue(self._tx(msg_function(v))) + + def test_brake_safety_check(self): + self._generic_limit_safety_check(self._brake_msg, self.MIN_BRAKE, self.MAX_BRAKE, self.MAX_POSSIBLE_BRAKE) + + def test_gas_safety_check(self): + self._generic_limit_safety_check(self._gas_msg, self.MIN_GAS, self.MAX_GAS, self.MAX_POSSIBLE_GAS, self.INACTIVE_GAS) + def test_rpm_safety_check(self): + self._generic_limit_safety_check(self._rpm_msg, self.MIN_GAS, self.MAX_GAS, self.MAX_POSSIBLE_GAS, self.INACTIVE_GAS) + def _brake_msg(self, brake): values = {"Brake_Pressure": brake} return self.packer.make_can_msg_panda("ES_Brake", self.ALT_BUS, values) From 11d2c55abb59ea1b1521742cf097dc9d3be9badb Mon Sep 17 00:00:00 2001 From: Justin Newberry Date: Mon, 7 Aug 2023 11:55:45 -0700 Subject: [PATCH 075/113] remove safety replay for now --- tests/safety_replay/test_safety_replay.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/safety_replay/test_safety_replay.py b/tests/safety_replay/test_safety_replay.py index 2c9d68c2c2..ba9435d0f2 100755 --- a/tests/safety_replay/test_safety_replay.py +++ b/tests/safety_replay/test_safety_replay.py @@ -13,8 +13,6 @@ ReplayRoute = namedtuple("ReplayRoute", ("route", "safety_mode", "param", "alternative_experience"), defaults=(0, 0)) logs = [ - ReplayRoute("8de015561e1ea4a0|2023-07-15--09-44-25--0--rlog.bz2", Panda.SAFETY_SUBARU, # SUBARU.CROSSTREK (openpilot long) - Panda.FLAG_SUBARU_LONG, ALT_EXP.DISABLE_DISENGAGE_ON_GAS), ReplayRoute("2425568437959f9d|2019-12-22--16-24-37.bz2", Panda.SAFETY_HONDA_NIDEC), # HONDA.CIVIC (fcw presents: 0x1FA blocked as expected) ReplayRoute("38bfd238edecbcd7|2019-06-07--10-15-25.bz2", Panda.SAFETY_TOYOTA, 66), # TOYOTA.PRIUS ReplayRoute("f89c604cf653e2bf|2018-09-29--13-46-50.bz2", Panda.SAFETY_GM), # GM.VOLT From 3c4ac7a2d2a6d1d81cd44531c037982770fc0739 Mon Sep 17 00:00:00 2001 From: Justin Newberry Date: Mon, 7 Aug 2023 11:58:49 -0700 Subject: [PATCH 076/113] revert unrelated changes --- tests/libpanda/safety_helpers.py | 1 + tests/safety_replay/test_safety_replay.py | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/libpanda/safety_helpers.py b/tests/libpanda/safety_helpers.py index 2509045705..75d33b053c 100644 --- a/tests/libpanda/safety_helpers.py +++ b/tests/libpanda/safety_helpers.py @@ -100,3 +100,4 @@ def set_honda_fwd_brake(self, c: bool) -> None: ... def set_honda_alt_brake_msg(self, c: bool) -> None: ... def set_honda_bosch_long(self, c: bool) -> None: ... def get_honda_hw(self) -> int: ... + diff --git a/tests/safety_replay/test_safety_replay.py b/tests/safety_replay/test_safety_replay.py index ba9435d0f2..b15b82543a 100755 --- a/tests/safety_replay/test_safety_replay.py +++ b/tests/safety_replay/test_safety_replay.py @@ -28,7 +28,7 @@ ReplayRoute("fbbfa6af821552b9|2020-03-03--08-09-43.bz2", Panda.SAFETY_NISSAN), # NISSAN.XTRAIL ReplayRoute("5b7c365c50084530_2020-04-15--16-13-24--3--rlog.bz2", Panda.SAFETY_HYUNDAI), # HYUNDAI.SONATA ReplayRoute("610ebb9faaad6b43|2020-06-13--15-28-36.bz2", Panda.SAFETY_HYUNDAI_LEGACY), # HYUNDAI.IONIQ_EV_LTD - ReplayRoute("5ab784f361e19b78_2020-06-08--16-30-41.bz2", Panda.SAFETY_SUBARU_PREGLOBAL), # SUBARU.OUTBACK_PREGLOBAL + ReplayRoute("5ab784f361e19b78_2020-06-08--16-30-41.bz2", Panda.SAFETY_SUBARU_PREGLOBAL), # SUBARU.OUTBACK_PREGLOBAL ReplayRoute("bb50caf5f0945ab1|2021-06-19--17-20-18.bz2", Panda.SAFETY_TESLA), # TESLA.AP2_MODELS ReplayRoute("bd6a637565e91581_2021-10-29--22-18-31--1--rlog.bz2", Panda.SAFETY_MAZDA), # MAZDA.CX9_2021 ReplayRoute("1a5d045d2c531a6d_2022-06-07--22-03-00--1--rlog.bz2", Panda.SAFETY_HONDA_BOSCH, # HONDA.CIVIC_2022 From d2dd7874af03078be84b49df035da4f486b62e00 Mon Sep 17 00:00:00 2001 From: Justin Newberry Date: Mon, 7 Aug 2023 12:00:22 -0700 Subject: [PATCH 077/113] pr cleanup --- tests/libpanda/safety_helpers.py | 1 + tests/safety_replay/test_safety_replay.py | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/libpanda/safety_helpers.py b/tests/libpanda/safety_helpers.py index 75d33b053c..234b8ecd02 100644 --- a/tests/libpanda/safety_helpers.py +++ b/tests/libpanda/safety_helpers.py @@ -101,3 +101,4 @@ def set_honda_alt_brake_msg(self, c: bool) -> None: ... def set_honda_bosch_long(self, c: bool) -> None: ... def get_honda_hw(self) -> int: ... + diff --git a/tests/safety_replay/test_safety_replay.py b/tests/safety_replay/test_safety_replay.py index b15b82543a..2dcc07cd1a 100755 --- a/tests/safety_replay/test_safety_replay.py +++ b/tests/safety_replay/test_safety_replay.py @@ -28,7 +28,7 @@ ReplayRoute("fbbfa6af821552b9|2020-03-03--08-09-43.bz2", Panda.SAFETY_NISSAN), # NISSAN.XTRAIL ReplayRoute("5b7c365c50084530_2020-04-15--16-13-24--3--rlog.bz2", Panda.SAFETY_HYUNDAI), # HYUNDAI.SONATA ReplayRoute("610ebb9faaad6b43|2020-06-13--15-28-36.bz2", Panda.SAFETY_HYUNDAI_LEGACY), # HYUNDAI.IONIQ_EV_LTD - ReplayRoute("5ab784f361e19b78_2020-06-08--16-30-41.bz2", Panda.SAFETY_SUBARU_PREGLOBAL), # SUBARU.OUTBACK_PREGLOBAL + ReplayRoute("5ab784f361e19b78_2020-06-08--16-30-41.bz2", Panda.SAFETY_SUBARU_PREGLOBAL), # SUBARU.OUTBACK_PREGLOBAL ReplayRoute("bb50caf5f0945ab1|2021-06-19--17-20-18.bz2", Panda.SAFETY_TESLA), # TESLA.AP2_MODELS ReplayRoute("bd6a637565e91581_2021-10-29--22-18-31--1--rlog.bz2", Panda.SAFETY_MAZDA), # MAZDA.CX9_2021 ReplayRoute("1a5d045d2c531a6d_2022-06-07--22-03-00--1--rlog.bz2", Panda.SAFETY_HONDA_BOSCH, # HONDA.CIVIC_2022 From b604cef6c8aaba8923aaafa95f2b4d0fa8003722 Mon Sep 17 00:00:00 2001 From: Justin Newberry Date: Mon, 7 Aug 2023 15:54:59 -0700 Subject: [PATCH 078/113] remove unused gasbrake safety test --- tests/safety/common.py | 48 ------------------------------------------ 1 file changed, 48 deletions(-) diff --git a/tests/safety/common.py b/tests/safety/common.py index cb1e806640..65b5b96ffa 100644 --- a/tests/safety/common.py +++ b/tests/safety/common.py @@ -175,54 +175,6 @@ def test_accel_actuation_limits(self, stock_longitudinal=False): self.assertEqual(should_tx, self._tx(self._accel_msg(accel))) -class LongitudinalGasBrakeSafetyTest(PandaSafetyTestBase, abc.ABC): - - MIN_BRAKE: int = 0 - MAX_BRAKE: Optional[int] = None - - MIN_GAS: int = 0 - MAX_GAS: Optional[int] = None - - INACTIVE_GAS: Optional[int] = 0 - - @classmethod - def setUpClass(cls): - if cls.__name__ == "LongitudinalGasBrakeSafetyTest": - cls.safety = None - raise unittest.SkipTest - - @abc.abstractmethod - def _gas_msg(self, gas: int): - pass - - @abc.abstractmethod - def _brake_msg(self, brake: int): - pass - - def test_gas_brake_limits_correct(self): - # Assert that max brake and max throttle is set - self.assertTrue(self.MAX_BRAKE is not None) - self.assertTrue(self.MAX_GAS is not None) - - self.assertGreater(self.MAX_BRAKE, self.MIN_BRAKE) - self.assertGreater(self.MAX_GAS, self.MIN_GAS) - - def _generic_limit_safety_check(self, msg_function, min_value, max_value, max_value_multiplier, inactive_value=0): - for enabled in [0, 1]: - for v in range(int(min_value), int(max_value*max_value_multiplier)): - self.safety.set_controls_allowed(enabled) - if (not enabled and v != inactive_value) or v > max_value or v < min_value: - self.assertFalse(self._tx(msg_function(v))) - else: - self.assertTrue(self._tx(msg_function(v))) - - def test_brake_safety_check(self): - self._generic_limit_safety_check(self._brake_msg, self.MIN_BRAKE, self.MAX_BRAKE, 1.5) - - def test_gas_safety_check(self): - self._generic_limit_safety_check(self._gas_msg, self.MIN_GAS, self.MAX_GAS, 1.2, self.INACTIVE_GAS) - - class TorqueSteeringSafetyTestBase(PandaSafetyTestBase, abc.ABC): MAX_RATE_UP = 0 From 3e6010e4d89275c1f113bc91fc0a611c8f6c34e0 Mon Sep 17 00:00:00 2001 From: Justin Newberry Date: Mon, 7 Aug 2023 15:58:53 -0700 Subject: [PATCH 079/113] fix ruff linter --- tests/safety/test_subaru.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/safety/test_subaru.py b/tests/safety/test_subaru.py index c2f1af1ff9..6728a8a457 100755 --- a/tests/safety/test_subaru.py +++ b/tests/safety/test_subaru.py @@ -114,11 +114,11 @@ class TestSubaruLongitudinalSafetyBase(TestSubaruSafetyBase): MIN_BRAKE = 0 MAX_BRAKE = 600 MAX_POSSIBLE_BRAKE = 2**16 - + FWD_BLACKLISTED_ADDRS = {2: [MSG_SUBARU_ES_LKAS, MSG_SUBARU_ES_Brake, MSG_SUBARU_ES_Distance, MSG_SUBARU_ES_Status, MSG_SUBARU_ES_DashStatus, MSG_SUBARU_ES_LKAS_State, MSG_SUBARU_ES_Infotainment]} - + def _generic_limit_safety_check(self, msg_function, min_value, max_value, max_possible_value, inactive_value=0): for enabled in [0, 1]: for v in range(min_value, max_possible_value): @@ -130,13 +130,13 @@ def _generic_limit_safety_check(self, msg_function, min_value, max_value, max_po def test_brake_safety_check(self): self._generic_limit_safety_check(self._brake_msg, self.MIN_BRAKE, self.MAX_BRAKE, self.MAX_POSSIBLE_BRAKE) - + def test_gas_safety_check(self): self._generic_limit_safety_check(self._gas_msg, self.MIN_GAS, self.MAX_GAS, self.MAX_POSSIBLE_GAS, self.INACTIVE_GAS) def test_rpm_safety_check(self): self._generic_limit_safety_check(self._rpm_msg, self.MIN_GAS, self.MAX_GAS, self.MAX_POSSIBLE_GAS, self.INACTIVE_GAS) - + def _brake_msg(self, brake): values = {"Brake_Pressure": brake} return self.packer.make_can_msg_panda("ES_Brake", self.ALT_BUS, values) From 5a83f3271453cd561c00796e371ddd72b6802c23 Mon Sep 17 00:00:00 2001 From: Justin Newberry Date: Tue, 8 Aug 2023 16:53:54 -0700 Subject: [PATCH 080/113] PR cleanup --- board/safety/safety_subaru.h | 36 +++++++++++++++++------------------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/board/safety/safety_subaru.h b/board/safety/safety_subaru.h index a2b735bd69..c7d7d75c8d 100644 --- a/board/safety/safety_subaru.h +++ b/board/safety/safety_subaru.h @@ -15,9 +15,9 @@ const SteeringLimits SUBARU_GEN2_STEERING_LIMITS = SUBARU_STEERING_LIMITS_GENERA const LongitudinalLimits SUBARU_LONG_LIMITS = { - .min_gas = 808, + .min_gas = 808, // engine braking .max_gas = 3400, // approx 2 m/s^2 when maxing cruise_rpm and cruise_throttle - .inactive_gas = 1818, // this is zero acceleration, and 808 appears to be engine braking + .inactive_gas = 1818, // this is zero acceleration .max_brake = 600, // approx -3.5 m/s^2 }; @@ -187,24 +187,22 @@ static int subaru_tx_hook(CANPacket_t *to_send) { violation |= steer_torque_cmd_checks(desired_torque, -1, limits); } - if (subaru_longitudinal) { - // check es_brake brake_pressure limits - if (addr == MSG_SUBARU_ES_Brake) { - int es_brake_pressure = ((GET_BYTES(to_send, 0, 4) >> 16) & 0xFFFFU); - violation |= longitudinal_brake_checks(es_brake_pressure, SUBARU_LONG_LIMITS); - } + // check es_brake brake_pressure limits + if (addr == MSG_SUBARU_ES_Brake) { + int es_brake_pressure = GET_BYTES(to_send, 2, 2); + violation |= longitudinal_brake_checks(es_brake_pressure, SUBARU_LONG_LIMITS); + } - // check es_distance cruise_throttle limits - if (addr == MSG_SUBARU_ES_Distance) { - int cruise_throttle = ((GET_BYTES(to_send, 0, 4) >> 16) & 0xFFFU); - violation |= longitudinal_gas_checks(cruise_throttle, SUBARU_LONG_LIMITS); - } + // check es_distance cruise_throttle limits + if (addr == MSG_SUBARU_ES_Distance) { + int cruise_throttle = (GET_BYTES(to_send, 2, 2) & 0xFFFU); + violation |= longitudinal_gas_checks(cruise_throttle, SUBARU_LONG_LIMITS); + } - // check es_status cruise_rpm limits - if (addr == MSG_SUBARU_ES_Status) { - int cruise_rpm = ((GET_BYTES(to_send, 0, 4) >> 16) & 0xFFFU); - violation |= longitudinal_gas_checks(cruise_rpm, SUBARU_LONG_LIMITS); - } + // check es_status cruise_rpm limits + if (addr == MSG_SUBARU_ES_Status) { + int cruise_rpm = (GET_BYTES(to_send, 2, 2) & 0xFFFU); + violation |= longitudinal_gas_checks(cruise_rpm, SUBARU_LONG_LIMITS); } if (violation) { @@ -244,7 +242,7 @@ static const addr_checks* subaru_init(uint16_t param) { subaru_gen2 = GET_FLAG(param, SUBARU_PARAM_GEN2); #ifdef ALLOW_DEBUG - subaru_longitudinal = GET_FLAG(param, SUBARU_PARAM_LONGITUDINAL); + subaru_longitudinal = GET_FLAG(param, SUBARU_PARAM_LONGITUDINAL) && !subaru_gen2; #endif if (subaru_gen2) { From 08621fa10689a3e12b1321e4f291b514af1cbd3f Mon Sep 17 00:00:00 2001 From: Justin Newberry Date: Tue, 8 Aug 2023 16:54:43 -0700 Subject: [PATCH 081/113] pr cleanup? --- board/safety/safety_subaru.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/board/safety/safety_subaru.h b/board/safety/safety_subaru.h index 56e9e76f8a..751db0f072 100644 --- a/board/safety/safety_subaru.h +++ b/board/safety/safety_subaru.h @@ -15,7 +15,7 @@ const SteeringLimits SUBARU_GEN2_STEERING_LIMITS = SUBARU_STEERING_LIMITS_GENERA const LongitudinalLimits SUBARU_LONG_LIMITS = { - .min_gas = 808, // engine braking + .min_gas = 808, // appears to be engine braking .max_gas = 3400, // approx 2 m/s^2 when maxing cruise_rpm and cruise_throttle .inactive_gas = 1818, // this is zero acceleration .max_brake = 600, // approx -3.5 m/s^2 From 59734b7fbe361b50634ab982465bcd71edf5b2fe Mon Sep 17 00:00:00 2001 From: Justin Newberry Date: Wed, 9 Aug 2023 09:56:57 -0700 Subject: [PATCH 082/113] added common brake test and moved generic test to base safety class --- tests/safety/common.py | 71 +++++++++++++++++++++++++++++++++++------- 1 file changed, 59 insertions(+), 12 deletions(-) diff --git a/tests/safety/common.py b/tests/safety/common.py index 0bec019fb1..ad0752bdd8 100644 --- a/tests/safety/common.py +++ b/tests/safety/common.py @@ -69,6 +69,27 @@ def _rx(self, msg): def _tx(self, msg): return self.safety.safety_tx_hook(msg) + def _generic_limit_safety_check(self, msg_function, min_allowed_value: float, max_allowed_value: float, min_test_value: float, + max_test_value: float, test_delta: float = 1, inactive_value=0, msg_allowed=True): + """ + Enforces that only a specific range of values in a msg_function are allowed to be sent, only when controls_allowed is true. + Tests the range of min_test_value -> max_test_value with a delta of test_delta. + Message is always allowed if value is equal to inactive_value. + Message is never allowed if msg_allowed is false, for example when openpilot long is not enabled. + """ + + # Ensure that we at least test the allowed_value range + self.assertGreater(max_test_value, max_allowed_value) + self.assertLessEqual(min_test_value, min_allowed_value) + + for controls_allowed in [0, 1]: + # enforce we don't skip over 0 or inactive + for v in np.concatenate((np.arange(min_test_value, max_test_value, test_delta), [0, inactive_value])): + v = round(v, 2) # floats might not hit exact boundary conditions without rounding + self.safety.set_controls_allowed(controls_allowed) + should_tx = (controls_allowed or v == inactive_value) and (v <= max_allowed_value and v >= min_allowed_value) and msg_allowed + self.assertEqual(self._tx(msg_function(v)), should_tx, (controls_allowed, v)) + class InterceptorSafetyTest(PandaSafetyTestBase): @@ -161,18 +182,44 @@ def test_accel_actuation_limits(self, stock_longitudinal=False): (self.MIN_ACCEL, self.MAX_ACCEL, ALTERNATIVE_EXPERIENCE.RAISE_LONGITUDINAL_LIMITS_TO_ISO_MAX)) for min_accel, max_accel, alternative_experience in limits: - # enforce we don't skip over 0 or inactive accel - for accel in np.concatenate((np.arange(min_accel - 1, max_accel + 1, 0.05), [0, self.INACTIVE_ACCEL])): - accel = round(accel, 2) # floats might not hit exact boundary conditions without rounding - for controls_allowed in [True, False]: - self.safety.set_controls_allowed(controls_allowed) - self.safety.set_alternative_experience(alternative_experience) - if stock_longitudinal: - should_tx = False - else: - should_tx = controls_allowed and min_accel <= accel <= max_accel - should_tx = should_tx or accel == self.INACTIVE_ACCEL - self.assertEqual(should_tx, self._tx(self._accel_msg(accel))) + self.safety.set_alternative_experience(alternative_experience) + self._generic_limit_safety_check(self._accel_msg, min_accel, max_accel, min_accel - 1, max_accel + 1, 0.05, self.INACTIVE_ACCEL, not stock_longitudinal) + + +class LongitudinalGasBrakeSafetyTest(PandaSafetyTestBase, abc.ABC): + + MIN_BRAKE: int = 0 + MAX_BRAKE: Optional[int] = None + MAX_POSSIBLE_BRAKE: Optional[int] = None + + MIN_GAS: int = 0 + MAX_GAS: Optional[int] = None + INACTIVE_GAS = 0 + MAX_POSSIBLE_GAS: Optional[int] = None + + @abc.abstractmethod + def _gas_msg(self, gas: int): + pass + + @abc.abstractmethod + def _brake_msg(self, brake: int): + pass + + def test_gas_brake_limits_correct(self): + # Assert that max brake and max gas limits are set + self.assertTrue(self.MAX_BRAKE is not None) + self.assertTrue(self.MAX_GAS is not None) + self.assertTrue(self.MAX_POSSIBLE_BRAKE is not None) + self.assertTrue(self.MAX_POSSIBLE_GAS is not None) + + self.assertGreater(self.MAX_BRAKE, self.MIN_BRAKE) + self.assertGreater(self.MAX_GAS, self.MIN_GAS) + + def test_brake_safety_check(self): + self._generic_limit_safety_check(self._brake_msg, self.MIN_BRAKE, self.MAX_BRAKE, 0, self.MAX_POSSIBLE_BRAKE, 1) + + def test_gas_safety_check(self): + self._generic_limit_safety_check(self._gas_msg, self.MIN_GAS, self.MAX_GAS, 0, self.MAX_POSSIBLE_GAS, 1, self.INACTIVE_GAS) class TorqueSteeringSafetyTestBase(PandaSafetyTestBase, abc.ABC): From 1ac5e9dff9e1803b832c1c65ba76be7ae219f508 Mon Sep 17 00:00:00 2001 From: Justin Newberry Date: Wed, 9 Aug 2023 10:00:17 -0700 Subject: [PATCH 083/113] remove duplicate test --- tests/safety/common.py | 54 +++++------------------------------------- 1 file changed, 6 insertions(+), 48 deletions(-) diff --git a/tests/safety/common.py b/tests/safety/common.py index 772d4829e9..705f948c27 100644 --- a/tests/safety/common.py +++ b/tests/safety/common.py @@ -197,6 +197,12 @@ class LongitudinalGasBrakeSafetyTest(PandaSafetyTestBase, abc.ABC): INACTIVE_GAS = 0 MAX_POSSIBLE_GAS: Optional[int] = None + @classmethod + def setUpClass(cls): + if cls.__name__ == "LongitudinalGasBrakeSafetyTest": + cls.safety = None + raise unittest.SkipTest + @abc.abstractmethod def _gas_msg(self, gas: int): pass @@ -222,54 +228,6 @@ def test_gas_safety_check(self): self._generic_limit_safety_check(self._gas_msg, self.MIN_GAS, self.MAX_GAS, 0, self.MAX_POSSIBLE_GAS, 1, self.INACTIVE_GAS) -class LongitudinalGasBrakeSafetyTest(PandaSafetyTestBase, abc.ABC): - - MIN_BRAKE: int = 0 - MAX_BRAKE: Optional[int] = None - - MIN_GAS: int = 0 - MAX_GAS: Optional[int] = None - - INACTIVE_GAS: Optional[int] = 0 - - @classmethod - def setUpClass(cls): - if cls.__name__ == "LongitudinalGasBrakeSafetyTest": - cls.safety = None - raise unittest.SkipTest - - @abc.abstractmethod - def _gas_msg(self, gas: int): - pass - - @abc.abstractmethod - def _brake_msg(self, brake: int): - pass - - def test_gas_brake_limits_correct(self): - # Assert that max brake and max throttle is set - self.assertTrue(self.MAX_BRAKE is not None) - self.assertTrue(self.MAX_GAS is not None) - - self.assertGreater(self.MAX_BRAKE, self.MIN_BRAKE) - self.assertGreater(self.MAX_GAS, self.MIN_GAS) - - def _generic_limit_safety_check(self, msg_function, min_allowed_value: int, max_allowed_value: int, inactive_value=0): - for enabled in [0, 1]: - for v in np.concatenate((np.arange(min_allowed_value - 1, max_allowed_value + 1, 1), [0, inactive_value])): - self.safety.set_controls_allowed(enabled) - if (not enabled and v != inactive_value) or v > max_allowed_value or v < min_allowed_value: - self.assertFalse(self._tx(msg_function(v)), (v, min_allowed_value, max_allowed_value)) - else: - self.assertTrue(self._tx(msg_function(v)), (v, min_allowed_value, max_allowed_value)) - - def test_brake_safety_check(self): - self._generic_limit_safety_check(self._brake_msg, self.MIN_BRAKE, self.MAX_BRAKE) - - def test_gas_safety_check(self): - self._generic_limit_safety_check(self._gas_msg, self.MIN_GAS, self.MAX_GAS, self.INACTIVE_GAS) - - class TorqueSteeringSafetyTestBase(PandaSafetyTestBase, abc.ABC): MAX_RATE_UP = 0 From 9360a9e2543d1288e4ef50c4d57d928579f3b76c Mon Sep 17 00:00:00 2001 From: Justin Newberry Date: Wed, 9 Aug 2023 10:14:46 -0700 Subject: [PATCH 084/113] wip --- tests/safety/common.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/safety/common.py b/tests/safety/common.py index 705f948c27..ad836c4579 100644 --- a/tests/safety/common.py +++ b/tests/safety/common.py @@ -70,7 +70,7 @@ def _tx(self, msg): return self.safety.safety_tx_hook(msg) def _generic_limit_safety_check(self, msg_function, min_allowed_value: float, max_allowed_value: float, min_test_value: float, - max_test_value: float, test_delta: float = 1, inactive_value=0, msg_allowed=True): + max_test_value: float, test_delta: float = 1, inactive_value: float = 0, msg_allowed=True): """ Enforces that only a specific range of values in a msg_function are allowed to be sent, only when controls_allowed is true. Tests the range of min_test_value -> max_test_value with a delta of test_delta. @@ -84,7 +84,7 @@ def _generic_limit_safety_check(self, msg_function, min_allowed_value: float, ma for controls_allowed in [0, 1]: # enforce we don't skip over 0 or inactive - for v in np.concatenate((np.arange(min_test_value, max_test_value, test_delta), [0, inactive_value])): + for v in np.concatenate((np.arange(min_test_value, max_test_value, test_delta), np.array([0, inactive_value]))): v = round(v, 2) # floats might not hit exact boundary conditions without rounding self.safety.set_controls_allowed(controls_allowed) should_tx = (controls_allowed or v == inactive_value) and (v <= max_allowed_value and v >= min_allowed_value) and msg_allowed From 20338daa01f63d6036c474a8b018a5f877ab169b Mon Sep 17 00:00:00 2001 From: Justin Newberry Date: Wed, 9 Aug 2023 10:52:55 -0700 Subject: [PATCH 085/113] wip --- tests/safety/common.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tests/safety/common.py b/tests/safety/common.py index ad836c4579..a9ecbef185 100644 --- a/tests/safety/common.py +++ b/tests/safety/common.py @@ -82,13 +82,14 @@ def _generic_limit_safety_check(self, msg_function, min_allowed_value: float, ma self.assertGreater(max_test_value, max_allowed_value) self.assertLessEqual(min_test_value, min_allowed_value) - for controls_allowed in [0, 1]: + for controls_allowed in [False, True]: # enforce we don't skip over 0 or inactive for v in np.concatenate((np.arange(min_test_value, max_test_value, test_delta), np.array([0, inactive_value]))): v = round(v, 2) # floats might not hit exact boundary conditions without rounding self.safety.set_controls_allowed(controls_allowed) - should_tx = (controls_allowed or v == inactive_value) and (v <= max_allowed_value and v >= min_allowed_value) and msg_allowed - self.assertEqual(self._tx(msg_function(v)), should_tx, (controls_allowed, v)) + should_tx = controls_allowed and min_allowed_value <= v <= max_allowed_value + should_tx = should_tx or + self.assertEqual(self._tx(msg_function(v)), should_tx, (controls_allowed, should_tx, v)) class InterceptorSafetyTest(PandaSafetyTestBase): From fa49064c18fdedacbc0972c50d44bc9733778848 Mon Sep 17 00:00:00 2001 From: Justin Newberry Date: Wed, 9 Aug 2023 11:18:05 -0700 Subject: [PATCH 086/113] rename longitudinalaccelsafety --- tests/safety/common.py | 30 ++---------------------------- tests/safety/hyundai_common.py | 2 +- tests/safety/test_honda.py | 2 +- tests/safety/test_toyota.py | 2 +- tests/safety/test_volkswagen_pq.py | 2 +- 5 files changed, 6 insertions(+), 32 deletions(-) diff --git a/tests/safety/common.py b/tests/safety/common.py index a9ecbef185..34048017e2 100644 --- a/tests/safety/common.py +++ b/tests/safety/common.py @@ -158,26 +158,16 @@ def test_gas_interceptor_safety_check(self): self.assertEqual(send, self._tx(self._interceptor_gas_cmd(gas))) -class LongitudinalAccelSafetyTest(PandaSafetyTestBase, abc.ABC): +class TestLongitudinalAccelSafetyBase(PandaSafetyTestBase, abc.ABC): MAX_ACCEL: float = 2.0 MIN_ACCEL: float = -3.5 INACTIVE_ACCEL: float = 0.0 - @classmethod - def setUpClass(cls): - if cls.__name__ == "LongitudinalAccelSafetyTest": - cls.safety = None - raise unittest.SkipTest - @abc.abstractmethod def _accel_msg(self, accel: float): pass - def test_accel_limits_correct(self): - self.assertGreater(self.MAX_ACCEL, 0) - self.assertLess(self.MIN_ACCEL, 0) - def test_accel_actuation_limits(self, stock_longitudinal=False): limits = ((self.MIN_ACCEL, self.MAX_ACCEL, ALTERNATIVE_EXPERIENCE.DEFAULT), (self.MIN_ACCEL, self.MAX_ACCEL, ALTERNATIVE_EXPERIENCE.RAISE_LONGITUDINAL_LIMITS_TO_ISO_MAX)) @@ -187,7 +177,7 @@ def test_accel_actuation_limits(self, stock_longitudinal=False): self._generic_limit_safety_check(self._accel_msg, min_accel, max_accel, min_accel - 1, max_accel + 1, 0.05, self.INACTIVE_ACCEL, not stock_longitudinal) -class LongitudinalGasBrakeSafetyTest(PandaSafetyTestBase, abc.ABC): +class TestLongitudinalGasBrakeSafetyBase(PandaSafetyTestBase, abc.ABC): MIN_BRAKE: int = 0 MAX_BRAKE: Optional[int] = None @@ -198,12 +188,6 @@ class LongitudinalGasBrakeSafetyTest(PandaSafetyTestBase, abc.ABC): INACTIVE_GAS = 0 MAX_POSSIBLE_GAS: Optional[int] = None - @classmethod - def setUpClass(cls): - if cls.__name__ == "LongitudinalGasBrakeSafetyTest": - cls.safety = None - raise unittest.SkipTest - @abc.abstractmethod def _gas_msg(self, gas: int): pass @@ -212,16 +196,6 @@ def _gas_msg(self, gas: int): def _brake_msg(self, brake: int): pass - def test_gas_brake_limits_correct(self): - # Assert that max brake and max gas limits are set - self.assertTrue(self.MAX_BRAKE is not None) - self.assertTrue(self.MAX_GAS is not None) - self.assertTrue(self.MAX_POSSIBLE_BRAKE is not None) - self.assertTrue(self.MAX_POSSIBLE_GAS is not None) - - self.assertGreater(self.MAX_BRAKE, self.MIN_BRAKE) - self.assertGreater(self.MAX_GAS, self.MIN_GAS) - def test_brake_safety_check(self): self._generic_limit_safety_check(self._brake_msg, self.MIN_BRAKE, self.MAX_BRAKE, 0, self.MAX_POSSIBLE_BRAKE, 1) diff --git a/tests/safety/hyundai_common.py b/tests/safety/hyundai_common.py index ad93809714..b55f00758f 100644 --- a/tests/safety/hyundai_common.py +++ b/tests/safety/hyundai_common.py @@ -74,7 +74,7 @@ def test_sampling_cruise_buttons(self): self._rx(self._button_msg(Buttons.NONE)) -class HyundaiLongitudinalBase(common.LongitudinalAccelSafetyTest): +class HyundaiLongitudinalBase(common.TestLongitudinalAccelSafetyBase): # pylint: disable=no-member,abstract-method DISABLED_ECU_UDS_MSG: Tuple[int, int] diff --git a/tests/safety/test_honda.py b/tests/safety/test_honda.py index 11b1c623e9..d7582e387d 100755 --- a/tests/safety/test_honda.py +++ b/tests/safety/test_honda.py @@ -536,7 +536,7 @@ def setUp(self): self.safety.init_tests() -class TestHondaBoschRadarlessLongSafety(common.LongitudinalAccelSafetyTest, HondaButtonEnableBase, +class TestHondaBoschRadarlessLongSafety(common.TestLongitudinalAccelSafetyBase, HondaButtonEnableBase, TestHondaBoschRadarlessSafetyBase): """ Covers the Honda Bosch Radarless safety mode with longitudinal control diff --git a/tests/safety/test_toyota.py b/tests/safety/test_toyota.py index a0ba9c1130..eed76ea204 100755 --- a/tests/safety/test_toyota.py +++ b/tests/safety/test_toyota.py @@ -20,7 +20,7 @@ def interceptor_msg(gas, addr): class TestToyotaSafetyBase(common.PandaSafetyTest, common.InterceptorSafetyTest, - common.LongitudinalAccelSafetyTest): + common.TestLongitudinalAccelSafetyBase): TX_MSGS = [[0x283, 0], [0x2E6, 0], [0x2E7, 0], [0x33E, 0], [0x344, 0], [0x365, 0], [0x366, 0], [0x4CB, 0], # DSU bus 0 [0x128, 1], [0x141, 1], [0x160, 1], [0x161, 1], [0x470, 1], # DSU bus 1 diff --git a/tests/safety/test_volkswagen_pq.py b/tests/safety/test_volkswagen_pq.py index be71f1d7e7..a0e10b8767 100755 --- a/tests/safety/test_volkswagen_pq.py +++ b/tests/safety/test_volkswagen_pq.py @@ -139,7 +139,7 @@ def test_spam_cancel_safety_check(self): self.assertTrue(self._tx(self._button_msg(resume=True))) -class TestVolkswagenPqLongSafety(TestVolkswagenPqSafety, common.LongitudinalAccelSafetyTest): +class TestVolkswagenPqLongSafety(TestVolkswagenPqSafety, common.TestLongitudinalAccelSafetyBase): TX_MSGS = [[MSG_HCA_1, 0], [MSG_LDW_1, 0], [MSG_ACC_SYSTEM, 0], [MSG_ACC_GRA_ANZEIGE, 0]] FWD_BLACKLISTED_ADDRS = {2: [MSG_HCA_1, MSG_LDW_1, MSG_ACC_SYSTEM, MSG_ACC_GRA_ANZEIGE]} FWD_BUS_LOOKUP = {0: 2, 2: 0} From f8d49144a938a4ee36702d618c2a290a8ef507fd Mon Sep 17 00:00:00 2001 From: Justin Newberry Date: Wed, 9 Aug 2023 11:20:31 -0700 Subject: [PATCH 087/113] revert limits correct test --- tests/safety/common.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/safety/common.py b/tests/safety/common.py index 34048017e2..3605a189e0 100644 --- a/tests/safety/common.py +++ b/tests/safety/common.py @@ -168,6 +168,10 @@ class TestLongitudinalAccelSafetyBase(PandaSafetyTestBase, abc.ABC): def _accel_msg(self, accel: float): pass + def test_accel_limits_correct(self): + self.assertGreater(self.MAX_ACCEL, 0) + self.assertLess(self.MIN_ACCEL, 0) + def test_accel_actuation_limits(self, stock_longitudinal=False): limits = ((self.MIN_ACCEL, self.MAX_ACCEL, ALTERNATIVE_EXPERIENCE.DEFAULT), (self.MIN_ACCEL, self.MAX_ACCEL, ALTERNATIVE_EXPERIENCE.RAISE_LONGITUDINAL_LIMITS_TO_ISO_MAX)) From 353207b4b3504bc0c4399bacfce67e108b6eab46 Mon Sep 17 00:00:00 2001 From: Justin Newberry Date: Wed, 9 Aug 2023 11:28:40 -0700 Subject: [PATCH 088/113] fix controls allowed test --- tests/safety/common.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/safety/common.py b/tests/safety/common.py index 3605a189e0..df43fe9726 100644 --- a/tests/safety/common.py +++ b/tests/safety/common.py @@ -72,10 +72,10 @@ def _tx(self, msg): def _generic_limit_safety_check(self, msg_function, min_allowed_value: float, max_allowed_value: float, min_test_value: float, max_test_value: float, test_delta: float = 1, inactive_value: float = 0, msg_allowed=True): """ - Enforces that only a specific range of values in a msg_function are allowed to be sent, only when controls_allowed is true. + Enforces that a signal within a message is only allowed to be sent within a specific range, min_allowed_value -> max_allowed_value. Tests the range of min_test_value -> max_test_value with a delta of test_delta. - Message is always allowed if value is equal to inactive_value. - Message is never allowed if msg_allowed is false, for example when openpilot long is not enabled. + Message is also only allowed to be sent when controls_allowed is true, unless the value is equal to inactive_value. + Message is never allowed in msg_allowed is false, for example when stock longitudinal is enabled and you are sending acceleration requests. """ # Ensure that we at least test the allowed_value range @@ -88,7 +88,7 @@ def _generic_limit_safety_check(self, msg_function, min_allowed_value: float, ma v = round(v, 2) # floats might not hit exact boundary conditions without rounding self.safety.set_controls_allowed(controls_allowed) should_tx = controls_allowed and min_allowed_value <= v <= max_allowed_value - should_tx = should_tx or + should_tx = (should_tx or v == inactive_value) and msg_allowed self.assertEqual(self._tx(msg_function(v)), should_tx, (controls_allowed, should_tx, v)) From f13b2b69e8ab7452e05d7128155b6ba5ceb148fe Mon Sep 17 00:00:00 2001 From: Justin Newberry Date: Thu, 10 Aug 2023 13:30:09 -0700 Subject: [PATCH 089/113] move gm over to long gas brake test --- tests/safety/common.py | 4 +- tests/safety/hyundai_common.py | 2 +- tests/safety/test_gm.py | 59 ++++++++---------------------- tests/safety/test_honda.py | 2 +- tests/safety/test_toyota.py | 2 +- tests/safety/test_volkswagen_pq.py | 2 +- 6 files changed, 22 insertions(+), 49 deletions(-) diff --git a/tests/safety/common.py b/tests/safety/common.py index df43fe9726..2f8dc56fb4 100644 --- a/tests/safety/common.py +++ b/tests/safety/common.py @@ -158,7 +158,7 @@ def test_gas_interceptor_safety_check(self): self.assertEqual(send, self._tx(self._interceptor_gas_cmd(gas))) -class TestLongitudinalAccelSafetyBase(PandaSafetyTestBase, abc.ABC): +class LongitudinalAccelSafetyTest(PandaSafetyTestBase, abc.ABC): MAX_ACCEL: float = 2.0 MIN_ACCEL: float = -3.5 @@ -181,7 +181,7 @@ def test_accel_actuation_limits(self, stock_longitudinal=False): self._generic_limit_safety_check(self._accel_msg, min_accel, max_accel, min_accel - 1, max_accel + 1, 0.05, self.INACTIVE_ACCEL, not stock_longitudinal) -class TestLongitudinalGasBrakeSafetyBase(PandaSafetyTestBase, abc.ABC): +class LongitudinalGasBrakeSafetyTest(PandaSafetyTestBase, abc.ABC): MIN_BRAKE: int = 0 MAX_BRAKE: Optional[int] = None diff --git a/tests/safety/hyundai_common.py b/tests/safety/hyundai_common.py index b55f00758f..ad93809714 100644 --- a/tests/safety/hyundai_common.py +++ b/tests/safety/hyundai_common.py @@ -74,7 +74,7 @@ def test_sampling_cruise_buttons(self): self._rx(self._button_msg(Buttons.NONE)) -class HyundaiLongitudinalBase(common.TestLongitudinalAccelSafetyBase): +class HyundaiLongitudinalBase(common.LongitudinalAccelSafetyTest): # pylint: disable=no-member,abstract-method DISABLED_ECU_UDS_MSG: Tuple[int, int] diff --git a/tests/safety/test_gm.py b/tests/safety/test_gm.py index f6b604ad31..14ce22785e 100755 --- a/tests/safety/test_gm.py +++ b/tests/safety/test_gm.py @@ -14,11 +14,14 @@ class Buttons: CANCEL = 6 -class GmLongitudinalBase(common.PandaSafetyTest): +class GmLongitudinalBase(common.PandaSafetyTest, common.LongitudinalGasBrakeSafetyTest): # pylint: disable=no-member,abstract-method PCM_CRUISE = False # openpilot can control the PCM state if longitudinal + MAX_POSSIBLE_GAS = 2**12 + MAX_POSSIBLE_BRAKE = 2**12 + def test_set_resume_buttons(self): """ SET and RESUME enter controls allowed on their falling and rising edges, respectively. @@ -54,6 +57,14 @@ def test_cruise_engaged_prev(self): def _pcm_status_msg(self, enable): pass + def _brake_msg(self, brake): + values = {"FrictionBrakeCmd": -brake} + return self.packer_chassis.make_can_msg_panda("EBCMFrictionBrakeCmd", self.BRAKE_BUS, values) + + def _gas_msg(self, gas): + values = {"GasRegenCmd": gas} + return self.packer.make_can_msg_panda("ASCMGasRegenCmd", 0, values) + class TestGmSafetyBase(common.PandaSafetyTest, common.DriverTorqueSteeringSafetyTest): STANDSTILL_THRESHOLD = 10 * 0.0311 @@ -70,11 +81,6 @@ class TestGmSafetyBase(common.PandaSafetyTest, common.DriverTorqueSteeringSafety DRIVER_TORQUE_ALLOWANCE = 65 DRIVER_TORQUE_FACTOR = 4 - MAX_GAS = 0 - MAX_REGEN = 0 - INACTIVE_REGEN = 0 - MAX_BRAKE = 0 - PCM_CRUISE = True # openpilot is tied to the PCM state if not longitudinal @classmethod @@ -118,14 +124,6 @@ def _user_gas_msg(self, gas): values["CruiseState"] = self.safety.get_controls_allowed() return self.packer.make_can_msg_panda("AcceleratorPedal2", 0, values) - def _send_brake_msg(self, brake): - values = {"FrictionBrakeCmd": -brake} - return self.packer_chassis.make_can_msg_panda("EBCMFrictionBrakeCmd", self.BRAKE_BUS, values) - - def _send_gas_msg(self, gas): - values = {"GasRegenCmd": gas} - return self.packer.make_can_msg_panda("ASCMGasRegenCmd", 0, values) - def _torque_driver_msg(self, torque): values = {"LKADriverAppldTrq": torque} return self.packer.make_can_msg_panda("PSCMStatus", 0, values) @@ -138,24 +136,6 @@ def _button_msg(self, buttons): values = {"ACCButtons": buttons} return self.packer.make_can_msg_panda("ASCMSteeringButton", self.BUTTONS_BUS, values) - def test_brake_safety_check(self): - for enabled in [0, 1]: - for b in range(0, 500): - self.safety.set_controls_allowed(enabled) - if abs(b) > self.MAX_BRAKE or (not enabled and b != 0): - self.assertFalse(self._tx(self._send_brake_msg(b))) - else: - self.assertTrue(self._tx(self._send_brake_msg(b))) - - def test_gas_safety_check(self): - # Block if enabled and out of actuation range, disabled and not inactive regen, or if stock longitudinal - for enabled in [0, 1]: - for gas_regen in range(0, 2 ** 12 - 1): - self.safety.set_controls_allowed(enabled) - should_tx = ((enabled and self.MAX_REGEN <= gas_regen <= self.MAX_GAS) or - gas_regen == self.INACTIVE_REGEN) - self.assertEqual(should_tx, self._tx(self._send_gas_msg(gas_regen)), (enabled, gas_regen)) - class TestGmAscmSafety(GmLongitudinalBase, TestGmSafetyBase): TX_MSGS = [[384, 0], [1033, 0], [1034, 0], [715, 0], [880, 0], # pt bus @@ -167,8 +147,8 @@ class TestGmAscmSafety(GmLongitudinalBase, TestGmSafetyBase): BRAKE_BUS = 2 MAX_GAS = 3072 - MAX_REGEN = 1404 - INACTIVE_REGEN = 1404 + MIN_GAS = 1404 + INACTIVE_GAS = 1404 MAX_BRAKE = 400 def setUp(self): @@ -222,13 +202,6 @@ def test_buttons(self): self._rx(self._pcm_status_msg(enabled)) self.assertEqual(enabled, self._tx(self._button_msg(Buttons.CANCEL))) - # GM Cam safety mode does not allow longitudinal messages - def test_brake_safety_check(self): - pass - - def test_gas_safety_check(self): - pass - class TestGmCameraLongitudinalSafety(GmLongitudinalBase, TestGmCameraSafetyBase): TX_MSGS = [[384, 0], [789, 0], [715, 0], [880, 0], # pt bus @@ -237,8 +210,8 @@ class TestGmCameraLongitudinalSafety(GmLongitudinalBase, TestGmCameraSafetyBase) BUTTONS_BUS = 0 # rx only MAX_GAS = 3400 - MAX_REGEN = 1514 - INACTIVE_REGEN = 1554 + MIN_GAS = 1514 + INACTIVE_GAS = 1554 MAX_BRAKE = 400 def setUp(self): diff --git a/tests/safety/test_honda.py b/tests/safety/test_honda.py index d7582e387d..11b1c623e9 100755 --- a/tests/safety/test_honda.py +++ b/tests/safety/test_honda.py @@ -536,7 +536,7 @@ def setUp(self): self.safety.init_tests() -class TestHondaBoschRadarlessLongSafety(common.TestLongitudinalAccelSafetyBase, HondaButtonEnableBase, +class TestHondaBoschRadarlessLongSafety(common.LongitudinalAccelSafetyTest, HondaButtonEnableBase, TestHondaBoschRadarlessSafetyBase): """ Covers the Honda Bosch Radarless safety mode with longitudinal control diff --git a/tests/safety/test_toyota.py b/tests/safety/test_toyota.py index eed76ea204..a0ba9c1130 100755 --- a/tests/safety/test_toyota.py +++ b/tests/safety/test_toyota.py @@ -20,7 +20,7 @@ def interceptor_msg(gas, addr): class TestToyotaSafetyBase(common.PandaSafetyTest, common.InterceptorSafetyTest, - common.TestLongitudinalAccelSafetyBase): + common.LongitudinalAccelSafetyTest): TX_MSGS = [[0x283, 0], [0x2E6, 0], [0x2E7, 0], [0x33E, 0], [0x344, 0], [0x365, 0], [0x366, 0], [0x4CB, 0], # DSU bus 0 [0x128, 1], [0x141, 1], [0x160, 1], [0x161, 1], [0x470, 1], # DSU bus 1 diff --git a/tests/safety/test_volkswagen_pq.py b/tests/safety/test_volkswagen_pq.py index a0e10b8767..be71f1d7e7 100755 --- a/tests/safety/test_volkswagen_pq.py +++ b/tests/safety/test_volkswagen_pq.py @@ -139,7 +139,7 @@ def test_spam_cancel_safety_check(self): self.assertTrue(self._tx(self._button_msg(resume=True))) -class TestVolkswagenPqLongSafety(TestVolkswagenPqSafety, common.TestLongitudinalAccelSafetyBase): +class TestVolkswagenPqLongSafety(TestVolkswagenPqSafety, common.LongitudinalAccelSafetyTest): TX_MSGS = [[MSG_HCA_1, 0], [MSG_LDW_1, 0], [MSG_ACC_SYSTEM, 0], [MSG_ACC_GRA_ANZEIGE, 0]] FWD_BLACKLISTED_ADDRS = {2: [MSG_HCA_1, MSG_LDW_1, MSG_ACC_SYSTEM, MSG_ACC_GRA_ANZEIGE]} FWD_BUS_LOOKUP = {0: 2, 2: 0} From e5c7a359b0888e2fcc71b663b5abd08a30ede5fd Mon Sep 17 00:00:00 2001 From: Justin Newberry Date: Thu, 10 Aug 2023 13:33:42 -0700 Subject: [PATCH 090/113] assert that limits are reasonable --- tests/safety/common.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/safety/common.py b/tests/safety/common.py index 2f8dc56fb4..048e2b5096 100644 --- a/tests/safety/common.py +++ b/tests/safety/common.py @@ -192,6 +192,13 @@ class LongitudinalGasBrakeSafetyTest(PandaSafetyTestBase, abc.ABC): INACTIVE_GAS = 0 MAX_POSSIBLE_GAS: Optional[int] = None + def test_gas_brake_limits_correct(self): + self.assertIsNotNone(self.MAX_POSSIBLE_BRAKE) + self.assertIsNotNone(self.MAX_POSSIBLE_GAS) + + self.assertGreater(self.MAX_BRAKE, self.MIN_BRAKE) + self.assertGreater(self.MAX_GAS, self.MIN_GAS) + @abc.abstractmethod def _gas_msg(self, gas: int): pass From 1697759cdfc9fdea8a66afa9358e3cd296ba5ae7 Mon Sep 17 00:00:00 2001 From: Justin Newberry Date: Thu, 10 Aug 2023 22:57:42 -0700 Subject: [PATCH 091/113] fix typing --- tests/safety/common.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/safety/common.py b/tests/safety/common.py index 048e2b5096..e3a778e344 100644 --- a/tests/safety/common.py +++ b/tests/safety/common.py @@ -53,6 +53,8 @@ def _regen_test(self): class PandaSafetyTestBase(unittest.TestCase): + safety: libpanda_py.libpanda + @classmethod def setUpClass(cls): if cls.__name__ == "PandaSafetyTestBase": From db1be0c8f3f4a26299030345e06e1983d71a8bc3 Mon Sep 17 00:00:00 2001 From: Justin Newberry Date: Thu, 10 Aug 2023 23:00:30 -0700 Subject: [PATCH 092/113] fix linter again --- tests/safety/common.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/safety/common.py b/tests/safety/common.py index e3a778e344..e8ac3a0e3b 100644 --- a/tests/safety/common.py +++ b/tests/safety/common.py @@ -7,6 +7,7 @@ from opendbc.can.packer import CANPacker # pylint: disable=import-error from panda import ALTERNATIVE_EXPERIENCE +from panda.python import Panda from panda.tests.libpanda import libpanda_py MAX_WRONG_COUNTERS = 5 @@ -53,8 +54,8 @@ def _regen_test(self): class PandaSafetyTestBase(unittest.TestCase): - safety: libpanda_py.libpanda - + safety: Panda + @classmethod def setUpClass(cls): if cls.__name__ == "PandaSafetyTestBase": From 2b20d281d00c9ca2d9bc23041d84109da9672d1a Mon Sep 17 00:00:00 2001 From: Justin Newberry Date: Thu, 10 Aug 2023 23:05:08 -0700 Subject: [PATCH 093/113] fix linter again --- tests/libpanda/libpanda_py.py | 2 +- tests/safety/common.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/libpanda/libpanda_py.py b/tests/libpanda/libpanda_py.py index e8f063bf97..ef19a0212f 100644 --- a/tests/libpanda/libpanda_py.py +++ b/tests/libpanda/libpanda_py.py @@ -117,7 +117,7 @@ def logging_read(self, buffer: bytearray) -> int: ... logging_bank_size: int logging_rate_limit: int -libpanda: Panda = ffi.dlopen(libpanda_fn) +libpanda: PandaSafety = ffi.dlopen(libpanda_fn) # helpers diff --git a/tests/safety/common.py b/tests/safety/common.py index e8ac3a0e3b..08615726c1 100644 --- a/tests/safety/common.py +++ b/tests/safety/common.py @@ -7,8 +7,8 @@ from opendbc.can.packer import CANPacker # pylint: disable=import-error from panda import ALTERNATIVE_EXPERIENCE -from panda.python import Panda from panda.tests.libpanda import libpanda_py +from panda.tests.libpanda.safety_helpers import PandaSafety MAX_WRONG_COUNTERS = 5 VEHICLE_SPEED_FACTOR = 100 @@ -54,7 +54,7 @@ def _regen_test(self): class PandaSafetyTestBase(unittest.TestCase): - safety: Panda + safety: PandaSafety @classmethod def setUpClass(cls): From 29c4bdef03feed1ff33b151d6ec49e7d7594da82 Mon Sep 17 00:00:00 2001 From: Justin Newberry Date: Thu, 10 Aug 2023 23:08:29 -0700 Subject: [PATCH 094/113] fix linter again --- tests/libpanda/libpanda_py.py | 2 +- tests/safety/common.py | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/libpanda/libpanda_py.py b/tests/libpanda/libpanda_py.py index ef19a0212f..e8f063bf97 100644 --- a/tests/libpanda/libpanda_py.py +++ b/tests/libpanda/libpanda_py.py @@ -117,7 +117,7 @@ def logging_read(self, buffer: bytearray) -> int: ... logging_bank_size: int logging_rate_limit: int -libpanda: PandaSafety = ffi.dlopen(libpanda_fn) +libpanda: Panda = ffi.dlopen(libpanda_fn) # helpers diff --git a/tests/safety/common.py b/tests/safety/common.py index 08615726c1..9217337b8f 100644 --- a/tests/safety/common.py +++ b/tests/safety/common.py @@ -8,7 +8,6 @@ from opendbc.can.packer import CANPacker # pylint: disable=import-error from panda import ALTERNATIVE_EXPERIENCE from panda.tests.libpanda import libpanda_py -from panda.tests.libpanda.safety_helpers import PandaSafety MAX_WRONG_COUNTERS = 5 VEHICLE_SPEED_FACTOR = 100 @@ -54,7 +53,7 @@ def _regen_test(self): class PandaSafetyTestBase(unittest.TestCase): - safety: PandaSafety + safety: libpanda_py.Panda @classmethod def setUpClass(cls): From 316823353434d33e63a570c7e5cbec7d58d3bd11 Mon Sep 17 00:00:00 2001 From: Shane Smiskol Date: Fri, 11 Aug 2023 00:02:50 -0700 Subject: [PATCH 095/113] like to make it clear --- tests/safety/common.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/safety/common.py b/tests/safety/common.py index 9217337b8f..90de4ecf75 100644 --- a/tests/safety/common.py +++ b/tests/safety/common.py @@ -202,18 +202,18 @@ def test_gas_brake_limits_correct(self): self.assertGreater(self.MAX_GAS, self.MIN_GAS) @abc.abstractmethod - def _gas_msg(self, gas: int): + def _send_gas_msg(self, gas: int): pass @abc.abstractmethod - def _brake_msg(self, brake: int): + def _send_brake_msg(self, brake: int): pass def test_brake_safety_check(self): - self._generic_limit_safety_check(self._brake_msg, self.MIN_BRAKE, self.MAX_BRAKE, 0, self.MAX_POSSIBLE_BRAKE, 1) + self._generic_limit_safety_check(self._send_brake_msg, self.MIN_BRAKE, self.MAX_BRAKE, 0, self.MAX_POSSIBLE_BRAKE, 1) def test_gas_safety_check(self): - self._generic_limit_safety_check(self._gas_msg, self.MIN_GAS, self.MAX_GAS, 0, self.MAX_POSSIBLE_GAS, 1, self.INACTIVE_GAS) + self._generic_limit_safety_check(self._send_gas_msg, self.MIN_GAS, self.MAX_GAS, 0, self.MAX_POSSIBLE_GAS, 1, self.INACTIVE_GAS) class TorqueSteeringSafetyTestBase(PandaSafetyTestBase, abc.ABC): From 27dd7b58bf0ac85e324cac1c22335cf667bd2192 Mon Sep 17 00:00:00 2001 From: Shane Smiskol Date: Fri, 11 Aug 2023 00:04:16 -0700 Subject: [PATCH 096/113] typo --- tests/safety/common.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/safety/common.py b/tests/safety/common.py index 90de4ecf75..82576a6da0 100644 --- a/tests/safety/common.py +++ b/tests/safety/common.py @@ -77,7 +77,7 @@ def _generic_limit_safety_check(self, msg_function, min_allowed_value: float, ma Enforces that a signal within a message is only allowed to be sent within a specific range, min_allowed_value -> max_allowed_value. Tests the range of min_test_value -> max_test_value with a delta of test_delta. Message is also only allowed to be sent when controls_allowed is true, unless the value is equal to inactive_value. - Message is never allowed in msg_allowed is false, for example when stock longitudinal is enabled and you are sending acceleration requests. + Message is never allowed if msg_allowed is false, for example when stock longitudinal is enabled and you are sending acceleration requests. """ # Ensure that we at least test the allowed_value range From 49081e32cc30b5af3ea422045d83f1d62457b2f6 Mon Sep 17 00:00:00 2001 From: Shane Smiskol Date: Fri, 11 Aug 2023 00:05:24 -0700 Subject: [PATCH 097/113] fix from merge --- tests/safety/test_gm.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/safety/test_gm.py b/tests/safety/test_gm.py index 992209cc55..2c022666e7 100755 --- a/tests/safety/test_gm.py +++ b/tests/safety/test_gm.py @@ -18,8 +18,8 @@ class GmLongitudinalBase(common.PandaSafetyTest, common.LongitudinalGasBrakeSafe # pylint: disable=no-member,abstract-method MAX_GAS = 0 - MAX_REGEN = 0 - INACTIVE_REGEN = 0 + MIN_GAS = 0 + INACTIVE_GAS = 0 MAX_BRAKE = 400 MAX_POSSIBLE_GAS = 2**12 @@ -84,8 +84,8 @@ def test_gas_safety_check(self): for enabled in [0, 1]: for gas_regen in range(0, 2 ** 12 - 1): self.safety.set_controls_allowed(enabled) - should_tx = ((enabled and self.MAX_REGEN <= gas_regen <= self.MAX_GAS) or - gas_regen == self.INACTIVE_REGEN) + should_tx = ((enabled and self.MIN_GAS <= gas_regen <= self.MAX_GAS) or + gas_regen == self.INACTIVE_GAS) self.assertEqual(should_tx, self._tx(self._send_gas_msg(gas_regen)), (enabled, gas_regen)) From c9ddf6421a1d0473ca1e6efa46b2224a151a23f9 Mon Sep 17 00:00:00 2001 From: Shane Smiskol Date: Fri, 11 Aug 2023 00:30:35 -0700 Subject: [PATCH 098/113] clearer names --- tests/safety/common.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/safety/common.py b/tests/safety/common.py index 82576a6da0..8cdfba8eae 100644 --- a/tests/safety/common.py +++ b/tests/safety/common.py @@ -71,22 +71,22 @@ def _rx(self, msg): def _tx(self, msg): return self.safety.safety_tx_hook(msg) - def _generic_limit_safety_check(self, msg_function, min_allowed_value: float, max_allowed_value: float, min_test_value: float, - max_test_value: float, test_delta: float = 1, inactive_value: float = 0, msg_allowed=True): + def _generic_limit_safety_check(self, msg_function, min_allowed_value: float, max_allowed_value: float, min_possible_value: float, + max_possible_value: float, test_delta: float = 1, inactive_value: float = 0, msg_allowed=True): """ Enforces that a signal within a message is only allowed to be sent within a specific range, min_allowed_value -> max_allowed_value. - Tests the range of min_test_value -> max_test_value with a delta of test_delta. + Tests the range of min_possible_value -> max_possible_value with a delta of test_delta. Message is also only allowed to be sent when controls_allowed is true, unless the value is equal to inactive_value. Message is never allowed if msg_allowed is false, for example when stock longitudinal is enabled and you are sending acceleration requests. """ # Ensure that we at least test the allowed_value range - self.assertGreater(max_test_value, max_allowed_value) - self.assertLessEqual(min_test_value, min_allowed_value) + self.assertGreater(max_possible_value, max_allowed_value) + self.assertLessEqual(min_possible_value, min_allowed_value) for controls_allowed in [False, True]: # enforce we don't skip over 0 or inactive - for v in np.concatenate((np.arange(min_test_value, max_test_value, test_delta), np.array([0, inactive_value]))): + for v in np.concatenate((np.arange(min_possible_value, max_possible_value, test_delta), np.array([0, inactive_value]))): v = round(v, 2) # floats might not hit exact boundary conditions without rounding self.safety.set_controls_allowed(controls_allowed) should_tx = controls_allowed and min_allowed_value <= v <= max_allowed_value From c0367c168ae22a87b6f4427585ebfdc8bf1e993b Mon Sep 17 00:00:00 2001 From: Justin Newberry Date: Fri, 11 Aug 2023 09:13:31 -0700 Subject: [PATCH 099/113] dont need thesemore --- tests/safety/test_gm.py | 21 --------------------- 1 file changed, 21 deletions(-) diff --git a/tests/safety/test_gm.py b/tests/safety/test_gm.py index 2c022666e7..07edf51a04 100755 --- a/tests/safety/test_gm.py +++ b/tests/safety/test_gm.py @@ -17,9 +17,6 @@ class Buttons: class GmLongitudinalBase(common.PandaSafetyTest, common.LongitudinalGasBrakeSafetyTest): # pylint: disable=no-member,abstract-method - MAX_GAS = 0 - MIN_GAS = 0 - INACTIVE_GAS = 0 MAX_BRAKE = 400 MAX_POSSIBLE_GAS = 2**12 @@ -70,24 +67,6 @@ def test_cancel_button(self): self._rx(self._button_msg(Buttons.CANCEL)) self.assertFalse(self.safety.get_controls_allowed()) - def test_brake_safety_check(self): - for enabled in [0, 1]: - for b in range(0, 500): - self.safety.set_controls_allowed(enabled) - if abs(b) > self.MAX_BRAKE or (not enabled and b != 0): - self.assertFalse(self._tx(self._send_brake_msg(b))) - else: - self.assertTrue(self._tx(self._send_brake_msg(b))) - - def test_gas_safety_check(self): - # Block if enabled and out of actuation range, disabled and not inactive regen, or if stock longitudinal - for enabled in [0, 1]: - for gas_regen in range(0, 2 ** 12 - 1): - self.safety.set_controls_allowed(enabled) - should_tx = ((enabled and self.MIN_GAS <= gas_regen <= self.MAX_GAS) or - gas_regen == self.INACTIVE_GAS) - self.assertEqual(should_tx, self._tx(self._send_gas_msg(gas_regen)), (enabled, gas_regen)) - class TestGmSafetyBase(common.PandaSafetyTest, common.DriverTorqueSteeringSafetyTest): STANDSTILL_THRESHOLD = 10 * 0.0311 From 1c4b49d10de87c3f1555577bd5cdb7d6c2b7b75d Mon Sep 17 00:00:00 2001 From: Justin Newberry Date: Fri, 11 Aug 2023 09:27:54 -0700 Subject: [PATCH 100/113] subaru: use common test --- board/safety.h | 6 ++++++ board/safety/safety_subaru.h | 5 ++++- board/safety_declarations.h | 6 ++++++ tests/safety/test_subaru.py | 29 +++++++++-------------------- 4 files changed, 25 insertions(+), 21 deletions(-) diff --git a/board/safety.h b/board/safety.h index 4d7efd2a0f..4f9a28ec61 100644 --- a/board/safety.h +++ b/board/safety.h @@ -509,6 +509,12 @@ bool longitudinal_speed_checks(int desired_speed, const LongitudinalLimits limit return !get_longitudinal_allowed() && (desired_speed != limits.inactive_speed); } +bool longitudinal_alt_checks(int desired_alt, const LongitudinalLimits limits){ + bool alt_valid = get_longitudinal_allowed() && !max_limit_check(desired_alt, limits.max_alt, limits.min_alt); + bool alt_inactive = desired_alt == limits.inactive_alt; + return !(alt_valid || alt_inactive); +} + bool longitudinal_gas_checks(int desired_gas, const LongitudinalLimits limits) { bool gas_valid = get_longitudinal_allowed() && !max_limit_check(desired_gas, limits.max_gas, limits.min_gas); bool gas_inactive = desired_gas == limits.inactive_gas; diff --git a/board/safety/safety_subaru.h b/board/safety/safety_subaru.h index 751db0f072..fbead1cd2d 100644 --- a/board/safety/safety_subaru.h +++ b/board/safety/safety_subaru.h @@ -19,6 +19,9 @@ const LongitudinalLimits SUBARU_LONG_LIMITS = { .max_gas = 3400, // approx 2 m/s^2 when maxing cruise_rpm and cruise_throttle .inactive_gas = 1818, // this is zero acceleration .max_brake = 600, // approx -3.5 m/s^2 + + .min_alt = 0, // Transmission RPM limits + .max_alt = 2400, }; #define MSG_SUBARU_Brake_Status 0x13c @@ -203,7 +206,7 @@ static int subaru_tx_hook(CANPacket_t *to_send) { // check es_status cruise_rpm limits if (addr == MSG_SUBARU_ES_Status) { int cruise_rpm = (GET_BYTES(to_send, 2, 2) & 0xFFFU); - violation |= longitudinal_gas_checks(cruise_rpm, SUBARU_LONG_LIMITS); + violation |= longitudinal_alt_checks(cruise_rpm, SUBARU_LONG_LIMITS); } if (violation) { diff --git a/board/safety_declarations.h b/board/safety_declarations.h index b2f0ea9854..de5b270a1d 100644 --- a/board/safety_declarations.h +++ b/board/safety_declarations.h @@ -90,6 +90,11 @@ typedef struct { const int inactive_gas; const int max_brake; + // alternative gas limits, ex: transmission rpm on subaru + const int max_alt; + const int min_alt; + const int inactive_alt; + // speed cmd limits const int inactive_speed; } LongitudinalLimits; @@ -163,6 +168,7 @@ bool steer_angle_cmd_checks(int desired_angle, bool steer_control_enabled, const bool longitudinal_accel_checks(int desired_accel, const LongitudinalLimits limits); bool longitudinal_speed_checks(int desired_speed, const LongitudinalLimits limits); bool longitudinal_gas_checks(int desired_gas, const LongitudinalLimits limits); +bool longitudinal_alt_checks(int desired_alt, const LongitudinalLimits limits); bool longitudinal_brake_checks(int desired_brake, const LongitudinalLimits limits); bool longitudinal_interceptor_checks(CANPacket_t *to_send); void pcm_cruise_check(bool cruise_engaged); diff --git a/tests/safety/test_subaru.py b/tests/safety/test_subaru.py index 65e21164c4..bf9a11f240 100755 --- a/tests/safety/test_subaru.py +++ b/tests/safety/test_subaru.py @@ -105,7 +105,7 @@ class TestSubaruGen2SafetyBase(TestSubaruSafetyBase): MAX_TORQUE = 1000 -class TestSubaruLongitudinalSafetyBase(TestSubaruSafetyBase): +class TestSubaruLongitudinalSafetyBase(TestSubaruSafetyBase, common.LongitudinalGasBrakeSafetyTest): MIN_GAS = 808 MAX_GAS = 3400 INACTIVE_GAS = 1818 @@ -115,37 +115,26 @@ class TestSubaruLongitudinalSafetyBase(TestSubaruSafetyBase): MAX_BRAKE = 600 MAX_POSSIBLE_BRAKE = 2**16 + MIN_RPM = 0 + MAX_RPM = 2400 + MAX_POSSIBLE_RPM = 2**12 + FWD_BLACKLISTED_ADDRS = {2: [MSG_SUBARU_ES_LKAS, MSG_SUBARU_ES_Brake, MSG_SUBARU_ES_Distance, MSG_SUBARU_ES_Status, MSG_SUBARU_ES_DashStatus, MSG_SUBARU_ES_LKAS_State, MSG_SUBARU_ES_Infotainment]} - def _generic_limit_safety_check(self, msg_function, min_value, max_value, max_possible_value, inactive_value=0): - for enabled in [0, 1]: - for v in range(min_value, max_possible_value): - self.safety.set_controls_allowed(enabled) - if (not enabled and v != inactive_value) or v > max_value or v < min_value: - self.assertFalse(self._tx(msg_function(v))) - else: - self.assertTrue(self._tx(msg_function(v))) - - def test_brake_safety_check(self): - self._generic_limit_safety_check(self._brake_msg, self.MIN_BRAKE, self.MAX_BRAKE, self.MAX_POSSIBLE_BRAKE) - - def test_gas_safety_check(self): - self._generic_limit_safety_check(self._gas_msg, self.MIN_GAS, self.MAX_GAS, self.MAX_POSSIBLE_GAS, self.INACTIVE_GAS) - def test_rpm_safety_check(self): - self._generic_limit_safety_check(self._rpm_msg, self.MIN_GAS, self.MAX_GAS, self.MAX_POSSIBLE_GAS, self.INACTIVE_GAS) + self._generic_limit_safety_check(self._send_rpm_msg, self.MIN_RPM, self.MAX_RPM, 0, self.MAX_POSSIBLE_RPM, 1) - def _brake_msg(self, brake): + def _send_brake_msg(self, brake): values = {"Brake_Pressure": brake} return self.packer.make_can_msg_panda("ES_Brake", self.ALT_BUS, values) - def _gas_msg(self, gas): + def _send_gas_msg(self, gas): values = {"Cruise_Throttle": gas} return self.packer.make_can_msg_panda("ES_Distance", self.ALT_BUS, values) - def _rpm_msg(self, rpm): + def _send_rpm_msg(self, rpm): values = {"Cruise_RPM": rpm} return self.packer.make_can_msg_panda("ES_Status", self.ALT_BUS, values) From 3e3b383b0ad7ff7579db45258134415debdfca5b Mon Sep 17 00:00:00 2001 From: Justin Newberry Date: Sun, 13 Aug 2023 02:29:40 +0000 Subject: [PATCH 101/113] fix merge issue --- board/safety/safety_subaru.h | 5 ----- 1 file changed, 5 deletions(-) diff --git a/board/safety/safety_subaru.h b/board/safety/safety_subaru.h index 253c488775..077fa07504 100644 --- a/board/safety/safety_subaru.h +++ b/board/safety/safety_subaru.h @@ -219,11 +219,6 @@ static int subaru_tx_hook(CANPacket_t *to_send) { violation |= longitudinal_alt_checks(cruise_rpm, SUBARU_LONG_LIMITS); } - // Only allow ES_Distance when Cruise_Cancel is true, and Cruise_Throttle is "inactive" (1818) - if (addr == MSG_SUBARU_ES_Distance){ - - } - if (violation){ tx = 0; } From 79f70e12145b25e603973e96da0cbfcc26a70491 Mon Sep 17 00:00:00 2001 From: Justin Newberry Date: Sun, 13 Aug 2023 02:36:18 +0000 Subject: [PATCH 102/113] inactive gas from long limits --- board/safety/safety_subaru.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/board/safety/safety_subaru.h b/board/safety/safety_subaru.h index 077fa07504..545580461b 100644 --- a/board/safety/safety_subaru.h +++ b/board/safety/safety_subaru.h @@ -207,8 +207,8 @@ static int subaru_tx_hook(CANPacket_t *to_send) { } else { // If openpilot is not controlling long, only allow ES_Distance for cruise cancel requests, - // (when Cruise_Cancel is true, and Cruise_Throttle is "inactive" at 1818) - violation |= (cruise_throttle != 1818); + // (when Cruise_Cancel is true, and Cruise_Throttle is inactive) + violation |= (cruise_throttle != SUBARU_LONG_LIMITS.inactive_gas); violation |= (!cruise_cancel); } } From 442d95ae73022c29d7d3f0809195b472b48d403a Mon Sep 17 00:00:00 2001 From: Justin Newberry Date: Sun, 13 Aug 2023 02:54:23 +0000 Subject: [PATCH 103/113] fix tests --- tests/safety/test_subaru.py | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/tests/safety/test_subaru.py b/tests/safety/test_subaru.py index ef95c2fb87..1543819ed2 100755 --- a/tests/safety/test_subaru.py +++ b/tests/safety/test_subaru.py @@ -98,7 +98,9 @@ def _user_gas_msg(self, gas): def _pcm_status_msg(self, enable): values = {"Cruise_Activated": enable} return self.packer.make_can_msg_panda("CruiseControl", self.ALT_BUS, values) + +class TestSubaruStockLongitudinalSafetyBase(TestSubaruSafetyBase): def _cancel_msg(self, cancel, cruise_throttle=0): values = {"Cruise_Cancel": cancel, "Cruise_Throttle": cruise_throttle} return self.packer.make_can_msg_panda("ES_Distance", self.ALT_BUS, values) @@ -109,14 +111,6 @@ def test_cancel_message(self): self._generic_limit_safety_check(partial(self._cancel_msg, cancel), self.INACTIVE_GAS, self.INACTIVE_GAS, 0, 2**12, 1, self.INACTIVE_GAS, cancel) -class TestSubaruGen2SafetyBase(TestSubaruSafetyBase): - ALT_BUS = SUBARU_ALT_BUS - - MAX_RATE_UP = 40 - MAX_RATE_DOWN = 40 - MAX_TORQUE = 1000 - - class TestSubaruLongitudinalSafetyBase(TestSubaruSafetyBase, common.LongitudinalGasBrakeSafetyTest): MIN_GAS = 808 MAX_GAS = 3400 @@ -151,7 +145,15 @@ def _send_rpm_msg(self, rpm): return self.packer.make_can_msg_panda("ES_Status", self.ALT_BUS, values) -class TestSubaruGen1Safety(TestSubaruSafetyBase): +class TestSubaruGen2SafetyBase(TestSubaruStockLongitudinalSafetyBase): + ALT_BUS = SUBARU_ALT_BUS + + MAX_RATE_UP = 40 + MAX_RATE_DOWN = 40 + MAX_TORQUE = 1000 + + +class TestSubaruGen1Safety(TestSubaruStockLongitudinalSafetyBase): FLAGS = 0 TX_MSGS = lkas_tx_msgs(SUBARU_MAIN_BUS) @@ -163,7 +165,7 @@ class TestSubaruGen2Safety(TestSubaruGen2SafetyBase): class TestSubaruGen1LongitudinalSafety(TestSubaruLongitudinalSafetyBase): FLAGS = Panda.FLAG_SUBARU_LONG - TX_MSGS = lkas_tx_msgs(0) + long_tx_msgs() + TX_MSGS = lkas_tx_msgs(SUBARU_MAIN_BUS) + long_tx_msgs() if __name__ == "__main__": From b36ae945bdbc08a4bab6e6e7c4701253641890f4 Mon Sep 17 00:00:00 2001 From: Justin Newberry Date: Sun, 13 Aug 2023 02:57:29 +0000 Subject: [PATCH 104/113] linter --- tests/safety/test_subaru.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/safety/test_subaru.py b/tests/safety/test_subaru.py index 1543819ed2..430e9d3318 100755 --- a/tests/safety/test_subaru.py +++ b/tests/safety/test_subaru.py @@ -98,7 +98,7 @@ def _user_gas_msg(self, gas): def _pcm_status_msg(self, enable): values = {"Cruise_Activated": enable} return self.packer.make_can_msg_panda("CruiseControl", self.ALT_BUS, values) - + class TestSubaruStockLongitudinalSafetyBase(TestSubaruSafetyBase): def _cancel_msg(self, cancel, cruise_throttle=0): From 8b0146c7cbdcf11884047487e0ebeb499d88dd8c Mon Sep 17 00:00:00 2001 From: Justin Newberry Date: Tue, 15 Aug 2023 02:31:46 +0000 Subject: [PATCH 105/113] fix gen2 --- tests/safety/test_subaru.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/safety/test_subaru.py b/tests/safety/test_subaru.py index efd93755fc..8e91246f26 100755 --- a/tests/safety/test_subaru.py +++ b/tests/safety/test_subaru.py @@ -159,6 +159,9 @@ class TestSubaruGen2TorqueStockLongitudinalSafety(TestSubaruStockLongitudinalSaf MAX_RATE_DOWN = 40 MAX_TORQUE = 1000 + FLAGS = Panda.FLAG_SUBARU_GEN2 + TX_MSGS = lkas_tx_msgs(SUBARU_ALT_BUS) + class TestSubaruGen1LongitudinalSafety(TestSubaruLongitudinalSafetyBase): FLAGS = Panda.FLAG_SUBARU_LONG From 8f4c7d76cb71701c83bcaf7df1f8962ca25f33d3 Mon Sep 17 00:00:00 2001 From: Justin Newberry Date: Tue, 15 Aug 2023 02:51:58 +0000 Subject: [PATCH 106/113] fix linter --- tests/safety/common.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/safety/common.py b/tests/safety/common.py index dcf908cf03..36df296eac 100644 --- a/tests/safety/common.py +++ b/tests/safety/common.py @@ -954,7 +954,8 @@ def test_tx_hook_on_wrong_safety_mode(self): continue if attr.startswith('TestToyota') and current_test.startswith('TestToyota'): continue - if {attr, current_test}.issubset({'TestSubaruGen1TorqueStockLongitudinalSafety', 'TestSubaruGen2TorqueStockLongitudinalSafety', 'TestSubaruGen1LongitudinalSafety'}): + if {attr, current_test}.issubset({'TestSubaruGen1TorqueStockLongitudinalSafety', 'TestSubaruGen2TorqueStockLongitudinalSafety', + 'TestSubaruGen1LongitudinalSafety'}): continue if {attr, current_test}.issubset({'TestVolkswagenPqSafety', 'TestVolkswagenPqStockSafety', 'TestVolkswagenPqLongSafety'}): continue From ac7d178019e068437945a05e1dcca4b6a38d020f Mon Sep 17 00:00:00 2001 From: Justin Newberry Date: Tue, 15 Aug 2023 15:13:07 -0700 Subject: [PATCH 107/113] also test torque when doing long control --- tests/safety/test_subaru.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/safety/test_subaru.py b/tests/safety/test_subaru.py index 8e91246f26..f5c30e6e0a 100755 --- a/tests/safety/test_subaru.py +++ b/tests/safety/test_subaru.py @@ -163,7 +163,7 @@ class TestSubaruGen2TorqueStockLongitudinalSafety(TestSubaruStockLongitudinalSaf TX_MSGS = lkas_tx_msgs(SUBARU_ALT_BUS) -class TestSubaruGen1LongitudinalSafety(TestSubaruLongitudinalSafetyBase): +class TestSubaruGen1LongitudinalSafety(TestSubaruLongitudinalSafetyBase, TestSubaruTorqueSafetyBase): FLAGS = Panda.FLAG_SUBARU_LONG TX_MSGS = lkas_tx_msgs(SUBARU_MAIN_BUS) + long_tx_msgs() From 495426a7f6b51020dc7e0e30cd0127751d52e0b3 Mon Sep 17 00:00:00 2001 From: Justin Newberry Date: Tue, 15 Aug 2023 16:14:09 -0700 Subject: [PATCH 108/113] renamre transmision rpm --- board/safety.h | 8 ++++---- board/safety_declarations.h | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/board/safety.h b/board/safety.h index 4f9a28ec61..8b90676e11 100644 --- a/board/safety.h +++ b/board/safety.h @@ -509,10 +509,10 @@ bool longitudinal_speed_checks(int desired_speed, const LongitudinalLimits limit return !get_longitudinal_allowed() && (desired_speed != limits.inactive_speed); } -bool longitudinal_alt_checks(int desired_alt, const LongitudinalLimits limits){ - bool alt_valid = get_longitudinal_allowed() && !max_limit_check(desired_alt, limits.max_alt, limits.min_alt); - bool alt_inactive = desired_alt == limits.inactive_alt; - return !(alt_valid || alt_inactive); +bool longitudinal_transmission_rpm_checks(int desired_transmission_rpm, const LongitudinalLimits limits){ + bool transmission_rpm_valid = get_longitudinal_allowed() && !max_limit_check(desired_transmission_rpm, limits.max_transmission_rpm, limits.min_transmission_rpm); + bool transmission_rpm_inactive = desired_transmission_rpm == limits.inactive_transmission_rpm; + return !(transmission_rpm_valid || transmission_rpm_inactive); } bool longitudinal_gas_checks(int desired_gas, const LongitudinalLimits limits) { diff --git a/board/safety_declarations.h b/board/safety_declarations.h index 0c40c560f0..89119275df 100644 --- a/board/safety_declarations.h +++ b/board/safety_declarations.h @@ -90,10 +90,10 @@ typedef struct { const int inactive_gas; const int max_brake; - // alternative cmd limits, ex: transmission rpm on subaru - const int max_alt; - const int min_alt; - const int inactive_alt; + // transmission rpm limits + const int max_transmission_rpm; + const int min_transmission_rpm; + const int inactive_transmission_rpm; // speed cmd limits const int inactive_speed; From 34e9163b74cff728a1261ea5214d3979fc472e30 Mon Sep 17 00:00:00 2001 From: Justin Newberry Date: Tue, 15 Aug 2023 16:15:13 -0700 Subject: [PATCH 109/113] consistent whitespace --- board/safety.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/board/safety.h b/board/safety.h index 8b90676e11..645d41d830 100644 --- a/board/safety.h +++ b/board/safety.h @@ -509,7 +509,7 @@ bool longitudinal_speed_checks(int desired_speed, const LongitudinalLimits limit return !get_longitudinal_allowed() && (desired_speed != limits.inactive_speed); } -bool longitudinal_transmission_rpm_checks(int desired_transmission_rpm, const LongitudinalLimits limits){ +bool longitudinal_transmission_rpm_checks(int desired_transmission_rpm, const LongitudinalLimits limits) { bool transmission_rpm_valid = get_longitudinal_allowed() && !max_limit_check(desired_transmission_rpm, limits.max_transmission_rpm, limits.min_transmission_rpm); bool transmission_rpm_inactive = desired_transmission_rpm == limits.inactive_transmission_rpm; return !(transmission_rpm_valid || transmission_rpm_inactive); From 0b1f0ad431d7e2618e67809d47ebf8bf80ab3890 Mon Sep 17 00:00:00 2001 From: Justin Newberry Date: Tue, 15 Aug 2023 16:29:31 -0700 Subject: [PATCH 110/113] rename declarations too --- board/safety_declarations.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/board/safety_declarations.h b/board/safety_declarations.h index 89119275df..9064bb62e0 100644 --- a/board/safety_declarations.h +++ b/board/safety_declarations.h @@ -168,7 +168,7 @@ bool steer_angle_cmd_checks(int desired_angle, bool steer_control_enabled, const bool longitudinal_accel_checks(int desired_accel, const LongitudinalLimits limits); bool longitudinal_speed_checks(int desired_speed, const LongitudinalLimits limits); bool longitudinal_gas_checks(int desired_gas, const LongitudinalLimits limits); -bool longitudinal_alt_checks(int desired_alt, const LongitudinalLimits limits); +bool longitudinal_transmission_rpm_checks(int desired_transmission_rpm, const LongitudinalLimits limits); bool longitudinal_brake_checks(int desired_brake, const LongitudinalLimits limits); bool longitudinal_interceptor_checks(CANPacket_t *to_send); void pcm_cruise_check(bool cruise_engaged); From 0ecc177c97be15c88ccf76c5fbb3585aa63d3956 Mon Sep 17 00:00:00 2001 From: Justin Newberry Date: Tue, 15 Aug 2023 16:31:05 -0700 Subject: [PATCH 111/113] rename transmission rpm --- board/safety/safety_subaru.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/board/safety/safety_subaru.h b/board/safety/safety_subaru.h index 545580461b..c81bfe9a53 100644 --- a/board/safety/safety_subaru.h +++ b/board/safety/safety_subaru.h @@ -20,8 +20,8 @@ const LongitudinalLimits SUBARU_LONG_LIMITS = { .inactive_gas = 1818, // this is zero acceleration .max_brake = 600, // approx -3.5 m/s^2 - .min_alt = 0, // Transmission RPM limits - .max_alt = 2400, + .min_transmission_rpm = 0, + .max_transmission_rpm = 2400, }; #define MSG_SUBARU_Brake_Status 0x13c @@ -216,7 +216,7 @@ static int subaru_tx_hook(CANPacket_t *to_send) { // check es_status cruise_rpm limits if (addr == MSG_SUBARU_ES_Status) { int cruise_rpm = (GET_BYTES(to_send, 2, 2) & 0xFFFU); - violation |= longitudinal_alt_checks(cruise_rpm, SUBARU_LONG_LIMITS); + violation |= longitudinal_transmission_rpm_checks(cruise_rpm, SUBARU_LONG_LIMITS); } if (violation){ From 3fba0208c06b05ae648aaf6d72621c5de8a9f881 Mon Sep 17 00:00:00 2001 From: Justin Newberry Date: Tue, 15 Aug 2023 17:28:10 -0700 Subject: [PATCH 112/113] same line --- board/safety/safety_subaru.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/board/safety/safety_subaru.h b/board/safety/safety_subaru.h index c81bfe9a53..7d467a0ba7 100644 --- a/board/safety/safety_subaru.h +++ b/board/safety/safety_subaru.h @@ -204,8 +204,7 @@ static int subaru_tx_hook(CANPacket_t *to_send) { if (subaru_longitudinal) { violation |= longitudinal_gas_checks(cruise_throttle, SUBARU_LONG_LIMITS); - } - else { + } else { // If openpilot is not controlling long, only allow ES_Distance for cruise cancel requests, // (when Cruise_Cancel is true, and Cruise_Throttle is inactive) violation |= (cruise_throttle != SUBARU_LONG_LIMITS.inactive_gas); From 9e1312ef599bee533b6a474ef5bd8d2e2d1362e3 Mon Sep 17 00:00:00 2001 From: Justin Newberry Date: Tue, 15 Aug 2023 17:29:25 -0700 Subject: [PATCH 113/113] actually is transmission rpm --- board/safety/safety_subaru.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/board/safety/safety_subaru.h b/board/safety/safety_subaru.h index 7d467a0ba7..7a64362566 100644 --- a/board/safety/safety_subaru.h +++ b/board/safety/safety_subaru.h @@ -212,10 +212,10 @@ static int subaru_tx_hook(CANPacket_t *to_send) { } } - // check es_status cruise_rpm limits + // check es_status transmission_rpm limits if (addr == MSG_SUBARU_ES_Status) { - int cruise_rpm = (GET_BYTES(to_send, 2, 2) & 0xFFFU); - violation |= longitudinal_transmission_rpm_checks(cruise_rpm, SUBARU_LONG_LIMITS); + int transmission_rpm = (GET_BYTES(to_send, 2, 2) & 0xFFFU); + violation |= longitudinal_transmission_rpm_checks(transmission_rpm, SUBARU_LONG_LIMITS); } if (violation){