Skip to content

Commit

Permalink
Initial attempt at 2 accel msg testing
Browse files Browse the repository at this point in the history
  • Loading branch information
chrispypatt committed Nov 5, 2024
1 parent 6cc54a5 commit 9d53389
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 23 deletions.
24 changes: 19 additions & 5 deletions tests/safety/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,20 @@ def setUpClass(cls):
def _accel_msg(self, accel: float):
pass

def _accel_msg_2(self, accel: float):
return None

def _should_tx_1(self, controls_allowed: bool, stock_longitudinal: bool, accel: float, min_accel: float, max_accel: float):
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
return should_tx

def _should_tx_2(self, controls_allowed: bool, stock_longitudinal: bool, accel: float, min_accel: float, max_accel: float):
return None

def test_accel_limits_correct(self):
self.assertGreater(self.MAX_ACCEL, 0)
self.assertLess(self.MIN_ACCEL, 0)
Expand All @@ -149,12 +163,12 @@ def test_accel_actuation_limits(self, stock_longitudinal=False):
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
should_tx = self._should_tx_1(controls_allowed, stock_longitudinal, accel, min_accel, max_accel)
self.assertEqual(should_tx, self._tx(self._accel_msg(accel)))
accel_msg_2 = self._accel_msg_2(accel)
if accel_msg_2 is not None:
should_tx_2 = self._should_tx_2(controls_allowed, stock_longitudinal, accel, min_accel, max_accel)
self.assertEqual(should_tx_2, self._tx(self._accel_msg_2(accel)))


class LongitudinalGasBrakeSafetyTest(PandaSafetyTestBase, abc.ABC):
Expand Down
33 changes: 15 additions & 18 deletions tests/safety/test_toyota.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import unittest
import itertools

from panda import Panda, ALTERNATIVE_EXPERIENCE
from panda import Panda
from panda.tests.libpanda import libpanda_py
import panda.tests.safety.common as common
from panda.tests.safety.common import CANPackerPanda
Expand Down Expand Up @@ -366,24 +366,21 @@ def _accel_2_msg(self, accel, cancel_req=0):
values = {"ACCEL_CMD": accel}
return self.packer.make_can_msg_panda("ACC_CONTROL_2", 0, values)

# FIXME: Replaces common test, refactor common tests to handle this situation better?
# On a SecOC vehicle, we still transmit ACC_CONTROL but the accel value moves to ACC_CONTROL_2
# Verify that all non-idle accel values in ACC_CONTROL are rejected, verify ACC_CONTROL_2 accel normally
def _should_tx_1(self, controls_allowed: bool, stock_longitudinal: bool, accel: float, min_accel: float, max_accel: float):
return accel == self.INACTIVE_ACCEL

def _should_tx_2(self, controls_allowed: bool, stock_longitudinal: bool, accel: float, min_accel: float, max_accel: float):
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
return should_tx

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))

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)
# On a SecOC vehicle, we still transmit ACC_CONTROL but the accel value moves to ACC_CONTROL_2
# Verify that all non-idle accel values in ACC_CONTROL are rejected, verify ACC_CONTROL_2 accel normally
should_tx_1 = accel == self.INACTIVE_ACCEL
should_tx_2 = (controls_allowed and min_accel <= accel <= max_accel) or accel == self.INACTIVE_ACCEL
self.assertEqual(should_tx_1, self._tx(self._accel_msg(accel)))
self.assertEqual(should_tx_2, self._tx(self._accel_2_msg(accel)))
super().test_accel_actuation_limits(stock_longitudinal=stock_longitudinal)


if __name__ == "__main__":
Expand Down

0 comments on commit 9d53389

Please sign in to comment.