-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdistr_dyn.m
280 lines (237 loc) · 6.89 KB
/
distr_dyn.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
function [xxdot] = distr_dyn(t,xx,par)
% state
% x = [position velocity]
global flag33 flag50 flag90
dt = par.dt;
nAg = par.nAg;
M = par.DIM;
QBp = par.QBp;
QBdp = par.QBdp;
R = par.R;
kF = par.kF;
kA = par.kA;
qA = par.qA;
Xdes = par.Xdes;
dijs = par.dijs;
tl = par.tl;
kr = par.kr;
ka = par.ka;
G = par.G;
T0 = par.T0;
dist_coeff = par.dist_coeff;
max_dist = par.max_dist;
gains = par.gains;
gammaOBS_P = par.gammaOBS_P;
gammaOBS_D = par.gammaOBS_D;
% alph = par.alpha; % must contain two values in (0,1)
% if the toplogy remains constant:
% F = par.F;
% if the topology does not remain constant:
% A = adjacency(G);
% for i = 2:nAg
% p_i = x((i-1)*M+1:(i-1)*M+M);
% for j = 1:i-1
% if dijs(i,j) >= 0 && dijs(i,j) < dist_coeff*max_dist
% p_j = x((j-1)*M+1:(j-1)*M+M);
% if A(i,j) == 1 && norm(p_i-p_j) > dist_coeff*max_dist
% A(i,j) = 0;
% A(j,i) = 0;
% end
% if A(i,j) == 0 && norm(p_i-p_j) <= dist_coeff*max_dist
% A(i,j) = 1;
% A(j,i) = 1;
% end
% end
% end
% end
% G = graph(A);
% degs = sum(A);
% D = diag(degs);
% I_nAg = eye(nAg);
% I_M = eye(M);
% F = kron((D+I_nAg)^-1*(A+I_nAg),I_M);
% alpha = (1-t/tl)*alph(1) + (t/tl)*alph(2);
% alpha = abs(alph(1)-(alph(1)-alph(2))*(1-exp(-t*10)));
tt = 1+floor(t/dt);
NI = nAg*M;
NS = NI*2;
xdot = zeros(NS,1);
% online rigidity analysis
% fprintf(num2str(tt))
% fprintf(' ')
% fprintf(num2str(t))
% fprintf(' ')
% rigidity_analysis(G,x(1:nAg*M)',nAg,M)
if t/tl > 0.33 && ~flag33
fprintf('> 33%%\n')
flag33 = 1;
end
if t/tl > 0.50 && ~flag50
fprintf('> 50%%\n')
flag50 = 1;
end
if t/tl > 0.90 && ~flag90
fprintf('> 90%%\n')
flag90 = 1;
end
% preprocessing: consensus to get centroid info
pBdes = Xdes(tt,1:M)';
dpBdes = Xdes(tt,M+1:M+M)';
% % % pB = xx(1:nAg*M);
% % % MSEpB = zeros(1,nAg);
% % % dpB = xx(nAg*M+1:2*nAg*M);
% % % MSEdpB = zeros(1,nAg);
% % % k = 1;
% % % done = 0;
% % % while ~done && k <= 100
% % % pB = F*pB;
% % % dpB = F*dpB;
% % % % computing the disagreement
% % % for i = 1:nAg
% % % Ni = neighbors(G,i);
% % % MSEpB(i) = 0;
% % % MSEdpB(i) = 0;
% % % pBi = pB((i-1)*M+1:(i-1)*M+M);
% % % dpBi = dpB((i-1)*M+1:(i-1)*M+M);
% % % for jj = 1:length(Ni)
% % % j = Ni(jj);
% % % pBj = pB((j-1)*M+1:(j-1)*M+M);
% % % dpBj = dpB((j-1)*M+1:(j-1)*M+M);
% % % MSEpB(i) = MSEpB(i) + (pBi-pBj)'*(pBi-pBj);
% % % MSEdpB(i) = MSEdpB(i) + (dpBi-dpBj)'*(dpBi-dpBj);
% % % end
% % % end
% % % i = 1;
% % % done = 1;
% % % while i <= nAg && done
% % % if MSEpB(i) > 10^-8 || MSEdpB(i) > 10^-8
% % % done = 0;
% % % end
% % % i = i+1;
% % % end
% % % k = k+1;
% % % end
p_hat = zeros(NI,nAg);
dp_hat = zeros(NI,nAg);
% velocity integrator ---> pdot = v
xdot(1:nAg*M) = xx(nAg*M+1:2*nAg*M);
% input to be assigned to the velocity ---> vdot = u
I_M = eye(M);
kP_tr = gains(1);
kD_tr = gains(2);
kP_fo = gains(3);
kD_fo = gains(4);
kP_al = gains(5);
NI2 = NI*nAg;
for i = 1:nAg
Ni = neighbors(G,i);
deg_i = length(Ni);
formation_i = zeros(M,1);
alignment_i = zeros(M,1);
dformation_i = zeros(M,1);
intval_i = (i-1)*M+1:(i-1)*M+M;
p_i = xx(intval_i);
dp_i = xx((nAg+i-1)*M+1:(nAg+i-1)*M+M);
p_hat(:,i) = xx(NS+(i-1)*NI+1:NS+(i-1)*NI+NI);
dp_hat(:,i) = xx(NI2+NS+(i-1)*NI+1:NI2+NS+(i-1)*NI+NI);
for jj = 1:deg_i
j = Ni(jj);
dij = dijs(i,j);
p_j = xx((j-1)*M+1:(j-1)*M+M);
dp_j = xx((nAg+j-1)*M+1:(nAg+j-1)*M+M);
eij = p_i-p_j;
deij = dp_i-dp_j;
sij = eij'*eij;
sig1_ij = sigma(sij,dij,1,kr,ka);
sig2_ij = sigma(sij,dij,2,kr,ka);
sig1_h = sig1_ij > 0;
if 0
sig1_h = 1;
end
newt_ij = 1; % pinv(2*sig2_ij*(eij*eij')+sig1_h*sig1_ij*I_M);
% if sig1_h
% newt_ij = ka*newt_ij;
% else
% newt_ij = kr*newt_ij;
% end
%newt_ij = newt_ij/norm(newt_ij,2);
formation_i = formation_i + kF*newt_ij*sig1_ij*eij;
alignment_i = alignment_i + kA*qA(i,j)*deij; %sig1_h*
dformation_i = dformation_i + kF*(2*sig2_ij*(eij*eij')+sig1_h*sig1_ij*I_M)*deij;
%dformation_i = dformation_i + kF*(2*sig2_ij*(eij*eij')+sig1_ij*I_M)*deij;
end
% acceleration integrator ---> pddot = u
pBhat = zeros(M,1);
dpBhat = zeros(M,1);
for k = 1:nAg
pBhat = pBhat + p_hat((k-1)*M+1:(k-1)*M+M,i)/nAg;
dpBhat = dpBhat + dp_hat((k-1)*M+1:(k-1)*M+M,i)/nAg;
end
xdot(nAg*M+intval_i) = -(R(intval_i,intval_i)^-1)*...
(kP_tr*QBp*(pBhat-pBdes) +...
kD_tr*QBdp*(dpBhat-dpBdes) +...
kP_fo*formation_i +...
kD_fo*dformation_i +...
kP_al*alignment_i);
%[norm(mean(p_hat(:,i))-pBdes) norm(mean(dp_hat(:,i))-dpBdes)]
%pause
end
% checking saturation
u_sat = 50;
flag_t = 0;
for j = 1:nAg*M
u_j = xdot(nAg*M+j);
if abs(u_j) > u_sat
xdot(nAg*M+j) = sign(u_j)*u_sat;
if ~flag_t
fprintf(strcat('Saturation for t =',num2str(t),'\n'))
flag_t = 1;
end
end
end
% in this example the artificial saturation works from t = 0
% to t = 0.35726, since the initial conditions for u is inappropriate
% observer for the centroid
up_obs = zeros(2*NI2,1);
for i = 1:nAg
Ni = neighbors(G,i);
intval_i = (i-1)*NI+1:(i-1)*NI+NI;
idp = zeros(NI,1);
iu = zeros(NI,1);
intval_ii = (i-1)*M+1:(i-1)*M+M;
idp(intval_ii) = xdot(intval_ii);
iu(intval_ii) = xdot(NI+intval_ii);
for j_ = 1:length(Ni)
j = Ni(j_);
intval_jj = (j-1)*M+1:(j-1)*M+M;
up_obs(intval_i) = up_obs(intval_i) +...
p_hat(:,i)-p_hat(:,j);
up_obs(NI2+intval_i) = up_obs(NI2+intval_i) +...
dp_hat(:,i)-dp_hat(:,j);
idp(intval_jj) = xdot(intval_jj);
iu(intval_jj) = xdot(NI+intval_jj);
end
notNi = setdiff(1:nAg,Ni);
for j_ = 1:length(notNi)
j = Ni(j_);
intval_jj = (j-1)*M+1:(j-1)*M+M;
idp(intval_jj) = dp_hat(intval_jj,i);
end
% for j = 1:nAg
% intval_jj = (j-1)*M+1:(j-1)*M+M;
% idp(intval_jj) = xdot(intval_jj);
% iu(intval_jj) = xdot(NI+intval_jj);
% end
projp = zeros(NI,1);
projdp = zeros(NI,1);
projp(intval_ii) = p_hat(intval_ii,i)-xx(intval_ii);
projdp(intval_ii) = dp_hat(intval_ii,i)-xx(NI+intval_ii);
up_obs(intval_i) = -gammaOBS_P*(up_obs(intval_i) + projp) + idp;
%up_obs(intval_i) = dp_hat(:,i);
up_obs(NI2+intval_i) = -gammaOBS_D*(up_obs(NI2+intval_i) + projdp) + iu; %
end
% final update
% pdot = xdot(1:NI);
% dpdot = xdot(NI+1:NS);
xxdot = [xdot; up_obs]; %kron(ones(NI,1),pdot); kron(ones(NI,1),dpdot)];
end