|
1 | 1 | import numpy as np
|
2 | 2 | from opendbc.can.packer import CANPacker
|
3 |
| -from opendbc.car import Bus, DT_CTRL, apply_driver_steer_torque_limits, common_fault_avoidance, make_tester_present_msg, structs |
| 3 | +from opendbc.car import Bus, DT_CTRL, apply_driver_steer_torque_limits, apply_std_steer_angle_limits, common_fault_avoidance, \ |
| 4 | + make_tester_present_msg, structs |
4 | 5 | from opendbc.car.common.conversions import Conversions as CV
|
5 | 6 | from opendbc.car.hyundai import hyundaicanfd, hyundaican
|
6 | 7 | from opendbc.car.hyundai.carstate import CarState
|
|
16 | 17 |
|
17 | 18 | # EPS faults if you apply torque while the steering angle is above 90 degrees for more than 1 second
|
18 | 19 | # All slightly below EPS thresholds to avoid fault
|
19 |
| -MAX_ANGLE = 85 |
20 |
| -MAX_ANGLE_FRAMES = 89 |
21 |
| -MAX_ANGLE_CONSECUTIVE_FRAMES = 2 |
| 20 | +MAX_FAULT_ANGLE = 85 |
| 21 | +MAX_FAULT_ANGLE_FRAMES = 89 |
| 22 | +MAX_FAULT_ANGLE_CONSECUTIVE_FRAMES = 2 |
22 | 23 |
|
23 | 24 |
|
24 | 25 | def process_hud_alert(enabled, fingerprint, hud_control):
|
@@ -53,33 +54,54 @@ def __init__(self, dbc_names, CP, CP_SP):
|
53 | 54 | self.CAN = CanBus(CP)
|
54 | 55 | self.params = CarControllerParams(CP)
|
55 | 56 | self.packer = CANPacker(dbc_names[Bus.pt])
|
56 |
| - self.angle_limit_counter = 0 |
| 57 | + self.car_fingerprint = CP.carFingerprint |
57 | 58 |
|
58 | 59 | self.accel_last = 0
|
59 | 60 | self.apply_torque_last = 0
|
60 |
| - self.car_fingerprint = CP.carFingerprint |
| 61 | + self.apply_angle_last = 0 |
| 62 | + self.lkas_max_torque = 0 |
61 | 63 | self.last_button_frame = 0
|
| 64 | + self.angle_limit_counter = 0 |
62 | 65 |
|
63 | 66 | def update(self, CC, CC_SP, CS, now_nanos):
|
64 | 67 | EsccCarController.update(self, CS)
|
65 | 68 | MadsCarController.update(self, self.CP, CC, CC_SP, self.frame)
|
66 | 69 | actuators = CC.actuators
|
67 | 70 | hud_control = CC.hudControl
|
68 | 71 |
|
| 72 | + # TODO: needed for angle control cars? |
| 73 | + # >90 degree steering fault prevention |
| 74 | + self.angle_limit_counter, apply_steer_req = common_fault_avoidance(abs(CS.out.steeringAngleDeg) >= MAX_FAULT_ANGLE, CC.latActive, |
| 75 | + self.angle_limit_counter, MAX_FAULT_ANGLE_FRAMES, |
| 76 | + MAX_FAULT_ANGLE_CONSECUTIVE_FRAMES) |
| 77 | + # Hold torque with induced temporary fault when cutting the actuation bit |
| 78 | + torque_fault = CC.latActive and not apply_steer_req |
| 79 | + |
| 80 | + apply_torque = 0 |
| 81 | + |
69 | 82 | # steering torque
|
70 |
| - new_torque = int(round(actuators.torque * self.params.STEER_MAX)) |
71 |
| - apply_torque = apply_driver_steer_torque_limits(new_torque, self.apply_torque_last, CS.out.steeringTorque, self.params) |
| 83 | + if not self.CP.flags & HyundaiFlags.CANFD_ANGLE_STEERING: |
| 84 | + new_torque = int(round(actuators.torque * self.params.STEER_MAX)) |
| 85 | + apply_torque = apply_driver_steer_torque_limits(new_torque, self.apply_torque_last, CS.out.steeringTorque, self.params) |
72 | 86 |
|
73 |
| - # >90 degree steering fault prevention |
74 |
| - self.angle_limit_counter, apply_steer_req = common_fault_avoidance(abs(CS.out.steeringAngleDeg) >= MAX_ANGLE, CC.latActive, |
75 |
| - self.angle_limit_counter, MAX_ANGLE_FRAMES, |
76 |
| - MAX_ANGLE_CONSECUTIVE_FRAMES) |
| 87 | + # angle control |
| 88 | + else: |
| 89 | + if CS.out.steeringPressed: |
| 90 | + self.apply_angle_last = actuators.steeringAngleDeg |
| 91 | + self.apply_angle_last = apply_std_steer_angle_limits(actuators.steeringAngleDeg, self.apply_angle_last, CS.out.vEgoRaw, |
| 92 | + CS.out.steeringAngleDeg, CC.latActive, self.params.ANGLE_LIMITS) |
| 93 | + |
| 94 | + # Similar to torque control driver torque override, we ramp up and down the max allowed torque, |
| 95 | + # but this is a single threshold for simplicity. It also matches the stock system behavior. |
| 96 | + if abs(CS.out.steeringTorque) > self.params.STEER_THRESHOLD: |
| 97 | + self.lkas_max_torque = max(self.lkas_max_torque - self.params.ANGLE_TORQUE_DOWN_RATE, self.params.ANGLE_MIN_TORQUE) |
| 98 | + else: |
| 99 | + # ramp back up on engage as well |
| 100 | + self.lkas_max_torque = min(self.lkas_max_torque + self.params.ANGLE_TORQUE_UP_RATE, self.params.ANGLE_MAX_TORQUE) |
77 | 101 |
|
78 | 102 | if not CC.latActive:
|
79 | 103 | apply_torque = 0
|
80 |
| - |
81 |
| - # Hold torque with induced temporary fault when cutting the actuation bit |
82 |
| - torque_fault = CC.latActive and not apply_steer_req |
| 104 | + self.lkas_max_torque = 0 |
83 | 105 |
|
84 | 106 | self.apply_torque_last = apply_torque
|
85 | 107 |
|
@@ -115,7 +137,8 @@ def update(self, CC, CC_SP, CS, now_nanos):
|
115 | 137 | lka_steering_long = lka_steering and self.CP.openpilotLongitudinalControl
|
116 | 138 |
|
117 | 139 | # steering control
|
118 |
| - can_sends.extend(hyundaicanfd.create_steering_messages(self.packer, self.CP, self.CAN, CC.enabled, apply_steer_req, apply_torque, self.lkas_icon)) |
| 140 | + can_sends.extend(hyundaicanfd.create_steering_messages(self.packer, self.CP, self.CAN, CC.enabled, apply_steer_req, apply_torque, |
| 141 | + self.apply_angle_last, self.lkas_max_torque, self.lkas_icon)) |
119 | 142 |
|
120 | 143 | # prevent LFA from activating on LKA steering cars by sending "no lane lines detected" to ADAS ECU
|
121 | 144 | if self.frame % 5 == 0 and lka_steering:
|
@@ -177,6 +200,7 @@ def update(self, CC, CC_SP, CS, now_nanos):
|
177 | 200 | new_actuators = actuators.as_builder()
|
178 | 201 | new_actuators.torque = apply_torque / self.params.STEER_MAX
|
179 | 202 | new_actuators.torqueOutputCan = apply_torque
|
| 203 | + new_actuators.steeringAngleDeg = self.apply_angle_last |
180 | 204 | new_actuators.accel = accel
|
181 | 205 |
|
182 | 206 | self.frame += 1
|
|
0 commit comments