-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSimulation.m
183 lines (170 loc) · 5.91 KB
/
Simulation.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
% Copyright (c) 2020, Nicolò Bargellesi, Luca Facin & Lorenzo Marchini
%
% This source code is licensed under the MIT-style license found in the
% LICENSE file in the root directory of this source tree.
%
clear
close all
clc
%% Coordinated Surveillance
N = 200; % duration
Npre = 500; % duration transient
Simulation_preprocessing; % load data
% SEBS Initialization
Np = NoC;
s1 = round(0.5/Tc);
env.IvReset();
env.heatReset();
d = zeros(NoC,2);
detected = false;
for t = 1:Npre
S = zeros(1,length(env.V)); % Initialize vertex intentions
for n = randperm(NoC)
% SEBS
if t/s1 == round(t/s1)
v(n) = C(n).SEBS(env,Np,S,L,M); % Select next step
d(n,:) = env.Vm(v(n),:) - C(n).X(1:2);
% Share Intention
view = env.A(env.Vmap(v(n)),:);
view(1,env.Vmap(v(n))) = 1;
% Simulate transmission errors
if randi([0,100])/100 > e_tx
S(logical(view)) = S(logical(view)) + 1;
end
end
end
% MOVE
for n = 1:NoC
C(n).V = v(n);
C(n).X(1:2) = C(n).X(1:2) + d(n,:)/s1;
% Simulate transmission errors
if randi([0,100])/100 > e_tx
env.IvUpdate(t*Tc,C(n).V) % Update Idleness
env.deheat(C(n).V) % Deheat viewed vertices
end
end
end
% Tracking Control Initialization
nL = 0;
s = round(T/Tc);
realZ = [];
hctrl.h0 = C(1).zMax;
hctrl.vh0 = 0;
% open Simulink model
open_system('control.slx');
figure()
for t = Npre+1:N+Npre
env.plotBorders();
hold on
% TARGET TRAJECTORY
if t > Npre+50
x = (Ac*x' + Qc*randn(4,1))'; % New Trajectory point
% verify if new trajectory point is inside our environment
in = inpolygon(x(1),x(2),B(:,1),B(:,2));
in1 = inpolygon(x(1),x(2),NFZ2{1}(:,1),NFZ2{1}(:,2));
in2 = inpolygon(x(1),x(2),NFZ2{2}(:,1),NFZ2{2}(:,2));
if in
scatter(x(1),x(2),30,[0.8500, 0.3250, 0.0980],'filled')
end
% DETECTION
firstStep = false;
for n = 1:NoC
if (abs(C(n).X(1)-x(1)) < C(n).FoV && abs(C(n).X(2)-x(2)) < C(n).FoV && ~detected && in && ~in1 && ~in2)
C(n).Task = 1;
Np = Np-1;
env.addHeat(3,1,1,C(n).V);
detected = true;
firstStep = true;
tDetect = t;
% Initialize Filter
r = C(n).FoV*C(n).eFoV;
R = r^2*eye(2);
y = (H*x' + sqrt(R)*randn(2,1))';
x_pred = [y,0 0];
P_pred = blkdiag(R,eye(2));
d(n,:) = x_pred(1:2) - C(n).X(1:2);
break
end
end
end
S = zeros(1,length(env.V)); % Initialize vertex intentions
for n = randperm(NoC)
% SEBS
if t/s1 == round(t/s1) && C(n).Task == 0
v(n) = C(n).SEBS(env,Np,S,L,M); % Select next step
d(n,:) = env.Vm(v(n),:) - C(n).X(1:2);
% Share Intention
view = env.A(env.Vmap(v(n)),:);
view(1,env.Vmap(v(n))) = 1;
S(logical(view)) = S(logical(view)) + 1;
elseif t/s == round(t/s) && C(n).Task == 1 && ~firstStep
if randi([0,100])/100 > err && abs(C(n).X(1)-x(1)) < C(n).FoV && abs(C(n).X(2)-x(2)) < C(n).FoV
r = C(n).FoV*C(n).eFoV;
R = r^2*eye(2);
y = (H*x' + sqrt(R)*randn(2,1))';
nL = 0;
else
y = [0 0]; % loss of a measure
nL = nL+1;
end
% Predicton
[x_pred, P_pred] = kalman(A,H,Q,R,y,x_pred,P_pred);
d(n,:) = x_pred(1:2) - C(n).X(1:2);
sd = sqrt(P_pred);
sdMax = max((sd(1,1)+sd(2,2))/2 , T*(sd(3,3)+sd(4,4))/2);
% TRACKING LOSS
% verify if the prediction is inside the allowed area
in = inpolygon(x_pred(1),x_pred(2),B(:,1),B(:,2));
in1 = inpolygon(x_pred(1),x_pred(2),NFZ2{1}(:,1),NFZ2{1}(:,2));
in2 = inpolygon(x_pred(1),x_pred(2),NFZ2{2}(:,1),NFZ2{2}(:,2));
if ~in || in1 || in2 || nL>5
C(n).Task = 0;
Np = Np+1;
index = find(ismembertol(env.V,round(C(n).X(1:2)),'ByRows',true));
env.addHeat(3,1,1,index);
index1 = logical(env.Vmap==index);
if sum(index1) == 0
index = find(env.A(index,:));
v(n) = find(env.Vmap==index(1));
d(n,:) = env.Vm(v(n),:) - C(n).X(1:2);
else
v(n) = find(index1);
d(n,:) = env.Vm(v(n),:) - C(n).X(1:2);
end
detected = false;
end
end
end
% MOVE
for n = 1:NoC
if C(n).Task == 0
C(n).V = v(n);
C(n).X(1:2) = C(n).X(1:2) + d(n,:)/s1;
if C(n).X(3) - C(n).zMax > 0.01
z = C(n).zMax;
sim('control');
hctrl.h0 = symout.data(end,2);
hctrl.vh0 = symout.data(end,1);
C(n).updateZ(hctrl.h0);
else
C(n).updateZ(C(n).zMax);
end
elseif C(n).Task == 1
C(n).X(1:2) = C(n).X(1:2) + d(n,:)/s;
% ZOOM
if t-tDetect >= Ts*s
[z,k] = C(n).optimalZoom(gamma,sdMax);
zoom = [z;k];
sim('control');
hctrl.h0 = symout.data(end,2);
hctrl.vh0 = symout.data(end,1);
C(n).updateZ(hctrl.h0);
end
end
C(n).plot;
env.IvUpdate(t*Tc,C(n).V) % Update Idleness
env.deheat(C(n).V) % Deheat viewed vertices
end
hold off
pause(Tc)
end