-
Notifications
You must be signed in to change notification settings - Fork 584
/
Copy pathLQR_Dynamics_Model.py
536 lines (410 loc) · 16 KB
/
LQR_Dynamics_Model.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
"""
LQR and PID Controller
author: huiming zhou
"""
import os
import sys
import math
from enum import Enum
import matplotlib.pyplot as plt
sys.path.append(os.path.dirname(os.path.abspath(__file__)) +
"/../../MotionPlanning/")
import Control.draw_lqr as draw
from Control.config_control import *
import CurvesGenerator.reeds_shepp as rs
class Gear(Enum):
GEAR_DRIVE = 1
GEAR_REVERSE = 2
class VehicleState:
def __init__(self, x=0.0, y=0.0, yaw=0.0,
v=0.0, gear=Gear.GEAR_DRIVE):
self.x = x
self.y = y
self.yaw = yaw
self.v = v
self.e_cg = 0.0
self.theta_e = 0.0
self.gear = gear
self.steer = 0.0
def UpdateVehicleState(self, delta, a, e_cg, theta_e,
gear=Gear.GEAR_DRIVE):
"""
update states of vehicle
:param theta_e: yaw error to ref trajectory
:param e_cg: lateral error to ref trajectory
:param delta: steering angle [rad]
:param a: acceleration [m / s^2]
:param gear: gear mode [GEAR_DRIVE / GEAR/REVERSE]
"""
wheelbase_ = wheelbase
delta, a = self.RegulateInput(delta, a)
self.steer = delta
self.gear = gear
self.x += self.v * math.cos(self.yaw) * ts
self.y += self.v * math.sin(self.yaw) * ts
self.yaw += self.v / wheelbase_ * math.tan(delta) * ts
self.e_cg = e_cg
self.theta_e = theta_e
if gear == Gear.GEAR_DRIVE:
self.v += a * ts
else:
self.v += -1.0 * a * ts
self.v = self.RegulateOutput(self.v)
@staticmethod
def RegulateInput(delta, a):
"""
regulate delta to : - max_steer_angle ~ max_steer_angle
regulate a to : - max_acceleration ~ max_acceleration
:param delta: steering angle [rad]
:param a: acceleration [m / s^2]
:return: regulated delta and acceleration
"""
if delta < -1.0 * max_steer_angle:
delta = -1.0 * max_steer_angle
if delta > 1.0 * max_steer_angle:
delta = 1.0 * max_steer_angle
if a < -1.0 * max_acceleration:
a = -1.0 * max_acceleration
if a > 1.0 * max_acceleration:
a = 1.0 * max_acceleration
return delta, a
@staticmethod
def RegulateOutput(v):
"""
regulate v to : -max_speed ~ max_speed
:param v: calculated speed [m / s]
:return: regulated speed
"""
max_speed_ = max_speed
if v < -1.0 * max_speed_:
v = -1.0 * max_speed_
if v > 1.0 * max_speed_:
v = 1.0 * max_speed_
return v
class TrajectoryAnalyzer:
def __init__(self, x, y, yaw, k):
self.x_ = x
self.y_ = y
self.yaw_ = yaw
self.k_ = k
self.ind_old = 0
self.ind_end = len(x)
def ToTrajectoryFrame(self, vehicle_state):
"""
errors to trajectory frame
theta_e = yaw_vehicle - yaw_ref_path
e_cg = lateral distance of center of gravity (cg) in frenet frame
:param vehicle_state: vehicle state (class VehicleState)
:return: theta_e, e_cg, yaw_ref, k_ref
"""
x_cg = vehicle_state.x
y_cg = vehicle_state.y
yaw = vehicle_state.yaw
# calc nearest point in ref path
dx = [x_cg - ix for ix in self.x_[self.ind_old: self.ind_end]]
dy = [y_cg - iy for iy in self.y_[self.ind_old: self.ind_end]]
ind_add = int(np.argmin(np.hypot(dx, dy)))
dist = math.hypot(dx[ind_add], dy[ind_add])
# calc lateral relative position of vehicle to ref path
vec_axle_rot_90 = np.array([[math.cos(yaw + math.pi / 2.0)],
[math.sin(yaw + math.pi / 2.0)]])
vec_path_2_cg = np.array([[dx[ind_add]],
[dy[ind_add]]])
if np.dot(vec_axle_rot_90.T, vec_path_2_cg) > 0.0:
e_cg = 1.0 * dist # vehicle on the right of ref path
else:
e_cg = -1.0 * dist # vehicle on the left of ref path
# calc yaw error: theta_e = yaw_vehicle - yaw_ref
self.ind_old += ind_add
yaw_ref = self.yaw_[self.ind_old]
theta_e = pi_2_pi(yaw - yaw_ref)
# calc ref curvature
k_ref = self.k_[self.ind_old]
return theta_e, e_cg, yaw_ref, k_ref
class LatController:
"""
Lateral Controller using LQR
"""
def ComputeControlCommand(self, vehicle_state, ref_trajectory):
"""
calc lateral control command.
:param vehicle_state: vehicle state
:param ref_trajectory: reference trajectory (analyzer)
:return: steering angle (optimal u), theta_e, e_cg
"""
ts_ = ts
e_cg_old = vehicle_state.e_cg
theta_e_old = vehicle_state.theta_e
theta_e, e_cg, yaw_ref, k_ref = \
ref_trajectory.ToTrajectoryFrame(vehicle_state)
# Calc linearized time-discrete system model
matrix_ad_, matrix_bd_ = self.UpdateMatrix(vehicle_state)
matrix_state_ = np.zeros((state_size, 1))
matrix_r_ = np.diag(matrix_r)
matrix_q_ = np.diag(matrix_q)
# Solve Ricatti equations using value iteration
matrix_k_ = self.SolveLQRProblem(matrix_ad_, matrix_bd_, matrix_q_,
matrix_r_, eps, max_iteration)
# state vector: 4x1
matrix_state_[0][0] = e_cg
matrix_state_[1][0] = (e_cg - e_cg_old) / ts_
matrix_state_[2][0] = theta_e
matrix_state_[3][0] = (theta_e - theta_e_old) / ts_
# feedback steering angle
steer_angle_feedback = -(matrix_k_ @ matrix_state_)[0][0]
# calc feedforward term to decrease steady error
steer_angle_feedforward = self.ComputeFeedForward(vehicle_state, k_ref, matrix_k_)
steer_angle = steer_angle_feedback + steer_angle_feedforward
return steer_angle, theta_e, e_cg
@staticmethod
def ComputeFeedForward(vehicle_state, ref_curvature, matrix_k_):
"""
calc feedforward control term to decrease the steady error.
:param vehicle_state: vehicle state
:param ref_curvature: curvature of the target point in ref trajectory
:param matrix_k_: feedback matrix K
:return: feedforward term
"""
mass_ = m_f + m_r
wheelbase_ = l_f + l_r
kv = l_r * mass_ / 2.0 / c_f / wheelbase_ - \
l_f * mass_ / 2.0 / c_r / wheelbase_
v = vehicle_state.v
if vehicle_state.gear == Gear.GEAR_REVERSE:
steer_angle_feedforward = wheelbase_ * ref_curvature
else:
steer_angle_feedforward = wheelbase_ * ref_curvature + kv * v * v * ref_curvature - \
matrix_k_[0][2] * \
(l_r * ref_curvature -
l_f * mass_ * v * v * ref_curvature / 2.0 / c_r / wheelbase_)
return steer_angle_feedforward
@staticmethod
def SolveLQRProblem(A, B, Q, R, tolerance, max_num_iteration):
"""
iteratively calculating feedback matrix K
:param A: matrix_a_
:param B: matrix_b_
:param Q: matrix_q_
:param R: matrix_r_
:param tolerance: lqr_eps
:param max_num_iteration: max_iteration
:return: feedback matrix K
"""
assert np.size(A, 0) == np.size(A, 1) and \
np.size(B, 0) == np.size(A, 0) and \
np.size(Q, 0) == np.size(Q, 1) and \
np.size(Q, 0) == np.size(A, 1) and \
np.size(R, 0) == np.size(R, 1) and \
np.size(R, 0) == np.size(B, 1), \
"LQR solver: one or more matrices have incompatible dimensions."
M = np.zeros((np.size(Q, 0), np.size(R, 1)))
AT = A.T
BT = B.T
MT = M.T
P = Q
num_iteration = 0
diff = math.inf
while num_iteration < max_num_iteration and diff > tolerance:
num_iteration += 1
P_next = AT @ P @ A - (AT @ P @ B + M) @ \
np.linalg.pinv(R + BT @ P @ B) @ (BT @ P @ A + MT) + Q
# check the difference between P and P_next
diff = (abs(P_next - P)).max()
P = P_next
if num_iteration >= max_num_iteration:
print("LQR solver cannot converge to a solution",
"last consecutive result diff is: ", diff)
K = np.linalg.inv(BT @ P @ B + R) @ (BT @ P @ A + MT)
return K
@staticmethod
def UpdateMatrix(vehicle_state):
"""
calc A and b matrices of linearized, discrete system.
:return: A, b
"""
ts_ = ts
mass_ = m_f + m_r
v = vehicle_state.v
matrix_a_ = np.zeros((state_size, state_size)) # continuous A matrix
if vehicle_state.gear == Gear.GEAR_REVERSE:
"""
A matrix (Gear Reverse)
[0.0, 0.0, 1.0 * v 0.0;
0.0, -(c_f + c_r) / m / v, (c_f + c_r) / m,
(l_r * c_r - l_f * c_f) / m / v;
0.0, 0.0, 0.0, 1.0;
0.0, (lr * cr - lf * cf) / i_z / v, (l_f * c_f - l_r * c_r) / i_z,
-1.0 * (l_f^2 * c_f + l_r^2 * c_r) / i_z / v;]
"""
matrix_a_[0][1] = 0.0
matrix_a_[0][2] = 1.0 * v
else:
"""
A matrix (Gear Drive)
[0.0, 1.0, 0.0, 0.0;
0.0, -(c_f + c_r) / m / v, (c_f + c_r) / m,
(l_r * c_r - l_f * c_f) / m / v;
0.0, 0.0, 0.0, 1.0;
0.0, (lr * cr - lf * cf) / i_z / v, (l_f * c_f - l_r * c_r) / i_z,
-1.0 * (l_f^2 * c_f + l_r^2 * c_r) / i_z / v;]
"""
matrix_a_[0][1] = 1.0
matrix_a_[0][2] = 0.0
matrix_a_[1][1] = -1.0 * ( + c_r) / mass_ / v
matrix_a_[1][2] = (c_f + c_r) / mass_
matrix_a_[1][3] = (l_r * c_r - l_f * c_f) / mass_ / v
matrix_a_[2][3] = 1.0
matrix_a_[3][1] = (l_r * c_r - l_f * c_f) / Iz / v
matrix_a_[3][2] = (l_f * c_f - l_r * c_r) / Iz
matrix_a_[3][3] = -1.0 * (l_f ** 2 * c_f + l_r ** 2 * c_r) / Iz / v
# Tustin's method (bilinear transform)
matrix_i = np.eye(state_size) # identical matrix
matrix_ad_ = np.linalg.pinv(matrix_i - ts_ * 0.5 * matrix_a_) @ \
(matrix_i + ts_ * 0.5 * matrix_a_) # discrete A matrix
# b = [0.0, c_f / m, 0.0, l_f * c_f / I_z].T
matrix_b_ = np.zeros((state_size, 1)) # continuous b matrix
matrix_b_[1][0] = c_f / mass_
matrix_b_[3][0] = l_f * c_f / Iz
matrix_bd_ = matrix_b_ * ts_ # discrete b matrix
return matrix_ad_, matrix_bd_
class LonController:
"""
Longitudinal Controller using PID.
"""
@staticmethod
def ComputeControlCommand(target_speed, vehicle_state, dist):
"""
calc acceleration command using PID.
:param target_speed: target speed [m / s]
:param vehicle_state: vehicle state
:param dist: distance to goal [m]
:return: control command (acceleration) [m / s^2]
"""
if vehicle_state.gear == Gear.GEAR_DRIVE:
direct = 1.0
else:
direct = -1.0
a = 0.3 * (target_speed - direct * vehicle_state.v)
if dist < 10.0:
if vehicle_state.v > 2.0:
a = -3.0
elif vehicle_state.v < -2:
a = -1.0
return a
def pi_2_pi(angle):
"""
regulate theta to -pi ~ pi.
:param angle: input angle
:return: regulated angle
"""
M_PI = math.pi
if angle > M_PI:
return angle - 2.0 * M_PI
if angle < -M_PI:
return angle + 2.0 * M_PI
return angle
def generate_path(s):
"""
design path using reeds-shepp path generator.
divide paths into sections, in each section the direction is the same.
:param s: objective positions and directions.
:return: paths
"""
wheelbase_ = wheelbase
max_c = math.tan(0.5 * max_steer_angle) / wheelbase_
path_x, path_y, yaw, direct, rc = [], [], [], [], []
x_rec, y_rec, yaw_rec, direct_rec, rc_rec = [], [], [], [], []
direct_flag = 1.0
for i in range(len(s) - 1):
s_x, s_y, s_yaw = s[i][0], s[i][1], np.deg2rad(s[i][2])
g_x, g_y, g_yaw = s[i + 1][0], s[i + 1][1], np.deg2rad(s[i + 1][2])
path_i = rs.calc_optimal_path(s_x, s_y, s_yaw,
g_x, g_y, g_yaw, max_c)
irc, rds = rs.calc_curvature(path_i.x, path_i.y, path_i.yaw, path_i.directions)
ix = path_i.x
iy = path_i.y
iyaw = path_i.yaw
idirect = path_i.directions
for j in range(len(ix)):
if idirect[j] == direct_flag:
x_rec.append(ix[j])
y_rec.append(iy[j])
yaw_rec.append(iyaw[j])
direct_rec.append(idirect[j])
rc_rec.append(irc[j])
else:
if len(x_rec) == 0 or direct_rec[0] != direct_flag:
direct_flag = idirect[j]
continue
path_x.append(x_rec)
path_y.append(y_rec)
yaw.append(yaw_rec)
direct.append(direct_rec)
rc.append(rc_rec)
x_rec, y_rec, yaw_rec, direct_rec, rc_rec = \
[x_rec[-1]], [y_rec[-1]], [yaw_rec[-1]], [-direct_rec[-1]], [rc_rec[-1]]
path_x.append(x_rec)
path_y.append(y_rec)
yaw.append(yaw_rec)
direct.append(direct_rec)
rc.append(rc_rec)
x_all, y_all = [], []
for ix, iy in zip(path_x, path_y):
x_all += ix
y_all += iy
return path_x, path_y, yaw, direct, rc, x_all, y_all
def main():
# generate path
states = [(0, 0, 0), (20, 15, 0), (35, 20, 90), (40, 0, 180),
(20, 0, 120), (5, -10, 180), (15, 5, 30)]
#
# states = [(-3, 3, 120), (10, -7, 30), (10, 13, 30), (20, 5, -25),
# (35, 10, 180), (30, -10, 160), (5, -12, 90)]
x_ref, y_ref, yaw_ref, direct, curv, x_all, y_all = generate_path(states)
maxTime = 100.0
yaw_old = 0.0
x0, y0, yaw0, direct0 = \
x_ref[0][0], y_ref[0][0], yaw_ref[0][0], direct[0][0]
x_rec, y_rec, yaw_rec, direct_rec = [], [], [], []
lat_controller = LatController()
lon_controller = LonController()
for x, y, yaw, gear, k in zip(x_ref, y_ref, yaw_ref, direct, curv):
t = 0.0
if gear[0] == 1.0:
direct = Gear.GEAR_DRIVE
else:
direct = Gear.GEAR_REVERSE
ref_trajectory = TrajectoryAnalyzer(x, y, yaw, k)
vehicle_state = VehicleState(x=x0, y=y0, yaw=yaw0, v=0.1, gear=direct)
while t < maxTime:
dist = math.hypot(vehicle_state.x - x[-1], vehicle_state.y - y[-1])
if gear[0] > 0:
target_speed = 25.0 / 3.6
else:
target_speed = 15.0 / 3.6
delta_opt, theta_e, e_cg = \
lat_controller.ComputeControlCommand(vehicle_state, ref_trajectory)
a_opt = lon_controller.ComputeControlCommand(target_speed, vehicle_state, dist)
vehicle_state.UpdateVehicleState(pi_2_pi(delta_opt), a_opt, e_cg, theta_e, direct)
t += ts
if dist <= 0.5:
break
x_rec.append(vehicle_state.x)
y_rec.append(vehicle_state.y)
yaw_rec.append(vehicle_state.yaw)
x0 = x_rec[-1]
y0 = y_rec[-1]
yaw0 = yaw_rec[-1]
plt.cla()
plt.plot(x_all, y_all, color='gray', linewidth=2.0)
plt.plot(x_rec, y_rec, linewidth=2.0, color='darkviolet')
draw.draw_car(x0, y0, yaw0, -vehicle_state.steer)
plt.axis("equal")
plt.title("LQR (Dynamics): v=" + str(vehicle_state.v * 3.6)[:4] + "km/h")
plt.gcf().canvas.mpl_connect('key_release_event',
lambda event:
[exit(0) if event.key == 'escape' else None])
plt.pause(0.001)
plt.show()
if __name__ == '__main__':
main()