1
+ from collections import deque
1
2
import math
2
3
3
4
from cereal import log
6
7
from selfdrive .controls .lib .pid import PIDController
7
8
from selfdrive .controls .lib .vehicle_model import ACCELERATION_DUE_TO_GRAVITY
8
9
10
+ from common .op_params import opParams
11
+
9
12
# At higher speeds (25+mph) we can assume:
10
13
# Lateral acceleration achieved by a specific car correlates to
11
14
# torque applied to the steering rack. It does not correlate to
@@ -31,6 +34,8 @@ def __init__(self, CP, CI):
31
34
self .use_steering_angle = CP .lateralTuning .torque .useSteeringAngle
32
35
self .friction = CP .lateralTuning .torque .friction
33
36
self .kf = CP .lateralTuning .torque .kf
37
+ self .op_params = opParams ()
38
+ self .errors = deque ([0 ] * 10 , maxlen = 10 ) # 10 frames
34
39
35
40
def reset (self ):
36
41
super ().reset ()
@@ -55,19 +60,26 @@ def update(self, active, CS, VM, params, last_actuators, desired_curvature, desi
55
60
56
61
setpoint = desired_lateral_accel + LOW_SPEED_FACTOR * desired_curvature
57
62
measurement = actual_lateral_accel + LOW_SPEED_FACTOR * actual_curvature
63
+
58
64
error = setpoint - measurement
59
- pid_log .error = error
65
+ error_rate = (error - self .errors [0 ]) / len (self .errors )
66
+ self .errors .append (error )
67
+ # live tune for now
68
+ self .pid .k_d = self .op_params .get ('torque_derivative' )
60
69
61
70
ff = desired_lateral_accel - params .roll * ACCELERATION_DUE_TO_GRAVITY
62
71
# convert friction into lateral accel units for feedforward
63
72
friction_compensation = interp (desired_lateral_jerk , [- JERK_THRESHOLD , JERK_THRESHOLD ], [- self .friction , self .friction ])
64
73
ff += friction_compensation / self .kf
65
74
output_torque = self .pid .update (error ,
75
+ error_rate = error_rate ,
66
76
override = CS .steeringPressed , feedforward = ff ,
67
77
speed = CS .vEgo ,
68
78
freeze_integrator = CS .steeringRateLimited )
69
79
70
80
pid_log .active = True
81
+ pid_log .error = error
82
+ pid_log .error_rate = error_rate
71
83
pid_log .p = self .pid .p
72
84
pid_log .i = self .pid .i
73
85
pid_log .d = self .pid .d
0 commit comments