Skip to content

Commit

Permalink
Hyundai: fix button enable controls mismatch (commaai#1152)
Browse files Browse the repository at this point in the history
* fix hyundai long button controls mismatch

* test

* comment
  • Loading branch information
sshane authored and sunnyhaibin committed Nov 12, 2022
1 parent 016615e commit 156f37e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
14 changes: 7 additions & 7 deletions board/safety/safety_hyundai_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,19 @@ void hyundai_common_cruise_buttons_check(const int cruise_button, const int main
}

if (hyundai_longitudinal) {
// exit controls on cancel press
if (cruise_button == HYUNDAI_BTN_CANCEL) {
controls_allowed_long = 0;
}

// enter controls on falling edge of resume or set
bool set = (cruise_button == HYUNDAI_BTN_NONE) && (cruise_button_prev == HYUNDAI_BTN_SET);
bool res = (cruise_button == HYUNDAI_BTN_NONE) && (cruise_button_prev == HYUNDAI_BTN_RESUME);
bool set = (cruise_button != HYUNDAI_BTN_SET) && (cruise_button_prev == HYUNDAI_BTN_SET);
bool res = (cruise_button != HYUNDAI_BTN_RESUME) && (cruise_button_prev == HYUNDAI_BTN_RESUME);
if (set || res) {
controls_allowed = 1;
controls_allowed_long = 1;
}

// exit controls on cancel press
if (cruise_button == HYUNDAI_BTN_CANCEL) {
controls_allowed_long = 0;
}

cruise_button_prev = cruise_button;
}
}
Expand Down
23 changes: 14 additions & 9 deletions tests/safety/hyundai_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,21 @@ def test_set_resume_buttons(self):
"""
SET and RESUME enter controls allowed on their falling edge.
"""
for btn in range(8):
self.safety.set_controls_allowed(0)
for _ in range(10):
self._rx(self._button_msg(btn))
self.assertFalse(self.safety.get_controls_allowed())

# should enter controls allowed on falling edge
if btn in (Buttons.RESUME, Buttons.SET):
for btn_prev in range(8):
for btn_cur in range(8):
self._rx(self._button_msg(Buttons.NONE))
self.assertTrue(self.safety.get_controls_allowed())
self.safety.set_controls_allowed(0)
for _ in range(10):
self._rx(self._button_msg(btn_prev))
self.assertFalse(self.safety.get_controls_allowed())

# should enter controls allowed on falling edge and not transitioning to cancel
should_enable = btn_cur != btn_prev and \
btn_cur != Buttons.CANCEL and \
btn_prev in (Buttons.RESUME, Buttons.SET)

self._rx(self._button_msg(btn_cur))
self.assertEqual(should_enable, self.safety.get_controls_allowed())

def test_cancel_button(self):
self.safety.set_controls_allowed(1)
Expand Down

0 comments on commit 156f37e

Please sign in to comment.