diff --git a/opendbc_repo/opendbc/car/leapmotor/carcontroller.py b/opendbc_repo/opendbc/car/leapmotor/carcontroller.py index ed9a552a22..943e578446 100644 --- a/opendbc_repo/opendbc/car/leapmotor/carcontroller.py +++ b/opendbc_repo/opendbc/car/leapmotor/carcontroller.py @@ -64,11 +64,10 @@ 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 - # (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) + # (while the wheel follows openpilot's angle, the stricter rule: see CarControllerParams.OVERRIDE_TRACKING_ERROR) + tracking = (self.lat_active_last and + abs(self.apply_angle_last - CS.out.steeringAngleDeg) < CarControllerParams.OVERRIDE_TRACKING_ERROR) + lat_active = CC.latActive and not (CS.driver_override_tracking if tracking 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 14cb27b3dc..5f910f6e68 100644 --- a/opendbc_repo/opendbc/car/leapmotor/carstate.py +++ b/opendbc_repo/opendbc/car/leapmotor/carstate.py @@ -19,11 +19,10 @@ 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 override used by the controller while the wheel follows openpilot's angle (see OVERRIDE_TRACKING_ERROR) + self.tracking_pressed_cnt = 0 + self.tracking_override_frames = 0 + self.driver_override_tracking = 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 @@ -72,17 +71,15 @@ 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 + # While the wheel follows openpilot the share estimate overshoots, so there it only removes torque, and the flag + # needs torque against the EPS as well (see CarControllerParams.OVERRIDE_TRACKING_ERROR) + tracking_torque = ret.steeringTorque if ret.steeringTorque * eps_share <= 0 else \ + (driver_torque_raw if driver_torque_raw * eps_share > 0 else 0.) + pressed = abs(tracking_torque) > CarControllerParams.STEER_DRIVER_ALLOWANCE + self.tracking_pressed_cnt = int(np.clip(self.tracking_pressed_cnt + (1 if pressed else -1), 0, 11)) + opposing = tracking_torque * eps_share < 0 and abs(tracking_torque) >= CarControllerParams.OVERRIDE_OPPOSING_TORQUE + self.tracking_override_frames = self.tracking_override_frames + 1 if eps_override and opposing else 0 + self.driver_override_tracking = self.tracking_override_frames >= 2 or self.tracking_pressed_cnt > 5 # 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 d0dc31c72e..9f101726c6 100644 --- a/opendbc_repo/opendbc/car/leapmotor/tests/test_leapmotor.py +++ b/opendbc_repo/opendbc/car/leapmotor/tests/test_leapmotor.py @@ -226,10 +226,10 @@ 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): + def test_override_while_the_wheel_follows(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. + # the wheel sprang back 14 deg; the tracking 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 @@ -249,37 +249,35 @@ class TestLeapmotorCan(unittest.TestCase): 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 + self.assertFalse(CS.driver_override_tracking) # 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) + self.assertTrue(CS.driver_override_tracking) - def test_bend_rule_only_while_the_wheel_follows(self): + def test_tracking_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): + def steering(wheel, command=-43.): 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, + CC.actuators.steeringAngleDeg = command + CS = SimpleNamespace(driver_override=False, driver_override_tracking=False, 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 + controller.lat_active_last, controller.apply_angle_last = True, command # 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.assertTrue(steering(-41.)) # follows within 3 deg: the tracking rule, which sees no driver + self.assertTrue(steering(1., command=2.)) # also near straight ahead self.assertFalse(steering(-36.)) # held 7 deg short: the previous rule lets go - self.assertFalse(steering(-10.)) # near straight ahead: the previous rule + self.assertFalse(steering(3., command=12.)) # held straight against a 12 deg command (route 00000010 23:18:35) def test_accel_replaces_only_the_request(self): # with openpilot longitudinal, only ACC_ACCEL_REQ changes besides the steering fields diff --git a/opendbc_repo/opendbc/car/leapmotor/values.py b/opendbc_repo/opendbc/car/leapmotor/values.py index ebfb75a198..784fc99f84 100644 --- a/opendbc_repo/opendbc/car/leapmotor/values.py +++ b/opendbc_repo/opendbc/car/leapmotor/values.py @@ -34,19 +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. + # While steering, the EPS often pushes 50-175, and the reading follows less of that than EPS_TORQUE_PER_DRIVER_TORQUE + # assumes: what is left over looks like the driver turning with the EPS (3-5), and the EPS's own DRIVER_OVERRIDE flag + # comes on with it. openpilot let go, the wheel sprang back 3-94 deg and the car drifted (route 0000000a, 12:30-12:31 + # in bends, the driver's hand resting on the wheel on the driver camera). So while the wheel follows openpilot's + # angle within OVERRIDE_TRACKING_ERROR, 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. A driver who steers moves the wheel off openpilot's + # angle, and from there the previous rule applies: of 218 let-gos on 8 drives, all 101 that came with the wheel + # already 3 deg or more off (the driver holding it straight against a 12 deg command at route 00000010 23:18:35 + # among them) are unchanged. Of the 38 after which the wheel sprang back, 37 no longer happen at that moment. + # A model of the EPS torque needed hands-off (from speed, lateral acceleration, angle and rate) did not tell them + # apart: at every kind of let-go the EPS was 60-70 above it. + OVERRIDE_TRACKING_ERROR = 3. # deg + OVERRIDE_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