Skip to content

Commit

Permalink
GM: clean up longitudinal safety test (commaai#1573)
Browse files Browse the repository at this point in the history
* clean up gm long safety test

* move to long base

* common brake
  • Loading branch information
sshane authored Aug 11, 2023
1 parent 25f1444 commit 61590b6
Showing 1 changed file with 23 additions and 32 deletions.
55 changes: 23 additions & 32 deletions tests/safety/test_gm.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ class Buttons:
class GmLongitudinalBase(common.PandaSafetyTest):
# pylint: disable=no-member,abstract-method

MAX_GAS = 0
MAX_REGEN = 0
INACTIVE_REGEN = 0
MAX_BRAKE = 400

PCM_CRUISE = False # openpilot can control the PCM state if longitudinal

def test_set_resume_buttons(self):
Expand All @@ -41,6 +46,24 @@ 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.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))

# override these tests from PandaSafetyTest, GM longitudinal uses button enable
def test_disable_control_allowed_from_cruise(self):
pass
Expand Down Expand Up @@ -70,11 +93,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
Expand Down Expand Up @@ -138,24 +156,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
Expand All @@ -169,7 +169,6 @@ class TestGmAscmSafety(GmLongitudinalBase, TestGmSafetyBase):
MAX_GAS = 3072
MAX_REGEN = 1404
INACTIVE_REGEN = 1404
MAX_BRAKE = 400

def setUp(self):
self.packer = CANPackerPanda("gm_global_a_powertrain_generated")
Expand Down Expand Up @@ -222,13 +221,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
Expand All @@ -239,7 +231,6 @@ class TestGmCameraLongitudinalSafety(GmLongitudinalBase, TestGmCameraSafetyBase)
MAX_GAS = 3400
MAX_REGEN = 1514
INACTIVE_REGEN = 1554
MAX_BRAKE = 400

def setUp(self):
self.packer = CANPackerPanda("gm_global_a_powertrain_generated")
Expand Down

0 comments on commit 61590b6

Please sign in to comment.