2019-09-24 18:50:53 -06:00
|
|
|
#!/usr/bin/env python3
|
2018-04-02 12:41:52 -06:00
|
|
|
import unittest
|
|
|
|
import numpy as np
|
2019-10-02 19:20:32 -06:00
|
|
|
from panda import Panda
|
2019-11-15 01:52:34 -07:00
|
|
|
from panda.tests.safety import libpandasafety_py
|
2020-04-13 18:13:08 -06:00
|
|
|
import panda.tests.safety.common as common
|
2020-04-16 15:52:59 -06:00
|
|
|
from panda.tests.safety.common import CANPackerPanda, make_msg, UNSAFE_MODE
|
2018-04-02 12:41:52 -06:00
|
|
|
|
2020-04-10 14:45:55 -06:00
|
|
|
MAX_ACCEL = 1.5
|
|
|
|
MIN_ACCEL = -3.0
|
2018-04-02 12:41:52 -06:00
|
|
|
|
2020-04-10 14:45:55 -06:00
|
|
|
ISO_MAX_ACCEL = 2.0
|
|
|
|
ISO_MIN_ACCEL = -3.5
|
2020-04-06 09:56:36 -06:00
|
|
|
|
2020-05-31 18:39:42 -06:00
|
|
|
class TestToyotaSafety(common.PandaSafetyTest, common.InterceptorSafetyTest,
|
2020-04-29 21:37:12 -06:00
|
|
|
common.TorqueSteeringSafetyTest):
|
2019-12-21 02:25:54 -07:00
|
|
|
|
2020-04-10 14:45:55 -06:00
|
|
|
TX_MSGS = [[0x283, 0], [0x2E6, 0], [0x2E7, 0], [0x33E, 0], [0x344, 0], [0x365, 0], [0x366, 0], [0x4CB, 0], # DSU bus 0
|
|
|
|
[0x128, 1], [0x141, 1], [0x160, 1], [0x161, 1], [0x470, 1], # DSU bus 1
|
|
|
|
[0x2E4, 0], [0x411, 0], [0x412, 0], [0x343, 0], [0x1D2, 0], # LKAS + ACC
|
|
|
|
[0x200, 0], [0x750, 0]] # interceptor + blindspot monitor
|
|
|
|
STANDSTILL_THRESHOLD = 1 # 1kph
|
|
|
|
RELAY_MALFUNCTION_ADDR = 0x2E4
|
|
|
|
RELAY_MALFUNCTION_BUS = 0
|
|
|
|
FWD_BLACKLISTED_ADDRS = {2: [0x2E4, 0x412, 0x191, 0x343]}
|
|
|
|
FWD_BUS_LOOKUP = {0: 2, 2: 0}
|
2020-04-27 21:35:36 -06:00
|
|
|
INTERCEPTOR_THRESHOLD = 845
|
2019-12-21 02:25:54 -07:00
|
|
|
|
2020-04-29 21:37:12 -06:00
|
|
|
MAX_RATE_UP = 10
|
|
|
|
MAX_RATE_DOWN = 25
|
|
|
|
MAX_TORQUE = 1500
|
|
|
|
MAX_RT_DELTA = 375
|
|
|
|
RT_INTERVAL = 250000
|
|
|
|
MAX_TORQUE_ERROR = 350
|
2020-05-31 15:07:01 -06:00
|
|
|
TORQUE_MEAS_TOLERANCE = 1 # toyota safety adds one to be conversative for rounding
|
2020-04-29 21:37:12 -06:00
|
|
|
|
2020-04-27 16:39:08 -06:00
|
|
|
def setUp(self):
|
|
|
|
self.packer = CANPackerPanda("toyota_prius_2017_pt_generated")
|
|
|
|
self.safety = libpandasafety_py.libpandasafety
|
|
|
|
self.safety.set_safety_hooks(Panda.SAFETY_TOYOTA, 66)
|
2020-04-28 11:33:20 -06:00
|
|
|
self.safety.init_tests()
|
2018-04-02 12:41:52 -06:00
|
|
|
|
|
|
|
def _torque_meas_msg(self, torque):
|
2020-04-10 14:45:55 -06:00
|
|
|
values = {"STEER_TORQUE_EPS": torque}
|
|
|
|
return self.packer.make_can_msg_panda("STEER_TORQUE_SENSOR", 0, values)
|
2018-04-02 12:41:52 -06:00
|
|
|
|
|
|
|
def _torque_msg(self, torque):
|
2020-04-10 14:45:55 -06:00
|
|
|
values = {"STEER_TORQUE_CMD": torque}
|
|
|
|
return self.packer.make_can_msg_panda("STEERING_LKA", 0, values)
|
2018-04-02 12:41:52 -06:00
|
|
|
|
|
|
|
def _accel_msg(self, accel):
|
2020-04-10 14:45:55 -06:00
|
|
|
values = {"ACCEL_CMD": accel}
|
|
|
|
return self.packer.make_can_msg_panda("ACC_CONTROL", 0, values)
|
2018-04-02 12:41:52 -06:00
|
|
|
|
2020-03-05 01:16:03 -07:00
|
|
|
def _speed_msg(self, s):
|
2020-06-01 02:49:26 -06:00
|
|
|
values = {("WHEEL_SPEED_%s" % n): s for n in ["FR", "FL", "RR", "RL"]}
|
2020-04-10 14:45:55 -06:00
|
|
|
return self.packer.make_can_msg_panda("WHEEL_SPEEDS", 0, values)
|
2019-06-05 14:38:59 -06:00
|
|
|
|
2020-04-10 14:45:55 -06:00
|
|
|
def _brake_msg(self, pressed):
|
|
|
|
values = {"BRAKE_PRESSED": pressed}
|
|
|
|
return self.packer.make_can_msg_panda("BRAKE_MODULE", 0, values)
|
2019-11-17 01:24:19 -07:00
|
|
|
|
2020-04-10 14:45:55 -06:00
|
|
|
def _gas_msg(self, pressed):
|
|
|
|
cruise_active = self.safety.get_controls_allowed()
|
|
|
|
values = {"GAS_RELEASED": not pressed, "CRUISE_ACTIVE": cruise_active}
|
|
|
|
return self.packer.make_can_msg_panda("PCM_CRUISE", 0, values)
|
2018-04-02 12:41:52 -06:00
|
|
|
|
2020-04-13 18:13:08 -06:00
|
|
|
def _pcm_status_msg(self, cruise_on):
|
2020-04-10 14:45:55 -06:00
|
|
|
values = {"CRUISE_ACTIVE": cruise_on}
|
|
|
|
return self.packer.make_can_msg_panda("PCM_CRUISE", 0, values)
|
2018-04-02 12:41:52 -06:00
|
|
|
|
2020-04-27 21:35:36 -06:00
|
|
|
# Toyota gas gains are the same
|
|
|
|
def _interceptor_msg(self, gas, addr):
|
|
|
|
to_send = make_msg(0, addr, 6)
|
|
|
|
to_send[0].RDLR = ((gas & 0xff) << 8) | ((gas & 0xff00) >> 8) | \
|
|
|
|
((gas & 0xff) << 24) | ((gas & 0xff00) << 8)
|
|
|
|
return to_send
|
2018-04-02 12:41:52 -06:00
|
|
|
|
|
|
|
def test_accel_actuation_limits(self):
|
2020-04-10 14:45:55 -06:00
|
|
|
limits = ((MIN_ACCEL, MAX_ACCEL, UNSAFE_MODE.DEFAULT),
|
|
|
|
(ISO_MIN_ACCEL, ISO_MAX_ACCEL, UNSAFE_MODE.RAISE_LONGITUDINAL_LIMITS_TO_ISO_MAX))
|
|
|
|
|
|
|
|
for min_accel, max_accel, unsafe_mode in limits:
|
|
|
|
for accel in np.arange(min_accel - 1, max_accel + 1, 0.1):
|
|
|
|
for controls_allowed in [True, False]:
|
|
|
|
self.safety.set_controls_allowed(controls_allowed)
|
|
|
|
self.safety.set_unsafe_mode(unsafe_mode)
|
|
|
|
if controls_allowed:
|
2020-06-01 02:49:26 -06:00
|
|
|
should_tx = int(min_accel * 1000) <= int(accel * 1000) <= int(max_accel * 1000)
|
2020-04-10 14:45:55 -06:00
|
|
|
else:
|
|
|
|
should_tx = np.isclose(accel, 0, atol=0.0001)
|
|
|
|
self.assertEqual(should_tx, self._tx(self._accel_msg(accel)))
|
2020-04-06 09:56:36 -06:00
|
|
|
|
2019-12-21 02:25:54 -07:00
|
|
|
def test_rx_hook(self):
|
|
|
|
# checksum checks
|
|
|
|
for msg in ["trq", "pcm"]:
|
|
|
|
self.safety.set_controls_allowed(1)
|
|
|
|
if msg == "trq":
|
|
|
|
to_push = self._torque_meas_msg(0)
|
|
|
|
if msg == "pcm":
|
2020-04-13 18:13:08 -06:00
|
|
|
to_push = self._pcm_status_msg(True)
|
2020-04-10 14:45:55 -06:00
|
|
|
self.assertTrue(self._rx(to_push))
|
2019-12-21 02:25:54 -07:00
|
|
|
to_push[0].RDHR = 0
|
2020-04-10 14:45:55 -06:00
|
|
|
self.assertFalse(self._rx(to_push))
|
2019-12-21 02:25:54 -07:00
|
|
|
self.assertFalse(self.safety.get_controls_allowed())
|
|
|
|
|
2019-06-11 20:24:08 -06:00
|
|
|
|
2018-04-02 12:41:52 -06:00
|
|
|
if __name__ == "__main__":
|
|
|
|
unittest.main()
|