-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCubicSplines.py
More file actions
127 lines (94 loc) · 3.7 KB
/
CubicSplines.py
File metadata and controls
127 lines (94 loc) · 3.7 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
import matplotlib.pyplot as plt
import numpy as np
import math
RESOLUTION = 0.01
ROBOT_WIDTH = 1.0;
class Waypoint():
def __init__(self, x_, y_, heading_):
self.x = x_
self.y = y_
#Convert heading from degrees to slope
self.heading = math.tan(math.radians(heading_))
waypoints = []
# waypoints.append(Waypoint(0, 0, 45))
# waypoints.append(Waypoint(1, 5, 30.0))
# waypoints.append(Waypoint(2, 2, 0.0))
# waypoints.append(Waypoint(8, 8, -45))
waypoints.append(Waypoint(0, 0, 0))
waypoints.append(Waypoint(5, 0, 0))
waypoints.append(Waypoint(10, 0, 0))
waypoints.append(Waypoint(15, 0, 45))
waypoints.append(Waypoint(17, 2, 45))
#Length is 1 less than amount of points
#Stores an array of the constants for each spline
splines = []
def f(x, X):
#Function to graph that fits the data
return X[0,0] + (x * X[1,0]) + (X[2,0] * x ** 2) + (X[3,0] * x ** 3)
def df(x, X):
#Function to graph the heading
return X[1,0] + 2*(X[2,0] * x) + 3*(X[3,0] * x ** 2)
def parallelCurveY(x1, x2, X, d):
return f(x1, X) - (d / ((1 + df(x1, X) ** 2) ** (0.5)))
def parallelCurveX(x, d, X):
return x + (d * df(x, X)) / ((1 + df(x, X) ** 2) ** 0.5)
#Plots the spline
def plotSpline(section, startingPoint, endingPoint):
#plt.plot(x, f(x, X))
x = np.arange(startingPoint.x, endingPoint.x + RESOLUTION, RESOLUTION)
plt.plot(x, f(x, section))
#Plots the spline's heading
def plotHeading(section, startingPoint, endingPoint):
x = np.arange(startingPoint.x, endingPoint.x + RESOLUTION, RESOLUTION)
plt.plot(x, df(x, section))
#Plots parallel curve
def plotParallelCurve(section, startingPoint, endingPoint, d):
x1 = np.arange(startingPoint.x, endingPoint.x + RESOLUTION, RESOLUTION)
x2 = parallelCurveX(x1, d, section)
plt.plot(x2, parallelCurveY(x1, x2, section, d))
def calcLengthCurve(section, startX, endX):
length = 0;
xValues = np.arange(startX, endX + RESOLUTION, RESOLUTION);
yValues = f(xValues, section)
for i in range(len(xValues) - 1):
length += ((xValues[i + 1] - xValues[i]) ** 2 + (yValues[i + 1] - yValues[i]) ** 2) ** 0.5
return length
#Points are waypoints
def calculateSpline(previousPoint, currentPoint):
#System equations:
# y(start) = a + b * x(start) + c * x(start)^2 + d * x(start)^3
# y(end) = a + b * x(end) + c * x(end)^2 + d * x(end)^3
# heading(start) -> slope = b + ((2*c) * x(start)) + ((3d) * x(start)^2)
# heading(end) -> slope = b + ((2*c) * x(end)) + ((3d) * x(end)^2)
A = np.matrix([[1, previousPoint.x, previousPoint.x ** 2, previousPoint.x ** 3],
[1, currentPoint.x, currentPoint.x ** 2, currentPoint.x ** 3],
[0, 1, previousPoint.x * 2, 3 * previousPoint.x ** 2],
[0, 1, currentPoint.x * 2, 3 * currentPoint.x ** 2]])
B = np.matrix([previousPoint.y, currentPoint.y, previousPoint.heading, currentPoint.heading])
# X = B * A^-1
X = np.linalg.inv(A)
# print X, "\n\n"
# print B, "\n\n"
X = np.dot(X, np.transpose(B))
return X
xRaw = [point.x for point in waypoints]
yRaw = [point.y for point in waypoints]
plt.figure(1)
plt.scatter(xRaw, yRaw)
plt.ylabel('Data')
#Calculate the splines between waypoints and add to splines array
for i in range(1, len(waypoints)):
splines.append(calculateSpline(waypoints[i - 1], waypoints[i]))
totalSplineLength = 0
leftSplineLength = 0
rightSplineLength = 0
#plot splines
for i in range(0, len(waypoints) - 1):
# plotSpline(splines[i], waypoints[i], waypoints[i + 1])
plotParallelCurve(splines[i], waypoints[i], waypoints[i + 1], ROBOT_WIDTH / 2.0)
plotParallelCurve(splines[i], waypoints[i], waypoints[i + 1], -ROBOT_WIDTH / 2.0)
# plotHeading(splines[i], waypoints[i], waypoints[i + 1])
#Calculate length of splines
totalSplineLength += calcLengthCurve(splines[i], waypoints[i].x, waypoints[i + 1].x)
print "Length: ", totalSplineLength
plt.show()