-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrajectory.py
More file actions
155 lines (142 loc) · 4.42 KB
/
Copy pathtrajectory.py
File metadata and controls
155 lines (142 loc) · 4.42 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
143
144
145
146
147
148
149
150
151
152
153
154
155
import math
import matplotlib
import matplotlib.pyplot as plt
from numpy import array, dot
from math import acos
from cmath import phase, exp
from numpy.linalg import norm
class Trajectory:
def __init__(self, p):
self.p = p
self.reset()
def update(self, d):
assert d >= 0
if self.pos >= len(self.p)-1:
return self.x, self.y
xDis = self.p[self.pos+1][0] - self.x
yDis = self.p[self.pos+1][1] - self.y
Dis = (xDis**2+yDis**2)**(0.5)
if d >= Dis:
d = d - Dis
self.x, self.y = self.p[self.pos+1]
self.pos += 1
return self.update(d)
else:
frac = d/Dis
self.x = xDis * frac + self.x
self.y = yDis * frac + self.y
return self.x, self.y
def error(self, x, y, theta):
min_distance_error=float('inf')
min_waypoint_distance=float('inf')
i=self.waypoint+1
q=array([self.p[i][0]-self.p[i-1][0], self.p[i][1]-self.p[i-1][1]])
c=array([x-self.p[i-1][0], y-self.p[i-1][1]])
togo=array([x-self.p[i][0], y-self.p[i][1]])
waypoint_distance = norm(togo)
if norm(c) == 0:
min_distance_error = 0
else:
proj_q_c = (dot(q,c)/norm(q)) * (q/norm(q))
distance_error = norm(c-proj_q_c)
min_distance_error = distance_error
orientation_vector=exp(1j*theta)
q=q[0]+1j*q[1]
c=c[0]+1j*c[1]
if c == 0 or (q/c).imag < 0:
min_distance_error *= -1
theta_error = phase(q/orientation_vector)
# this should really be a configurable tolerance
if waypoint_distance < 3: self.waypoint = min(self.waypoint+1,len(self.p)-2)
return min_distance_error,theta_error
def done(self):
return self.pos >= len(self.p)-1
def reset(self):
self.x = self.p[0][0]
self.y = self.p[0][1]
self.pos = 0
self.waypoint = 0
def copy(self):
return Trajectory(self.p)
if __name__ == '__main__':
from car import Car
#case 1
c = Car(0., 0., 0., 1.)
xList = []
yList = []
for i in range(10):
delta_theta = math.pi/2
c.update(delta_theta)
xList.append(c.x)
yList.append(c.y)
p = zip(xList, yList)
traj = Trajectory(p)
d = Car(0., 0., 0., 1.)
for i in range(10):
derr, therr = traj.error(d.x, d.y, d.angle)
print "case 1:", "%.2f, %.2f" % (derr, therr)
traj.update(1)
d.update(delta_theta)
#
# #case 2
# c = Car(0., 0., 0., 1.)
# xList = [0]
# yList = [0]
# for i in range(10):
# delta_theta = math.pi/2
# c.update(delta_theta)
# xList.append(c.x)
# yList.append(c.y)
# p = zip(xList, yList)
# traj = Trajectory(p)
# d = Car(0., 0., 0., .5)
#
# for i in range(10):
# derr, therr = traj.error(d.x, d.y, d.angle)
# print "case 2:", "%.2f, %.2f" % (derr, therr)
# traj.update(1)
# d.update(delta_theta)
# #case 3
# c = Car(0., 0., 0., 1.)
# xList = [0]
# yList = [0]
# for i in range(10):
# delta_theta = 0
# c.update(delta_theta)
# xList.append(c.x)
# yList.append(c.y)
# p = zip(xList, yList)
# traj = Trajectory(p)
# d = Car(math.pi/4, 0., 0., 1.)
#
# for i in range(5):
# derr, therr = traj.error(d.x, d.y, d.angle)
# print "case 3:", "%.2f, %.2f" % (derr, therr)
# traj.update(1)
# d.update(-math.pi/2)
# derr, therr = traj.error(d.x, d.y, d.angle)
# print "case 3:", "%.2f, %.2f" % (derr, therr)
# traj.update(1)
# d.update(math.pi/2)
# #case 4
# c = Car(math.pi, 0., 0., 1.)
# xList = [0]
# yList = [0]
# for i in range(10):
# delta_theta = 0
# c.update(delta_theta)
# xList.append(c.x)
# yList.append(c.y)
# p = zip(xList, yList)
# traj = Trajectory(p)
# d = Car(math.pi/4, 0., 0., 1.)
#
# for i in range(5):
# derr, therr = traj.error(d.x, d.y, d.angle)
# print "case 4:", "%.2f, %.2f" % (derr, therr)
# traj.update(1)
# d.update(-math.pi/2)
# derr, therr = traj.error(d.x, d.y, d.angle)
# print "case 4:", "%.2f, %.2f" % (derr, therr)
# traj.update(1)
# d.update(math.pi/2)