-
Notifications
You must be signed in to change notification settings - Fork 23
/
dmpc_soft_bound.m
369 lines (326 loc) · 9.58 KB
/
dmpc_soft_bound.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
clc
clear all
close all
warning('off','all')
% Time settings and variables
T = 20; % Trajectory final time
h = 0.2; % time step duration
tk = 0:h:T;
K = T/h + 1; % number of time steps
Ts = 0.01; % period for interpolation @ 100Hz
t = 0:Ts:T; % interpolated time vector
k_hor = 15; % horizon length
% Variables for ellipsoid constraint
order = 2; % choose between 2 or 4 for the order of the super ellipsoid
rmin = 0.5; % X-Y protection radius for collisions
c = 1.5; % make this one for spherical constraint
E = diag([1,1,c]);
E1 = E^(-1);
E2 = E^(-order);
N = 4; % number of vehicles
% Workspace boundaries
pmin = [-2.5,-2.5,0.2];
pmax = [2.5,2.5,2.2];
% pmin = [-1.0,-1.0,0.2];
% pmax = [1.0,1.0,2.2];
% % Workspace boundaries
% pmin = [-5,-5,0.2];
% pmax = [5,5,5];
% Minimum distance between vehicles in m
rmin_init = 0.75;
% Initial positions
% [po,pf] = randomTest(N,pmin,pmax,rmin_init);
% Initial positions
po1 = [1.501,1.5,1.5];
po2 = [-1.5,-1.5,1.5];
po3 = [-1.5,1.5,1.5];
po4 = [1.5,-1.5,1.5];
po = cat(3,po1,po2,po3,po4);
% Final positions
pf1 = [-1.5,-1.5,1.5];
pf2 = [1.5,1.5,1.5];
pf3 = [1.5,-1.5,1.5];
pf4 = [-1.5,1.5,1.5];
pf = cat(3,pf1,pf2,pf3,pf4);
%% Solving the problem
l = [];
p = [];
v = [];
a = [];
pk = [];
vk = [];
ak = [];
success = 0; %check if QP was feasible
at_goal = 0; %At the end of solving, makes sure every agent arrives at the goal
error_tol = 0.05; % 5cm destination tolerance
violation = 0; % checks if violations occured at end of algorithm
outbound = 0;
coll = 0;
term = -5*10^4;
% Penalty matrices when there're predicted collisions
Q = 1000;
S = 100;
% Maximum acceleration in m/s^2
alim = 1.0;
% Some pre computations
A = getPosMat(h,k_hor);
Aux = [1 0 0 h 0 0;
0 1 0 0 h 0;
0 0 1 0 0 h;
0 0 0 1 0 0;
0 0 0 0 1 0;
0 0 0 0 0 1];
b = [h^2/2*eye(3);
h*eye(3)];
prev_row = zeros(6,3*k_hor); % For the first iteration of constructing matrix Ain
A_initp = [];
A_init = eye(6);
A_p = [];
A_v = [];
Delta = getDeltaMat(k_hor);
for k = 1:k_hor
add_b = [zeros(size(b,1),size(b,2)*(k-1)) b zeros(size(b,1),size(b,2)*(k_hor-k))];
new_row = Aux*prev_row + add_b;
A_p = [A_p; new_row(1:3,:)];
A_v = [A_v; new_row(4:6,:)];
prev_row = new_row;
A_init = Aux*A_init;
A_initp = [A_initp; A_init(1:3,:)];
end
failed_goal = 0; %how many times the algorithm failed to reach goal
tries = 1; % how many iterations it took the DMPC to find a solution
tic % measure the time it gets to solve the optimization problem
pred = [];
moreconstr = [];
for k = 1:K
for n = 1:N
if k==1
poi = po(:,:,n);
pfi = pf(:,:,n);
[pi,vi,ai] = initDMPC(poi,pfi,h,k_hor,K);
success = 1;
else
pok = pk(:,k-1,n);
vok = vk(:,k-1,n);
aok = ak(:,k-1,n);
[pi,vi,ai,success,outbound,coll] = solveSoftDMPCbound(pok',pf(:,:,n),vok',aok',n,h,l,k_hor,rmin,pmin,pmax,alim,A,A_initp,A_p,A_v,Delta,Q,S,E1,E2,order,term);
end
if (~success || outbound || coll) %problem was infeasible, exit and retry
break;
end
new_l(:,:,n) = pi;
pk(:,k,n) = pi(:,1);
vk(:,k,n) = vi(:,1);
ak(:,k,n) = ai(:,1);
end
if ~success %Heuristic: increase Q, make init more slowly,
if outbound
fprintf("Failed - problem unfeasible, vehicle couldn't stay in workspace @ k_T = %i, n = %i\n",k,n)
elseif coll
fprintf("Failed - collision detected @ k_T = %i by vehicle n = %i\n",k,n);
else
fprintf("Failed - problem unfeasible @ k_T = %i, n = %i\n",k,n)
end
break;
end
l = new_l;
pred(:,:,:,k) = l;
end
pass = 0;
if success
pass = ReachedGoal(pk,pf,K,error_tol,N); %check if agents reached goal
end
if success && pass
at_goal = 1;
elseif success && ~pass %if not at goal, retry with more aggressive behaviour
failed_goal = failed_goal + 1;
fprintf("Failed - Did not reach goal: trial #%i\n",tries-1)
end
passed = success && at_goal %DMPC was successful or not
toc
if passed
% Interpolate for better resolution
for i = 1:N
p(:,:,i) = spline(tk,pk(:,:,i),t);
v(:,:,i) = spline(tk,vk(:,:,i),t);
a(:,:,i) = spline(tk,ak(:,:,i),t);
end
% Check if collision constraints were not violated
for i = 1:N
for j = 1:N
if(i~=j)
differ = E1*(p(:,:,i) - p(:,:,j));
dist = (sum(differ.^order,1)).^(1/order);
if min(dist) < (rmin - 0.05)
[value,index] = min(dist);
violation = 1;
fprintf("Collision constraint violated after interpolation by %.2fcm: vehicles %i and %i @ k = %i \n", (rmin -value)*100,i,j,index)
end
end
end
end
if ~violation
fprintf("No collisions found! Successful computation\n")
end
% Calculate how much time is required to complete the transition
% within a 5cm margin of the goal
for i = 1:N
differ = p(:,:,i) - repmat(pf(:,:,i),length(t),1)';
dist = sqrt(sum(differ.^2,1));
hola = find(dist >= 0.05,1,'last');
if isempty(hola)
time_index(i) = 0;
else
time_index(i) = hola + 1;
end
end
max_time_index = max(time_index);
fprintf("The trajectory can be completed in %.2f seconds\n",max_time_index*Ts);
totdist_dmpc = sum(sum(sqrt(diff(p(1,:,:)).^2+diff(p(2,:,:)).^2+diff(p(3,:,:)).^2)));
fprintf("The sum of trajectory length is %.2f\n",totdist_dmpc);
end
%%
figure(1)
colors = distinguishable_colors(N);
set(gcf, 'Position', get(0, 'Screensize'));
set(gcf,'currentchar',' ')
while get(gcf,'currentchar')==' '
for i = 1:N
h_line(i) = animatedline('LineWidth',2,'Color',colors(i,:),'LineStyle',':');
end
for k = 1:K
for i = 1:N
clearpoints(h_line(i));
addpoints(h_line(i),pred(1,:,i,k),pred(2,:,i,k),pred(3,:,i,k));
hold on;
grid on;
xlim([pmin(1),pmax(1)])
ylim([pmin(2),pmax(2)])
zlim([0,pmax(3)])
plot3(pk(1,k,i),pk(2,k,i),pk(3,k,i),'o',...
'LineWidth',2,'Color',colors(i,:));
plot3(po(1,1,i), po(1,2,i), po(1,3,i),'^',...
'LineWidth',2,'Color',colors(i,:));
plot3(pf(1,1,i), pf(1,2,i), pf(1,3,i),'x',...
'LineWidth',2,'Color',colors(i,:));
end
drawnow
end
clf
pause(0.5)
end
%% Plotting
L = length(t);
colors = distinguishable_colors(N);
for i = 1:N
figure(1);
h_plot(i) = plot3(p(1,:,i), p(2,:,i), p(3,:,i), 'LineWidth',1.5,...
'Color',colors(i,:));
h_label{i} = ['Vehicle #' num2str(i)];
hold on;
grid on;
xlim([pmin(1),pmax(1)])
ylim([pmin(2),pmax(2)])
zlim([0,pmax(3)])
xlabel('x[m]')
ylabel('y[m]');
zlabel('z[m]')
plot3(po(1,1,i), po(1,2,i), po(1,3,i),'x',...
'LineWidth',3,'Color',colors(i,:));
% plot3(pf(1,1,i), pf(1,2,i), pf(1,3,i),'x',...
% 'LineWidth',5,'Color',colors(i,:));
figure(2)
diff = p(:,:,i) - repmat(pf(:,:,i),length(t),1)';
dist = sqrt(sum(diff.^2,1));
plot(t, dist, 'LineWidth',1.5);
grid on;
hold on;
xlabel('t [s]')
ylabel('Distance to target [m]');
figure(3)
subplot(3,1,1)
plot(t,p(1,:,i),'LineWidth',1.5);
plot(t,pmin(1)*ones(length(t),1),'--r','LineWidth',1.5);
plot(t,pmax(1)*ones(length(t),1),'--r','LineWidth',1.5);
ylabel('x [m]')
xlabel ('t [s]')
grid on;
hold on;
subplot(3,1,2)
plot(t,p(2,:,i),'LineWidth',1.5);
plot(t,pmin(2)*ones(length(t),1),'--r','LineWidth',1.5);
plot(t,pmax(2)*ones(length(t),1),'--r','LineWidth',1.5);
ylabel('y [m]')
xlabel ('t [s]')
grid on;
hold on;
subplot(3,1,3)
plot(t,p(3,:,i),'LineWidth',1.5);
plot(t,pmin(3)*ones(length(t),1),'--r','LineWidth',1.5);
plot(t,pmax(3)*ones(length(t),1),'--r','LineWidth',1.5);
ylabel('z [m]')
xlabel ('t [s]')
grid on;
hold on;
figure(4)
subplot(3,1,1)
plot(t,v(1,:,i),'LineWidth',1.5);
ylabel('vx [m/s]')
xlabel ('t [s]')
grid on;
hold on;
subplot(3,1,2)
plot(t,v(2,:,i),'LineWidth',1.5);
ylabel('vy [m/s]')
xlabel ('t [s]')
grid on;
hold on;
subplot(3,1,3)
plot(t,v(3,:,i),'LineWidth',1.5);
ylabel('vz [m/s]')
xlabel ('t [s]')
grid on;
hold on;
figure(5)
subplot(3,1,1)
plot(t,a(1,:,i),'LineWidth',1.5);
plot(t,alim*ones(length(t),1),'--r','LineWidth',1.5);
plot(t,-alim*ones(length(t),1),'--r','LineWidth',1.5);
ylabel('ax [m/s]')
xlabel ('t [s]')
grid on;
hold on;
subplot(3,1,2)
plot(t,a(2,:,i),'LineWidth',1.5);
plot(t,alim*ones(length(t),1),'--r','LineWidth',1.5);
plot(t,-alim*ones(length(t),1),'--r','LineWidth',1.5);
ylabel('ay [m/s]')
xlabel ('t [s]')
grid on;
hold on;
subplot(3,1,3)
plot(t,a(3,:,i),'LineWidth',1.5);
plot(t,alim*ones(length(t),1),'--r','LineWidth',1.5);
plot(t,-alim*ones(length(t),1),'--r','LineWidth',1.5);
ylabel('az [m/s]')
xlabel ('t [s]')
grid on;
hold on;
end
%%
figure(6)
for i = 1:N
for j = 1:N
if(i~=j)
differ = E1*(pk(:,:,i) - pk(:,:,j));
dist = (sum(differ.^order,1)).^(1/order);
plot(tk, dist, 'LineWidth',1.5);
grid on;
hold on;
xlabel('t [s]')
ylabel('Inter-agent distance [m]');
end
end
end
plot(tk,rmin*ones(length(tk),1),'--r','LineWidth',1.5);
% legend(h_plot,h_label);