-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRedSimulation.m
602 lines (492 loc) · 21.9 KB
/
RedSimulation.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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
%% Housekeeping
clear
clc
close all
% Note - coordinate axes
% y: North
% x: East
% z: up
% angles: positive clockwise between -pi & pi
%% Constants - angles in degrees
g = 9.81;
Rd = 30;
% Blue parameters
phi_dot_b = 15;
phi_max_b = 45;
theta_dot_b = 15;
theta_max_b = 45;
n_b = 1;
acc_max_b = 2;
dec_max_b = -1;
% Red parameters
phi_dot_r = 15;
phi_max_r = 45;
theta_dot_r = 15;
theta_max_r = 45;
n_r = 1;
acc_max_r = 2;
dec_max_r = -1;
% Initial position
xb = 100;
yb = 10;
zb = 100;
phi_b = 0;
theta_b = 0;
psi_b = 0;
Vb = 10;
xr = 100;
yr = 0;
zr = 100;
phi_r = 0;
theta_r = 0;
psi_r = 0;
Vr = 10;
%% Simulation loop
Timecount = 1;
for time = 1:500
%% Control inputs:
% u_phi = {-1, 0, +1} - Roll left, no roll, roll-right
% u_theta = {-1, 0, +1} - Pitch-down, no pitch, pitch-down
% u_vel = {-1, 0, +1} - Deccelerate, no-acceleration, accelerate
% Blue
u_phi_b = [-1, 0, +1];
u_theta_b = [-1, 0, +1];
u_vel_b = [-1, 0, +1];
% Red
u_phi_r = [-1, 0, +1];
u_theta_r = [-1, 0, +1];
u_vel_r = [-1, 0, +1];
%% Equations of Motion and Score Matrix
% t_0: start of integration
% t_f: end of integration
% dt: timestep
t_0 = 0;
dt = 0.25;
t_f = 1.5;
% Set counter for blue maneuvres
countB = 1;
for i = 1:3 % Blue phi (roll)
for j = 1:3 % Blue theta (pitch)
for ij = 1:3 % Velocity blue
% Set counter for red maneuvres
countR = 1;
for k = 1:3 % Red phi (roll)
for m = 1:3 % Red theta (pitch)
for km = 1:3 % Velocity red
% Velocity adavancements for blue and red
if u_vel_r(km) < 0
a_r = abs(dec_max_r);
elseif u_vel_r(km) == 0
a_r = 0;
elseif u_vel_r(km) > 0
a_r = acc_max_r;
end
if u_vel_b(ij) < 0
a_b = abs(dec_max_b);
elseif u_vel_b(ij) == 0
a_b = 0;
elseif u_vel_b(ij) > 0
a_b = acc_max_b;
end
Vr_new = Vr + u_vel_r(km)*a_r*dt;
Vb_new = Vb + u_vel_b(ij)*a_b*dt;
%% Time Integration
for t=t_0:dt:t_f %Assume V_x and control input is constant during look ahead time
%%%%% BLUE AIRCRAFT
% ROLL
% Advancement of phi; phi_dot - maximum roll rate
phi_b_test = phi_b + u_phi_b(i)*phi_dot_b*dt;
% Phi stays within acceptable values
phi_b_test = max(phi_b_test,-phi_max_b);
phi_b_test = min(phi_b_test, phi_max_b);
if abs(phi_b_test)>180
phi_b_test = wrapTo180(phi_b_test); % Wrap angle
end
% PITCH
% Advancement of theta; theta_dot - maximum pitch rate
theta_b_test = theta_b + u_theta_b(j)*theta_dot_b*dt;
% Theta stays within acceptable values
theta_b_test = max(theta_b_test,-theta_max_b);
theta_b_test = min(theta_b_test, theta_max_b);
if abs(theta_b_test)>180
theta_b_test = wrapTo180(theta_b_test); % Wrap angle
end
% HEADING
psi_dot_b_test = 180/pi*g*n_b*tand(phi_b)/Vb_new;
psi_b_test = psi_b + psi_dot_b_test*dt;
if abs(psi_b_test)>180
psi_b_test = wrapTo180(psi_b_test); % Wrap angle
end
%%%%% RED AIRCRAFT
% ROLL
% Advancement of phi; phi_dot - maximum roll rate
phi_r_test = phi_r + u_phi_r(k)*phi_dot_r*dt;
% Phi stays within acceptable values
phi_r_test = max(phi_r_test,-phi_max_r);
phi_r_test = min(phi_r_test, phi_max_r);
if abs(phi_r_test)>180
phi_r_test = wrapTo180(phi_r_test); % Wrap angle
end
% PITCH
% Advancement of theta; theta_dot - maximum pitch rate
theta_r_test = theta_r + u_theta_r(m)*theta_dot_r*dt;
% Theta stays within acceptable values
theta_r_test = max(theta_r_test,-theta_max_r);
theta_r_test = min(theta_r_test, theta_max_r);
if abs(theta_r_test)>180
theta_r_test = wrapTo180(theta_r_test); % Wrap angle
end
% HEADING
psi_dot_r_test = 180/pi*g*n_r*tand(phi_r_test)/Vr_new;
psi_r_test = psi_r + psi_dot_r_test*dt;
if abs(psi_r_test)>180
psi_r_test = wrapTo180(psi_r_test); % Wrap angle
end
end
% New coordinates are:
xb_test = xb + Vb_new*dt*cosd(theta_b_test)*sind(psi_b_test);
yb_test = yb + Vb_new*dt*cosd(theta_b_test)*cosd(psi_b_test);
zb_test = zb + Vb_new*dt*sind(theta_b_test);
xr_test = xr + Vr_new*dt*cosd(theta_r_test)*sind(psi_r_test);
yr_test = yr + Vr_new*dt*cosd(theta_r_test)*cosd(psi_r_test);
zr_test = zr + Vr_new*dt*sind(theta_r_test);
% Range
R = sqrt((xb_test-xr_test)^2+(yb_test-yr_test)^2+(zb_test-zr_test)^2);
% Angles:
% Angle between Blue-Red line and y-axis:
if yr_test < yb_test
alpha = abs(atand(abs(xr_test-xb_test)/abs(yr_test-yb_test)));
elseif yb_test < yr_test
alpha = abs(atand(abs(xb_test-xr_test)/abs(yb_test-yr_test)));
end
% Angle between Blue-Red line and x-z plane:
vector = [abs(xr_test-xb_test), abs(yr_test-yb_test), abs(zr_test-zb_test)];
norm_vector = norm(vector);
% y_axis = [0,1,0];
% norm_y = norm(y_axis);
% alpha = abs(acosd(sum(vector.*y_axis)/(norm_vector*norm_y)));
% Angle between Blue-Red line and x-y plane:
z_axis = [0,0,1];
norm_z = norm(z_axis);
beta = abs(acosd(sum(vector.*z_axis)/(norm_vector*norm_z)));
% Hence:
ATAh = abs(alpha + psi_b_test);
if ATAh>180
ATAh = abs(wrapTo180(ATAh));
end
AAh = abs(alpha + psi_r_test);
if AAh>180
AAh = abs(wrapTo180(AAh));
end
ATAv = abs((90 - beta) - phi_b_test);
if ATAv>180
ATAv = abs(wrapTo180(ATAv));
end
AAv = abs((90 - beta) - phi_r_test);
if AAv>180
AAv = abs(wrapTo180(AAv));
end
%% Score Function - Orientation
% Horizontally:
% AAh: aspect angle
% ATAh: antenna train angle
% For red: AAh = ATAh = 0 --> Sa = 1
% For blue: AAh = ATAh = 180 --> Sa = -1
Sah = 1 - abs(AAh+ATAh)/180;
% Vertically:
% AAv: aspect angle
% ATAv: antenna train angle
% For red: AA = ATA = 0 --> Sa = 1
% For blue: AA = ATA = 180 --> Sa = -1
Sav = 1-abs(AAv+ATAv)/180;
% Assembled:
Sa = 0.5*(Sav+Sah);
%% Score Function - Range
% R: distance between aircraft
% Rd: desired distance between aircraft
% er: distance error
% k: constant
er = abs(R-Rd);
% Range score for each aircraft
Sr = exp(-er/(180));
%% Score Function - Velocity
% bd: desired velocity
% b0, b1, bc: curve parameters
% Vx: velocity of aircraft
% CVb, CVr: weights for scores
% Rc: combat range
% Rd: desired distance between aircraft
% a: acceleration value
% Combat range is such that constant maximum de/acceleration can be applied for a time
% t and the resulting speed is Vb exactly at Rd.
%Rc = (Vb_new - Vr_new)/a_r + Rd;
Rc = 0.5;
% Value of a is max. acceleration (a_r > 0) if Vr < Vb
% Value of a is max. deceleration (a_r < 0 ) if Vr > Vb
if Vr_new < Vb_new
a_r = acc_max_r;
elseif Vr_new > Vb_new
a_r = dec_max_r;
end
% Estimation of desired V as a function of R for
% red aircraft
if R <= Rd
bd = Vb_new;
elseif R < Rc & R > Rd
bd = a_r*er + Vr_new;
elseif R > Rc
bd = 1; %Guess- bd = max. endurance?
end
b0 = 0.1*bd;
b1 = 0.3*bd;
bc = 3*bd;
% Absolute contribution
Sv_b = (1-((b1-b0)/(b1+Vb_new))^2)*exp(-(Vb_new-bd)^2/bc^2);
Sv_r = (1-((b1-b0)/(b1+Vr_new))^2)*exp(-(Vr_new-bd)^2/bc^2);
% Function weights
CVb = 1;
CVr = 1;
% For red: Sv = 1
% For blue: Sv = -1
Sv = CVr*Sv_r - CVb*Sv_b;
%% Score Function - Terrain
% ad: desired clearance
% a0, a1, ac: curve parameters
% z: altitude of aircraft
% CHb, CHr: weights for scores
ad = 2; %Guess - must revise
a1 = 0.5; %Guess - must revise
a0 = (2*a1+ad)/300;
ac = ad-a1;
% To avoid penalisisng high altitudes, evaluate function at ad (S = 1) if z > ad:
% For terrain following, penalise when z > ad.
if zr >= ad
Sh_r = 1;
elseif zr < ad
Sh_r = (1-((a0-b1)/(zr-a1))^2)*exp(-(zr-ad)^2/ac^2);
end
if zb >= ad
Sh_b = 1;
elseif zb < ad;
Sh_b = (1-((a0-b1)/(zb-a1))^2)*exp(-(zb-ad)^2/ac^2);
end
% Function weights
CHb = 1;
CHr = 1;
% For red: Sv = 1
% For blue: Sv = -1
Sh = CHr*Sh_r - CHb*Sh_b;
%% Scoring Function - Assembled
%S(countB,countR) = Sr*(Sa+Sh); %+ Sv;
S(countB,countR) = Sah;
% Advance counter for red maneuvres
countR = countR + 1;
end
end
end
% Advance counter for blue maneuvres
countB = countB + 1;
end
end
end
%% Max-min search - red wants to maximize, blue wants to minimize S
[rowS, colS] = size(S);
for i = 1:colS
mins(i) = min(S(:,i));
end
maxs = max(mins);
[~, col] = find(S==maxs);
col = unique(col);
if length(col) ~= 1
col = col(ceil(rand*length(col)));
%col = round(median(col));
end
%% Min-max search - blue wants to minimize
[rowS, colS] = size(S);
for i = 1:rowS
maxs(i) = max(S(:,i));
end
mins = min(maxs);
[row, ~] = find(S==mins);
row = unique(row);
if length(row) ~= 1
row = row(ceil(rand*length(row)));
%row = row(1);
end
S_count(Timecount) = S(row, col);
% Locate control inputs that lead to the obtained row & column
% RED
% Velocity
if mod(col,3)/3==0
u_vel_r_fin = 1;
elseif mod(col,3)/3== 2/3
u_vel_r_fin = 0;
elseif mod(col,3)/3== 1/3
u_vel_r_fin = -1;
end
% Phi
if ceil(col/9) == 3
u_phi_r_fin = 1;
elseif ceil(col/9) == 2
u_phi_r_fin = 0;
elseif ceil(col/9) == 1
u_phi_r_fin = -1;
end
% Theta
if ceil(ceil(col/3)/3) == 3
u_theta_r_fin = 1;
elseif ceil(ceil(col/3)/3) == 2
u_theta_r_fin = 0;
elseif ceil(ceil(col/3)/3) == 1
u_theta_r_fin = -1;
end
% BLUE
% Velocity
if mod(row,3)/3==0
u_vel_b_fin = 1;
elseif mod(row,3)/3== 2/3
u_vel_b_fin = 0;
elseif mod(row,3)/3== 1/3
u_vel_b_fin = -1;
end
% Phi
if ceil(row/9) == 3
u_phi_b_fin = 1;
elseif ceil(row/9) == 2
u_phi_b_fin = 0;
elseif ceil(row/9) == 1
u_phi_b_fin = -1;
end
% Theta
if ceil(ceil(row/3)/3) == 3
u_theta_b_fin = 1;
elseif ceil(ceil(row/3)/3) == 2
u_theta_b_fin = 0;
elseif ceil(ceil(row/3)/3) == 1
u_theta_b_fin = -1;
end
% u_vel_b_fin = 0;
% u_phi_b_fin = 0;
% u_theta_b_fin = 0;
%% Recalculate new positions
% Velocity adavancements for red
if u_vel_r_fin < 0
a_r = abs(dec_max_r);
elseif u_vel_r_fin == 0
a_r = 0;
elseif u_vel_r_fin > 0
a_r = acc_max_r;
end
Vr_new_fin = Vr + u_vel_r_fin*a_r*dt;
% Velocity adavancements for blue
if u_vel_b_fin < 0
a_b = abs(dec_max_b);
elseif u_vel_b_fin == 0
a_b = 0;
elseif u_vel_b_fin > 0
a_b = acc_max_b;
end
Vb_new_fin = Vb + u_vel_b_fin*a_b*dt;
%% Time Integration
for t=t_0:dt:t_f %Assume V_x and control input is constant during look ahead time
%%%%% BLUE AIRCRAFT
% ROLL
% Advancement of phi; phi_dot - maximum roll rate
phi_b = phi_b + u_phi_b_fin*phi_dot_b*dt;
% Phi stays within acceptable values
phi_b = max(phi_b,-phi_max_b);
phi_b = min(phi_b, phi_max_b);
if abs(phi_r)>180
phi_b = wrapTo180(phi_b); % Wrap angle
end
% PITCH
% Advancement of theta; theta_dot - maximum pitch rate
theta_b = theta_b + u_theta_b_fin*theta_dot_b*dt;
% Theta stays within acceptable values
theta_b = max(theta_b,-theta_max_b);
theta_b = min(theta_b, theta_max_b);
if abs(theta_b)>180
theta_b = wrapTo180(theta_b); % Wrap angle
end
% HEADING
psi_dot_b = 180/pi*g*n_b*tand(phi_b)/Vb_new_fin;
psi_b = psi_b + psi_dot_b*dt;
if abs(psi_b)>180
psi_b = wrapTo180(psi_b); % Wrap angle
end
%%%%% RED AIRCRAFT
% ROLL
% Advancement of phi; phi_dot - maximum roll rate
phi_r = phi_r + u_phi_r_fin*phi_dot_r*dt;
% Phi stays within acceptable values
phi_r = max(phi_r,-phi_max_r);
phi_r = min(phi_r, phi_max_r);
if abs(phi_r)>180
phi_r = wrapTo180(phi_r); % Wrap angle
end
% PITCH
% Advancement of theta; theta_dot - maximum pitch rate
theta_r = theta_r + u_theta_r_fin*theta_dot_r*dt;
% Theta stays within acceptable values
theta_r = max(theta_r,-theta_max_r);
theta_r = min(theta_r, theta_max_r);
if abs(theta_r)>180
theta_r = wrapTo180(theta_r); % Wrap angle
end
% HEADING
psi_dot_r = 180/pi*g*n_b*tand(phi_r)/Vr_new_fin;
psi_r = psi_r + psi_dot_r*dt;
if abs(psi_r)>0
psi_r = wrapTo180(psi_r); % Wrap angle
end
end
% New coordinates are:
xr_fin = xr + Vr_new_fin*dt*cosd(theta_r)*sind(psi_r);
yr_fin = yr + Vr_new_fin*dt*cosd(theta_r)*cosd(psi_r);
zr_fin = zr + Vr_new_fin*dt*sind(theta_r);
xb_fin = xb + Vb_new_fin*dt*cosd(theta_b)*sind(psi_b);
yb_fin = yb + Vb_new_fin*dt*cosd(theta_b)*cosd(psi_b);
zb_fin = zb + Vb_new_fin*dt*sind(theta_b);
% Re-position aircraft to restart loop
xr_pos(Timecount) = xr_fin;
yr_pos(Timecount) = yr_fin;
zr_pos(Timecount) = zr_fin;
xr = xr_fin;
yr = yr_fin;
zr = zr_fin;
% Re-position aircraft to restart loop
xb_pos(Timecount) = xb_fin;
yb_pos(Timecount) = yb_fin;
zb_pos(Timecount) = zb_fin;
xb = xb_fin;
yb = yb_fin;
zb = zb_fin;
subplot(2,1,1);
plot3(xr_pos,yr_pos,zr_pos,'r')
hold on
plot3(xb_pos,yb_pos,zb_pos,'b')
grid on
axis square
subplot(2,1,2);
plot(Timecount, S_count(Timecount),"-xk")
hold on
grid on
axis square
pause(0.5)
Timecount = Timecount + 1;
end
%% Plot postion of aircraft
plot3(xr_pos,yr_pos,zr_pos,'r')
hold on
plot3(xb_pos,yb_pos,zb_pos,'b')
grid on
pause(0.5)
figure
subplot(2,1,1);
plot(xr_pos,yr_pos,'r');
hold on
plot(xb_pos,yb_pos,'b');
subplot(2,1,2);
plot(xr_pos,zr_pos,'r');
hold on
plot(xb_pos,zb_pos,'b');