Skip to content

Commit

Permalink
Make cruise_engaged_prev a global + test case for it (commaai#519)
Browse files Browse the repository at this point in the history
* make cruise_engaged_prev a global

* test for cruise_engaged_prev
  • Loading branch information
adeebshihadeh authored Apr 28, 2020
1 parent 2115376 commit d9355c4
Show file tree
Hide file tree
Showing 13 changed files with 31 additions and 15 deletions.
5 changes: 2 additions & 3 deletions board/safety/safety_chrysler.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions board/safety/safety_hyundai.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
5 changes: 2 additions & 3 deletions board/safety/safety_nissan.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
5 changes: 2 additions & 3 deletions board/safety/safety_subaru.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions board/safety/safety_toyota.h
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions board/safety_declarations.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions tests/safety/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
1 change: 1 addition & 0 deletions tests/safety/libpandasafety_py.py
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions tests/safety/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions tests/safety/test_gm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]}
Expand Down
1 change: 1 addition & 0 deletions tests/safety/test_honda.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
3 changes: 3 additions & 0 deletions tests/safety/test_volkswagen_mqb.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions tests/safety/test_volkswagen_pq.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit d9355c4

Please sign in to comment.