2021-06-23 18:41:43 +08:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
import unittest
|
|
|
|
import numpy as np
|
|
|
|
from panda import Panda
|
|
|
|
import panda.tests.safety.common as common
|
2022-12-01 09:41:24 +08:00
|
|
|
from panda.tests.libpanda import libpanda_py
|
2021-06-23 18:41:43 +08:00
|
|
|
from panda.tests.safety.common import CANPackerPanda
|
|
|
|
|
2021-11-18 20:50:10 +08:00
|
|
|
MAX_ACCEL = 2.0
|
|
|
|
MIN_ACCEL = -3.5
|
|
|
|
|
2022-12-03 08:05:35 +08:00
|
|
|
|
2021-06-23 18:41:43 +08:00
|
|
|
class CONTROL_LEVER_STATE:
|
|
|
|
DN_1ST = 32
|
|
|
|
UP_1ST = 16
|
|
|
|
DN_2ND = 8
|
|
|
|
UP_2ND = 4
|
|
|
|
RWD = 2
|
|
|
|
FWD = 1
|
|
|
|
IDLE = 0
|
|
|
|
|
|
|
|
|
|
|
|
class TestTeslaSafety(common.PandaSafetyTest):
|
|
|
|
STANDSTILL_THRESHOLD = 0
|
|
|
|
GAS_PRESSED_THRESHOLD = 3
|
|
|
|
RELAY_MALFUNCTION_BUS = 0
|
|
|
|
FWD_BUS_LOOKUP = {0: 2, 2: 0}
|
|
|
|
|
|
|
|
def setUp(self):
|
2021-11-18 20:50:10 +08:00
|
|
|
self.packer = None
|
|
|
|
raise unittest.SkipTest
|
2021-06-23 18:41:43 +08:00
|
|
|
|
|
|
|
def _speed_msg(self, speed):
|
2021-11-18 20:50:10 +08:00
|
|
|
values = {"DI_vehicleSpeed": speed / 0.447}
|
|
|
|
return self.packer.make_can_msg_panda("DI_torque2", 0, values)
|
2021-06-23 18:41:43 +08:00
|
|
|
|
2022-03-29 04:13:27 +08:00
|
|
|
def _user_brake_msg(self, brake):
|
2021-06-23 18:41:43 +08:00
|
|
|
values = {"driverBrakeStatus": 2 if brake else 1}
|
|
|
|
return self.packer.make_can_msg_panda("BrakeMessage", 0, values)
|
|
|
|
|
2022-03-29 04:13:27 +08:00
|
|
|
def _user_gas_msg(self, gas):
|
2021-06-23 18:41:43 +08:00
|
|
|
values = {"DI_pedalPos": gas}
|
|
|
|
return self.packer.make_can_msg_panda("DI_torque1", 0, values)
|
|
|
|
|
|
|
|
def _control_lever_cmd(self, command):
|
|
|
|
values = {"SpdCtrlLvr_Stat": command}
|
|
|
|
return self.packer.make_can_msg_panda("STW_ACTN_RQ", 0, values)
|
|
|
|
|
2021-11-18 20:50:10 +08:00
|
|
|
def _pcm_status_msg(self, enable):
|
|
|
|
values = {"DI_cruiseState": 2 if enable else 0}
|
|
|
|
return self.packer.make_can_msg_panda("DI_state", 0, values)
|
|
|
|
|
2023-04-09 07:20:38 +08:00
|
|
|
def _long_control_msg(self, set_speed, acc_val=0, jerk_limits=(0, 0), accel_limits=(0, 0), aeb_event=0, bus=0):
|
2021-11-18 20:50:10 +08:00
|
|
|
values = {
|
|
|
|
"DAS_setSpeed": set_speed,
|
|
|
|
"DAS_accState": acc_val,
|
|
|
|
"DAS_aebEvent": aeb_event,
|
|
|
|
"DAS_jerkMin": jerk_limits[0],
|
|
|
|
"DAS_jerkMax": jerk_limits[1],
|
|
|
|
"DAS_accelMin": accel_limits[0],
|
|
|
|
"DAS_accelMax": accel_limits[1],
|
|
|
|
}
|
2023-04-09 07:20:38 +08:00
|
|
|
return self.packer.make_can_msg_panda("DAS_control", bus, values)
|
2021-11-18 20:50:10 +08:00
|
|
|
|
2022-12-03 08:05:35 +08:00
|
|
|
|
|
|
|
class TestTeslaSteeringSafety(TestTeslaSafety, common.AngleSteeringSafetyTest):
|
2021-11-18 20:50:10 +08:00
|
|
|
TX_MSGS = [[0x488, 0], [0x45, 0], [0x45, 2]]
|
|
|
|
RELAY_MALFUNCTION_ADDR = 0x488
|
|
|
|
FWD_BLACKLISTED_ADDRS = {2: [0x488]}
|
|
|
|
|
2022-12-03 08:45:23 +08:00
|
|
|
# Angle control limits
|
2022-12-03 08:05:35 +08:00
|
|
|
DEG_TO_CAN = 10
|
|
|
|
|
2023-05-03 08:34:24 +08:00
|
|
|
ANGLE_RATE_BP = [0., 5., 15.]
|
2023-06-02 05:10:36 +08:00
|
|
|
ANGLE_RATE_UP = [10., 1.6, .3] # windup limit
|
|
|
|
ANGLE_RATE_DOWN = [10., 7.0, .8] # unwind limit
|
2022-12-03 08:05:35 +08:00
|
|
|
|
2021-11-18 20:50:10 +08:00
|
|
|
def setUp(self):
|
|
|
|
self.packer = CANPackerPanda("tesla_can")
|
2022-12-01 09:41:24 +08:00
|
|
|
self.safety = libpanda_py.libpanda
|
2021-11-18 20:50:10 +08:00
|
|
|
self.safety.set_safety_hooks(Panda.SAFETY_TESLA, 0)
|
|
|
|
self.safety.init_tests()
|
2021-06-23 18:41:43 +08:00
|
|
|
|
2022-12-03 08:45:23 +08:00
|
|
|
def _angle_cmd_msg(self, angle: float, enabled: bool):
|
2022-12-03 08:05:35 +08:00
|
|
|
values = {"DAS_steeringAngleRequest": angle, "DAS_steeringControlType": 1 if enabled else 0}
|
|
|
|
return self.packer.make_can_msg_panda("DAS_steeringControl", 0, values)
|
2021-06-23 18:41:43 +08:00
|
|
|
|
2022-12-03 08:45:23 +08:00
|
|
|
def _angle_meas_msg(self, angle: float):
|
|
|
|
values = {"EPAS_internalSAS": angle}
|
|
|
|
return self.packer.make_can_msg_panda("EPAS_sysStatus", 0, values)
|
|
|
|
|
2021-06-23 18:41:43 +08:00
|
|
|
def test_acc_buttons(self):
|
2022-03-19 05:52:52 +08:00
|
|
|
"""
|
2022-03-21 13:00:19 +08:00
|
|
|
FWD (cancel) always allowed.
|
2022-03-19 05:52:52 +08:00
|
|
|
"""
|
|
|
|
btns = [
|
|
|
|
(CONTROL_LEVER_STATE.FWD, True),
|
|
|
|
(CONTROL_LEVER_STATE.RWD, False),
|
|
|
|
(CONTROL_LEVER_STATE.UP_1ST, False),
|
|
|
|
(CONTROL_LEVER_STATE.UP_2ND, False),
|
|
|
|
(CONTROL_LEVER_STATE.DN_1ST, False),
|
|
|
|
(CONTROL_LEVER_STATE.DN_2ND, False),
|
2022-03-21 13:00:19 +08:00
|
|
|
(CONTROL_LEVER_STATE.IDLE, False),
|
2022-03-19 05:52:52 +08:00
|
|
|
]
|
|
|
|
for btn, should_tx in btns:
|
|
|
|
for controls_allowed in (True, False):
|
|
|
|
self.safety.set_controls_allowed(controls_allowed)
|
|
|
|
tx = self._tx(self._control_lever_cmd(btn))
|
|
|
|
self.assertEqual(tx, should_tx)
|
|
|
|
|
2021-06-23 18:41:43 +08:00
|
|
|
|
2021-11-18 20:50:10 +08:00
|
|
|
class TestTeslaLongitudinalSafety(TestTeslaSafety):
|
|
|
|
def setUp(self):
|
|
|
|
raise unittest.SkipTest
|
|
|
|
|
|
|
|
def test_no_aeb(self):
|
|
|
|
for aeb_event in range(4):
|
|
|
|
self.assertEqual(self._tx(self._long_control_msg(10, aeb_event=aeb_event)), aeb_event == 0)
|
|
|
|
|
2022-05-16 19:12:14 +08:00
|
|
|
def test_stock_aeb_passthrough(self):
|
|
|
|
no_aeb_msg = self._long_control_msg(10, aeb_event=0)
|
2023-04-09 07:20:38 +08:00
|
|
|
no_aeb_msg_cam = self._long_control_msg(10, aeb_event=0, bus=2)
|
|
|
|
aeb_msg_cam = self._long_control_msg(10, aeb_event=1, bus=2)
|
2022-05-16 19:12:14 +08:00
|
|
|
|
|
|
|
# stock system sends no AEB -> no forwarding, and OP is allowed to TX
|
2023-04-09 07:20:38 +08:00
|
|
|
self.assertEqual(1, self._rx(no_aeb_msg_cam))
|
2023-04-09 07:45:59 +08:00
|
|
|
self.assertEqual(-1, self.safety.safety_fwd_hook(2, no_aeb_msg_cam.addr))
|
2022-05-16 19:12:14 +08:00
|
|
|
self.assertEqual(True, self._tx(no_aeb_msg))
|
|
|
|
|
|
|
|
# stock system sends AEB -> forwarding, and OP is not allowed to TX
|
2023-04-09 07:20:38 +08:00
|
|
|
self.assertEqual(1, self._rx(aeb_msg_cam))
|
2023-04-09 07:45:59 +08:00
|
|
|
self.assertEqual(0, self.safety.safety_fwd_hook(2, aeb_msg_cam.addr))
|
2022-05-16 19:12:14 +08:00
|
|
|
self.assertEqual(False, self._tx(no_aeb_msg))
|
|
|
|
|
2021-11-18 20:50:10 +08:00
|
|
|
def test_acc_accel_limits(self):
|
2022-03-29 04:03:07 +08:00
|
|
|
for controls_allowed in [True, False]:
|
|
|
|
self.safety.set_controls_allowed(controls_allowed)
|
|
|
|
for min_accel in np.arange(MIN_ACCEL - 1, MAX_ACCEL + 1, 0.1):
|
|
|
|
for max_accel in np.arange(MIN_ACCEL - 1, MAX_ACCEL + 1, 0.1):
|
|
|
|
# floats might not hit exact boundary conditions without rounding
|
|
|
|
min_accel = round(min_accel, 2)
|
|
|
|
max_accel = round(max_accel, 2)
|
|
|
|
if controls_allowed:
|
|
|
|
send = (MIN_ACCEL <= min_accel <= MAX_ACCEL) and (MIN_ACCEL <= max_accel <= MAX_ACCEL)
|
|
|
|
else:
|
|
|
|
send = np.all(np.isclose([min_accel, max_accel], 0, atol=0.0001))
|
|
|
|
self.assertEqual(send, self._tx(self._long_control_msg(10, acc_val=4, accel_limits=[min_accel, max_accel])))
|
2021-11-18 20:50:10 +08:00
|
|
|
|
2022-12-03 08:05:35 +08:00
|
|
|
|
2021-11-18 20:50:10 +08:00
|
|
|
class TestTeslaChassisLongitudinalSafety(TestTeslaLongitudinalSafety):
|
|
|
|
TX_MSGS = [[0x488, 0], [0x45, 0], [0x45, 2], [0x2B9, 0]]
|
|
|
|
RELAY_MALFUNCTION_ADDR = 0x488
|
|
|
|
FWD_BLACKLISTED_ADDRS = {2: [0x2B9, 0x488]}
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.packer = CANPackerPanda("tesla_can")
|
2022-12-01 09:41:24 +08:00
|
|
|
self.safety = libpanda_py.libpanda
|
2021-11-18 20:50:10 +08:00
|
|
|
self.safety.set_safety_hooks(Panda.SAFETY_TESLA, Panda.FLAG_TESLA_LONG_CONTROL)
|
|
|
|
self.safety.init_tests()
|
|
|
|
|
2022-12-03 08:05:35 +08:00
|
|
|
|
2021-11-18 20:50:10 +08:00
|
|
|
class TestTeslaPTLongitudinalSafety(TestTeslaLongitudinalSafety):
|
|
|
|
TX_MSGS = [[0x2BF, 0]]
|
|
|
|
RELAY_MALFUNCTION_ADDR = 0x2BF
|
|
|
|
FWD_BLACKLISTED_ADDRS = {2: [0x2BF]}
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.packer = CANPackerPanda("tesla_powertrain")
|
2022-12-01 09:41:24 +08:00
|
|
|
self.safety = libpanda_py.libpanda
|
2021-11-18 20:50:10 +08:00
|
|
|
self.safety.set_safety_hooks(Panda.SAFETY_TESLA, Panda.FLAG_TESLA_LONG_CONTROL | Panda.FLAG_TESLA_POWERTRAIN)
|
|
|
|
self.safety.init_tests()
|
2021-06-23 18:41:43 +08:00
|
|
|
|
2022-12-03 08:05:35 +08:00
|
|
|
|
2021-06-23 18:41:43 +08:00
|
|
|
if __name__ == "__main__":
|
|
|
|
unittest.main()
|