VW MQB: Cleanup message counter handling (#1034)

This commit is contained in:
Jason Young
2022-08-15 23:49:06 -04:00
committed by GitHub
parent ba90f6a8c9
commit 4412eeaf1a

View File

@@ -3,7 +3,7 @@ import unittest
from panda import Panda
from panda.tests.safety import libpandasafety_py
import panda.tests.safety.common as common
from panda.tests.safety.common import CANPackerPanda, MAX_WRONG_COUNTERS
from panda.tests.safety.common import CANPackerPanda
MSG_ESP_19 = 0xB2 # RX from ABS, for wheel speeds
MSG_LH_EPS_03 = 0x9F # RX from EPS, for driver steering torque
@@ -16,13 +16,6 @@ MSG_LDW_02 = 0x397 # TX by OP, Lane line recognition and text alerts
class TestVolkswagenMqbSafety(common.PandaSafetyTest, common.DriverTorqueSteeringSafetyTest):
cnt_lh_eps_03 = 0
cnt_esp_05 = 0
cnt_tsk_06 = 0
cnt_motor_20 = 0
cnt_hca_01 = 0
cnt_gra_acc_01 = 0
STANDSTILL_THRESHOLD = 1
RELAY_MALFUNCTION_ADDR = MSG_HCA_01
RELAY_MALFUNCTION_BUS = 0
@@ -50,41 +43,32 @@ class TestVolkswagenMqbSafety(common.PandaSafetyTest, common.DriverTorqueSteerin
# Brake light switch _esp_05_msg
def _user_brake_msg(self, brake):
values = {"ESP_Fahrer_bremst": brake, "COUNTER": self.cnt_esp_05 % 16}
self.__class__.cnt_esp_05 += 1
values = {"ESP_Fahrer_bremst": brake}
return self.packer.make_can_msg_panda("ESP_05", 0, values)
# Driver throttle input
def _user_gas_msg(self, gas):
values = {"MO_Fahrpedalrohwert_01": gas, "COUNTER": self.cnt_motor_20 % 16}
self.__class__.cnt_motor_20 += 1
values = {"MO_Fahrpedalrohwert_01": gas}
return self.packer.make_can_msg_panda("Motor_20", 0, values)
# ACC engagement status
def _pcm_status_msg(self, enable):
values = {"TSK_Status": 3 if enable else 1, "COUNTER": self.cnt_tsk_06 % 16}
self.__class__.cnt_tsk_06 += 1
values = {"TSK_Status": 3 if enable else 1}
return self.packer.make_can_msg_panda("TSK_06", 0, values)
# Driver steering input torque
def _torque_driver_msg(self, torque):
values = {"EPS_Lenkmoment": abs(torque), "EPS_VZ_Lenkmoment": torque < 0,
"COUNTER": self.cnt_lh_eps_03 % 16}
self.__class__.cnt_lh_eps_03 += 1
values = {"EPS_Lenkmoment": abs(torque), "EPS_VZ_Lenkmoment": torque < 0}
return self.packer.make_can_msg_panda("LH_EPS_03", 0, values)
# openpilot steering output torque
def _torque_cmd_msg(self, torque, steer_req=1):
values = {"Assist_Torque": abs(torque), "Assist_VZ": torque < 0,
"COUNTER": self.cnt_hca_01 % 16}
self.__class__.cnt_hca_01 += 1
values = {"Assist_Torque": abs(torque), "Assist_VZ": torque < 0}
return self.packer.make_can_msg_panda("HCA_01", 0, values)
# Cruise control buttons
def _gra_acc_01_msg(self, cancel=0, resume=0, _set=0):
values = {"GRA_Abbrechen": cancel, "GRA_Tip_Setzen": _set,
"GRA_Tip_Wiederaufnahme": resume, "COUNTER": self.cnt_gra_acc_01 % 16}
self.__class__.cnt_gra_acc_01 += 1
values = {"GRA_Abbrechen": cancel, "GRA_Tip_Setzen": _set, "GRA_Tip_Wiederaufnahme": resume}
return self.packer.make_can_msg_panda("GRA_ACC_01", 0, values)
def test_torque_measurements(self):
@@ -107,55 +91,6 @@ class TestVolkswagenMqbSafety(common.PandaSafetyTest, common.DriverTorqueSteerin
self.assertEqual(0, self.safety.get_torque_driver_max())
self.assertEqual(0, self.safety.get_torque_driver_min())
def test_rx_hook(self):
# checksum checks
# TODO: Would be ideal to check ESP_19 as well, but it has no checksum
# or counter, and I'm not sure if we can easily validate Panda's simple
# temporal reception-rate check here.
for msg in [MSG_LH_EPS_03, MSG_ESP_05, MSG_TSK_06, MSG_MOTOR_20]:
self.safety.set_controls_allowed(1)
if msg == MSG_LH_EPS_03:
to_push = self._torque_driver_msg(0)
if msg == MSG_ESP_05:
to_push = self._user_brake_msg(False)
if msg == MSG_TSK_06:
to_push = self._pcm_status_msg(True)
if msg == MSG_MOTOR_20:
to_push = self._user_gas_msg(0)
self.assertTrue(self._rx(to_push))
to_push[0].data[4] ^= 0xFF
self.assertFalse(self._rx(to_push))
self.assertFalse(self.safety.get_controls_allowed())
# counter
# reset wrong_counters to zero by sending valid messages
for i in range(MAX_WRONG_COUNTERS + 1):
self.__class__.cnt_lh_eps_03 += 1
self.__class__.cnt_esp_05 += 1
self.__class__.cnt_tsk_06 += 1
self.__class__.cnt_motor_20 += 1
if i < MAX_WRONG_COUNTERS:
self.safety.set_controls_allowed(1)
self._rx(self._torque_driver_msg(0))
self._rx(self._user_brake_msg(False))
self._rx(self._pcm_status_msg(True))
self._rx(self._user_gas_msg(0))
else:
self.assertFalse(self._rx(self._torque_driver_msg(0)))
self.assertFalse(self._rx(self._user_brake_msg(False)))
self.assertFalse(self._rx(self._pcm_status_msg(True)))
self.assertFalse(self._rx(self._user_gas_msg(0)))
self.assertFalse(self.safety.get_controls_allowed())
# restore counters for future tests with a couple of good messages
for i in range(2):
self.safety.set_controls_allowed(1)
self._rx(self._torque_driver_msg(0))
self._rx(self._user_brake_msg(False))
self._rx(self._pcm_status_msg(True))
self._rx(self._user_gas_msg(0))
self.assertTrue(self.safety.get_controls_allowed())
class TestVolkswagenMqbStockSafety(TestVolkswagenMqbSafety):
TX_MSGS = [[MSG_HCA_01, 0], [MSG_LDW_02, 0], [MSG_GRA_ACC_01, 0], [MSG_GRA_ACC_01, 2]]