-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolveDubins3d.m
280 lines (212 loc) · 8.7 KB
/
solveDubins3d.m
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
% Dubins Path 3D Modeling
% Lasted edited 6/25/2021 by Dr. Cynthia Sung and Lucien Peach
% Lasted edited 7/27/2021 by Wei-Hsi Chen
% Outputs row vector and two radian values
% Make sure that Od and Op are normalized prior to entry
function [tmin, theta1min, theta2min] = solveDubins3d(r, Od, Op)
% input params
od = Od(:, 4).';
ad = Od(:, 1).';
op = Op(:, 4).';
ap = Op(:, 1).';
% normalize 'velocity vectors'
ad = ad / norm(ad);
ap = ap / norm(ap);
% solve for optimal t = [tx, ty, tz]
T0 = od-op+randn(size(od));
% Initialize vectors for error and solutions
Tsol = zeros(4, 3);
Terror = zeros(4, 3);
% provides individual solutions
Tsol(1, :) = fsolve(@dubinspath1, T0); % + ... +
Tsol(2, :) = fsolve(@dubinspath2, T0); % - ... -
Tsol(3, :) = fsolve(@dubinspath3, T0); % + ... -
Tsol(4, :) = fsolve(@dubinspath4, T0); % - ... +
thetamatrix = zeros(4, 2);
ds = zeros(4, 1);
T_hat = zeros(4, 3);
for i = 1:4
T_normalized = norm(Tsol(i, :));
T_hat(i, :) = Tsol(i, :)/T_normalized;
% Find angle using arccos, the range of the output angle is (0, pi)
% thetamatrix(i, 1) = acos(dot(ap, T_hat(i, :)));
% thetamatrix(i, 2) = acos(dot(ad, T_hat(i, :)));
% Find angle using atan2, the range of the output angle is (-pi, pi)
thetamatrix(i, 1) = atan2(norm(cross(ap, T_hat(i, :))),dot(ap, T_hat(i, :)));
thetamatrix(i, 2) = atan2(norm(cross(T_hat(i, :), ad)),dot(T_hat(i, :), ad));
ds(i) = r*thetamatrix(i, 1) + T_normalized + r*thetamatrix(i, 2);
end
[minds, indexds] = min(ds);
disp(minds);
% Output minimum Tunittor as well as minimum theta1 and theta2 values
tmin = Tsol(indexds, :);
theta1min = thetamatrix(indexds, 1);
theta2min = thetamatrix(indexds, 2);
% double check the error is low
Terror(1, :) = dubinspath1(Tsol(1, :));
Terror(2, :) = dubinspath2(Tsol(2, :));
Terror(3, :) = dubinspath3(Tsol(3, :));
Terror(4, :) = dubinspath4(Tsol(4, :));
% Display solutions and error
disp(Tsol)
disp(Terror)
% Plotting
% --------------------------------------------------------------------
% decompose path in relevant points for plotting
[Cd1, Pd1, Cp1, Pp1] = decomposePath1(Tsol(1,:));
% plotting
hold on
subplot(2, 2, 1)
plotCirclePath(op, od, Cp1, Pp1, Cd1, Pd1, ap, ad, 'b', 'r');
axis equal
% decompose path in relevant points for plotting
[Cd2, Pd2, Cp2, Pp2] = decomposePath2(Tsol(2,:));
% plotting
hold on
subplot(2, 2, 2)
plotCirclePath(op, od, Cp2, Pp2, Cd2, Pd2, ap, ad, 'b', 'r')
axis equal
% decompose path in relevant points for plotting
[Cd3, Pd3, Cp3, Pp3] = decomposePath3(Tsol(3,:));
% plotting
hold on
subplot(2, 2, 3)
plotCirclePath(op, od, Cp3, Pp3, Cd3, Pd3, ap, ad, 'b', 'r')
axis equal
% decompose path in relevant points for plotting
[Cd4, Pd4, Cp4, Pp4] = decomposePath4(Tsol(4,:));
% plotting
hold on
subplot(2, 2, 4)
plotCirclePath(op, od, Cp4, Pp4, Cd4, Pd4, ap, ad, 'b', 'r')
axis equal
close all
% Functions
% ---------------------------------------------------------------------
function [Cd, Pd, Cp, Pp] = decomposePath1(T) % + ... +
% find relevant points based on T vector
Tnorm = norm(T);
Tunit = T/Tnorm;
wp = cross(ap, cross(Tunit, ap));
yp = cross(Tunit, cross(Tunit, ap));
Cp = op + r * wp / norm(wp); % center of starting circle
Pp = Cp - r * yp / norm(yp); % point of leaving starting circle
wd = -cross(ad, cross(Tunit, ad));
yd = -cross(Tunit, cross(Tunit, ad));
Cd = od + r * wd / norm(wd); % center of ending circle
Pd = Cd - r * yd / norm(yd); % point of entering ending circle
end
function [Cd, Pd, Cp, Pp] = decomposePath2(T) % - ... -
% find relevant points based on T vector
Tnorm = norm(T);
Tunit = T/Tnorm;
wp = -cross(ap, cross(Tunit, ap));
yp = -cross(Tunit, cross(Tunit, ap));
Cp = op + r * wp / norm(wp); % center of starting circle
Pp = Cp - r * yp / norm(yp); % point of leaving starting circle
wd = cross(ad, cross(Tunit, ad));
yd = cross(Tunit, cross(Tunit, ad));
Cd = od + r * wd / norm(wd); % center of ending circle
Pd = Cd - r * yd / norm(yd); % point of entering ending circle
end
function [Cd, Pd, Cp, Pp] = decomposePath3(T) % + ... -
% find relevant points based on T vector
Tnorm = norm(T);
Tunit = T/Tnorm;
wp = cross(ap, cross(Tunit, ap));
yp = cross(Tunit, cross(Tunit, ap));
Cp = op + r * wp / norm(wp); % center of starting circle
Pp = Cp - r * yp / norm(yp); % point of leaving starting circle
wd = cross(ad, cross(Tunit, ad));
yd = cross(Tunit, cross(Tunit, ad));
Cd = od + r * wd / norm(wd); % center of ending circle
Pd = Cd - r * yd / norm(yd); % point of entering ending circle
end
function [Cd, Pd, Cp, Pp] = decomposePath4(T) % - ... +
% find relevant points based on T vector
Tnorm = norm(T);
Tunit = T/Tnorm;
wp = -cross(ap, cross(Tunit, ap));
yp = -cross(Tunit, cross(Tunit, ap));
Cp = op + r * wp / norm(wp); % center of starting circle
Pp = Cp - r * yp / norm(yp); % point of leaving starting circle
wd = -cross(ad, cross(Tunit, ad));
yd = -cross(Tunit, cross(Tunit, ad));
Cd = od + r * wd / norm(wd); % center of ending circle
Pd = Cd - r * yd / norm(yd); % point of entering ending circle
end
%
% -------------------------------------------------------------------
% function err = dubinspath(T)
% % alternate formulation based just on vector math
% [Cd, Pd, Cp, Pp] = decomposePath(T);
% err = T - (Pp-Pd);
% end
function err = dubinspath1(T) % + ... +
% equations to solve
Tnorm = norm(T);
Tunit = T/Tnorm;
theta1 = atan2(norm(cross(ap,Tunit)),dot(ap,Tunit));
theta2 = atan2(norm(cross(Tunit,ad)),dot(Tunit,ad));
err = T + ...
r*(tan(theta1/2) + tan(theta2/2))*Tunit + ...
r*(tan(theta1/2)*ap + tan(theta2/2)*ad) + ...
- (od-op);
end
function err = dubinspath2(T) % - ... -
% equations to solve
Tnorm = norm(T);
Tunit = T/Tnorm;
theta1 = atan2(norm(cross(ap,Tunit)),dot(ap,Tunit));
theta2 = atan2(norm(cross(Tunit,ad)),dot(Tunit,ad));
err = T - ...
r*(tan(theta1/2) + tan(theta2/2))*Tunit - ...
r*(tan(theta1/2)*ap + tan(theta2/2)*ad) + ...
- (od-op);
end
function err = dubinspath3(T) % + ... -
% equations to solve
Tnorm = norm(T);
Tunit = T/Tnorm;
theta1 = atan2(norm(cross(ap,Tunit)),dot(ap,Tunit));
theta2 = atan2(norm(cross(Tunit,ad)),dot(Tunit,ad));
err = T + ...
r*(tan(theta1/2) - tan(theta2/2))*Tunit + ...
r*(tan(theta1/2)*ap - tan(theta2/2)*ad) + ...
- (od-op);
end
function err = dubinspath4(T) % - ... +
% equations to solve
Tnorm = norm(T);
Tunit = T/Tnorm;
theta1 = atan2(norm(cross(ap,Tunit)),dot(ap,Tunit));
theta2 = atan2(norm(cross(Tunit,ad)),dot(Tunit,ad));
err = T + ...
r*(-tan(theta1/2) + tan(theta2/2))*Tunit + ...
r*(-tan(theta1/2)*ap + tan(theta2/2)*ad) + ...
- (od-op);
end
% Plotting without overriding
function plotCirclePath(Op, Od, Cp, Pp, Cd, Pd, ap, ad, col1, col2)
hold on
plot3(Op(1),Op(2),Op(3), ['*' col2])
plot3(Op(1)+[0,r*ap(1)],Op(2)+[0,r*ap(2)],Op(3)+[0,r*ap(3)],['-' col2])
plot3(Cp(1),Cp(2),Cp(3), ['*' col2])
plot3(Pp(1),Pp(2),Pp(3),'*g')
n = cross(Op-Cp, Pp-Cp);
v = null(n);
theta = linspace(0,2*pi,50);
circle_pts = bsxfun(@plus, Cp(:), r * (v(:,1)*cos(theta) + v(:,2)*sin(theta)));
plot3(circle_pts(1,:),circle_pts(2,:),circle_pts(3,:), ['--' col2]);
plot3(Od(1),Od(2),Od(3), ['*' col1])
plot3(Od(1)+[0,r*ad(1)],Od(2)+[0,r*ad(2)],Od(3)+[0,r*ad(3)],['-' col1])
plot3(Cd(1),Cd(2),Cd(3), ['*' col1])
plot3(Pd(1),Pd(2),Pd(3),'*g')
n = cross(Od-Cd, Pd-Cd);
v = null(n);
theta = linspace(0,2*pi,50);
circle_pts = bsxfun(@plus, Cd(:), r * (v(:,1)*cos(theta) + v(:,2)*sin(theta)));
plot3(circle_pts(1,:),circle_pts(2,:),circle_pts(3,:), ['--' col1]);
plot3([Pd(1) Pp(1)], [Pd(2) Pp(2)], [Pd(3) Pp(3)],'-g')
end
end