1
1
#! /usr/bin/python
2
2
"""
3
3
4
- Path tracking simulation with rear wheel feedback steering control and PID speed control.
4
+ Path tracking simulation with LQR steering control and PID speed control.
5
5
6
6
author: Atsushi Sakai
7
7
14
14
from matplotrecorder import matplotrecorder
15
15
import scipy .linalg as la
16
16
17
- Kp = 1.0 # speed propotional gain
18
- # steering control parameter
19
- KTH = 1.0
20
- KE = 0.5
17
+ Kp = 1.0 # speed proportional gain
21
18
19
+ # LQR parameter
22
20
Q = np .eye (4 )
23
21
R = np .eye (1 )
24
22
25
23
animation = True
26
24
# animation = False
27
25
28
- matplotrecorder .donothing = True
26
+ # matplotrecorder.donothing = True
29
27
30
28
31
29
def PIDControl (target , current ):
@@ -81,26 +79,7 @@ def dlqr(A, B, Q, R):
81
79
return K , X , eigVals
82
80
83
81
84
- def rear_wheel_feedback_control (state , cx , cy , cyaw , ck , preind ):
85
- ind , e = calc_nearest_index (state , cx , cy , cyaw )
86
-
87
- k = ck [ind ]
88
- v = state .v
89
- th_e = pi_2_pi (state .yaw - cyaw [ind ])
90
-
91
- omega = v * k * math .cos (th_e ) / (1.0 - k * e ) - \
92
- KTH * abs (v ) * th_e - KE * v * math .sin (th_e ) * e / th_e
93
-
94
- if th_e == 0.0 or omega == 0.0 :
95
- return 0.0 , ind
96
-
97
- delta = math .atan2 (unicycle_model .L * omega / v , 1.0 )
98
- # print(k, v, e, th_e, omega, delta)
99
-
100
- return delta , ind
101
-
102
-
103
- def lqr_steering_control (state , cx , cy , cyaw , ck , target_ind ):
82
+ def lqr_steering_control (state , cx , cy , cyaw , ck , pe , pth_e ):
104
83
ind , e = calc_nearest_index (state , cx , cy , cyaw )
105
84
106
85
k = ck [ind ]
@@ -123,19 +102,16 @@ def lqr_steering_control(state, cx, cy, cyaw, ck, target_ind):
123
102
x = np .matrix (np .zeros ((4 , 1 )))
124
103
125
104
x [0 , 0 ] = e
126
- x [1 , 0 ] = 0.0
105
+ x [1 , 0 ] = ( e - pe ) / unicycle_model . dt
127
106
x [2 , 0 ] = th_e
128
- x [3 , 0 ] = 0.0
107
+ x [3 , 0 ] = ( th_e - pth_e ) / unicycle_model . dt
129
108
130
109
ff = math .atan2 (unicycle_model .L * k , 1 )
131
110
fb = pi_2_pi ((- K * x )[0 , 0 ])
132
- print (math .degrees (th_e ))
133
- # print(K, x)
134
- print (math .degrees (ff ), math .degrees (fb ))
135
111
136
112
delta = ff + fb
137
- # print(delta)
138
- return delta
113
+
114
+ return delta , ind , e , th_e
139
115
140
116
141
117
def calc_nearest_index (state , cx , cy , cyaw ):
@@ -173,12 +149,10 @@ def closed_loop_prediction(cx, cy, cyaw, ck, speed_profile, goal):
173
149
t = [0.0 ]
174
150
target_ind = calc_nearest_index (state , cx , cy , cyaw )
175
151
176
- while T >= time :
177
- di , target_ind = rear_wheel_feedback_control (
178
- state , cx , cy , cyaw , ck , target_ind )
152
+ e , e_th = 0.0 , 0.0
179
153
180
- dl = lqr_steering_control ( state , cx , cy , cyaw , ck , target_ind )
181
- # print(di, dl )
154
+ while T >= time :
155
+ dl , target_ind , e , e_th = lqr_steering_control ( state , cx , cy , cyaw , ck , e , e_th )
182
156
183
157
ai = PIDControl (speed_profile [target_ind ], state .v )
184
158
# state = unicycle_model.update(state, ai, di)
@@ -249,9 +223,9 @@ def calc_speed_profile(cx, cy, cyaw, target_speed):
249
223
250
224
251
225
def main ():
252
- print ("rear wheel feedback tracking start!!" )
253
- ax = [0.0 , 6.0 , 12.5 , 5 .0 , 7.5 , 3.0 , - 1.0 ]
254
- ay = [0.0 , 0 .0 , 5.0 , 6.5 , 3.0 , 5.0 , - 2.0 ]
226
+ print ("LQR steering control tracking start!!" )
227
+ ax = [0.0 , 6.0 , 12.5 , 10 .0 , 7.5 , 3.0 , - 1.0 ]
228
+ ay = [0.0 , - 3 .0 , - 5.0 , 6.5 , 3.0 , 5.0 , - 2.0 ]
255
229
goal = [ax [- 1 ], ay [- 1 ]]
256
230
257
231
cx , cy , cyaw , ck , s = pycubicspline .calc_spline_course (ax , ay , ds = 0.1 )
0 commit comments