-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvehicle_task_type.adb
542 lines (513 loc) · 23.5 KB
/
vehicle_task_type.adb
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
-- Have a discussion about stage d with Xu Si u6714758 and we got the similar idea about controlling vehicles number
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
-------------------------The best Stage b & c-----------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
-- with Exceptions; use Exceptions;
-- with Vectors_3D; use Vectors_3D;
-- with Vehicle_Interface; use Vehicle_Interface;
-- with Swarm_Structures_Base; use Swarm_Structures_Base;
-- with Vehicle_Message_Type; use Vehicle_Message_Type;
-- with Real_Type; use Real_Type;
-- with Ada.Numerics; use Ada.Numerics;
-- with Ada.Real_Time; use Ada.Real_Time;
--
-- package body Vehicle_Task_Type is
--
-- task body Vehicle_Task is
--
-- Vehicle_No : Positive;
-- Vehicle_Class : Natural;
-- Find_It : Boolean := False;
-- Angle_Vehicle : Long_Float := 0.0;
-- Outer_Message : Inter_Vehicle_Messages;
-- Local_Message : Inter_Vehicle_Messages;
--
-- -- The outer four circles
-- function Outer_Circle (radius : Real_Type.Real; distance : Real_Type.Real) return Vectors_3D.Vector_3D is
-- begin
-- if Vehicle_Class = 0 then -- XZ circle
-- return (Local_Message.Globe_Position (x) + radius * Real_Elementary_Functions.Sin (Angle_Vehicle) + distance,
-- Local_Message.Globe_Position (y) + radius * Real_Elementary_Functions.Cos (Angle_Vehicle),
-- Local_Message.Globe_Position (z));
-- elsif Vehicle_Class = 1 then -- XZ circle
-- return (Local_Message.Globe_Position (x) + radius * Real_Elementary_Functions.Cos (Angle_Vehicle) - distance,
-- Local_Message.Globe_Position (y) + radius * Real_Elementary_Functions.Sin (Angle_Vehicle),
-- Local_Message.Globe_Position (z));
-- elsif Vehicle_Class = 2 then -- XZ circle
-- return (Local_Message.Globe_Position (x),
-- Local_Message.Globe_Position (y) + radius * Real_Elementary_Functions.Cos (Angle_Vehicle),
-- Local_Message.Globe_Position (z) + radius * Real_Elementary_Functions.Sin (Angle_Vehicle) + distance);
-- else -- XZ circle
-- return (Local_Message.Globe_Position (x),
-- Local_Message.Globe_Position (y) + radius * Real_Elementary_Functions.Sin (Angle_Vehicle),
-- Local_Message.Globe_Position (z) + radius * Real_Elementary_Functions.Cos (Angle_Vehicle) - distance);
-- end if;
-- end Outer_Circle;
--
-- -- The inner circle
-- function Inner_Circle (radius : Real_Type.Real) return Vectors_3D.Vector_3D is
-- begin
-- return (Local_Message.Globe_Position (x) + radius * Real_Elementary_Functions.Sin (Angle_Vehicle),
-- Local_Message.Globe_Position (y),
-- Local_Message.Globe_Position (z) + radius * Real_Elementary_Functions.Cos (Angle_Vehicle));
-- end Inner_Circle;
--
-- -- Checking the time to get the newest message
-- procedure Check_Info is
-- begin
-- -- if the receiving time is newest then updating the local information
-- if Outer_Message.Time_Checker > Local_Message.Time_Checker then
-- Local_Message := Outer_Message;
-- elsif Outer_Message.Time_Checker < Local_Message.Time_Checker then
-- Outer_Message := Local_Message;
-- end if;
-- end Check_Info;
--
-- begin
--
-- accept Identify (Set_Vehicle_No : Positive; Local_Task_Id : out Task_Id) do
-- Vehicle_No := Set_Vehicle_No;
-- Local_Task_Id := Current_Task;
-- Vehicle_Class := Vehicle_No mod 4; -- from 0 to 3
-- Angle_Vehicle := Long_Float (Vehicle_No mod 16) * (Pi / 8.0);
-- end Identify;
--
-- select
--
-- Flight_Termination.Stop;
--
-- then abort
--
-- Outer_task_loop : loop
--
-- Wait_For_Next_Physics_Update;
--
-- -- Try to find the energy globe
-- declare
-- Try_Find_Globes : constant Energy_Globes := Energy_Globes_Around;
-- begin
-- for i in Try_Find_Globes'Range loop
-- Find_It := Try_Find_Globes (i).Position'Valid_Scalars;
-- if Find_It then
-- Outer_Message.Globe_Position := Try_Find_Globes (i).Position;
-- -- only generate new time point when it finds the position of the globe
-- Outer_Message.Time_Checker := Clock;
-- Outer_Message.Charge_Avaliable := True;
-- Local_Message := Outer_Message;
-- Send (Outer_Message);
-- Find_It := False;
-- exit;
-- end if;
-- end loop;
-- end;
--
-- Set_Throttle (0.9);
--
-- -- Updating the angle for each physics update
-- Angle_Vehicle := Angle_Vehicle + Pi / 60.0;
--
-- -- The emergency case when the charge level below 30%
-- if Current_Charge < 0.3 then
-- if Messages_Waiting then
-- Receive (Outer_Message);
-- Check_Info;
-- end if;
-- Outer_Message.Charge_Avaliable := False;
-- Local_Message.Charge_Avaliable := False;
-- Send (Outer_Message);
-- Set_Destination (Local_Message.Globe_Position + Local_Message.globe_velocity * 0.2);
-- Set_Throttle (Full_Throttle);
--
-- -- The normal case
-- elsif Messages_Waiting then
-- Receive (Outer_Message);
-- Check_Info;
-- if Current_Charge < 0.6 then
-- if Local_Message.Charge_Avaliable then
-- Outer_Message.Charge_Avaliable := False;
-- Local_Message.Charge_Avaliable := False;
-- Set_Destination (Inner_Circle (0.16));
-- Set_Throttle (0.6);
-- else
-- Set_Destination (Inner_Circle (0.16));
-- Set_Throttle (0.6);
-- end if;
-- elsif Current_Charge < 0.7 then
-- Set_Destination (Inner_Circle (0.1));
-- else
-- Set_Destination (Outer_Circle (0.08, 0.16));
-- Set_Throttle (0.6);
-- end if;
-- Send (Outer_Message);
-- else
-- Set_Destination (Outer_Circle (0.08, 0.16));
-- end if;
--
-- end loop Outer_task_loop;
-- end select;
-- exception
-- when E : others => Show_Exception (E);
-- end Vehicle_Task;
-- end Vehicle_Task_Type;
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
-------------------------------Stage d------------------------------------
------------Also support stage b & c but not very good pattern------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
with Exceptions; use Exceptions;
with Vectors_3D; use Vectors_3D;
with Vehicle_Interface; use Vehicle_Interface;
with Swarm_Structures_Base; use Swarm_Structures_Base;
with Vehicle_Message_Type; use Vehicle_Message_Type;
with Real_Type; use Real_Type;
with Ada.Numerics; use Ada.Numerics;
with Ada.Real_Time; use Ada.Real_Time;
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Containers; use Ada.Containers;
package body Vehicle_Task_Type is
task body Vehicle_Task is
Vehicle_No : Positive;
Vehicle_Class : Natural;
Find_It : Boolean := False;
Angle_Vehicle : Long_Float := 0.0;
Outer_Message : Inter_Vehicle_Messages;
Local_Message : Inter_Vehicle_Messages;
Target_No : Natural;
-- The outer four circles
function Outer_Circle (radius : Real_Type.Real; distance : Real_Type.Real) return Vectors_3D.Vector_3D is
begin
if Vehicle_Class = 0 then -- XZ circle
return (Local_Message.Globe_Position (x) + radius * Real_Elementary_Functions.Sin (Angle_Vehicle) + distance,
Local_Message.Globe_Position (y) + radius * Real_Elementary_Functions.Cos (Angle_Vehicle),
Local_Message.Globe_Position (z));
elsif Vehicle_Class = 1 then -- XZ circle
return (Local_Message.Globe_Position (x) + radius * Real_Elementary_Functions.Cos (Angle_Vehicle) - distance,
Local_Message.Globe_Position (y) + radius * Real_Elementary_Functions.Sin (Angle_Vehicle),
Local_Message.Globe_Position (z));
elsif Vehicle_Class = 2 then -- XZ circle
return (Local_Message.Globe_Position (x),
Local_Message.Globe_Position (y) + radius * Real_Elementary_Functions.Cos (Angle_Vehicle),
Local_Message.Globe_Position (z) + radius * Real_Elementary_Functions.Sin (Angle_Vehicle) + distance);
else -- XZ circle
return (Local_Message.Globe_Position (x),
Local_Message.Globe_Position (y) + radius * Real_Elementary_Functions.Sin (Angle_Vehicle),
Local_Message.Globe_Position (z) + radius * Real_Elementary_Functions.Cos (Angle_Vehicle) - distance);
end if;
end Outer_Circle;
-- The inner circle
function Inner_Circle (radius : Real_Type.Real) return Vectors_3D.Vector_3D is
begin
return (Local_Message.Globe_Position (x) + radius * Real_Elementary_Functions.Sin (Angle_Vehicle),
Local_Message.Globe_Position (y),
Local_Message.Globe_Position (z) + radius * Real_Elementary_Functions.Cos (Angle_Vehicle));
end Inner_Circle;
-- Checking the time to get the newest message
procedure Check_Info is
begin
-- if the receiving time is newest then updating the local information
if Outer_Message.Time_Checker > Local_Message.Time_Checker then
-- Checking the survival set's length
if Natural (Outer_Message.Set_For_Alive.Length) < Target_No then
Outer_Message.Set_For_Alive.Include (Vehicle_No);
Local_Message := Outer_Message;
elsif Natural (Outer_Message.Set_For_Alive.Length) > Target_No then
Outer_Message.Set_For_Alive.Exclude (Vehicle_No);
Local_Message := Outer_Message;
else
Local_Message := Outer_Message;
end if;
-- Upload local information to override the order one
elsif Outer_Message.Time_Checker < Local_Message.Time_Checker then
Outer_Message := Local_Message;
end if;
end Check_Info;
begin
accept Identify (Set_Vehicle_No : Positive; Local_Task_Id : out Task_Id) do
Vehicle_No := Set_Vehicle_No;
Local_Task_Id := Current_Task;
Vehicle_Class := Vehicle_No mod 4; -- from 0 to 3
Angle_Vehicle := Long_Float (Vehicle_No mod 16) * (Pi / 8.0);
Target_No := Target_No_of_Elements;
end Identify;
Set_Throttle (0.9);
select
Flight_Termination.Stop;
then abort
Outer_task_loop : loop
Wait_For_Next_Physics_Update;
-- Try to find the energy globe
declare
Try_Find_Globes : constant Energy_Globes := Energy_Globes_Around;
begin
for i in Try_Find_Globes'Range loop
Find_It := Try_Find_Globes (i).Position'Valid_Scalars;
if Find_It then
Outer_Message.Globe_Position := Try_Find_Globes (i).Position;
-- only generate new time point when it finds the position of the globe
Outer_Message.Time_Checker := Clock;
Outer_Message.Charge_Avaliable := True;
Local_Message := Outer_Message;
Put_Line ("How many " & Count_Type'Image (Outer_Message.Set_For_Alive.Length) & " alive");
Send (Outer_Message);
Find_It := False;
exit;
end if;
end loop;
end;
Set_Throttle (0.9);
-- The emergency case when charge level below 40%
if Current_Charge < 0.4 then
if Messages_Waiting then
Receive (Outer_Message);
Check_Info;
end if;
-- In order to improve performance in here I use if else
-- But it's ok to uncomment it.
-- while Messages_Waiting loop
-- Receive (Outer_Message);
-- Check_Info;
-- end loop;
if Local_Message.Set_For_Alive.Contains (Vehicle_No) then
Outer_Message.Charge_Avaliable := False;
Local_Message.Charge_Avaliable := False;
Send (Outer_Message);
Set_Destination (Local_Message.Globe_Position + Local_Message.globe_velocity * 0.2);
Set_Throttle (Full_Throttle);
else
Set_Throttle (Full_Throttle);
end if;
-- The normal case
elsif Messages_Waiting then
Receive (Outer_Message);
Check_Info;
if Local_Message.Set_For_Alive.Contains (Vehicle_No) then
if Current_Charge < 0.6 then
if Local_Message.Charge_Avaliable then
Outer_Message.Charge_Avaliable := False;
Local_Message.Charge_Avaliable := False;
Set_Destination (Inner_Circle (Long_Float (Current_Charge) * 0.1));
Set_Throttle (0.6);
else
Set_Destination (Inner_Circle (Long_Float (Current_Charge) * 0.1));
Set_Throttle (0.6);
end if;
elsif Current_Charge < 0.7 then
Set_Destination (Inner_Circle (Long_Float (Current_Charge) * 0.1));
else
Set_Destination (Outer_Circle (Long_Float (Current_Charge) * 0.05 / 2.0, Long_Float (Current_Charge) * 0.1));
Set_Throttle (0.6);
end if;
Angle_Vehicle := Angle_Vehicle + Pi / 120.0;
Send (Outer_Message);
else
Set_Throttle (Full_Throttle);
end if;
else
Set_Throttle (Full_Throttle);
end if;
end loop Outer_task_loop;
end select;
exception
when E : others => Show_Exception (E);
end Vehicle_Task;
end Vehicle_Task_Type;
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
-------------------Attempt 1 (only support stage a)-----------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
-- with Exceptions; use Exceptions;
-- with Vectors_3D; use Vectors_3D;
-- with Vehicle_Interface; use Vehicle_Interface;
-- with Swarm_Structures_Base; use Swarm_Structures_Base;
--
-- package body Vehicle_Task_Type is
--
-- gloe : Vector_3D;
--
-- task body Vehicle_Task is
--
-- Vehicle_No : Positive; -- You will want to take the pragma out, once you use the "Vehicle_No"
-- pragma Unreferenced (Vehicle_No);
-- find_it : Boolean := False;
--
-- begin
--
-- accept Identify (Set_Vehicle_No : Positive; Local_Task_Id : out Task_Id) do
-- Vehicle_No := Set_Vehicle_No;
-- Local_Task_Id := Current_Task;
-- end Identify;
--
-- select
--
-- Flight_Termination.Stop;
--
-- then abort
--
-- -- Put_Line (Float'Image (Float (vehicle_gas_class) * 0.09));
--
-- Outer_task_loop : loop
--
-- Wait_For_Next_Physics_Update;
--
-- -- Try to find the energy globe
-- declare
-- try_find_globes : constant Energy_Globes := Energy_Globes_Around;
-- begin
-- for i in try_find_globes'Range loop
-- find_it := try_find_globes (i).Position'Valid_Scalars;
-- if find_it then
-- gloe := try_find_globes (i).Position;
-- find_it := False;
-- exit;
-- end if;
-- end loop;
-- end;
--
-- Set_Throttle (0.9);
--
-- -- Different cases for different charge level
-- if Current_Charge < 0.3 then
-- Set_Destination (gloe);
-- Set_Throttle (Full_Throttle);
-- else
-- Set_Destination (gloe + (0.1, 0.1, 0.1));
-- Set_Throttle (0.6);
-- end if;
--
-- end loop Outer_task_loop;
-- end select;
-- exception
-- when E : others => Show_Exception (E);
-- end Vehicle_Task;
-- end Vehicle_Task_Type;
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
-------------------Attempt 2 (support stage b & c)------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
-- with Exceptions; use Exceptions;
-- with Vectors_3D; use Vectors_3D;
-- with Vehicle_Interface; use Vehicle_Interface;
-- with Swarm_Structures_Base; use Swarm_Structures_Base;
-- with Vehicle_Message_Type; use Vehicle_Message_Type;
-- with Real_Type; use Real_Type;
-- with Ada.Numerics; use Ada.Numerics;
-- with Ada.Real_Time; use Ada.Real_Time;
--
-- package body Vehicle_Task_Type is
--
-- task body Vehicle_Task is
--
-- Vehicle_No : Positive;
-- Vehicle_Class : Natural;
-- pragma Unreferenced (Vehicle_Class);
-- Find_It : Boolean := False;
-- Angle_Vehicle : Long_Float := 0.0;
-- Outer_Message : Inter_Vehicle_Messages;
-- Local_Message : Inter_Vehicle_Messages;
--
-- -- The inner circle
-- function Inner_Circle (radius : Real_Type.Real) return Vectors_3D.Vector_3D is
-- begin
-- return (Local_Message.Globe_Position (x) + radius * Real_Elementary_Functions.Sin (Angle_Vehicle),
-- Local_Message.Globe_Position (y),
-- Local_Message.Globe_Position (z) + radius * Real_Elementary_Functions.Cos (Angle_Vehicle));
-- end Inner_Circle;
--
-- -- Checking timestamps to get the newest message
-- procedure Check_Info is
-- begin
-- -- if the receiving time is newest then updating the local information
-- if Outer_Message.Time_Checker > Local_Message.Time_Checker then
-- Local_Message := Outer_Message;
-- elsif Outer_Message.Time_Checker < Local_Message.Time_Checker then
-- Outer_Message := Local_Message;
-- end if;
-- end Check_Info;
--
-- begin
--
-- accept Identify (Set_Vehicle_No : Positive; Local_Task_Id : out Task_Id) do
-- Vehicle_No := Set_Vehicle_No;
-- Local_Task_Id := Current_Task;
-- Vehicle_Class := Vehicle_No mod 4; -- from 0 to 3
-- Angle_Vehicle := Long_Float (Vehicle_No mod 16) * (Pi / 8.0);
-- end Identify;
--
-- select
--
-- Flight_Termination.Stop;
--
-- then abort
--
-- Outer_task_loop : loop
--
-- Wait_For_Next_Physics_Update;
--
-- -- Try to find the energy globe
-- declare
-- Try_Find_Globes : constant Energy_Globes := Energy_Globes_Around;
-- begin
-- for i in Try_Find_Globes'Range loop
-- Find_It := Try_Find_Globes (i).Position'Valid_Scalars;
-- if Find_It then
-- Outer_Message.Globe_Position := Try_Find_Globes (i).Position;
-- -- only generate new time point when it finds the position of the globe
-- Outer_Message.Time_Checker := Clock;
-- Outer_Message.Charge_Avaliable := True;
-- Local_Message := Outer_Message;
-- Send (Outer_Message);
-- Find_It := False;
-- exit;
-- end if;
-- end loop;
-- end;
--
-- Set_Throttle (0.9);
--
-- -- Emergency case when current charge level is below 30%
-- if Current_Charge < 0.3 then
-- Receive (Outer_Message);
-- Check_Info;
-- Outer_Message.Charge_Avaliable := False;
-- Local_Message.Charge_Avaliable := False;
-- Set_Destination (Local_Message.Globe_Position);
-- Set_Throttle (Full_Throttle);
--
-- -- Normal cases
-- elsif Messages_Waiting then
-- Receive (Outer_Message);
-- Check_Info;
-- if Current_Charge < 0.5 then
-- Outer_Message.Charge_Avaliable := False;
-- Local_Message.Charge_Avaliable := False;
-- Set_Destination (Local_Message.Globe_Position);
-- Set_Throttle (Full_Throttle);
-- elsif Current_Charge < 0.7 then
-- Set_Destination (Inner_Circle (Long_Float (Current_Charge) * 0.4));
-- else
-- Set_Destination (Inner_Circle (Long_Float (Current_Charge) * 0.4));
-- end if;
-- Send (Outer_Message);
-- end if;
--
-- Angle_Vehicle := Angle_Vehicle + Pi / 120.0;
--
-- end loop Outer_task_loop;
-- end select;
-- exception
-- when E : others => Show_Exception (E);
-- end Vehicle_Task;
-- end Vehicle_Task_Type;