diff --git a/opendbc_repo/opendbc/safety/modes/leapmotor.h b/opendbc_repo/opendbc/safety/modes/leapmotor.h index 7ee3139939..97b993b438 100644 --- a/opendbc_repo/opendbc/safety/modes/leapmotor.h +++ b/opendbc_repo/opendbc/safety/modes/leapmotor.h @@ -19,6 +19,12 @@ // negated to match openpilot's convention (positive = left) #define LEAPMOTOR_ANGLE_CENTER 32490 +// ADAS_LKA.LCC_RAMP (bytes 16-17, little-endian) sets how much effort the EPS may put into following the angle: idle +// 0x7801, 254 lower per step deeper. Stock LCC goes to 46 steps; openpilot's low-speed boost to at most 56 (0x4071), the +// deepest value whose top 4 bits stay 4. Must match leapmotorcan.LCC_RAMP_IDLE and LCC_RAMP_STEPS_BOOST_MAX. +#define LEAPMOTOR_LCC_RAMP_IDLE 0x7801 +#define LEAPMOTOR_LCC_RAMP_DEEPEST 0x4071 + // ADAS_LKA.ACC_ACCEL_REQ: byte 4, 0.05 m/s^2 per bit, raw 100 = 0 m/s^2. Must match CarControllerParams. #define LEAPMOTOR_ACCEL_INACTIVE 100 // The camera's request at or below this (-2.0 m/s^2) is passed on: openpilot may ask for more braking, not less @@ -162,6 +168,13 @@ static bool leapmotor_tx_hook(const CANPacket_t *msg) { tx = false; } + // while openpilot steers, the EPS effort may go no deeper than the boost's maximum. Otherwise the field carries the + // camera's own value, which is not a ramp depth (LCC state bits), and is passed on. + int lcc_ramp = (int)GET_BYTES_LE(msg, 16, 2); // LCC_RAMP + if (lka_active && ((lcc_ramp < LEAPMOTOR_LCC_RAMP_DEEPEST) || (lcc_ramp > LEAPMOTOR_LCC_RAMP_IDLE))) { + tx = false; + } + if (leapmotor_longitudinal) { int accel = msg->data[4]; // ACC_ACCEL_REQ // The camera's own request may always be passed on: that is what the stock ACC sends when openpilot is not in diff --git a/opendbc_repo/opendbc/safety/tests/test_leapmotor.py b/opendbc_repo/opendbc/safety/tests/test_leapmotor.py index 17d5c79395..f874113c1a 100644 --- a/opendbc_repo/opendbc/safety/tests/test_leapmotor.py +++ b/opendbc_repo/opendbc/safety/tests/test_leapmotor.py @@ -4,6 +4,7 @@ import unittest import numpy as np from opendbc.car.lateral import get_max_angle_delta_vm, get_max_angle_vm +from opendbc.car.leapmotor import leapmotorcan from opendbc.car.leapmotor.carcontroller import get_safety_CP from opendbc.car.leapmotor.values import CarControllerParams, LeapmotorSafetyFlags from opendbc.car.structs import CarParams @@ -14,6 +15,9 @@ from opendbc.safety.tests.common import CANPackerSafety, away_round, round_speed ADAS_LKA = 0x83 POWERTRAIN_STATE = 0xB7 +# ADAS_LKA.LCC_RAMP bounds while steering, as in leapmotor.h +LCC_RAMP_IDLE = 0x7801 +LCC_RAMP_DEEPEST = 0x4071 def round_angle(apply_angle, can_offset=0): @@ -148,7 +152,7 @@ class TestLeapmotorSafety(common.CarSafetyTest, common.AngleSteeringSafetyTest): self.safety.init_tests() def _angle_cmd_msg(self, angle: float, enabled: bool, increment_timer: bool = True): - values = {"STEER_ANGLE_REQ": angle, "LCC_STEERING_ACTIVE_COPY": enabled, "ACC_ACCEL_REQ": 0} + values = {"STEER_ANGLE_REQ": angle, "LCC_STEERING_ACTIVE_COPY": enabled, "ACC_ACCEL_REQ": 0, "LCC_RAMP": LCC_RAMP_IDLE} if increment_timer: self.safety.set_timer(self.cnt_angle_cmd * int(1e6 / self.LATERAL_FREQUENCY)) self.__class__.cnt_angle_cmd += 1 @@ -256,6 +260,22 @@ class TestLeapmotorSafety(common.CarSafetyTest, common.AngleSteeringSafetyTest): bad[0] ^= 0x01 self.assertFalse(self._rx(libsafety_py.make_CANPacket(addr, self.MAIN_BUS, bytes(bad))), hex(addr)) + def test_lcc_ramp_limit(self): + # while steering, the EPS effort word must be between idle and the deepest boost value; otherwise it is free + self.safety.set_controls_allowed(True) + self._reset_speed_measurement(10) + for lka_active in (True, False): + for ramp in (LCC_RAMP_IDLE + 1, LCC_RAMP_IDLE, LCC_RAMP_DEEPEST, LCC_RAMP_DEEPEST - 1, 0, 0xFFFF): + values = {"STEER_ANGLE_REQ": 0, "LCC_STEERING_ACTIVE_COPY": lka_active, "ACC_ACCEL_REQ": 0, "LCC_RAMP": ramp} + self.safety.set_desired_angle_last(0) + msg = self.packer.make_can_msg_safety("ADAS_LKA", self.MAIN_BUS, values) + allowed = not lka_active or LCC_RAMP_DEEPEST <= ramp <= LCC_RAMP_IDLE + self.assertEqual(allowed, self._tx(msg), (lka_active, hex(ramp))) + + def test_lcc_ramp_limit_matches_car_code(self): + self.assertEqual(LCC_RAMP_IDLE, leapmotorcan.LCC_RAMP_IDLE) + self.assertEqual(LCC_RAMP_DEEPEST, leapmotorcan.LCC_RAMP_IDLE - leapmotorcan.LCC_RAMP_STEP * leapmotorcan.LCC_RAMP_STEPS_BOOST_MAX) + def test_accel_request_unchecked_without_longitudinal(self): # only with the longitudinal flag is the acceleration request checked: an unknown hard request is then blocked self.safety.set_controls_allowed(False)