1-
21import math
32import scipy .integrate
43import scipy .optimize
76from collections import namedtuple
87from roboticstoolbox .mobile import *
98
9+
1010def solvepath (x , q0 = [0 , 0 , 0 ], ** kwargs ):
1111 # x[:4] is 4 coeffs of curvature polynomial
1212 # x[4] is total path length
@@ -16,7 +16,7 @@ def solvepath(x, q0=[0, 0, 0], **kwargs):
1616 def dotfunc (s , q , poly ):
1717 # q = (x, y, θ)
1818 # qdot = (cosθ, sinθ, ϰ)
19- k = poly [0 ] * s ** 3 + poly [1 ] * s ** 2 + poly [2 ] * s + poly [3 ]
19+ k = poly [0 ] * s ** 3 + poly [1 ] * s ** 2 + poly [2 ] * s + poly [3 ]
2020 # k = ((poly[0] * s + poly[1]) * s + poly[2]) * s + poly[3]
2121
2222 # save maximum curvature for this path solution
@@ -31,11 +31,13 @@ def dotfunc(s, q, poly):
3131 sol = scipy .integrate .solve_ivp (dotfunc , [0 , s_f ], q0 , args = (cpoly ,), ** kwargs )
3232 return sol .y , maxcurvature
3333
34+
3435def xcurvature (x ):
3536 # inequality constraint function, must be non-negative
3637 _ , maxcurvature = solvepath (x , q0 = (0 , 0 , 0 ))
3738 return maxcurvature
3839
40+
3941def costfunc (x , start , goal ):
4042 # final cost of path from start with params
4143 # p[0:4] is polynomial: k0, a, b, c
@@ -49,8 +51,9 @@ def costfunc(x, start, goal):
4951 e = np .linalg .norm (path [:, - 1 ] - np .r_ [goal ])
5052
5153 return e
52- class CurvaturePolyPlanner (PlannerBase ):
5354
55+
56+ class CurvaturePolyPlanner (PlannerBase ):
5457 def __init__ (self , curvature = None ):
5558 """
5659 Continuous curvature path planner
@@ -81,7 +84,6 @@ def __init__(self, curvature=None):
8184 super ().__init__ (ndims = 3 )
8285 self .curvature = curvature
8386
84-
8587 def query (self , start , goal ):
8688 r"""
8789 Find a path betwee two configurations
@@ -113,41 +115,51 @@ def query(self, start, goal):
113115 self ._start = start
114116 self ._goal = goal
115117
116- # initial estimate of path length is Euclidean distance
118+ # initial estimate of path length is Euclidean distance
117119 d = np .linalg .norm (goal [:2 ] - start [:2 ])
118120 # state vector is kappa_0, a, b, c, s_f
119121
120122 if self .curvature is not None :
121- nlcontraints = (scipy .optimize .NonlinearConstraint (xcurvature , 0 , self .curvature ),)
123+ nlcontraints = (
124+ scipy .optimize .NonlinearConstraint (xcurvature , 0 , self .curvature ),
125+ )
122126 else :
123127 nlcontraints = ()
124128
125- sol = scipy .optimize .minimize (costfunc , [0 , 0 , 0 , 0 , d ],
129+ sol = scipy .optimize .minimize (
130+ costfunc ,
131+ [0 , 0 , 0 , 0 , d ],
126132 constraints = nlcontraints ,
127133 bounds = [(None , None ), (None , None ), (None , None ), (None , None ), (d , None )],
128- args = (start , goal ))
134+ args = (start , goal ),
135+ )
129136 print (sol )
130- path , maxcurvature = solvepath (sol .x , q0 = start , dense_output = True , max_step = 1e-2 )
137+ path , maxcurvature = solvepath (
138+ sol .x , q0 = start , dense_output = True , max_step = 1e-2
139+ )
131140
132- status = namedtuple ('CurvaturePolyStatus' , ['length' , 'maxcurvature' , 'poly' ])(sol .x [4 ], maxcurvature , sol .x [:4 ])
141+ status = namedtuple ("CurvaturePolyStatus" , ["length" , "maxcurvature" , "poly" ])(
142+ sol .x [4 ], maxcurvature , sol .x [:4 ]
143+ )
133144
134145 return path .T , status
135146
136- if __name__ == '__main__' :
147+
148+ if __name__ == "__main__" :
137149 from math import pi
138150
139151 # start = (1, 1, pi/4)
140152 # goal = (-3, -3, -pi/4)
141- start = (0 , 0 , - pi / 4 )
142- goal = (1 , 2 , pi / 4 )
153+ # start = (0, 0, -pi/4)
154+ # goal = (1, 2, pi/4)
143155
144- start = (0 , 0 , pi / 2 )
145- goal = (1 , 0 , pi / 2 )
156+ start = (0 , 0 , pi / 2 )
157+ goal = (1 , 0 , pi / 2 )
146158
147159 planner = CurvaturePolyPlanner ()
148160 path , status = planner .query (start , goal )
149- print (' start' , path [0 ,:])
150- print (' goal' , path [- 1 , :])
161+ print (" start" , path [0 , :])
162+ print (" goal" , path [- 1 , :])
151163
152164 print (status )
153165 planner .plot (path , block = True )
@@ -163,4 +175,4 @@ def query(self, start, goal):
163175 # c[i] /= sf ** (3-i)
164176
165177 # print(solvepath(np.r_[c, 1], start))
166- # print(c)
178+ # print(c)
0 commit comments