11"""
22
3- LQR local path planning module
3+ LQR local path planning
44
55author: Atsushi Sakai (@Atsushi_twi)
66
1313
1414show_animation = True
1515
16- MAX_TIME = 100.0
17- DT = 0.1
16+ MAX_TIME = 100.0 # Maximum simulation time
17+ DT = 0.1 # Time tick
1818
1919
2020def LQRplanning (sx , sy , gx , gy ):
2121
2222 rx , ry = [sx ], [sy ]
2323
24- x = np .matrix ([gx - sx , gy - sy ]).T # State vector
24+ x = np .matrix ([sx - gx , sy - gy ]).T # State vector
2525
2626 # Linear system model
2727 A , B = get_system_model ()
2828
29- time = 0.0
29+ found_path = False
3030
31+ time = 0.0
3132 while time <= MAX_TIME :
3233 time += DT
3334
3435 u = LQR_control (A , B , x )
3536
3637 x = A * x + B * u
3738
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 )
4441
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 )
4743 if d <= 0.1 :
4844 print ("Goal!!" )
45+ found_path = True
4946 break
5047
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+
5161 return rx , ry
5262
5363
@@ -89,8 +99,9 @@ def dlqr(A, B, Q, R):
8999
90100
91101def 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 ]])
94105 B = np .matrix ([0.0 , 1.0 ]).T
95106
96107 return A , B
@@ -108,18 +119,19 @@ def LQR_control(A, B, x):
108119def main ():
109120 print (__file__ + " start!!" )
110121
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
115126
116127 rx , ry = LQRplanning (sx , sy , gx , gy )
117128
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 ()
123135
124136
125137if __name__ == '__main__' :
0 commit comments