diff --git a/opendbc_repo/opendbc/car/leapmotor/carcontroller.py b/opendbc_repo/opendbc/car/leapmotor/carcontroller.py index 5183b19801..ed9a552a22 100644 --- a/opendbc_repo/opendbc/car/leapmotor/carcontroller.py +++ b/opendbc_repo/opendbc/car/leapmotor/carcontroller.py @@ -64,7 +64,11 @@ class CarController(CarControllerBase): actuators = CC.actuators can_sends = [] # like the stock LCC, let go while the driver steers (the EPS flags it), then ramp back in - lat_active = CC.latActive and not CS.driver_override + # (in a held bend that the wheel follows, with the bend rule: see CarControllerParams.OVERRIDE_BEND_MIN_ANGLE) + in_bend = (self.lat_active_last and CS.eps_pushes_into_bend and + abs(CS.out.steeringAngleDeg) >= CarControllerParams.OVERRIDE_BEND_MIN_ANGLE and + abs(self.apply_angle_last - CS.out.steeringAngleDeg) < CarControllerParams.OVERRIDE_BEND_TRACKING_ERROR) + lat_active = CC.latActive and not (CS.driver_override_in_bend if in_bend else CS.driver_override) # take the wheel back only once it is near the angle openpilot wants: after the driver let go mid-turn at walking # pace with the wheel at +308 deg and the model at +24, the EPS swung it to -203 deg in 3 s if lat_active and not self.lat_active_last: diff --git a/opendbc_repo/opendbc/car/leapmotor/carstate.py b/opendbc_repo/opendbc/car/leapmotor/carstate.py index e0907ad689..14cb27b3dc 100644 --- a/opendbc_repo/opendbc/car/leapmotor/carstate.py +++ b/opendbc_repo/opendbc/car/leapmotor/carstate.py @@ -1,3 +1,4 @@ +import numpy as np from opendbc.car import DT_CTRL, Bus, structs from opendbc.can.parser import CANParser from opendbc.car.common.conversions import Conversions as CV @@ -18,6 +19,11 @@ class CarState(CarStateBase): self.lka_received_seen = False self.driver_override_frames = 0 self.driver_override = False + # the override in a held bend, used by the controller while the wheel follows it (see OVERRIDE_BEND_MIN_ANGLE) + self.bend_pressed_cnt = 0 + self.bend_override_frames = 0 + self.driver_override_in_bend = False + self.eps_pushes_into_bend = False # 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 @@ -51,8 +57,9 @@ class CarState(CarStateBase): self.steering_angle_last = angle ret.steeringTorqueEps = cp.vl['EPS_STEERING']['STEER_TORQUE_EPS'] # the driver's torque: the reading minus the share that follows the EPS's own effort - ret.steeringTorque = cp.vl['EPS_STEERING']['STEER_TORQUE_DRIVER'] + \ - self.eps_torque_lagged.update(ret.steeringTorqueEps) / CarControllerParams.EPS_TORQUE_PER_DRIVER_TORQUE + driver_torque_raw = cp.vl['EPS_STEERING']['STEER_TORQUE_DRIVER'] + eps_share = self.eps_torque_lagged.update(ret.steeringTorqueEps) / CarControllerParams.EPS_TORQUE_PER_DRIVER_TORQUE + ret.steeringTorque = driver_torque_raw + eps_share # the torque reading is coarse: during an LCC override it only reached 2-3, but the EPS flagged the override itself. # The EPS also sets that flag whenever the car is above about 121 km/h, with or without the driver (99% of frames # above 121 km/h on a stock drive, and openpilot's own drive at 125 km/h), so above 118 km/h only the torque counts. @@ -65,6 +72,17 @@ class CarState(CarStateBase): # every 0.7 s (41 times in 14 min). The flag alone is not enough: above 118 km/h it is always set, and at walking # pace it stayed off while openpilot pulled the wheel from +324 to -203 deg through the driver's hands. self.driver_override = self.driver_override_frames >= 2 or torque_pressed + # In a held bend the share estimate overshoots, so there it only removes torque, and the flag needs torque against + # the EPS as well (see CarControllerParams.OVERRIDE_BEND_MIN_ANGLE); the controller picks this while the wheel follows + bend_torque = ret.steeringTorque if ret.steeringTorque * eps_share <= 0 else \ + (driver_torque_raw if driver_torque_raw * eps_share > 0 else 0.) + self.bend_pressed_cnt = int(np.clip(self.bend_pressed_cnt + (1 if abs(bend_torque) > CarControllerParams.STEER_DRIVER_ALLOWANCE else -1), + 0, 11)) + opposing = bend_torque * eps_share < 0 and abs(bend_torque) >= CarControllerParams.OVERRIDE_BEND_OPPOSING_TORQUE + self.bend_override_frames = self.bend_override_frames + 1 if eps_override and opposing else 0 + self.driver_override_in_bend = self.bend_override_frames >= 2 or self.bend_pressed_cnt > 5 + # the EPS's share and the angle have opposite signs while it holds the wheel into the turn + self.eps_pushes_into_bend = eps_share * ret.steeringAngleDeg < 0 # The EPS reports whether it receives ADAS_LKA (0x83): losing it after it was received means our steering frame is # missing or rejected (camera-unplug test). Not set before the camera's first frame after ignition. lka_received = bool(cp.vl['EPS_STEERING']['ADAS_LKA_RECEIVED']) diff --git a/opendbc_repo/opendbc/car/leapmotor/tests/test_leapmotor.py b/opendbc_repo/opendbc/car/leapmotor/tests/test_leapmotor.py index ae9cd9b3ca..d0dc31c72e 100644 --- a/opendbc_repo/opendbc/car/leapmotor/tests/test_leapmotor.py +++ b/opendbc_repo/opendbc/car/leapmotor/tests/test_leapmotor.py @@ -226,6 +226,61 @@ class TestLeapmotorCan(unittest.TestCase): eps = [-12, -7, -7, -6, -8, -14, -28, -47, -57, -56] + [-57] * 20 self.assertTrue(any(pressed(driver, eps))) + def test_override_in_a_held_bend(self): + # route 0000000a 12:31:42.9, left bend at 33 km/h, the driver's hand resting on the wheel: the EPS holds about 130 into + # the turn, the reading follows less of it than assumed and the EPS sets its own flag. The previous rule let go and + # the wheel sprang back 14 deg; the bend rule keeps steering. A driver turning against the EPS still counts. + from opendbc.car.leapmotor.carstate import CarState + from opendbc.car.leapmotor.interface import CarInterface + + def run(frames, angle=-41.): + CP = CarInterface.get_non_essential_params("LEAPMOTOR_B10") + CS = CarState(CP) + parsers = CS.get_can_parsers(CP) + packer = CANPacker("leapmotor_b10") + for i, (driver, eps, flag) in enumerate(frames): + dat = packer.make_can_msg("EPS_STEERING", 0, {"STEER_TORQUE_DRIVER": driver, "STEER_TORQUE_EPS": eps, "DRIVER_OVERRIDE": flag, + "STEER_ANGLE": angle, "COUNTER": i % 16})[1] + parsers[Bus.pt].update([(int((i + 1) * 1e7), [(0x18D, dat, 0)])]) + parsers[Bus.cam].update([(int((i + 1) * 1e7), [])]) + CS.update(parsers) + return CS + + hands_off = [(-4, 120, 0)] * 40 + [(-5, 133, 0)] * 3 + [(-3, 133, 1)] * 3 + CS = run(hands_off) + self.assertTrue(CS.driver_override) + self.assertFalse(CS.driver_override_in_bend) + self.assertTrue(CS.eps_pushes_into_bend) + self.assertFalse(run(hands_off, angle=41.).eps_pushes_into_bend) # pushing out of the turn: the previous rule + # the driver straightens the wheel against the EPS: the reading goes well past the EPS's share + CS = run([(-4, 120, 0)] * 40 + [(-16, 133, 1)] * 8) + self.assertTrue(CS.driver_override) + self.assertTrue(CS.driver_override_in_bend) + + def test_bend_rule_only_while_the_wheel_follows(self): + from types import SimpleNamespace + from opendbc.car import structs as car_structs + from opendbc.car.leapmotor.carcontroller import CarController as Controller, get_safety_CP as safety_CP + from opendbc.car.leapmotor.values import DBC as CAR_DBC + + def steering(wheel, into_bend=True): + controller = Controller(CAR_DBC["LEAPMOTOR_B10"], safety_CP()) + CC = car_structs.CarControl() + CC.latActive = True + CC.actuators.steeringAngleDeg = -43. + CS = SimpleNamespace(driver_override=False, driver_override_in_bend=False, eps_pushes_into_bend=into_bend, adas_lka=None, + powertrain_state=None, out=SimpleNamespace(steeringAngleDeg=-43., vEgoRaw=33 / 3.6, steeringTorqueEps=130)) + controller.lat_active_last, controller.apply_angle_last = True, -43. # steering, the wheel following + CS.driver_override = True + CS.out.steeringAngleDeg = wheel + controller.update(CC.as_reader(), CS, 0) + return controller.lat_active_last + + self.assertTrue(steering(-41.)) # follows within 3 deg: the bend rule, which sees no driver + self.assertFalse(steering(-41., into_bend=False)) + self.assertFalse(steering(-36.)) # held 7 deg short: the previous rule lets go + self.assertFalse(steering(-10.)) # near straight ahead: the previous rule + def test_accel_replaces_only_the_request(self): # with openpilot longitudinal, only ACC_ACCEL_REQ changes besides the steering fields for hexdata in REAL_ADAS_LKA["acc"]: diff --git a/opendbc_repo/opendbc/car/leapmotor/values.py b/opendbc_repo/opendbc/car/leapmotor/values.py index 342f5e6733..ebfb75a198 100644 --- a/opendbc_repo/opendbc/car/leapmotor/values.py +++ b/opendbc_repo/opendbc/car/leapmotor/values.py @@ -34,6 +34,19 @@ class CarControllerParams: EPS_TORQUE_PER_DRIVER_TORQUE = 15.5 EPS_TORQUE_LAG = 0.04 # s STEER_DRIVER_ALLOWANCE = 3 + # In a held bend the EPS pushes 60-175 into the turn, and the reading follows less of that than EPS_TORQUE_PER_DRIVER_TORQUE + # assumes: what is left over looks like the driver turning further in (3-5), and the EPS's own DRIVER_OVERRIDE flag + # comes on with it. openpilot let go mid-bend, the wheel sprang back 7-18 deg and the car drifted wide (route + # 0000000a, 12:30-12:31, the driver's hand resting on the wheel on the driver camera). While the wheel is in a bend + # (OVERRIDE_BEND_MIN_ANGLE), within OVERRIDE_BEND_TRACKING_ERROR of openpilot's angle and the EPS pushes into the turn, + # torque in the EPS's own direction counts only as far as the raw reading shows it, and the flag needs the driver's + # torque against the EPS too. Replayed on 8 drives: 11 of 218 let-gos removed, the wheel sprang back after every + # one of them; none where the wheel stayed where the driver held it. A driver holding the wheel against openpilot + # (route 00000010 23:18:35, near straight ahead) makes the wheel fall behind or the EPS push out of the turn, and + # keeps the previous rule. + OVERRIDE_BEND_MIN_ANGLE = 15. # deg + OVERRIDE_BEND_TRACKING_ERROR = 3. # deg + OVERRIDE_BEND_OPPOSING_TORQUE = 1. # Largest gap (deg) between openpilot's angle and the wheel's at which it takes the wheel back. It stops the case it was # made for: at walking pace the driver let go with the wheel at +308 deg and the model at +24, and the EPS swung it to # -203 deg. 10 deg was too tight: right after the driver lets go in a bend the gap is 11-27 deg, and on route 26b