diff --git a/opendbc_repo/opendbc/car/dashboard_speed_limit.py b/opendbc_repo/opendbc/car/dashboard_speed_limit.py index 84b91a8b5d..8dbf0ed3aa 100644 --- a/opendbc_repo/opendbc/car/dashboard_speed_limit.py +++ b/opendbc_repo/opendbc/car/dashboard_speed_limit.py @@ -83,6 +83,14 @@ def volvo_sign(speed_kph: float) -> tuple[Status, float]: return (Status.VALID, speed_kph / 3.6) if speed_kph > 0 else (Status.ABSENT, 0.0) +def leapmotor_sign(raw: int) -> tuple[Status, float]: + # ROAD_SIGNS.SPEED_LIMIT is in km/h: 20-130 in tens on the B10's drives. 0 and 255 only last single frames, and + # no code for "no sign" has been seen, so neither is evidence of absence. + if 1 <= raw <= 250: + return Status.VALID, raw / 3.6 + return Status.UNKNOWN, 0.0 + + def hyundai_canfd_sign(speed: int, system_status: int, is_metric: bool) -> tuple[Status, float]: # FR_CMR_02_100ms defines 0 as no recognition, 253 as unlimited, and # 254/255 as reserved/invalid. A failed ISLW system is not evidence of absence. diff --git a/opendbc_repo/opendbc/car/leapmotor/carstate.py b/opendbc_repo/opendbc/car/leapmotor/carstate.py index 554af0e732..e0907ad689 100644 --- a/opendbc_repo/opendbc/car/leapmotor/carstate.py +++ b/opendbc_repo/opendbc/car/leapmotor/carstate.py @@ -2,6 +2,7 @@ from opendbc.car import DT_CTRL, Bus, structs from opendbc.can.parser import CANParser from opendbc.car.common.conversions import Conversions as CV from opendbc.car.common.filter_simple import FirstOrderFilter +from opendbc.car.dashboard_speed_limit import Tracker as LimitTracker, leapmotor_sign, parser_expiry from opendbc.car.interfaces import CarStateBase from opendbc.car.leapmotor.values import DBC, CarControllerParams @@ -20,6 +21,7 @@ class CarState(CarStateBase): # the EPS's own effort as it shows up in the driver-torque reading, which lags it (see EPS_TORQUE_LAG) self.eps_torque_lagged = FirstOrderFilter(0., CarControllerParams.EPS_TORQUE_LAG, DT_CTRL) self.powertrain_state: dict | None = None + self.dashboard_limit = LimitTracker() def update(self, can_parsers) -> structs.CarState: cp = can_parsers[Bus.pt] @@ -94,6 +96,11 @@ class CarState(CarStateBase): ret.cruiseState.standstill = ret.cruiseState.enabled and ret.standstill # the camera's forward collision warning, shown by openpilot too ret.stockFcw = bool(cp_cam.vl['ROAD_SIGNS']['FCW']) + # the camera's posted speed limit, as the cluster shows it; offered to the speed limit controller as the dashboard + timestamp, expiry = parser_expiry(cp_cam, 'ROAD_SIGNS', 'SPEED_LIMIT') + if timestamp > self.dashboard_limit.observation.observed_ns: + status, value_mps = leapmotor_sign(int(cp_cam.vl['ROAD_SIGNS']['SPEED_LIMIT'])) + self.dashboard_limit.update(timestamp, status, value_mps, valid_until_ns=expiry) # latest camera frame, rebuilt by the controller; None until the camera's 0x83 has been received self.adas_lka = dict(cp_cam.vl['ADAS_LKA']) if cp_cam.ts_nanos['ADAS_LKA']['COUNTER'] > 0 else None diff --git a/opendbc_repo/opendbc/car/leapmotor/tests/test_leapmotor.py b/opendbc_repo/opendbc/car/leapmotor/tests/test_leapmotor.py index be0c16c8e7..ae9cd9b3ca 100644 --- a/opendbc_repo/opendbc/car/leapmotor/tests/test_leapmotor.py +++ b/opendbc_repo/opendbc/car/leapmotor/tests/test_leapmotor.py @@ -314,5 +314,37 @@ class TestLeapmotorCan(unittest.TestCase): self.assertLess(abs(ret.steeringTorqueEps), 100) self.assertFalse(CS.driver_override) + def test_dashboard_speed_limit(self): + # the camera's posted limit is offered as the dashboard source, and expires with the camera's frames + from opendbc.car.dashboard_speed_limit import Status + from opendbc.car.leapmotor.carstate import CarState + from opendbc.car.leapmotor.interface import CarInterface + CP = CarInterface.get_non_essential_params("LEAPMOTOR_B10") + CS = CarState(CP) + parsers = CS.get_can_parsers(CP) + packer = CANPacker("leapmotor_b10") + + def frame(i, limit): + t = int((i + 1) * 2e7) + parsers[Bus.pt].update([(t, [])]) + parsers[Bus.cam].update([(t, [packer.make_can_msg("ROAD_SIGNS", 2, {"SPEED_LIMIT": limit, "COUNTER": i % 16})])]) + CS.update(parsers) + return CS.dashboard_limit.observation, t + + for i in range(60): # the parser learns the camera's frame rate, and so the expiry, after 1 s + observation, t = frame(i, 50) + self.assertEqual(observation.status, Status.VALID) + self.assertAlmostEqual(observation.speed_mps, 50 / 3.6) + self.assertEqual(observation.observed_ns, t) + self.assertGreater(observation.valid_until_ns, t) + episode = observation.episode + observation, _ = frame(60, 30) + self.assertAlmostEqual(observation.speed_mps, 30 / 3.6) + self.assertGreater(observation.episode, episode) + for i, limit in enumerate((0, 255), start=61): + observation, _ = frame(i, limit) + self.assertEqual(observation.status, Status.UNKNOWN) + + if __name__ == "__main__": unittest.main()