From d9355c4148c7a71b7763ad51eb42feaa8f9bcfcb Mon Sep 17 00:00:00 2001 From: Adeeb <8762862+quillford@users.noreply.github.com> Date: Mon, 27 Apr 2020 22:13:30 -0700 Subject: [PATCH] Make cruise_engaged_prev a global + test case for it (#519) * make cruise_engaged_prev a global * test for cruise_engaged_prev --- board/safety/safety_chrysler.h | 5 ++--- board/safety/safety_hyundai.h | 5 ++--- board/safety/safety_nissan.h | 5 ++--- board/safety/safety_subaru.h | 5 ++--- board/safety/safety_toyota.h | 5 ++--- board/safety_declarations.h | 1 + tests/safety/common.py | 7 +++++++ tests/safety/libpandasafety_py.py | 1 + tests/safety/test.c | 4 ++++ tests/safety/test_gm.py | 1 + tests/safety/test_honda.py | 1 + tests/safety/test_volkswagen_mqb.py | 3 +++ tests/safety/test_volkswagen_pq.py | 3 +++ 13 files changed, 31 insertions(+), 15 deletions(-) diff --git a/board/safety/safety_chrysler.h b/board/safety/safety_chrysler.h index df0e27a7cbba39b..3ace1a3ac2b5c94 100644 --- a/board/safety/safety_chrysler.h +++ b/board/safety/safety_chrysler.h @@ -20,7 +20,6 @@ const int CHRYSLER_RX_CHECK_LEN = sizeof(chrysler_rx_checks) / sizeof(chrysler_r int chrysler_rt_torque_last = 0; int chrysler_desired_torque_last = 0; -int chrysler_cruise_engaged_last = 0; int chrysler_speed = 0; uint32_t chrysler_ts_last = 0; struct sample_t chrysler_torque_meas; // last few torques measured @@ -90,13 +89,13 @@ static int chrysler_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) { // enter controls on rising edge of ACC, exit controls on ACC off if (addr == 500) { int cruise_engaged = ((GET_BYTE(to_push, 2) & 0x38) >> 3) == 7; - if (cruise_engaged && !chrysler_cruise_engaged_last) { + if (cruise_engaged && !cruise_engaged_prev) { controls_allowed = 1; } if (!cruise_engaged) { controls_allowed = 0; } - chrysler_cruise_engaged_last = cruise_engaged; + cruise_engaged_prev = cruise_engaged; } // update speed diff --git a/board/safety/safety_hyundai.h b/board/safety/safety_hyundai.h index 171ff8db85f6bc0..651db4a13863985 100644 --- a/board/safety/safety_hyundai.h +++ b/board/safety/safety_hyundai.h @@ -20,7 +20,6 @@ const int HYUNDAI_RX_CHECK_LEN = sizeof(hyundai_rx_checks) / sizeof(hyundai_rx_c int hyundai_rt_torque_last = 0; int hyundai_desired_torque_last = 0; -int hyundai_cruise_engaged_last = 0; uint32_t hyundai_ts_last = 0; struct sample_t hyundai_torque_driver; // last few driver torques measured @@ -45,13 +44,13 @@ static int hyundai_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) { if (addr == 1057) { // 2 bits: 13-14 int cruise_engaged = (GET_BYTES_04(to_push) >> 13) & 0x3; - if (cruise_engaged && !hyundai_cruise_engaged_last) { + if (cruise_engaged && !cruise_engaged_prev) { controls_allowed = 1; } if (!cruise_engaged) { controls_allowed = 0; } - hyundai_cruise_engaged_last = cruise_engaged; + cruise_engaged_prev = cruise_engaged; } // exit controls on rising edge of gas press diff --git a/board/safety/safety_nissan.h b/board/safety/safety_nissan.h index 07e0f430568ef90..aca99d19b69aa9d 100644 --- a/board/safety/safety_nissan.h +++ b/board/safety/safety_nissan.h @@ -25,7 +25,6 @@ const int NISSAN_RX_CHECK_LEN = sizeof(nissan_rx_checks) / sizeof(nissan_rx_chec float nissan_speed = 0; //int nissan_controls_allowed_last = 0; uint32_t nissan_ts_angle_last = 0; -int nissan_cruise_engaged_last = 0; int nissan_desired_angle_last = 0; struct sample_t nissan_angle_meas; // last 3 steer angles @@ -104,13 +103,13 @@ static int nissan_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) { if ((bus == 2) && (addr == 0x30f)) { bool cruise_engaged = (GET_BYTE(to_push, 0) >> 3) & 1; - if (cruise_engaged && !nissan_cruise_engaged_last) { + if (cruise_engaged && !cruise_engaged_prev) { controls_allowed = 1; } if (!cruise_engaged) { controls_allowed = 0; } - nissan_cruise_engaged_last = cruise_engaged; + cruise_engaged_prev = cruise_engaged; } } return valid; diff --git a/board/safety/safety_subaru.h b/board/safety/safety_subaru.h index 4d609811dbf4e43..11d85643245b320 100644 --- a/board/safety/safety_subaru.h +++ b/board/safety/safety_subaru.h @@ -30,7 +30,6 @@ AddrCheckStruct subaru_l_rx_checks[] = { const int SUBARU_RX_CHECK_LEN = sizeof(subaru_rx_checks) / sizeof(subaru_rx_checks[0]); const int SUBARU_L_RX_CHECK_LEN = sizeof(subaru_l_rx_checks) / sizeof(subaru_l_rx_checks[0]); -int subaru_cruise_engaged_last = 0; int subaru_rt_torque_last = 0; int subaru_desired_torque_last = 0; uint32_t subaru_ts_last = 0; @@ -87,13 +86,13 @@ static int subaru_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) { ((addr == 0x144) && !subaru_global)) { int bit_shift = subaru_global ? 9 : 17; int cruise_engaged = ((GET_BYTES_48(to_push) >> bit_shift) & 1); - if (cruise_engaged && !subaru_cruise_engaged_last) { + if (cruise_engaged && !cruise_engaged_prev) { controls_allowed = 1; } if (!cruise_engaged) { controls_allowed = 0; } - subaru_cruise_engaged_last = cruise_engaged; + cruise_engaged_prev = cruise_engaged; } // sample subaru wheel speed, averaging opposite corners diff --git a/board/safety/safety_toyota.h b/board/safety/safety_toyota.h index 5acd894ce3b4d58..f0ed6c4c4b2cf75 100644 --- a/board/safety/safety_toyota.h +++ b/board/safety/safety_toyota.h @@ -49,7 +49,6 @@ int toyota_dbc_eps_torque_factor = 100; // conversion factor for STEER_TORQUE_ int toyota_desired_torque_last = 0; // last desired steer torque int toyota_rt_torque_last = 0; // last desired torque for real time check uint32_t toyota_ts_last = 0; -int toyota_cruise_engaged_last = 0; // cruise state struct sample_t toyota_torque_meas; // last 3 motor torques produced by the eps @@ -101,10 +100,10 @@ static int toyota_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) { if (!cruise_engaged) { controls_allowed = 0; } - if (cruise_engaged && !toyota_cruise_engaged_last) { + if (cruise_engaged && !cruise_engaged_prev) { controls_allowed = 1; } - toyota_cruise_engaged_last = cruise_engaged; + cruise_engaged_prev = cruise_engaged; // handle gas_pressed bool gas_pressed = ((GET_BYTE(to_push, 0) >> 4) & 1) == 0; diff --git a/board/safety_declarations.h b/board/safety_declarations.h index 4af83957070212d..68d20fca6f5d095 100644 --- a/board/safety_declarations.h +++ b/board/safety_declarations.h @@ -89,6 +89,7 @@ bool gas_interceptor_detected = false; int gas_interceptor_prev = 0; bool gas_pressed_prev = false; bool brake_pressed_prev = false; +bool cruise_engaged_prev = false; bool vehicle_moving = false; // This can be set with a USB command diff --git a/tests/safety/common.py b/tests/safety/common.py index da04f52be0db6f1..44050edf61ff65d 100644 --- a/tests/safety/common.py +++ b/tests/safety/common.py @@ -226,6 +226,13 @@ def test_disable_control_allowed_from_cruise(self): self._rx(self._pcm_status_msg(False)) self.assertFalse(self.safety.get_controls_allowed()) + def test_cruise_engaged_prev(self): + for engaged in [True, False]: + self._rx(self._pcm_status_msg(engaged)) + self.assertEqual(engaged, self.safety.get_cruise_engaged_prev()) + self._rx(self._pcm_status_msg(not engaged)) + self.assertEqual(not engaged, self.safety.get_cruise_engaged_prev()) + def test_allow_brake_at_zero_speed(self): # Brake was already pressed self._rx(self._speed_msg(0)) diff --git a/tests/safety/libpandasafety_py.py b/tests/safety/libpandasafety_py.py index 5e5ad2c27737efb..dc2cc2bea32838b 100644 --- a/tests/safety/libpandasafety_py.py +++ b/tests/safety/libpandasafety_py.py @@ -41,6 +41,7 @@ int get_gas_interceptor_prev(void); bool get_gas_pressed_prev(void); bool get_brake_pressed_prev(void); +bool get_cruise_engaged_prev(void); bool get_vehicle_moving(void); int get_hw_type(void); void set_timer(uint32_t t); diff --git a/tests/safety/test.c b/tests/safety/test.c index 66283127603ebdd..b5c4f9b1da818e0 100644 --- a/tests/safety/test.c +++ b/tests/safety/test.c @@ -129,6 +129,10 @@ bool get_brake_pressed_prev(void){ return brake_pressed_prev; } +bool get_cruise_engaged_prev(void){ + return cruise_engaged_prev; +} + bool get_vehicle_moving(void){ return vehicle_moving; } diff --git a/tests/safety/test_gm.py b/tests/safety/test_gm.py index d5d3ad07dff01d2..25b7238dd57813a 100644 --- a/tests/safety/test_gm.py +++ b/tests/safety/test_gm.py @@ -40,6 +40,7 @@ def setUp(self): # override these tests from PandaSafetyTest, GM uses button enable def test_disable_control_allowed_from_cruise(self): pass def test_enable_control_allowed_from_cruise(self): pass + def test_cruise_engaged_prev(self): pass def _speed_msg(self, speed): values = {"%sWheelSpd"%s: speed for s in ["RL", "RR"]} diff --git a/tests/safety/test_honda.py b/tests/safety/test_honda.py index 9617afa1eaef9ff..ffdd0635cce57a0 100755 --- a/tests/safety/test_honda.py +++ b/tests/safety/test_honda.py @@ -36,6 +36,7 @@ def setUpClass(cls): # override these inherited tests. honda doesn't use pcm enable def test_disable_control_allowed_from_cruise(self): pass def test_enable_control_allowed_from_cruise(self): pass + def test_cruise_engaged_prev(self): pass def _speed_msg(self, speed): values = {"XMISSION_SPEED": speed, "COUNTER": self.cnt_speed % 4} diff --git a/tests/safety/test_volkswagen_mqb.py b/tests/safety/test_volkswagen_mqb.py index c8c4111feeb1587..749103477ecad81 100644 --- a/tests/safety/test_volkswagen_mqb.py +++ b/tests/safety/test_volkswagen_mqb.py @@ -48,6 +48,9 @@ def setUp(self): self.safety.set_safety_hooks(Panda.SAFETY_VOLKSWAGEN_MQB, 0) self.safety.init_tests_volkswagen() + # override these inherited tests from PandaSafetyTest + def test_cruise_engaged_prev(self): pass + def _set_prev_torque(self, t): self.safety.set_volkswagen_desired_torque_last(t) self.safety.set_volkswagen_rt_torque_last(t) diff --git a/tests/safety/test_volkswagen_pq.py b/tests/safety/test_volkswagen_pq.py index b5c84fa9cce17de..8e2b75ce9713088 100644 --- a/tests/safety/test_volkswagen_pq.py +++ b/tests/safety/test_volkswagen_pq.py @@ -52,6 +52,9 @@ def setUp(self): self.safety.set_safety_hooks(Panda.SAFETY_VOLKSWAGEN_PQ, 0) self.safety.init_tests_volkswagen() + # override these inherited tests from PandaSafetyTest + def test_cruise_engaged_prev(self): pass + def _set_prev_torque(self, t): self.safety.set_volkswagen_desired_torque_last(t) self.safety.set_volkswagen_rt_torque_last(t)