3
3
4
4
from cereal import log
5
5
from opendbc .car .lateral import FRICTION_THRESHOLD , get_friction
6
+ from opendbc .car .interfaces import LatControlInputs
6
7
from openpilot .common .constants import ACCELERATION_DUE_TO_GRAVITY
7
8
from openpilot .selfdrive .controls .lib .latcontrol import LatControl
8
9
from openpilot .common .pid import PIDController
@@ -28,11 +29,9 @@ class LatControlTorque(LatControl):
28
29
def __init__ (self , CP , CP_SP , CI ):
29
30
super ().__init__ (CP , CP_SP , CI )
30
31
self .torque_params = CP .lateralTuning .torque .as_builder ()
31
- self .torque_from_lateral_accel = CI .torque_from_lateral_accel ()
32
- self .lateral_accel_from_torque = CI .lateral_accel_from_torque ()
33
32
self .pid = PIDController (self .torque_params .kp , self .torque_params .ki ,
34
- k_f = self .torque_params .kf )
35
- self .update_limits ()
33
+ k_f = self .torque_params .kf , pos_limit = self . steer_max , neg_limit = - self . steer_max )
34
+ self .torque_from_lateral_accel = CI . torque_from_lateral_accel ()
36
35
self .steering_angle_deadzone_deg = self .torque_params .steeringAngleDeadzoneDeg
37
36
38
37
self .extension = LatControlTorqueExt (self , CP , CP_SP )
@@ -41,11 +40,6 @@ def update_live_torque_params(self, latAccelFactor, latAccelOffset, friction):
41
40
self .torque_params .latAccelFactor = latAccelFactor
42
41
self .torque_params .latAccelOffset = latAccelOffset
43
42
self .torque_params .friction = friction
44
- self .update_limits ()
45
-
46
- def update_limits (self ):
47
- self .pid .set_limits (self .lateral_accel_from_torque (self .steer_max , self .torque_params ),
48
- self .lateral_accel_from_torque (- self .steer_max , self .torque_params ))
49
43
50
44
def update (self , active , CS , VM , params , steer_limited_by_safety , desired_curvature , calibrated_pose , curvature_limited ):
51
45
pid_log = log .ControlsState .LateralTorqueState .new_message ()
@@ -67,10 +61,13 @@ def update(self, active, CS, VM, params, steer_limited_by_safety, desired_curvat
67
61
setpoint = desired_lateral_accel + low_speed_factor * desired_curvature
68
62
measurement = actual_lateral_accel + low_speed_factor * actual_curvature
69
63
gravity_adjusted_lateral_accel = desired_lateral_accel - roll_compensation
70
-
71
- # do error correction in lateral acceleration space, convert at end to handle non-linear torque responses correctly
72
- pid_log .error = float (setpoint - measurement )
73
- ff = gravity_adjusted_lateral_accel
64
+ torque_from_setpoint = self .torque_from_lateral_accel (LatControlInputs (setpoint , roll_compensation , CS .vEgo , CS .aEgo ), self .torque_params ,
65
+ gravity_adjusted = False )
66
+ torque_from_measurement = self .torque_from_lateral_accel (LatControlInputs (measurement , roll_compensation , CS .vEgo , CS .aEgo ), self .torque_params ,
67
+ gravity_adjusted = False )
68
+ pid_log .error = float (torque_from_setpoint - torque_from_measurement )
69
+ ff = self .torque_from_lateral_accel (LatControlInputs (gravity_adjusted_lateral_accel , roll_compensation , CS .vEgo , CS .aEgo ), self .torque_params ,
70
+ gravity_adjusted = True )
74
71
ff += get_friction (desired_lateral_accel - actual_lateral_accel , lateral_accel_deadzone , FRICTION_THRESHOLD , self .torque_params )
75
72
76
73
# Lateral acceleration torque controller extension updates
@@ -80,18 +77,17 @@ def update(self, active, CS, VM, params, steer_limited_by_safety, desired_curvat
80
77
desired_curvature , actual_curvature )
81
78
82
79
freeze_integrator = steer_limited_by_safety or CS .steeringPressed or CS .vEgo < 5
83
- output_lataccel = self .pid .update (pid_log .error ,
80
+ output_torque = self .pid .update (pid_log .error ,
84
81
feedforward = ff ,
85
82
speed = CS .vEgo ,
86
83
freeze_integrator = freeze_integrator )
87
- output_torque = self .torque_from_lateral_accel (output_lataccel , self .torque_params )
88
84
89
85
pid_log .active = True
90
86
pid_log .p = float (self .pid .p )
91
87
pid_log .i = float (self .pid .i )
92
88
pid_log .d = float (self .pid .d )
93
89
pid_log .f = float (self .pid .f )
94
- pid_log .output = float (- output_torque ) # TODO: log lat accel?
90
+ pid_log .output = float (- output_torque )
95
91
pid_log .actualLateralAccel = float (actual_lateral_accel )
96
92
pid_log .desiredLateralAccel = float (desired_lateral_accel )
97
93
pid_log .saturated = bool (self ._check_saturation (self .steer_max - abs (output_torque ) < 1e-3 , CS , steer_limited_by_safety , curvature_limited ))
0 commit comments