1
1
"""
2
2
3
- LQR local path planning module
3
+ LQR local path planning
4
4
5
5
author: Atsushi Sakai (@Atsushi_twi)
6
6
13
13
14
14
show_animation = True
15
15
16
- MAX_TIME = 100.0
17
- DT = 0.1
16
+ MAX_TIME = 100.0 # Maximum simulation time
17
+ DT = 0.1 # Time tick
18
18
19
19
20
20
def LQRplanning (sx , sy , gx , gy ):
21
21
22
22
rx , ry = [sx ], [sy ]
23
23
24
- x = np .matrix ([gx - sx , gy - sy ]).T # State vector
24
+ x = np .matrix ([sx - gx , sy - gy ]).T # State vector
25
25
26
26
# Linear system model
27
27
A , B = get_system_model ()
28
28
29
- time = 0.0
29
+ found_path = False
30
30
31
+ time = 0.0
31
32
while time <= MAX_TIME :
32
33
time += DT
33
34
34
35
u = LQR_control (A , B , x )
35
36
36
37
x = A * x + B * u
37
38
38
- rx .append (x [0 , 0 ])
39
- ry .append (x [1 , 0 ])
40
-
41
- plt .plot (rx , ry )
42
- plt .plot (rx [- 1 ], ry [- 1 ], "xr" )
43
- plt .pause (1.0 )
39
+ rx .append (x [0 , 0 ] + gx )
40
+ ry .append (x [1 , 0 ] + gy )
44
41
45
- d = math .sqrt ((gx - x [0 , 0 ])** 2 + (gy - x [1 , 0 ])** 2 )
46
- print (d )
42
+ d = math .sqrt ((gx - rx [- 1 ])** 2 + (gy - ry [- 1 ])** 2 )
47
43
if d <= 0.1 :
48
44
print ("Goal!!" )
45
+ found_path = True
49
46
break
50
47
48
+ # animation
49
+ if show_animation :
50
+ plt .cla ()
51
+ plt .plot (sx , sy , "or" )
52
+ plt .plot (gx , gy , "ob" )
53
+ plt .plot (rx , ry , "-r" )
54
+ plt .axis ("equal" )
55
+ plt .pause (1.0 )
56
+
57
+ if not found_path :
58
+ print ("Cannot found path" )
59
+ return [], []
60
+
51
61
return rx , ry
52
62
53
63
@@ -89,8 +99,9 @@ def dlqr(A, B, Q, R):
89
99
90
100
91
101
def get_system_model ():
92
- A = np .eye (2 ) * DT
93
- A [0 , 1 ] = 1.0
102
+
103
+ A = np .matrix ([[DT , 1.0 ],
104
+ [0.0 , DT ]])
94
105
B = np .matrix ([0.0 , 1.0 ]).T
95
106
96
107
return A , B
@@ -108,18 +119,19 @@ def LQR_control(A, B, x):
108
119
def main ():
109
120
print (__file__ + " start!!" )
110
121
111
- sx = - 10 .0
112
- sy = - 5 .0
113
- gx = 0 .0
114
- gy = 0 .0
122
+ sx = 6 .0
123
+ sy = 6 .0
124
+ gx = 10 .0
125
+ gy = 10 .0
115
126
116
127
rx , ry = LQRplanning (sx , sy , gx , gy )
117
128
118
- plt .plot (sx , sy , "xb" )
119
- plt .plot (gx , gy , "xb" )
120
- plt .plot (rx , ry )
121
- plt .axis ("equal" )
122
- plt .show ()
129
+ if show_animation :
130
+ plt .plot (sx , sy , "or" )
131
+ plt .plot (gx , gy , "ob" )
132
+ plt .plot (rx , ry , "-r" )
133
+ plt .axis ("equal" )
134
+ plt .show ()
123
135
124
136
125
137
if __name__ == '__main__' :
0 commit comments