1- #! /usr/bin/python
2- # -*- coding: utf-8 -*-
31"""
42
53Dubins path planner sample code
64
75author Atsushi Sakai(@Atsushi_twi)
86
9- License MIT
10-
117"""
128import math
9+ import numpy as np
10+ import matplotlib .pyplot as plt
11+
12+ show_animation = True
1313
1414
1515def mod2pi (theta ):
@@ -147,7 +147,7 @@ def dubins_path_planning_from_origin(ex, ey, eyaw, c):
147147 dx = ex
148148 dy = ey
149149 D = math .sqrt (dx ** 2.0 + dy ** 2.0 )
150- d = D / c
150+ d = D * c
151151 # print(dx, dy, D, d)
152152
153153 theta = mod2pi (math .atan2 (dy , dx ))
@@ -163,7 +163,6 @@ def dubins_path_planning_from_origin(ex, ey, eyaw, c):
163163 for planner in planners :
164164 t , p , q , mode = planner (alpha , beta , d )
165165 if t is None :
166- # print("".join(mode) + " cannot generate path")
167166 continue
168167
169168 cost = (abs (t ) + abs (p ) + abs (q ))
@@ -213,14 +212,6 @@ def dubins_path_planning(sx, sy, syaw, ex, ey, eyaw, c):
213212 py = [- math .sin (- syaw ) * x + math .cos (- syaw ) *
214213 y + sy for x , y in zip (lpx , lpy )]
215214 pyaw = [pi_2_pi (iyaw + syaw ) for iyaw in lpyaw ]
216- # print(syaw)
217- # pyaw = lpyaw
218-
219- # plt.plot(pyaw, "-r")
220- # plt.plot(lpyaw, "-b")
221- # plt.plot(eyaw, "*r")
222- # plt.plot(syaw, "*b")
223- # plt.show()
224215
225216 return px , py , pyaw , mode , clen
226217
@@ -234,14 +225,14 @@ def generate_course(length, mode, c):
234225 for m , l in zip (mode , length ):
235226 pd = 0.0
236227 if m is "S" :
237- d = 1.0 / c
228+ d = 1.0 * c
238229 else : # turning couse
239230 d = math .radians (3.0 )
240231
241232 while pd < abs (l - d ):
242233 # print(pd, l)
243- px .append (px [- 1 ] + d * c * math .cos (pyaw [- 1 ]))
244- py .append (py [- 1 ] + d * c * math .sin (pyaw [- 1 ]))
234+ px .append (px [- 1 ] + d / c * math .cos (pyaw [- 1 ]))
235+ py .append (py [- 1 ] + d / c * math .sin (pyaw [- 1 ]))
245236
246237 if m is "L" : # left turn
247238 pyaw .append (pyaw [- 1 ] + d )
@@ -252,8 +243,8 @@ def generate_course(length, mode, c):
252243 pd += d
253244 else :
254245 d = l - pd
255- px .append (px [- 1 ] + d * c * math .cos (pyaw [- 1 ]))
256- py .append (py [- 1 ] + d * c * math .sin (pyaw [- 1 ]))
246+ px .append (px [- 1 ] + d / c * math .cos (pyaw [- 1 ]))
247+ py .append (py [- 1 ] + d / c * math .sin (pyaw [- 1 ]))
257248
258249 if m is "L" : # left turn
259250 pyaw .append (pyaw [- 1 ] + d )
@@ -267,10 +258,9 @@ def generate_course(length, mode, c):
267258
268259
269260def plot_arrow (x , y , yaw , length = 1.0 , width = 0.5 , fc = "r" , ec = "k" ):
270- u """
261+ """
271262 Plot arrow
272263 """
273- import matplotlib .pyplot as plt
274264
275265 if not isinstance (x , float ):
276266 for (ix , iy , iyaw ) in zip (x , y , yaw ):
@@ -281,9 +271,8 @@ def plot_arrow(x, y, yaw, length=1.0, width=0.5, fc="r", ec="k"):
281271 plt .plot (x , y )
282272
283273
284- if __name__ == '__main__' :
274+ def main () :
285275 print ("Dubins path planner sample start!!" )
286- import matplotlib .pyplot as plt
287276
288277 start_x = 1.0 # [m]
289278 start_y = 1.0 # [m]
@@ -298,16 +287,58 @@ def plot_arrow(x, y, yaw, length=1.0, width=0.5, fc="r", ec="k"):
298287 px , py , pyaw , mode , clen = dubins_path_planning (start_x , start_y , start_yaw ,
299288 end_x , end_y , end_yaw , curvature )
300289
301- plt .plot (px , py , label = "final course " + "" .join (mode ))
290+ if show_animation :
291+ plt .plot (px , py , label = "final course " + "" .join (mode ))
292+
293+ # plotting
294+ plot_arrow (start_x , start_y , start_yaw )
295+ plot_arrow (end_x , end_y , end_yaw )
296+
297+ # for (ix, iy, iyaw) in zip(px, py, pyaw):
298+ # plot_arrow(ix, iy, iyaw, fc="b")
299+
300+ plt .legend ()
301+ plt .grid (True )
302+ plt .axis ("equal" )
303+ plt .show ()
304+
305+
306+ def test ():
302307
303- # plotting
304- plot_arrow (start_x , start_y , start_yaw )
305- plot_arrow (end_x , end_y , end_yaw )
308+ NTEST = 5
306309
307- # for (ix, iy, iyaw) in zip(px, py, pyaw):
308- # plot_arrow(ix, iy, iyaw, fc="b")
310+ for i in range (NTEST ):
311+ start_x = (np .random .rand () - 0.5 ) * 10.0 # [m]
312+ start_y = (np .random .rand () - 0.5 ) * 10.0 # [m]
313+ start_yaw = math .radians ((np .random .rand () - 0.5 ) * 180.0 ) # [rad]
309314
310- plt .legend ()
311- plt .grid (True )
312- plt .axis ("equal" )
313- plt .show ()
315+ end_x = (np .random .rand () - 0.5 ) * 10.0 # [m]
316+ end_y = (np .random .rand () - 0.5 ) * 10.0 # [m]
317+ end_yaw = math .radians ((np .random .rand () - 0.5 ) * 180.0 ) # [rad]
318+
319+ curvature = 1.0 / (np .random .rand () * 5.0 )
320+
321+ px , py , pyaw , mode , clen = dubins_path_planning (
322+ start_x , start_y , start_yaw , end_x , end_y , end_yaw , curvature )
323+
324+ if show_animation :
325+ plt .cla ()
326+ plt .plot (px , py , label = "final course " + str (mode ))
327+
328+ # plotting
329+ plot_arrow (start_x , start_y , start_yaw )
330+ plot_arrow (end_x , end_y , end_yaw )
331+
332+ plt .legend ()
333+ plt .grid (True )
334+ plt .axis ("equal" )
335+ plt .xlim (- 10 , 10 )
336+ plt .ylim (- 10 , 10 )
337+ plt .pause (1.0 )
338+
339+ print ("Test done" )
340+
341+
342+ if __name__ == '__main__' :
343+ test ()
344+ main ()
0 commit comments