This repository was archived by the owner on May 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCubicPathGenerator
More file actions
142 lines (89 loc) · 3.97 KB
/
Copy pathCubicPathGenerator
File metadata and controls
142 lines (89 loc) · 3.97 KB
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
import numpy as np
import matplotlib.pyplot as plt
class FossenCubicSpline():
def __init__(self,x,y):
self.Wpx = x
self.Wpy = y
self.N = len(self.Wpx)
def Spline_generator(self,xstart,xfinal,ystart,yfinal):
N = len(self.Wpx)-1 # Toplam Denklem Sayısı
th = list(range(0,(N+1))) # theta values along the path
c1 = [3*th[0]**2,2*th[0],1,0]
c2 = [3*th[N]**2,2*th[N],1,0]
## Y vector generation accorting to number of waypoint
# xstart = self.Wpx[0]
# xfinal = self.Wpx[-1]
# ystart = self.Wpy[0]
# yfinal = self.Wpy[-1]
# y_x = np.zeros((4*N,1))
# y_y = np.zeros((4*N,1))
y_x = [xstart,self.Wpx[0]]
y_y = [ystart,self.Wpy[0]]
## Generate A Matrix
for k in range(1,N):
y_x.extend([self.Wpx[k],self.Wpx[k],0,0])
y_y.extend([self.Wpy[k],self.Wpy[k],0,0])
y_x.extend([self.Wpx[N],xfinal])
y_y.extend([self.Wpy[N],yfinal])
A = np.zeros((4*N,4*N))
A[0,0:4] = c1
A[1,0:4] = np.array([th[0]**3,th[0]**2,th[0],1])
A[4*N-1,(4*(N-1)):4*N] = c2
A[(4*N-1)-1,(4*(N-1)):4*N] = [th[N]**3,th[N]**2,th[N],1]
for j in range(1,N):
row = 4*(j-1)+2
col = 4*(j)
A[row,col-4:col] = [th[j]**3,th[j]**2,th[j],1]
A[row+1,col:col+4] = [th[j]**3,th[j]**2,th[j],1]
A[row+2,col-4:col] = [-3*th[j]**2,-2*th[j],-1,0]
A[row+2,col:col+4] = [3*th[j]**2,2*th[j],1,0]
A[row+3,col-4:col] = [-6*th[j],-2,0,0]
A[row+3,col:col+4] = [6*th[j],2,0,0]
x_x = np.matmul(np.linalg.inv(A),y_x)
x_y = np.matmul(np.linalg.inv(A),y_y)
self.Acoeff = np.zeros((N,4))
self.Bcoeff = np.zeros((N,4))
self.x_theta_one=np.zeros((N,4))
self.y_theta_one=np.zeros((N,4))
self.x_theta_two=np.zeros((N,4))
self.y_theta_two=np.zeros((N,4))
self.x_theta_three=np.zeros((N,4))
self.y_theta_three=np.zeros((N,4))
for m in range(0,N):
self.Acoeff[m,:] = x_x[4*m:4*m+4]
self.Bcoeff[m,:] = x_y[4*m:4*m+4]
self.x_theta_one[m,:] = [0,3*self.Acoeff[m,0],2*self.Acoeff[m,1],self.Acoeff[m,2]]
self.y_theta_one[m,:] = [0,3*self.Bcoeff[m,0],2*self.Bcoeff[m,1],self.Bcoeff[m,2]]
self.x_theta_two[m,:] = [0,0,6*self.Acoeff[m,0],2*self.Acoeff[m,1]]
self.y_theta_two[m,:] = [0,0,6*self.Bcoeff[m,0],2*self.Bcoeff[m,1]]
self.x_theta_three[m,:] = [0,0,0,6*self.Acoeff[m,0]]
self.y_theta_three[m,:] = [0,0,0,self.Bcoeff[m,0]]
table_x = [self.Acoeff,self.x_theta_one,self.x_theta_two,self.x_theta_three]
table_y = [self.Bcoeff,self.y_theta_one,self.y_theta_two,self.y_theta_three]
return table_x ,table_y
def curve_plot(self):
plt.plot(self.Wpx,self.Wpy,'go')
for i in range(0,self.N-1):
th = np.linspace(i,i+1)
plt.plot(np.polyval(self.Acoeff[i,:],th),np.polyval(self.Bcoeff[i,:],th))
plt.xlabel('X Position')
plt.ylabel('Y Position')
plt.show()
for i in range(0,self.N-1):
th = np.linspace(i,i+1)
plt.plot(th,np.polyval(self.x_theta_one[i,:],th))
plt.xlabel('Way Points ')
plt.ylabel('First Derivative of X')
plt.show()
for i in range(0,self.N-1):
th = np.linspace(i,i+1)
plt.plot(th,np.polyval(self.x_theta_two[i,:],th))
plt.xlabel('Way Points ')
plt.ylabel('Second Derivative of Y')
plt.show()
if __name__ == '__main__':
x = [0, 200, 400, 700, 1000]
y = [0, 600, 500, 400, 1200]
cubic = FossenCubicSpline(x,y)
X,Y=cubic.Spline_generator(10,0,190,0)
cubic.curve_plot()