leapmotor: offer the camera's speed limit to the speed limit controller

ROAD_SIGNS.SPEED_LIMIT, the limit the cluster shows, becomes the dashboard
source (DASH), expiring with the camera's frames. On route 00000010 it
followed the road signs, while the vision source held a misread 25 km/h
for 26 minutes.
This commit is contained in:
ryan
2026-11-16 00:33:53 +01:00
parent 25da2438a3
commit fe89adeca1
3 changed files with 47 additions and 0 deletions
@@ -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.
@@ -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
@@ -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()